blob: 8dfef85d0d7e654dd89835d77280958f1457ba08 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001--- a/src/ldump.c
2+++ b/src/ldump.c
3@@ -67,12 +67,12 @@ static void DumpString(const TString* s,
4 {
5 if (s==NULL || getstr(s)==NULL)
6 {
7- size_t size=0;
8+ unsigned int size=0;
9 DumpVar(size,D);
10 }
11 else
12 {
13- size_t size=s->tsv.len+1; /* include trailing '\0' */
14+ unsigned int size=s->tsv.len+1; /* include trailing '\0' */
15 DumpVar(size,D);
16 DumpBlock(getstr(s),size,D);
17 }
18--- a/src/lundump.c
19+++ b/src/lundump.c
20@@ -25,6 +25,7 @@ typedef struct {
21 ZIO* Z;
22 Mbuffer* b;
23 const char* name;
24+ int swap;
25 } LoadState;
26
27 #ifdef LUAC_TRUST_BINARIES
28@@ -40,7 +41,6 @@ static void error(LoadState* S, const ch
29 }
30 #endif
31
32-#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
33 #define LoadByte(S) (lu_byte)LoadChar(S)
34 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
35 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
36@@ -51,6 +51,49 @@ static void LoadBlock(LoadState* S, void
37 IF (r!=0, "unexpected end");
38 }
39
40+static void LoadMem (LoadState* S, void* b, int n, size_t size)
41+{
42+ LoadBlock(S,b,n*size);
43+ if (S->swap)
44+ {
45+ char* p=(char*) b;
46+ char c;
47+ switch (size)
48+ {
49+ case 1:
50+ break;
51+ case 2:
52+ while (n--)
53+ {
54+ c=p[0]; p[0]=p[1]; p[1]=c;
55+ p+=2;
56+ }
57+ break;
58+ case 4:
59+ while (n--)
60+ {
61+ c=p[0]; p[0]=p[3]; p[3]=c;
62+ c=p[1]; p[1]=p[2]; p[2]=c;
63+ p+=4;
64+ }
65+ break;
66+ case 8:
67+ while (n--)
68+ {
69+ c=p[0]; p[0]=p[7]; p[7]=c;
70+ c=p[1]; p[1]=p[6]; p[6]=c;
71+ c=p[2]; p[2]=p[5]; p[5]=c;
72+ c=p[3]; p[3]=p[4]; p[4]=c;
73+ p+=8;
74+ }
75+ break;
76+ default:
77+ IF(1, "bad size");
78+ break;
79+ }
80+ }
81+}
82+
83 static int LoadChar(LoadState* S)
84 {
85 char x;
86@@ -82,7 +125,7 @@ static lua_Integer LoadInteger(LoadState
87
88 static TString* LoadString(LoadState* S)
89 {
90- size_t size;
91+ unsigned int size;
92 LoadVar(S,size);
93 if (size==0)
94 return NULL;
95@@ -196,6 +239,7 @@ static void LoadHeader(LoadState* S)
96 char s[LUAC_HEADERSIZE];
97 luaU_header(h);
98 LoadBlock(S,s,LUAC_HEADERSIZE);
99+ S->swap=(s[6]!=h[6]); s[6]=h[6];
100 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
101 }
102
103@@ -230,7 +274,7 @@ void luaU_header (char* h)
104 *h++=(char)LUAC_FORMAT;
105 *h++=(char)*(char*)&x; /* endianness */
106 *h++=(char)sizeof(int);
107- *h++=(char)sizeof(size_t);
108+ *h++=(char)sizeof(unsigned int);
109 *h++=(char)sizeof(Instruction);
110 *h++=(char)sizeof(lua_Number);
111