lundump.c 5.9 KB

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