123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- ** $Id: lundump.c,v 1.39 2001/02/23 17:17:25 roberto Exp roberto $
- ** load bytecodes from files
- ** See Copyright Notice in lua.h
- */
- #include <stdio.h>
- #include <string.h>
- #define LUA_PRIVATE
- #include "lua.h"
- #include "lfunc.h"
- #include "lmem.h"
- #include "lopcodes.h"
- #include "lstring.h"
- #include "lundump.h"
- #define LoadByte ezgetc
- static const l_char* ZNAME (ZIO* Z)
- {
- const l_char* s=zname(Z);
- return (*s==l_c('@')) ? s+1 : s;
- }
- static void unexpectedEOZ (lua_State* L, ZIO* Z)
- {
- luaO_verror(L,l_s("unexpected end of file in `%.99s'"),ZNAME(Z));
- }
- static int ezgetc (lua_State* L, ZIO* Z)
- {
- int c=zgetc(Z);
- if (c==EOZ) unexpectedEOZ(L,Z);
- return c;
- }
- static void ezread (lua_State* L, ZIO* Z, void* b, int n)
- {
- int r=zread(Z,b,n);
- if (r!=0) unexpectedEOZ(L,Z);
- }
- static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
- {
- if (swap)
- {
- l_char *p=(l_char *) b+size-1;
- int n=size;
- while (n--) *p--=(l_char)ezgetc(L,Z);
- }
- else
- ezread(L,Z,b,size);
- }
- static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
- {
- if (swap)
- {
- l_char *q=(l_char *) b;
- while (m--)
- {
- l_char *p=q+size-1;
- int n=size;
- while (n--) *p--=(l_char)ezgetc(L,Z);
- q+=size;
- }
- }
- else
- ezread(L,Z,b,m*size);
- }
- static int LoadInt (lua_State* L, ZIO* Z, int swap)
- {
- int x;
- LoadBlock(L,&x,sizeof(x),Z,swap);
- return x;
- }
- static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
- {
- size_t x;
- LoadBlock(L,&x,sizeof(x),Z,swap);
- return x;
- }
- static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
- {
- lua_Number x;
- LoadBlock(L,&x,sizeof(x),Z,swap);
- return x;
- }
- static TString* LoadString (lua_State* L, ZIO* Z, int swap)
- {
- size_t size=LoadSize(L,Z,swap);
- if (size==0)
- return NULL;
- else
- {
- char* s=luaO_openspace(L,size,char);
- LoadBlock(L,s,size,Z,0);
- return luaS_newlstr(L,s,size-1); /* remove trailing l_c('\0') */
- }
- }
- static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
- {
- int size=LoadInt(L,Z,swap);
- tf->code=luaM_newvector(L,size,Instruction);
- tf->sizecode=size;
- LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
- }
- static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
- {
- int i,n;
- n=LoadInt(L,Z,swap);
- tf->locvars=luaM_newvector(L,n,LocVar);
- tf->sizelocvars=n;
- for (i=0; i<n; i++)
- {
- tf->locvars[i].varname=LoadString(L,Z,swap);
- tf->locvars[i].startpc=LoadInt(L,Z,swap);
- tf->locvars[i].endpc=LoadInt(L,Z,swap);
- }
- }
- static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
- {
- int n;
- n=LoadInt(L,Z,swap);
- tf->lineinfo=luaM_newvector(L,n,int);
- tf->sizelineinfo=n;
- LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
- }
- static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
- static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
- {
- int i,n;
- n=LoadInt(L,Z,swap);
- tf->kstr=luaM_newvector(L,n,TString*);
- tf->sizekstr=n;
- for (i=0; i<n; i++)
- tf->kstr[i]=LoadString(L,Z,swap);
- n=LoadInt(L,Z,swap);
- tf->knum=luaM_newvector(L,n,lua_Number);
- tf->sizeknum=n;
- LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
- n=LoadInt(L,Z,swap);
- tf->kproto=luaM_newvector(L,n,Proto*);
- tf->sizekproto=n;
- for (i=0; i<n; i++)
- tf->kproto[i]=LoadFunction(L,Z,swap);
- }
- static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
- {
- Proto* tf=luaF_newproto(L);
- tf->source=LoadString(L,Z,swap);
- tf->lineDefined=LoadInt(L,Z,swap);
- tf->numparams=LoadInt(L,Z,swap);
- tf->is_vararg=LoadByte(L,Z);
- tf->maxstacksize=LoadInt(L,Z,swap);
- LoadLocals(L,tf,Z,swap);
- LoadLines(L,tf,Z,swap);
- LoadConstants(L,tf,Z,swap);
- LoadCode(L,tf,Z,swap);
- return tf;
- }
- static void LoadSignature (lua_State* L, ZIO* Z)
- {
- const l_char* s=l_s(SIGNATURE);
- while (*s!=0 && ezgetc(L,Z)==*s)
- ++s;
- if (*s!=0) luaO_verror(L,l_s("bad signature in `%.99s'"),ZNAME(Z));
- }
- static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z)
- {
- int r=ezgetc(L,Z);
- if (r!=s)
- luaO_verror(L,l_s("virtual machine mismatch in `%.99s':\n")
- l_s(" %.20s is %d but read %d"),ZNAME(Z),what,s,r);
- }
- #define TESTSIZE(s) TestSize(L,s,#s,Z)
- #define V(v) v/16,v%16
- static int LoadHeader (lua_State* L, ZIO* Z)
- {
- int version,swap;
- lua_Number f=0,tf=TEST_NUMBER;
- LoadSignature(L,Z);
- version=ezgetc(L,Z);
- if (version>VERSION)
- luaO_verror(L,l_s("`%.99s' too new:\n")
- l_s(" read version %d.%d; expected at most %d.%d"),
- ZNAME(Z),V(version),V(VERSION));
- if (version<VERSION0) /* check last major change */
- luaO_verror(L,l_s("`%.99s' too old:\n")
- l_s(" read version %d.%d; expected at least %d.%d"),
- ZNAME(Z),V(version),V(VERSION));
- swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
- TESTSIZE(sizeof(int));
- TESTSIZE(sizeof(size_t));
- TESTSIZE(sizeof(Instruction));
- TESTSIZE(SIZE_INSTRUCTION);
- TESTSIZE(SIZE_OP);
- TESTSIZE(SIZE_B);
- TESTSIZE(sizeof(lua_Number));
- f=LoadNumber(L,Z,swap);
- if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
- luaO_verror(L,l_s("unknown number format in `%.99s':\n")
- l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT),
- ZNAME(Z),f,tf);
- return swap;
- }
- static Proto* LoadChunk (lua_State* L, ZIO* Z)
- {
- return LoadFunction(L,Z,LoadHeader(L,Z));
- }
- /*
- ** load one chunk from a file or buffer
- ** return main if ok and NULL at EOF
- */
- Proto* luaU_undump (lua_State* L, ZIO* Z)
- {
- Proto* tf=NULL;
- int c=zgetc(Z);
- if (c==ID_CHUNK)
- tf=LoadChunk(L,Z);
- c=zgetc(Z);
- if (c!=EOZ)
- luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
- return tf;
- }
- /*
- ** find byte order
- */
- int luaU_endianess (void)
- {
- int x=1;
- return *(l_char*)&x;
- }
|