pack.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * MessagePack for C packing routine
  3. *
  4. * Copyright (C) 2008-2009 FURUHASHI Sadayuki
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or copy at
  8. * http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef MSGPACK_PACK_H
  11. #define MSGPACK_PACK_H
  12. #include "pack_define.h"
  13. #include "object.h"
  14. #include "timestamp.h"
  15. #include <stdlib.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * @defgroup msgpack_buffer Buffers
  21. * @ingroup msgpack
  22. * @{
  23. * @}
  24. */
  25. /**
  26. * @defgroup msgpack_pack Serializer
  27. * @ingroup msgpack
  28. * @{
  29. */
  30. typedef int (*msgpack_packer_write)(void* data, const char* buf, size_t len);
  31. typedef struct msgpack_packer {
  32. void* data;
  33. msgpack_packer_write callback;
  34. } msgpack_packer;
  35. static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback);
  36. static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback);
  37. static void msgpack_packer_free(msgpack_packer* pk);
  38. static int msgpack_pack_char(msgpack_packer* pk, char d);
  39. static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d);
  40. static int msgpack_pack_short(msgpack_packer* pk, short d);
  41. static int msgpack_pack_int(msgpack_packer* pk, int d);
  42. static int msgpack_pack_long(msgpack_packer* pk, long d);
  43. static int msgpack_pack_long_long(msgpack_packer* pk, long long d);
  44. static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d);
  45. static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d);
  46. static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d);
  47. static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d);
  48. static int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d);
  49. static int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d);
  50. static int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d);
  51. static int msgpack_pack_uint32(msgpack_packer* pk, uint32_t d);
  52. static int msgpack_pack_uint64(msgpack_packer* pk, uint64_t d);
  53. static int msgpack_pack_int8(msgpack_packer* pk, int8_t d);
  54. static int msgpack_pack_int16(msgpack_packer* pk, int16_t d);
  55. static int msgpack_pack_int32(msgpack_packer* pk, int32_t d);
  56. static int msgpack_pack_int64(msgpack_packer* pk, int64_t d);
  57. static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d);
  58. static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d);
  59. static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d);
  60. static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d);
  61. static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d);
  62. static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d);
  63. static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d);
  64. static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d);
  65. static int msgpack_pack_float(msgpack_packer* pk, float d);
  66. static int msgpack_pack_double(msgpack_packer* pk, double d);
  67. static int msgpack_pack_nil(msgpack_packer* pk);
  68. static int msgpack_pack_true(msgpack_packer* pk);
  69. static int msgpack_pack_false(msgpack_packer* pk);
  70. static int msgpack_pack_array(msgpack_packer* pk, size_t n);
  71. static int msgpack_pack_map(msgpack_packer* pk, size_t n);
  72. static int msgpack_pack_str(msgpack_packer* pk, size_t l);
  73. static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l);
  74. static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l);
  75. static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l);
  76. static int msgpack_pack_bin(msgpack_packer* pk, size_t l);
  77. static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l);
  78. static int msgpack_pack_ext(msgpack_packer* pk, size_t l, int8_t type);
  79. static int msgpack_pack_ext_body(msgpack_packer* pk, const void* b, size_t l);
  80. static int msgpack_pack_timestamp(msgpack_packer* pk, const msgpack_timestamp* d);
  81. MSGPACK_DLLEXPORT
  82. int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
  83. /** @} */
  84. #define msgpack_pack_inline_func(name) \
  85. inline int msgpack_pack ## name
  86. #define msgpack_pack_inline_func_cint(name) \
  87. inline int msgpack_pack ## name
  88. #define msgpack_pack_inline_func_fixint(name) \
  89. inline int msgpack_pack_fix ## name
  90. #define msgpack_pack_user msgpack_packer*
  91. #define msgpack_pack_append_buffer(user, buf, len) \
  92. return (*(user)->callback)((user)->data, (const char*)buf, len)
  93. #include "pack_template.h"
  94. inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback)
  95. {
  96. pk->data = data;
  97. pk->callback = callback;
  98. }
  99. inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback)
  100. {
  101. msgpack_packer* pk = (msgpack_packer*)calloc(1, sizeof(msgpack_packer));
  102. if(!pk) { return NULL; }
  103. msgpack_packer_init(pk, data, callback);
  104. return pk;
  105. }
  106. inline void msgpack_packer_free(msgpack_packer* pk)
  107. {
  108. free(pk);
  109. }
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif /* msgpack/pack.h */