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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define LUA_REDIS_PARSER_VERSION "0.13"
enum {
BAD_REPLY = 0,
STATUS_REPLY = 1,
ERROR_REPLY = 2,
INTEGER_REPLY = 3,
BULK_REPLY = 4,
MULTI_BULK_REPLY = 5
};
enum {
PARSE_OK = 0,
PARSE_ERROR = 1
};
static const char *redis_typenames[] = {
"bad reply",
"status reply",
"error reply",
"integer reply",
"bulk reply",
"multi-bulk reply"
};
#define UINT64_LEN (sizeof("18446744073709551615") - 1)
static void *redis_null = NULL;
static int parse_reply_helper(lua_State *L, char **src, size_t len);
static int redis_parse_reply(lua_State *L);
static int redis_parse_replies(lua_State *L);
static int redis_build_query(lua_State *L);
static const char * parse_single_line_reply(const char *src, const char *last,
size_t *dst_len);
static const char * parse_bulk_reply(const char *src, const char *last,
size_t *dst_len);
static int parse_multi_bulk_reply(lua_State *L, char **src,
const char *last);
static size_t get_num_size(size_t i);
static char *sprintf_num(char *dst, int64_t ui64);
static int redis_typename(lua_State *L);
#define redis_nelems(arr) \
(sizeof(arr) / sizeof(arr[0]))
static const struct luaL_Reg redis_parser[] = {
{"parse_reply", redis_parse_reply},
{"parse_replies", redis_parse_replies},
{"build_query", redis_build_query},
{"typename", redis_typename},
{NULL, NULL}
};
int
luaopen_redis_parser(lua_State *L)
{
luaL_register(L, "redis.parser", redis_parser);
lua_pushliteral(L, LUA_REDIS_PARSER_VERSION);
lua_setfield(L, -2, "_VERSION");
lua_pushlightuserdata(L, redis_null);
lua_setfield(L, -2, "null");
lua_pushnumber(L, BAD_REPLY);
lua_setfield(L, -2, "BAD_REPLY");
lua_pushnumber(L, STATUS_REPLY);
lua_setfield(L, -2, "STATUS_REPLY");
lua_pushnumber(L, ERROR_REPLY);
lua_setfield(L, -2, "ERROR_REPLY");
lua_pushnumber(L, INTEGER_REPLY);
lua_setfield(L, -2, "INTEGER_REPLY");
lua_pushnumber(L, BULK_REPLY);
lua_setfield(L, -2, "BULK_REPLY");
lua_pushnumber(L, MULTI_BULK_REPLY);
lua_setfield(L, -2, "MULTI_BULK_REPLY");
return 1;
}
static int
redis_parse_reply(lua_State *L)
{
char *p;
size_t len;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expected one argument but got %d",
lua_gettop(L));
}
p = (char *) luaL_checklstring(L, 1, &len);
return parse_reply_helper(L, &p, len);
}
static int
redis_parse_replies(lua_State *L)
{
char *p;
char *q;
int i, n, nret;
size_t len;
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected two arguments but got %d",
lua_gettop(L));
}
p = (char *) luaL_checklstring(L, 1, &len);
n = luaL_checknumber(L, 2);
dd("n = %d", (int) n);
lua_pop(L, 1);
lua_createtable(L, n, 0); /* table */
for (i = 1; i <= n; i++) {
dd("parsing reply %d", i);
lua_createtable(L, n, 2); /* table table */
q = p;
nret = parse_reply_helper(L, &p, len); /* table table res typ */
if (nret != 2) {
return luaL_error(L, "internal error: redis_parse_reply "
"returns %d", nret);
}
dd("p = %p, q = %p, len = %d", p, q, (int) len);
len -= p - q;
dd("len is now %d", (int) len);
lua_rawseti(L, -3, 2); /* table table res */
lua_rawseti(L, -2, 1); /* table table */
lua_rawseti(L, -2, i); /* table */
}
return 1;
}
static int
parse_reply_helper(lua_State *L, char **src, size_t len)
{
char *p;
const char *last;
const char *dst;
size_t dst_len;
lua_Number num;
int rc;
p = *src;
if (len == 0) {
lua_pushliteral(L, "empty reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
last = p + len;
switch (*p) {
case '+':
p++;
dst = parse_single_line_reply(p, last, &dst_len);
if (dst_len == -2) {
lua_pushliteral(L, "bad status reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
*src += dst_len + 1 + sizeof("\r\n") - 1;
lua_pushlstring(L, dst, dst_len);
lua_pushnumber(L, STATUS_REPLY);
break;
case '-':
p++;
dst = parse_single_line_reply(p, last, &dst_len);
if (dst_len == -2) {
lua_pushliteral(L, "bad error reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
*src += dst_len + 1 + sizeof("\r\n") - 1;
lua_pushlstring(L, dst, dst_len);
lua_pushnumber(L, ERROR_REPLY);
break;
case ':':
p++;
dst = parse_single_line_reply(p, last, &dst_len);
if (dst_len == -2) {
lua_pushliteral(L, "bad integer reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
*src += dst_len + 1 + sizeof("\r\n") - 1;
lua_pushlstring(L, dst, dst_len);
num = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushnumber(L, num);
lua_pushnumber(L, INTEGER_REPLY);
break;
case '$':
p++;
dst = parse_bulk_reply(p, last, &dst_len);
if (dst_len == -2) {
lua_pushliteral(L, "bad bulk reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
if (dst_len == -1) {
*src = (char *) dst + sizeof("\r\n") - 1;
/* lua_pushlightuserdata(L, redis_null); */
lua_pushnil(L);
lua_pushnumber(L, BULK_REPLY);
return 2;
}
*src = (char *) dst + dst_len + sizeof("\r\n") - 1;
lua_pushlstring(L, dst, dst_len);
lua_pushnumber(L, BULK_REPLY);
break;
case '*':
p++;
rc = parse_multi_bulk_reply(L, &p, last);
if (rc != PARSE_OK) {
lua_pushliteral(L, "bad multi bulk reply");
lua_pushnumber(L, BAD_REPLY);
return 2;
}
/* rc == PARSE_OK */
*src = (char *) p;
lua_pushnumber(L, MULTI_BULK_REPLY);
break;
default:
lua_pushliteral(L, "invalid reply");
lua_pushnumber(L, BAD_REPLY);
break;
}
return 2;
}
static const char *
parse_single_line_reply(const char *src, const char *last, size_t *dst_len)
{
const char *p = src;
int seen_cr = 0;
while (p != last) {
if (*p == '\r') {
seen_cr = 1;
} else if (seen_cr) {
if (*p == '\n') {
*dst_len = p - src - 1;
return src;
}
seen_cr = 0;
}
p++;
}
/* CRLF not found at all */
*dst_len = -2;
return NULL;
}
#define CHECK_EOF if (p >= last) goto invalid;
static const char *
parse_bulk_reply(const char *src, const char *last, size_t *dst_len)
{
const char *p = src;
ssize_t size = 0;
const char *dst;
CHECK_EOF
/* read the bulk size */
if (*p == '-') {
p++;
CHECK_EOF
while (*p != '\r') {
if (*p < '0' || *p > '9') {
goto invalid;
}
p++;
CHECK_EOF
}
/* *p == '\r' */
if (last - p < size + sizeof("\r\n") - 1) {
goto invalid;
}
p++;
if (*p++ != '\n') {
goto invalid;
}
*dst_len = -1;
return p - (sizeof("\r\n") - 1);
}
while (*p != '\r') {
if (*p < '0' || *p > '9') {
goto invalid;
}
size *= 10;
size += *p - '0';
p++;
CHECK_EOF
}
/* *p == '\r' */
p++;
CHECK_EOF
if (*p++ != '\n') {
goto invalid;
}
/* read the bulk data */
if (last - p < size + sizeof("\r\n") - 1) {
goto invalid;
}
dst = p;
p += size;
if (*p++ != '\r') {
goto invalid;
}
if (*p++ != '\n') {
goto invalid;
}
*dst_len = size;
return dst;
invalid:
*dst_len = -2;
return NULL;
}
static int
parse_multi_bulk_reply(lua_State *L, char **src, const char *last)
{
const char *p = *src;
int count = 0;
int i;
size_t dst_len;
const char *dst;
dd("enter multi bulk parser");
CHECK_EOF
while (*p != '\r') {
if (*p == '-') {
p++;
CHECK_EOF
if (*p < '0' || *p > '9') {
goto invalid;
}
while (*p != '\r') {
p++;
CHECK_EOF
}
count = -1;
break;
}
if (*p < '0' || *p > '9') {
dd("expecting digit, but found %c", *p);
goto invalid;
}
count *= 10;
count += *p - '0';
p++;
CHECK_EOF
}
dd("count = %d", count);
/* *p == '\r' */
p++;
CHECK_EOF
if (*p++ != '\n') {
goto invalid;
}
dd("reading the individual bulks");
if (count == -1) {
/* lua_pushlightuserdata(L, redis_null); */
lua_pushnil(L);
*src = (char *) p;
return PARSE_OK;
}
lua_createtable(L, count, 0);
for (i = 1; i <= count; i++) {
CHECK_EOF
switch (*p) {
case '+':
case '-':
case ':':
p++;
dst = parse_single_line_reply(p, last, &dst_len);
break;
case '$':
p++;
dst = parse_bulk_reply(p, last, &dst_len);
break;
default:
goto invalid;
}
if (dst_len == -2) {
dd("bulk %d reply parse fail for multi bulks", i);
return PARSE_ERROR;
}
if (dst_len == -1) {
lua_pushnil(L);
p = dst + sizeof("\r\n") - 1;
} else {
lua_pushlstring(L, dst, dst_len);
p = dst + dst_len + sizeof("\r\n") - 1;
}
lua_rawseti(L, -2, i);
}
*src = (char *) p;
return PARSE_OK;
invalid:
return PARSE_ERROR;
}
static int
redis_build_query(lua_State *L)
{
int i, n;
size_t len, total;
const char *p;
char *last;
char *buf;
int flag;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expected one argument but got %d",
lua_gettop(L));
}
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_objlen(L, 1);
if (n == 0) {
return luaL_error(L, "empty input param table");
}
total = sizeof("*") - 1
+ get_num_size(n)
+ sizeof("\r\n") - 1
;
for (i = 1; i <= n; i++) {
lua_rawgeti(L, 1, i);
dd("param type: %d (%d)", lua_type(L, -1), LUA_TUSERDATA);
switch (lua_type(L, -1)) {
case LUA_TSTRING:
case LUA_TNUMBER:
lua_tolstring(L, -1, &len);
total += sizeof("$") - 1
+ get_num_size(len)
+ sizeof("\r\n") - 1
+ len
+ sizeof("\r\n") - 1
;
break;
case LUA_TBOOLEAN:
total += sizeof("$1\r\n1\r\n") - 1;
break;
case LUA_TLIGHTUSERDATA:
p = lua_touserdata(L, -1);
dd("user data: %p", p);
if (p == redis_null) {
total += sizeof("$-1\r\n") - 1;
break;
}
default:
return luaL_error(L, "parameter %d is not a string, number, "
"redis.parser.null, or boolean value", i);
}
lua_pop(L, 1);
}
buf = lua_newuserdata(L, total); /* lua_newuserdata never returns NULL */
last = buf;
*last++ = '*';
last = sprintf_num(last, n);
*last++ = '\r'; *last++ = '\n';
for (i = 1; i <= n; i++) {
lua_rawgeti(L, 1, i);
switch (lua_type(L, -1)) {
case LUA_TSTRING:
case LUA_TNUMBER:
p = luaL_checklstring(L, -1, &len);
*last++ = '$';
last = sprintf_num(last, len);
*last++ = '\r'; *last++ = '\n';
memcpy(last, p, len);
last += len;
*last++ = '\r'; *last++ = '\n';
break;
case LUA_TBOOLEAN:
memcpy(last, "$1\r\n", sizeof("$1\r\n") - 1);
last += sizeof("$1\r\n") - 1;
flag = lua_toboolean(L, -1);
*last++ = flag ? '1' : '0';
*last++ = '\r'; *last++ = '\n';
break;
case LUA_TLIGHTUSERDATA:
/* must be null */
memcpy(last, "$-1\r\n", sizeof("$-1\r\n") - 1);
last += sizeof("$-1\r\n") - 1;
break;
default:
/* cannot reach here */
break;
}
lua_pop(L, 1);
}
if (last - buf != (ssize_t) total) {
return luaL_error(L, "buffer error");
}
lua_pushlstring(L, buf, total);
return 1;
}
static size_t
get_num_size(size_t i)
{
size_t n = 0;
do {
i = i / 10;
n++;
} while (i > 0);
return n;
}
static char *
sprintf_num(char *dst, int64_t ui64)
{
char *p;
char temp[UINT64_LEN + 1];
size_t len;
p = temp + UINT64_LEN;
do {
*--p = (char) (ui64 % 10 + '0');
} while (ui64 /= 10);
len = (temp + UINT64_LEN) - p;
memcpy(dst, p, len);
return dst + len;
}
static int
redis_typename(lua_State *L)
{
int typ;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument but got %d",
lua_gettop(L));
}
typ = (int) luaL_checkinteger(L, 1);
if (typ < 0 || typ >= redis_nelems(redis_typenames)) {
lua_pushnil(L);
return 1;
}
lua_pushstring(L, redis_typenames[typ]);
return 1;
}
|