lundump.c 5.6 KB

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