lundump.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** $Id: lundump.c,v 2.28 2014/02/28 12:25:12 roberto Exp roberto $
  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 l_noret 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 LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
  30. #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
  31. #if !defined(luai_verifycode)
  32. #define luai_verifycode(L,b,f) /* empty */
  33. #endif
  34. static void LoadBlock(LoadState* S, void* b, size_t size)
  35. {
  36. if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
  37. }
  38. static lu_byte LoadByte(LoadState* S)
  39. {
  40. lu_byte x;
  41. LoadVar(S,x);
  42. return x;
  43. }
  44. static int LoadInt(LoadState* S)
  45. {
  46. int x;
  47. LoadVar(S,x);
  48. if (x<0) error(S,"corrupted");
  49. return x;
  50. }
  51. static lua_Number LoadNumber(LoadState* S)
  52. {
  53. lua_Number x;
  54. LoadVar(S,x);
  55. return x;
  56. }
  57. static lua_Integer LoadInteger(LoadState* S)
  58. {
  59. lua_Integer x;
  60. LoadVar(S,x);
  61. return x;
  62. }
  63. static TString* LoadString(LoadState* S)
  64. {
  65. size_t size;
  66. LoadVar(S,size);
  67. if (size==0)
  68. return NULL;
  69. else
  70. {
  71. char* s=luaZ_openspace(S->L,S->b,size);
  72. LoadVector(S,s,size);
  73. return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  74. }
  75. }
  76. static void LoadCode(LoadState* S, Proto* f)
  77. {
  78. int n=LoadInt(S);
  79. f->code=luaM_newvector(S->L,n,Instruction);
  80. f->sizecode=n;
  81. LoadVector(S,f->code,n);
  82. }
  83. static void LoadFunction(LoadState* S, Proto* f);
  84. static void LoadConstants(LoadState* S, Proto* f)
  85. {
  86. int i,n;
  87. n=LoadInt(S);
  88. f->k=luaM_newvector(S->L,n,TValue);
  89. f->sizek=n;
  90. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  91. for (i=0; i<n; i++)
  92. {
  93. TValue* o=&f->k[i];
  94. int t=LoadByte(S);
  95. switch (t)
  96. {
  97. case LUA_TNIL:
  98. setnilvalue(o);
  99. break;
  100. case LUA_TBOOLEAN:
  101. setbvalue(o,LoadByte(S));
  102. break;
  103. case LUA_TNUMFLT:
  104. setnvalue(o,LoadNumber(S));
  105. break;
  106. case LUA_TNUMINT:
  107. setivalue(o,LoadInteger(S));
  108. break;
  109. case LUA_TSHRSTR: case LUA_TLNGSTR:
  110. setsvalue2n(S->L,o,LoadString(S));
  111. break;
  112. default: lua_assert(0);
  113. }
  114. }
  115. n=LoadInt(S);
  116. f->p=luaM_newvector(S->L,n,Proto*);
  117. f->sizep=n;
  118. for (i=0; i<n; i++) f->p[i]=NULL;
  119. for (i=0; i<n; i++)
  120. {
  121. f->p[i]=luaF_newproto(S->L);
  122. LoadFunction(S,f->p[i]);
  123. }
  124. }
  125. static void LoadUpvalues(LoadState* S, Proto* f)
  126. {
  127. int i,n;
  128. n=LoadInt(S);
  129. f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
  130. f->sizeupvalues=n;
  131. for (i=0; i<n; i++) f->upvalues[i].name=NULL;
  132. for (i=0; i<n; i++)
  133. {
  134. f->upvalues[i].instack=LoadByte(S);
  135. f->upvalues[i].idx=LoadByte(S);
  136. }
  137. }
  138. static void LoadDebug(LoadState* S, Proto* f)
  139. {
  140. int i,n;
  141. f->source=LoadString(S);
  142. n=LoadInt(S);
  143. f->lineinfo=luaM_newvector(S->L,n,int);
  144. f->sizelineinfo=n;
  145. LoadVector(S,f->lineinfo,n);
  146. n=LoadInt(S);
  147. f->locvars=luaM_newvector(S->L,n,LocVar);
  148. f->sizelocvars=n;
  149. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  150. for (i=0; i<n; i++)
  151. {
  152. f->locvars[i].varname=LoadString(S);
  153. f->locvars[i].startpc=LoadInt(S);
  154. f->locvars[i].endpc=LoadInt(S);
  155. }
  156. n=LoadInt(S);
  157. for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
  158. }
  159. static void LoadFunction(LoadState* S, Proto* f)
  160. {
  161. f->linedefined=LoadInt(S);
  162. f->lastlinedefined=LoadInt(S);
  163. f->numparams=LoadByte(S);
  164. f->is_vararg=LoadByte(S);
  165. f->maxstacksize=LoadByte(S);
  166. LoadCode(S,f);
  167. LoadConstants(S,f);
  168. LoadUpvalues(S,f);
  169. LoadDebug(S,f);
  170. }
  171. static void checkstring(LoadState *S, const char *s, const char *msg)
  172. {
  173. char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
  174. LoadVector(S,buff,strlen(s)+1);
  175. if (strcmp(s,buff)!=0) error(S,msg);
  176. }
  177. static void fchecksize(LoadState *S, size_t size, const char *tname)
  178. {
  179. if (LoadByte(S) != size)
  180. error(S,luaO_pushfstring(S->L,"%s size mismatch in",tname));
  181. }
  182. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  183. static void checkHeader(LoadState* S)
  184. {
  185. checkstring(S,LUA_SIGNATURE+1,"not a");
  186. checkstring(S,LUAC_DATA,"corrupted");
  187. if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in");
  188. if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in");
  189. checksize(S,int);
  190. checksize(S,size_t);
  191. checksize(S,Instruction);
  192. checksize(S,lua_Integer);
  193. checksize(S,lua_Number);
  194. if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in");
  195. if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in");
  196. }
  197. /*
  198. ** load precompiled chunk
  199. */
  200. Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  201. {
  202. LoadState S;
  203. Closure* cl;
  204. if (*name=='@' || *name=='=')
  205. S.name=name+1;
  206. else if (*name==LUA_SIGNATURE[0])
  207. S.name="binary string";
  208. else
  209. S.name=name;
  210. S.L=L;
  211. S.Z=Z;
  212. S.b=buff;
  213. checkHeader(&S);
  214. cl=luaF_newLclosure(L,LoadByte(&S));
  215. setclLvalue(L,L->top,cl); incr_top(L);
  216. cl->l.p=luaF_newproto(L);
  217. LoadFunction(&S,cl->l.p);
  218. lua_assert(cl->l.nupvalues==cl->l.p->sizeupvalues);
  219. luai_verifycode(L,buff,cl->l.p);
  220. return cl;
  221. }