lundump.c 5.4 KB

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