-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsproto.c
413 lines (375 loc) · 8.6 KB
/
lsproto.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
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "msvcint.h"
#include "cJSON.h"
#include "lsproto.h"
#define MAX_GLOBALSPROTO 16
#define ENCODE_BUFFERSIZE 2050
#define ENCODE_MAXSIZE 0x1000000
#define ENCODE_DEEPLEVEL 64
struct sproto * lnewproto(const char * buffer, size_t sz) {
struct sproto * sp;
sp = sproto_create(buffer, sz);
return sp;
}
int ldeleteproto(struct sproto * sp) {
sproto_release(sp);
return 0;
}
struct sproto_type * lquerytype(struct sproto *sp, const char * type_name) {
if (sp == NULL) {
return NULL;
}
return sproto_type(sp, type_name);
}
int encode(const struct sproto_arg *args)
{
struct encode_ud *self = NULL;
cJSON* pItem = NULL;
self = (struct encode_ud *)(args->ud);
if (self->deep >= ENCODE_DEEPLEVEL)
return -1;
if (self->json_item == NULL)
return -1;
pItem = cJSON_GetObjectItem(self->json_item, args->tagname);
if (pItem == NULL) {
return SPROTO_CB_NIL;
}
if (args->index > 0) {
pItem = cJSON_GetArrayItem(pItem, args->index - 1);
if (pItem == NULL) { //Êý×é½áÊø
return SPROTO_CB_NIL;
}
}
switch (args->type) {
case SPROTO_TINTEGER: {
int64_t v;
int64_t vh;
if (pItem->type != cJSON_Number)
{
return -1;
}
v = (int64_t)(pItem->valueint);
vh = v >> 31;
if (vh == 0 || vh == -1) {
*(uint32_t *)args->value = (uint32_t)v;
return 4;
}
else {
*(uint64_t *)args->value = (uint64_t)v;
return 8;
}
}
case SPROTO_TBOOLEAN: {
int v = 0;
if (pItem->type != cJSON_False && pItem->type != cJSON_True){
return -1;
}
v = 0;
if (pItem->type != cJSON_True){
v = 1;
}
*(int *)args->value = v;
return 4;
}
case SPROTO_TSTRING: {
size_t sz = 0;
if (pItem->type != cJSON_String){
return -1;
}
sz = strlen(pItem->valuestring);
if (sz > args->length)
return -1;
memcpy(args->value, pItem->valuestring, sz);
return sz + 1;
}
case SPROTO_TSTRUCT: {
struct encode_ud sub;
int r = 0;
if (pItem->type != cJSON_Array && pItem->type != cJSON_Object){
return -1;
}
sub.st = args->subtype;
sub.json_item = pItem;
r = sproto_encode(args->subtype, args->value, args->length, encode, &sub);
if (r < 0)
return r;
return r;
}
default:
return -1;
}
}
void * expand_buffer(void* p, int osz, int nsz) {
void *output;
do {
osz *= 2;
} while (osz < nsz);
if (osz > ENCODE_MAXSIZE) {
return NULL;
}
output = realloc(p, osz);
return output;
}
/*
lightuserdata sproto_type
table source
return string
*/
int lencode(struct sproto * sp,struct sproto_type * st, char* jsonStr, char** ppBuffer, size_t sz) {
struct encode_ud self;
cJSON *json = NULL;
self.st = st;
self.tbl_index = 0;
json = cJSON_Parse(jsonStr);
if (json == NULL)
return 0;
for (;;) {
int r = 0;
self.array_tag = NULL;
self.array_index = 0;
self.deep = 0;
self.iter_index = self.tbl_index+1;
self.json_item = json;
r = sproto_encode(st, *ppBuffer, sz, encode, &self);
if (r<0) {
*ppBuffer = (char*)expand_buffer(*ppBuffer, sz, sz*2);
sz *= 2;
} else {
return 1;
}
}
return 0;
}
int
decode(const struct sproto_arg *args) {
struct decode_ud * self = (decode_ud*)args->ud;
if (self->deep >= ENCODE_DEEPLEVEL)
return -1;
if (args->length == 0) {
return 0;
}
cJSON* arr_js = NULL;
cJSON* item = NULL;
if (args->index > 0) {
// It's array
if (args->tagname != self->array_tag) {
self->array_tag = args->tagname;
arr_js = cJSON_CreateArray();
cJSON_AddItemToObject(self->json_item, args->tagname, arr_js);
}
else {
arr_js = cJSON_GetObjectItem(self->json_item, args->tagname);
}
}
switch (args->type) {
case SPROTO_TINTEGER: {
// notice: in lua 5.2, 52bit integer support (not 64)
int val = *(uint64_t*)args->value;
item = cJSON_CreateNumber(val);
break;
}
case SPROTO_TBOOLEAN: {
int v = *(uint64_t*)args->value;
break;
}
case SPROTO_TSTRING: {
char* val = (char*)args->value;
char* str = new char[args->length + 1];
strncpy(str, val, args->length);
str[args->length] = 0;
item = cJSON_CreateString(str);
break;
}
case SPROTO_TSTRUCT: {
struct decode_ud sub;
int r;
sub.deep = self->deep + 1;
sub.array_index = 0;
sub.array_tag = NULL;
item = cJSON_CreateObject();
sub.json_item = item;
r = sproto_decode(args->subtype, args->value, args->length, decode, &sub);
if (r < 0 || r != args->length)
return r;
break;
}
default:;
//luaL_error(L, "Invalid type");
}
if (args->index > 0) {
if (arr_js && item)
cJSON_AddItemToArray(arr_js, item);
}
else {
if (item)
cJSON_AddItemToObject(self->json_item, args->tagname, item);
}
return 0;
}
void ldumpproto(struct sproto * sp) {
sproto_dump(sp);
}
/*
string source / (lightuserdata , integer)
return string
*/
char* lpack(const void * buffer, size_t sz, size_t* pOutSize) {
// the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB).
int bytes;
size_t maxsz = (sz + 2047) / 2048 * 2 + sz + 2;
void * output = malloc(10*1024);
int osz = 10*1024;
if (osz < maxsz) {
output = expand_buffer(output, osz, maxsz);
}
bytes = sproto_pack(buffer, sz, output, maxsz);
if (bytes > maxsz) {
return NULL;
}
return (char*)output;
}
char* lunpack(const void * buffer, size_t sz, size_t* pOutSize) {
char * output = (char*)malloc(10*1024);
int osz =10*1024;
int r = sproto_unpack(buffer, sz, output, osz);
if (r < 0)
return NULL;
if (r > osz) {
output = (char*)expand_buffer(output, osz, r);
r = sproto_unpack(buffer, sz, output, r);
if (r < 0)
return NULL;
}
*pOutSize = osz;
return output;
}
int lprotocol(struct sproto * sp, const char* name, struct protocol_ret * pRet) {
struct protocol_ret;
int tag;
tag = sproto_prototag(sp, name);
if (tag < 0)
return 0;
pRet->tag = tag;
pRet->request = sproto_protoquery(sp, tag, SPROTO_REQUEST);
pRet->response = sproto_protoquery(sp, tag, SPROTO_RESPONSE);
return 3;
}
/* global sproto pointer for multi states
NOTICE : It is not thread safe
*/
struct sproto * G_sproto[MAX_GLOBALSPROTO];
int lsaveproto(struct sproto * sp, int index) {
if (index < 0 || index >= MAX_GLOBALSPROTO) {
return 0;
}
G_sproto[index] = sp;
return 1;
}
struct sproto * lloadproto(int index) {
struct sproto * sp;
if (index < 0 || index >= MAX_GLOBALSPROTO) {
return NULL;
}
sp = G_sproto[index];
if (sp == NULL) {
return NULL;
}
return sp;
}
#if 0
void
push_default(const struct sproto_arg *args, int array) {
lua_State *L = args->ud;
switch(args->type) {
case SPROTO_TINTEGER:
if (args->extra)
lua_pushnumber(L, 0.0);
else
lua_pushinteger(L, 0);
break;
case SPROTO_TBOOLEAN:
lua_pushboolean(L, 0);
break;
case SPROTO_TSTRING:
lua_pushliteral(L, "");
break;
case SPROTO_TSTRUCT:
if (array) {
lua_pushstring(L, sproto_name(args->subtype));
} else {
lua_createtable(L, 0, 1);
lua_pushstring(L, sproto_name(args->subtype));
lua_setfield(L, -2, "__type");
}
break;
default:
luaL_error(L, "Invalid type %d", args->type);
break;
}
}
int
encode_default(const struct sproto_arg *args) {
lua_State *L = args->ud;
lua_pushstring(L, args->tagname);
if (args->index > 0) {
lua_newtable(L);
push_default(args, 1);
lua_setfield(L, -2, "__array");
lua_rawset(L, -3);
return SPROTO_CB_NOARRAY;
} else {
push_default(args, 0);
lua_rawset(L, -3);
return SPROTO_CB_NIL;
}
}
/*
lightuserdata sproto_type
return default table
*/
int
ldefault(lua_State *L) {
int ret;
// 64 is always enough for dummy buffer, except the type has many fields ( > 27).
char dummy[64];
struct sproto_type * st = lua_touserdata(L, 1);
if (st == NULL) {
return luaL_argerror(L, 1, "Need a sproto_type object");
}
lua_newtable(L);
ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L);
if (ret<0) {
// try again
int sz = sizeof(dummy) * 2;
void * tmp = lua_newuserdata(L, sz);
lua_insert(L, -2);
for (;;) {
ret = sproto_encode(st, tmp, sz, encode_default, L);
if (ret >= 0)
break;
sz *= 2;
tmp = lua_newuserdata(L, sz);
lua_replace(L, -3);
}
}
return 1;
}
luaL_Reg l[] = {
{ "newproto", lnewproto },
{ "deleteproto", ldeleteproto },
{ "dumpproto", ldumpproto },
{ "querytype", lquerytype },
{ "decode", ldecode },
{ "protocol", lprotocol },
{ "loadproto", lloadproto },
{ "saveproto", lsaveproto },
{ "default", ldefault },
{ NULL, NULL },
};
luaL_newlib(L,l);
pushfunction_withbuffer(L, "encode", lencode);
pushfunction_withbuffer(L, "pack", lpack);
pushfunction_withbuffer(L, "unpack", lunpack);
#endif