| 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 | |
| 13 | #include "pack_define.h" |
| 14 | #include "object.h" |
| 15 | #include "timestamp.h" |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * @defgroup msgpack_buffer Buffers |
| 25 | * @ingroup msgpack |
| 26 | * @{ |
| 27 | * @} |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * @defgroup msgpack_pack Serializer |
| 32 | * @ingroup msgpack |
| 33 | * @{ |
| 34 | */ |
| 35 | |
| 36 | typedef int (*msgpack_packer_write)(void* data, const char* buf, size_t len); |
| 37 | |
| 38 | typedef struct msgpack_packer { |
| 39 | void* data; |
| 40 | msgpack_packer_write callback; |
| 41 | } msgpack_packer; |
| 42 | |
| 43 | static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback); |
| 44 | |
| 45 | static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback); |
| 46 | static void msgpack_packer_free(msgpack_packer* pk); |
| 47 | |
| 48 | static int msgpack_pack_char(msgpack_packer* pk, char d); |
| 49 | |
| 50 | static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d); |
| 51 | static int msgpack_pack_short(msgpack_packer* pk, short d); |
| 52 | static int msgpack_pack_int(msgpack_packer* pk, int d); |
| 53 | static int msgpack_pack_long(msgpack_packer* pk, long d); |
| 54 | static int msgpack_pack_long_long(msgpack_packer* pk, long long d); |
| 55 | static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d); |
| 56 | static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d); |
| 57 | static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d); |
| 58 | static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d); |
| 59 | static int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d); |
| 60 | |
| 61 | static int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d); |
| 62 | static int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d); |
| 63 | static int msgpack_pack_uint32(msgpack_packer* pk, uint32_t d); |
| 64 | static int msgpack_pack_uint64(msgpack_packer* pk, uint64_t d); |
| 65 | static int msgpack_pack_int8(msgpack_packer* pk, int8_t d); |
| 66 | static int msgpack_pack_int16(msgpack_packer* pk, int16_t d); |
| 67 | static int msgpack_pack_int32(msgpack_packer* pk, int32_t d); |
| 68 | static int msgpack_pack_int64(msgpack_packer* pk, int64_t d); |
| 69 | |
| 70 | static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d); |
| 71 | static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d); |
| 72 | static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d); |
| 73 | static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d); |
| 74 | static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d); |
| 75 | static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d); |
| 76 | static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d); |
| 77 | static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d); |
| 78 | |
| 79 | static int msgpack_pack_float(msgpack_packer* pk, float d); |
| 80 | static int msgpack_pack_double(msgpack_packer* pk, double d); |
| 81 | |
| 82 | static int msgpack_pack_nil(msgpack_packer* pk); |
| 83 | static int msgpack_pack_true(msgpack_packer* pk); |
| 84 | static int msgpack_pack_false(msgpack_packer* pk); |
| 85 | |
| 86 | static int msgpack_pack_array(msgpack_packer* pk, size_t n); |
| 87 | |
| 88 | static int msgpack_pack_map(msgpack_packer* pk, size_t n); |
| 89 | |
| 90 | static int msgpack_pack_str(msgpack_packer* pk, size_t l); |
| 91 | static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l); |
| 92 | static int msgpack_pack_str_with_body(msgpack_packer* pk, const void* b, size_t l); |
| 93 | |
| 94 | static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l); |
| 95 | static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l); |
| 96 | |
| 97 | static int msgpack_pack_bin(msgpack_packer* pk, size_t l); |
| 98 | static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l); |
| 99 | static int msgpack_pack_bin_with_body(msgpack_packer* pk, const void* b, size_t l); |
| 100 | |
| 101 | static int msgpack_pack_ext(msgpack_packer* pk, size_t l, int8_t type); |
| 102 | static int msgpack_pack_ext_body(msgpack_packer* pk, const void* b, size_t l); |
| 103 | static int msgpack_pack_ext_with_body(msgpack_packer* pk, const void* b, size_t l, int8_t type); |
| 104 | |
| 105 | static int msgpack_pack_timestamp(msgpack_packer* pk, const msgpack_timestamp* d); |
| 106 | |
| 107 | MSGPACK_DLLEXPORT |
| 108 | int msgpack_pack_object(msgpack_packer* pk, msgpack_object d); |
| 109 | |
| 110 | |
| 111 | /** @} */ |
| 112 | |
| 113 | |
| 114 | #define msgpack_pack_inline_func(name) \ |
| 115 | inline int msgpack_pack ## name |
| 116 | |
| 117 | #define msgpack_pack_inline_func_cint(name) \ |
| 118 | inline int msgpack_pack ## name |
| 119 | |
| 120 | #define msgpack_pack_inline_func_fixint(name) \ |
| 121 | inline int msgpack_pack_fix ## name |
| 122 | |
| 123 | #define msgpack_pack_user msgpack_packer* |
| 124 | |
| 125 | #define msgpack_pack_append_buffer(user, buf, len) \ |
| 126 | return (*(user)->callback)((user)->data, (const char*)buf, len) |
| 127 | |
| 128 | #include "pack_template.h" |
| 129 | |
| 130 | inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback) |
| 131 | { |
| 132 | pk->data = data; |
| 133 | pk->callback = callback; |
| 134 | } |
| 135 | |
| 136 | inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback) |
| 137 | { |
| 138 | msgpack_packer* pk = (msgpack_packer*)calloc(1, sizeof(msgpack_packer)); |
| 139 | if(!pk) { return NULL; } |
| 140 | msgpack_packer_init(pk, data, callback); |
| 141 | return pk; |
| 142 | } |
| 143 | |
| 144 | inline void msgpack_packer_free(msgpack_packer* pk) |
| 145 | { |
| 146 | free(pk); |
| 147 | } |
| 148 | |
| 149 | inline int msgpack_pack_str_with_body(msgpack_packer* pk, const void* b, size_t l) |
| 150 | { |
| 151 | int ret = msgpack_pack_str(pk, l); |
| 152 | if (ret != 0) { return ret; } |
| 153 | return msgpack_pack_str_body(pk, b, l); |
| 154 | } |
| 155 | |
| 156 | inline int msgpack_pack_bin_with_body(msgpack_packer* pk, const void* b, size_t l) |
| 157 | { |
| 158 | int ret = msgpack_pack_bin(pk, l); |
| 159 | if (ret != 0) { return ret; } |
| 160 | return msgpack_pack_bin_body(pk, b, l); |
| 161 | } |
| 162 | |
| 163 | inline int msgpack_pack_ext_with_body(msgpack_packer* pk, const void* b, size_t l, int8_t type) |
| 164 | { |
| 165 | int ret = msgpack_pack_ext(pk, l, type); |
| 166 | if (ret != 0) { return ret; } |
| 167 | return msgpack_pack_ext_body(pk, b, l); |
| 168 | } |
| 169 | |
| 170 | #ifdef __cplusplus |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | #endif /* msgpack/pack.h */ |