Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / classfmt / ClassFileStruct.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.classfmt;
12
13 abstract public class ClassFileStruct implements ClassFileConstants {
14         byte[] reference;
15         int structOffset;
16 public ClassFileStruct(byte classFileBytes[], int off) {
17         reference = classFileBytes;
18         structOffset = off;
19 }
20 public ClassFileStruct (byte classFileBytes[], int off, boolean verifyStructure) {
21         reference = classFileBytes;
22         structOffset = off;
23 }
24 public double doubleAt(int relativeOffset) {
25         return (Double.longBitsToDouble(this.i8At(relativeOffset)));
26 }
27 public float floatAt(int relativeOffset) {
28         return (Float.intBitsToFloat(this.i4At(relativeOffset)));
29 }
30 public int i1At(int relativeOffset) {
31         return reference[relativeOffset + structOffset];
32 }
33 public int i2At(int relativeOffset) {
34         int position = relativeOffset + structOffset;
35         return (reference[position++] << 8) + (reference[position] & 0xFF);
36 }
37 public int i4At(int relativeOffset) {
38         int position = relativeOffset + structOffset;
39         return ((reference[position++] & 0xFF) << 24) + ((reference[position++] & 0xFF) << 16) + ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF);
40 }
41 public long i8At(int relativeOffset) {
42         int position = relativeOffset + structOffset;
43         return (((long) (reference[position++] & 0xFF)) << 56) 
44                                         + (((long) (reference[position++] & 0xFF)) << 48) 
45                                         + (((long) (reference[position++] & 0xFF)) << 40) 
46                                         + (((long) (reference[position++] & 0xFF)) << 32) 
47                                         + (((long) (reference[position++] & 0xFF)) << 24) 
48                                         + (((long) (reference[position++] & 0xFF)) << 16) 
49                                         + (((long) (reference[position++] & 0xFF)) << 8) 
50                                         + (reference[position++] & 0xFF);
51 }
52 public static String printTypeModifiers(int modifiers) {
53
54         java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
55         java.io.PrintWriter print = new java.io.PrintWriter(out);
56
57         if ((modifiers & AccPublic) != 0) print.print("public "); //$NON-NLS-1$
58         if ((modifiers & AccPrivate) != 0) print.print("private "); //$NON-NLS-1$
59         if ((modifiers & AccFinal) != 0) print.print("final "); //$NON-NLS-1$
60         if ((modifiers & AccSuper) != 0) print.print("super "); //$NON-NLS-1$
61         if ((modifiers & AccInterface) != 0) print.print("interface "); //$NON-NLS-1$
62         if ((modifiers & AccAbstract) != 0) print.print("abstract "); //$NON-NLS-1$
63         print.flush();
64         return out.toString();
65 }
66 public int u1At(int relativeOffset) {
67         return (reference[relativeOffset + structOffset] & 0xFF);
68 }
69 public int u2At(int relativeOffset) {
70         int position = relativeOffset + structOffset;
71         return ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF);
72 }
73 public long u4At(int relativeOffset) {
74         int position = relativeOffset + structOffset;
75         return (((reference[position++] & 0xFFL) << 24) + ((reference[position++] & 0xFF) << 16) + ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF));
76 }
77 public char[] utf8At(int relativeOffset, int bytesAvailable) {
78         int length = bytesAvailable;
79         char outputBuf[] = new char[bytesAvailable];
80         int outputPos = 0;
81         int readOffset = this.structOffset + relativeOffset;
82         
83         while (length != 0) {
84                 int x = this.reference[readOffset++] & 0xFF;
85                 length--;
86                 if ((0x80 & x) != 0) {
87                         if ((x & 0x20) != 0) {
88                                 length-=2;
89                                 x = ((x & 0xF) << 12) | ((this.reference[readOffset++] & 0x3F) << 6) | (this.reference[readOffset++] & 0x3F);
90                         } else {
91                                 length--;
92                                 x = ((x & 0x1F) << 6) | (this.reference[readOffset++] & 0x3F);
93                         }
94                 }
95                 outputBuf[outputPos++] = (char) x;
96         }
97
98         if (outputPos != bytesAvailable) {
99                 System.arraycopy(outputBuf, 0, (outputBuf = new char[outputPos]), 0, outputPos);
100         }
101         return outputBuf;
102 }
103
104 protected void reset() {
105         this.reference = null;
106 }
107
108 public char[] utf8At(int relativeOffset, int bytesAvailable, boolean testValidity) throws ClassFormatException {
109         int x, y, z;
110         int length = bytesAvailable;
111         char outputBuf[] = new char[bytesAvailable];
112         int outputPos = 0;
113         int readOffset = structOffset + relativeOffset;
114         
115         while (length != 0) {
116                 x = reference[readOffset++] & 0xFF;
117                 length--;
118                 if ((0x80 & x) != 0) {
119                         if (testValidity) {
120                                 if ((0x40 & x) == 0) {
121                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
122                                 }
123                                 if (length < 1) {
124                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
125                                 }
126                         }
127                         y = this.reference[readOffset++] & 0xFF;
128                         length--;
129                         if (testValidity) {
130                                 if ((y & 0xC0) != 0x80) {
131                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
132                                 }
133                         }
134                         if ((x & 0x20) != 0) {
135                                 if (testValidity && (length < 1)) {
136                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
137                                 }
138                                 z = this.reference[readOffset++] & 0xFF;
139                                 length--;
140                                 if (testValidity && ((z & 0xC0) != 0x80)) {
141                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
142                                 }
143                                 x = ((x & 0x1F) << 12) + ((y & 0x3F) << 6) + (z & 0x3F);
144                                 if (testValidity && (x < 0x0800)) {
145                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
146                                 }
147                         } else {
148                                 x = ((x & 0x1F) << 6) + (y & 0x3F);
149                                 if (testValidity && !((x == 0) || (x >= 0x80))) {
150                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
151                                 }
152                         }
153                 } else {
154                         if (testValidity && x == 0) {
155                                         throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
156                         }
157                 }
158                 outputBuf[outputPos++] = (char) x;
159         }
160
161         if (outputPos != bytesAvailable) {
162                 System.arraycopy(outputBuf, 0, (outputBuf = new char[outputPos]), 0, outputPos);
163         }
164         return outputBuf;
165 }
166 public static void verifyMethodNameAndSignature(char[] name, char[] signature) throws ClassFormatException {
167
168         // ensure name is not empty 
169         if (name.length == 0) {
170                 throw new ClassFormatException(ClassFormatException.ErrInvalidMethodName);
171         }
172
173         // if name begins with the < character it must be clinit or init
174         if (name[0] == '<') {
175                 if (new String(name).equals("<clinit>") || new String(name).equals("<init>")) { //$NON-NLS-2$ //$NON-NLS-1$
176                         int signatureLength = signature.length;
177                         if (!((signatureLength > 2)
178                                 && (signature[0] == '(')
179                                 && (signature[signatureLength - 2] == ')')
180                                 && (signature[signatureLength - 1] == 'V'))) {
181                                 throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
182                         }
183                 } else {
184                         throw new ClassFormatException(ClassFormatException.ErrInvalidMethodName);
185                 }
186         }
187 }
188 }