21
21
#include <stdio.h>
22
22
#include <math.h>
23
23
24
- u1 u1of (unsigned char first ) {
24
+ typedef unsigned char ubyte ;
25
+ typedef unsigned short ushort ;
26
+ typedef unsigned int uint ;
27
+
28
+ u1 u1of (ubyte first ) {
25
29
u1 u = { first };
26
30
27
31
return u ;
28
32
}
29
33
30
- u2 u2of (unsigned char first , unsigned char second ) {
34
+ u2 u2of (ubyte first , ubyte second ) {
31
35
u2 u = { u1of (first ), u1of (second ) };
32
36
33
37
return u ;
34
38
}
35
39
36
- u4 u4of (unsigned char first , unsigned char second , unsigned char third , unsigned char fourth ) {
40
+ u4 u4of (ubyte first , ubyte second , ubyte third , ubyte fourth ) {
37
41
u4 u = { u2of (first , second ), u2of (third , fourth ) };
38
42
39
43
return u ;
40
44
}
41
45
42
- unsigned char byteOf (u1 u ) {
46
+ ubyte byteOf (u1 u ) {
43
47
return u .byte ;
44
48
}
45
49
46
- unsigned short shortOf (u2 u ) {
50
+ ushort shortOf (u2 u ) {
47
51
return (u .second .byte << 8 ) | u .first .byte ;
48
52
}
49
53
50
- unsigned int intOf (u4 u ) {
54
+ uint intOf (u4 u ) {
51
55
return (u .second .second .byte << 24 ) + (u .second .first .byte << 16 ) + (u .first .second .byte << 8 ) + (u .first .first .byte << 0 );
52
56
}
53
57
54
- Bytes bytesOf (const char * content , unsigned int size ) {
55
- Bytes bytes = { (unsigned char * ) content , size , 0 };
58
+ Bytes bytesOf (const char * content , uint size ) {
59
+ Bytes bytes = { (ubyte * ) content , size , 0 };
56
60
57
61
return bytes ;
58
62
}
59
63
60
- unsigned char readByte (Bytes * bytes ) {
64
+ ubyte readByte (Bytes * bytes ) {
61
65
return bytes -> actual [bytes -> ptr ++ ];
62
66
}
63
67
@@ -73,7 +77,7 @@ u4 readu4(Bytes* bytes) {
73
77
return u4of (readByte (bytes ), readByte (bytes ), readByte (bytes ), readByte (bytes ));
74
78
}
75
79
76
- u1 * readu1array_ (unsigned int len , Bytes * bytes ) {
80
+ u1 * readu1array_ (uint len , Bytes * bytes ) {
77
81
u1 * r = (u1 * ) malloc (len * sizeof (u1 ));
78
82
79
83
for (int i = 0 ; i < len ; i ++ ) {
@@ -84,7 +88,7 @@ u1* readu1array_(unsigned int len, Bytes* bytes) {
84
88
}
85
89
86
90
u1 * readu1array (u2 len , Bytes * bytes ) {
87
- unsigned short actualLen = shortOf (len );
91
+ ushort actualLen = shortOf (len );
88
92
u1 * r = (u1 * ) malloc (actualLen * sizeof (u1 ));
89
93
90
94
for (int i = 0 ; i < actualLen ; i ++ ) {
@@ -95,7 +99,7 @@ u1* readu1array(u2 len, Bytes* bytes) {
95
99
}
96
100
97
101
u2 * readu2array (u2 len , Bytes * bytes ) {
98
- unsigned short actualLen = shortOf (len );
102
+ ushort actualLen = shortOf (len );
99
103
u2 * r = (u2 * ) malloc (actualLen * sizeof (u2 ));
100
104
101
105
for (int i = 0 ; i < actualLen ; i ++ ) {
@@ -107,7 +111,7 @@ u2* readu2array(u2 len, Bytes* bytes) {
107
111
108
112
cp_info * readConstantPoolItem (Bytes * bytes ) {
109
113
u1 tag = readu1 (bytes );
110
- unsigned char actual = byteOf (tag );
114
+ ubyte actual = byteOf (tag );
111
115
cp_info * r ;
112
116
113
117
switch (actual ) {
@@ -273,7 +277,7 @@ cp_info** readConstantPool(u2 count, Bytes* bytes) {
273
277
return (cp_info * * ) malloc (0 * sizeof (cp_info * ));
274
278
}
275
279
276
- unsigned short len = shortOf (count ) - 1 ;
280
+ ushort len = shortOf (count ) - 1 ;
277
281
278
282
cp_info * * r = (cp_info * * ) malloc (shortOf (count ) * sizeof (cp_info * ));
279
283
@@ -287,7 +291,7 @@ cp_info** readConstantPool(u2 count, Bytes* bytes) {
287
291
}
288
292
289
293
String readUTFString (CONSTANT_Utf8_info from ) {
290
- unsigned short len = shortOf (from .length );
294
+ ushort len = shortOf (from .length );
291
295
char * content = (char * ) malloc ((len + 1 ) * sizeof (char ));
292
296
293
297
for (int i = 0 ; i < len ; i ++ ) {
@@ -300,7 +304,7 @@ String readUTFString(CONSTANT_Utf8_info from) {
300
304
}
301
305
302
306
char * readUTF (CONSTANT_Utf8_info from ) {
303
- unsigned short len = shortOf (from .length );
307
+ ushort len = shortOf (from .length );
304
308
char * content = (char * ) malloc ((len + 1 ) * sizeof (char ));
305
309
306
310
for (int i = 0 ; i < len ; i ++ ) {
@@ -550,7 +554,7 @@ attribute_info* readAttribute(cp_info** constant_pool, Bytes* bytes) {
550
554
551
555
for (int i = 0 ; i < shortOf (number_of_entries ); i ++ ) {
552
556
u1 frame_type = readu1 (bytes );
553
- unsigned short byte_frame = byteOf (frame_type );
557
+ ushort byte_frame = byteOf (frame_type );
554
558
stack_map_frame * entry ;
555
559
556
560
if (byte_frame <= 63 ) {
@@ -900,7 +904,7 @@ attribute_info* readAttribute(cp_info** constant_pool, Bytes* bytes) {
900
904
}
901
905
902
906
attribute_info * * readAttributes (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
903
- unsigned short len = shortOf (count );
907
+ ushort len = shortOf (count );
904
908
attribute_info * * r = (attribute_info * * ) malloc (len * sizeof (attribute_info * ));
905
909
906
910
for (int i = 0 ; i < len ; i ++ ) {
@@ -922,7 +926,7 @@ field_info readField(cp_info** constant_pool, Bytes* bytes) {
922
926
}
923
927
924
928
field_info * readFields (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
925
- unsigned short len = shortOf (count );
929
+ ushort len = shortOf (count );
926
930
field_info * r = (field_info * ) malloc (len * sizeof (field_info ));
927
931
928
932
for (int i = 0 ; i < len ; i ++ ) {
@@ -944,7 +948,7 @@ method_info readMethod(cp_info** constant_pool, Bytes* bytes) {
944
948
}
945
949
946
950
method_info * readMethods (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
947
- unsigned short len = shortOf (count );
951
+ ushort len = shortOf (count );
948
952
method_info * r = (method_info * ) malloc (len * sizeof (method_info ));
949
953
950
954
for (int i = 0 ; i < len ; i ++ ) {
@@ -993,7 +997,7 @@ ClassFile readClassFile(Bytes bytes) {
993
997
return classFile ;
994
998
}
995
999
996
- String writeClassAccessFlags (unsigned short access_flags , boolean isClass ) {
1000
+ String writeClassAccessFlags (ushort access_flags , boolean isClass ) {
997
1001
String output = stringEmpty ();
998
1002
999
1003
if ((access_flags & 0x0001 ) != 0 ) { // public
@@ -1046,7 +1050,7 @@ CONSTANT_Utf8_info UTF8(cp_info** constant_pool, u2 index) {
1046
1050
return * (CONSTANT_Utf8_info * ) constant_pool [shortOf (index )];
1047
1051
}
1048
1052
1049
- String readJavaVersion (unsigned short majorVersion , unsigned short minorVersion ) {
1053
+ String readJavaVersion (ushort majorVersion , ushort minorVersion ) {
1050
1054
String result = stringEmpty ();
1051
1055
1052
1056
switch (majorVersion ) {
@@ -1195,7 +1199,7 @@ Tuple readMethodDescriptor(String descriptor) {
1195
1199
return tupleOf (dynamicParameters , dynamicReturnType );
1196
1200
}
1197
1201
1198
- attribute_info * findAttribute (char * find , unsigned short size , attribute_info * * attributes , cp_info * * constant_pool ) {
1202
+ attribute_info * findAttribute (char * find , ushort size , attribute_info * * attributes , cp_info * * constant_pool ) {
1199
1203
attribute_info * attribute = (attribute_info * ) malloc (sizeof (attribute_info ));
1200
1204
1201
1205
for (int i = 0 ; i < size ; i ++ ) {
@@ -1236,7 +1240,7 @@ String readByteCode(u1* code, int* ptr) {
1236
1240
return itos (byteOf (code [++ * ptr ]));
1237
1241
}
1238
1242
1239
- unsigned short readShortIn (u1 * code , int * ptr ) {
1243
+ ushort readShortIn (u1 * code , int * ptr ) {
1240
1244
u1 first = code [++ * ptr ];
1241
1245
u1 second = code [++ * ptr ];
1242
1246
@@ -1282,7 +1286,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1282
1286
for (int i = 0 ; i < intOf (code .code_length ); i ++ ) {
1283
1287
append (& result , stringOf ("\t\t" , 2 ));
1284
1288
1285
- unsigned char next = byteOf (code .code [i ]);
1289
+ ubyte next = byteOf (code .code [i ]);
1286
1290
String typeInfo = stringEmpty ();
1287
1291
1288
1292
switch (next ) {
@@ -1736,7 +1740,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1736
1740
case 0xba :
1737
1741
append (& result , stringOf ("invokedynamic" , 13 ));
1738
1742
1739
- unsigned short cnst = readShortIn (code .code , & i );
1743
+ ushort cnst = readShortIn (code .code , & i );
1740
1744
1741
1745
append (& result , stringOf (" $" , 2 ));
1742
1746
append (& result , itos (cnst ));
@@ -1749,7 +1753,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1749
1753
case 0xb9 :
1750
1754
append (& result , stringOf ("invokeinterface" , 15 ));
1751
1755
1752
- unsigned short cnst_ = readShortIn (code .code , & i );
1756
+ ushort cnst_ = readShortIn (code .code , & i );
1753
1757
1754
1758
append (& result , stringOf (" $" , 2 ));
1755
1759
append (& result , itos (cnst_ ));
@@ -1764,7 +1768,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1764
1768
case 0xb7 :
1765
1769
append (& result , stringOf ("invokespecial" , 13 ));
1766
1770
1767
- unsigned short cnst__ = readShortIn (code .code , & i );
1771
+ ushort cnst__ = readShortIn (code .code , & i );
1768
1772
1769
1773
append (& result , stringOf (" $" , 2 ));
1770
1774
append (& result , itos (cnst__ ));
@@ -1775,7 +1779,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1775
1779
case 0xb8 :
1776
1780
append (& result , stringOf ("invokestatic" , 12 ));
1777
1781
1778
- unsigned short cnst___ = readShortIn (code .code , & i );
1782
+ ushort cnst___ = readShortIn (code .code , & i );
1779
1783
1780
1784
append (& result , stringOf (" $" , 2 ));
1781
1785
append (& result , itos (cnst___ ));
@@ -1786,7 +1790,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1786
1790
case 0xb6 :
1787
1791
append (& result , stringOf ("invokevirtual" , 13 ));
1788
1792
1789
- unsigned short cnst____ = readShortIn (code .code , & i );
1793
+ ushort cnst____ = readShortIn (code .code , & i );
1790
1794
1791
1795
append (& result , stringOf (" $" , 2 ));
1792
1796
append (& result , itos (cnst____ ));
@@ -1920,16 +1924,16 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
1920
1924
1921
1925
i += (i + 1 ) % 4 ; // 0-3 byte pad
1922
1926
1923
- unsigned char defaultByte1 = byteOf (code .code [++ i ]);
1924
- unsigned char defaultByte2 = byteOf (code .code [++ i ]);
1925
- unsigned char defaultByte3 = byteOf (code .code [++ i ]);
1926
- unsigned char defaultByte4 = byteOf (code .code [++ i ]);
1927
+ ubyte defaultByte1 = byteOf (code .code [++ i ]);
1928
+ ubyte defaultByte2 = byteOf (code .code [++ i ]);
1929
+ ubyte defaultByte3 = byteOf (code .code [++ i ]);
1930
+ ubyte defaultByte4 = byteOf (code .code [++ i ]);
1927
1931
int defaultByte = (defaultByte1 << 24 ) | (defaultByte2 << 16 ) | (defaultByte3 << 8 ) | defaultByte4 ;
1928
1932
1929
- unsigned char npairs1 = byteOf (code .code [++ i ]);
1930
- unsigned char npairs2 = byteOf (code .code [++ i ]);
1931
- unsigned char npairs3 = byteOf (code .code [++ i ]);
1932
- unsigned char npairs4 = byteOf (code .code [++ i ]);
1933
+ ubyte npairs1 = byteOf (code .code [++ i ]);
1934
+ ubyte npairs2 = byteOf (code .code [++ i ]);
1935
+ ubyte npairs3 = byteOf (code .code [++ i ]);
1936
+ ubyte npairs4 = byteOf (code .code [++ i ]);
1933
1937
int npairs = (npairs1 << 24 ) | (npairs2 << 16 ) | (npairs3 << 8 ) | npairs4 ;
1934
1938
1935
1939
append (& result , stringOf (" $" , 2 ));
@@ -2001,7 +2005,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
2001
2005
append (& result , stringOf ("new" , 3 ));
2002
2006
append (& result , stringOf (" $" , 2 ));
2003
2007
2004
- unsigned short cnst_____ = readShortIn (code .code , & i );
2008
+ ushort cnst_____ = readShortIn (code .code , & i );
2005
2009
2006
2010
append (& result , itos (cnst_____ ));
2007
2011
@@ -2090,22 +2094,22 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
2090
2094
2091
2095
i += (i + 1 ) % 4 ; // 0-3 byte pad
2092
2096
2093
- unsigned char defaultByte1_ = byteOf (code .code [++ i ]);
2094
- unsigned char defaultByte2_ = byteOf (code .code [++ i ]);
2095
- unsigned char defaultByte3_ = byteOf (code .code [++ i ]);
2096
- unsigned char defaultByte4_ = byteOf (code .code [++ i ]);
2097
+ ubyte defaultByte1_ = byteOf (code .code [++ i ]);
2098
+ ubyte defaultByte2_ = byteOf (code .code [++ i ]);
2099
+ ubyte defaultByte3_ = byteOf (code .code [++ i ]);
2100
+ ubyte defaultByte4_ = byteOf (code .code [++ i ]);
2097
2101
int defaultByte_ = (defaultByte1_ << 24 ) | (defaultByte2_ << 16 ) | (defaultByte3_ << 8 ) | defaultByte4_ ;
2098
2102
2099
- unsigned char lowbyte1 = byteOf (code .code [++ i ]);
2100
- unsigned char lowbyte2 = byteOf (code .code [++ i ]);
2101
- unsigned char lowbyte3 = byteOf (code .code [++ i ]);
2102
- unsigned char lowbyte4 = byteOf (code .code [++ i ]);
2103
+ ubyte lowbyte1 = byteOf (code .code [++ i ]);
2104
+ ubyte lowbyte2 = byteOf (code .code [++ i ]);
2105
+ ubyte lowbyte3 = byteOf (code .code [++ i ]);
2106
+ ubyte lowbyte4 = byteOf (code .code [++ i ]);
2103
2107
int lowbyte = (lowbyte1 << 24 ) | (lowbyte2 << 16 ) | (lowbyte3 << 8 ) | lowbyte4 ;
2104
2108
2105
- unsigned char highbyte1 = byteOf (code .code [++ i ]);
2106
- unsigned char highbyte2 = byteOf (code .code [++ i ]);
2107
- unsigned char highbyte3 = byteOf (code .code [++ i ]);
2108
- unsigned char highbyte4 = byteOf (code .code [++ i ]);
2109
+ ubyte highbyte1 = byteOf (code .code [++ i ]);
2110
+ ubyte highbyte2 = byteOf (code .code [++ i ]);
2111
+ ubyte highbyte3 = byteOf (code .code [++ i ]);
2112
+ ubyte highbyte4 = byteOf (code .code [++ i ]);
2109
2113
int highbyte = (highbyte1 << 24 ) | (highbyte2 << 16 ) | (highbyte3 << 8 ) | highbyte4 ;
2110
2114
2111
2115
append (& result , stringOf (" $" , 2 ));
@@ -2123,7 +2127,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
2123
2127
case 0xc4 :
2124
2128
append (& result , stringOf ("wide" , 4 ));
2125
2129
2126
- unsigned char opcode = byteOf (code .code [++ i ]);
2130
+ ubyte opcode = byteOf (code .code [++ i ]);
2127
2131
2128
2132
append (& result , stringOf (" $" , 2 ));
2129
2133
append (& result , itos (opcode ));
@@ -2188,7 +2192,7 @@ char* writeClassFile(ClassFile class) {
2188
2192
2189
2193
append (& output , className );
2190
2194
2191
- unsigned short superClass = shortOf (class .super_class );
2195
+ ushort superClass = shortOf (class .super_class );
2192
2196
2193
2197
if (superClass != 0 ) {
2194
2198
append (& output , stringOf (" extends " , 9 ));
@@ -2202,7 +2206,7 @@ char* writeClassFile(ClassFile class) {
2202
2206
append (& output , stringOf (superName , shortOf (super .length )));
2203
2207
}
2204
2208
2205
- unsigned short interfacesCount = shortOf (class .interfaces_count );
2209
+ ushort interfacesCount = shortOf (class .interfaces_count );
2206
2210
2207
2211
if (interfacesCount != 0 ) {
2208
2212
append (& output , stringOf (" implements " , 12 ));
0 commit comments