Freeciv21
Develop your civilization from humble roots to a global empire
dataio_raw.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 /*
15  * The DataIO module provides a system independent (endianess 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 #include <cmath>
23 #include <cstdint>
24 #include <cstring>
25 
26 // Qt
27 #include <QtEndian>
28 
29 // utility
30 #include "support.h"
31 
32 // common
33 #include "events.h"
34 #include "player.h"
35 #include "requirements.h"
36 #include "tech.h"
37 #include "unit.h"
38 #include "worklist.h"
39 
40 /* common/aicore */
41 #include "cm.h"
42 
43 #include "dataio.h"
44 
45 static bool get_conv(char *dst, size_t ndst, const char *src, size_t nsrc);
46 
49 
50 // Uncomment to make field range tests to asserts, fatal with -F
51 // #define FIELD_RANGE_ASSERT
52 
53 #if defined(FREECIV_TESTMATIC) && !defined(FIELD_RANGE_ASSERT)
54 #define FIELD_RANGE_ASSERT
55 #endif
56 
57 #ifdef FIELD_RANGE_ASSERT
58 /* This evaluates _test_ twice. If that's a problem,
59  * it should evaluate it just once and store result to variable.
60  * That would lose verbosity of the assert message. */
61 #define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
62  fc_assert(!(_test_)); \
63  if (_test_) { \
64  _action_ qCritical(_format_, ##__VA_ARGS__); \
65  }
66 #else // FIELD_RANGE_ASSERT
67 #define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
68  if (_test_) { \
69  _action_ qCritical(_format_, ##__VA_ARGS__); \
70  }
71 #endif // FIELD_RANGE_ASSERT
72 
77 {
78  put_conv_callback = fun;
79 }
80 
85 static bool get_conv(char *dst, size_t ndst, const char *src, size_t nsrc)
86 {
87  size_t len = nsrc; // length to copy, not including null
88  bool ret = true;
89 
90  if (ndst > 0 && len >= ndst) {
91  ret = false;
92  len = ndst - 1;
93  }
94 
95  memcpy(dst, src, len);
96  dst[len] = '\0';
97 
98  return ret;
99 }
100 
105 {
106  get_conv_callback = fun;
107 }
108 
112 bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src,
113  size_t nsrc)
114 {
115  return get_conv_callback(dst, ndst, src, nsrc);
116 }
117 
121 static bool enough_space(struct raw_data_out *dout, size_t size)
122 {
123  if (dout->current + size > dout->dest_size) {
124  dout->too_short = true;
125  return false;
126  } else {
127  dout->used = MAX(dout->used, dout->current + size);
128  return true;
129  }
130 }
131 
135 static bool enough_data(struct data_in *din, size_t size)
136 {
137  return dio_input_remaining(din) >= size;
138 }
139 
144 void dio_output_init(struct raw_data_out *dout, void *destination,
145  size_t dest_size)
146 {
147  dout->dest = destination;
148  dout->dest_size = dest_size;
149  dout->current = 0;
150  dout->used = 0;
151  dout->too_short = false;
152 }
153 
157 size_t dio_output_used(struct raw_data_out *dout) { return dout->used; }
158 
163 void dio_output_rewind(struct raw_data_out *dout) { dout->current = 0; }
164 
169 void dio_input_init(struct data_in *din, const void *src, size_t src_size)
170 {
171  din->src = src;
172  din->src_size = src_size;
173  din->current = 0;
174 }
175 
180 void dio_input_rewind(struct data_in *din) { din->current = 0; }
181 
185 size_t dio_input_remaining(struct data_in *din)
186 {
187  return din->src_size - din->current;
188 }
189 
193 size_t data_type_size(enum data_type type)
194 {
195  switch (type) {
196  case DIOT_UINT8:
197  case DIOT_SINT8:
198  return 1;
199  case DIOT_UINT16:
200  case DIOT_SINT16:
201  return 2;
202  case DIOT_UINT32:
203  case DIOT_SINT32:
204  return 4;
205  case DIOT_LAST:
206  break;
207  }
208 
209  fc_assert_msg(false, "data_type %d not handled.", type);
210  return 0;
211 }
212 
216 bool dio_input_skip(struct data_in *din, size_t size)
217 {
218  if (enough_data(din, size)) {
219  din->current += size;
220  return true;
221  } else {
222  return false;
223  }
224 }
225 
229 void dio_put_uint8_raw(struct raw_data_out *dout, int value)
230 {
231  uint8_t x = value;
232  FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_1_byte);
233 
234  FIELD_RANGE_TEST((int) x != value, ,
235  "Trying to put %d into 8 bits; "
236  "it will result %d at receiving side.",
237  value, (int) x);
238 
239  if (enough_space(dout, 1)) {
240  memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 1);
241  dout->current++;
242  }
243 }
244 
248 void dio_put_uint16_raw(struct raw_data_out *dout, int value)
249 {
250  uint16_t x = qToBigEndian(uint16_t(value));
251  FC_STATIC_ASSERT(sizeof(x) == 2, uint16_not_2_bytes);
252 
253  FIELD_RANGE_TEST((int) qFromBigEndian(x) != value, ,
254  "Trying to put %d into 16 bits; "
255  "it will result %d at receiving side.",
256  value, (int) qFromBigEndian(x));
257 
258  if (enough_space(dout, 2)) {
259  memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 2);
260  dout->current += 2;
261  }
262 }
263 
267 void dio_put_uint32_raw(struct raw_data_out *dout, int value)
268 {
269  uint32_t x = qToBigEndian(uint32_t(value));
270  FC_STATIC_ASSERT(sizeof(x) == 4, uint32_not_4_bytes);
271 
272  FIELD_RANGE_TEST((int) qFromBigEndian(x) != value, ,
273  "Trying to put %d into 32 bits; "
274  "it will result %d at receiving side.",
275  value, (int) qFromBigEndian(x));
276 
277  if (enough_space(dout, 4)) {
278  memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 4);
279  dout->current += 4;
280  }
281 }
282 
286 void dio_put_type_raw(struct raw_data_out *dout, enum data_type type,
287  int value)
288 {
289  switch (type) {
290  case DIOT_UINT8:
291  dio_put_uint8_raw(dout, value);
292  return;
293  case DIOT_UINT16:
294  dio_put_uint16_raw(dout, value);
295  return;
296  case DIOT_UINT32:
297  dio_put_uint32_raw(dout, value);
298  return;
299  case DIOT_SINT8:
300  dio_put_sint8_raw(dout, value);
301  return;
302  case DIOT_SINT16:
303  dio_put_sint16_raw(dout, value);
304  return;
305  case DIOT_SINT32:
306  dio_put_sint32_raw(dout, value);
307  return;
308  case DIOT_LAST:
309  break;
310  }
311 
312  fc_assert_msg(false, "data_type %d not handled.", type);
313 }
314 
318 void dio_put_sint8_raw(struct raw_data_out *dout, int value)
319 {
320  dio_put_uint8_raw(dout, 0 <= value ? value : value + 0x100);
321 }
322 
326 void dio_put_sint16_raw(struct raw_data_out *dout, int value)
327 {
328  dio_put_uint16_raw(dout, 0 <= value ? value : value + 0x10000);
329 }
330 
334 void dio_put_sint32_raw(struct raw_data_out *dout, int value)
335 {
336  if (sizeof(int) == 4) {
337  dio_put_uint32_raw(dout, value);
338  } else {
339  dio_put_uint32_raw(dout, (0 <= value ? value : value + 0x100000000));
340  }
341 }
342 
346 void dio_put_bool8_raw(struct raw_data_out *dout, bool value)
347 {
348  FIELD_RANGE_TEST(value != true && value != false, value = (value != false);
349  , "Trying to put a non-boolean: %d", (int) value);
350 
351  dio_put_uint8_raw(dout, value ? 1 : 0);
352 }
353 
357 void dio_put_bool32_raw(struct raw_data_out *dout, bool value)
358 {
359  FIELD_RANGE_TEST(value != true && value != false, value = (value != false);
360  , "Trying to put a non-boolean: %d", (int) value);
361 
362  dio_put_uint32_raw(dout, value ? 1 : 0);
363 }
364 
369 void dio_put_ufloat_raw(struct raw_data_out *dout, float value,
370  int float_factor)
371 {
372  uint32_t v = value * float_factor;
373 
375  fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
376  "Trying to put %f with factor %d in 32 bits; "
377  "it will result %f at receiving side, having error of %f units.",
378  value, float_factor, (float) v / float_factor,
379  fabsf((float) v / float_factor - value) * float_factor);
380 
381  dio_put_uint32_raw(dout, v);
382 }
383 
388 void dio_put_sfloat_raw(struct raw_data_out *dout, float value,
389  int float_factor)
390 {
391  int32_t v = value * float_factor;
392 
394  fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
395  "Trying to put %f with factor %d in 32 bits; "
396  "it will result %f at receiving side, having error of %f units.",
397  value, float_factor, (float) v / float_factor,
398  fabsf((float) v / float_factor - value) * float_factor);
399 
400  dio_put_sint32_raw(dout, v);
401 }
402 
406 void dio_put_memory_raw(struct raw_data_out *dout, const void *value,
407  size_t size)
408 {
409  if (enough_space(dout, size)) {
410  memcpy(ADD_TO_POINTER(dout->dest, dout->current), value, size);
411  dout->current += size;
412  }
413 }
414 
418 void dio_put_string_raw(struct raw_data_out *dout, const char *value)
419 {
420  if (put_conv_callback) {
421  size_t length;
422  char *buffer;
423 
424  if ((buffer = (*put_conv_callback)(value, &length))) {
425  dio_put_memory_raw(dout, buffer, length + 1);
426  delete[] buffer;
427  }
428  } else {
429  dio_put_memory_raw(dout, value, qstrlen(value) + 1);
430  }
431 }
432 
437  const struct cm_parameter *param)
438 {
439  int i;
440 
441  for (i = 0; i < O_LAST; i++) {
442  dio_put_sint16_raw(dout, param->minimal_surplus[i]);
443  }
444 
445  dio_put_bool8_raw(dout, param->max_growth);
446  dio_put_bool8_raw(dout, param->require_happy);
447  dio_put_bool8_raw(dout, param->allow_disorder);
448  dio_put_bool8_raw(dout, param->allow_specialists);
449 
450  for (i = 0; i < O_LAST; i++) {
451  dio_put_uint16_raw(dout, param->factor[i]);
452  }
453 
454  dio_put_uint16_raw(dout, param->happy_factor);
455 }
456 
461  const struct unit_order *order)
462 {
463  dio_put_uint8_raw(dout, order->order);
464  dio_put_uint8_raw(dout, order->activity);
465  dio_put_sint32_raw(dout, order->target);
466  dio_put_sint16_raw(dout, order->sub_target);
467  dio_put_uint8_raw(dout, order->action);
468  dio_put_sint8_raw(dout, order->dir);
469 }
470 
476  const struct worklist *pwl)
477 {
478  int i, length = worklist_length(pwl);
479 
480  dio_put_uint8_raw(dout, length);
481  for (i = 0; i < length; i++) {
482  const struct universal *pcp = &(pwl->entries[i]);
483 
484  dio_put_uint8_raw(dout, pcp->kind);
486  }
487 }
488 
492 bool dio_get_uint8_raw(struct data_in *din, int *dest)
493 {
494  uint8_t x;
495 
496  FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_byte);
497 
498  if (!enough_data(din, 1)) {
499  log_packet("Packet too short to read 1 byte");
500 
501  return false;
502  }
503 
504  memcpy(&x, ADD_TO_POINTER(din->src, din->current), 1);
505  *dest = x;
506  din->current++;
507  return true;
508 }
509 
513 bool dio_get_uint16_raw(struct data_in *din, int *dest)
514 {
515  uint16_t x;
516 
517  FC_STATIC_ASSERT(sizeof(x) == 2, uint16_not_2_bytes);
518 
519  if (!enough_data(din, 2)) {
520  log_packet("Packet too short to read 2 bytes");
521 
522  return false;
523  }
524 
525  memcpy(&x, ADD_TO_POINTER(din->src, din->current), 2);
526  *dest = qFromBigEndian(x);
527  din->current += 2;
528  return true;
529 }
530 
534 bool dio_get_uint32_raw(struct data_in *din, int *dest)
535 {
536  uint32_t x;
537 
538  FC_STATIC_ASSERT(sizeof(x) == 4, uint32_not_4_bytes);
539 
540  if (!enough_data(din, 4)) {
541  log_packet("Packet too short to read 4 bytes");
542 
543  return false;
544  }
545 
546  memcpy(&x, ADD_TO_POINTER(din->src, din->current), 4);
547  *dest = qFromBigEndian(x);
548  din->current += 4;
549  return true;
550 }
551 
555 bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
556 {
557  switch (type) {
558  case DIOT_UINT8:
559  return dio_get_uint8_raw(din, dest);
560  case DIOT_UINT16:
561  return dio_get_uint16_raw(din, dest);
562  case DIOT_UINT32:
563  return dio_get_uint32_raw(din, dest);
564  case DIOT_SINT8:
565  return dio_get_sint8_raw(din, dest);
566  case DIOT_SINT16:
567  return dio_get_sint16_raw(din, dest);
568  case DIOT_SINT32:
569  return dio_get_sint32_raw(din, dest);
570  case DIOT_LAST:
571  break;
572  }
573 
574  fc_assert_msg(false, "data_type %d not handled.", type);
575  return false;
576 }
577 
581 bool dio_get_bool8_raw(struct data_in *din, bool *dest)
582 {
583  int ival;
584 
585  if (!dio_get_uint8_raw(din, &ival)) {
586  return false;
587  }
588 
589  if (ival != 0 && ival != 1) {
590  log_packet("Got a bad boolean: %d", ival);
591  return false;
592  }
593 
594  *dest = (ival != 0);
595  return true;
596 }
597 
601 bool dio_get_bool32_raw(struct data_in *din, bool *dest)
602 {
603  int ival;
604 
605  if (!dio_get_uint32_raw(din, &ival)) {
606  return false;
607  }
608 
609  if (ival != 0 && ival != 1) {
610  log_packet("Got a bad boolean: %d", ival);
611  return false;
612  }
613 
614  *dest = (ival != 0);
615  return true;
616 }
617 
622 bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
623 {
624  int ival;
625 
626  if (!dio_get_uint32_raw(din, &ival)) {
627  return false;
628  }
629 
630  *dest = static_cast<float>(ival) / float_factor;
631  return true;
632 }
633 
638 bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
639 {
640  int ival;
641 
642  if (!dio_get_sint32_raw(din, &ival)) {
643  return false;
644  }
645 
646  *dest = static_cast<float>(ival) / float_factor;
647  return true;
648 }
649 
653 bool dio_get_sint8_raw(struct data_in *din, int *dest)
654 {
655  int tmp;
656 
657  if (!dio_get_uint8_raw(din, &tmp)) {
658  return false;
659  }
660 
661  if (tmp > 0x7f) {
662  tmp -= 0x100;
663  }
664  *dest = tmp;
665  return true;
666 }
667 
671 bool dio_get_sint16_raw(struct data_in *din, int *dest)
672 {
673  int tmp;
674 
675  if (!dio_get_uint16_raw(din, &tmp)) {
676  return false;
677  }
678 
679  if (tmp > 0x7fff) {
680  tmp -= 0x10000;
681  }
682  *dest = tmp;
683  return true;
684 }
685 
689 bool dio_get_sint32_raw(struct data_in *din, int *dest)
690 {
691  int tmp;
692 
693  if (!dio_get_uint32_raw(din, &tmp)) {
694  return false;
695  }
696 
697  if (sizeof(int) != 4) {
698  if (tmp > 0x7fffffff) {
699  tmp -= 0x100000000;
700  }
701  }
702 
703  *dest = tmp;
704  return true;
705 }
706 
710 bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
711 {
712  if (!enough_data(din, dest_size)) {
713  log_packet("Got too short memory");
714  return false;
715  }
716 
717  memcpy(dest, ADD_TO_POINTER(din->src, din->current), dest_size);
718  din->current += dest_size;
719  return true;
720 }
721 
725 bool dio_get_string_raw(struct data_in *din, char *dest,
726  size_t max_dest_size)
727 {
728  char *c;
729  size_t offset, remaining;
730 
731  fc_assert(max_dest_size > 0);
732 
733  if (!enough_data(din, 1)) {
734  log_packet("Got a bad string");
735  return false;
736  }
737 
738  remaining = dio_input_remaining(din);
739  c = static_cast<char *>(ADD_TO_POINTER(din->src, din->current));
740 
741  // avoid using qstrlen (or qstrcpy) on an (unsigned char*) --dwp
742  for (offset = 0; offset < remaining && c[offset] != '\0'; offset++) {
743  // nothing
744  }
745 
746  if (offset >= remaining) {
747  log_packet("Got a too short string");
748  return false;
749  }
750 
751  if (!(*get_conv_callback)(dest, max_dest_size, c, offset)) {
752  log_packet("Got a bad encoded string");
753  return false;
754  }
755 
756  din->current += offset + 1;
757  return true;
758 }
759 
764  struct cm_parameter *param)
765 {
766  int i;
767 
768  for (i = 0; i < O_LAST; i++) {
769  if (!dio_get_sint16_raw(din, &param->minimal_surplus[i])) {
770  log_packet("Got a bad cm_parameter");
771  return false;
772  }
773  }
774 
775  if (!dio_get_bool8_raw(din, &param->max_growth)
776  || !dio_get_bool8_raw(din, &param->require_happy)
777  || !dio_get_bool8_raw(din, &param->allow_disorder)
778  || !dio_get_bool8_raw(din, &param->allow_specialists)) {
779  log_packet("Got a bad cm_parameter");
780  return false;
781  }
782 
783  for (i = 0; i < O_LAST; i++) {
784  if (!dio_get_uint16_raw(din, &param->factor[i])) {
785  log_packet("Got a bad cm_parameter");
786  return false;
787  }
788  }
789 
790  if (!dio_get_uint16_raw(din, &param->happy_factor)) {
791  log_packet("Got a bad cm_parameter");
792  return false;
793  }
794 
795  return true;
796 }
797 
801 bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
802 {
803  // These fields are enums
804  int iorder, iactivity, idir;
805 
806  if (!dio_get_uint8_raw(din, &iorder) || !dio_get_uint8_raw(din, &iactivity)
807  || !dio_get_sint32_raw(din, &order->target)
808  || !dio_get_sint16_raw(din, &order->sub_target)
809  || !dio_get_uint8_raw(din, &order->action)
810  || !dio_get_sint8_raw(din, &idir)) {
811  log_packet("Got a bad unit_order");
812  return false;
813  }
814 
815  order->order = unit_orders(iorder);
816  order->activity = unit_activity(iactivity);
817  order->dir = direction8(idir);
818 
819  return true;
820 }
821 
826 bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
827 {
828  int i, length;
829 
830  worklist_init(pwl);
831 
832  if (!dio_get_uint8_raw(din, &length)) {
833  log_packet("Got a bad worklist");
834  return false;
835  }
836 
837  for (i = 0; i < length; i++) {
838  int identifier;
839  int kind;
840  struct universal univ;
841 
842  if (!dio_get_uint8_raw(din, &kind)
843  || !dio_get_uint8_raw(din, &identifier)) {
844  log_packet("Got a too short worklist");
845  return false;
846  }
847 
848  /*
849  * FIXME: the value returned by universal_by_number() should be checked!
850  */
851  univ = universal_by_number(universals_n(kind), identifier);
852  worklist_append(pwl, &univ);
853  }
854 
855  return true;
856 }
857 
862  struct act_prob *aprob)
863 {
864  int min, max;
865 
866  if (!dio_get_uint8_raw(din, &min) || !dio_get_uint8_raw(din, &max)) {
867  log_packet("Got a bad action probability");
868  return false;
869  }
870 
871  aprob->min = min;
872  aprob->max = max;
873 
874  return true;
875 }
876 
881  const struct act_prob *aprob)
882 {
883  dio_put_uint8_raw(dout, aprob->min);
884  dio_put_uint8_raw(dout, aprob->max);
885 }
886 
890 bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
891 {
892  int type, range, value;
893  bool survives, present, quiet;
894 
895  if (!dio_get_uint8_raw(din, &type) || !dio_get_sint32_raw(din, &value)
896  || !dio_get_uint8_raw(din, &range)
897  || !dio_get_bool8_raw(din, &survives)
898  || !dio_get_bool8_raw(din, &present)
899  || !dio_get_bool8_raw(din, &quiet)) {
900  log_packet("Got a bad requirement");
901  return false;
902  }
903 
904  *preq = req_from_values(type, range, survives, present, quiet, value);
905  if (preq->source.kind == universals_n_invalid()) {
906  // Keep bad requirements but make sure we never touch them.
907  qWarning() << "The server sent an invalid or unknown requirement.";
908  preq->source.kind = VUT_NONE;
909  }
910 
911  return true;
912 }
913 
918  const struct requirement *preq)
919 {
920  int type, range, value;
921  bool survives, present, quiet;
922 
923  req_get_values(preq, &type, &range, &survives, &present, &quiet, &value);
924 
925  dio_put_uint8_raw(dout, type);
926  dio_put_sint32_raw(dout, value);
927  dio_put_uint8_raw(dout, range);
928  dio_put_bool8_raw(dout, survives);
929  dio_put_bool8_raw(dout, present);
930  dio_put_bool8_raw(dout, quiet);
931 }
void dio_put_cm_parameter_raw(struct raw_data_out *dout, const struct cm_parameter *param)
Insert cm_parameter struct.
Definition: dataio_raw.cpp:436
void dio_put_action_probability_raw(struct raw_data_out *dout, const struct act_prob *aprob)
Serialize an action probability.
Definition: dataio_raw.cpp:880
void dio_put_unit_order_raw(struct raw_data_out *dout, const struct unit_order *order)
Insert the given unit_order struct/.
Definition: dataio_raw.cpp:460
bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
De-serialize a requirement.
Definition: dataio_raw.cpp:890
bool dio_get_sint8_raw(struct data_in *din, int *dest)
Take value from 8 bits.
Definition: dataio_raw.cpp:653
static bool enough_space(struct raw_data_out *dout, size_t size)
Returns TRUE iff the output has size bytes available.
Definition: dataio_raw.cpp:121
void dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
Insert a float number, which is multiplied by 'float_factor' before being encoded into an uint32.
Definition: dataio_raw.cpp:369
bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
Take worklist item count and then kind and number for each item, and put them to provided worklist.
Definition: dataio_raw.cpp:826
bool dio_get_action_probability_raw(struct data_in *din, struct act_prob *aprob)
De-serialize an action probability.
Definition: dataio_raw.cpp:861
void dio_put_bool8_raw(struct raw_data_out *dout, bool value)
Insert value 0 or 1 using 8 bits.
Definition: dataio_raw.cpp:346
void dio_output_init(struct raw_data_out *dout, void *destination, size_t dest_size)
Initializes the output to the given output buffer and the given buffer size.
Definition: dataio_raw.cpp:144
void dio_put_uint16_raw(struct raw_data_out *dout, int value)
Insert value using 16 bits.
Definition: dataio_raw.cpp:248
void dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
Insert a float number, which is multiplied by 'float_factor' before being encoded into a sint32.
Definition: dataio_raw.cpp:388
void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun)
Sets string conversion callback to use when getting text.
Definition: dataio_raw.cpp:104
bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src, size_t nsrc)
Call the get_conv callback.
Definition: dataio_raw.cpp:112
bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
Get a signed float number, which have been multiplied by 'float_factor' and encoded into a sint32 by ...
Definition: dataio_raw.cpp:638
size_t dio_input_remaining(struct data_in *din)
Return the number of unread bytes.
Definition: dataio_raw.cpp:185
void dio_put_uint8_raw(struct raw_data_out *dout, int value)
Insert value using 8 bits.
Definition: dataio_raw.cpp:229
void dio_put_sint16_raw(struct raw_data_out *dout, int value)
Insert value using 16 bits.
Definition: dataio_raw.cpp:326
bool dio_get_uint32_raw(struct data_in *din, int *dest)
Receive uint32 value to dest.
Definition: dataio_raw.cpp:534
bool dio_get_sint16_raw(struct data_in *din, int *dest)
Take value from 16 bits.
Definition: dataio_raw.cpp:671
bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
Take unit_order struct and put it in the provided orders.
Definition: dataio_raw.cpp:801
bool dio_get_sint32_raw(struct data_in *din, int *dest)
Take value from 32 bits.
Definition: dataio_raw.cpp:689
void dio_put_sint32_raw(struct raw_data_out *dout, int value)
Insert value using 32 bits.
Definition: dataio_raw.cpp:334
bool dio_get_cm_parameter_raw(struct data_in *din, struct cm_parameter *param)
Get city manager parameters.
Definition: dataio_raw.cpp:763
void dio_input_rewind(struct data_in *din)
Rewinds the stream so that the get-functions start from the beginning.
Definition: dataio_raw.cpp:180
void dio_put_uint32_raw(struct raw_data_out *dout, int value)
Insert value using 32 bits.
Definition: dataio_raw.cpp:267
bool dio_get_uint16_raw(struct data_in *din, int *dest)
Receive uint16 value to dest.
Definition: dataio_raw.cpp:513
bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
Take memory block directly.
Definition: dataio_raw.cpp:710
void dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
Insert block directly from memory.
Definition: dataio_raw.cpp:406
static bool get_conv(char *dst, size_t ndst, const char *src, size_t nsrc)
Returns FALSE if the destination isn't large enough or the source was bad.
Definition: dataio_raw.cpp:85
bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
Receive value using 'size' bits to dest.
Definition: dataio_raw.cpp:555
bool dio_get_bool32_raw(struct data_in *din, bool *dest)
Take boolean value from 32 bits.
Definition: dataio_raw.cpp:601
bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
Get an unsigned float number, which have been multiplied by 'float_factor' and encoded into an uint32...
Definition: dataio_raw.cpp:622
void dio_put_worklist_raw(struct raw_data_out *dout, const struct worklist *pwl)
Insert number of worklist items as 8 bit value and then insert 8 bit kind and 8 bit number for each w...
Definition: dataio_raw.cpp:475
void dio_put_requirement_raw(struct raw_data_out *dout, const struct requirement *preq)
Serialize a requirement.
Definition: dataio_raw.cpp:917
bool dio_get_uint8_raw(struct data_in *din, int *dest)
Receive uint8 value to dest.
Definition: dataio_raw.cpp:492
static bool enough_data(struct data_in *din, size_t size)
Returns TRUE iff the input contains size unread bytes.
Definition: dataio_raw.cpp:135
static DIO_GET_CONV_FUN get_conv_callback
Definition: dataio_raw.cpp:48
bool dio_input_skip(struct data_in *din, size_t size)
Skips 'n' bytes.
Definition: dataio_raw.cpp:216
size_t data_type_size(enum data_type type)
Return the size of the data_type in bytes.
Definition: dataio_raw.cpp:193
void dio_put_type_raw(struct raw_data_out *dout, enum data_type type, int value)
Insert value using 'size' bits.
Definition: dataio_raw.cpp:286
bool dio_get_bool8_raw(struct data_in *din, bool *dest)
Take boolean value from 8 bits.
Definition: dataio_raw.cpp:581
void dio_put_string_raw(struct raw_data_out *dout, const char *value)
Insert nullptr-terminated string.
Definition: dataio_raw.cpp:418
size_t dio_output_used(struct raw_data_out *dout)
Return the maximum number of bytes used.
Definition: dataio_raw.cpp:157
void dio_input_init(struct data_in *din, const void *src, size_t src_size)
Initializes the input to the given input buffer and the given number of valid input bytes.
Definition: dataio_raw.cpp:169
#define FIELD_RANGE_TEST(_test_, _action_, _format_,...)
Definition: dataio_raw.cpp:67
static DIO_PUT_CONV_FUN put_conv_callback
Definition: dataio_raw.cpp:47
void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun)
Sets string conversion callback to be used when putting text.
Definition: dataio_raw.cpp:76
bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
Take string.
Definition: dataio_raw.cpp:725
void dio_put_bool32_raw(struct raw_data_out *dout, bool value)
Insert value 0 or 1 using 32 bits.
Definition: dataio_raw.cpp:357
void dio_output_rewind(struct raw_data_out *dout)
Rewinds the stream so that the put-functions start from the beginning.
Definition: dataio_raw.cpp:163
void dio_put_sint8_raw(struct raw_data_out *dout, int value)
Insert value using 8 bits.
Definition: dataio_raw.cpp:318
bool(* DIO_GET_CONV_FUN)(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition: dataio_raw.h:52
char *(* DIO_PUT_CONV_FUN)(const char *src, size_t *length)
Definition: dataio_raw.h:49
data_type
Definition: dataio_raw.h:37
@ DIOT_UINT8
Definition: dataio_raw.h:38
@ DIOT_UINT32
Definition: dataio_raw.h:40
@ DIOT_UINT16
Definition: dataio_raw.h:39
@ DIOT_LAST
Definition: dataio_raw.h:45
@ DIOT_SINT32
Definition: dataio_raw.h:43
@ DIOT_SINT8
Definition: dataio_raw.h:41
@ DIOT_SINT16
Definition: dataio_raw.h:42
@ O_LAST
Definition: fc_types.h:91
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
#define log_packet
Definition: log.h:72
#define fc_assert(condition)
Definition: log.h:89
#define FC_STATIC_ASSERT(cond, tag)
Definition: log.h:158
int len
Definition: packhand.cpp:127
void req_get_values(const struct requirement *req, int *type, int *range, bool *survives, bool *present, bool *quiet, int *value)
Return the value of a req as a serializable integer.
struct requirement req_from_values(int type, int range, bool survives, bool present, bool quiet, int value)
Set the values of a req from serializable integers.
struct universal universal_by_number(const enum universals_n kind, const int value)
Combine values into a universal structure.
int universal_number(const struct universal *source)
Return the universal number of the constituent.
#define ADD_TO_POINTER(p, n)
Definition: shared.h:80
#define MAX(x, y)
Definition: shared.h:48
size_t size
Definition: specvec.h:64
int max
Definition: fc_types.h:1060
int min
Definition: fc_types.h:1059
bool allow_disorder
Definition: cm.h:20
int factor[O_LAST]
Definition: cm.h:23
bool max_growth
Definition: cm.h:18
bool allow_specialists
Definition: cm.h:21
bool require_happy
Definition: cm.h:19
int minimal_surplus[O_LAST]
Definition: cm.h:17
int happy_factor
Definition: cm.h:24
const void * src
Definition: dataio_raw.h:25
size_t src_size
Definition: dataio_raw.h:26
size_t current
Definition: dataio_raw.h:26
bool too_short
Definition: dataio_raw.h:32
void * dest
Definition: dataio_raw.h:30
size_t used
Definition: dataio_raw.h:31
size_t dest_size
Definition: dataio_raw.h:31
size_t current
Definition: dataio_raw.h:31
struct universal source
Definition: requirements.h:68
enum unit_activity activity
Definition: unit.h:81
enum unit_orders order
Definition: unit.h:80
int action
Definition: unit.h:87
enum direction8 dir
Definition: unit.h:89
int target
Definition: unit.h:84
int sub_target
Definition: unit.h:85
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct universal entries[MAX_LEN_WORKLIST]
Definition: worklist.h:25
unit_orders
Definition: unit.h:31
void worklist_init(struct worklist *pwl)
Initialize a worklist to be empty.
Definition: worklist.cpp:32
bool worklist_append(struct worklist *pwl, const struct universal *prod)
Adds the id to the next available slot in the worklist.
Definition: worklist.cpp:138
int worklist_length(const struct worklist *pwl)
Returns the number of entries in the worklist.
Definition: worklist.cpp:51