pack.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * MessagePack for Python packing routine
  3. *
  4. * Copyright (C) 2009 Naoki INADA
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include "sysdep.h"
  21. #include <limits.h>
  22. #include <string.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #ifdef _MSC_VER
  27. #define inline __inline
  28. #endif
  29. typedef struct msgpack_packer {
  30. char *buf;
  31. size_t length;
  32. size_t buf_size;
  33. bool use_bin_type;
  34. } msgpack_packer;
  35. typedef struct Packer Packer;
  36. static inline int msgpack_pack_int(msgpack_packer* pk, int d);
  37. static inline int msgpack_pack_long(msgpack_packer* pk, long d);
  38. static inline int msgpack_pack_long_long(msgpack_packer* pk, long long d);
  39. static inline int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d);
  40. static inline int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d);
  41. static inline int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d);
  42. //static inline int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d);
  43. static inline int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d);
  44. static inline int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d);
  45. static inline int msgpack_pack_uint32(msgpack_packer* pk, uint32_t d);
  46. static inline int msgpack_pack_uint64(msgpack_packer* pk, uint64_t d);
  47. static inline int msgpack_pack_int8(msgpack_packer* pk, int8_t d);
  48. static inline int msgpack_pack_int16(msgpack_packer* pk, int16_t d);
  49. static inline int msgpack_pack_int32(msgpack_packer* pk, int32_t d);
  50. static inline int msgpack_pack_int64(msgpack_packer* pk, int64_t d);
  51. static inline int msgpack_pack_float(msgpack_packer* pk, float d);
  52. static inline int msgpack_pack_double(msgpack_packer* pk, double d);
  53. static inline int msgpack_pack_nil(msgpack_packer* pk);
  54. static inline int msgpack_pack_true(msgpack_packer* pk);
  55. static inline int msgpack_pack_false(msgpack_packer* pk);
  56. static inline int msgpack_pack_array(msgpack_packer* pk, unsigned int n);
  57. static inline int msgpack_pack_map(msgpack_packer* pk, unsigned int n);
  58. static inline int msgpack_pack_raw(msgpack_packer* pk, size_t l);
  59. static inline int msgpack_pack_bin(msgpack_packer* pk, size_t l);
  60. static inline int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
  61. static inline int msgpack_pack_ext(msgpack_packer* pk, int8_t typecode, size_t l);
  62. static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l)
  63. {
  64. char* buf = pk->buf;
  65. size_t bs = pk->buf_size;
  66. size_t len = pk->length;
  67. if (len + l > bs) {
  68. bs = (len + l) * 2;
  69. buf = (char*)realloc(buf, bs);
  70. if (!buf) return -1;
  71. }
  72. memcpy(buf + len, data, l);
  73. len += l;
  74. pk->buf = buf;
  75. pk->buf_size = bs;
  76. pk->length = len;
  77. return 0;
  78. }
  79. #define msgpack_pack_append_buffer(user, buf, len) \
  80. return msgpack_pack_write(user, (const char*)buf, len)
  81. #include "pack_template.h"
  82. #ifdef __cplusplus
  83. }
  84. #endif