2121#include <stdio.h>
2222#include <math.h>
2323
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 ) {
2529 u1 u = { first };
2630
2731 return u ;
2832}
2933
30- u2 u2of (unsigned char first , unsigned char second ) {
34+ u2 u2of (ubyte first , ubyte second ) {
3135 u2 u = { u1of (first ), u1of (second ) };
3236
3337 return u ;
3438}
3539
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 ) {
3741 u4 u = { u2of (first , second ), u2of (third , fourth ) };
3842
3943 return u ;
4044}
4145
42- unsigned char byteOf (u1 u ) {
46+ ubyte byteOf (u1 u ) {
4347 return u .byte ;
4448}
4549
46- unsigned short shortOf (u2 u ) {
50+ ushort shortOf (u2 u ) {
4751 return (u .second .byte << 8 ) | u .first .byte ;
4852}
4953
50- unsigned int intOf (u4 u ) {
54+ uint intOf (u4 u ) {
5155 return (u .second .second .byte << 24 ) + (u .second .first .byte << 16 ) + (u .first .second .byte << 8 ) + (u .first .first .byte << 0 );
5256}
5357
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 };
5660
5761 return bytes ;
5862}
5963
60- unsigned char readByte (Bytes * bytes ) {
64+ ubyte readByte (Bytes * bytes ) {
6165 return bytes -> actual [bytes -> ptr ++ ];
6266}
6367
@@ -73,7 +77,7 @@ u4 readu4(Bytes* bytes) {
7377 return u4of (readByte (bytes ), readByte (bytes ), readByte (bytes ), readByte (bytes ));
7478}
7579
76- u1 * readu1array_ (unsigned int len , Bytes * bytes ) {
80+ u1 * readu1array_ (uint len , Bytes * bytes ) {
7781 u1 * r = (u1 * ) malloc (len * sizeof (u1 ));
7882
7983 for (int i = 0 ; i < len ; i ++ ) {
@@ -84,7 +88,7 @@ u1* readu1array_(unsigned int len, Bytes* bytes) {
8488}
8589
8690u1 * readu1array (u2 len , Bytes * bytes ) {
87- unsigned short actualLen = shortOf (len );
91+ ushort actualLen = shortOf (len );
8892 u1 * r = (u1 * ) malloc (actualLen * sizeof (u1 ));
8993
9094 for (int i = 0 ; i < actualLen ; i ++ ) {
@@ -95,7 +99,7 @@ u1* readu1array(u2 len, Bytes* bytes) {
9599}
96100
97101u2 * readu2array (u2 len , Bytes * bytes ) {
98- unsigned short actualLen = shortOf (len );
102+ ushort actualLen = shortOf (len );
99103 u2 * r = (u2 * ) malloc (actualLen * sizeof (u2 ));
100104
101105 for (int i = 0 ; i < actualLen ; i ++ ) {
@@ -107,7 +111,7 @@ u2* readu2array(u2 len, Bytes* bytes) {
107111
108112cp_info * readConstantPoolItem (Bytes * bytes ) {
109113 u1 tag = readu1 (bytes );
110- unsigned char actual = byteOf (tag );
114+ ubyte actual = byteOf (tag );
111115 cp_info * r ;
112116
113117 switch (actual ) {
@@ -273,7 +277,7 @@ cp_info** readConstantPool(u2 count, Bytes* bytes) {
273277 return (cp_info * * ) malloc (0 * sizeof (cp_info * ));
274278 }
275279
276- unsigned short len = shortOf (count ) - 1 ;
280+ ushort len = shortOf (count ) - 1 ;
277281
278282 cp_info * * r = (cp_info * * ) malloc (shortOf (count ) * sizeof (cp_info * ));
279283
@@ -287,7 +291,7 @@ cp_info** readConstantPool(u2 count, Bytes* bytes) {
287291}
288292
289293String readUTFString (CONSTANT_Utf8_info from ) {
290- unsigned short len = shortOf (from .length );
294+ ushort len = shortOf (from .length );
291295 char * content = (char * ) malloc ((len + 1 ) * sizeof (char ));
292296
293297 for (int i = 0 ; i < len ; i ++ ) {
@@ -300,7 +304,7 @@ String readUTFString(CONSTANT_Utf8_info from) {
300304}
301305
302306char * readUTF (CONSTANT_Utf8_info from ) {
303- unsigned short len = shortOf (from .length );
307+ ushort len = shortOf (from .length );
304308 char * content = (char * ) malloc ((len + 1 ) * sizeof (char ));
305309
306310 for (int i = 0 ; i < len ; i ++ ) {
@@ -550,7 +554,7 @@ attribute_info* readAttribute(cp_info** constant_pool, Bytes* bytes) {
550554
551555 for (int i = 0 ; i < shortOf (number_of_entries ); i ++ ) {
552556 u1 frame_type = readu1 (bytes );
553- unsigned short byte_frame = byteOf (frame_type );
557+ ushort byte_frame = byteOf (frame_type );
554558 stack_map_frame * entry ;
555559
556560 if (byte_frame <= 63 ) {
@@ -900,7 +904,7 @@ attribute_info* readAttribute(cp_info** constant_pool, Bytes* bytes) {
900904}
901905
902906attribute_info * * readAttributes (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
903- unsigned short len = shortOf (count );
907+ ushort len = shortOf (count );
904908 attribute_info * * r = (attribute_info * * ) malloc (len * sizeof (attribute_info * ));
905909
906910 for (int i = 0 ; i < len ; i ++ ) {
@@ -922,7 +926,7 @@ field_info readField(cp_info** constant_pool, Bytes* bytes) {
922926}
923927
924928field_info * readFields (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
925- unsigned short len = shortOf (count );
929+ ushort len = shortOf (count );
926930 field_info * r = (field_info * ) malloc (len * sizeof (field_info ));
927931
928932 for (int i = 0 ; i < len ; i ++ ) {
@@ -944,7 +948,7 @@ method_info readMethod(cp_info** constant_pool, Bytes* bytes) {
944948}
945949
946950method_info * readMethods (cp_info * * constant_pool , u2 count , Bytes * bytes ) {
947- unsigned short len = shortOf (count );
951+ ushort len = shortOf (count );
948952 method_info * r = (method_info * ) malloc (len * sizeof (method_info ));
949953
950954 for (int i = 0 ; i < len ; i ++ ) {
@@ -993,7 +997,7 @@ ClassFile readClassFile(Bytes bytes) {
993997 return classFile ;
994998}
995999
996- String writeClassAccessFlags (unsigned short access_flags , boolean isClass ) {
1000+ String writeClassAccessFlags (ushort access_flags , boolean isClass ) {
9971001 String output = stringEmpty ();
9981002
9991003 if ((access_flags & 0x0001 ) != 0 ) { // public
@@ -1046,7 +1050,7 @@ CONSTANT_Utf8_info UTF8(cp_info** constant_pool, u2 index) {
10461050 return * (CONSTANT_Utf8_info * ) constant_pool [shortOf (index )];
10471051}
10481052
1049- String readJavaVersion (unsigned short majorVersion , unsigned short minorVersion ) {
1053+ String readJavaVersion (ushort majorVersion , ushort minorVersion ) {
10501054 String result = stringEmpty ();
10511055
10521056 switch (majorVersion ) {
@@ -1195,7 +1199,7 @@ Tuple readMethodDescriptor(String descriptor) {
11951199 return tupleOf (dynamicParameters , dynamicReturnType );
11961200}
11971201
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 ) {
11991203 attribute_info * attribute = (attribute_info * ) malloc (sizeof (attribute_info ));
12001204
12011205 for (int i = 0 ; i < size ; i ++ ) {
@@ -1236,7 +1240,7 @@ String readByteCode(u1* code, int* ptr) {
12361240 return itos (byteOf (code [++ * ptr ]));
12371241}
12381242
1239- unsigned short readShortIn (u1 * code , int * ptr ) {
1243+ ushort readShortIn (u1 * code , int * ptr ) {
12401244 u1 first = code [++ * ptr ];
12411245 u1 second = code [++ * ptr ];
12421246
@@ -1282,7 +1286,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
12821286 for (int i = 0 ; i < intOf (code .code_length ); i ++ ) {
12831287 append (& result , stringOf ("\t\t" , 2 ));
12841288
1285- unsigned char next = byteOf (code .code [i ]);
1289+ ubyte next = byteOf (code .code [i ]);
12861290 String typeInfo = stringEmpty ();
12871291
12881292 switch (next ) {
@@ -1736,7 +1740,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
17361740 case 0xba :
17371741 append (& result , stringOf ("invokedynamic" , 13 ));
17381742
1739- unsigned short cnst = readShortIn (code .code , & i );
1743+ ushort cnst = readShortIn (code .code , & i );
17401744
17411745 append (& result , stringOf (" $" , 2 ));
17421746 append (& result , itos (cnst ));
@@ -1749,7 +1753,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
17491753 case 0xb9 :
17501754 append (& result , stringOf ("invokeinterface" , 15 ));
17511755
1752- unsigned short cnst_ = readShortIn (code .code , & i );
1756+ ushort cnst_ = readShortIn (code .code , & i );
17531757
17541758 append (& result , stringOf (" $" , 2 ));
17551759 append (& result , itos (cnst_ ));
@@ -1764,7 +1768,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
17641768 case 0xb7 :
17651769 append (& result , stringOf ("invokespecial" , 13 ));
17661770
1767- unsigned short cnst__ = readShortIn (code .code , & i );
1771+ ushort cnst__ = readShortIn (code .code , & i );
17681772
17691773 append (& result , stringOf (" $" , 2 ));
17701774 append (& result , itos (cnst__ ));
@@ -1775,7 +1779,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
17751779 case 0xb8 :
17761780 append (& result , stringOf ("invokestatic" , 12 ));
17771781
1778- unsigned short cnst___ = readShortIn (code .code , & i );
1782+ ushort cnst___ = readShortIn (code .code , & i );
17791783
17801784 append (& result , stringOf (" $" , 2 ));
17811785 append (& result , itos (cnst___ ));
@@ -1786,7 +1790,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
17861790 case 0xb6 :
17871791 append (& result , stringOf ("invokevirtual" , 13 ));
17881792
1789- unsigned short cnst____ = readShortIn (code .code , & i );
1793+ ushort cnst____ = readShortIn (code .code , & i );
17901794
17911795 append (& result , stringOf (" $" , 2 ));
17921796 append (& result , itos (cnst____ ));
@@ -1920,16 +1924,16 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
19201924
19211925 i += (i + 1 ) % 4 ; // 0-3 byte pad
19221926
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 ]);
19271931 int defaultByte = (defaultByte1 << 24 ) | (defaultByte2 << 16 ) | (defaultByte3 << 8 ) | defaultByte4 ;
19281932
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 ]);
19331937 int npairs = (npairs1 << 24 ) | (npairs2 << 16 ) | (npairs3 << 8 ) | npairs4 ;
19341938
19351939 append (& result , stringOf (" $" , 2 ));
@@ -2001,7 +2005,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
20012005 append (& result , stringOf ("new" , 3 ));
20022006 append (& result , stringOf (" $" , 2 ));
20032007
2004- unsigned short cnst_____ = readShortIn (code .code , & i );
2008+ ushort cnst_____ = readShortIn (code .code , & i );
20052009
20062010 append (& result , itos (cnst_____ ));
20072011
@@ -2090,22 +2094,22 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
20902094
20912095 i += (i + 1 ) % 4 ; // 0-3 byte pad
20922096
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 ]);
20972101 int defaultByte_ = (defaultByte1_ << 24 ) | (defaultByte2_ << 16 ) | (defaultByte3_ << 8 ) | defaultByte4_ ;
20982102
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 ]);
21032107 int lowbyte = (lowbyte1 << 24 ) | (lowbyte2 << 16 ) | (lowbyte3 << 8 ) | lowbyte4 ;
21042108
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 ]);
21092113 int highbyte = (highbyte1 << 24 ) | (highbyte2 << 16 ) | (highbyte3 << 8 ) | highbyte4 ;
21102114
21112115 append (& result , stringOf (" $" , 2 ));
@@ -2123,7 +2127,7 @@ String readCode(Code_attribute code, cp_info** constant_pool) {
21232127 case 0xc4 :
21242128 append (& result , stringOf ("wide" , 4 ));
21252129
2126- unsigned char opcode = byteOf (code .code [++ i ]);
2130+ ubyte opcode = byteOf (code .code [++ i ]);
21272131
21282132 append (& result , stringOf (" $" , 2 ));
21292133 append (& result , itos (opcode ));
@@ -2188,7 +2192,7 @@ char* writeClassFile(ClassFile class) {
21882192
21892193 append (& output , className );
21902194
2191- unsigned short superClass = shortOf (class .super_class );
2195+ ushort superClass = shortOf (class .super_class );
21922196
21932197 if (superClass != 0 ) {
21942198 append (& output , stringOf (" extends " , 9 ));
@@ -2202,7 +2206,7 @@ char* writeClassFile(ClassFile class) {
22022206 append (& output , stringOf (superName , shortOf (super .length )));
22032207 }
22042208
2205- unsigned short interfacesCount = shortOf (class .interfaces_count );
2209+ ushort interfacesCount = shortOf (class .interfaces_count );
22062210
22072211 if (interfacesCount != 0 ) {
22082212 append (& output , stringOf (" implements " , 12 ));
0 commit comments