protobuf.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * protobuf.h
  3. *
  4. * Created on: Dec 7, 2016
  5. * Author: JohnJones
  6. */
  7. #ifndef __PROTOBUF_H__
  8. #define __PROTOBUF_H__
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. enum WireType {
  12. WIRETYPE_VARINT,
  13. WIRETYPE_64BIT,
  14. WIRETYPE_LENGTH_DELIMITED,
  15. WIRETYPE_START_GROUP,
  16. WIRETYPE_END_GROUP,
  17. WIRETYPE_32BIT
  18. };
  19. /***
  20. * Encode a length delimited field into the buffer
  21. * @param field_number the field number
  22. * @param wire_type the wire type
  23. * @param incoming the values
  24. * @param incoming_length the lenght of incoming
  25. * @param buffer the pointer to where to place the encoded value
  26. * @param max_buffer_length the buffer length remaining
  27. * @param bytes_written the number of bytes written
  28. * @returns true(1) on success
  29. */
  30. int protobuf_encode_length_delimited(int field_number, enum WireType wire_type, const char* incoming, size_t incoming_length,
  31. unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
  32. int protobuf_decode_length_delimited(const unsigned char* buffer, size_t buffer_length, char** results, size_t *results_length, size_t* bytes_read);
  33. /***
  34. * encode a string into the buffer
  35. * @param field_number the field number
  36. * @param incoming the string value
  37. * @param buffer the pointer to where to place the encoded value
  38. * @param max_buffer_length the buffer length remaining
  39. * @param bytes_written the number of bytes written
  40. * @returns true(1) on success
  41. */
  42. int protobuf_encode_string(int field_number, enum WireType wire_type, const char* incoming, unsigned char* buffer,
  43. size_t max_buffer_length, size_t* bytes_written);
  44. /**
  45. * Pull a string from the protobuf buffer
  46. * @param the buffer, positioned at the field size
  47. * @param buffer_length the buffer length
  48. * @param results the results (NOTE: will allocate memory)
  49. * @param bytes_read the number of bytes read
  50. * @returns true(1) on success
  51. */
  52. int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, char** results, size_t* bytes_read);
  53. /***
  54. * encode a varint into the buffer
  55. * @param field_number the field number
  56. * @param field_type the field type
  57. * @param incoming the value
  58. * @param buffer the pointer to where to place the encoded value
  59. * @param max_buffer_length the buffer length remaining
  60. * @param bytes_written the number of bytes written
  61. * @returns true(1) on success
  62. */
  63. int protobuf_encode_varint(int field_number, enum WireType field_type, unsigned long long incoming, unsigned char* buffer,
  64. size_t max_buffer_length, size_t* bytes_written);
  65. int protobuf_decode_varint(const unsigned char* buffer, size_t buffer_length, unsigned long long* results, size_t* bytes_read);
  66. /***
  67. * retrieve field number and field type from current buffer at position 0
  68. * @param buffer the incoming buffer
  69. * @param buffer_length the length of the buffer
  70. * @param field_no the resultant field number
  71. * @param field_type the field type
  72. * @param bytes_read the number of bytes read from the buffer
  73. */
  74. int protobuf_decode_field_and_type(const unsigned char* buffer, int buffer_length, int *field_no, enum WireType *field_type, size_t* bytes_read);
  75. #endif /* PROTOBUF_H_ */