File: classread.cc

package info (click to toggle)
ht 0.5.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,388 kB
  • ctags: 9,064
  • sloc: cpp: 51,336; ansic: 11,954; sh: 2,742; yacc: 1,142; lex: 396; makefile: 178
file content (296 lines) | stat: -rw-r--r-- 7,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * java .class file parsing
 */

#include "stream.h"
#include "class.h"

#include <stdlib.h>
#include <string.h>

static u1 inp[4];
static u4 offset;
#define cls_read(a, b, c, d) (((b) != ((d)->read((a), (b)*(c)))) \
				? (0) : (offset+=(b), (b)))
#define READ1() \
  (((inp[0]=inp[1]=inp[2]=inp[3]=0),  \
   (1 == cls_read (inp, 1, 1, htio))) \
   ? (u1)(inp[0]) : 0)
#define READ2() \
  (((inp[0]=inp[1]=inp[2]=inp[3]=0),  \
   (2 == cls_read (inp, 2, 1, htio))) \
   ? ((((u2)inp[0])<<8)|inp[1]) : 0)
#define READ4() \
  (((inp[0]=inp[1]=inp[2]=inp[3]=0),  \
   (4 == cls_read (inp, 4, 1, htio))) \
   ? (((((((u4)inp[0]<<8)|inp[1])<<8)|inp[2])<<8)|inp[3]) : 0)
#define READN(inb, n) cls_read (inb, n, 1, htio)
#define SKIPN(n) {u1 b; for (u4 i=0; i<n; i++) {cls_read(&b, 1, 1, htio);}}
 

/* extract name from a utf8 constant pool entry */
static char *
get_string (ht_stream *htio, classfile *clazz, int index)
{
  return clazz->cpool[index]->value.string;
}

/* read and return constant pool entry */
static cp_info *
read_cpool_entry (ht_stream *htio, classfile *clazz)
{
  cp_info *cp;
  u2 idx;
  
  cp         = (cp_info *)malloc (sizeof (*cp));
  cp->offset = offset;
  cp->tag    = READ1();
  switch (cp->tag) {
    case CONSTANT_Utf8:
	 idx = READ2();
	 cp->value.string = (char *)malloc (idx+1);
	 cls_read (cp->value.string, idx, 1, htio);
	 cp->value.string[idx] = 0;
	 break;
    case CONSTANT_Integer:
    case CONSTANT_Float:
	 cp->value.fval = READ4();
	 break;
    case CONSTANT_Long:
    case CONSTANT_Double:
	 cp->value.llval[0] = READ4();
	 cp->value.llval[1] = READ4();
	 break;
    case CONSTANT_Class:
    case CONSTANT_String:
	 cp->value.llval[0] = READ2();
	 break;
    case CONSTANT_Fieldref:
    case CONSTANT_Methodref:
    case CONSTANT_InterfaceMethodref:
    case CONSTANT_NameAndType:
	 cp->value.llval[0] = READ2();
	 cp->value.llval[1] = READ2();
	 break;
    default:
	 return NULL;
  }
  return (cp);
}

/* read and return an attribute read */
attrib_info *
attribute_read (ht_stream *htio, classfile *clazz)
{
  attrib_info *a;
  u4 len;
  char *aname;

  a = (attrib_info *)malloc (sizeof (*a));
  if (!a) {
    return NULL;
  }
  a->offset    = offset;
  a->name      = READ2();
  a->len = len = READ4();

  aname = get_string (htio, clazz, a->name); 
  if (!strcmp (aname, "ConstantValue")) {
    a->tag = ATTRIB_ConstantValue;
  } else if (!strcmp (aname, "Code")) {
    a->tag = ATTRIB_Code;
  } else if (!strcmp (aname, "Exceptions")) {
    a->tag = ATTRIB_Exceptions;
  } else if (!strcmp (aname, "InnerClasses")) {
    a->tag = ATTRIB_InnerClasses;
  } else if (!strcmp (aname, "Synthetic")) {
    a->tag = ATTRIB_Synthetic;
  } else if (!strcmp (aname, "SourceFile")) {
    a->tag = ATTRIB_SourceFile;
  } else if (!strcmp (aname, "LineNumberTable")) {
    a->tag = ATTRIB_LineNumberTable;
  } else if (!strcmp (aname, "LocalVariableTable")) {
    a->tag = ATTRIB_LocalVariableTable;
  } else if (!strcmp (aname, "Deprecated")) {
    a->tag = ATTRIB_Deprecated;
  }
  SKIPN(len);
  return (a);
}

/* read and return method info */
static mf_info *
read_fieldmethod (ht_stream *htio, classfile *clazz)
{
  mf_info *m;
  u2 idx;
  int i;

  m = (mf_info *)calloc (1, sizeof (*m));
  if (!m) {
    return NULL;
  }
  m->offset   = offset;
  idx         = READ2();
  idx         = READ2();
  m->name     = get_string (htio, clazz, idx);
  idx         = READ2();
  m->desc     = get_string (htio, clazz, idx);
  idx         = READ2();
  m->attribs_count = idx;
  if (idx) {
    m->attribs = (attrib_info **)malloc (idx * sizeof (*(m->attribs)));
    if (!m->attribs) {
	 return NULL;
    }
    for (i=0; i<(int)idx; i++) {
	 m->attribs[i] = attribute_read (htio, clazz);
    }
  }
  return m;
}

