-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaclass.c
More file actions
492 lines (394 loc) · 10.9 KB
/
javaclass.c
File metadata and controls
492 lines (394 loc) · 10.9 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* javaclass.c
*
* Operations on Java ClassFile struct
*
*
* Created by Robert van Engelen on 1/2/05.
* Copyright 2005 Robert van Engelen. All rights reserved.
*
*/
#include "javaclass.h"
static int save_constant_pool(FILE *fd, struct ClassFile *cf);
static int save_interfaces(FILE *fd, struct ClassFile *cf);
static int save_fields(FILE *fd, struct ClassFile *cf);
static int save_methods(FILE *fd, struct ClassFile *cf);
static int save_attributes(FILE *fd, struct ClassFile *cf);
static int save_Utf8_info(FILE *fd, const char *name);
static int save_Integer_info(FILE *fd, u4 i);
static int save_Float_info(FILE *fd, float f);
static int save_Class_info(FILE *fd, u2 name_index);
static int save_Fieldref_info(FILE *fd, struct Pair p);
static int save_Methodref_info(FILE *fd, struct Pair p);
static int save_NameAndType_info(FILE *fd, struct Pair p);
static int save_u1(FILE *fd, unsigned int n);
static int save_u2(FILE *fd, unsigned int n);
static int save_u4(FILE *fd, unsigned int n);
// Set up a class file structure
int init_ClassFile(struct ClassFile *cf)
{
cf->access = ACC_PUBLIC;
cf->name = NULL;
cf->constant_pool_count = 0;
cf->constant_pool = NULL;
cf->field_count = 0;
cf->fields = NULL;
cf->method_count = 0;
cf->methods = NULL;
return 0;
}
// Add a regular C string to the constant pool encoded in UTF8 format
int constant_pool_add_Utf8(struct ClassFile *cf, const char *name)
{
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Utf8 && !strcmp((*p)->data.u, name))
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Utf8;
(*p)->data.u = strdup(name);
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a 32-bit integer to the constant pool
int constant_pool_add_Integer(struct ClassFile *cf, u4 i)
{
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Integer && (*p)->data.i == i)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Integer;
(*p)->data.i = i;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a 32-bit float to the constant pool
int constant_pool_add_Float(struct ClassFile *cf, float f)
{
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Float && (*p)->data.f == f)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Float;
(*p)->data.f = f;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a class name to the constant pool
int constant_pool_add_Class(struct ClassFile *cf, const char *name)
{
int r = constant_pool_add_Utf8(cf, name);
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Class && (*p)->data.r == r)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Class;
(*p)->data.r = r;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a field reference to the constant pool
int constant_pool_add_Fieldref(struct ClassFile *cf, const char *class_name, const char *field_name, const char *descriptor)
{
struct Pair t;
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
t.i = constant_pool_add_Class(cf, class_name);
t.j = constant_pool_add_NameAndType(cf, field_name, descriptor);
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Fieldref && (*p)->data.t.i == t.i && (*p)->data.t.j == t.j)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Fieldref;
(*p)->data.t = t;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a method reference to the constant pool
int constant_pool_add_Methodref(struct ClassFile *cf, const char *class_name, const char *method_name, const char *descriptor)
{
struct Pair t;
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
t.i = constant_pool_add_Class(cf, class_name);
t.j = constant_pool_add_NameAndType(cf, method_name, descriptor);
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_Methodref && (*p)->data.t.i == t.i && (*p)->data.t.j == t.j)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_Methodref;
(*p)->data.t = t;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Add a (name,descriptor) pair to the constant pool
int constant_pool_add_NameAndType(struct ClassFile *cf, const char *name, const char *descriptor)
{
struct Pair t;
struct ConstantPool **p = &cf->constant_pool;
int index = 1;
t.i = constant_pool_add_Utf8(cf, name);
t.j = constant_pool_add_Utf8(cf, descriptor);
while (*p != NULL)
{
if ((*p)->tag == CONSTANT_NameAndType && (*p)->data.t.i == t.i && (*p)->data.t.j == t.j)
return index;
p = &(*p)->next;
index++;
}
*p = (struct ConstantPool*)malloc(sizeof(struct ConstantPool));
if (*p)
{
(*p)->tag = CONSTANT_NameAndType;
(*p)->data.t = t;
(*p)->next = NULL;
}
return ++cf->constant_pool_count;
}
// Save the class file to file "name.class", where "name" is the name of the class
int save_classFile(struct ClassFile *cf)
{
FILE *fd;
char filename[256];
int this_class_index, super_class_index;
strcpy(filename, cf->name);
strcat(filename, ".class");
fd = fopen(filename, "w");
this_class_index = constant_pool_add_Class(cf, cf->name);
super_class_index = constant_pool_add_Class(cf, "java/lang/Object");
save_u4(fd, 0xCAFEBABE); // magic number
save_u2(fd, 0x0000); // minor_version
save_u2(fd, 0x002E); // major_version
save_constant_pool(fd, cf);
save_u2(fd, cf->access); // access_flags
save_u2(fd, this_class_index); // this_class (index in pool refers to CONSTANT_Class_info)
save_u2(fd, super_class_index); // super_class (index in pool refers to CONSTANT_Class_info)
save_interfaces(fd, cf);
save_fields(fd, cf);
save_methods(fd, cf);
save_attributes(fd, cf);
fclose(fd);
return 0;
}
// Static functions
static int save_constant_pool(FILE *fd, struct ClassFile *cf)
{
struct ConstantPool *p;
int i;
// pool contains this class, super class, field names, field descriptors, method names, method descriptors, method code
save_u2(fd, cf->constant_pool_count + 2 + 2 * cf->field_count + 2 * cf->method_count); // constant_pool_count
for (p = cf->constant_pool; p; p = p->next)
{
switch (p->tag)
{
case CONSTANT_Utf8:
save_Utf8_info(fd, p->data.u);
break;
case CONSTANT_Integer:
save_Integer_info(fd, p->data.i);
break;
case CONSTANT_Float:
save_Float_info(fd, p->data.f);
break;
// case CONSTANT_Long:
// case CONSTANT_Double:
case CONSTANT_Class:
save_Class_info(fd, p->data.r);
break;
// case CONSTANT String:
case CONSTANT_Fieldref:
save_Fieldref_info(fd, p->data.t);
break;
case CONSTANT_Methodref:
save_Methodref_info(fd, p->data.t);
break;
// case CONSTANT_InterfaceMethodref:
case CONSTANT_NameAndType:
save_NameAndType_info(fd, p->data.t);
break;
}
}
// save standard stuff, indexed from cf->constant_pool_count + 1
save_Utf8_info(fd, "Code");
// field name index = cf->constant_pool_count + 2 + 2 * i
for (i = 0; i < cf->field_count; i++)
{
save_Utf8_info(fd, cf->fields[i].name);
save_Utf8_info(fd, cf->fields[i].descriptor);
}
// method name index = cf->constant_pool_count + 2 + 2 * field_count + 2 * i
for (i = 0; i < cf->method_count; i++)
{
save_Utf8_info(fd, cf->methods[i].name);
save_Utf8_info(fd, cf->methods[i].descriptor);
}
return 0;
}
static int save_interfaces(FILE *fd, struct ClassFile *cf)
{
// Not used at this time
(void)cf; // appease -Wall -Wextra
save_u2(fd, 0); // interfaces_count
// Each (u2) value in the interfaces array must be a valid index into the constant_pool table.
// u2(...)
return 0;
}
static int save_fields(FILE *fd, struct ClassFile *cf)
{
int i;
save_u2(fd, cf->field_count); // field_count
for (i = 0; i < cf->field_count; i++)
{
save_u2(fd, cf->fields[i].access);
save_u2(fd, cf->constant_pool_count + 2 + 2 * i);
save_u2(fd, cf->constant_pool_count + 3 + 2 * i);
save_u2(fd, 0); // no attributes
}
return 0;
}
static int save_methods(FILE *fd, struct ClassFile *cf)
{
int i;
save_u2(fd, cf->method_count); // method_count
for (i = 0; i < cf->method_count; i++)
{
save_u2(fd, cf->methods[i].access);
save_u2(fd, cf->constant_pool_count + 2 + 2 * cf->field_count + 2 * i);
save_u2(fd, cf->constant_pool_count + 3 + 2 * cf->field_count + 2 * i);
if (cf->methods[i].code_length != 0)
{
u4 j;
save_u2(fd, 1); // one attribute
save_u2(fd, cf->constant_pool_count + 1); // attribute_name_index
save_u4(fd, cf->methods[i].code_length + 12); // attribute_length
save_u2(fd, cf->methods[i].max_stack);
save_u2(fd, cf->methods[i].max_locals);
save_u4(fd, cf->methods[i].code_length);
for (j = 0; j < cf->methods[i].code_length; j++)
save_u1(fd, cf->methods[i].code[j]);
save_u2(fd, 0); // no exceptions
save_u2(fd, 0); // no attributes
}
else
save_u2(fd, 0);
}
return 0;
}
static int save_attributes(FILE *fd, struct ClassFile *cf)
{
(void)cf; // appease -Wall -Wextra
save_u2(fd, 0); // attributes_count
// Each value of the attributes table must be an attribute structure.
return 0;
}
static int save_Utf8_info(FILE *fd, const char *name)
{
save_u1(fd, CONSTANT_Utf8);
save_u2(fd, strlen(name));
fprintf(fd, "%s", name);
return 0;
}
static int save_Integer_info(FILE *fd, u4 i)
{
save_u1(fd, CONSTANT_Integer);
save_u4(fd, i);
return 0;
}
static int save_Float_info(FILE *fd, float f)
{ int i;
*(float*)&i = f;
save_u1(fd, CONSTANT_Float);
save_u4(fd, i);
return 0;
}
static int save_Class_info(FILE *fd, u2 name_index)
{
save_u1(fd, CONSTANT_Class);
save_u2(fd, name_index);
return 0;
}
static int save_Fieldref_info(FILE *fd, struct Pair p)
{
save_u1(fd, CONSTANT_Fieldref);
save_u2(fd, p.i);
save_u2(fd, p.j);
return 0;
}
static int save_Methodref_info(FILE *fd, struct Pair p)
{
save_u1(fd, CONSTANT_Methodref);
save_u2(fd, p.i);
save_u2(fd, p.j);
return 0;
}
static int save_NameAndType_info(FILE *fd, struct Pair p)
{
save_u1(fd, CONSTANT_NameAndType);
save_u2(fd, p.i);
save_u2(fd, p.j);
return 0;
}
static int save_u1(FILE *fd, unsigned int n)
{
fprintf(fd, "%c", (int)(n&0xFF));
return 0;
}
static int save_u2(FILE *fd, unsigned int n)
{
fprintf(fd, "%c%c", (int)((n>>8)&0xFF), (int)(n&0xFF));
return 0;
}
static int save_u4(FILE *fd, unsigned int n)
{
fprintf(fd, "%c%c%c%c", (int)((n>>24)&0xFF), (int)((n>>16)&0xFF), (int)((n>>8)&0xFF), (int)(n&0xFF));
return 0;
}