FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ltask/src/lua-seri.c at master · cloudwu/ltask · GitHub
cloudwu
ltask
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ltask
/
src
/
lua-seri.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
952 lines (865 loc) · 19.8 KB
Breadcrumbs
ltask
/
src
/
lua-seri.c
Copy path
File metadata and controls
952 lines (865 loc) · 19.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*
modify from https://github.com/cloudwu/lua-serialize
*/
#ifdef
TEST_SERI
#define
LUA_LIB
#endif
#include
<lua.h>
#include
<lauxlib.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
<assert.h>
#include
<string.h>
#define
TYPE_BOOLEAN
0
#define
TYPE_BOOLEAN_NIL
0
#define
TYPE_BOOLEAN_FALSE
1
#define
TYPE_BOOLEAN_TRUE
2
// hibits 0 false 1 true
#define
TYPE_NUMBER
1
// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double
#define
TYPE_NUMBER_ZERO
0
#define
TYPE_NUMBER_BYTE
1
#define
TYPE_NUMBER_WORD
2
#define
TYPE_NUMBER_DWORD
4
#define
TYPE_NUMBER_QWORD
6
#define
TYPE_NUMBER_REAL
8
#define
TYPE_USERDATA
2
// hibits 0 : void *
// hibits 1 : c function
#define
TYPE_USERDATA_POINTER
0
#define
TYPE_USERDATA_CFUNCTION
1
#define
TYPE_SHORT_STRING
3
// hibits 0~31 : len
#define
TYPE_LONG_STRING
4
// hibits 0~30 : array size , 31 : extend size
#define
TYPE_TABLE
5
#define
TYPE_TABLE_MARK
6
// hibits 0~30 : ref ancestor id , 31 : extend id ( number )
#define
TYPE_REF
7
#define
MAX_COOKIE
32
#define
EXTEND_NUMBER
(MAX_COOKIE-1)
#define
COMBINE_TYPE
(
t
,
v
) ((t) | (v) << 3)
#define
BLOCK_SIZE
128
#define
MAX_DEPTH
31
#define
MAX_REFERENCE
32
struct
block
{
struct
block
*
next
;
char
buffer
[
BLOCK_SIZE
];
};
struct
stack
{
int
depth
;
int
ref_index
;
int
objectid
;
int
ancestor
[
MAX_DEPTH
];
};
struct
reference
{
const
void
*
object
;
uint8_t
*
address
;
};
struct
write_block
{
struct
block
*
head
;
struct
block
*
current
;
int
len
;
int
ptr
;
struct
stack
s
;
struct
reference
r
[
MAX_REFERENCE
];
};
struct
read_block
{
char
*
buffer
;
int
len
;
int
ptr
;
struct
stack
s
;
};
inline
static
struct
block
*
blk_alloc
(
void
) {
struct
block
*
b
=
malloc
(
sizeof
(
struct
block
));
b
->
next
=
NULL
;
return
b
;
}
static
inline
void
wb_push
(
struct
write_block
*
b
,
const
void
*
buf
,
int
sz
) {
const
char
*
buffer
=
buf
;
if
(
b
->
ptr
>=
BLOCK_SIZE
) {
_again
:
b
->
current
=
b
->
current
->
next
=
blk_alloc
();
b
->
ptr
=
0
;
}
if
(
b
->
ptr
<=
BLOCK_SIZE
-
sz
) {
memcpy
(
b
->
current
->
buffer
+
b
->
ptr
,
buffer
,
sz
);
b
->
ptr
+=
sz
;
b
->
len
+=
sz
;
}
else
{
int
copy
=
BLOCK_SIZE
-
b
->
ptr
;
memcpy
(
b
->
current
->
buffer
+
b
->
ptr
,
buffer
,
copy
);
buffer
+=
copy
;
b
->
len
+=
copy
;
sz
-=
copy
;
goto
_again
;
}
}
static
inline
void
*
wb_address
(
struct
write_block
*
b
) {
if
(
b
->
ptr
==
BLOCK_SIZE
) {
b
->
current
=
b
->
current
->
next
=
blk_alloc
();
b
->
ptr
=
0
;
}
return
(
void
*
)(
b
->
current
->
buffer
+
b
->
ptr
);
}
static
inline
void
init_stack
(
struct
stack
*
s
) {
s
->
depth
=
0
;
s
->
objectid
=
0
;
s
->
ref_index
=
0
;
}
static
void
wb_init
(
struct
write_block
*
wb
,
struct
block
*
b
) {
wb
->
head
=
b
;
assert
(
b
->
next
==
NULL
);
wb
->
len
=
0
;
wb
->
current
=
wb
->
head
;
wb
->
ptr
=
0
;
init_stack
(
&
wb
->
s
);
}
static
void
wb_free
(
struct
write_block
*
wb
) {
struct
block
*
blk
=
wb
->
head
;
blk
=
blk
->
next
;
// the first block is on stack
while
(
blk
) {
struct
block
*
next
=
blk
->
next
;
free
(
blk
);
blk
=
next
;
}
wb
->
head
=
NULL
;
wb
->
current
=
NULL
;
wb
->
ptr
=
0
;
wb
->
len
=
0
;
}
static
void
rball_init
(
struct
read_block
*
rb
,
char
*
buffer
,
int
size
) {
rb
->
buffer
=
buffer
;
rb
->
len
=
size
;
rb
->
ptr
=
0
;
init_stack
(
&
rb
->
s
);
}
static
const
void
*
rb_read
(
struct
read_block
*
rb
,
int
sz
) {
if
(
rb
->
len
<
sz
) {
return
NULL
;
}
int
ptr
=
rb
->
ptr
;
rb
->
ptr
+=
sz
;
rb
->
len
-=
sz
;
return
rb
->
buffer
+
ptr
;
}
static
inline
void
wb_nil
(
struct
write_block
*
wb
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_BOOLEAN
,
TYPE_BOOLEAN_NIL
);
wb_push
(
wb
,
&
n
,
1
);
}
static
inline
void
wb_boolean
(
struct
write_block
*
wb
,
int
boolean
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_BOOLEAN
,
boolean
?
TYPE_BOOLEAN_TRUE
:
TYPE_BOOLEAN_FALSE
);
wb_push
(
wb
,
&
n
,
1
);
}
static
inline
void
wb_integer
(
struct
write_block
*
wb
,
lua_Integer
v
) {
int
type
=
TYPE_NUMBER
;
if
(
v
==
0
) {
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_ZERO
);
wb_push
(
wb
,
&
n
,
1
);
}
else
if
(
v
!=
(
int32_t
)
v
) {
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_QWORD
);
int64_t
v64
=
v
;
wb_push
(
wb
,
&
n
,
1
);
wb_push
(
wb
,
&
v64
,
sizeof
(
v64
));
}
else
if
(
v
<
0
) {
int32_t
v32
=
(
int32_t
)
v
;
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_DWORD
);
wb_push
(
wb
,
&
n
,
1
);
wb_push
(
wb
,
&
v32
,
sizeof
(
v32
));
}
else
if
(
v
<
0x100
) {
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_BYTE
);
wb_push
(
wb
,
&
n
,
1
);
uint8_t
byte
=
(
uint8_t
)
v
;
wb_push
(
wb
,
&
byte
,
sizeof
(
byte
));
}
else
if
(
v
<
0x10000
) {
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_WORD
);
wb_push
(
wb
,
&
n
,
1
);
uint16_t
word
=
(
uint16_t
)
v
;
wb_push
(
wb
,
&
word
,
sizeof
(
word
));
}
else
{
uint8_t
n
=
COMBINE_TYPE
(
type
,
TYPE_NUMBER_DWORD
);
wb_push
(
wb
,
&
n
,
1
);
uint32_t
v32
=
(
uint32_t
)
v
;
wb_push
(
wb
,
&
v32
,
sizeof
(
v32
));
}
}
static
inline
void
wb_real
(
struct
write_block
*
wb
,
double
v
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_NUMBER
,
TYPE_NUMBER_REAL
);
wb_push
(
wb
,
&
n
,
1
);
wb_push
(
wb
,
&
v
,
sizeof
(
v
));
}
static
inline
void
wb_pointer
(
struct
write_block
*
wb
,
void
*
v
,
int
subtype
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_USERDATA
,
subtype
);
wb_push
(
wb
,
&
n
,
1
);
wb_push
(
wb
,
&
v
,
sizeof
(
v
));
}
static
inline
void
wb_string
(
struct
write_block
*
wb
,
const
char
*
str
,
size_t
len
) {
if
(
len
<
MAX_COOKIE
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_SHORT_STRING
,
len
);
wb_push
(
wb
,
&
n
,
1
);
if
(
len
>
0
) {
wb_push
(
wb
,
str
,
len
);
}
}
else
{
uint8_t
n
;
if
(
len
<
0x10000
) {
n
=
COMBINE_TYPE
(
TYPE_LONG_STRING
,
2
);
wb_push
(
wb
,
&
n
,
1
);
uint16_t
x
=
(
uint16_t
)
len
;
wb_push
(
wb
,
&
x
,
2
);
}
else
{
n
=
COMBINE_TYPE
(
TYPE_LONG_STRING
,
4
);
wb_push
(
wb
,
&
n
,
1
);
uint32_t
x
=
(
uint32_t
)
len
;
assert
(
x
==
len
);
// can't fit in 4 bytes
wb_push
(
wb
,
&
x
,
4
);
}
wb_push
(
wb
,
str
,
len
);
}
}
static
void
pack_one
(
lua_State
*
L
,
struct
write_block
*
b
,
int
index
);
static
int
wb_table_array
(
lua_State
*
L
,
struct
write_block
*
wb
,
int
index
) {
int
array_size
=
(
int
)
lua_rawlen
(
L
,
index
);
if
(
array_size
>=
EXTEND_NUMBER
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_TABLE
,
EXTEND_NUMBER
);
wb_push
(
wb
,
&
n
,
1
);
wb_integer
(
wb
,
array_size
);
}
else
{
uint8_t
n
=
COMBINE_TYPE
(
TYPE_TABLE
,
array_size
);
wb_push
(
wb
,
&
n
,
1
);
}
int
i
;
for
(
i
=
1
;
i
<=
array_size
;
i
++
) {
lua_rawgeti
(
L
,
index
,
i
);
pack_one
(
L
,
wb
,
-1
);
lua_pop
(
L
,
1
);
}
return
array_size
;
}
static
void
wb_table_hash
(
lua_State
*
L
,
struct
write_block
*
wb
,
int
index
,
int
array_size
) {
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
index
)
!=
0
) {
if
(
lua_type
(
L
,
-2
)
==
LUA_TNUMBER
) {
if
(
lua_isinteger
(
L
,
-2
)) {
lua_Integer
x
=
lua_tointeger
(
L
,
-2
);
if
(
x
>
0
&&
x
<=
array_size
) {
lua_pop
(
L
,
1
);
continue
;
}
}
}
pack_one
(
L
,
wb
,
-2
);
pack_one
(
L
,
wb
,
-1
);
lua_pop
(
L
,
1
);
}
wb_nil
(
wb
);
}
static
void
wb_table_metapairs
(
lua_State
*
L
,
struct
write_block
*
wb
,
int
index
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_TABLE
,
0
);
wb_push
(
wb
,
&
n
,
1
);
lua_pushvalue
(
L
,
index
);
lua_call
(
L
,
1
,
3
);
for
(;;) {
lua_pushvalue
(
L
,
-2
);
lua_pushvalue
(
L
,
-2
);
lua_copy
(
L
,
-5
,
-3
);
lua_call
(
L
,
2
,
2
);
int
type
=
lua_type
(
L
,
-2
);
if
(
type
==
LUA_TNIL
) {
lua_pop
(
L
,
4
);
break
;
}
pack_one
(
L
,
wb
,
-2
);
pack_one
(
L
,
wb
,
-1
);
lua_pop
(
L
,
1
);
}
wb_nil
(
wb
);
}
static
inline
void
mark_table
(
lua_State
*
L
,
struct
write_block
*
b
,
int
index
) {
const
void
*
obj
=
lua_topointer
(
L
,
index
);
struct
stack
*
s
=
&
b
->
s
;
int
id
=
s
->
objectid
++
;
void
*
addr
=
wb_address
(
b
);
if
(
id
==
MAX_REFERENCE
) {
lua_createtable
(
L
,
0
,
MAX_REFERENCE
+
1
);
lua_replace
(
L
,
s
->
ref_index
);
lua_createtable
(
L
,
MAX_REFERENCE
+
1
,
0
);
lua_replace
(
L
,
s
->
ref_index
+
1
);
int
i
;
for
(
i
=
0
;
i
<
MAX_REFERENCE
;
i
++
) {
lua_pushinteger
(
L
,
i
+
1
);
lua_rawsetp
(
L
,
s
->
ref_index
,
b
->
r
[
i
].
object
);
if
(
b
->
r
[
i
].
address
) {
lua_pushlightuserdata
(
L
, (
void
*
)
b
->
r
[
i
].
address
);
lua_rawseti
(
L
,
s
->
ref_index
+
1
,
i
+
1
);
}
}
}
if
(
id
<
MAX_REFERENCE
) {
b
->
r
[
id
].
object
=
obj
;
b
->
r
[
id
].
address
=
addr
;
}
else
{
++
id
;
lua_pushinteger
(
L
,
id
);
lua_rawsetp
(
L
,
s
->
ref_index
,
obj
);
lua_pushlightuserdata
(
L
,
addr
);
lua_rawseti
(
L
,
s
->
ref_index
+
1
,
id
);
}
}
static
void
wb_table
(
lua_State
*
L
,
struct
write_block
*
wb
,
int
index
) {
luaL_checkstack
(
L
,
LUA_MINSTACK
,
NULL
);
if
(
index
<
0
) {
index
=
lua_gettop
(
L
)
+
index
+
1
;
}
mark_table
(
L
,
wb
,
index
);
if
(
luaL_getmetafield
(
L
,
index
,
"__pairs"
)
!=
LUA_TNIL
) {
wb_table_metapairs
(
L
,
wb
,
index
);
}
else
{
int
array_size
=
wb_table_array
(
L
,
wb
,
index
);
wb_table_hash
(
L
,
wb
,
index
,
array_size
);
}
}
static
int
ref_ancestor
(
lua_State
*
L
,
struct
write_block
*
b
,
int
index
) {
struct
stack
*
s
=
&
b
->
s
;
int
n
=
s
->
depth
;
if
(
n
==
0
||
n
>=
MAX_DEPTH
)
return
0
;
int
i
;
const
void
*
obj
=
lua_topointer
(
L
,
index
);
for
(
i
=
n
-
1
;
i
>=
0
;
i
--
) {
const
void
*
ancestor
=
lua_topointer
(
L
,
s
->
ancestor
[
i
]);
if
(
ancestor
==
obj
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_REF
,
i
);
wb_push
(
b
,
&
n
,
1
);
return
1
;
}
}
return
0
;
}
static
inline
void
change_mark
(
uint8_t
*
tag
) {
assert
((
*
tag
&
0x7
)
==
TYPE_TABLE
);
*
tag
=
COMBINE_TYPE
(
TYPE_TABLE_MARK
,
*
tag
>>
3
);
}
static
inline
int
lookup_ref
(
lua_State
*
L
,
struct
write_block
*
b
,
const
void
*
obj
) {
if
(
b
->
s
.
objectid
<=
MAX_REFERENCE
) {
int
i
;
for
(
i
=
0
;
i
<
b
->
s
.
objectid
;
i
++
) {
if
(
obj
==
b
->
r
[
i
].
object
) {
if
(
b
->
r
[
i
].
address
) {
change_mark
(
b
->
r
[
i
].
address
);
b
->
r
[
i
].
address
=
NULL
;
}
return
i
+
1
;
}
}
return
0
;
}
else
{
if
(
lua_rawgetp
(
L
,
b
->
s
.
ref_index
,
obj
)
!=
LUA_TNUMBER
) {
lua_pop
(
L
,
1
);
return
0
;
}
int
id
=
lua_tointeger
(
L
,
-1
);
lua_pop
(
L
,
1
);
if
(
lua_rawgeti
(
L
,
b
->
s
.
ref_index
+
1
,
id
)
==
LUA_TLIGHTUSERDATA
) {
uint8_t
*
tag
=
(
uint8_t
*
)
lua_touserdata
(
L
,
-1
);
lua_pop
(
L
,
1
);
change_mark
(
tag
);
lua_pushnil
(
L
);
lua_rawseti
(
L
,
b
->
s
.
ref_index
+
1
,
id
);
}
else
{
lua_pop
(
L
,
1
);
}
return
id
;
}
}
static
int
ref_object
(
lua_State
*
L
,
struct
write_block
*
b
,
int
index
) {
const
void
*
obj
=
lua_topointer
(
L
,
index
);
int
id
=
lookup_ref
(
L
,
b
,
obj
);
if
(
id
>
0
) {
uint8_t
n
=
COMBINE_TYPE
(
TYPE_REF
,
EXTEND_NUMBER
);
wb_push
(
b
,
&
n
,
1
);
wb_integer
(
b
,
id
);
return
1
;
}
else
{
return
0
;
}
}
static
void
pack_one
(
lua_State
*
L
,
struct
write_block
*
b
,
int
index
) {
struct
stack
*
s
=
&
b
->
s
;
int
type
=
lua_type
(
L
,
index
);
switch
(
type
) {
case
LUA_TNIL
:
wb_nil
(
b
);
break
;
case
LUA_TNUMBER
: {
if
(
lua_isinteger
(
L
,
index
)) {
lua_Integer
x
=
lua_tointeger
(
L
,
index
);
wb_integer
(
b
,
x
);
}
else
{
lua_Number
n
=
lua_tonumber
(
L
,
index
);
wb_real
(
b
,
n
);
}
break
;
}
case
LUA_TBOOLEAN
:
wb_boolean
(
b
,
lua_toboolean
(
L
,
index
));
break
;
case
LUA_TSTRING
: {
size_t
sz
=
0
;
const
char
*
str
=
lua_tolstring
(
L
,
index
,
&
sz
);
wb_string
(
b
,
str
,
sz
);
break
;
}
case
LUA_TLIGHTUSERDATA
:
wb_pointer
(
b
,
lua_touserdata
(
L
,
index
),
TYPE_USERDATA_POINTER
);
break
;
case
LUA_TFUNCTION
: {
lua_CFunction
func
=
lua_tocfunction
(
L
,
index
);
if
(
func
==
NULL
||
lua_getupvalue
(
L
,
index
,
1
)
!=
NULL
) {
luaL_error
(
L
,
"Only light C function can be serialized"
);
}
wb_pointer
(
b
, (
void
*
)
func
,
TYPE_USERDATA_CFUNCTION
);
break
; }
case
LUA_TTABLE
: {
if
(
index
<
0
) {
index
=
lua_gettop
(
L
)
+
index
+
1
;
}
if
(
ref_ancestor
(
L
,
b
,
index
))
break
;
if
(
ref_object
(
L
,
b
,
index
))
break
;
if
(
s
->
depth
<
MAX_DEPTH
)
s
->
ancestor
[
s
->
depth
]
=
index
;
++
s
->
depth
;
wb_table
(
L
,
b
,
index
);
--
s
->
depth
;
break
;
}
default
:
wb_free
(
b
);
luaL_error
(
L
,
"Unsupport type %s to serialize"
,
lua_typename
(
L
,
type
));
}
}
static
void
pack_from
(
lua_State
*
L
,
struct
write_block
*
b
,
int
from
) {
int
top
=
lua_gettop
(
L
);
int
n
=
top
-
from
;
int
i
;
lua_pushnil
(
L
);
// slot for table ref lookup { pointer -> id }
lua_pushnil
(
L
);
// slot for table refs array { address, ... }
b
->
s
.
ref_index
=
top
+
1
;
for
(
i
=
1
;
i
<=
n
;
i
++
) {
pack_one
(
L
,
b
,
from
+
i
);
}
}
static
inline
void
invalid_stream_line
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
line
) {
int
len
=
rb
->
len
;
luaL_error
(
L
,
"Invalid serialize stream %d (line:%d)"
,
len
,
line
);
}
#define
invalid_stream
(
L
,
rb
) invalid_stream_line(L,rb,__LINE__)
static
lua_Integer
get_integer
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
cookie
) {
switch
(
cookie
) {
case
TYPE_NUMBER_ZERO
:
return
0
;
case
TYPE_NUMBER_BYTE
: {
uint8_t
n
;
const
uint8_t
*
pn
=
(
const
uint8_t
*
)
rb_read
(
rb
,
sizeof
(
n
));
if
(
pn
==
NULL
)
invalid_stream
(
L
,
rb
);
n
=
*
pn
;
return
n
;
}
case
TYPE_NUMBER_WORD
: {
uint16_t
n
;
const
void
*
pn
=
rb_read
(
rb
,
sizeof
(
n
));
if
(
pn
==
NULL
)
invalid_stream
(
L
,
rb
);
memcpy
(
&
n
,
pn
,
sizeof
(
n
));
return
n
;
}
case
TYPE_NUMBER_DWORD
: {
int32_t
n
;
const
void
*
pn
=
rb_read
(
rb
,
sizeof
(
n
));
if
(
pn
==
NULL
)
invalid_stream
(
L
,
rb
);
memcpy
(
&
n
,
pn
,
sizeof
(
n
));
return
n
;
}
case
TYPE_NUMBER_QWORD
: {
int64_t
n
;
const
void
*
pn
=
rb_read
(
rb
,
sizeof
(
n
));
if
(
pn
==
NULL
)
invalid_stream
(
L
,
rb
);
memcpy
(
&
n
,
pn
,
sizeof
(
n
));
return
n
;
}
default
:
invalid_stream
(
L
,
rb
);
return
0
;
}
}
static
double
get_real
(
lua_State
*
L
,
struct
read_block
*
rb
) {
double
n
;
const
void
*
pn
=
rb_read
(
rb
,
sizeof
(
n
));
if
(
pn
==
NULL
)
invalid_stream
(
L
,
rb
);
memcpy
(
&
n
,
pn
,
sizeof
(
n
));
return
n
;
}
static
void
*
get_pointer
(
lua_State
*
L
,
struct
read_block
*
rb
) {
void
*
userdata
=
0
;
const
void
*
v
=
rb_read
(
rb
,
sizeof
(
userdata
));
if
(
v
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
memcpy
(
&
userdata
,
v
,
sizeof
(
userdata
));
return
userdata
;
}
static
void
get_buffer
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
len
) {
const
char
*
p
=
(
const
char
*
)
rb_read
(
rb
,
len
);
if
(
p
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
lua_pushlstring
(
L
,
p
,
len
);
}
static
void
unpack_one
(
lua_State
*
L
,
struct
read_block
*
rb
);
static
int
get_extend_integer
(
lua_State
*
L
,
struct
read_block
*
rb
) {
uint8_t
type
;
const
uint8_t
*
t
=
rb_read
(
rb
,
sizeof
(
type
));
if
(
t
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
type
=
*
t
;
int
cookie
=
type
>>
3
;
if
((
type
&
7
)
!=
TYPE_NUMBER
||
cookie
==
TYPE_NUMBER_REAL
) {
invalid_stream
(
L
,
rb
);
}
return
(
int
)
get_integer
(
L
,
rb
,
cookie
);
}
static
void
unpack_table
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
array_size
,
int
type
) {
if
(
array_size
==
EXTEND_NUMBER
) {
array_size
=
get_extend_integer
(
L
,
rb
);
}
struct
stack
*
s
=
&
rb
->
s
;
int
id
=
++
s
->
objectid
;
luaL_checkstack
(
L
,
LUA_MINSTACK
,
NULL
);
lua_createtable
(
L
,
array_size
,
0
);
if
(
type
==
TYPE_TABLE_MARK
) {
lua_pushvalue
(
L
,
-1
);
if
(
lua_type
(
L
,
s
->
ref_index
)
==
LUA_TNIL
) {
lua_newtable
(
L
);
lua_replace
(
L
,
s
->
ref_index
);
}
lua_rawseti
(
L
,
s
->
ref_index
,
id
);
}
if
(
s
->
depth
<
MAX_DEPTH
)
s
->
ancestor
[
s
->
depth
]
=
lua_gettop
(
L
);
++
s
->
depth
;
int
i
;
for
(
i
=
1
;
i
<=
array_size
;
i
++
) {
unpack_one
(
L
,
rb
);
lua_rawseti
(
L
,
-2
,
i
);
}
--
s
->
depth
;
for
(;;) {
unpack_one
(
L
,
rb
);
if
(
lua_isnil
(
L
,
-1
)) {
lua_pop
(
L
,
1
);
return
;
}
++
s
->
depth
;
unpack_one
(
L
,
rb
);
--
s
->
depth
;
lua_rawset
(
L
,
-3
);
}
}
static
void
unpack_ref
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
ref
) {
struct
stack
*
s
=
&
rb
->
s
;
if
(
ref
==
EXTEND_NUMBER
) {
int
id
=
get_extend_integer
(
L
,
rb
);
if
(
lua_type
(
L
,
s
->
ref_index
)
!=
LUA_TTABLE
||
lua_rawgeti
(
L
,
s
->
ref_index
,
id
)
!=
LUA_TTABLE
) {
luaL_error
(
L
,
"Invalid ref object id %d"
,
id
);
}
}
else
{
if
(
ref
>=
s
->
depth
)
luaL_error
(
L
,
"Invalid ref object %d/%d"
,
ref
,
s
->
depth
);
lua_pushvalue
(
L
,
s
->
ancestor
[
ref
]);
}
}
static
void
push_value
(
lua_State
*
L
,
struct
read_block
*
rb
,
int
type
,
int
cookie
) {
switch
(
type
) {
case
TYPE_BOOLEAN
:
switch
(
cookie
) {
case
TYPE_BOOLEAN_NIL
:
lua_pushnil
(
L
);
break
;
case
TYPE_BOOLEAN_FALSE
:
lua_pushboolean
(
L
,
0
);
break
;
case
TYPE_BOOLEAN_TRUE
:
lua_pushboolean
(
L
,
1
);
break
;
default
:
luaL_error
(
L
,
"Invalid boolean subtype %d"
,
cookie
);
}
break
;
case
TYPE_NUMBER
:
if
(
cookie
==
TYPE_NUMBER_REAL
) {
lua_pushnumber
(
L
,
get_real
(
L
,
rb
));
}
else
{
lua_pushinteger
(
L
,
get_integer
(
L
,
rb
,
cookie
));
}
break
;
case
TYPE_USERDATA
:
if
(
cookie
==
TYPE_USERDATA_POINTER
)
lua_pushlightuserdata
(
L
,
get_pointer
(
L
,
rb
));
else
{
if
(
cookie
!=
TYPE_USERDATA_CFUNCTION
)
luaL_error
(
L
,
"Invalid userdata"
);
lua_pushcfunction
(
L
, (
lua_CFunction
)
get_pointer
(
L
,
rb
));
}
break
;
case
TYPE_SHORT_STRING
:
get_buffer
(
L
,
rb
,
cookie
);
break
;
case
TYPE_LONG_STRING
: {
if
(
cookie
==
2
) {
const
void
*
plen
=
rb_read
(
rb
,
2
);
if
(
plen
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
uint16_t
n
;
memcpy
(
&
n
,
plen
,
sizeof
(
n
));
get_buffer
(
L
,
rb
,
n
);
}
else
{
if
(
cookie
!=
4
) {
invalid_stream
(
L
,
rb
);
}
const
void
*
plen
=
rb_read
(
rb
,
4
);
if
(
plen
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
uint32_t
n
;
memcpy
(
&
n
,
plen
,
sizeof
(
n
));
get_buffer
(
L
,
rb
,
n
);
}
break
;
}
case
TYPE_TABLE
:
case
TYPE_TABLE_MARK
:
unpack_table
(
L
,
rb
,
cookie
,
type
);
break
;
case
TYPE_REF
:
unpack_ref
(
L
,
rb
,
cookie
);
break
;
default
:
invalid_stream
(
L
,
rb
);
break
;
}
}
static
void
unpack_one
(
lua_State
*
L
,
struct
read_block
*
rb
) {
uint8_t
type
;
const
uint8_t
*
t
=
rb_read
(
rb
,
sizeof
(
type
));
if
(
t
==
NULL
) {
invalid_stream
(
L
,
rb
);
}
type
=
*
t
;
push_value
(
L
,
rb
,
type
&
0x7
,
type
>>
3
);
}
static
void
*
seri
(
struct
block
*
b
,
int
len
) {
if
(
len
<
0
)
return
NULL
;
uint8_t
*
buffer
=
malloc
(
len
+
4
);
memcpy
(
buffer
,
&
len
,
4
);
// write length
uint8_t
*
ptr
=
buffer
+
4
;
while
(
len
>
0
) {
if
(
len
>=
BLOCK_SIZE
) {
len
-=
BLOCK_SIZE
;
if
(
len
<
0
) {
free
(
buffer
);
return
NULL
;
}
memcpy
(
ptr
,
b
->
buffer
,
BLOCK_SIZE
);
ptr
+=
BLOCK_SIZE
;
b
=
b
->
next
;
}
else
{
memcpy
(
ptr
,
b
->
buffer
,
len
);
break
;
}
}
return
buffer
;
}
int
seri_unpack
(
lua_State
*
L
,
void
*
buffer
) {
int
top
=
lua_gettop
(
L
);
int
len
=
0
;
memcpy
(
&
len
,
buffer
,
4
);
// get length
struct
read_block
rb
;
rball_init
(
&
rb
, (
char
*
)
buffer
+
4
,
len
);
lua_pushnil
(
L
);
// slot for ref table
rb
.
s
.
ref_index
=
top
+
1
;
int
i
;
for
(
i
=
0
;;
i
++
) {
if
(
i
%
8
==
0
) {
luaL_checkstack
(
L
,
LUA_MINSTACK
,
NULL
);
}
uint8_t
type
=
0
;
const
uint8_t
*
t
=
rb_read
(
&
rb
,
sizeof
(
type
));
if
(
t
==
NULL
)
break
;
type
=
*
t
;
push_value
(
L
,
&
rb
,
type
&
0x7
,
type
>>
3
);
}
return
lua_gettop
(
L
)
-
1
-
top
;
}
static
int
seri_unpack_
(
lua_State
*
L
) {
void
*
buffer
=
lua_touserdata
(
L
,
1
);
lua_settop
(
L
,
0
);
return
seri_unpack
(
L
,
buffer
);
}
int
seri_unpackptr
(
lua_State
*
L
,
void
*
buffer
) {
int
top
=
lua_gettop
(
L
);
lua_pushcfunction
(
L
,
seri_unpack_
);
lua_pushlightuserdata
(
L
,
buffer
);
int
err
=
lua_pcall
(
L
,
1
,
LUA_MULTRET
,
0
);
free
(
buffer
);
if
(
err
!=
LUA_OK
) {
lua_error
(
L
);
}
return
lua_gettop
(
L
)
-
top
;
}
int
seri_unpackstr
(
lua_State
*
L
) {
const
char
*
buffer
=
luaL_checkstring
(
L
,
1
);
lua_settop
(
L
,
1
);
lua_pushcfunction
(
L
,
seri_unpack_
);
lua_pushlightuserdata
(
L
, (
void
*
)
buffer
);
if
(
lua_pcall
(
L
,
1
,
LUA_MULTRET
,
0
)
!=
LUA_OK
) {
lua_error
(
L
);
}
return
lua_gettop
(
L
)
-
1
;
}
void
*
seri_pack
(
lua_State
*
L
,
int
from
,
int
*
sz
) {
struct
block
temp
;
temp
.
next
=
NULL
;
struct
write_block
wb
;
wb_init
(
&
wb
,
&
temp
);
pack_from
(
L
,
&
wb
,
from
);
assert
(
wb
.
head
==
&
temp
);
void
*
buffer
=
seri
(
&
temp
,
wb
.
len
);
if
(
sz
) {
*
sz
=
wb
.
len
+
4
;
}
wb_free
(
&
wb
);
return
buffer
;
}
void
*
seri_packstring
(
const
char
*
str
,
size_t
sz
,
void
*
p
,
size_t
*
output
) {
struct
block
temp
;
temp
.
next
=
NULL
;
struct
write_block
wb
;
wb_init
(
&
wb
,
&
temp
);
if
(
sz
==
0
) {
sz
=
strlen
(
str
);
}
wb_string
(
&
wb
,
str
,
sz
);
if
(
p
) {
wb_pointer
(
&
wb
,
p
,
TYPE_USERDATA_POINTER
);
}
assert
(
wb
.
head
==
&
temp
);
void
*
buffer
=
seri
(
&
temp
,
wb
.
len
);
if
(
output
) {
*
output
=
wb
.
len
+
4
;
}
wb_free
(
&
wb
);
return
buffer
;
}
int
luaseri_unpack
(
lua_State
*
L
) {
if
(
lua_isnoneornil
(
L
,
1
)) {
return
0
;
}
lua_settop
(
L
,
1
);
return
seri_unpack_
(
L
);
}
int
luaseri_unpack_remove
(
lua_State
*
L
) {
if
(
lua_isnoneornil
(
L
,
1
)) {
return
0
;
}
return
seri_unpackptr
(
L
,
lua_touserdata
(
L
,
1
));
}
int
luaseri_pack
(
lua_State
*
L
) {
int
sz
=
0
;
void
*
buffer
=
seri_pack
(
L
,
0
,
&
sz
);
if
(
buffer
==
NULL
) {
return
luaL_error
(
L
,
"block is too large"
);
}
lua_pushlightuserdata
(
L
,
buffer
);
lua_pushinteger
(
L
,
sz
);
return
2
;
}
int
luaseri_remove
(
lua_State
*
L
) {
if
(
lua_isnoneornil
(
L
,
1
))
return
0
;
luaL_checktype
(
L
,
1
,
LUA_TLIGHTUSERDATA
);
void
*
data
=
lua_touserdata
(
L
,
1
);
size_t
sz
=
luaL_checkinteger
(
L
,
2
);
(
void
)
sz
;
free
(
data
);
return
0
;
}
#ifdef
TEST_SERI
LUAMOD_API
int
luaopen_seri
(
lua_State
*
L
) {
luaL_checkversion
(
L
);
luaL_Reg
l
[]
=
{
{
"pack"
,
luaseri_pack
},
{
"unpack"
,
luaseri_unpack
},
{
"unpack_remove"
,
luaseri_unpack_remove
},
{
NULL
,
NULL
},
};
luaL_newlib
(
L
,
l
);
return
1
;
}
#endif
Back
|
FazBrowse Home
|
New Git URL