123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /*
- ** $Id: lundump.c,v 1.18 1999/04/09 03:10:40 lhf Exp lhf $
- ** load bytecodes from files
- ** See Copyright Notice in lua.h
- */
- #include <stdio.h>
- #include <string.h>
- #include "lauxlib.h"
- #include "lfunc.h"
- #include "lmem.h"
- #include "lopcodes.h"
- #include "lstring.h"
- #include "lundump.h"
- #define LoadBlock(b,size,Z) ezread(Z,b,size)
- #if LUAC_NATIVE
- #define doLoadNumber(x,Z) LoadBlock(&x,sizeof(x),Z)
- #else
- #define doLoadNumber(x,Z) x=LoadNumber(Z)
- #endif
- static void unexpectedEOZ (ZIO* Z)
- {
- luaL_verror("unexpected end of file in %s",zname(Z));
- }
- static int ezgetc (ZIO* Z)
- {
- int c=zgetc(Z);
- if (c==EOZ) unexpectedEOZ(Z);
- return c;
- }
- static void ezread (ZIO* Z, void* b, int n)
- {
- int r=zread(Z,b,n);
- if (r!=0) unexpectedEOZ(Z);
- }
- static unsigned int LoadWord (ZIO* Z)
- {
- unsigned int hi=ezgetc(Z);
- unsigned int lo=ezgetc(Z);
- return (hi<<8)|lo;
- }
- static unsigned long LoadLong (ZIO* Z)
- {
- unsigned long hi=LoadWord(Z);
- unsigned long lo=LoadWord(Z);
- return (hi<<16)|lo;
- }
- static real LoadNumber (ZIO* Z)
- {
- char b[256];
- int size=ezgetc(Z);
- LoadBlock(b,size,Z);
- b[size]=0;
- if (b[0]=='-')
- return -luaO_str2d(b+1);
- else
- return luaO_str2d(b);
- }
- static int LoadInt (ZIO* Z, char* message)
- {
- unsigned long l=LoadLong(Z);
- unsigned int i=l;
- if (i!=l) luaL_verror(message,l,zname(Z));
- return i;
- }
- #define PAD 5 /* two word operands plus opcode */
- static Byte* LoadCode (ZIO* Z)
- {
- int size=LoadInt(Z,"code too long (%ld bytes) in %s");
- Byte* b=luaM_malloc(size+PAD);
- LoadBlock(b,size,Z);
- if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
- memset(b+size,ENDCODE,PAD); /* pad code for safety */
- return b;
- }
- static TaggedString* LoadTString (ZIO* Z)
- {
- long size=LoadLong(Z);
- if (size==0)
- return NULL;
- else
- {
- char* s=luaL_openspace(size);
- LoadBlock(s,size,Z);
- return luaS_newlstr(s,size-1);
- }
- }
- static void LoadLocals (TProtoFunc* tf, ZIO* Z)
- {
- int i,n=LoadInt(Z,"too many locals (%ld) in %s");
- if (n==0) return;
- tf->locvars=luaM_newvector(n+1,LocVar);
- for (i=0; i<n; i++)
- {
- tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
- tf->locvars[i].varname=LoadTString(Z);
- }
- tf->locvars[i].line=-1; /* flag end of vector */
- tf->locvars[i].varname=NULL;
- }
- static TProtoFunc* LoadFunction (ZIO* Z);
- static void LoadConstants (TProtoFunc* tf, ZIO* Z)
- {
- int i,n=LoadInt(Z,"too many constants (%ld) in %s");
- tf->nconsts=n;
- if (n==0) return;
- tf->consts=luaM_newvector(n,TObject);
- for (i=0; i<n; i++)
- {
- TObject* o=tf->consts+i;
- ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */
- switch (ttype(o))
- {
- case LUA_T_NUMBER:
- doLoadNumber(nvalue(o),Z);
- break;
- case LUA_T_STRING:
- tsvalue(o)=LoadTString(Z);
- break;
- case LUA_T_PROTO:
- tfvalue(o)=LoadFunction(Z);
- break;
- case LUA_T_NIL:
- break;
- default: /* cannot happen */
- luaU_badconstant("load",i,o,tf);
- break;
- }
- }
- }
- static TProtoFunc* LoadFunction (ZIO* Z)
- {
- TProtoFunc* tf=luaF_newproto();
- tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
- tf->source=LoadTString(Z);
- if (tf->source==NULL) tf->source=luaS_new(zname(Z));
- tf->code=LoadCode(Z);
- LoadLocals(tf,Z);
- LoadConstants(tf,Z);
- return tf;
- }
- static void LoadSignature (ZIO* Z)
- {
- char* s=SIGNATURE;
- while (*s!=0 && ezgetc(Z)==*s)
- ++s;
- if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
- }
- static void LoadHeader (ZIO* Z)
- {
- int version,sizeofR;
- LoadSignature(Z);
- version=ezgetc(Z);
- if (version>VERSION)
- luaL_verror(
- "%s too new: version=0x%02x; expected at most 0x%02x",
- zname(Z),version,VERSION);
- if (version<VERSION0) /* check last major change */
- luaL_verror(
- "%s too old: version=0x%02x; expected at least 0x%02x",
- zname(Z),version,VERSION0);
- sizeofR=ezgetc(Z); /* test number representation */
- #if LUAC_NATIVE
- if (sizeofR==0)
- luaL_verror("cannot read numbers in %s: "
- "support for decimal format not enabled",
- zname(Z));
- if (sizeofR!=sizeof(real))
- luaL_verror("unknown number size in %s: read %d; expected %d",
- zname(Z),sizeofR,sizeof(real));
- else
- {
- real f=-TEST_NUMBER,tf=TEST_NUMBER;
- doLoadNumber(f,Z);
- if (f!=tf)
- luaL_verror("unknown number representation in %s: "
- "read " NUMBER_FMT "; expected " NUMBER_FMT,
- zname(Z),f,tf);
- }
- #else
- if (sizeofR!=0)
- luaL_verror("cannot read numbers in %s: "
- "support for native format not enabled",
- zname(Z));
- #endif
- }
- static TProtoFunc* LoadChunk (ZIO* Z)
- {
- LoadHeader(Z);
- return LoadFunction(Z);
- }
- /*
- ** load one chunk from a file or buffer
- ** return main if ok and NULL at EOF
- */
- TProtoFunc* luaU_undump1 (ZIO* Z)
- {
- int c=zgetc(Z);
- if (c==ID_CHUNK)
- return LoadChunk(Z);
- else if (c!=EOZ)
- luaL_verror("%s is not a Lua binary file",zname(Z));
- return NULL;
- }
- /*
- * handle constants that cannot happen
- */
- void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf)
- {
- int t=ttype(o);
- char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
- luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
- }
|