-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLuaChunk.pas
More file actions
1514 lines (1384 loc) · 44.4 KB
/
Copy pathLuaChunk.pas
File metadata and controls
1514 lines (1384 loc) · 44.4 KB
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit LuaChunk;
{
LuaChunk.pas - Multi-version Lua binary chunk parser and Proto structures.
Parses the binary format produced by luac 5.1-5.5 into an in-memory tree of
TProto records that mirrors the internal ldump.c / ldo.c layout.
Supports Lua 5.1, 5.2, 5.3, 5.4, and 5.5 bytecode formats.
}
{$mode objfpc}{$H+}
interface
uses
SysUtils, LuaOpcodes;
const
LUA_MAGIC : array[0..3] of AnsiChar = (#$1B,'L','u','a');
LUA_VERSION = $51;
LUA_TNIL = 0;
LUA_TBOOLEAN = 1;
LUA_TNUMBER = 3;
LUA_TSTRING = 4;
{ 5.3 constant tags (variant 0 = float, variant 1 = integer) }
LUA_TNUMFLT_53 = $03; { 5.3: LUA_TNUMBER | (0 << 4) = float }
LUA_TNUMINT_53 = $13; { 5.3: LUA_TNUMBER | (1 << 4) = integer }
{ 5.4+ constant tags (swapped: variant 0 = integer, variant 1 = float) }
LUA_VNUMINT_54 = $03; { 5.4+: LUA_TNUMBER | (0 << 4) = integer }
LUA_VNUMFLT_54 = $13; { 5.4+: LUA_TNUMBER | (1 << 4) = float }
{ Aliases for backward compatibility }
LUA_TNUMINT = $13; { 5.3 integer constant tag }
LUA_TNUMFLT = $03; { 5.3 float constant tag }
{ Instruction field sizes }
SIZE_OP = 6;
SIZE_A = 8;
SIZE_B = 9;
SIZE_C = 9;
SIZE_Bx = SIZE_B + SIZE_C; { 18 }
MAXARG_A = (1 shl SIZE_A) - 1;
MAXARG_B = (1 shl SIZE_B) - 1;
MAXARG_C = (1 shl SIZE_C) - 1;
MAXARG_Bx = (1 shl SIZE_Bx) - 1;
MAXARG_sBx = MAXARG_Bx shr 1; { 131071 -- bias for signed Bx }
{ Constant bit flag in B/C: if set the argument is a constant index }
BITRK = 1 shl (SIZE_B - 1); { 256 }
{ Opcode count }
NUM_OPCODES = 38;
type
{ ------------------------------------------------------------------ }
{ Raw instruction word }
{ ------------------------------------------------------------------ }
TInstruction = LongWord; { 32-bit }
{ ------------------------------------------------------------------ }
{ Decoded instruction fields }
{ ------------------------------------------------------------------ }
TOpCode = (
OP_MOVE, { 00 }
OP_LOADK, { 01 }
OP_LOADBOOL, { 02 }
OP_LOADNIL, { 03 }
OP_GETUPVAL, { 04 }
OP_GETGLOBAL, { 05 }
OP_GETTABLE, { 06 }
OP_SETGLOBAL, { 07 }
OP_SETUPVAL, { 08 }
OP_SETTABLE, { 09 }
OP_NEWTABLE, { 10 }
OP_SELF, { 11 }
OP_ADD, { 12 }
OP_SUB, { 13 }
OP_MUL, { 14 }
OP_DIV, { 15 }
OP_MOD, { 16 }
OP_POW, { 17 }
OP_UNM, { 18 }
OP_NOT, { 19 }
OP_LEN, { 20 }
OP_CONCAT, { 21 }
OP_JMP, { 22 }
OP_EQ, { 23 }
OP_LT, { 24 }
OP_LE, { 25 }
OP_TEST, { 26 }
OP_TESTSET, { 27 }
OP_CALL, { 28 }
OP_TAILCALL, { 29 }
OP_RETURN, { 30 }
OP_FORLOOP, { 31 }
OP_FORPREP, { 32 }
OP_TFORLOOP, { 33 }
OP_SETLIST, { 34 }
OP_CLOSE, { 35 }
OP_CLOSURE, { 36 }
OP_VARARG { 37 }
);
{ Instruction encoding format }
TInstFormat = (ifABC, ifABx, ifAsBx);
{ ------------------------------------------------------------------ }
{ Constant value }
{ ------------------------------------------------------------------ }
TLuaConstKind = (lckNil, lckBoolean, lckNumber, lckString, lckInteger, lckFloat);
TLuaConst = record
Kind : TLuaConstKind;
BValue : Boolean; { lckBoolean }
NValue : Double; { lckNumber / lckFloat }
SValue : AnsiString; { lckString }
IValue : Int64; { lckInteger (5.3+) }
end;
{ ------------------------------------------------------------------ }
{ Local variable debug info }
{ ------------------------------------------------------------------ }
TLocVar = record
Name : AnsiString;
StartPC : Integer; { first instruction where var is active }
EndPC : Integer; { first instruction where var is dead }
end;
{ ------------------------------------------------------------------ }
{ Upvalue descriptor (5.2+) }
{ ------------------------------------------------------------------ }
TUpvalDesc = record
InStack : Boolean;
Idx : Byte;
Kind : Byte; { 5.4+ }
Name : AnsiString;
end;
{ ------------------------------------------------------------------ }
{ Function prototype (mirrors GCproto / Proto in lobject.h) }
{ ------------------------------------------------------------------ }
PProto = ^TProto;
TProto = record
{ identity / source }
Source : AnsiString;
LineDefined : Integer;
LastLineDefined : Integer;
{ code }
Code : array of TInstruction;
LineInfo : array of Integer; { one entry per instruction }
{ constants }
K : array of TLuaConst;
{ upvalues }
NUps : Byte;
UpvalueNames : array of AnsiString;
UpvalDescs : array of TUpvalDesc; { 5.2+ upvalue descriptors }
{ nested protos (inner functions / closures) }
P : array of PProto;
{ local variable debug info }
LocVars : array of TLocVar;
{ register / parameter info }
MaxStack : Byte;
NumParams : Byte;
IsVarArg : Byte; { bit 0 = VARARG_HASARG, bit 1 = VARARG_ISVARARG, bit 2 = VARARG_NEEDSARG }
{ version tag (set by parser) }
LuaVersion : Byte; { $51, $52, $53, $54, $55 }
end;
{ ------------------------------------------------------------------ }
{ Top-level binary chunk header }
{ ------------------------------------------------------------------ }
TChunkHeader = record
Version : Byte;
Format : Byte;
LittleEndian : Boolean;
IntSize : Byte;
SizeTSize : Byte;
InstSize : Byte;
NumberSize : Byte;
Integral : Byte;
{ 5.3+ additions }
IntegerSize : Byte; { size of lua_Integer }
FloatSize : Byte; { size of lua_Number (float) }
end;
TLuaChunk = record
Header : TChunkHeader;
Root : PProto;
OpTable : TOpcodeTable;
end;
{ ------------------------------------------------------------------ }
{ Parser }
{ ------------------------------------------------------------------ }
TChunkParser = class
private
FData : array of Byte;
FPos : Integer;
FHeader : TChunkHeader;
FSavedStrs : array of AnsiString; { 5.5 string dedup table }
FSavedCount : Integer; { number of saved strings }
procedure Error(const Msg: AnsiString);
function ReadByte: Byte;
function ReadInt: Integer;
function ReadSizeT: QWord;
function ReadInstruction: TInstruction;
function ReadNumber: Double;
function ReadString: AnsiString;
function ReadInteger: Int64; { 5.3-5.4 fixed-size lua_Integer }
function ReadIntegerVarint55: Int64; { 5.5 varint + zigzag lua_Integer }
function ReadFloat: Double; { 5.3+ lua_Number (float) }
function ReadString53: AnsiString; { 5.3+ string format with 1-byte short length }
function ReadVarint54: QWord; { 5.4 varint: last byte has bit 7 set }
function ReadVarint55: QWord; { 5.5 varint: continuation bytes have bit 7 set }
function ReadVarint: QWord; { dispatches to 54 or 55 based on version }
function ReadString54: AnsiString; { 5.4 string: varint length, no NUL }
function ReadString55: AnsiString; { 5.5 string: varint length, with NUL, dedup }
{ Version-aware helpers: dispatch based on FHeader.Version }
function ReadIntV: Integer; { ReadVarint for 5.4+, ReadInt for earlier }
function ReadStringV: AnsiString; { version-dispatched string reader }
{ Version-specific header parsing }
procedure ParseHeader;
procedure ParseHeader51;
procedure ParseHeader52;
procedure ParseHeader53;
procedure ParseHeader54;
procedure ParseHeader55;
{ Proto parsing - dispatches by version }
function ParseProto: PProto;
procedure ParseProto51(P: PProto);
procedure ParseProto52(P: PProto);
procedure ParseProto53(P: PProto);
procedure ParseProto54(P: PProto);
procedure ParseProto55(P: PProto);
{ Shared sub-parsers }
procedure ParseCode(Proto: PProto);
procedure ParseConstants(Proto: PProto); { 5.1-5.2 constants }
procedure ParseConstants53(Proto: PProto); { 5.3+ constants (lckInteger/lckFloat) }
procedure ParseUpvalues(Proto: PProto); { 5.1 nups byte }
procedure ParseUpvalDescs(Proto: PProto); { 5.2+ upvalue descriptors }
procedure ParseSubProtos(Proto: PProto);
procedure ParseLineInfo(Proto: PProto);
procedure ParseAbsLineInfo(Proto: PProto); { 5.4+ absolute line info }
procedure ParseLocVars(Proto: PProto);
procedure ParseUpvalueNames(Proto: PProto);
public
constructor Create(const AData: array of Byte; ALen: Integer);
function Parse: TLuaChunk;
end;
{ ------------------------------------------------------------------ }
{ Instruction decode helpers (module-level) }
{ ------------------------------------------------------------------ }
function GET_OPCODE(i: TInstruction): TOpCode;
function GETARG_A(i: TInstruction): Integer;
function GETARG_B(i: TInstruction): Integer;
function GETARG_C(i: TInstruction): Integer;
function GETARG_Bx(i: TInstruction): Integer;
function GETARG_sBx(i: TInstruction): Integer;
function ISK(x: Integer): Boolean; { true if x is a constant index }
function INDEXRK(x: Integer): Integer; { strip the RK bit }
function OpcodeFormat(Op: TOpCode): TInstFormat;
function OpcodeName(Op: TOpCode): AnsiString;
{ Free a proto tree }
procedure FreeProto(P: PProto);
{ Load chunk from file }
function LoadChunkFromFile(const FileName: AnsiString; out Chunk: TLuaChunk): Boolean;
implementation
{ ======================================================================
Instruction decode helpers
====================================================================== }
function GET_OPCODE(i: TInstruction): TOpCode;
begin
Result := TOpCode(i and $3F); { low 6 bits }
end;
function GETARG_A(i: TInstruction): Integer;
begin
Result := (i shr 6) and $FF; { bits 6..13 }
end;
function GETARG_C(i: TInstruction): Integer;
begin
Result := (i shr 14) and $1FF; { bits 14..22 }
end;
function GETARG_B(i: TInstruction): Integer;
begin
Result := (i shr 23) and $1FF; { bits 23..31 }
end;
function GETARG_Bx(i: TInstruction): Integer;
begin
Result := (i shr 14) and $3FFFF; { bits 14..31, 18 bits }
end;
function GETARG_sBx(i: TInstruction): Integer;
begin
Result := GETARG_Bx(i) - MAXARG_sBx;
end;
function ISK(x: Integer): Boolean;
begin
Result := (x and BITRK) <> 0;
end;
function INDEXRK(x: Integer): Integer;
begin
Result := x and (BITRK - 1);
end;
{ Instruction format table (indexed by opcode ordinal) }
const
OpFmtTable: array[TOpCode] of TInstFormat = (
ifABC, { MOVE }
ifABx, { LOADK }
ifABC, { LOADBOOL }
ifABC, { LOADNIL }
ifABC, { GETUPVAL }
ifABx, { GETGLOBAL }
ifABC, { GETTABLE }
ifABx, { SETGLOBAL }
ifABC, { SETUPVAL }
ifABC, { SETTABLE }
ifABC, { NEWTABLE }
ifABC, { SELF }
ifABC, { ADD }
ifABC, { SUB }
ifABC, { MUL }
ifABC, { DIV }
ifABC, { MOD }
ifABC, { POW }
ifABC, { UNM }
ifABC, { NOT }
ifABC, { LEN }
ifABC, { CONCAT }
ifAsBx, { JMP }
ifABC, { EQ }
ifABC, { LT }
ifABC, { LE }
ifABC, { TEST }
ifABC, { TESTSET }
ifABC, { CALL }
ifABC, { TAILCALL }
ifABC, { RETURN }
ifAsBx, { FORLOOP }
ifAsBx, { FORPREP }
ifABC, { TFORLOOP }
ifABC, { SETLIST }
ifABC, { CLOSE }
ifABx, { CLOSURE }
ifABC { VARARG }
);
const
OpcodeNames: array[TOpCode] of AnsiString = (
'MOVE','LOADK','LOADBOOL','LOADNIL','GETUPVAL','GETGLOBAL','GETTABLE',
'SETGLOBAL','SETUPVAL','SETTABLE','NEWTABLE','SELF','ADD','SUB','MUL',
'DIV','MOD','POW','UNM','NOT','LEN','CONCAT','JMP','EQ','LT','LE',
'TEST','TESTSET','CALL','TAILCALL','RETURN','FORLOOP','FORPREP',
'TFORLOOP','SETLIST','CLOSE','CLOSURE','VARARG'
);
function OpcodeFormat(Op: TOpCode): TInstFormat;
begin
Result := OpFmtTable[Op];
end;
function OpcodeName(Op: TOpCode): AnsiString;
begin
Result := OpcodeNames[Op];
end;
{ ======================================================================
FreeProto
====================================================================== }
procedure FreeProto(P: PProto);
var
I: Integer;
begin
if P = nil then Exit;
for I := 0 to High(P^.P) do
FreeProto(P^.P[I]);
SetLength(P^.Code, 0);
SetLength(P^.LineInfo, 0);
SetLength(P^.K, 0);
SetLength(P^.UpvalueNames, 0);
SetLength(P^.UpvalDescs, 0);
SetLength(P^.P, 0);
SetLength(P^.LocVars, 0);
Dispose(P);
end;
{ ======================================================================
TChunkParser
====================================================================== }
constructor TChunkParser.Create(const AData: array of Byte; ALen: Integer);
begin
inherited Create;
SetLength(FData, ALen);
if ALen > 0 then
Move(AData[0], FData[0], ALen);
FPos := 0;
FSavedCount := 0;
SetLength(FSavedStrs, 0);
end;
procedure TChunkParser.Error(const Msg: AnsiString);
begin
raise Exception.CreateFmt('LuaChunk parse error at offset %d: %s', [FPos, Msg]);
end;
function TChunkParser.ReadByte: Byte;
begin
if FPos >= Length(FData) then
Error('unexpected end of data');
Result := FData[FPos];
Inc(FPos);
end;
function TChunkParser.ReadInt: Integer;
var
B: array[0..7] of Byte;
I: Integer;
begin
if FPos + FHeader.IntSize > Length(FData) then
Error('unexpected end of data reading int');
for I := 0 to FHeader.IntSize - 1 do
B[I] := FData[FPos + I];
Inc(FPos, FHeader.IntSize);
Result := 0;
if FHeader.LittleEndian then
Move(B[0], Result, FHeader.IntSize)
else begin
{ big-endian: reverse bytes into low end of Result }
for I := 0 to FHeader.IntSize - 1 do
Result := (Result shl 8) or B[I];
end;
end;
function TChunkParser.ReadSizeT: QWord;
var
B: array[0..7] of Byte;
I: Integer;
begin
if FPos + FHeader.SizeTSize > Length(FData) then
Error('unexpected end of data reading size_t');
for I := 0 to FHeader.SizeTSize - 1 do
B[I] := FData[FPos + I];
Inc(FPos, FHeader.SizeTSize);
Result := 0;
if FHeader.LittleEndian then
Move(B[0], Result, FHeader.SizeTSize)
else
for I := 0 to FHeader.SizeTSize - 1 do
Result := (Result shl 8) or B[I];
end;
function TChunkParser.ReadInstruction: TInstruction;
var
B: array[0..3] of Byte;
I: Integer;
begin
if FHeader.InstSize <> 4 then
Error('only 4-byte instructions are supported');
if FPos + 4 > Length(FData) then
Error('unexpected end of data reading instruction');
for I := 0 to 3 do
B[I] := FData[FPos + I];
Inc(FPos, 4);
Result := 0;
if FHeader.LittleEndian then
Move(B[0], Result, 4)
else
for I := 0 to 3 do
Result := (Result shl 8) or B[I];
end;
function TChunkParser.ReadNumber: Double;
var
B: array[0..7] of Byte;
I: Integer;
N: Double;
Rev: array[0..7] of Byte;
begin
if FHeader.NumberSize > 8 then
Error('lua_Number size > 8 not supported');
if FPos + FHeader.NumberSize > Length(FData) then
Error('unexpected end of data reading number');
for I := 0 to FHeader.NumberSize - 1 do
B[I] := FData[FPos + I];
Inc(FPos, FHeader.NumberSize);
N := 0;
if FHeader.LittleEndian then
Move(B[0], N, FHeader.NumberSize)
else begin
{ reverse bytes }
for I := 0 to FHeader.NumberSize - 1 do
Rev[FHeader.NumberSize - 1 - I] := B[I];
Move(Rev[0], N, FHeader.NumberSize);
end;
Result := N;
end;
function TChunkParser.ReadString: AnsiString;
var
Len: QWord;
begin
Len := ReadSizeT;
if Len = 0 then begin
Result := '';
Exit;
end;
{ Lua includes the NUL terminator in the length }
if FPos + Integer(Len) > Length(FData) then
Error('unexpected end of data reading string');
SetLength(Result, Len - 1);
if Len > 1 then
Move(FData[FPos], Result[1], Len - 1);
Inc(FPos, Len);
end;
function TChunkParser.ReadInteger: Int64;
var
B: array[0..7] of Byte;
I: Integer;
Rev: array[0..7] of Byte;
begin
if FHeader.IntegerSize > 8 then
Error('lua_Integer size > 8 not supported');
if FPos + FHeader.IntegerSize > Length(FData) then
Error('unexpected end of data reading integer');
for I := 0 to FHeader.IntegerSize - 1 do
B[I] := FData[FPos + I];
Inc(FPos, FHeader.IntegerSize);
Result := 0;
if FHeader.LittleEndian then
Move(B[0], Result, FHeader.IntegerSize)
else begin
for I := 0 to FHeader.IntegerSize - 1 do
Rev[FHeader.IntegerSize - 1 - I] := B[I];
Move(Rev[0], Result, FHeader.IntegerSize);
end;
end;
function TChunkParser.ReadIntegerVarint55: Int64;
var
Raw: QWord;
begin
{ 5.5 encodes lua_Integer using varint + zigzag:
A non-negative x is coded as 2*x; a negative x is coded as -2*x - 1.
So: encoded = if x >= 0 then 2*x else (-2*x - 1)
Decode: if (raw and 1) = 0 then x = raw shr 1
else x = -(Int64(raw shr 1) + 1) }
Raw := ReadVarint55;
if (Raw and 1) = 0 then
Result := Int64(Raw shr 1)
else
Result := -(Int64(Raw shr 1) + 1);
end;
function TChunkParser.ReadFloat: Double;
var
B: array[0..7] of Byte;
I: Integer;
N: Double;
Rev: array[0..7] of Byte;
begin
if FHeader.FloatSize > 8 then
Error('lua_Number (float) size > 8 not supported');
if FPos + FHeader.FloatSize > Length(FData) then
Error('unexpected end of data reading float');
for I := 0 to FHeader.FloatSize - 1 do
B[I] := FData[FPos + I];
Inc(FPos, FHeader.FloatSize);
N := 0;
if FHeader.LittleEndian then
Move(B[0], N, FHeader.FloatSize)
else begin
for I := 0 to FHeader.FloatSize - 1 do
Rev[FHeader.FloatSize - 1 - I] := B[I];
Move(Rev[0], N, FHeader.FloatSize);
end;
Result := N;
end;
function TChunkParser.ReadString53: AnsiString;
var
B: Byte;
Len: QWord;
begin
{ 5.3+ string format: first byte is the length.
0 -> nil/empty string
<0xFF -> string length is (byte - 1)
0xFF -> read a size_t for the actual length }
B := ReadByte;
if B = 0 then begin
Result := '';
Exit;
end;
if B < $FF then
Len := QWord(B) - 1
else
Len := ReadSizeT - 1;
if Len = 0 then begin
Result := '';
Exit;
end;
if FPos + Integer(Len) > Length(FData) then
Error('unexpected end of data reading string53');
SetLength(Result, Len);
Move(FData[FPos], Result[1], Len);
Inc(FPos, Len);
end;
function TChunkParser.ReadIntV: Integer;
begin
if FHeader.Version >= $54 then
Result := Integer(ReadVarint)
else
Result := ReadInt;
end;
function TChunkParser.ReadStringV: AnsiString;
begin
if FHeader.Version >= $55 then
Result := ReadString55
else if FHeader.Version >= $54 then
Result := ReadString54
else if FHeader.Version >= $53 then
Result := ReadString53
else
Result := ReadString;
end;
function TChunkParser.ReadVarint54: QWord;
var
B: Byte;
begin
{ 5.4 varint encoding:
Each byte stores 7 data bits (bits 0-6).
The LAST byte (least significant) has bit 7 SET (0x80).
Earlier continuation bytes have bit 7 CLEAR.
Written big-endian (MSB first). }
Result := 0;
repeat
B := ReadByte;
Result := (Result shl 7) or QWord(B and $7F);
until (B and $80) <> 0;
end;
function TChunkParser.ReadVarint55: QWord;
var
B: Byte;
begin
{ 5.5 varint encoding (MSB Varint):
Each byte stores 7 data bits (bits 0-6).
Continuation bytes (more significant) have bit 7 SET (0x80).
The LAST byte (least significant) has bit 7 CLEAR.
Written big-endian (MSB first). }
Result := 0;
repeat
B := ReadByte;
Result := (Result shl 7) or QWord(B and $7F);
until (B and $80) = 0;
end;
function TChunkParser.ReadVarint: QWord;
begin
if FHeader.Version >= $55 then
Result := ReadVarint55
else
Result := ReadVarint54;
end;
function TChunkParser.ReadString54: AnsiString;
var
Len: QWord;
begin
{ 5.4 string format: varint-encoded length.
0 -> nil/empty string
otherwise length = varint - 1 (DumpString adds 1 to distinguish nil from empty)
String data does NOT include NUL terminator }
Len := ReadVarint;
if Len = 0 then begin
Result := '';
Exit;
end;
Dec(Len); { actual string length = encoded length - 1 }
if Len = 0 then begin
Result := '';
Exit;
end;
if FPos + Integer(Len) > Length(FData) then
Error('unexpected end of data reading string54');
SetLength(Result, Len);
Move(FData[FPos], Result[1], Len);
Inc(FPos, Len);
end;
function TChunkParser.ReadString55: AnsiString;
var
Size: QWord;
Idx: QWord;
StrLen: QWord;
begin
{ 5.5 string format with deduplication:
varint(0), varint(0) -> NULL string
varint(0), varint(idx) -> reuse saved string at index (idx)
varint(tsslen+1), data[tsslen+1] -> new string; data includes NUL terminator }
Size := ReadVarint;
if Size = 0 then begin
Idx := ReadVarint;
if Idx = 0 then begin
Result := '';
Exit;
end;
{ Reuse saved string - idx is 1-based }
if (Idx - 1) < QWord(FSavedCount) then
Result := FSavedStrs[Idx - 1]
else begin
Error(Format('string dedup index %d out of range (saved=%d)', [Idx, FSavedCount]));
Result := '';
end;
Exit;
end;
{ New string: Size = tsslen + 1 (from dumpSize(D, size+1))
Data on disk = tsslen + 1 bytes (string content + NUL terminator)
String length = tsslen = Size - 1 }
StrLen := Size - 1; { tsslen: string length without NUL }
if FPos + Integer(StrLen) + 1 > Length(FData) then
Error('unexpected end of data reading string55');
if StrLen = 0 then begin
Result := '';
Inc(FPos, 1); { skip just the NUL }
end else begin
SetLength(Result, StrLen);
Move(FData[FPos], Result[1], StrLen);
Inc(FPos, StrLen + 1); { skip string content + NUL terminator }
end;
{ Save for dedup }
if FSavedCount >= Length(FSavedStrs) then
SetLength(FSavedStrs, FSavedCount + 64);
FSavedStrs[FSavedCount] := Result;
Inc(FSavedCount);
end;
{ ======================================================================
Version-specific header parsers
====================================================================== }
procedure TChunkParser.ParseHeader51;
begin
{ 5.1: format, endian_flag, int_size, sizet_size, inst_size, number_size, integral_flag }
FHeader.Format := ReadByte;
FHeader.LittleEndian := ReadByte = 1;
FHeader.IntSize := ReadByte;
FHeader.SizeTSize := ReadByte;
FHeader.InstSize := ReadByte;
if FHeader.InstSize <> 4 then
Error('only 4-byte instructions supported');
FHeader.NumberSize := ReadByte;
FHeader.Integral := ReadByte;
{ 5.1 defaults for fields not present }
FHeader.IntegerSize := 0;
FHeader.FloatSize := 0;
end;
procedure TChunkParser.ParseHeader52;
var
I: Integer;
LuacData: array[0..5] of Byte;
begin
{ 5.2: format, endian_flag, int_size, sizet_size, inst_size, number_size,
integral_flag, LUAC_DATA (6 bytes: 0x19 0x93 0x0D 0x0A 0x1A 0x0A) }
FHeader.Format := ReadByte;
FHeader.LittleEndian := ReadByte = 1;
FHeader.IntSize := ReadByte;
FHeader.SizeTSize := ReadByte;
FHeader.InstSize := ReadByte;
if FHeader.InstSize <> 4 then
Error('only 4-byte instructions supported');
FHeader.NumberSize := ReadByte;
FHeader.Integral := ReadByte;
{ Read and verify LUAC_DATA }
for I := 0 to 5 do
LuacData[I] := ReadByte;
if (LuacData[0] <> $19) or (LuacData[1] <> $93) or
(LuacData[2] <> $0D) or (LuacData[3] <> $0A) or
(LuacData[4] <> $1A) or (LuacData[5] <> $0A) then
Error('invalid LUAC_DATA in 5.2 header');
FHeader.IntegerSize := 0;
FHeader.FloatSize := 0;
end;
procedure TChunkParser.ParseHeader53;
var
I: Integer;
LuacData: array[0..5] of Byte;
TestInt: Int64;
B: array[0..7] of Byte;
begin
{ 5.3: format, LUAC_DATA (6 bytes), int_size, sizet_size, inst_size,
integer_size, float_size, LUAC_INT (0x5678 test), LUAC_NUM (370.5 test) }
FHeader.Format := ReadByte;
{ Read and verify LUAC_DATA }
for I := 0 to 5 do
LuacData[I] := ReadByte;
if (LuacData[0] <> $19) or (LuacData[1] <> $93) or
(LuacData[2] <> $0D) or (LuacData[3] <> $0A) or
(LuacData[4] <> $1A) or (LuacData[5] <> $0A) then
Error('invalid LUAC_DATA in 5.3 header');
FHeader.IntSize := ReadByte;
FHeader.SizeTSize := ReadByte;
FHeader.InstSize := ReadByte;
if FHeader.InstSize <> 4 then
Error('only 4-byte instructions supported');
FHeader.IntegerSize := ReadByte;
FHeader.FloatSize := ReadByte;
FHeader.NumberSize := FHeader.FloatSize;
{ Read LUAC_INT test value to detect endianness }
if FHeader.IntegerSize > 8 then
Error('lua_Integer size > 8 not supported');
if FPos + FHeader.IntegerSize > Length(FData) then
Error('unexpected end of data reading LUAC_INT');
for I := 0 to FHeader.IntegerSize - 1 do
B[I] := FData[FPos + I];
{ Try little-endian first }
TestInt := 0;
Move(B[0], TestInt, FHeader.IntegerSize);
if TestInt = $5678 then
FHeader.LittleEndian := True
else begin
{ Try big-endian }
FHeader.LittleEndian := False;
TestInt := 0;
for I := 0 to FHeader.IntegerSize - 1 do
TestInt := (TestInt shl 8) or B[I];
if TestInt <> $5678 then
Error(Format('LUAC_INT test failed: got %d, expected %d', [TestInt, $5678]));
end;
Inc(FPos, FHeader.IntegerSize);
{ Read LUAC_NUM test value (370.5) }
if FHeader.FloatSize > 8 then
Error('lua_Number (float) size > 8 not supported');
if FPos + FHeader.FloatSize > Length(FData) then
Error('unexpected end of data reading LUAC_NUM');
{ Just skip the test value - endianness already determined }
Inc(FPos, FHeader.FloatSize);
FHeader.Integral := 0;
end;
procedure TChunkParser.ParseHeader54;
var
I: Integer;
LuacData: array[0..5] of Byte;
TestInt: Int64;
B: array[0..7] of Byte;
begin
{ 5.4/5.5: format, LUAC_DATA (6 bytes), inst_size, integer_size, float_size,
LUAC_INT (0x5678 test), LUAC_NUM (370.5 test) }
FHeader.Format := ReadByte;
{ Read and verify LUAC_DATA }
for I := 0 to 5 do
LuacData[I] := ReadByte;
if (LuacData[0] <> $19) or (LuacData[1] <> $93) or
(LuacData[2] <> $0D) or (LuacData[3] <> $0A) or
(LuacData[4] <> $1A) or (LuacData[5] <> $0A) then
Error('invalid LUAC_DATA in 5.4+ header');
{ 5.4+ omits int_size and sizet_size from header }
FHeader.InstSize := ReadByte;
if FHeader.InstSize <> 4 then
Error('only 4-byte instructions supported');
FHeader.IntegerSize := ReadByte;
FHeader.FloatSize := ReadByte;
FHeader.NumberSize := FHeader.FloatSize;
{ 5.4+ uses varint for sizes, so set defaults for int/sizet }
FHeader.IntSize := FHeader.IntegerSize;
FHeader.SizeTSize := FHeader.IntegerSize;
{ Read LUAC_INT test value to detect endianness }
if FHeader.IntegerSize > 8 then
Error('lua_Integer size > 8 not supported');
if FPos + FHeader.IntegerSize > Length(FData) then
Error('unexpected end of data reading LUAC_INT');
for I := 0 to FHeader.IntegerSize - 1 do
B[I] := FData[FPos + I];
{ Try little-endian first }
TestInt := 0;
Move(B[0], TestInt, FHeader.IntegerSize);
if TestInt = $5678 then
FHeader.LittleEndian := True
else begin
FHeader.LittleEndian := False;
TestInt := 0;
for I := 0 to FHeader.IntegerSize - 1 do
TestInt := (TestInt shl 8) or B[I];
if TestInt <> $5678 then
Error(Format('LUAC_INT test failed: got %d, expected %d', [TestInt, $5678]));
end;
Inc(FPos, FHeader.IntegerSize);
{ Read LUAC_NUM test value (370.5) - just skip, endianness already known }
if FHeader.FloatSize > 8 then
Error('lua_Number (float) size > 8 not supported');
if FPos + FHeader.FloatSize > Length(FData) then
Error('unexpected end of data reading LUAC_NUM');
Inc(FPos, FHeader.FloatSize);
FHeader.Integral := 0;
end;
procedure TChunkParser.ParseHeader55;
var
I: Integer;
LuacData: array[0..5] of Byte;
IntSize, InstSize, IntegerSize, FloatSize: Byte;
TestInt32: LongInt;
TestInst: LongWord;
TestInt64: Int64;
TestNum: Double;
B: array[0..7] of Byte;
Rev: array[0..7] of Byte;
begin
{ 5.5: format, LUAC_DATA (6 bytes), then 4 dumpNumInfo blocks:
each block = size_byte + test_value (size_byte bytes)
1) int: sizeof(int), LUAC_INT (-0x5678)
2) Instruction: sizeof(Instruction), LUAC_INST (0x12345678)
3) lua_Integer: sizeof(lua_Integer), LUAC_INT (-0x5678)
4) lua_Number: sizeof(lua_Number), LUAC_NUM (-370.5) }
FHeader.Format := ReadByte;
{ Read and verify LUAC_DATA }
for I := 0 to 5 do
LuacData[I] := ReadByte;
if (LuacData[0] <> $19) or (LuacData[1] <> $93) or
(LuacData[2] <> $0D) or (LuacData[3] <> $0A) or
(LuacData[4] <> $1A) or (LuacData[5] <> $0A) then
Error('invalid LUAC_DATA in 5.5 header');
{ Block 1: int }
IntSize := ReadByte;
FHeader.IntSize := IntSize;
if IntSize > 8 then Error('int size > 8 not supported');
{ Read test int value to detect endianness }
for I := 0 to IntSize - 1 do
B[I] := FData[FPos + I];
TestInt32 := 0;
Move(B[0], TestInt32, IntSize);
if TestInt32 = -$5678 then
FHeader.LittleEndian := True
else begin
FHeader.LittleEndian := False;
TestInt32 := 0;
for I := 0 to IntSize - 1 do
Rev[IntSize - 1 - I] := B[I];
Move(Rev[0], TestInt32, IntSize);
if TestInt32 <> -$5678 then
Error(Format('LUAC_INT (int) test failed in 5.5 header: got %d', [TestInt32]));
end;
Inc(FPos, IntSize);
{ Block 2: Instruction }
InstSize := ReadByte;
FHeader.InstSize := InstSize;
if InstSize <> 4 then
Error('only 4-byte instructions supported');
{ Read instruction test value }
for I := 0 to InstSize - 1 do
B[I] := FData[FPos + I];