lundump.c 4.9 KB

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