lundump.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. ** $Id: lundump.c,v 1.14 1999/09/06 13:55:09 roberto Exp roberto $
  3. ** load bytecodes from files
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "lauxlib.h"
  10. #include "lfunc.h"
  11. #include "lmem.h"
  12. #include "lopcodes.h"
  13. #include "lstring.h"
  14. #include "lundump.h"
  15. #define LoadBlock(L, b,size,Z) ezread(L, Z,b,size)
  16. static void unexpectedEOZ (lua_State *L, ZIO* Z)
  17. {
  18. luaL_verror(L, "unexpected end of file in %s",zname(Z));
  19. }
  20. static int ezgetc (lua_State *L, ZIO* Z)
  21. {
  22. int c=zgetc(Z);
  23. if (c==EOZ) unexpectedEOZ(L, Z);
  24. return c;
  25. }
  26. static void ezread (lua_State *L, ZIO* Z, void* b, int n)
  27. {
  28. int r=zread(Z,b,n);
  29. if (r!=0) unexpectedEOZ(L, Z);
  30. }
  31. static unsigned int LoadWord (lua_State *L, ZIO* Z)
  32. {
  33. unsigned int hi=ezgetc(L, Z);
  34. unsigned int lo=ezgetc(L, Z);
  35. return (hi<<8)|lo;
  36. }
  37. static unsigned long LoadLong (lua_State *L, ZIO* Z)
  38. {
  39. unsigned long hi=LoadWord(L, Z);
  40. unsigned long lo=LoadWord(L, Z);
  41. return (hi<<16)|lo;
  42. }
  43. /*
  44. * convert number from text
  45. */
  46. real luaU_str2d (lua_State *L, const char* b, const char* where)
  47. {
  48. real x;
  49. if (!luaO_str2d(b, &x))
  50. luaL_verror(L, "cannot convert number '%s' in %s",b,where);
  51. return x;
  52. }
  53. static real LoadNumber (lua_State *L, ZIO* Z, int native)
  54. {
  55. real x;
  56. if (native)
  57. {
  58. LoadBlock(L, &x,sizeof(x),Z);
  59. return x;
  60. }
  61. else
  62. {
  63. char b[256];
  64. int size=ezgetc(L, Z);
  65. LoadBlock(L, b,size,Z);
  66. b[size]=0;
  67. return luaU_str2d(L, b,zname(Z));
  68. }
  69. }
  70. static int LoadInt (lua_State *L, ZIO* Z, const char* message)
  71. {
  72. unsigned long l=LoadLong(L, Z);
  73. unsigned int i=l;
  74. if (i!=l) luaL_verror(L, message,l,zname(Z));
  75. return i;
  76. }
  77. #define PAD 5 /* two word operands plus opcode */
  78. static Byte* LoadCode (lua_State *L, ZIO* Z)
  79. {
  80. int size=LoadInt(L, Z,"code too long (%ld bytes) in %s");
  81. Byte* b=luaM_malloc(L, size+PAD);
  82. LoadBlock(L, b,size,Z);
  83. if (b[size-1]!=ENDCODE) luaL_verror(L, "bad code in %s",zname(Z));
  84. memset(b+size,ENDCODE,PAD); /* pad code for safety */
  85. return b;
  86. }
  87. static TaggedString* LoadTString (lua_State *L, ZIO* Z)
  88. {
  89. long size=LoadLong(L, Z);
  90. if (size==0)
  91. return NULL;
  92. else
  93. {
  94. char* s=luaL_openspace(L, size);
  95. LoadBlock(L, s,size,Z);
  96. return luaS_newlstr(L, s,size-1);
  97. }
  98. }
  99. static void LoadLocals (lua_State *L, TProtoFunc* tf, ZIO* Z)
  100. {
  101. int i,n=LoadInt(L, Z,"too many locals (%ld) in %s");
  102. if (n==0) return;
  103. tf->locvars=luaM_newvector(L, n+1,LocVar);
  104. for (i=0; i<n; i++)
  105. {
  106. tf->locvars[i].line=LoadInt(L, Z,"too many lines (%ld) in %s");
  107. tf->locvars[i].varname=LoadTString(L, Z);
  108. }
  109. tf->locvars[i].line=-1; /* flag end of vector */
  110. tf->locvars[i].varname=NULL;
  111. }
  112. static TProtoFunc* LoadFunction (lua_State *L, ZIO* Z, int native);
  113. static void LoadConstants (lua_State *L, TProtoFunc* tf, ZIO* Z, int native)
  114. {
  115. int i,n=LoadInt(L, Z,"too many constants (%ld) in %s");
  116. tf->nconsts=n;
  117. if (n==0) return;
  118. tf->consts=luaM_newvector(L, n,TObject);
  119. for (i=0; i<n; i++)
  120. {
  121. TObject* o=tf->consts+i;
  122. ttype(o)=-ezgetc(L, Z); /* ttype(o) is negative - ORDER LUA_T */
  123. switch (ttype(o))
  124. {
  125. case LUA_T_NUMBER:
  126. nvalue(o)=LoadNumber(L, Z,native);
  127. break;
  128. case LUA_T_STRING:
  129. tsvalue(o)=LoadTString(L, Z);
  130. break;
  131. case LUA_T_PROTO:
  132. tfvalue(o)=LoadFunction(L, Z,native);
  133. break;
  134. case LUA_T_NIL:
  135. break;
  136. default: /* cannot happen */
  137. luaU_badconstant(L, "load",i,o,tf);
  138. break;
  139. }
  140. }
  141. }
  142. static TProtoFunc* LoadFunction (lua_State *L, ZIO* Z, int native)
  143. {
  144. TProtoFunc* tf=luaF_newproto(L);
  145. tf->lineDefined=LoadInt(L, Z,"lineDefined too large (%ld) in %s");
  146. tf->source=LoadTString(L, Z);
  147. if (tf->source==NULL) tf->source=luaS_new(L, zname(Z));
  148. tf->code=LoadCode(L, Z);
  149. LoadLocals(L, tf,Z);
  150. LoadConstants(L, tf,Z,native);
  151. return tf;
  152. }
  153. static void LoadSignature (lua_State *L, ZIO* Z)
  154. {
  155. const char* s=SIGNATURE;
  156. while (*s!=0 && ezgetc(L, Z)==*s)
  157. ++s;
  158. if (*s!=0) luaL_verror(L, "bad signature in %s",zname(Z));
  159. }
  160. static int LoadHeader (lua_State *L, ZIO* Z)
  161. {
  162. int version,sizeofR;
  163. int native;
  164. LoadSignature(L, Z);
  165. version=ezgetc(L, Z);
  166. if (version>VERSION)
  167. luaL_verror(L,
  168. "%s too new: version=0x%02x; expected at most 0x%02x",
  169. zname(Z),version,VERSION);
  170. if (version<VERSION0) /* check last major change */
  171. luaL_verror(L,
  172. "%s too old: version=0x%02x; expected at least 0x%02x",
  173. zname(Z),version,VERSION0);
  174. sizeofR=ezgetc(L, Z);
  175. native=(sizeofR!=0);
  176. if (native) /* test number representation */
  177. {
  178. if (sizeofR!=sizeof(real))
  179. luaL_verror(L, "unknown number size in %s: read %d; expected %d",
  180. zname(Z),sizeofR,sizeof(real));
  181. else
  182. {
  183. real tf=TEST_NUMBER;
  184. real f=LoadNumber(L, Z,native);
  185. if ((long)f!=(long)tf)
  186. luaL_verror(L, "unknown number format in %s: "
  187. "read " NUMBER_FMT "; expected " NUMBER_FMT,
  188. zname(Z),f,tf);
  189. }
  190. }
  191. return native;
  192. }
  193. static TProtoFunc* LoadChunk (lua_State *L, ZIO* Z)
  194. {
  195. return LoadFunction(L, Z,LoadHeader(L, Z));
  196. }
  197. /*
  198. ** load one chunk from a file or buffer
  199. ** return main if ok and NULL at EOF
  200. */
  201. TProtoFunc* luaU_undump1 (lua_State *L, ZIO* Z)
  202. {
  203. int c=zgetc(Z);
  204. if (c==ID_CHUNK)
  205. return LoadChunk(L, Z);
  206. else if (c!=EOZ)
  207. luaL_verror(L, "%s is not a Lua binary file",zname(Z));
  208. return NULL;
  209. }
  210. /*
  211. * handle constants that cannot happen
  212. */
  213. void luaU_badconstant (lua_State *L, const char* s, int i, const TObject* o, TProtoFunc* tf)
  214. {
  215. int t=ttype(o);
  216. const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
  217. luaL_verror(L, "cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
  218. }