/* read and return classfile */
classfile *
class_read (ht_stream *htio)
{
  classfile *clazz;
  u2 count;
  u2 cpcount, index;
  int i;
  
  clazz = (classfile *)malloc (sizeof (*clazz));
  if (!clazz) {
    return NULL;
  }
  clazz->offset = offset = 0;
  clazz->magic  = READ4();
  if (clazz->magic != 0xCAFEBABE) {
    return NULL;
  }
  clazz->minor_version = READ2();
  clazz->major_version = READ2();
  count = clazz->cpool_count = READ2();
  clazz->cpool = (cp_info **)malloc ((count+1) * sizeof (*(clazz->cpool)));
  if (!clazz->cpool) {
    return NULL;
  }
  cpcount = clazz->cpool_count;
  for (i=1; i<(int)count; i++) {
    clazz->cpool[i] = read_cpool_entry (htio, clazz);
    if ((clazz->cpool[i]->tag == CONSTANT_Long) ||
	   (clazz->cpool[i]->tag == CONSTANT_Double)) {
	 i++;
    }
  }
  clazz->coffset = offset;
  clazz->access_flags = READ2();
  index = READ2();
  clazz->this_class = index;
  index = READ2();
  clazz->super_class = index;
  count = clazz->interfaces_count = READ2();
  if (count) {
    clazz->interfaces = (u2 *)malloc (count * sizeof (*(clazz->interfaces)));
    if (!clazz->interfaces) {
	 return NULL;
    }
    for (i=0; i<(int)count; i++) {
	 index = READ2();
	 clazz->interfaces[i] = index;
    }
  } else {
    clazz->interfaces = 0;
  }
  clazz->foffset = offset;
  count = clazz->fields_count = READ2();
  if (count) {
    clazz->fields = (mf_info **)malloc (count * sizeof (*(clazz->fields)));
    if (!clazz->fields) {
	 return NULL;
    }
    for (i=0; i<(int)count; i++) {
	 clazz->fields[i] = read_fieldmethod (htio, clazz);
    }
  } else {
    clazz->fields = 0;
  }
  clazz->moffset = offset;
  count = clazz->methods_count = READ2();
  if (count) {
    clazz->methods = (mf_info **)malloc (count * sizeof (*(clazz->methods)));
    if (!clazz->methods) {
	 return NULL;
    }
    for (i=0; i<(int)count; i++) {
	 clazz->methods[i] = read_fieldmethod (htio, clazz);
    }
  } else {
    clazz->methods = 0;
  }
  clazz->aoffset = offset;
  count = clazz->attribs_count = READ2();
  if (count) {
    clazz->attribs = (attrib_info **)malloc (count*sizeof (*(clazz->attribs)));
    if (!clazz->attribs) {
	 return NULL; 
    }
    for (i=0; i<(int)count; i++) {
	 clazz->attribs[i] = attribute_read (htio, clazz);
	 if (!clazz->attribs[i]) {
	   return NULL;
	 }
    }
  } else {
    clazz->attribs = 0;
  }
  return clazz;
}
void
class_unread (classfile *clazz)
{
  u1 tag;
  unsigned i, j;

  if (!clazz) {
    return;
  }
  for (i=1; i<clazz->cpool_count; i++) {
    tag = clazz->cpool[i]->tag;
    free (clazz->cpool[i]);
    if ((tag == CONSTANT_Long) || (tag == CONSTANT_Double)) {
	 i++;
    }
  }
  if (clazz->cpool_count)
    free (clazz->cpool);
  if (clazz->interfaces) {
    free (clazz->interfaces);
  }
  for (i=0; i<clazz->fields_count; i++) {
    for (j=0; j<clazz->fields[i]->attribs_count; j++) {
	 free (clazz->fields[i]->attribs[j]);
    }
    if (clazz->fields[i]->attribs_count) 
	 free (clazz->fields[i]->attribs);
    free (clazz->fields[i]);
  }
  if (clazz->fields_count)
    free (clazz->fields);
  for (i=0; i<clazz->methods_count; i++) {
    for (j=0; j<clazz->methods[i]->attribs_count; j++) {
	 free (clazz->methods[i]->attribs[j]);
    }
    if (clazz->methods[i]->attribs_count) 
	 free (clazz->methods[i]->attribs);
    free (clazz->methods[i]);
  }
  if (clazz->methods_count)
    free (clazz->methods);
  for (i=0; i<clazz->attribs_count; i++) {
    free (clazz->attribs[i]);
  }
  if (clazz->attribs_count) {
    free (clazz->attribs);
  }
}