/
luatexts.c
905 lines (754 loc) · 19.6 KB
/
luatexts.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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
* luatexts.c: Trivial Lua human-readable binary-safe serialization library
* See copyright information in file COPYRIGHT.
*/
#if defined (__cplusplus)
extern "C" {
#endif
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include "luainternals.h"
#if defined (__cplusplus)
}
#endif
/* Really spammy SPAM */
#if 0
#define XSPAM(a) printf a
#else
#define XSPAM(a) (void)0
#endif
/* Regular SPAM */
#if 1
#define SPAM(a) printf a
#else
#define SPAM(a) (void)0
#endif
/* Error-message SPAM */
#if 0
#define ESPAM(a) printf a
#else
#define ESPAM(a) (void)0
#endif
#define LUATEXTS_VERSION "luatexts 0.1"
#define LUATEXTS_COPYRIGHT "Copyright (C) 2011, luatexts authors"
#define LUATEXTS_DESCRIPTION \
"Trivial Lua human-readable binary-safe serialization library"
#define LUATEXTS_ESUCCESS (0)
#define LUATEXTS_EFAILURE (1)
#define LUATEXTS_EBADSIZE (2)
#define LUATEXTS_EBADDATA (3)
#define LUATEXTS_EBADTYPE (4)
#define LUATEXTS_EGARBAGE (5)
#define LUATEXTS_ETOOHUGE (6)
#define LUATEXTS_EBADUTF8 (7)
#define LUATEXTS_ECLIPPED (8)
#define LUATEXTS_CNIL '-'
#define LUATEXTS_CFALSE '0'
#define LUATEXTS_CTRUE '1'
#define LUATEXTS_CNUMBER 'N'
#define LUATEXTS_CUINT 'U'
#define LUATEXTS_CSTRING 'S'
#define LUATEXTS_CTABLE 'T'
#define LUATEXTS_CSTRINGUTF8 '8'
/* WARNING: Make sure these match your luaconf.h */
typedef lua_Number LUATEXTS_NUMBER;
typedef unsigned long LUATEXTS_UINT;
#define luatexts_tonumber strtod
#define luatexts_touint strtoul
typedef struct lts_LoadState
{
const unsigned char * pos;
size_t unread;
} lts_LoadState;
static void ltsLS_init(
lts_LoadState * ls,
const unsigned char * data,
size_t len
)
{
ls->pos = (len > 0) ? data : NULL;
ls->unread = len;
}
#define ltsLS_good(ls) \
((ls)->pos != NULL)
#define ltsLS_unread(ls) \
((ls)->unread)
static const unsigned char * ltsLS_eat(lts_LoadState * ls, size_t len)
{
const unsigned char * result = NULL;
if (ltsLS_good(ls))
{
if (ltsLS_unread(ls) >= len)
{
result = ls->pos;
ls->pos += len;
ls->unread -= len;
XSPAM(("XXX %lu '%s'\n", (unsigned long) len, result));
}
else
{
ls->unread = 0;
ls->pos = NULL;
}
}
return result;
}
/*
* UTF-8 handling implemented based on information here:
* http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
*/
static const signed char utf8_char_len[256] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0
};
/*
* *Increments* len_bytes by the number of bytes read.
* Fails on invalid UTF-8 characters.
*/
static int ltsLS_eatutf8char(lts_LoadState * ls, size_t * len_bytes)
{
unsigned char b = 0;
signed char expected_length = 0;
int i = 0;
const unsigned char * origin = ls->pos;
/* Check if we have any data in the buffer */
if (!ltsLS_good(ls) || ltsLS_unread(ls) < 1)
{
ESPAM(("eatutf8char: no buffer to start with\n"));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_ECLIPPED;
}
/* We have at least one byte in the buffer, let's check it out. */
b = *ls->pos;
/* We did just eat a byte, no matter what happens next. */
++ls->pos;
--ls->unread;
/* Get an expected length of a character. */
expected_length = utf8_char_len[b];
XSPAM((
"eatutf8char: first byte 0x%X (%d) expected length: %d\n",
b, b, expected_length
));
/* Check if it was a valid first byte. */
if (expected_length < 1)
{
ESPAM(("eatutf8char: invalid start byte 0x%X (%d)\n", b, b));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
/* If it was a single-byte ASCII character, return right away. */
if (expected_length == 1)
{
XSPAM(("eatutf8char: ascii 0x%X (%d)\n", b, b));
*len_bytes += expected_length;
return LUATEXTS_ESUCCESS;
}
/*
* It was a multi-byte character. Check if we have enough bytes unread.
* Note that we've eaten one byte already.
*/
if (ltsLS_unread(ls) + 1 < (unsigned char)expected_length)
{
ESPAM(("eatutf8char: multibyte character clipped\n"));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_ECLIPPED; /* Assuming it is buffer's fault. */
}
/* Let's eat the rest of characters */
for (i = 1; i < expected_length; ++i)
{
b = *ls->pos;
/* We did just eat a byte, no matter what happens next. */
++ls->pos;
--ls->unread;
XSPAM(("eatutf8char: cont 0x%X (%d)\n", b, b));
/* Check if it is a continuation byte */
if (utf8_char_len[b] != -1)
{
ESPAM(("eatutf8char: invalid continuation byte 0x%X (%d)\n", b, b));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
}
/* All bytes are correct, let's check out for overlong forms */
if (
expected_length == 2 && (
(origin[0] & 0xFE) == 0xC0
)
)
{
ESPAM((
"eatutf8char: overlong form 2 0x%X 0x%X (%d %d)\n",
origin[0], origin[0],
origin[1], origin[1]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
else if (
expected_length == 3 && (
origin[0] == 0xE0
&& (origin[1] & 0xE0) == 0x80
)
)
{
ESPAM((
"eatutf8char: overlong form 3 0x%X 0x%X 0x%X (%d %d %d)\n",
origin[0], origin[0],
origin[1], origin[1],
origin[2], origin[2]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
else if (
expected_length == 4 && (
origin[0] == 0xF0
&& (origin[1] & 0xF0) == 0x80
)
)
{
ESPAM((
"eatutf8char: overlong form 4 0x%X 0x%X 0x%X 0x%X (%d %d %d %d)\n",
origin[0], origin[0],
origin[1], origin[1],
origin[2], origin[2],
origin[3], origin[3]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
else if (
expected_length == 5 && (
origin[0] == 0xF8
&& (origin[1] & 0xF8) == 0x80
)
)
{
ESPAM((
"eatutf8char: overlong form 5"
" 0x%X 0x%X 0x%X 0x%X 0x%X (%d %d %d %d %d)\n",
origin[0], origin[0],
origin[1], origin[1],
origin[2], origin[2],
origin[3], origin[3],
origin[4], origin[4]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
else if (
expected_length == 6 && (
origin[0] == 0xFC
&& (origin[1] & 0xFC) == 0x80
)
)
{
ESPAM((
"eatutf8char: overlong form 6"
" 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X (%d %d %d %d %d %d)\n",
origin[0], origin[0],
origin[1], origin[1],
origin[2], origin[2],
origin[3], origin[3],
origin[4], origin[4],
origin[5], origin[5]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
/* No overlongs, check for surrogates. */
if (
expected_length == 3 && (
origin[0] == 0xED
&& (origin[1] & 0xE0) == 0xA0
)
)
{
ESPAM((
"eatutf8char: surrogate form 3 0x%X 0x%X 0x%X (%d %d %d)\n",
origin[0], origin[0],
origin[1], origin[1],
origin[2], origin[2]
));
ls->unread = 0;
ls->pos = NULL;
return LUATEXTS_EBADUTF8;
}
/*
* Note: Not checking for U+FFFE or U+FFFF.
*
* Chapter 3 of version 3.2 of the Unicode standard, Paragraph C5 says
* "A process shall not interpret either U+FFFE or U+FFFF as an abstract
* character", but table 3.1B includes them among
* the "Legal UTF-8 Byte Sequences".
*
* We opt to pass them through.
*/
/* Phew. All done, the UTF-8 character is valid. */
XSPAM(("eatutf8char: read one char successfully\n"));
*len_bytes += expected_length;
return LUATEXTS_ESUCCESS;
}
/*
* Eats specified number of UTF-8 characters. Returns NULL if failed.
* Fails on invalid UTF-8 characters.
*/
static int ltsLS_eatutf8(
lts_LoadState * ls,
size_t num_chars,
const unsigned char ** dest,
size_t * len_bytes
)
{
const unsigned char * origin = ls->pos;
size_t num_bytes = 0;
size_t i = 0;
int result = LUATEXTS_ESUCCESS;
for (i = 0; i < num_chars; ++i)
{
result = ltsLS_eatutf8char(ls, &num_bytes);
if (result != LUATEXTS_ESUCCESS)
{
return result;
}
}
*dest = origin;
*len_bytes = num_bytes;
return result;
}
/*
* Returned length does not include trailing '\r\n' (or '\n' if '\r' is missing).
* It is guaranteed that on success it is safe to read byte at (*dest + len + 1)
* and that byte is either '\r' or '\n'.
*/
static int ltsLS_readline(
lts_LoadState * ls,
const unsigned char ** dest,
size_t * len
)
{
const unsigned char * origin = ls->pos;
unsigned char last = 0;
size_t read = 0;
while (ltsLS_good(ls))
{
if (ltsLS_unread(ls) > 0)
{
unsigned char b = *ls->pos;
++ls->pos;
--ls->unread;
XSPAM(("LINE CHAR 0x%X\n", b));
if (b == '\n')
{
*dest = origin;
*len = (last == '\r') ? read - 1 : read;
XSPAM((
"LINE END %s read %lu\n",
(last == '\r') ? "CRLF" : "LF",
(long unsigned int)read
));
return LUATEXTS_ESUCCESS;
}
last = b;
++read;
}
else
{
ls->unread = 0;
ls->pos = NULL;
}
}
return LUATEXTS_ECLIPPED;
}
static int ltsLS_readuint(lts_LoadState * ls, LUATEXTS_UINT * dest)
{
size_t len = 0;
const unsigned char * data = NULL;
int result = ltsLS_readline(ls, &data, &len);
XSPAM(("UINT readline %d\n", result));
if (result == LUATEXTS_ESUCCESS && len == 0)
{
result = LUATEXTS_EBADDATA;
}
if (result == LUATEXTS_ESUCCESS)
{
/*
* This is safe, since we're guaranteed to have
* at least one non-numeric trailing byte.
*/
char * endptr = NULL;
LUATEXTS_UINT value = luatexts_touint((const char *)data, &endptr, 10);
if ((const unsigned char *)endptr != data + len)
{
XSPAM((
"UINT GARBAGE end %p start %p len %lu\n",
endptr, data, (long unsigned int)len
));
result = LUATEXTS_EGARBAGE;
}
else
{
if ((lua_Integer)value < 0) /* Implementation detail */
{
result = LUATEXTS_ETOOHUGE;
}
else
{
*dest = value;
}
}
}
return result;
}
static int ltsLS_readnumber(lts_LoadState * ls, LUATEXTS_NUMBER * dest)
{
size_t len = 0;
const unsigned char * data = NULL;
int result = ltsLS_readline(ls, &data, &len);
if (result == LUATEXTS_ESUCCESS && len == 0)
{
result = LUATEXTS_EBADDATA;
}
if (result == LUATEXTS_ESUCCESS)
{
/*
* This is safe, since we're guaranteed to have
* at least one non-numeric trailing byte.
*/
char * endptr = NULL;
LUATEXTS_NUMBER value = strtod((const char *)data, &endptr);
XSPAM(("XXX NUM %.54g\n", value));
if ((const unsigned char *)endptr != data + len)
{
result = LUATEXTS_EGARBAGE;
}
else
{
*dest = value;
}
}
return result;
}
static int load_value(lua_State * L, lts_LoadState * ls);
static int load_table(lua_State * L, lts_LoadState * ls)
{
LUATEXTS_UINT array_size = 0;
LUATEXTS_UINT hash_size = 0;
LUATEXTS_UINT total_size = 0;
int result = ltsLS_readuint(ls, &array_size);
if (result == LUATEXTS_ESUCCESS)
{
result = ltsLS_readuint(ls, &hash_size);
}
if (result == LUATEXTS_ESUCCESS)
{
total_size = array_size + hash_size;
/*
* Security note: not checking for unread buffer size underflow.
* Motivation: will fail at read time, so why bother?
*/
if (
array_size < 0 || array_size > MAXASIZE ||
hash_size < 0 ||
(hash_size > 0 && ceillog2((unsigned int)hash_size) > MAXBITS)
)
{
result = LUATEXTS_ETOOHUGE;
}
}
if (result == LUATEXTS_ESUCCESS)
{
LUATEXTS_UINT i = 0;
lua_createtable(L, array_size, hash_size);
for (i = 0; i < total_size; ++i)
{
int key_type = LUA_TNONE;
result = load_value(L, ls); /* Load key. */
if (result != LUATEXTS_ESUCCESS)
{
break;
}
/* Table key can't be nil or NaN */
key_type = lua_type(L, -1);
if (key_type == LUA_TNIL)
{
/* Corrupt data? */
result = LUATEXTS_EBADDATA;
break;
}
if (key_type == LUA_TNUMBER)
{
lua_Number key = lua_tonumber(L, -1);
if (luai_numisnan(key))
{
/* Corrupt data? */
result = LUATEXTS_EBADDATA;
break;
}
}
result = load_value(L, ls); /* Load value. */
if (result != LUATEXTS_ESUCCESS)
{
break;
}
lua_rawset(L, -3);
}
}
return result;
}
static int load_value(lua_State * L, lts_LoadState * ls)
{
size_t len = 0;
const unsigned char * type = NULL;
/* Read value type */
int result = ltsLS_readline(ls, &type, &len);
if (result == LUATEXTS_ESUCCESS && len != 1)
{
result = LUATEXTS_EGARBAGE;
}
/* Read value data */
if (result == LUATEXTS_ESUCCESS)
{
XSPAM(("VALUE TYPE '%c'\n", type[0]));
switch (type[0])
{
case LUATEXTS_CNIL:
lua_pushnil(L);
break;
case LUATEXTS_CFALSE:
lua_pushboolean(L, 0);
break;
case LUATEXTS_CTRUE:
lua_pushboolean(L, 1);
break;
case LUATEXTS_CNUMBER:
{
lua_Number value;
result = ltsLS_readnumber(ls, &value);
if (result == LUATEXTS_ESUCCESS)
{
lua_pushnumber(L, value);
}
}
break;
case LUATEXTS_CUINT:
{
LUATEXTS_UINT value;
result = ltsLS_readuint(ls, &value);
if (result == LUATEXTS_ESUCCESS)
{
XSPAM(("VALUE UINT %lu\n", value));
lua_pushinteger(L, value);
}
}
break;
case LUATEXTS_CSTRING:
{
const unsigned char * str = NULL;
/* Read string size */
LUATEXTS_UINT len = 0;
result = ltsLS_readuint(ls, &len);
/* Read string data */
if (result == LUATEXTS_ESUCCESS)
{
str = ltsLS_eat(ls, len);
if (str == NULL)
{
result = LUATEXTS_EBADSIZE;
}
}
/* Eat newline after string data */
if (result == LUATEXTS_ESUCCESS)
{
size_t empty = 0;
const unsigned char * nl = NULL;
result = ltsLS_readline(ls, &nl, &empty);
if (result == LUATEXTS_ESUCCESS && empty != 0)
{
result = LUATEXTS_EGARBAGE;
}
}
if (result == LUATEXTS_ESUCCESS)
{
lua_pushlstring(L, (const char *)str, len);
}
}
break;
case LUATEXTS_CSTRINGUTF8:
{
const unsigned char * str = NULL;
/* Read string size */
LUATEXTS_UINT len_chars = 0;
size_t len_bytes = 0;
result = ltsLS_readuint(ls, &len_chars);
/* Read string data */
if (result == LUATEXTS_ESUCCESS)
{
result = ltsLS_eatutf8(ls, len_chars, &str, &len_bytes);
}
/* Eat newline after string data */
if (result == LUATEXTS_ESUCCESS)
{
size_t empty = 0;
const unsigned char * nl = NULL;
result = ltsLS_readline(ls, &nl, &empty);
if (result == LUATEXTS_ESUCCESS && empty != 0)
{
result = LUATEXTS_EGARBAGE;
}
}
if (result == LUATEXTS_ESUCCESS)
{
lua_pushlstring(L, (const char *)str, len_bytes);
}
}
break;
case LUATEXTS_CTABLE:
result = load_table(L, ls);
break;
default:
result = LUATEXTS_EBADTYPE;
break;
}
}
XSPAM(("LOAD VALUE result %d\n", result));
return result;
}
static int luatexts_load(
lua_State * L,
const unsigned char * buf,
size_t len,
size_t * count
)
{
int result = LUATEXTS_ESUCCESS;
lts_LoadState ls;
LUATEXTS_UINT tuple_size = 0;
LUATEXTS_UINT i = 0;
int base = lua_gettop(L);
ltsLS_init(&ls, buf, len);
/*
* Security note: not checking tuple_size for sanity.
* Motivation: too complicated; we will fail if buffer is too small anyway.
*/
result = ltsLS_readuint(&ls, &tuple_size);
for (i = 0; i < tuple_size && result == LUATEXTS_ESUCCESS; ++i)
{
result = load_value(L, &ls);
}
/*
* Security note: ignoring unread bytes if any.
* Motivation: more casual, this is a text format after all.
*/
if (result == LUATEXTS_ESUCCESS)
{
*count = tuple_size;
}
else
{
XSPAM(("LOAD TUPLE error %d\n", result));
lua_settop(L, base); /* Discard intermediate results */
switch (result)
{
case LUATEXTS_EBADSIZE:
lua_pushliteral(L, "load failed: corrupt data, bad size");
break;
case LUATEXTS_EBADDATA:
lua_pushliteral(L, "load failed: corrupt data");
break;
case LUATEXTS_EBADTYPE:
lua_pushliteral(L, "load failed: unknown data type");
break;
case LUATEXTS_EGARBAGE:
lua_pushliteral(L, "load failed: garbage before newline");
break;
case LUATEXTS_ETOOHUGE:
lua_pushliteral(L, "load failed: value too huge");
break;
case LUATEXTS_EBADUTF8:
lua_pushliteral(L, "load failed: invalid utf-8 data");
break;
case LUATEXTS_ECLIPPED:
lua_pushliteral(L, "load failed: corrupt data, truncated");
break;
/* should not happen */
case LUATEXTS_EFAILURE:
default:
lua_pushliteral(L, "load failed: internal error");
break;
}
}
return result;
}
static int lload(lua_State * L)
{
size_t len = 0;
const unsigned char * buf = (const unsigned char *)luaL_checklstring(
L, 1, &len
);
size_t tuple_size = 0;
int result = 0;
lua_pushboolean(L, 1);
result = luatexts_load(L, buf, len, &tuple_size);
if (result != LUATEXTS_ESUCCESS)
{
lua_pushnil(L);
lua_replace(L, -3); /* Replace pre-pushed true with nil */
return 2; /* Error message already on stack */
}
return tuple_size + 1;
}
/* Lua module API */
static const struct luaL_reg R[] =
{
{ "load", lload },
{ NULL, NULL }
};
#ifdef __cplusplus
extern "C" {
#endif
LUALIB_API int luaopen_luatexts(lua_State * L)
{
/*
* Register module
*/
luaL_register(L, "luatexts", R);
/*
* Register module information
*/
lua_pushliteral(L, LUATEXTS_VERSION);
lua_setfield(L, -2, "_VERSION");
lua_pushliteral(L, LUATEXTS_COPYRIGHT);
lua_setfield(L, -2, "_COPYRIGHT");
lua_pushliteral(L, LUATEXTS_DESCRIPTION);
lua_setfield(L, -2, "_DESCRIPTION");
return 1;
}
#ifdef __cplusplus
}
#endif