lundump.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. ** $Id: lundump.c,v 1.26 2000/02/17 19:17:44 lhf Exp lhf $
  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)
  44. {
  45. char b[256];
  46. int size=ezgetc(L,Z);
  47. LoadBlock(L,b,size,Z);
  48. b[size]=0;
  49. return luaU_str2d(L,b,zname(Z));
  50. }
  51. static int LoadInt (lua_State* L, ZIO* Z, const char* message)
  52. {
  53. unsigned long l=LoadLong(L,Z);
  54. unsigned int i=l;
  55. if (i!=l) luaL_verror(L,message,l,zname(Z));
  56. return i;
  57. }
  58. static void LoadCode (lua_State* L, TProtoFunc* tf, ZIO* Z)
  59. {
  60. int size=LoadInt(L,Z,"code too long (%lu bytes) in %.255s");
  61. tf->code=luaM_newvector(L,size,Instruction);
  62. LoadBlock(L,tf->code,size*sizeof(tf->code[0]),Z);
  63. if (tf->code[size-1]!=ENDCODE) luaL_verror(L,"bad code in %.255s",zname(Z));
  64. }
  65. static TaggedString* LoadString (lua_State* L, ZIO* Z)
  66. {
  67. long size=LoadLong(L,Z);
  68. if (size==0)
  69. return NULL;
  70. else
  71. {
  72. char* s=luaL_openspace(L,size);
  73. LoadBlock(L,s,size,Z);
  74. return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
  75. }
  76. }
  77. static void LoadLocals (lua_State* L, TProtoFunc* tf, ZIO* Z)
  78. {
  79. int i,n=LoadInt(L,Z,"too many locals (%lu) in %.255s");
  80. if (n==0) return;
  81. tf->locvars=luaM_newvector(L,n+1,LocVar);
  82. for (i=0; i<n; i++)
  83. {
  84. tf->locvars[i].line=LoadInt(L,Z,"too many lines (%lu) in %.255s");
  85. tf->locvars[i].varname=LoadString(L,Z);
  86. }
  87. tf->locvars[i].line=-1; /* flag end of vector */
  88. tf->locvars[i].varname=NULL;
  89. }
  90. static TProtoFunc* LoadFunction (lua_State* L, ZIO* Z, int native);
  91. static void LoadConstants (lua_State* L, TProtoFunc* tf, ZIO* Z, int native)
  92. {
  93. int i,n;
  94. tf->nkstr=n=LoadInt(L,Z,"too many strings (%lu) in %.255s");
  95. if (n>0)
  96. {
  97. tf->kstr=luaM_newvector(L,n,TaggedString*);
  98. for (i=0; i<n; i++) tf->kstr[i]=LoadString(L,Z);
  99. }
  100. tf->nknum=n=LoadInt(L,Z,"too many numbers (%lu) in %.255s");
  101. if (n>0)
  102. {
  103. tf->knum=luaM_newvector(L,n,real);
  104. if (native)
  105. LoadBlock(L,tf->knum,n*sizeof(tf->knum[0]),Z);
  106. else
  107. for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
  108. }
  109. tf->nkproto=n=LoadInt(L,Z,"too many functions (%lu) in %.255s");
  110. if (n>0)
  111. {
  112. tf->kproto=luaM_newvector(L,n,TProtoFunc*);
  113. for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
  114. }
  115. }
  116. static TProtoFunc* LoadFunction (lua_State* L, ZIO* Z, int native)
  117. {
  118. TProtoFunc* tf=luaF_newproto(L);
  119. tf->lineDefined=LoadInt(L,Z,"lineDefined too large (%lu) in %.255s");
  120. tf->source=LoadString(L,Z);
  121. if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
  122. tf->numparams=LoadInt(L,Z,"numparams too large (%lu) in %.255s");
  123. tf->is_vararg=LoadInt(L,Z,"is_vararg too large (%lu) in %.255s");
  124. tf->maxstacksize=LoadInt(L,Z,"maxstacksize too large (%lu) in %.255s");
  125. LoadCode(L,tf,Z);
  126. LoadLocals(L,tf,Z);
  127. LoadConstants(L,tf,Z,native);
  128. return tf;
  129. }
  130. static void LoadSignature (lua_State* L, ZIO* Z)
  131. {
  132. const char* s=SIGNATURE;
  133. while (*s!=0 && ezgetc(L,Z)==*s)
  134. ++s;
  135. if (*s!=0) luaL_verror(L,"bad signature in %.255s",zname(Z));
  136. }
  137. #define V(v) v/16,v%16
  138. static int LoadHeader (lua_State* L, ZIO* Z)
  139. {
  140. int version,sizeofR,native;
  141. LoadSignature(L,Z);
  142. version=ezgetc(L,Z);
  143. if (version>VERSION)
  144. luaL_verror(L,
  145. "%.255s too new: its version is %d.%d; expected at most %d.%d",
  146. zname(Z),V(version),V(VERSION));
  147. if (version<VERSION0) /* check last major change */
  148. luaL_verror(L,
  149. "%.255s too old: its version is %d.%d; expected at least %d.%d",
  150. zname(Z),V(version),V(VERSION));
  151. sizeofR=ezgetc(L,Z);
  152. native=(sizeofR!=0);
  153. if (native) /* test number representation */
  154. {
  155. if (sizeofR!=sizeof(real))
  156. luaL_verror(L,"unknown number size in %.255s: read %d; expected %d",
  157. zname(Z),sizeofR,(int)sizeof(real));
  158. else
  159. {
  160. real f=0,tf=TEST_NUMBER;
  161. LoadBlock(L,&f,sizeof(f),Z);
  162. if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
  163. luaL_verror(L,"unknown number format in %.255s: "
  164. "read " NUMBER_FMT "; expected " NUMBER_FMT,
  165. zname(Z),f,tf);
  166. }
  167. }
  168. return native;
  169. }
  170. static TProtoFunc* LoadChunk (lua_State* L, ZIO* Z)
  171. {
  172. return LoadFunction(L,Z,LoadHeader(L,Z));
  173. }
  174. /*
  175. ** load one chunk from a file or buffer
  176. ** return main if ok and NULL at EOF
  177. */
  178. TProtoFunc* luaU_undump1 (lua_State* L, ZIO* Z)
  179. {
  180. int c=zgetc(Z);
  181. if (c==ID_CHUNK)
  182. return LoadChunk(L,Z);
  183. else if (c!=EOZ)
  184. luaL_verror(L,"%.255s is not a Lua binary file",zname(Z));
  185. return NULL;
  186. }
  187. /*
  188. * convert number from text
  189. */
  190. double luaU_str2d (lua_State* L, const char* b, const char* where)
  191. {
  192. double x;
  193. if (!luaO_str2d(b,&x))
  194. luaL_verror(L,"cannot convert number '%.255s' in %.255s",b,where);
  195. return x;
  196. }