LCOV - code coverage report
Current view: top level - src - lundump.c Coverage Total Hit
Test: Lua 5.2.4 Lines: 99.3 % 138 137
Test Date: 2024-04-28 10:23:12
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : ** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $
       3              : ** load precompiled Lua chunks
       4              : ** See Copyright Notice in lua.h
       5              : */
       6              : 
       7              : #include <string.h>
       8              : 
       9              : #define lundump_c
      10              : #define LUA_CORE
      11              : 
      12              : #include "lua.h"
      13              : 
      14              : #include "ldebug.h"
      15              : #include "ldo.h"
      16              : #include "lfunc.h"
      17              : #include "lmem.h"
      18              : #include "lobject.h"
      19              : #include "lstring.h"
      20              : #include "lundump.h"
      21              : #include "lzio.h"
      22              : 
      23              : typedef struct {
      24              :  lua_State* L;
      25              :  ZIO* Z;
      26              :  Mbuffer* b;
      27              :  const char* name;
      28              : } LoadState;
      29              : 
      30            6 : static l_noret error(LoadState* S, const char* why)
      31              : {
      32            6 :  luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
      33            6 :  luaD_throw(S->L,LUA_ERRSYNTAX);
      34              : }
      35              : 
      36              : #define LoadMem(S,b,n,size)     LoadBlock(S,b,(n)*(size))
      37              : #define LoadByte(S)             (lu_byte)LoadChar(S)
      38              : #define LoadVar(S,x)            LoadMem(S,&x,1,sizeof(x))
      39              : #define LoadVector(S,b,n,size)  LoadMem(S,b,n,size)
      40              : 
      41              : #if !defined(luai_verifycode)
      42              : #define luai_verifycode(L,b,f)  /* empty */
      43              : #endif
      44              : 
      45          436 : static void LoadBlock(LoadState* S, void* b, size_t size)
      46              : {
      47          436 :  if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
      48          435 : }
      49              : 
      50          110 : static int LoadChar(LoadState* S)
      51              : {
      52              :  char x;
      53          110 :  LoadVar(S,x);
      54          110 :  return x;
      55              : }
      56              : 
      57          150 : static int LoadInt(LoadState* S)
      58              : {
      59              :  int x;
      60          150 :  LoadVar(S,x);
      61          150 :  if (x<0) error(S,"corrupted");
      62          150 :  return x;
      63              : }
      64              : 
      65            4 : static lua_Number LoadNumber(LoadState* S)
      66              : {
      67              :  lua_Number x;
      68            4 :  LoadVar(S,x);
      69            4 :  return x;
      70              : }
      71              : 
      72           67 : static TString* LoadString(LoadState* S)
      73              : {
      74              :  size_t size;
      75           67 :  LoadVar(S,size);
      76           67 :  if (size==0)
      77            4 :   return NULL;
      78              :  else
      79              :  {
      80           63 :   char* s=luaZ_openspace(S->L,S->b,size);
      81           63 :   LoadBlock(S,s,size*sizeof(char));
      82           63 :   return luaS_newlstr(S->L,s,size-1);                /* remove trailing '\0' */
      83              :  }
      84              : }
      85              : 
      86           14 : static void LoadCode(LoadState* S, Proto* f)
      87              : {
      88           14 :  int n=LoadInt(S);
      89           14 :  f->code=luaM_newvector(S->L,n,Instruction);
      90           14 :  f->sizecode=n;
      91           14 :  LoadVector(S,f->code,n,sizeof(Instruction));
      92           14 : }
      93              : 
      94              : static void LoadFunction(LoadState* S, Proto* f);
      95              : 
      96           14 : static void LoadConstants(LoadState* S, Proto* f)
      97              : {
      98              :  int i,n;
      99           14 :  n=LoadInt(S);
     100           14 :  f->k=luaM_newvector(S->L,n,TValue);
     101           14 :  f->sizek=n;
     102           54 :  for (i=0; i<n; i++) setnilvalue(&f->k[i]);
     103           54 :  for (i=0; i<n; i++)
     104              :  {
     105           40 :   TValue* o=&f->k[i];
     106           40 :   int t=LoadChar(S);
     107           40 :   switch (t)
     108              :   {
     109            2 :    case LUA_TNIL:
     110            2 :         setnilvalue(o);
     111            2 :         break;
     112            2 :    case LUA_TBOOLEAN:
     113            2 :         setbvalue(o,LoadChar(S));
     114            2 :         break;
     115            4 :    case LUA_TNUMBER:
     116            4 :         setnvalue(o,LoadNumber(S));
     117            4 :         break;
     118           32 :    case LUA_TSTRING:
     119           32 :         setsvalue2n(S->L,o,LoadString(S));
     120           32 :         break;
     121           40 :     default: lua_assert(0);
     122              :   }
     123              :  }
     124           14 :  n=LoadInt(S);
     125           14 :  f->p=luaM_newvector(S->L,n,Proto*);
     126           14 :  f->sizep=n;
     127           20 :  for (i=0; i<n; i++) f->p[i]=NULL;
     128           20 :  for (i=0; i<n; i++)
     129              :  {
     130            6 :   f->p[i]=luaF_newproto(S->L);
     131            6 :   LoadFunction(S,f->p[i]);
     132              :  }
     133           14 : }
     134              : 
     135           14 : static void LoadUpvalues(LoadState* S, Proto* f)
     136              : {
     137              :  int i,n;
     138           14 :  n=LoadInt(S);
     139           14 :  f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
     140           14 :  f->sizeupvalues=n;
     141           27 :  for (i=0; i<n; i++) f->upvalues[i].name=NULL;
     142           27 :  for (i=0; i<n; i++)
     143              :  {
     144           13 :   f->upvalues[i].instack=LoadByte(S);
     145           13 :   f->upvalues[i].idx=LoadByte(S);
     146              :  }
     147           14 : }
     148              : 
     149           14 : static void LoadDebug(LoadState* S, Proto* f)
     150              : {
     151              :  int i,n;
     152           14 :  f->source=LoadString(S);
     153           14 :  n=LoadInt(S);
     154           14 :  f->lineinfo=luaM_newvector(S->L,n,int);
     155           14 :  f->sizelineinfo=n;
     156           14 :  LoadVector(S,f->lineinfo,n,sizeof(int));
     157           14 :  n=LoadInt(S);
     158           14 :  f->locvars=luaM_newvector(S->L,n,LocVar);
     159           14 :  f->sizelocvars=n;
     160           26 :  for (i=0; i<n; i++) f->locvars[i].varname=NULL;
     161           26 :  for (i=0; i<n; i++)
     162              :  {
     163           12 :   f->locvars[i].varname=LoadString(S);
     164           12 :   f->locvars[i].startpc=LoadInt(S);
     165           12 :   f->locvars[i].endpc=LoadInt(S);
     166              :  }
     167           14 :  n=LoadInt(S);
     168           23 :  for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
     169           14 : }
     170              : 
     171           14 : static void LoadFunction(LoadState* S, Proto* f)
     172              : {
     173           14 :  f->linedefined=LoadInt(S);
     174           14 :  f->lastlinedefined=LoadInt(S);
     175           14 :  f->numparams=LoadByte(S);
     176           14 :  f->is_vararg=LoadByte(S);
     177           14 :  f->maxstacksize=LoadByte(S);
     178           14 :  LoadCode(S,f);
     179           14 :  LoadConstants(S,f);
     180           14 :  LoadUpvalues(S,f);
     181           14 :  LoadDebug(S,f);
     182           14 : }
     183              : 
     184              : /* the code below must be consistent with the code in luaU_header */
     185              : #define N0      LUAC_HEADERSIZE
     186              : #define N1      (sizeof(LUA_SIGNATURE)-sizeof(char))
     187              : #define N2      N1+2
     188              : #define N3      N2+6
     189              : 
     190           14 : static void LoadHeader(LoadState* S)
     191              : {
     192              :  lu_byte h[LUAC_HEADERSIZE];
     193              :  lu_byte s[LUAC_HEADERSIZE];
     194           14 :  luaU_header(h);
     195           14 :  memcpy(s,h,sizeof(char));                      /* first char already read */
     196           14 :  LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
     197           13 :  if (memcmp(h,s,N0)==0) return;
     198            5 :  if (memcmp(h,s,N1)!=0) error(S,"not a");
     199            4 :  if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
     200            2 :  if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
     201              : }
     202              : 
     203              : /*
     204              : ** load precompiled chunk
     205              : */
     206           14 : Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
     207              : {
     208              :  LoadState S;
     209              :  Closure* cl;
     210           14 :  if (*name=='@' || *name=='=')
     211           13 :   S.name=name+1;
     212            1 :  else if (*name==LUA_SIGNATURE[0])
     213            1 :   S.name="binary string";
     214              :  else
     215            0 :   S.name=name;
     216           14 :  S.L=L;
     217           14 :  S.Z=Z;
     218           14 :  S.b=buff;
     219           14 :  LoadHeader(&S);
     220            8 :  cl=luaF_newLclosure(L,1);
     221            8 :  setclLvalue(L,L->top,cl); incr_top(L);
     222            8 :  cl->l.p=luaF_newproto(L);
     223            8 :  LoadFunction(&S,cl->l.p);
     224            8 :  if (cl->l.p->sizeupvalues != 1)
     225              :  {
     226            1 :   Proto* p=cl->l.p;
     227            1 :   cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
     228            1 :   cl->l.p=p;
     229            1 :   setclLvalue(L,L->top-1,cl);
     230              :  }
     231              :  luai_verifycode(L,buff,cl->l.p);
     232            8 :  return cl;
     233              : }
     234              : 
     235              : #define MYINT(s)        (s[0]-'0')
     236              : #define VERSION         MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
     237              : #define FORMAT          0               /* this is the official format */
     238              : 
     239              : /*
     240              : * make header for precompiled chunks
     241              : * if you change the code below be sure to update LoadHeader and FORMAT above
     242              : * and LUAC_HEADERSIZE in lundump.h
     243              : */
     244           24 : void luaU_header (lu_byte* h)
     245              : {
     246           24 :  int x=1;
     247           24 :  memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
     248           24 :  h+=sizeof(LUA_SIGNATURE)-sizeof(char);
     249           24 :  *h++=cast_byte(VERSION);
     250           24 :  *h++=cast_byte(FORMAT);
     251           24 :  *h++=cast_byte(*(char*)&x);                        /* endianness */
     252           24 :  *h++=cast_byte(sizeof(int));
     253           24 :  *h++=cast_byte(sizeof(size_t));
     254           24 :  *h++=cast_byte(sizeof(Instruction));
     255           24 :  *h++=cast_byte(sizeof(lua_Number));
     256           24 :  *h++=cast_byte(((lua_Number)0.5)==0);          /* is lua_Number integral? */
     257           24 :  memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
     258           24 : }
        

Generated by: LCOV version 2.0-1