Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

263 рядки
5.6 KiB

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