summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Noonan <steven@uplinklabs.net>2009-09-25 14:21:17 -0700
committerSteven Noonan <steven@uplinklabs.net>2009-09-25 14:32:30 -0700
commit40e7c2143143a5025a58736d9c8a0d35fdff6b94 (patch)
tree0884f23cab1fae5b3701086a3f83e11b168cd24d
parentcfcf2268d4220c9f438422a29a7538268d889609 (diff)
downloadcrawl-ref-40e7c2143143a5025a58736d9c8a0d35fdff6b94.tar.gz
crawl-ref-40e7c2143143a5025a58736d9c8a0d35fdff6b94.zip
Lua: load/save architecture-independent bytecode
If you run the Intel version of Crawl and then the PowerPC version, you will get "Lua error: global_prelude: bad header in precompiled chunk" seven times during the start of Crawl. This patch corrects the issue by making the bytecode architecture independent. Signed-off-by: Steven Noonan <steven@uplinklabs.net>
-rw-r--r--crawl-ref/source/util/lua/src/ldump.c4
-rw-r--r--crawl-ref/source/util/lua/src/lundump.c50
2 files changed, 49 insertions, 5 deletions
diff --git a/crawl-ref/source/util/lua/src/ldump.c b/crawl-ref/source/util/lua/src/ldump.c
index f08277d3ac..2adc54ff82 100644
--- a/crawl-ref/source/util/lua/src/ldump.c
+++ b/crawl-ref/source/util/lua/src/ldump.c
@@ -62,12 +62,12 @@ static void DumpString(const TString* s, DumpState* D)
{
if (s==NULL || getstr(s)==NULL)
{
- size_t size=0;
+ unsigned int size=0;
DumpVar(size,D);
}
else
{
- size_t size=s->tsv.len+1; /* include trailing '\0' */
+ unsigned int size=s->tsv.len+1; /* include trailing '\0' */
DumpVar(size,D);
DumpBlock(getstr(s),size,D);
}
diff --git a/crawl-ref/source/util/lua/src/lundump.c b/crawl-ref/source/util/lua/src/lundump.c
index 7fc635eeb7..3ee5e9cab2 100644
--- a/crawl-ref/source/util/lua/src/lundump.c
+++ b/crawl-ref/source/util/lua/src/lundump.c
@@ -25,6 +25,7 @@ typedef struct {
ZIO* Z;
Mbuffer* b;
const char* name;
+ int swap;
} LoadState;
#ifdef LUAC_TRUST_BINARIES
@@ -39,7 +40,6 @@ static void error(LoadState* S, const char* why)
}
#endif
-#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
#define LoadByte(S) (lu_byte)LoadChar(S)
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
@@ -50,6 +50,49 @@ static void LoadBlock(LoadState* S, void* b, size_t size)
IF (r!=0, "unexpected end");
}
+static void LoadMem (LoadState* S, void* b, int n, size_t size)
+{
+ LoadBlock(S,b,n*size);
+ if (S->swap)
+ {
+ char* p=(char*) b;
+ char c;
+ switch (size)
+ {
+ case 1:
+ break;
+ case 2:
+ while (n--)
+ {
+ c=p[0]; p[0]=p[1]; p[1]=c;
+ p+=2;
+ }
+ break;
+ case 4:
+ while (n--)
+ {
+ c=p[0]; p[0]=p[3]; p[3]=c;
+ c=p[1]; p[1]=p[2]; p[2]=c;
+ p+=4;
+ }
+ break;
+ case 8:
+ while (n--)
+ {
+ c=p[0]; p[0]=p[7]; p[7]=c;
+ c=p[1]; p[1]=p[6]; p[6]=c;
+ c=p[2]; p[2]=p[5]; p[5]=c;
+ c=p[3]; p[3]=p[4]; p[4]=c;
+ p+=8;
+ }
+ break;
+ default:
+ IF(1, "bad size");
+ break;
+ }
+ }
+}
+
static int LoadChar(LoadState* S)
{
char x;
@@ -74,7 +117,7 @@ static lua_Number LoadNumber(LoadState* S)
static TString* LoadString(LoadState* S)
{
- size_t size;
+ unsigned int size;
LoadVar(S,size);
if (size==0)
return NULL;
@@ -182,6 +225,7 @@ static void LoadHeader(LoadState* S)
char s[LUAC_HEADERSIZE];
luaU_header(h);
LoadBlock(S,s,LUAC_HEADERSIZE);
+ S->swap=(s[6]!=h[6]); s[6]=h[6];
IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
}
@@ -216,7 +260,7 @@ void luaU_header (char* h)
*h++=(char)LUAC_FORMAT;
*h++=(char)*(char*)&x; /* endianness */
*h++=(char)sizeof(int);
- *h++=(char)sizeof(size_t);
+ *h++=(char)sizeof(unsigned int);
*h++=(char)sizeof(Instruction);
*h++=(char)sizeof(lua_Number);
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */