-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathyar_pack.c
360 lines (300 loc) · 8.29 KB
/
yar_pack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* Yar - Concurrent RPC Server for PHP, C etc
*
* Copyright (C) 2012-2012 Xinchen Hui <laruence at gmail dot com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdarg.h> /* for va_list */
#include <string.h>
#include "msgpack.h"
#include "yar_common.h"
#include "yar_pack.h"
struct _yar_packager {
msgpack_packer *pk;
msgpack_sbuffer *bf;
};
struct _yar_data {
msgpack_object obj;
};
struct _yar_unpackager {
msgpack_unpacked msg;
};
struct _yar_unpack_iterator {
uint size;
uint position;
yar_data *data;
};
yar_packager * yar_pack_start(yar_data_type type, uint size) /* {{{ */ {
yar_packager *packager = malloc(sizeof(yar_packager));
packager->bf = msgpack_sbuffer_new();
packager->pk = msgpack_packer_new(packager->bf, msgpack_sbuffer_write);
switch (type) {
case YAR_DATA_ARRAY:
msgpack_pack_array(packager->pk, size);
break;
case YAR_DATA_MAP:
msgpack_pack_map(packager->pk, size);
break;
default:
break;
}
return packager;
}
/* }}} */
int yar_pack_push_array(yar_packager *packager, uint size) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_array(pk, size) < 0? 0: 1;
}
/* }}} */
int yar_pack_push_map(yar_packager *packager, uint size) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_map(pk, size) < 0? 0 : 1;
}
/* }}} */
int yar_pack_push_null(yar_packager *packager) /* {{{ */ {
msgpack_packer *pk = packager->pk;
msgpack_pack_nil(pk);
return 1;
}
/* }}} */
int yar_pack_push_bool(yar_packager *packager, int val) /* {{{ */ {
msgpack_packer *pk = packager->pk;
if (val == 0) {
if (msgpack_pack_false(pk) < 0) {
return 0;
}
} else {
if (msgpack_pack_true(pk) < 0) {
return 0;
}
}
return 1;
}
/* }}} */
int yar_pack_push_long(yar_packager *packager, long num) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_int64(pk, num) < 0? 0 : 1;
}
/* }}} */
int yar_pack_push_ulong(yar_packager *packager, ulong num) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_uint64(pk, num) < 0? 0 : 1;
}
/* }}} */
int yar_pack_push_double(yar_packager *packager, double num) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_double(pk, num) < 0? 0 : 1;
}
/* }}} */
int yar_pack_push_string(yar_packager *packager, char *str, uint len) /* {{{ */ {
int ret;
msgpack_packer *pk = packager->pk;
ret = msgpack_pack_str(pk, len);
if (ret < 0) {
return 0;
}
if (msgpack_pack_str_body(pk, str, len) < 0) {
return 0;
}
return 1;
}
/* }}} */
int yar_pack_push_data(yar_packager *packager, const yar_data *data) /* {{{ */ {
msgpack_packer *pk = packager->pk;
return msgpack_pack_object(pk, *(msgpack_object *)data) < 0? 0 : 1;
}
/* }}} */
int yar_pack_push_packager(yar_packager *packager, yar_packager *data) /* {{{ */ {
msgpack_sbuffer *buffer = packager->bf;
yar_payload payload = {0};
yar_pack_to_string(data, &payload);
return msgpack_sbuffer_write(buffer, payload.data, payload.size) < 0? 0 : 1;
}
/* }}} */
int yar_pack_to_string(yar_packager *packager, yar_payload *payload) /* {{{ */ {
msgpack_sbuffer *bf = packager->bf;
payload->size = bf->size;
payload->data = bf->data;
return 1;
}
/* }}} */
void yar_pack_free(yar_packager *packager) /* {{{ */ {
msgpack_sbuffer_free(packager->bf);
msgpack_packer_free(packager->pk);
free(packager);
}
/* }}} */
void yar_unpack_free(yar_unpackager *unpk) /* {{{ */ {
msgpack_unpacked_destroy(&unpk->msg);
free(unpk);
}
/* }}} */
yar_unpackager * yar_unpack_init(char *data, uint len) /* {{{ */ {
yar_unpackager *unpk = malloc(sizeof(yar_unpackager));
msgpack_unpacked_init(&unpk->msg);
if (!msgpack_unpack_next(&unpk->msg, data, len, NULL)) {
yar_unpack_free(unpk);
return NULL;
}
return unpk;
}
/* }}} */
const yar_data * yar_unpack_unpack(yar_unpackager *unpk) /* {{{ */ {
return (const yar_data *)&unpk->msg.data;
}
/* }}} */
yar_data_type yar_unpack_data_type(const yar_data *data, uint *size) /* {{{ */ {
msgpack_object *obj = (msgpack_object *)data;
switch (obj->type) {
case MSGPACK_OBJECT_NIL:
return YAR_DATA_NULL;
case MSGPACK_OBJECT_BOOLEAN:
return YAR_DATA_BOOL;
case MSGPACK_OBJECT_POSITIVE_INTEGER:
return YAR_DATA_ULONG;
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
return YAR_DATA_LONG;
case MSGPACK_OBJECT_FLOAT:
return YAR_DATA_DOUBLE;
case MSGPACK_OBJECT_STR:
*size = obj->via.str.size;
return YAR_DATA_STRING;
case MSGPACK_OBJECT_ARRAY:
*size = obj->via.array.size;
return YAR_DATA_ARRAY;
case MSGPACK_OBJECT_MAP:
*size = obj->via.map.size;
return YAR_DATA_MAP;
}
return 0;
}
/* }}} */
int yar_unpack_data_value(const yar_data *data, void *arg) /* {{{ */ {
msgpack_object *obj = (msgpack_object *)data;
switch (obj->type) {
case MSGPACK_OBJECT_NIL:
*(void **)arg = NULL;
return YAR_DATA_NULL;
case MSGPACK_OBJECT_BOOLEAN:
*(int *)arg = obj->via.boolean;
return YAR_DATA_BOOL;
case MSGPACK_OBJECT_POSITIVE_INTEGER:
*(ulong *)arg = obj->via.u64;
return YAR_DATA_ULONG;
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
*(long *)arg = obj->via.i64;
return YAR_DATA_LONG;
case MSGPACK_OBJECT_FLOAT:
*(double *)arg = obj->via.f64;
return YAR_DATA_DOUBLE;
case MSGPACK_OBJECT_STR:
*(const char **)arg = obj->via.str.ptr;
return YAR_DATA_STRING;
case MSGPACK_OBJECT_ARRAY:
*(const yar_data **)arg = (yar_data *)obj->via.array.ptr;
return YAR_DATA_ARRAY;
case MSGPACK_OBJECT_MAP:
*(const yar_data **)arg = (yar_data *)obj->via.map.ptr;
return YAR_DATA_MAP;
}
return 0;
}
/* }}} */
int yar_unpack_data_null(const yar_data *data, int *val) /* {{{ */ {
return yar_unpack_data_value(data, &val);
}
/* }}} */
int yar_unpack_data_bool(const yar_data *data, int *bval) /* {{{ */ {
return yar_unpack_data_value(data, bval);
}
/* }}} */
int yar_unpack_data_long(const yar_data *data, long *num) /* {{{ */ {
return yar_unpack_data_value(data, num);
}
/* }}} */
int yar_unpack_data_ulong(const yar_data *data, ulong *num) /* {{{ */ {
return yar_unpack_data_value(data, num);
}
/* }}} */
int yar_unpack_data_string(const yar_data *data, const char **str) /* {{{ */ {
return yar_unpack_data_value(data, str);
}
/* }}} */
int yar_unpack_data_array(const yar_data *data, const yar_data **arg) /* {{{ */ {
return yar_unpack_data_value(data, arg);
}
/* }}} */
int yar_unpack_data_map(const yar_data *data, const yar_data **arg) /* {{{ */ {
return yar_unpack_data_value(data, arg);
}
/* }}} */
yar_unpack_iterator * yar_unpack_iterator_init(const yar_data *data) /* {{{ */ {
yar_unpack_iterator *it = NULL;
switch (data->obj.type) {
case MSGPACK_OBJECT_ARRAY:
{
it = malloc(sizeof(yar_unpack_iterator));
it->position = 0;
it->size = data->obj.via.array.size;
it->data = (yar_data *)data->obj.via.array.ptr;
}
break;
case MSGPACK_OBJECT_MAP:
{
it = malloc(sizeof(yar_unpack_iterator));
it->position = 0;
it->size = data->obj.via.map.size * 2; /* kv */
it->data = (yar_data *)data->obj.via.map.ptr;
}
break;
default:
break;
}
return it;
}
/* }}} */
void yar_unpack_iterator_reset(yar_unpack_iterator *it) /* {{{ */ {
it->position = 0;
}
/* }}} */
int yar_unpack_iterator_next(yar_unpack_iterator *it) /* {{{ */ {
if (it->position + 1 < it->size) {
return ++it->position;
}
return 0;
}
/* }}} */
const yar_data *yar_unpack_iterator_current(yar_unpack_iterator *it) /* {{{ */ {
msgpack_object *obj = (msgpack_object *)it->data;
obj += it->position;
return (yar_data *)obj;
}
/* }}} */
void yar_unpack_iterator_free(yar_unpack_iterator *it) /* {{{ */ {
free(it);
}
/* }}} */
void yar_debug_print_data(const yar_data *data, FILE *fp) /* {{{ */ {
msgpack_object_print(fp? fp : stdout, *(msgpack_object *)data);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/