epb.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // epb.h
  2. // MicroMessenger
  3. //
  4. // Created by harlliu@tencent.com on 14-02-15.
  5. // Copyright 2014 Tencent. All rights reserved.
  6. //
  7. // Version : 1.0.2
  8. #ifndef __EPB_H__
  9. #define __EPB_H__
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. typedef struct
  13. {
  14. uint8_t *data;
  15. int len;
  16. } Bytes;
  17. typedef struct
  18. {
  19. const uint8_t *data;
  20. int len;
  21. } CBytes;
  22. typedef struct
  23. {
  24. char *str;
  25. int len;
  26. } String;
  27. typedef struct
  28. {
  29. const char *str;
  30. int len;
  31. } CString;
  32. typedef uint8_t Message;
  33. typedef struct
  34. {
  35. const uint8_t *unpack_buf;
  36. uint8_t *pack_buf;
  37. int buf_len;
  38. int buf_offset;
  39. } Epb;
  40. /*
  41. * embeded protobuf unpack functions
  42. */
  43. void epb_unpack_init(Epb *e, const uint8_t *buf, int len);
  44. bool epb_has_tag(Epb *e, uint16_t tag);
  45. //Varint
  46. int32_t epb_get_int32(Epb *e, uint16_t tag);
  47. uint32_t epb_get_uint32(Epb *e, uint16_t tag);
  48. int32_t epb_get_sint32(Epb *e, uint16_t tag);
  49. bool epb_get_bool(Epb *e, uint16_t tag);
  50. int epb_get_enum(Epb *e, uint16_t tag);
  51. //Length Delimited
  52. const char *epb_get_string(Epb *e, uint16_t tag, int *len);
  53. const uint8_t *epb_get_bytes(Epb *e, uint16_t tag, int *len);
  54. const Message *epb_get_message(Epb *e, uint16_t tag, int *len);
  55. //Length Delimited Packed Repeadted Field
  56. //TODO
  57. //Fixed32
  58. uint32_t epb_get_fixed32(Epb *e, uint16_t tag);
  59. int32_t epb_get_sfixed32(Epb *e, uint16_t tag);
  60. float epb_get_float(Epb *e, uint16_t tag);
  61. /*
  62. * embeded protobuf pack functions
  63. */
  64. void epb_pack_init(Epb *e, uint8_t *buf, int len);
  65. int epb_get_packed_size(Epb *e);
  66. //Varint
  67. int epb_set_int32(Epb *e, uint16_t tag, int32_t value);
  68. int epb_set_uint32(Epb *e, uint16_t tag, uint32_t value);
  69. int epb_set_sint32(Epb *e, uint16_t tag, int32_t value);
  70. int epb_set_bool(Epb *e, uint16_t tag, bool value);
  71. int epb_set_enum(Epb *e, uint16_t tag, int value);
  72. //Length Delimited
  73. int epb_set_string(Epb *e, uint16_t tag, const char *data, int len);
  74. int epb_set_bytes(Epb *e, uint16_t tag, const uint8_t *data, int len);
  75. int epb_set_message(Epb *e, uint16_t tag, const Message *data, int len);
  76. //Length Delimited Packed Repeadted Field
  77. //TODO
  78. //Fixed32
  79. int epb_set_fixed32(Epb *e, uint16_t tag, uint32_t value);
  80. int epb_set_sfixed32(Epb *e, uint16_t tag, int32_t value);
  81. int epb_set_float(Epb *e, uint16_t tag, float value);
  82. //Pack size
  83. int epb_varint32_pack_size(uint16_t tag, uint32_t value, bool is_signed);
  84. int epb_fixed32_pack_size(uint16_t tag);
  85. int epb_length_delimited_pack_size(uint16_t tag, int len);
  86. #endif