encode_legacy.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Attempts to test all the datatypes supported by ProtoBuf.
  2. * This is a backwards-compatibility test, using alltypes_legacy.h.
  3. * It is similar to encode_alltypes, but duplicated in order to allow
  4. * encode_alltypes to test any new features introduced later.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <pb_encode.h>
  10. #include "alltypes_legacy.h"
  11. #include "test_helpers.h"
  12. int main(int argc, char **argv)
  13. {
  14. int mode = (argc > 1) ? atoi(argv[1]) : 0;
  15. /* Initialize the structure with constants */
  16. AllTypes alltypes = {0};
  17. alltypes.req_int32 = -1001;
  18. alltypes.req_int64 = -1002;
  19. alltypes.req_uint32 = 1003;
  20. alltypes.req_uint64 = 1004;
  21. alltypes.req_sint32 = -1005;
  22. alltypes.req_sint64 = -1006;
  23. alltypes.req_bool = true;
  24. alltypes.req_fixed32 = 1008;
  25. alltypes.req_sfixed32 = -1009;
  26. alltypes.req_float = 1010.0f;
  27. alltypes.req_fixed64 = 1011;
  28. alltypes.req_sfixed64 = -1012;
  29. alltypes.req_double = 1013.0;
  30. strcpy(alltypes.req_string, "1014");
  31. alltypes.req_bytes.size = 4;
  32. memcpy(alltypes.req_bytes.bytes, "1015", 4);
  33. strcpy(alltypes.req_submsg.substuff1, "1016");
  34. alltypes.req_submsg.substuff2 = 1016;
  35. alltypes.req_enum = MyEnum_Truth;
  36. alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001;
  37. alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = -2002;
  38. alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003;
  39. alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004;
  40. alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005;
  41. alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = -2006;
  42. alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true;
  43. alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008;
  44. alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009;
  45. alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f;
  46. alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011;
  47. alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = -2012;
  48. alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0;
  49. alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014");
  50. alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4;
  51. memcpy(alltypes.rep_bytes[4].bytes, "2015", 4);
  52. alltypes.rep_submsg_count = 5;
  53. strcpy(alltypes.rep_submsg[4].substuff1, "2016");
  54. alltypes.rep_submsg[4].substuff2 = 2016;
  55. alltypes.rep_submsg[4].has_substuff3 = true;
  56. alltypes.rep_submsg[4].substuff3 = 2016;
  57. alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
  58. if (mode != 0)
  59. {
  60. /* Fill in values for optional fields */
  61. alltypes.has_opt_int32 = true;
  62. alltypes.opt_int32 = 3041;
  63. alltypes.has_opt_int64 = true;
  64. alltypes.opt_int64 = 3042;
  65. alltypes.has_opt_uint32 = true;
  66. alltypes.opt_uint32 = 3043;
  67. alltypes.has_opt_uint64 = true;
  68. alltypes.opt_uint64 = 3044;
  69. alltypes.has_opt_sint32 = true;
  70. alltypes.opt_sint32 = 3045;
  71. alltypes.has_opt_sint64 = true;
  72. alltypes.opt_sint64 = 3046;
  73. alltypes.has_opt_bool = true;
  74. alltypes.opt_bool = true;
  75. alltypes.has_opt_fixed32 = true;
  76. alltypes.opt_fixed32 = 3048;
  77. alltypes.has_opt_sfixed32 = true;
  78. alltypes.opt_sfixed32 = 3049;
  79. alltypes.has_opt_float = true;
  80. alltypes.opt_float = 3050.0f;
  81. alltypes.has_opt_fixed64 = true;
  82. alltypes.opt_fixed64 = 3051;
  83. alltypes.has_opt_sfixed64 = true;
  84. alltypes.opt_sfixed64 = 3052;
  85. alltypes.has_opt_double = true;
  86. alltypes.opt_double = 3053.0;
  87. alltypes.has_opt_string = true;
  88. strcpy(alltypes.opt_string, "3054");
  89. alltypes.has_opt_bytes = true;
  90. alltypes.opt_bytes.size = 4;
  91. memcpy(alltypes.opt_bytes.bytes, "3055", 4);
  92. alltypes.has_opt_submsg = true;
  93. strcpy(alltypes.opt_submsg.substuff1, "3056");
  94. alltypes.opt_submsg.substuff2 = 3056;
  95. alltypes.has_opt_enum = true;
  96. alltypes.opt_enum = MyEnum_Truth;
  97. }
  98. alltypes.end = 1099;
  99. {
  100. uint8_t buffer[1024];
  101. pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
  102. /* Now encode it and check if we succeeded. */
  103. if (pb_encode(&stream, AllTypes_fields, &alltypes))
  104. {
  105. SET_BINARY_MODE(stdout);
  106. fwrite(buffer, 1, stream.bytes_written, stdout);
  107. return 0; /* Success */
  108. }
  109. else
  110. {
  111. fprintf(stderr, "Encoding failed!\n");
  112. return 1; /* Failure */
  113. }
  114. }
  115. }