MessagePack for C
pack.h
Go to the documentation of this file.
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 <stdlib.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 
35 typedef int (*msgpack_packer_write)(void* data, const char* buf, size_t len);
36 
37 typedef struct msgpack_packer {
38  void* data;
41 
42 static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback);
43 
44 static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback);
45 static void msgpack_packer_free(msgpack_packer* pk);
46 
47 static int msgpack_pack_char(msgpack_packer* pk, char d);
48 
49 static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d);
50 static int msgpack_pack_short(msgpack_packer* pk, short d);
51 static int msgpack_pack_int(msgpack_packer* pk, int d);
52 static int msgpack_pack_long(msgpack_packer* pk, long d);
53 static int msgpack_pack_long_long(msgpack_packer* pk, long long d);
54 static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d);
55 static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d);
56 static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d);
57 static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d);
58 static int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d);
59 
60 static int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d);
61 static int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d);
62 static int msgpack_pack_uint32(msgpack_packer* pk, uint32_t d);
63 static int msgpack_pack_uint64(msgpack_packer* pk, uint64_t d);
64 static int msgpack_pack_int8(msgpack_packer* pk, int8_t d);
65 static int msgpack_pack_int16(msgpack_packer* pk, int16_t d);
66 static int msgpack_pack_int32(msgpack_packer* pk, int32_t d);
67 static int msgpack_pack_int64(msgpack_packer* pk, int64_t d);
68 
69 static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d);
70 static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d);
71 static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d);
72 static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d);
73 static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d);
74 static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d);
75 static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d);
76 static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d);
77 
78 static int msgpack_pack_float(msgpack_packer* pk, float d);
79 static int msgpack_pack_double(msgpack_packer* pk, double d);
80 
81 static int msgpack_pack_nil(msgpack_packer* pk);
82 static int msgpack_pack_true(msgpack_packer* pk);
83 static int msgpack_pack_false(msgpack_packer* pk);
84 
85 static int msgpack_pack_array(msgpack_packer* pk, size_t n);
86 
87 static int msgpack_pack_map(msgpack_packer* pk, size_t n);
88 
89 static int msgpack_pack_str(msgpack_packer* pk, size_t l);
90 static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l);
91 
92 static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l);
93 static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l);
94 
95 static int msgpack_pack_bin(msgpack_packer* pk, size_t l);
96 static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l);
97 
98 static int msgpack_pack_ext(msgpack_packer* pk, size_t l, int8_t type);
99 static int msgpack_pack_ext_body(msgpack_packer* pk, const void* b, size_t l);
100 
102 
103 
107 #define msgpack_pack_inline_func(name) \
108  inline int msgpack_pack ## name
109 
110 #define msgpack_pack_inline_func_cint(name) \
111  inline int msgpack_pack ## name
112 
113 #define msgpack_pack_inline_func_fixint(name) \
114  inline int msgpack_pack_fix ## name
115 
116 #define msgpack_pack_user msgpack_packer*
117 
118 #define msgpack_pack_append_buffer(user, buf, len) \
119  return (*(user)->callback)((user)->data, (const char*)buf, len)
120 
121 #include "pack_template.h"
122 
123 inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback)
124 {
125  pk->data = data;
126  pk->callback = callback;
127 }
128 
129 inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback)
130 {
131  msgpack_packer* pk = (msgpack_packer*)calloc(1, sizeof(msgpack_packer));
132  if(!pk) { return NULL; }
133  msgpack_packer_init(pk, data, callback);
134  return pk;
135 }
136 
137 inline void msgpack_packer_free(msgpack_packer* pk)
138 {
139  free(pk);
140 }
141 
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif /* msgpack/pack.h */
struct msgpack_packer msgpack_packer
int(* msgpack_packer_write)(void *data, const char *buf, size_t len)
Definition: pack.h:35
msgpack_packer_write callback
Definition: pack.h:39
void * data
Definition: pack.h:38
const void * n
Definition: unpack_template.h:95
Definition: pack.h:37
int msgpack_pack_object(msgpack_packer *pk, msgpack_object d)
Definition: object.h:90
const char * data
Definition: unpack_template.h:89
const char size_t len
Definition: unpack_template.h:89