JAVACLFL.C 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* javaclfl.c: Copyright (C) Codemist Ltd., 1996. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "machine.h"
  5. #include "tags.h"
  6. #include "cslerror.h"
  7. #include "externs.h"
  8. #include "read.h"
  9. #include "stream.h"
  10. #include "arith.h"
  11. #include "entries.h"
  12. #include "javahost.h"
  13. #include "javaglb.h"
  14. /* #include "javatype.h" */
  15. #define D_CLASSFILE 1
  16. #define D_CLASSFILEIO 2
  17. #define D_CLASSFILECODE 4
  18. static int depth;
  19. static unsigned32 rd1(FILE *f)
  20. { unsigned32 x = getc(f);
  21. if (debugging & D_CLASSFILEIO) jdebug("rd1 %d", x);
  22. return x;
  23. }
  24. static unsigned32 rd2(FILE *f)
  25. { unsigned32 x = getc(f);
  26. x = x<<8 | getc(f);
  27. if (debugging & D_CLASSFILEIO) jdebug("rd2 0x%.4x", x);
  28. return x;
  29. }
  30. static unsigned32 rd4(FILE *f)
  31. { unsigned32 x = getc(f);
  32. x = x<<8 | getc(f);
  33. x = x<<8 | getc(f);
  34. x = x<<8 | getc(f);
  35. if (debugging & D_CLASSFILEIO) jdebug("rd4 0x%.8x", x);
  36. return x;
  37. }
  38. static char *rd1string(FILE *f, unsigned32 n)
  39. { int i;
  40. unsigned8 *p = (unsigned8 *)jmalloc((n+1) * sizeof(*p));
  41. for (i=0; i<n; i++) p[i] = rd1(f);
  42. p[n] = 0; /* @@@ ease C access for debugging (temp?) */
  43. return (char *)p;
  44. }
  45. static char *rd2string(FILE *f, unsigned32 n)
  46. { int i;
  47. unsigned16 *p = (unsigned16 *)jmalloc(n * sizeof(*p));
  48. for (i=0; i<n; i++) p[i] = rd2(f);
  49. return (char *)p;
  50. }
  51. static char *cpname(unsigned32 cpx, ClassFile *cf)
  52. { if (cpx < cf->constant_pool_count)
  53. { Cp_Info *cpitem = &cf->constant_pool[cpx];
  54. if (cpitem->tag == CONSTANT_Utf8) return cpitem->u.utf8;
  55. return "<non-utf8 const pool index>";
  56. }
  57. return "<bad const pool index>";
  58. }
  59. static Attribute_Sort lookup_attribute_sort(char *utf8, unsigned32 len)
  60. { /* Should use 'len' not rely on rd1string() zero padding. */
  61. if (strcmp(utf8,"SourceFile") == 0) return ATT_SourceFile;
  62. if (strcmp(utf8,"ConstantValue") == 0) return ATT_ConstantValue;
  63. if (strcmp(utf8,"Code") == 0) return ATT_Code;
  64. if (strcmp(utf8,"Exceptions") == 0) return ATT_Exceptions;
  65. if (strcmp(utf8,"LineNumberTable") == 0) return ATT_LineNumberTable;
  66. if (strcmp(utf8,"LocalVariableTable") == 0) return ATT_LocalVariableTable;
  67. return ATT_unknown;
  68. }
  69. static int rdAttribute_Info(FILE *f, ClassFile *cf, unsigned32 n, Attribute_Info **res);
  70. static int rdSourceFile_Attribute(FILE *f, ClassFile *cf,
  71. SourceFile_Attribute **res)
  72. { SourceFile_Attribute *p = (SourceFile_Attribute *)jmalloc(sizeof(*p));
  73. depth++;
  74. p->sourcefile_index = rd2(f);
  75. if (debugging & D_CLASSFILE)
  76. jdebug("%*sAttribute Sourcefile '%s'",
  77. depth*2, "", cpname(p->sourcefile_index, cf));
  78. *res = p;
  79. return (depth--, 0);
  80. }
  81. static int rdConstantValue_Attribute(FILE *f, ClassFile *cf,
  82. ConstantValue_Attribute **res)
  83. { ConstantValue_Attribute *p = (ConstantValue_Attribute *)jmalloc(sizeof(*p));
  84. depth++;
  85. p->constantvalue_index = rd2(f);
  86. if (debugging & D_CLASSFILE)
  87. jdebug("%*sAttribute ConstantValue [%d]",
  88. depth*2, "", p->constantvalue_index);
  89. *res = p;
  90. return (depth--, 0);
  91. }
  92. static int rdCode_Attribute(FILE *f, ClassFile *cf, Code_Attribute **res)
  93. { Code_Attribute *p = (Code_Attribute *)jmalloc(sizeof(*p));
  94. unsigned32 i,n;
  95. depth++;
  96. p->max_stack = rd2(f);
  97. p->max_locals = rd2(f);
  98. p->code_length = n = rd4(f);
  99. p->code = (unsigned8 *)jmalloc(n * sizeof(*p->code));
  100. for (i=0; i<n; i++) p->code[i] = rd1(f);
  101. p->exception_table_length = n = rd2(f);
  102. p->exception_table = (Exception_Info *)jmalloc(n * sizeof(*p->exception_table));
  103. for (i=0; i<n; i++)
  104. { Exception_Info *q = &p->exception_table[i];
  105. q->start_pc = rd2(f);
  106. q->end_pc = rd2(f);
  107. q->handler_pc = rd2(f);
  108. q->catch_type = rd2(f);
  109. }
  110. p->attributes_count = n = rd2(f);
  111. if (debugging & D_CLASSFILE)
  112. jdebug("%*sAttribute Code \
  113. (stack %d, locals %d, bytes %d, exceptions %d)",
  114. depth*2, "",
  115. p->max_stack, p->max_locals,
  116. p->code_length, p->exception_table_length);
  117. if (debugging & D_CLASSFILECODE)
  118. javadecode(p->code, p->code_length,
  119. cf->constant_pool, cf->constant_pool_count);
  120. if (rdAttribute_Info(f, cf, n, &p[i].attributes)) return 5;
  121. *res = p;
  122. return (depth--, 0);
  123. }
  124. static int rdExceptions_Attribute(FILE *f, ClassFile *cf,
  125. Exceptions_Attribute **res)
  126. { Exceptions_Attribute *p = (Exceptions_Attribute *)jmalloc(sizeof(*p));
  127. unsigned32 i,n;
  128. depth++;
  129. p->number_of_exceptions = n = rd2(f);
  130. p->exception_index_table = (unsigned16 *)jmalloc(n * sizeof(*p->exception_index_table));
  131. for (i=0; i<n; i++)
  132. p->exception_index_table[i] = rd2(f);
  133. if (debugging & D_CLASSFILE)
  134. jdebug("%*sAttribute Exceptions (entries %d)",
  135. depth*2, "", p->number_of_exceptions);
  136. *res = p;
  137. return (depth--, 0);
  138. }
  139. static int rdLineNumberTable_Attribute(FILE *f, ClassFile *cf,
  140. LineNumberTable_Attribute **res)
  141. { LineNumberTable_Attribute *p = (LineNumberTable_Attribute *)jmalloc(sizeof(*p));
  142. unsigned32 i,n;
  143. depth++;
  144. p->line_number_table_length = n = rd2(f);
  145. p->line_number_table = (LineNumber_Info *)jmalloc(n * sizeof(*p->line_number_table));
  146. for (i=0; i<n; i++)
  147. { LineNumber_Info *q = &p->line_number_table[i];
  148. q->start_pc = rd2(f);
  149. q->line_number = rd2(f);
  150. }
  151. if (debugging & D_CLASSFILE)
  152. jdebug("%*sAttribute LineNumberTable (entries %d)",
  153. depth*2, "", p->line_number_table_length);
  154. *res = p;
  155. return (depth--, 0);
  156. }
  157. static int rdLocalVariableTable_Attribute(FILE *f, ClassFile *cf,
  158. LocalVariableTable_Attribute **res)
  159. { LocalVariableTable_Attribute *p = (LocalVariableTable_Attribute *)jmalloc(sizeof(*p));
  160. unsigned32 i,n;
  161. depth++;
  162. p->local_variable_table_length = n = rd2(f);
  163. p->local_variable_table = (LocalVariable_Info *)jmalloc(n * sizeof(*p->local_variable_table));
  164. for (i=0; i<n; i++)
  165. { LocalVariable_Info *q = &p->local_variable_table[i];
  166. q->start_pc = rd2(f);
  167. q->length = rd2(f);
  168. q->name_index = rd2(f);
  169. q->signature_index = rd2(f);
  170. q->slot = rd2(f);
  171. }
  172. if (debugging & D_CLASSFILE)
  173. jdebug("%*sAttribute LocalVariableTable (entries %d)",
  174. depth*2, "", p->local_variable_table_length);
  175. *res = p;
  176. return (depth--, 0);
  177. }
  178. /* 'Attributes' occur more than once in a Classfile, hence they are */
  179. /* stored in *res (OK/NOK is return value), but reading Attributes */
  180. /* also requires cf to access the previously read Cp_Info. */
  181. static int rdAttribute_Info(FILE *f, ClassFile *cf, unsigned32 n, Attribute_Info **res)
  182. { Attribute_Info *p;
  183. unsigned32 i;
  184. if (n > 0xffff) return 1;
  185. p = (Attribute_Info *)jmalloc(n * sizeof(*p));
  186. if (debugging & D_CLASSFILE)
  187. jdebug("%*sAttributes_Info %d", depth*2, "", n);
  188. for (i=0; i<n; i++)
  189. { unsigned32 cpx = rd2(f);
  190. unsigned32 len = rd4(f);
  191. Cp_Info *cpitem = &cf->constant_pool[cpx];
  192. if (cpitem->tag != CONSTANT_Utf8) return 3;
  193. switch (p[i].sort = lookup_attribute_sort(cpitem->u.utf8, cpitem->len))
  194. {
  195. case ATT_Code:
  196. if (rdCode_Attribute(f, cf, &p[i].uattr.code)) return 6;
  197. break;
  198. case ATT_SourceFile:
  199. if (rdSourceFile_Attribute(f, cf, &p[i].uattr.sourcefile)) return 6;
  200. break;
  201. case ATT_ConstantValue:
  202. if (rdConstantValue_Attribute(f, cf, &p[i].uattr.constantvalue))
  203. return 6;
  204. break;
  205. case ATT_Exceptions:
  206. if (rdExceptions_Attribute(f, cf, &p[i].uattr.exceptions))
  207. return 6;
  208. break;
  209. case ATT_LineNumberTable:
  210. if (rdLineNumberTable_Attribute(f, cf, &p[i].uattr.linenumbertable))
  211. return 6;
  212. break;
  213. case ATT_LocalVariableTable:
  214. if (rdLocalVariableTable_Attribute(f, cf, &p[i].uattr.localvariabletable))
  215. return 6;
  216. break;
  217. default: /* currently ignore other attributes */
  218. if (debugging & D_CLASSFILE)
  219. jdebug("%*sAttribute '%s' unknown", (depth+1)*2, "",
  220. cpitem->u.utf8);
  221. while (len) rd1(f), len--;
  222. }
  223. }
  224. *res = p;
  225. return 0;
  226. }
  227. static int rdCp_Info(FILE *f, ClassFile *cf, unsigned32 n)
  228. { Cp_Info *p;
  229. unsigned32 i, nn;
  230. depth++;
  231. if (n > 0xffff) return 1;
  232. if (debugging & D_CLASSFILE) jdebug("Cp_Info %d", n);
  233. p = (Cp_Info *)jmalloc(n * sizeof(*p));
  234. /* Yes, the next line really is '1'! */
  235. for (i=1; i<n; i++) switch (p[i].tag = rd1(f))
  236. {
  237. default: jsyserr("tag %d", p[i].tag); return 2;
  238. case CONSTANT_Class:
  239. p[i].u.val = rd2(f); break;
  240. case CONSTANT_FieldRef:
  241. case CONSTANT_MethodRef:
  242. case CONSTANT_InterfaceMethodRef:
  243. /* stuff name_and_type_index into len */
  244. p[i].u.val = rd2(f); p[i].len = rd2(f); break;
  245. case CONSTANT_String:
  246. p[i].u.val = rd2(f); break;
  247. case CONSTANT_Integer:
  248. case CONSTANT_Float: p[i].u.val = rd4(f);
  249. break;
  250. case CONSTANT_Long:
  251. case CONSTANT_Double: p[i].u.val = rd4(f);
  252. i++; p[i].tag = CONSTANT_Xhalf; p[i].u.val = rd4(f);
  253. break;
  254. case CONSTANT_Utf8: if ((nn = rd2(f)) > 0xffff) nn = 0; /* EOF */
  255. p[i].len = nn; p[i].u.utf8 = rd1string(f, nn);
  256. break;
  257. case CONSTANT_Unicode: if ((nn = rd2(f)) > 0xffff) nn = 0; /* EOF */
  258. p[i].len = nn; p[i].u.utf8 = rd2string(f, nn);
  259. break;
  260. case CONSTANT_NameAndType:
  261. /* stuff signature_index into len */
  262. p[i].u.val = rd2(f); p[i].len = rd2(f); break;
  263. }
  264. cf->constant_pool = p;
  265. return (depth--, 0);
  266. }
  267. static int rdInterface_Info(FILE *f, ClassFile *cf, unsigned32 n)
  268. { unsigned16 *p;
  269. unsigned32 i;
  270. depth++;
  271. if (n > 0xffff) return 1;
  272. if (debugging & D_CLASSFILE) jdebug("Interface_Info %d", n);
  273. p = (unsigned16 *)jmalloc(n * sizeof(*p));
  274. for (i=0; i<n; i++) p[i] = rd2(f);
  275. cf->interfaces = p;
  276. return (depth--, 0);
  277. }
  278. static int rdField_Info(FILE *f, ClassFile *cf, unsigned32 n)
  279. { Field_Info *p;
  280. unsigned32 i;
  281. depth++;
  282. if (n > 0xffff) return 1;
  283. if (debugging & D_CLASSFILE) jdebug("Field_Info %d", n);
  284. p = (Field_Info *)jmalloc(n * sizeof(*p));
  285. for (i=0; i<n; i++)
  286. { unsigned32 nn;
  287. p[i].access_flags = rd2(f);
  288. p[i].name_index = rd2(f);
  289. p[i].signature_index = rd2(f);
  290. p[i].attributes_count = nn = rd2(f);
  291. if (debugging & D_CLASSFILE)
  292. jdebug(" Field '%s' signature '%s' flags 0x%x",
  293. cpname(p[i].name_index, cf),
  294. cpname(p[i].signature_index, cf),
  295. p[i].access_flags);
  296. if (rdAttribute_Info(f, cf, nn, &p[i].attributes)) return 4;
  297. }
  298. cf->fields = p;
  299. return (depth--, 0);
  300. }
  301. static int rdMethod_Info(FILE *f, ClassFile *cf, unsigned32 n)
  302. { Method_Info *p;
  303. unsigned32 i;
  304. depth++;
  305. if (n > 0xffff) return 1;
  306. if (debugging & D_CLASSFILE) jdebug("Method_Info %d", n);
  307. p = (Method_Info *)jmalloc(n * sizeof(*p));
  308. for (i=0; i<n; i++)
  309. { unsigned32 nn;
  310. p[i].access_flags = rd2(f);
  311. p[i].name_index = rd2(f);
  312. p[i].signature_index = rd2(f);
  313. p[i].attributes_count = nn = rd2(f);
  314. if (debugging & D_CLASSFILE)
  315. jdebug(" Method '%s' signature '%s' flags 0x%x",
  316. cpname(p[i].name_index, cf),
  317. cpname(p[i].signature_index, cf),
  318. p[i].access_flags);
  319. if (rdAttribute_Info(f, cf, nn, &p[i].attributes)) return 4;
  320. }
  321. cf->methods = p;
  322. return (depth--, 0);
  323. }
  324. ClassFile *rdClassFile(char *name)
  325. { FILE *f = fopen(name, "rb");
  326. ClassFile *cf = rdClassFILE1(f, name);
  327. fclose(f);
  328. return cf;
  329. }
  330. ClassFile *rdClassFILE1(FILE *f, char *name)
  331. {
  332. ClassFile *cf;
  333. unsigned32 n;
  334. depth = 0;
  335. if (f == 0)
  336. { jsyserr("cannot read '%s'", name); return 0; }
  337. if (rd4(f) != JAVA_MAGIC)
  338. { jsyserr("not class file '%s'", name); return 0; }
  339. if ((n = rd2(f)) > JAVA_THIS_MIN)
  340. jdebug("ClassFile '%s' wrong minor vsn %d", name, n);
  341. if ((n = rd2(f)) != JAVA_THIS_MAJ)
  342. jdebug("ClassFile '%s' wrong major vsn %d", name, n);
  343. cf = (ClassFile *)jmalloc(sizeof(ClassFile));
  344. cf->constant_pool_count = n = rd2(f);
  345. if (rdCp_Info(f, cf, n)) goto corrupt;
  346. cf->access_flags = rd2(f);
  347. cf->this_class = rd2(f);
  348. cf->super_class = rd2(f);
  349. cf->interfaces_count = n = rd2(f);
  350. if (rdInterface_Info(f, cf, n)) goto corrupt;
  351. cf->fields_count = n = rd2(f);
  352. if (rdField_Info(f, cf, n)) goto corrupt;
  353. cf->methods_count = n = rd2(f);
  354. if (rdMethod_Info(f, cf, n)) goto corrupt;
  355. cf->attributes_count = n = rd2(f);
  356. if (rdAttribute_Info(f, cf, n, &cf->attributes)) goto corrupt;
  357. if (rd1(f) != EOF) jdebug("junk at end of ClassFile '%s'", name);
  358. return cf;
  359. corrupt:
  360. jsyserr("ClassFile '%s' corrupted", name);
  361. return 0;
  362. }
  363.