lundump.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $
  3. ** load precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lundump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstring.h"
  17. #include "lundump.h"
  18. #include "lzio.h"
  19. #if !defined(luai_verifycode)
  20. #define luai_verifycode(L,b,f) /* empty */
  21. #endif
  22. typedef struct {
  23. lua_State *L;
  24. ZIO *Z;
  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 if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
  68. char buff[LUAI_MAXSHORTLEN];
  69. LoadVector(S, buff, size);
  70. return luaS_newlstr(S->L, buff, size);
  71. }
  72. else { /* long string */
  73. TString *ts = luaS_createlngstrobj(S->L, size);
  74. LoadVector(S, getstr(ts), size); /* load directly in final place */
  75. return ts;
  76. }
  77. }
  78. static void LoadCode (LoadState *S, Proto *f) {
  79. int n = LoadInt(S);
  80. f->code = luaM_newvector(S->L, n, Instruction);
  81. f->sizecode = n;
  82. LoadVector(S, f->code, n);
  83. }
  84. static void LoadFunction(LoadState *S, Proto *f, TString *psource);
  85. static void LoadConstants (LoadState *S, Proto *f) {
  86. int i;
  87. int n = LoadInt(S);
  88. f->k = luaM_newvector(S->L, n, TValue);
  89. f->sizek = n;
  90. for (i = 0; i < n; i++)
  91. setnilvalue(&f->k[i]);
  92. for (i = 0; i < n; i++) {
  93. TValue *o = &f->k[i];
  94. int t = LoadByte(S);
  95. switch (t) {
  96. case LUA_TNIL:
  97. setnilvalue(o);
  98. break;
  99. case LUA_TBOOLEAN:
  100. setbvalue(o, LoadByte(S));
  101. break;
  102. case LUA_TNUMFLT:
  103. setfltvalue(o, LoadNumber(S));
  104. break;
  105. case LUA_TNUMINT:
  106. setivalue(o, LoadInteger(S));
  107. break;
  108. case LUA_TSHRSTR:
  109. case LUA_TLNGSTR:
  110. setsvalue2n(S->L, o, LoadString(S));
  111. break;
  112. default:
  113. lua_assert(0);
  114. }
  115. }
  116. }
  117. static void LoadProtos (LoadState *S, Proto *f) {
  118. int i;
  119. int n = LoadInt(S);
  120. f->p = luaM_newvector(S->L, n, Proto *);
  121. f->sizep = n;
  122. for (i = 0; i < n; i++)
  123. f->p[i] = NULL;
  124. for (i = 0; i < n; i++) {
  125. f->p[i] = luaF_newproto(S->L);
  126. LoadFunction(S, f->p[i], f->source);
  127. }
  128. }
  129. static void LoadUpvalues (LoadState *S, Proto *f) {
  130. int i, n;
  131. n = LoadInt(S);
  132. f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
  133. f->sizeupvalues = n;
  134. for (i = 0; i < n; i++)
  135. f->upvalues[i].name = NULL;
  136. for (i = 0; i < n; i++) {
  137. f->upvalues[i].instack = LoadByte(S);
  138. f->upvalues[i].idx = LoadByte(S);
  139. }
  140. }
  141. static void LoadDebug (LoadState *S, Proto *f) {
  142. int i, n;
  143. n = LoadInt(S);
  144. f->lineinfo = luaM_newvector(S->L, n, int);
  145. f->sizelineinfo = n;
  146. LoadVector(S, f->lineinfo, n);
  147. n = LoadInt(S);
  148. f->locvars = luaM_newvector(S->L, n, LocVar);
  149. f->sizelocvars = n;
  150. for (i = 0; i < n; i++)
  151. f->locvars[i].varname = NULL;
  152. for (i = 0; i < n; i++) {
  153. f->locvars[i].varname = LoadString(S);
  154. f->locvars[i].startpc = LoadInt(S);
  155. f->locvars[i].endpc = LoadInt(S);
  156. }
  157. n = LoadInt(S);
  158. for (i = 0; i < n; i++)
  159. f->upvalues[i].name = LoadString(S);
  160. }
  161. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  162. f->source = LoadString(S);
  163. if (f->source == NULL) /* no source in dump? */
  164. f->source = psource; /* reuse parent's source */
  165. f->linedefined = LoadInt(S);
  166. f->lastlinedefined = LoadInt(S);
  167. f->numparams = LoadByte(S);
  168. f->is_vararg = LoadByte(S);
  169. f->maxstacksize = LoadByte(S);
  170. LoadCode(S, f);
  171. LoadConstants(S, f);
  172. LoadUpvalues(S, f);
  173. LoadProtos(S, f);
  174. LoadDebug(S, f);
  175. }
  176. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  177. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  178. size_t len = strlen(s);
  179. LoadVector(S, buff, len);
  180. if (memcmp(s, buff, len) != 0)
  181. error(S, msg);
  182. }
  183. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  184. if (LoadByte(S) != size)
  185. error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
  186. }
  187. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  188. static void checkHeader (LoadState *S) {
  189. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  190. if (LoadByte(S) != LUAC_VERSION)
  191. error(S, "version mismatch in");
  192. if (LoadByte(S) != LUAC_FORMAT)
  193. error(S, "format mismatch in");
  194. checkliteral(S, LUAC_DATA, "corrupted");
  195. checksize(S, int);
  196. checksize(S, size_t);
  197. checksize(S, Instruction);
  198. checksize(S, lua_Integer);
  199. checksize(S, lua_Number);
  200. if (LoadInteger(S) != LUAC_INT)
  201. error(S, "endianness mismatch in");
  202. if (LoadNumber(S) != LUAC_NUM)
  203. error(S, "float format mismatch in");
  204. }
  205. /*
  206. ** load precompiled chunk
  207. */
  208. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  209. LoadState S;
  210. LClosure *cl;
  211. if (*name == '@' || *name == '=')
  212. S.name = name + 1;
  213. else if (*name == LUA_SIGNATURE[0])
  214. S.name = "binary string";
  215. else
  216. S.name = name;
  217. S.L = L;
  218. S.Z = Z;
  219. checkHeader(&S);
  220. cl = luaF_newLclosure(L, LoadByte(&S));
  221. setclLvalue(L, L->top, cl);
  222. luaD_inctop(L);
  223. cl->p = luaF_newproto(L);
  224. LoadFunction(&S, cl->p, NULL);
  225. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  226. luai_verifycode(L, buff, cl->p);
  227. return cl;
  228. }