protobuf.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /***
  2. * Helper to derived protobuf objects
  3. */
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "varint.h"
  7. #include "protobuf.h"
  8. int protobuf_encode_length_delimited(int field_number, enum WireType field_type, const char* incoming, size_t incoming_length,
  9. unsigned char* buffer, size_t max_buffer_length, size_t* bytes_written)
  10. {
  11. // push the field number and wire type together
  12. unsigned int field_no = field_number << 3;
  13. unsigned long long field = field_no | field_type;
  14. size_t bytes_processed = 0;
  15. *bytes_written = 0;
  16. // field type & number
  17. varint_encode(field, buffer, max_buffer_length, &bytes_processed);
  18. *bytes_written += bytes_processed;
  19. // field size
  20. varint_encode(incoming_length, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_processed);
  21. *bytes_written += bytes_processed;
  22. // field value
  23. if (incoming_length > 0) {
  24. memcpy(&buffer[*bytes_written], incoming, incoming_length);
  25. *bytes_written += incoming_length;
  26. }
  27. return 1;
  28. }
  29. int protobuf_decode_length_delimited(const unsigned char* buffer, size_t buffer_length, char** results, size_t *results_length, size_t* bytes_read) {
  30. size_t pos = 0;
  31. *bytes_read = 0;
  32. // grab the field size
  33. size_t field_size = varint_decode(&buffer[pos], buffer_length - pos, bytes_read);
  34. pos += *bytes_read;
  35. // allocate memory
  36. *results = malloc(sizeof(char) * field_size);
  37. if ((*results) == NULL)
  38. return 0;
  39. memset(*results, 0, field_size);
  40. // copy the bytes
  41. memcpy( *results, &buffer[pos], field_size);
  42. pos += field_size;
  43. // set return values
  44. *bytes_read = pos;
  45. *results_length = field_size;
  46. return 1;
  47. }
  48. /***
  49. * encode a string into the buffer
  50. * @param field_number the field number
  51. * @param field_type the field type
  52. * @param incoming the string value
  53. * @param buffer the pointer to where to place the encoded value
  54. * @param max_buffer_length the buffer length remaining
  55. * @param bytes_written the number of bytes written
  56. * @returns true(1) on success
  57. */
  58. int protobuf_encode_string(int field_number, enum WireType field_type, const char* incoming, unsigned char* buffer,
  59. size_t max_buffer_length, size_t* bytes_written) {
  60. return protobuf_encode_length_delimited(field_number, field_type, incoming, strlen(incoming), buffer, max_buffer_length, bytes_written);
  61. }
  62. /***
  63. * encode a varint into the buffer
  64. * @param field_number the field number
  65. * @param field_type the field type
  66. * @param incoming the value
  67. * @param buffer the pointer to where to place the encoded value
  68. * @param max_buffer_length the buffer length remaining
  69. * @param bytes_written the number of bytes written
  70. * @returns true(1) on success
  71. */
  72. int protobuf_encode_varint(int field_number, enum WireType field_type, unsigned long long incoming, unsigned char* buffer,
  73. size_t max_buffer_length, size_t* bytes_written) {
  74. *bytes_written = 0;
  75. // push the field number and wire type together
  76. unsigned int field_no = field_number << 3;
  77. unsigned long long field = field_no | field_type;
  78. size_t bytes_processed;
  79. // field type & number
  80. varint_encode(field, buffer, max_buffer_length, &bytes_processed);
  81. *bytes_written += bytes_processed;
  82. // field value
  83. varint_encode(incoming, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_processed);
  84. *bytes_written += bytes_processed;
  85. return 1;
  86. }
  87. int protobuf_decode_varint(const unsigned char* buffer, size_t buffer_length, unsigned long long* results, size_t* bytes_read) {
  88. *results = varint_decode(buffer, buffer_length, bytes_read);
  89. return 1;
  90. }
  91. /**
  92. * Pull a string from the protobuf buffer
  93. * @param the buffer, positioned at the field size
  94. * @param buffer_length the buffer length
  95. * @param results the results (NOTE: will allocate memory)
  96. * @param bytes_read the number of bytes read
  97. * @returns true(1) on success
  98. */
  99. int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, char** results, size_t* bytes_read) {
  100. size_t pos = 0;
  101. *bytes_read = 0;
  102. // grab the field size
  103. int length = varint_decode(&buffer[pos], buffer_length - pos, bytes_read);
  104. pos += *bytes_read;
  105. *bytes_read += length;
  106. // allocate memory (if neccesary)
  107. if (length > 0) {
  108. *results = malloc(sizeof(char) * length + 1);
  109. if ((*results) == NULL)
  110. return 0;
  111. memset(*results, 0, length+1);
  112. // copy the string
  113. memcpy((*results), &buffer[pos], length);
  114. // don't forget the null
  115. (*results)[length] = 0;
  116. }
  117. return 1;
  118. }
  119. /***
  120. * retrieve field number and field type from current buffer at position 0
  121. * @param buffer the incoming buffer
  122. * @param buffer_length the length of the buffer
  123. * @param field_no the resultant field number
  124. * @param field_type the field type
  125. * @param bytes_read the number of bytes read from the buffer
  126. */
  127. int protobuf_decode_field_and_type(const unsigned char* buffer, int buffer_length, int *field_no, enum WireType *field_type, size_t* bytes_read) {
  128. *bytes_read = 0;
  129. unsigned long long field = varint_decode(buffer, buffer_length, bytes_read);
  130. *field_no = field >> 3;
  131. *field_type = field - (*field_no << 3);
  132. return 1;
  133. }