lundump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $
  3. ** load precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define lundump_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstring.h"
  16. #include "lundump.h"
  17. #include "lzio.h"
  18. typedef struct {
  19. lua_State* L;
  20. ZIO* Z;
  21. Mbuffer* b;
  22. const char* name;
  23. } LoadState;
  24. static void error(LoadState* S, const char* why)
  25. {
  26. luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
  27. luaD_throw(S->L,LUA_ERRSYNTAX);
  28. }
  29. #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
  30. #define LoadByte(S) (lu_byte)LoadChar(S)
  31. #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
  32. #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
  33. static void LoadBlock(LoadState* S, void* b, size_t size)
  34. {
  35. if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
  36. }
  37. static int LoadChar(LoadState* S)
  38. {
  39. char x;
  40. LoadVar(S,x);
  41. return x;
  42. }
  43. static int LoadInt(LoadState* S)
  44. {
  45. int x;
  46. LoadVar(S,x);
  47. return x;
  48. }
  49. static lua_Number LoadNumber(LoadState* S)
  50. {
  51. lua_Number x;
  52. LoadVar(S,x);
  53. return x;
  54. }
  55. static TString* LoadString(LoadState* S)
  56. {
  57. size_t size;
  58. LoadVar(S,size);
  59. if (size==0)
  60. return NULL;
  61. else
  62. {
  63. char* s=luaZ_openspace(S->L,S->b,size);
  64. LoadBlock(S,s,size);
  65. return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  66. }
  67. }
  68. static void LoadCode(LoadState* S, Proto* f)
  69. {
  70. int n=LoadInt(S);
  71. f->code=luaM_newvector(S->L,n,Instruction);
  72. f->sizecode=n;
  73. LoadVector(S,f->code,n,sizeof(Instruction));
  74. }
  75. static Proto* LoadFunction(LoadState* S);
  76. static void LoadConstants(LoadState* S, Proto* f)
  77. {
  78. int i,n;
  79. n=LoadInt(S);
  80. f->k=luaM_newvector(S->L,n,TValue);
  81. f->sizek=n;
  82. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  83. for (i=0; i<n; i++)
  84. {
  85. TValue* o=&f->k[i];
  86. int t=LoadChar(S);
  87. switch (t)
  88. {
  89. case LUA_TNIL:
  90. setnilvalue(o);
  91. break;
  92. case LUA_TBOOLEAN:
  93. setbvalue(o,LoadChar(S));
  94. break;
  95. case LUA_TNUMBER:
  96. setnvalue(o,LoadNumber(S));
  97. break;
  98. case LUA_TSTRING:
  99. setsvalue2n(S->L,o,LoadString(S));
  100. break;
  101. }
  102. }
  103. n=LoadInt(S);
  104. f->p=luaM_newvector(S->L,n,Proto*);
  105. f->sizep=n;
  106. for (i=0; i<n; i++) f->p[i]=NULL;
  107. for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
  108. }
  109. static void LoadUpvalues(LoadState* S, Proto* f)
  110. {
  111. int i,n;
  112. n=LoadInt(S);
  113. f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
  114. f->sizeupvalues=n;
  115. for (i=0; i<n; i++) f->upvalues[i].name=NULL;
  116. for (i=0; i<n; i++)
  117. {
  118. f->upvalues[i].instack=LoadChar(S);
  119. f->upvalues[i].idx=LoadChar(S);
  120. }
  121. }
  122. static void LoadDebug(LoadState* S, Proto* f)
  123. {
  124. int i,n;
  125. f->source=LoadString(S);
  126. n=LoadInt(S);
  127. f->lineinfo=luaM_newvector(S->L,n,int);
  128. f->sizelineinfo=n;
  129. LoadVector(S,f->lineinfo,n,sizeof(int));
  130. n=LoadInt(S);
  131. f->locvars=luaM_newvector(S->L,n,LocVar);
  132. f->sizelocvars=n;
  133. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  134. for (i=0; i<n; i++)
  135. {
  136. f->locvars[i].varname=LoadString(S);
  137. f->locvars[i].startpc=LoadInt(S);
  138. f->locvars[i].endpc=LoadInt(S);
  139. }
  140. n=LoadInt(S);
  141. for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
  142. }
  143. static Proto* LoadFunction(LoadState* S)
  144. {
  145. Proto* f=luaF_newproto(S->L);
  146. setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
  147. f->linedefined=LoadInt(S);
  148. f->lastlinedefined=LoadInt(S);
  149. f->numparams=LoadByte(S);
  150. f->is_vararg=LoadByte(S);
  151. f->maxstacksize=LoadByte(S);
  152. LoadCode(S,f);
  153. LoadConstants(S,f);
  154. LoadUpvalues(S,f);
  155. LoadDebug(S,f);
  156. S->L->top--;
  157. return f;
  158. }
  159. static void LoadHeader(LoadState* S)
  160. {
  161. char h[LUAC_HEADERSIZE];
  162. char s[LUAC_HEADERSIZE];
  163. luaU_header(h);
  164. LoadBlock(S,s,LUAC_HEADERSIZE);
  165. if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
  166. }
  167. /*
  168. ** load precompiled chunk
  169. */
  170. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  171. {
  172. LoadState S;
  173. if (*name=='@' || *name=='=')
  174. S.name=name+1;
  175. else if (*name==LUA_SIGNATURE[0])
  176. S.name="binary string";
  177. else
  178. S.name=name;
  179. S.L=L;
  180. S.Z=Z;
  181. S.b=buff;
  182. LoadHeader(&S);
  183. return LoadFunction(&S);
  184. }
  185. /*
  186. * make header
  187. * if you make any changes in the header or in LUA_SIGNATURE,
  188. * be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
  189. */
  190. void luaU_header (char* h)
  191. {
  192. int x=1;
  193. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
  194. h+=sizeof(LUA_SIGNATURE)-1;
  195. *h++=(char)LUAC_VERSION;
  196. *h++=(char)LUAC_FORMAT;
  197. *h++=(char)*(char*)&x; /* endianness */
  198. *h++=(char)sizeof(int);
  199. *h++=(char)sizeof(size_t);
  200. *h++=(char)sizeof(Instruction);
  201. *h++=(char)sizeof(lua_Number);
  202. *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  203. }