lundump.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. ** $Id: lundump.c,v 2.39 2014/06/18 18:35:43 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, TString *psource);
  80. static void LoadConstants (LoadState *S, Proto *f) {
  81. int i;
  82. int 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. }
  112. static void LoadProtos (LoadState *S, Proto *f) {
  113. int i;
  114. int n = LoadInt(S);
  115. f->p = luaM_newvector(S->L, n, Proto *);
  116. f->sizep = n;
  117. for (i = 0; i < n; i++)
  118. f->p[i] = NULL;
  119. for (i = 0; i < n; i++) {
  120. f->p[i] = luaF_newproto(S->L);
  121. LoadFunction(S, f->p[i], f->source);
  122. }
  123. }
  124. static void LoadUpvalues (LoadState *S, Proto *f) {
  125. int i, n;
  126. n = LoadInt(S);
  127. f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
  128. f->sizeupvalues = n;
  129. for (i = 0; i < n; i++)
  130. f->upvalues[i].name = NULL;
  131. for (i = 0; i < n; i++) {
  132. f->upvalues[i].instack = LoadByte(S);
  133. f->upvalues[i].idx = LoadByte(S);
  134. }
  135. }
  136. static void LoadDebug (LoadState *S, Proto *f) {
  137. int i, n;
  138. n = LoadInt(S);
  139. f->lineinfo = luaM_newvector(S->L, n, int);
  140. f->sizelineinfo = n;
  141. LoadVector(S, f->lineinfo, n);
  142. n = LoadInt(S);
  143. f->locvars = luaM_newvector(S->L, n, LocVar);
  144. f->sizelocvars = n;
  145. for (i = 0; i < n; i++)
  146. f->locvars[i].varname = NULL;
  147. for (i = 0; i < n; i++) {
  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++)
  154. f->upvalues[i].name = LoadString(S);
  155. }
  156. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  157. f->source = LoadString(S);
  158. if (f->source == NULL) /* no source in dump? */
  159. f->source = psource; /* reuse parent's source */
  160. f->linedefined = LoadInt(S);
  161. f->lastlinedefined = LoadInt(S);
  162. f->numparams = LoadByte(S);
  163. f->is_vararg = LoadByte(S);
  164. f->maxstacksize = LoadByte(S);
  165. LoadCode(S, f);
  166. LoadConstants(S, f);
  167. LoadUpvalues(S, f);
  168. LoadProtos(S, f);
  169. LoadDebug(S, f);
  170. }
  171. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  172. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  173. size_t len = strlen(s);
  174. LoadVector(S, buff, len);
  175. if (memcmp(s, buff, len) != 0)
  176. error(S, msg);
  177. }
  178. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  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. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  185. if (LoadByte(S) != LUAC_VERSION)
  186. error(S, "version mismatch in");
  187. if (LoadByte(S) != LUAC_FORMAT)
  188. error(S, "format mismatch in");
  189. checkliteral(S, LUAC_DATA, "corrupted");
  190. checksize(S, int);
  191. checksize(S, size_t);
  192. checksize(S, Instruction);
  193. checksize(S, lua_Integer);
  194. checksize(S, lua_Number);
  195. if (LoadInteger(S) != LUAC_INT)
  196. error(S, "endianness mismatch in");
  197. if (LoadNumber(S) != LUAC_NUM)
  198. error(S, "float format mismatch in");
  199. }
  200. /*
  201. ** load precompiled chunk
  202. */
  203. LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
  204. const char *name) {
  205. LoadState S;
  206. LClosure *cl;
  207. if (*name == '@' || *name == '=')
  208. S.name = name + 1;
  209. else if (*name == LUA_SIGNATURE[0])
  210. S.name = "binary string";
  211. else
  212. S.name = name;
  213. S.L = L;
  214. S.Z = Z;
  215. S.b = buff;
  216. checkHeader(&S);
  217. cl = luaF_newLclosure(L, LoadByte(&S));
  218. setclLvalue(L, L->top, cl);
  219. incr_top(L);
  220. cl->p = luaF_newproto(L);
  221. LoadFunction(&S, cl->p, NULL);
  222. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  223. luai_verifycode(L, buff, cl->p);
  224. return cl;
  225. }