encode_extensions.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Tests extension fields.
  2. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <pb_encode.h>
  7. #include "alltypes.pb.h"
  8. #include "extensions.pb.h"
  9. #include "test_helpers.h"
  10. int main(int argc, char **argv)
  11. {
  12. uint8_t buffer[1024];
  13. pb_ostream_t stream;
  14. AllTypes alltypes = {0};
  15. int32_t extensionfield1 = 12345;
  16. pb_extension_t ext1 = pb_extension_init_zero;
  17. ExtensionMessage extensionfield2 = {"test", 54321};
  18. pb_extension_t ext2 = pb_extension_init_zero;
  19. /* Set up the extensions */
  20. alltypes.extensions = &ext1;
  21. ext1.type = &AllTypes_extensionfield1;
  22. ext1.dest = &extensionfield1;
  23. ext1.next = &ext2;
  24. ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
  25. ext2.dest = &extensionfield2;
  26. ext2.next = NULL;
  27. /* Set up the output stream */
  28. stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
  29. /* Now encode the message and check if we succeeded. */
  30. if (pb_encode(&stream, AllTypes_fields, &alltypes))
  31. {
  32. SET_BINARY_MODE(stdout);
  33. fwrite(buffer, 1, stream.bytes_written, stdout);
  34. return 0; /* Success */
  35. }
  36. else
  37. {
  38. fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
  39. return 1; /* Failure */
  40. }
  41. /* Check that the field tags are properly generated */
  42. (void)AllTypes_extensionfield1_tag;
  43. (void)ExtensionMessage_AllTypes_extensionfield2_tag;
  44. }