-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb.c
executable file
·474 lines (426 loc) · 11.5 KB
/
pb.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* =====================================================================================
*
* Filename: pb.c
*
* Description: protoc-gen-lua
* Google's Protocol Buffers project, ported to lua.
* https://code.google.com/p/protoc-gen-lua/
*
* Copyright (c) 2010 , 林卓毅 (Zhuoyi Lin) netsnail@gmail.com
* All rights reserved.
*
* Use, modification and distribution are subject to the "New BSD License"
* as listed at <url: http://www.opensource.org/licenses/bsd-license.php >.
*
* Created: 2010年08月02日 18时04分21秒
*
* Company: NetEase
*
* =====================================================================================
*/
#include <stdint.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifdef _ALLBSD_SOURCE
#include <machine/endian.h>
#else
#include <machine/endian.h>
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define IS_LITTLE_ENDIAN
#endif
#define IOSTRING_META "protobuf.IOString"
#define checkiostring(L) \
(IOString*) luaL_checkudata(L, 1, IOSTRING_META)
#define IOSTRING_BUF_LEN 65535
typedef struct{
size_t size;
char buf[IOSTRING_BUF_LEN];
} IOString;
static void pack_varint(luaL_Buffer *b, uint64_t value)
{
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
if (value >= 0x80)
{
luaL_addchar(b, value | 0x80);
value >>= 7;
}
}
}
}
}
}
}
}
}
luaL_addchar(b, value);
}
static int varint_encoder(lua_State *L)
{
lua_Number l_value = luaL_checknumber(L, 2);
uint64_t value = (uint64_t)l_value;
luaL_Buffer b;
luaL_buffinit(L, &b);
pack_varint(&b, value);
lua_settop(L, 1);
luaL_pushresult(&b);
lua_call(L, 1, 0);
return 0;
}
static int signed_varint_encoder(lua_State *L)
{
lua_Number l_value = luaL_checknumber(L, 2);
int64_t value = (int64_t)l_value;
luaL_Buffer b;
luaL_buffinit(L, &b);
if (value < 0)
{
pack_varint(&b, *(uint64_t*)&value);
}else{
pack_varint(&b, value);
}
lua_settop(L, 1);
luaL_pushresult(&b);
lua_call(L, 1, 0);
return 0;
}
static int pack_fixed32(lua_State *L, uint8_t* value){
#ifdef IS_LITTLE_ENDIAN
lua_pushlstring(L, (char*)value, 4);
#else
uint32_t v = htole32(*(uint32_t*)value);
lua_pushlstring(L, (char*)&v, 4);
#endif
return 0;
}
static int pack_fixed64(lua_State *L, uint8_t* value){
#ifdef IS_LITTLE_ENDIAN
lua_pushlstring(L, (char*)value, 8);
#else
uint64_t v = htole64(*(uint64_t*)value);
lua_pushlstring(L, (char*)&v, 8);
#endif
return 0;
}
static int struct_pack(lua_State *L)
{
uint8_t format = luaL_checkinteger(L, 2);
lua_Number value = luaL_checknumber(L, 3);
lua_settop(L, 1);
switch(format){
case 'i':
{
int32_t v = (int32_t)value;
pack_fixed32(L, (uint8_t*)&v);
break;
}
case 'q':
{
int64_t v = (int64_t)value;
pack_fixed64(L, (uint8_t*)&v);
break;
}
case 'f':
{
float v = (float)value;
pack_fixed32(L, (uint8_t*)&v);
break;
}
case 'd':
{
double v = (double)value;
pack_fixed64(L, (uint8_t*)&v);
break;
}
case 'I':
{
uint32_t v = (uint32_t)value;
pack_fixed32(L, (uint8_t*)&v);
break;
}
case 'Q':
{
uint64_t v = (uint64_t) value;
pack_fixed64(L, (uint8_t*)&v);
break;
}
default:
luaL_error(L, "Unknown, format");
}
lua_call(L, 1, 0);
return 0;
}
static size_t size_varint(const char* buffer, size_t len)
{
size_t pos = 0;
while(buffer[pos] & 0x80){
++pos;
if(pos > len){
return -1;
}
}
return pos+1;
}
static uint64_t unpack_varint(const char* buffer, size_t len)
{
uint64_t value = buffer[0] & 0x7f;
size_t shift = 7;
size_t pos=0;
for(pos = 1; pos < len; ++pos)
{
value |= ((uint64_t)(buffer[pos] & 0x7f)) << shift;
shift += 7;
}
return value;
}
static int varint_decoder(lua_State *L)
{
size_t len;
const char* buffer = luaL_checklstring(L, 1, &len);
size_t pos = luaL_checkinteger(L, 2);
buffer += pos;
len = size_varint(buffer, len);
if(len == -1){
luaL_error(L, "error data %s, len:%d", buffer, len);
}else{
lua_pushnumber(L, (lua_Number)unpack_varint(buffer, len));
lua_pushinteger(L, len + pos);
}
return 2;
}
static int signed_varint_decoder(lua_State *L)
{
size_t len;
const char* buffer = luaL_checklstring(L, 1, &len);
size_t pos = luaL_checkinteger(L, 2);
buffer += pos;
len = size_varint(buffer, len);
if(len == -1){
luaL_error(L, "error data %s, len:%d", buffer, len);
}else{
lua_pushnumber(L, (lua_Number)(int64_t)unpack_varint(buffer, len));
lua_pushinteger(L, len + pos);
}
return 2;
}
static int zig_zag_encode32(lua_State *L)
{
int32_t n = luaL_checkinteger(L, 1);
uint32_t value = (n << 1) ^ (n >> 31);
lua_pushinteger(L, value);
return 1;
}
static int zig_zag_decode32(lua_State *L)
{
uint32_t n = (uint32_t)luaL_checkinteger(L, 1);
int32_t value = (n >> 1) ^ - (int32_t)(n & 1);
lua_pushinteger(L, value);
return 1;
}
static int zig_zag_encode64(lua_State *L)
{
int64_t n = (int64_t)luaL_checknumber(L, 1);
uint64_t value = (n << 1) ^ (n >> 63);
lua_pushinteger(L, value);
return 1;
}
static int zig_zag_decode64(lua_State *L)
{
uint64_t n = (uint64_t)luaL_checknumber(L, 1);
int64_t value = (n >> 1) ^ - (int64_t)(n & 1);
lua_pushinteger(L, value);
return 1;
}
static int read_tag(lua_State *L)
{
size_t len;
const char* buffer = luaL_checklstring(L, 1, &len);
size_t pos = luaL_checkinteger(L, 2);
buffer += pos;
len = size_varint(buffer, len);
if(len == -1){
luaL_error(L, "error data %s, len:%d", buffer, len);
}else{
lua_pushlstring(L, buffer, len);
lua_pushinteger(L, len + pos);
}
return 2;
}
static const uint8_t* unpack_fixed32(const uint8_t* buffer, uint8_t* cache)
{
#ifdef IS_LITTLE_ENDIAN
return buffer;
#else
*(uint32_t*)cache = le32toh(*(uint32_t*)buffer);
return cache;
#endif
}
static const uint8_t* unpack_fixed64(const uint8_t* buffer, uint8_t* cache)
{
#ifdef IS_LITTLE_ENDIAN
return buffer;
#else
*(uint64_t*)cache = le64toh(*(uint64_t*)buffer);
return cache;
#endif
}
static int struct_unpack(lua_State *L)
{
uint8_t format = luaL_checkinteger(L, 1);
size_t len;
const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len);
size_t pos = luaL_checkinteger(L, 3);
buffer += pos;
uint8_t out[8];
switch(format){
case 'i':
{
lua_pushinteger(L, *(int32_t*)unpack_fixed32(buffer, out));
break;
}
case 'q':
{
lua_pushnumber(L, (lua_Number)*(int64_t*)unpack_fixed64(buffer, out));
break;
}
case 'f':
{
lua_pushnumber(L, (lua_Number)*(float*)unpack_fixed32(buffer, out));
break;
}
case 'd':
{
lua_pushnumber(L, (lua_Number)*(double*)unpack_fixed64(buffer, out));
break;
}
case 'I':
{
lua_pushnumber(L, *(uint32_t*)unpack_fixed32(buffer, out));
break;
}
case 'Q':
{
lua_pushnumber(L, (lua_Number)*(uint64_t*)unpack_fixed64(buffer, out));
break;
}
default:
luaL_error(L, "Unknown, format");
}
return 1;
}
static int iostring_new(lua_State* L)
{
IOString* io = (IOString*)lua_newuserdata(L, sizeof(IOString));
io->size = 0;
luaL_getmetatable(L, IOSTRING_META);
lua_setmetatable(L, -2);
return 1;
}
static int iostring_str(lua_State* L)
{
IOString *io = checkiostring(L);
lua_pushlstring(L, io->buf, io->size);
return 1;
}
static int iostring_len(lua_State* L)
{
IOString *io = checkiostring(L);
lua_pushinteger(L, io->size);
return 1;
}
static int iostring_write(lua_State* L)
{
IOString *io = checkiostring(L);
size_t size;
const char* str = luaL_checklstring(L, 2, &size);
if(io->size + size > IOSTRING_BUF_LEN){
luaL_error(L, "Out of range");
}
memcpy(io->buf + io->size, str, size);
io->size += size;
return 0;
}
static int iostring_sub(lua_State* L)
{
IOString *io = checkiostring(L);
size_t begin = luaL_checkinteger(L, 2);
size_t end = luaL_checkinteger(L, 3);
if(begin > end || end > io->size)
{
luaL_error(L, "Out of range");
}
lua_pushlstring(L, io->buf + begin - 1, end - begin + 1);
return 1;
}
static int iostring_clear(lua_State* L)
{
IOString *io = checkiostring(L);
io->size = 0;
return 0;
}
static const struct luaL_reg _pb [] = {
{"varint_encoder", varint_encoder},
{"signed_varint_encoder", signed_varint_encoder},
{"read_tag", read_tag},
{"struct_pack", struct_pack},
{"struct_unpack", struct_unpack},
{"varint_decoder", varint_decoder},
{"signed_varint_decoder", signed_varint_decoder},
{"zig_zag_decode32", zig_zag_decode32},
{"zig_zag_encode32", zig_zag_encode32},
{"zig_zag_decode64", zig_zag_decode64},
{"zig_zag_encode64", zig_zag_encode64},
{"new_iostring", iostring_new},
{NULL, NULL}
};
static const struct luaL_reg _c_iostring_m [] = {
{"__tostring", iostring_str},
{"__len", iostring_len},
{"write", iostring_write},
{"sub", iostring_sub},
{"clear", iostring_clear},
{NULL, NULL}
};
int luaopen_pb (lua_State *L)
{
luaL_newmetatable(L, IOSTRING_META);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, _c_iostring_m);
luaL_register(L, "pb", _pb);
return 1;
}