Freeciv-3.4
Loading...
Searching...
No Matches
dataio_raw.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14/*
15 * The DataIO module provides a system independent (endianness and
16 * sizeof(int) independent) way to write and read data. It supports
17 * multiple datas which are collected in a buffer. It provides
18 * recognition of error cases like "not enough space" or "not enough
19 * data".
20 */
21
22#ifdef HAVE_CONFIG_H
23#include <fc_config.h>
24#endif
25
26#include "fc_prehdrs.h"
27
28#include <limits.h>
29#include <math.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#ifdef FREECIV_HAVE_SYS_TYPES_H
36#include <sys/types.h>
37#endif
38#ifdef HAVE_SYS_SOCKET_H
39#include <sys/socket.h>
40#endif
41#ifdef HAVE_NETINET_IN_H
42#include <netinet/in.h>
43#endif
44#ifdef HAVE_ARPA_INET_H
45#include <arpa/inet.h>
46#endif
47
48/* utility */
49#include "bitvector.h"
50#include "capability.h"
51#include "log.h"
52#include "mem.h"
53#include "support.h"
54
55/* common */
56#include "events.h"
57#include "player.h"
58#include "requirements.h"
59#include "tech.h"
60#include "worklist.h"
61
62/* common/aicore */
63#include "cm.h"
64
65#include "dataio.h"
66
67static bool get_conv(char *dst, size_t ndst, const char *src,
68 size_t nsrc);
69
72
73/* Uncomment to make field range tests to asserts, fatal with -F */
74/* #define FIELD_RANGE_ASSERT */
75
76#if defined(FREECIV_TESTMATIC) && !defined(FIELD_RANGE_ASSERT)
77#define FIELD_RANGE_ASSERT
78#endif
79
80#ifdef FIELD_RANGE_ASSERT
81/* Do log_error() first, so we get its output even if
82 * fc_assert() is fatal.
83 * fc_assert() before the _action_ might fix
84 * what we should find out to fail. */
85#define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
86 if (_test_) { \
87 log_error(_format_, ## __VA_ARGS__); \
88 fc_assert(!(_test_)); \
89 _action_ \
90 }
91#else /* FIELD_RANGE_ASSERT */
92#define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
93 if (_test_) { \
94 log_error(_format_, ## __VA_ARGS__); \
95 _action_ \
96 }
97#endif /* FIELD_RANGE_ASSERT */
98
99/**********************************************************************/
106
107/**********************************************************************/
111static bool get_conv(char *dst, size_t ndst, const char *src,
112 size_t nsrc)
113{
114 size_t len = nsrc; /* Length to copy, not including null */
115 bool ret = TRUE;
116
117 if (ndst > 0 && len >= ndst) {
118 ret = FALSE;
119 len = ndst - 1;
120 }
121
122 memcpy(dst, src, len);
123 dst[len] = '\0';
124
125 return ret;
126}
127
128/**********************************************************************/
135
136/**********************************************************************/
139bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src,
140 size_t nsrc)
141{
142 return get_conv_callback(dst, ndst, src, nsrc);
143}
144
145/**********************************************************************/
148static bool enough_space(struct raw_data_out *d_out, size_t size)
149{
150 if (d_out->current + size > d_out->dest_size) {
151 d_out->too_short = TRUE;
152
153 return FALSE;
154 } else {
155 d_out->used = MAX(d_out->used, d_out->current + size);
156
157 return TRUE;
158 }
159}
160
161/**********************************************************************/
164static bool enough_data(struct data_in *din, size_t size)
165{
166 return dio_input_remaining(din) >= size;
167}
168
169/**********************************************************************/
174 size_t dest_size)
175{
176 d_out->dest = destination;
177 d_out->dest_size = dest_size;
178 d_out->current = 0;
179 d_out->used = 0;
180 d_out->too_short = FALSE;
181}
182
183/**********************************************************************/
187{
188 return d_out->used;
189}
190
191/**********************************************************************/
196{
197 d_out->current = 0;
198}
199
200/**********************************************************************/
204void dio_input_init(struct data_in *din, const void *src, size_t src_size)
205{
206 din->src = src;
207 din->src_size = src_size;
208 din->current = 0;
209}
210
211/**********************************************************************/
216{
217 din->current = 0;
218}
219
220/**********************************************************************/
224{
225 return din->src_size - din->current;
226}
227
228/**********************************************************************/
232{
233 switch (type) {
234 case DIOT_UINT8:
235 case DIOT_SINT8:
236 return 1;
237 case DIOT_UINT16:
238 case DIOT_SINT16:
239 return 2;
240 case DIOT_UINT32:
241 case DIOT_SINT32:
242 return 4;
243 case DIOT_LAST:
244 break;
245 }
246
247 fc_assert_msg(FALSE, "data_type %d not handled.", type);
248 return 0;
249}
250
251/**********************************************************************/
254bool dio_input_skip(struct data_in *din, size_t size)
255{
256 if (enough_data(din, size)) {
257 din->current += size;
258 return TRUE;
259 } else {
260 return FALSE;
261 }
262}
263
264/**********************************************************************/
267int dio_put_uint8_raw(struct raw_data_out *d_out, int value)
268{
269 uint8_t x = value;
270
271 FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_1_byte);
272
273 FIELD_RANGE_TEST((int) x != value, ,
274 "Trying to put %d into 8 bits; "
275 "it will result %d at receiving side.",
276 value, (int) x);
277
278 if (enough_space(d_out, 1)) {
279 memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 1);
280 d_out->current++;
281 }
282
283 return 0;
284}
285
286/**********************************************************************/
289int dio_put_uint16_raw(struct raw_data_out *d_out, int value)
290{
291 uint16_t x = htons(value);
292
294
295 FIELD_RANGE_TEST((int) ntohs(x) != value, ,
296 "Trying to put %d into 16 bits; "
297 "it will result %d at receiving side.",
298 value, (int) ntohs(x));
299
300 if (enough_space(d_out, 2)) {
301 memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 2);
302 d_out->current += 2;
303 }
304
305 return 0;
306}
307
308/**********************************************************************/
311int dio_put_uint32_raw(struct raw_data_out *d_out, int value)
312{
313 uint32_t x = htonl(value);
314
316
317 FIELD_RANGE_TEST((int) ntohl(x) != value, ,
318 "Trying to put %d into 32 bits; "
319 "it will result %d at receiving side.",
320 value, (int) ntohl(x));
321
322 if (enough_space(d_out, 4)) {
323 memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 4);
324 d_out->current += 4;
325 }
326
327 return 0;
328}
329
330/**********************************************************************/
334 int value)
335{
336 switch (type) {
337 case DIOT_UINT8:
338 return dio_put_uint8_raw(d_out, value);
339 case DIOT_UINT16:
340 return dio_put_uint16_raw(d_out, value);
341 case DIOT_UINT32:
342 return dio_put_uint32_raw(d_out, value);
343 case DIOT_SINT8:
344 return dio_put_sint8_raw(d_out, value);
345 case DIOT_SINT16:
346 return dio_put_sint16_raw(d_out, value);
347 case DIOT_SINT32:
348 return dio_put_sint32_raw(d_out, value);
349 case DIOT_LAST:
350 break;
351 }
352
353 fc_assert_msg(FALSE, "data_type %d not handled.", type);
354
355 return -1;
356}
357
358/**********************************************************************/
361int dio_put_sint8_raw(struct raw_data_out *d_out, int value)
362{
363 return dio_put_uint8_raw(d_out, 0 <= value ? value : value + 0x100);
364}
365
366/**********************************************************************/
369int dio_put_sint16_raw(struct raw_data_out *d_out, int value)
370{
371 return dio_put_uint16_raw(d_out, 0 <= value ? value : value + 0x10000);
372}
373
374/**********************************************************************/
377int dio_put_sint32_raw(struct raw_data_out *d_out, int value)
378{
379#if SIZEOF_INT == 4
380 return dio_put_uint32_raw(d_out, value);
381#else
382 return dio_put_uint32_raw(d_out, (0 <= value ? value : value + 0x100000000));
383#endif
384}
385
386/**********************************************************************/
389int dio_put_bool8_raw(struct raw_data_out *d_out, bool value)
390{
391 FIELD_RANGE_TEST(value != TRUE && value != FALSE,
392 value = (value != FALSE);,
393 "Trying to put a non-boolean: %d", (int) value);
394
395 return dio_put_uint8_raw(d_out, value ? 1 : 0);
396}
397
398/**********************************************************************/
401int dio_put_bool32_raw(struct raw_data_out *d_out, bool value)
402{
403 FIELD_RANGE_TEST(value != TRUE && value != FALSE,
404 value = (value != FALSE);,
405 "Trying to put a non-boolean: %d",
406 (int) value);
407
408 return dio_put_uint32_raw(d_out, value ? 1 : 0);
409}
410
411/**********************************************************************/
415int dio_put_ufloat_raw(struct raw_data_out *d_out, float value,
416 int float_factor)
417{
418 uint32_t v = value * float_factor;
419
420 FIELD_RANGE_TEST(fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
421 "Trying to put %f with factor %d in 32 bits; "
422 "it will result %f at receiving side, having error of %f units.",
423 value, float_factor, (float) v / float_factor,
424 fabsf((float) v / float_factor - value) * float_factor);
425
426 return dio_put_uint32_raw(d_out, v);
427}
428
429/**********************************************************************/
433int dio_put_sfloat_raw(struct raw_data_out *d_out, float value,
434 int float_factor)
435{
436 int32_t v = value * float_factor;
437
438 FIELD_RANGE_TEST(fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
439 "Trying to put %f with factor %d in 32 bits; "
440 "it will result %f at receiving side, having error of %f units.",
441 value, float_factor, (float) v / float_factor,
442 fabsf((float) v / float_factor - value) * float_factor);
443
444 return dio_put_sint32_raw(d_out, v);
445}
446
447/**********************************************************************/
453 int stop_value)
454{
455 size_t count;
456 int e = 0;
457
458 for (count = 0; values[count] != stop_value; count++) {
459 /* nothing */
460 }
461
462 if (enough_space(d_out, 1 + count)) {
463 size_t i;
464
465 e |= dio_put_uint8_raw(d_out, count);
466
467 for (i = 0; i < count; i++) {
468 e |= dio_put_uint8_raw(d_out, values[i]);
469 }
470 }
471
472 return e;
473}
474
475/**********************************************************************/
481 int stop_value)
482{
483 size_t count;
484 int e = 0;
485
486 for (count = 0; values[count] != stop_value; count++) {
487 /* nothing */
488 }
489
490 if (enough_space(d_out, 1 + 2 * count)) {
491 size_t i;
492
493 e |= dio_put_uint8_raw(d_out, count);
494
495 for (i = 0; i < count; i++) {
496 e |= dio_put_uint16_raw(d_out, values[i]);
497 }
498 }
499
500 return e;
501}
502
503/**********************************************************************/
506int dio_put_memory_raw(struct raw_data_out *d_out, const void *value,
507 size_t size)
508{
509 if (enough_space(d_out, size)) {
510 memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), value, size);
511 d_out->current += size;
512 }
513
514 return 0;
515}
516
517/**********************************************************************/
520int dio_put_string_raw(struct raw_data_out *d_out, const char *value)
521{
522 if (put_conv_callback) {
523 size_t length;
524 char *buffer;
525 int e = 0;
526
527 if ((buffer = (*put_conv_callback) (value, &length))) {
528 e = dio_put_memory_raw(d_out, buffer, length + 1);
529 free(buffer);
530 }
531
532 return e;
533 } else {
534 return dio_put_memory_raw(d_out, value, strlen(value) + 1);
535 }
536}
537
538/**********************************************************************/
542 const struct cm_parameter *param)
543{
544 int i;
545 int e = 0;
546
547 for (i = 0; i < O_LAST; i++) {
549 }
550
551 e |= dio_put_bool8_raw(d_out, param->max_growth);
555
556 for (i = 0; i < O_LAST; i++) {
557 e |= dio_put_uint16_raw(d_out, param->factor[i]);
558 }
559
561
562 return e;
563}
564
565/**********************************************************************/
569 const struct unit_order *order)
570{
571 int e = 0;
572
573 e |= dio_put_uint8_raw(d_out, order->order);
574 e |= dio_put_uint8_raw(d_out, order->activity);
575 e |= dio_put_sint32_raw(d_out, order->target);
576 e |= dio_put_sint16_raw(d_out, order->sub_target);
577 e |= dio_put_uint8_raw(d_out, order->action);
578 e |= dio_put_sint8_raw(d_out, order->dir);
579
580 return e;
581}
582
583/**********************************************************************/
588{
589 int i, length = worklist_length(pwl);
590 int e = 0;
591
592 e |= dio_put_uint8_raw(d_out, length);
593 for (i = 0; i < length; i++) {
594 const struct universal *pcp = &(pwl->entries[i]);
595
596 e |= dio_put_uint8_raw(d_out, pcp->kind);
598 }
599
600 return e;
601}
602
603/**********************************************************************/
606bool dio_get_uint8_raw(struct data_in *din, int *dest)
607{
608 uint8_t x;
609
610 FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_byte);
611
612 if (!enough_data(din, 1)) {
613 log_packet("Packet too short to read 1 byte");
614
615 return FALSE;
616 }
617
618 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 1);
619 *dest = x;
620 din->current++;
621 return TRUE;
622}
623
624/**********************************************************************/
627bool dio_get_uint16_raw(struct data_in *din, int *dest)
628{
629 uint16_t x;
630
632
633 if (!enough_data(din, 2)) {
634 log_packet("Packet too short to read 2 bytes");
635
636 return FALSE;
637 }
638
639 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 2);
640 *dest = ntohs(x);
641 din->current += 2;
642 return TRUE;
643}
644
645/**********************************************************************/
648bool dio_get_uint32_raw(struct data_in *din, int *dest)
649{
650 uint32_t x;
651
653
654 if (!enough_data(din, 4)) {
655 log_packet("Packet too short to read 4 bytes");
656
657 return FALSE;
658 }
659
660 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 4);
661 *dest = ntohl(x);
662 din->current += 4;
663 return TRUE;
664}
665
666/**********************************************************************/
669bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
670{
671 switch (type) {
672 case DIOT_UINT8:
673 return dio_get_uint8_raw(din, dest);
674 case DIOT_UINT16:
675 return dio_get_uint16_raw(din, dest);
676 case DIOT_UINT32:
677 return dio_get_uint32_raw(din, dest);
678 case DIOT_SINT8:
679 return dio_get_sint8_raw(din, dest);
680 case DIOT_SINT16:
681 return dio_get_sint16_raw(din, dest);
682 case DIOT_SINT32:
683 return dio_get_sint32_raw(din, dest);
684 case DIOT_LAST:
685 break;
686 }
687
688 fc_assert_msg(FALSE, "data_type %d not handled.", type);
689 return FALSE;
690}
691
692/**********************************************************************/
695bool dio_get_bool8_raw(struct data_in *din, bool *dest)
696{
697 int ival;
698
699 if (!dio_get_uint8_raw(din, &ival)) {
700 return FALSE;
701 }
702
703 if (ival != 0 && ival != 1) {
704 log_packet("Got a bad boolean: %d", ival);
705 return FALSE;
706 }
707
708 *dest = (ival != 0);
709 return TRUE;
710}
711
712/**********************************************************************/
715bool dio_get_bool32_raw(struct data_in *din, bool * dest)
716{
717 int ival;
718
719 if (!dio_get_uint32_raw(din, &ival)) {
720 return FALSE;
721 }
722
723 if (ival != 0 && ival != 1) {
724 log_packet("Got a bad boolean: %d", ival);
725 return FALSE;
726 }
727
728 *dest = (ival != 0);
729 return TRUE;
730}
731
732/**********************************************************************/
736bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
737{
738 int ival;
739
740 if (!dio_get_uint32_raw(din, &ival)) {
741 return FALSE;
742 }
743
744 *dest = (float) ival / float_factor;
745 return TRUE;
746}
747
748/**********************************************************************/
752bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
753{
754 int ival;
755
756 if (!dio_get_sint32_raw(din, &ival)) {
757 return FALSE;
758 }
759
760 *dest = (float) ival / float_factor;
761 return TRUE;
762}
763
764/**********************************************************************/
767bool dio_get_sint8_raw(struct data_in *din, int *dest)
768{
769 int tmp;
770
771 if (!dio_get_uint8_raw(din, &tmp)) {
772 return FALSE;
773 }
774
775 if (tmp > 0x7f) {
776 tmp -= 0x100;
777 }
778 *dest = tmp;
779 return TRUE;
780}
781
782/**********************************************************************/
785bool dio_get_sint16_raw(struct data_in *din, int *dest)
786{
787 int tmp;
788
789 if (!dio_get_uint16_raw(din, &tmp)) {
790 return FALSE;
791 }
792
793 if (tmp > 0x7fff) {
794 tmp -= 0x10000;
795 }
796 *dest = tmp;
797 return TRUE;
798}
799
800/**********************************************************************/
803bool dio_get_sint32_raw(struct data_in *din, int *dest)
804{
805 int tmp;
806
807 if (!dio_get_uint32_raw(din, &tmp)) {
808 return FALSE;
809 }
810
811#if SIZEOF_INT != 4
812 if (tmp > 0x7fffffff) {
813 tmp -= 0x100000000;
814 }
815#endif
816
817 *dest = tmp;
818 return TRUE;
819}
820
821/**********************************************************************/
824bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
825{
826 if (!enough_data(din, dest_size)) {
827 log_packet("Got too short memory");
828 return FALSE;
829 }
830
831 memcpy(dest, ADD_TO_POINTER(din->src, din->current), dest_size);
832 din->current += dest_size;
833 return TRUE;
834}
835
836/**********************************************************************/
839bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
840{
841 char *c;
842 size_t offset, remaining;
843
845
846 if (!enough_data(din, 1)) {
847 log_packet("Got a bad string");
848 return FALSE;
849 }
850
851 remaining = dio_input_remaining(din);
852 c = ADD_TO_POINTER(din->src, din->current);
853
854 /* avoid using strlen (or strcpy) on an (unsigned char*) --dwp */
855 for (offset = 0; offset < remaining && c[offset] != '\0'; offset++) {
856 /* nothing */
857 }
858
859 if (offset >= remaining) {
860 log_packet("Got a too short string");
861 return FALSE;
862 }
863
864 if (!(*get_conv_callback) (dest, max_dest_size, c, offset)) {
865 log_packet("Got a bad encoded string");
866 return FALSE;
867 }
868
869 din->current += offset + 1;
870 return TRUE;
871}
872
873/**********************************************************************/
877 struct cm_parameter *param)
878{
879 int i;
880
881 for (i = 0; i < O_LAST; i++) {
882 if (!dio_get_sint16_raw(din, &param->minimal_surplus[i])) {
883 log_packet("Got a bad cm_parameter");
884 return FALSE;
885 }
886 }
887
888 if (!dio_get_bool8_raw(din, &param->max_growth)
892 log_packet("Got a bad cm_parameter");
893 return FALSE;
894 }
895
896 for (i = 0; i < O_LAST; i++) {
897 if (!dio_get_uint16_raw(din, &param->factor[i])) {
898 log_packet("Got a bad cm_parameter");
899 return FALSE;
900 }
901 }
902
903 if (!dio_get_uint16_raw(din, &param->happy_factor)) {
904 log_packet("Got a bad cm_parameter");
905 return FALSE;
906 }
907
908 return TRUE;
909}
910
911/**********************************************************************/
914bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
915{
916 /* These fields are enums */
917 int iorder, iactivity, idir;
918
921 || !dio_get_sint32_raw(din, &order->target)
922 || !dio_get_sint16_raw(din, &order->sub_target)
923 || !dio_get_uint8_raw(din, &order->action)
924 || !dio_get_sint8_raw(din, &idir)) {
925 log_packet("Got a bad unit_order");
926 return FALSE;
927 }
928
929 order->order = iorder;
930 order->activity = iactivity;
931 order->dir = idir;
932
933 return TRUE;
934}
935
936/**********************************************************************/
941{
942 int i, length;
943
945
946 if (!dio_get_uint8_raw(din, &length)) {
947 log_packet("Got a bad worklist");
948 return FALSE;
949 }
950
951 for (i = 0; i < length; i++) {
952 int identifier;
953 int kind;
954 struct universal univ;
955
957 || !dio_get_uint8_raw(din, &identifier)) {
958 log_packet("Got a too short worklist");
959 return FALSE;
960 }
961
962 /*
963 * FIXME: the value returned by universal_by_number() should be checked!
964 */
965 univ = universal_by_number(kind, identifier);
966 worklist_append(pwl, &univ);
967 }
968
969 return TRUE;
970}
971
972/**********************************************************************/
976bool dio_get_uint8_vec8_raw(struct data_in *din, int **values, int stop_value)
977{
978 int count, inx;
979 int *vec;
980
981 if (!dio_get_uint8_raw(din, &count)) {
982 return FALSE;
983 }
984
985 vec = fc_calloc(count + 1, sizeof(*vec));
986 for (inx = 0; inx < count; inx++) {
987 if (!dio_get_uint8_raw(din, vec + inx)) {
988 free (vec);
989 return FALSE;
990 }
991 }
992 vec[inx] = stop_value;
993 *values = vec;
994
995 return TRUE;
996}
997
998/**********************************************************************/
1001bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
1002{
1003 int count, inx;
1004 int *vec;
1005
1006 if (!dio_get_uint8_raw(din, &count)) {
1007 return FALSE;
1008 }
1009
1010 vec = fc_calloc(count + 1, sizeof(*vec));
1011 for (inx = 0; inx < count; inx++) {
1012 if (!dio_get_uint16_raw(din, vec + inx)) {
1013 free (vec);
1014 return FALSE;
1015 }
1016 }
1017 vec[inx] = stop_value;
1018 *values = vec;
1019
1020 return TRUE;
1021}
1022
1023/**********************************************************************/
1027 struct act_prob *aprob)
1028{
1029 int min, max;
1030
1031 if (!dio_get_uint8_raw(din, &min)
1032 || !dio_get_uint8_raw(din, &max)) {
1033 log_packet("Got a bad action probability");
1034 return FALSE;
1035 }
1036
1037 aprob->min = min;
1038 aprob->max = max;
1039
1040 return TRUE;
1041}
1042
1043/**********************************************************************/
1047 const struct act_prob *aprob)
1048{
1049 int e = 0;
1050
1051 e |= dio_put_uint8_raw(d_out, aprob->min);
1052 e |= dio_put_uint8_raw(d_out, aprob->max);
1053
1054 return e;
1055}
1056
1057/**********************************************************************/
1061{
1062 int type, range, value;
1063 bool survives, present, quiet;
1064
1065 if (!dio_get_uint8_raw(din, &type)
1068 || !dio_get_bool8_raw(din, &survives)
1069 || !dio_get_bool8_raw(din, &present)
1070 || !dio_get_bool8_raw(din, &quiet)) {
1071 log_packet("Got a bad requirement");
1072 return FALSE;
1073 }
1074
1075 /*
1076 * FIXME: the value returned by req_from_values() should be checked!
1077 */
1078 *preq = req_from_values(type, range, survives, present, quiet, value);
1079
1080 return TRUE;
1081}
1082
1083/**********************************************************************/
1087 const struct requirement *preq)
1088{
1089 int type, range, value;
1090 bool survives, present, quiet;
1091 int e = 0;
1092
1093 req_get_values(preq, &type, &range, &survives, &present, &quiet, &value);
1094
1098 e |= dio_put_bool8_raw(d_out, survives);
1099 e |= dio_put_bool8_raw(d_out, present);
1100 e |= dio_put_bool8_raw(d_out, quiet);
1101
1102 return e;
1103}
1104
1105/**********************************************************************/
1109{
1110 struct plocation *out = fc_malloc(sizeof(*out));
1111
1112 out->kind = PADR_FIELD;
1113 out->name = name;
1114 out->sub_location = nullptr;
1115
1116 return out;
1117}
1118
1119/**********************************************************************/
1123{
1124 struct plocation *out = fc_malloc(sizeof(*out));
1125
1126 out->kind = PADR_ELEMENT;
1127 out->number = number;
1128 out->sub_location = nullptr;
1129
1130 return out;
1131}
1132
1133/**********************************************************************/
1138const char *plocation_name(const struct plocation *loc)
1139{
1140 static char locname[10];
1141
1142 if (loc == nullptr) {
1143 return "No location";
1144 }
1145
1146 switch (loc->kind) {
1147 case PADR_FIELD:
1148 return loc->name;
1149 case PADR_ELEMENT:
1150 fc_snprintf(locname, sizeof(locname), SIZE_T_PRINTF, loc->number);
1151 return locname;
1152 }
1153
1154 return "Illegal location";
1155}
char * incite_cost
Definition comments.c:77
bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
bool dio_get_sint8_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:767
int dio_put_uint32_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:311
int dio_put_uint8_vec8_raw(struct raw_data_out *d_out, int *values, int stop_value)
Definition dataio_raw.c:452
int dio_put_bool32_raw(struct raw_data_out *d_out, bool value)
Definition dataio_raw.c:401
struct plocation * plocation_field_new(char *name)
size_t dio_output_used(struct raw_data_out *d_out)
Definition dataio_raw.c:186
bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
Definition dataio_raw.c:940
bool dio_get_action_probability_raw(struct data_in *din, struct act_prob *aprob)
int dio_put_cm_parameter_raw(struct raw_data_out *d_out, const struct cm_parameter *param)
Definition dataio_raw.c:541
void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun)
Definition dataio_raw.c:131
bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.c:139
bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
Definition dataio_raw.c:752
size_t dio_input_remaining(struct data_in *din)
Definition dataio_raw.c:223
bool dio_get_uint32_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:648
bool dio_get_sint16_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:785
bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
Definition dataio_raw.c:914
int dio_put_ufloat_raw(struct raw_data_out *d_out, float value, int float_factor)
Definition dataio_raw.c:415
bool dio_get_sint32_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:803
int dio_put_action_probability_raw(struct raw_data_out *d_out, const struct act_prob *aprob)
static bool enough_space(struct raw_data_out *d_out, size_t size)
Definition dataio_raw.c:148
int dio_put_uint16_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:289
int dio_put_worklist_raw(struct raw_data_out *d_out, const struct worklist *pwl)
Definition dataio_raw.c:587
bool dio_get_cm_parameter_raw(struct data_in *din, struct cm_parameter *param)
Definition dataio_raw.c:876
int dio_put_memory_raw(struct raw_data_out *d_out, const void *value, size_t size)
Definition dataio_raw.c:506
void dio_input_rewind(struct data_in *din)
Definition dataio_raw.c:215
bool dio_get_uint8_vec8_raw(struct data_in *din, int **values, int stop_value)
Definition dataio_raw.c:976
int dio_put_string_raw(struct raw_data_out *d_out, const char *value)
Definition dataio_raw.c:520
int dio_put_type_raw(struct raw_data_out *d_out, enum data_type type, int value)
Definition dataio_raw.c:333
void dio_output_rewind(struct raw_data_out *d_out)
Definition dataio_raw.c:195
bool dio_get_uint16_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:627
bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
Definition dataio_raw.c:824
static bool get_conv(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.c:111
bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
Definition dataio_raw.c:669
bool dio_get_bool32_raw(struct data_in *din, bool *dest)
Definition dataio_raw.c:715
int dio_put_bool8_raw(struct raw_data_out *d_out, bool value)
Definition dataio_raw.c:389
int dio_put_sfloat_raw(struct raw_data_out *d_out, float value, int float_factor)
Definition dataio_raw.c:433
void dio_output_init(struct raw_data_out *d_out, void *destination, size_t dest_size)
Definition dataio_raw.c:173
bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
Definition dataio_raw.c:736
int dio_put_sint16_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:369
bool dio_get_uint8_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:606
static bool enough_data(struct data_in *din, size_t size)
Definition dataio_raw.c:164
static DIO_GET_CONV_FUN get_conv_callback
Definition dataio_raw.c:71
bool dio_input_skip(struct data_in *din, size_t size)
Definition dataio_raw.c:254
bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
size_t data_type_size(enum data_type type)
Definition dataio_raw.c:231
struct plocation * plocation_elem_new(int number)
bool dio_get_bool8_raw(struct data_in *din, bool *dest)
Definition dataio_raw.c:695
int dio_put_sint32_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:377
int dio_put_sint8_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:361
void dio_input_init(struct data_in *din, const void *src, size_t src_size)
Definition dataio_raw.c:204
int dio_put_unit_order_raw(struct raw_data_out *d_out, const struct unit_order *order)
Definition dataio_raw.c:568
int dio_put_uint16_vec8_raw(struct raw_data_out *d_out, int *values, int stop_value)
Definition dataio_raw.c:480
#define FIELD_RANGE_TEST(_test_, _action_, _format_,...)
Definition dataio_raw.c:92
static DIO_PUT_CONV_FUN put_conv_callback
Definition dataio_raw.c:70
void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun)
Definition dataio_raw.c:102
bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
Definition dataio_raw.c:839
const char * plocation_name(const struct plocation *loc)
int dio_put_requirement_raw(struct raw_data_out *d_out, const struct requirement *preq)
int dio_put_uint8_raw(struct raw_data_out *d_out, int value)
Definition dataio_raw.c:267
char *(* DIO_PUT_CONV_FUN)(const char *src, size_t *length)
Definition dataio_raw.h:91
@ PADR_FIELD
Definition dataio_raw.h:57
@ PADR_ELEMENT
Definition dataio_raw.h:59
bool(* DIO_GET_CONV_FUN)(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.h:94
data_type
Definition dataio_raw.h:43
@ DIOT_UINT8
Definition dataio_raw.h:44
@ DIOT_UINT32
Definition dataio_raw.h:46
@ DIOT_UINT16
Definition dataio_raw.h:45
@ DIOT_LAST
Definition dataio_raw.h:51
@ DIOT_SINT32
Definition dataio_raw.h:49
@ DIOT_SINT8
Definition dataio_raw.h:47
@ DIOT_SINT16
Definition dataio_raw.h:48
@ O_LAST
Definition fc_types.h:103
struct tile * loc
Definition citydlg.c:227
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define log_packet
Definition log.h:138
#define fc_assert(condition)
Definition log.h:177
#define FC_STATIC_ASSERT(cond, tag)
Definition log.h:238
#define fc_calloc(n, esz)
Definition mem.h:38
#define fc_malloc(sz)
Definition mem.h:34
int len
Definition packhand.c:129
void req_get_values(const struct requirement *req, int *type, int *range, bool *survives, bool *present, bool *quiet, int *value)
struct requirement req_from_values(int type, int range, bool survives, bool present, bool quiet, int value)
struct universal universal_by_number(const enum universals_n kind, const int value)
int universal_number(const struct universal *source)
#define ADD_TO_POINTER(p, n)
Definition shared.h:86
#define MAX(x, y)
Definition shared.h:54
size_t size
Definition specvec.h:72
struct sprite int x
Definition sprite_g.h:31
bool allow_disorder
Definition cm.h:44
int factor[O_LAST]
Definition cm.h:47
bool max_growth
Definition cm.h:42
bool allow_specialists
Definition cm.h:45
bool require_happy
Definition cm.h:43
int minimal_surplus[O_LAST]
Definition cm.h:41
int happy_factor
Definition cm.h:48
size_t number
Definition dataio_raw.h:70
enum unit_activity activity
Definition unit.h:95
enum unit_orders order
Definition unit.h:94
int action
Definition unit.h:102
enum direction8 dir
Definition unit.h:104
int target
Definition unit.h:98
int sub_target
Definition unit.h:99
enum universals_n kind
Definition fc_types.h:595
universals_u value
Definition fc_types.h:594
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
void worklist_init(struct worklist *pwl)
Definition worklist.c:38
bool worklist_append(struct worklist *pwl, const struct universal *prod)
Definition worklist.c:147
int worklist_length(const struct worklist *pwl)
Definition worklist.c:57