forked from jarjin/tolua_runtime_V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmessage.c
455 lines (420 loc) · 11 KB
/
rmessage.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "pbc.h"
#include "alloc.h"
#include "map.h"
#include "context.h"
#include "proto.h"
#include "pattern.h"
#include "varint.h"
#include <stddef.h>
#include <string.h>
struct pbc_rmessage {
struct _message * msg;
struct map_sp * index; // key -> struct value *
struct heap * heap;
};
union _var {
pbc_var var;
pbc_array array;
struct pbc_rmessage message;
} ;
struct value {
struct _field * type;
union _var v;
};
int
pbc_rmessage_next(struct pbc_rmessage *m, const char **key) {
struct value * v = (struct value *)_pbcM_sp_next(m->index, key);
if (*key == NULL) {
return 0;
}
return _pbcP_type(v->type, NULL);
}
#define SIZE_VAR (offsetof(struct value, v) + sizeof(pbc_var))
#define SIZE_ARRAY (offsetof(struct value, v) + sizeof(pbc_array))
#define SIZE_MESSAGE (offsetof(struct value, v) + sizeof(struct pbc_rmessage))
static struct value *
read_string(struct heap *h, struct atom *a,struct _field *f, uint8_t *buffer) {
const char * temp = (const char *) (buffer + a->v.s.start);
int len = a->v.s.end - a->v.s.start;
if (len > 0 && temp[len-1] == '\0') {
struct value * v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->s.str = temp;
v->v.var->s.len = len;
return v;
} else {
struct value * v = (struct value *)_pbcH_alloc(h, SIZE_VAR + len + 1);
memcpy(((char *)v) + SIZE_VAR , temp, len);
*(((char *)v) + SIZE_VAR + len) = '\0';
v->v.var->s.str = ((char *)v) + SIZE_VAR;
v->v.var->s.len = len;
return v;
}
}
static void
read_string_var(struct heap *h, pbc_var var,struct atom *a,struct _field *f,uint8_t *buffer) {
const char * temp = (const char *) (buffer + a->v.s.start);
int len = a->v.s.end - a->v.s.start;
if (len == 0) {
var->s.str = "";
var->s.len = 0;
}
else if (temp[len-1] == '\0') {
var->s.str = temp;
var->s.len = len;
} else {
char * temp2 = (char *)_pbcH_alloc(h, len + 1);
memcpy(temp2, temp, len);
temp2[len]='\0';
var->s.str = temp2;
var->s.len = -len;
}
}
static void _pbc_rmessage_new(struct pbc_rmessage * ret , struct _message * type , void *buffer, int size, struct heap *h);
static struct value *
read_value(struct heap *h, struct _field *f, struct atom * a, uint8_t *buffer) {
struct value * v;
switch (f->type) {
case PTYPE_DOUBLE:
CHECK_BIT64(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->real = read_double(a);
break;
case PTYPE_FLOAT:
CHECK_BIT32(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->real = (double) read_float(a);
break;
case PTYPE_ENUM:
CHECK_VARINT(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->e.id = a->v.i.low;
v->v.var->e.name = (const char *)_pbcM_ip_query(f->type_name.e->id , a->v.i.low);
break;
case PTYPE_INT64:
case PTYPE_UINT64:
case PTYPE_INT32:
case PTYPE_UINT32:
case PTYPE_BOOL:
CHECK_VARINT(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->integer = a->v.i;
break;
case PTYPE_FIXED32:
case PTYPE_SFIXED32:
CHECK_BIT32(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->integer = a->v.i;
break;
case PTYPE_FIXED64:
case PTYPE_SFIXED64:
CHECK_BIT64(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->integer = a->v.i;
break;
case PTYPE_SINT32:
CHECK_VARINT(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->integer = a->v.i;
_pbcV_dezigzag32(&(v->v.var->integer));
break;
case PTYPE_SINT64:
CHECK_VARINT(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->integer = a->v.i;
_pbcV_dezigzag64(&(v->v.var->integer));
break;
case PTYPE_STRING:
CHECK_LEND(a,NULL);
v = read_string(h,a,f,buffer);
break;
case PTYPE_BYTES:
CHECK_LEND(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_VAR);
v->v.var->s.str = (const char *)(buffer + a->v.s.start);
v->v.var->s.len = a->v.s.end - a->v.s.start;
break;
case PTYPE_MESSAGE:
CHECK_LEND(a,NULL);
v = (struct value *)_pbcH_alloc(h, SIZE_MESSAGE);
_pbc_rmessage_new(&(v->v.message), f->type_name.m ,
buffer + a->v.s.start ,
a->v.s.end - a->v.s.start,h);
break;
default:
return NULL;
}
v->type = f;
return v;
}
static void
push_value_packed(struct _message * type, pbc_array array, struct _field *f, struct atom * aa, uint8_t *buffer) {
int n = _pbcP_unpack_packed((uint8_t *)buffer + aa->v.s.start, aa->v.s.end - aa->v.s.start,
f->type , array);
if (n<=0) {
// todo : error
type->env->lasterror = "Unpack packed field error";
return;
}
if (f->type == PTYPE_ENUM) {
int i;
for (i=0;i<n;i++) {
union _pbc_var * v = (union _pbc_var *)_pbcA_index_p(array, i);
int id = v->integer.low;
v->e.id = id;
v->e.name = (const char*)_pbcM_ip_query(f->type_name.e->id , id);
}
}
}
static void
push_value_array(struct heap *h, pbc_array array, struct _field *f, struct atom * a, uint8_t *buffer) {
pbc_var v;
switch (f->type) {
case PTYPE_DOUBLE:
v->real = read_double(a);
break;
case PTYPE_FLOAT:
v->real = (double) read_float(a);
break;
case PTYPE_ENUM:
v->e.id = a->v.i.low;
v->e.name = (const char *)_pbcM_ip_query(f->type_name.e->id , a->v.i.low);
break;
case PTYPE_INT64:
case PTYPE_UINT64:
case PTYPE_INT32:
case PTYPE_UINT32:
case PTYPE_FIXED32:
case PTYPE_FIXED64:
case PTYPE_SFIXED32:
case PTYPE_SFIXED64:
case PTYPE_BOOL:
v->integer = a->v.i;
break;
case PTYPE_SINT32:
v->integer = a->v.i;
_pbcV_dezigzag32(&(v->integer));
break;
case PTYPE_SINT64:
v->integer = a->v.i;
_pbcV_dezigzag64(&(v->integer));
break;
case PTYPE_STRING:
CHECK_LEND(a, );
read_string_var(h,v,a,f,buffer);
break;
case PTYPE_BYTES:
CHECK_LEND(a, );
v->s.str = (const char *)(buffer + a->v.s.start);
v->s.len = a->v.s.end - a->v.s.start;
break;
case PTYPE_MESSAGE: {
CHECK_LEND(a, );
struct pbc_rmessage message;
_pbc_rmessage_new(&message, f->type_name.m ,
buffer + a->v.s.start ,
a->v.s.end - a->v.s.start,h);
if (message.msg == NULL) {
return;
}
v->p[0] = message.msg;
v->p[1] = message.index;
break;
}
default:
return;
}
_pbcA_push(array,v);
}
static void
_pbc_rmessage_new(struct pbc_rmessage * ret , struct _message * type , void *buffer, int size , struct heap *h) {
if (size == 0) {
ret->msg = type;
ret->index = _pbcM_sp_new(0 , h);
ret->heap = h;
return;
}
pbc_ctx _ctx;
int count = _pbcC_open(_ctx,buffer,size);
if (count <= 0) {
type->env->lasterror = "rmessage decode context error";
memset(ret , 0, sizeof(*ret));
return;
}
struct context * ctx = (struct context *)_ctx;
ret->msg = type;
ret->index = _pbcM_sp_new(count, h);
ret->heap = h;
int i;
for (i=0;i<ctx->number;i++) {
int id = ctx->a[i].wire_id >> 3;
struct _field * f = (struct _field *)_pbcM_ip_query(type->id , id);
if (f) {
if (f->label == LABEL_REPEATED || f->label == LABEL_PACKED) {
struct value * v;
void ** vv = _pbcM_sp_query_insert(ret->index, f->name);
if (*vv == NULL) {
v = (struct value *)_pbcH_alloc(h, SIZE_ARRAY);
v->type = f;
_pbcA_open_heap(v->v.array,ret->heap);
*vv = v;
} else {
v= (struct value *)*vv;
}
if (f->label == LABEL_PACKED) {
push_value_packed(type, v->v.array , f , &(ctx->a[i]), (uint8_t *)buffer);
if (pbc_array_size(v->v.array) == 0) {
type->env->lasterror = "rmessage decode packed data error";
*vv = NULL;
}
} else {
push_value_array(h,v->v.array , f, &(ctx->a[i]), (uint8_t *)buffer);
if (pbc_array_size(v->v.array) == 0) {
type->env->lasterror = "rmessage decode repeated data error";
*vv = NULL;
}
}
} else {
struct value * v = read_value(h, f, &(ctx->a[i]), (uint8_t *)buffer);
if (v) {
_pbcM_sp_insert(ret->index, f->name, v);
} else {
type->env->lasterror = "rmessage decode data error";
}
}
}
}
_pbcC_close(_ctx);
}
struct pbc_rmessage *
pbc_rmessage_new(struct pbc_env * env, const char * type_name , struct pbc_slice * slice) {
struct _message * msg = _pbcP_get_message(env, type_name);
if (msg == NULL) {
env->lasterror = "Proto not found";
return NULL;
}
struct pbc_rmessage temp;
struct heap * h = _pbcH_new(slice->len);
_pbc_rmessage_new(&temp, msg , slice->buffer, slice->len , h);
if (temp.msg == NULL) {
_pbcH_delete(h);
return NULL;
}
struct pbc_rmessage *m = (struct pbc_rmessage *)_pbcH_alloc(temp.heap, sizeof(*m));
*m = temp;
return m;
}
void
pbc_rmessage_delete(struct pbc_rmessage * m) {
if (m) {
_pbcH_delete(m->heap);
}
}
const char *
pbc_rmessage_string(struct pbc_rmessage * m , const char *key , int index, int *sz) {
struct value * v = (struct value *)_pbcM_sp_query(m->index,key);
int type = 0;
pbc_var var;
if (v == NULL) {
type = _pbcP_message_default(m->msg, key, var);
} else {
if (v->type->label == LABEL_REPEATED || v->type->label == LABEL_PACKED) {
_pbcA_index(v->v.array, index, var);
} else {
var[0] = v->v.var[0];
}
type = v->type->type;
}
if (type == PTYPE_ENUM) {
if (sz) {
*sz = strlen(var->e.name);
}
return var->e.name;
}
if (sz) {
int len = var->s.len;
if (len<0) {
len = - len;
}
*sz = len;
}
return var->s.str;
}
uint32_t
pbc_rmessage_integer(struct pbc_rmessage *m , const char *key , int index, uint32_t *hi) {
struct value * v = (struct value *)_pbcM_sp_query(m->index,key);
pbc_var var;
int type = 0;
if (v == NULL) {
type = _pbcP_message_default(m->msg, key, var);
} else {
if (v->type->label == LABEL_REPEATED || v->type->label == LABEL_PACKED) {
_pbcA_index(v->v.array, index, var);
} else {
var[0] = v->v.var[0];
}
type = v->type->type;
}
if (type == PTYPE_ENUM) {
if (hi) {
*hi = 0;
}
return var->e.id;
}
if (hi) {
*hi = var->integer.hi;
}
return var->integer.low;
}
double
pbc_rmessage_real(struct pbc_rmessage * m, const char *key , int index) {
struct value * v = (struct value *)_pbcM_sp_query(m->index,key);
pbc_var var;
if (v == NULL) {
_pbcP_message_default(m->msg, key, var);
} else {
if (v->type->label == LABEL_REPEATED || v->type->label == LABEL_PACKED) {
_pbcA_index(v->v.array, index, var);
} else {
return v->v.var->real;
}
}
return var->real;
}
struct pbc_rmessage *
pbc_rmessage_message(struct pbc_rmessage * rm, const char *key, int index) {
struct value * v = (struct value *)_pbcM_sp_query(rm->index,key);
if (v == NULL) {
struct _field * f = (struct _field *)_pbcM_sp_query(rm->msg->name, key);
if (f == NULL) {
rm->msg->env->lasterror = "Invalid key for sub-message";
// invalid key
return NULL;
}
struct _message * m = f->type_name.m;
if (m->def == NULL) {
// m->def will be free at the end (pbc_delete).
m->def = (struct pbc_rmessage *)malloc(sizeof(struct pbc_rmessage));
m->def->msg = m;
m->def->index = NULL;
}
return m->def;
} else {
if (v->type->label == LABEL_REPEATED) {
return (struct pbc_rmessage *)_pbcA_index_p(v->v.array,index);
} else {
return &(v->v.message);
}
}
}
int
pbc_rmessage_size(struct pbc_rmessage *m, const char *key) {
struct value * v = (struct value *)_pbcM_sp_query(m->index,key);
if (v == NULL) {
return 0;
}
if (v->type->label == LABEL_REPEATED || v->type->label == LABEL_PACKED) {
return pbc_array_size(v->v.array);
} else {
return 1;
}
}