author | ksrini |
Tue, 29 May 2012 14:56:48 -0700 | |
changeset 12857 | 0a5f341c2a28 |
parent 10115 | eb08d08c7ef7 |
child 17490 | 46864558d068 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10115 | 2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.java.util.jar.pack; |
|
27 |
||
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
28 |
import com.sun.java.util.jar.pack.ConstantPool.Entry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
29 |
import java.util.AbstractCollection; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
30 |
import java.util.ArrayList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
31 |
import java.util.Collection; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
32 |
import java.util.Iterator; |
2 | 33 |
|
34 |
/** |
|
35 |
* Collection of relocatable constant pool references. |
|
36 |
* It operates with respect to a particular byte array, |
|
37 |
* and stores some of its state in the bytes themselves. |
|
38 |
* <p> |
|
39 |
* As a Collection, it can be iterated over, but it is not a List, |
|
40 |
* since it does not natively support indexed access. |
|
41 |
* <p> |
|
42 |
* |
|
43 |
* @author John Rose |
|
44 |
*/ |
|
10115 | 45 |
final class Fixups extends AbstractCollection<Fixups.Fixup> { |
2 | 46 |
byte[] bytes; // the subject of the relocations |
47 |
int head; // desc locating first reloc |
|
48 |
int tail; // desc locating last reloc |
|
49 |
int size; // number of relocations |
|
50 |
Entry[] entries; // [0..size-1] relocations |
|
51 |
int[] bigDescs; // descs which cannot be stored in the bytes |
|
52 |
||
53 |
// A "desc" (descriptor) is a bit-encoded pair of a location |
|
54 |
// and format. Every fixup occurs at a "desc". Until final |
|
55 |
// patching, bytes addressed by descs may also be used to |
|
56 |
// link this data structure together. If the bytes are missing, |
|
57 |
// or if the "desc" is too large to encode in the bytes, |
|
58 |
// it is kept in the bigDescs array. |
|
59 |
||
60 |
Fixups(byte[] bytes) { |
|
61 |
this.bytes = bytes; |
|
62 |
entries = new Entry[3]; |
|
63 |
bigDescs = noBigDescs; |
|
64 |
} |
|
65 |
Fixups() { |
|
66 |
// If there are no bytes, all descs are kept in bigDescs. |
|
67 |
this((byte[])null); |
|
68 |
} |
|
10115 | 69 |
Fixups(byte[] bytes, Collection<Fixup> fixups) { |
2 | 70 |
this(bytes); |
71 |
addAll(fixups); |
|
72 |
} |
|
10115 | 73 |
Fixups(Collection<Fixup> fixups) { |
2 | 74 |
this((byte[])null); |
75 |
addAll(fixups); |
|
76 |
} |
|
77 |
||
78 |
private static final int MINBIGSIZE = 1; |
|
79 |
// cleverly share empty bigDescs: |
|
80 |
private static int[] noBigDescs = {MINBIGSIZE}; |
|
81 |
||
82 |
public int size() { |
|
83 |
return size; |
|
84 |
} |
|
85 |
||
86 |
public void trimToSize() { |
|
87 |
if (size != entries.length) { |
|
88 |
Entry[] oldEntries = entries; |
|
89 |
entries = new Entry[size]; |
|
90 |
System.arraycopy(oldEntries, 0, entries, 0, size); |
|
91 |
} |
|
92 |
int bigSize = bigDescs[BIGSIZE]; |
|
93 |
if (bigSize == MINBIGSIZE) { |
|
94 |
bigDescs = noBigDescs; |
|
95 |
} else if (bigSize != bigDescs.length) { |
|
96 |
int[] oldBigDescs = bigDescs; |
|
97 |
bigDescs = new int[bigSize]; |
|
98 |
System.arraycopy(oldBigDescs, 0, bigDescs, 0, bigSize); |
|
99 |
} |
|
100 |
} |
|
101 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
102 |
public void visitRefs(Collection<Entry> refs) { |
2 | 103 |
for (int i = 0; i < size; i++) { |
104 |
refs.add(entries[i]); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
public void clear() { |
|
109 |
if (bytes != null) { |
|
110 |
// Clean the bytes: |
|
10115 | 111 |
for (Fixup fx : this) { |
2 | 112 |
//System.out.println("clean "+fx); |
113 |
storeIndex(fx.location(), fx.format(), 0); |
|
114 |
} |
|
115 |
} |
|
116 |
size = 0; |
|
117 |
if (bigDescs != noBigDescs) |
|
118 |
bigDescs[BIGSIZE] = MINBIGSIZE; |
|
119 |
// do not trim to size, however |
|
120 |
} |
|
121 |
||
122 |
public byte[] getBytes() { |
|
123 |
return bytes; |
|
124 |
} |
|
125 |
||
126 |
public void setBytes(byte[] newBytes) { |
|
127 |
if (bytes == newBytes) return; |
|
10115 | 128 |
ArrayList<Fixup> old = null; |
129 |
assert((old = new ArrayList<>(this)) != null); |
|
2 | 130 |
if (bytes == null || newBytes == null) { |
131 |
// One or the other representations is deficient. |
|
132 |
// Construct a checkpoint. |
|
10115 | 133 |
ArrayList<Fixup> save = new ArrayList<>(this); |
2 | 134 |
clear(); |
135 |
bytes = newBytes; |
|
136 |
addAll(save); |
|
137 |
} else { |
|
138 |
// assume newBytes is some sort of bitwise copy of the old bytes |
|
139 |
bytes = newBytes; |
|
140 |
} |
|
10115 | 141 |
assert(old.equals(new ArrayList<>(this))); |
2 | 142 |
} |
143 |
||
144 |
static final int LOC_SHIFT = 1; |
|
145 |
static final int FMT_MASK = 0x1; |
|
146 |
static final byte UNUSED_BYTE = 0; |
|
147 |
static final byte OVERFLOW_BYTE = -1; |
|
148 |
// fill pointer of bigDescs array is in element [0] |
|
149 |
static final int BIGSIZE = 0; |
|
150 |
||
151 |
// Format values: |
|
152 |
public static final int U2_FORMAT = 0; |
|
153 |
public static final int U1_FORMAT = 1; |
|
154 |
||
155 |
// Special values for the static methods. |
|
156 |
private static final int SPECIAL_LOC = 0; |
|
157 |
private static final int SPECIAL_FMT = U2_FORMAT; |
|
158 |
||
159 |
static int fmtLen(int fmt) { return 1+(fmt-U1_FORMAT)/(U2_FORMAT-U1_FORMAT); } |
|
160 |
static int descLoc(int desc) { return desc >>> LOC_SHIFT; } |
|
161 |
static int descFmt(int desc) { return desc & FMT_MASK; } |
|
162 |
static int descEnd(int desc) { return descLoc(desc) + fmtLen(descFmt(desc)); } |
|
163 |
static int makeDesc(int loc, int fmt) { |
|
164 |
int desc = (loc << LOC_SHIFT) | fmt; |
|
165 |
assert(descLoc(desc) == loc); |
|
166 |
assert(descFmt(desc) == fmt); |
|
167 |
return desc; |
|
168 |
} |
|
169 |
int fetchDesc(int loc, int fmt) { |
|
170 |
byte b1 = bytes[loc]; |
|
171 |
assert(b1 != OVERFLOW_BYTE); |
|
172 |
int value; |
|
173 |
if (fmt == U2_FORMAT) { |
|
174 |
byte b2 = bytes[loc+1]; |
|
175 |
value = ((b1 & 0xFF) << 8) + (b2 & 0xFF); |
|
176 |
} else { |
|
177 |
value = (b1 & 0xFF); |
|
178 |
} |
|
179 |
// Stored loc field is difference between its own loc and next loc. |
|
180 |
return value + (loc << LOC_SHIFT); |
|
181 |
} |
|
182 |
boolean storeDesc(int loc, int fmt, int desc) { |
|
183 |
if (bytes == null) |
|
184 |
return false; |
|
185 |
int value = desc - (loc << LOC_SHIFT); |
|
186 |
byte b1, b2; |
|
187 |
switch (fmt) { |
|
188 |
case U2_FORMAT: |
|
189 |
assert(bytes[loc+0] == UNUSED_BYTE); |
|
190 |
assert(bytes[loc+1] == UNUSED_BYTE); |
|
191 |
b1 = (byte)(value >> 8); |
|
192 |
b2 = (byte)(value >> 0); |
|
193 |
if (value == (value & 0xFFFF) && b1 != OVERFLOW_BYTE) { |
|
194 |
bytes[loc+0] = b1; |
|
195 |
bytes[loc+1] = b2; |
|
196 |
assert(fetchDesc(loc, fmt) == desc); |
|
197 |
return true; |
|
198 |
} |
|
199 |
break; |
|
200 |
case U1_FORMAT: |
|
201 |
assert(bytes[loc] == UNUSED_BYTE); |
|
202 |
b1 = (byte)value; |
|
203 |
if (value == (value & 0xFF) && b1 != OVERFLOW_BYTE) { |
|
204 |
bytes[loc] = b1; |
|
205 |
assert(fetchDesc(loc, fmt) == desc); |
|
206 |
return true; |
|
207 |
} |
|
208 |
break; |
|
209 |
default: assert(false); |
|
210 |
} |
|
211 |
// Failure. Caller must allocate a bigDesc. |
|
212 |
bytes[loc] = OVERFLOW_BYTE; |
|
213 |
assert(fmt==U1_FORMAT || (bytes[loc+1]=(byte)bigDescs[BIGSIZE])!=999); |
|
214 |
return false; |
|
215 |
} |
|
216 |
void storeIndex(int loc, int fmt, int value) { |
|
217 |
storeIndex(bytes, loc, fmt, value); |
|
218 |
} |
|
219 |
static |
|
220 |
void storeIndex(byte[] bytes, int loc, int fmt, int value) { |
|
221 |
switch (fmt) { |
|
222 |
case U2_FORMAT: |
|
223 |
assert(value == (value & 0xFFFF)) : (value); |
|
224 |
bytes[loc+0] = (byte)(value >> 8); |
|
225 |
bytes[loc+1] = (byte)(value >> 0); |
|
226 |
break; |
|
227 |
case U1_FORMAT: |
|
228 |
assert(value == (value & 0xFF)) : (value); |
|
229 |
bytes[loc] = (byte)value; |
|
230 |
break; |
|
231 |
default: assert(false); |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
/** Simple and necessary tuple to present each fixup. */ |
|
236 |
public static |
|
10115 | 237 |
class Fixup implements Comparable<Fixup> { |
2 | 238 |
int desc; // location and format of reloc |
239 |
Entry entry; // which entry to plug into the bytes |
|
240 |
Fixup(int desc, Entry entry) { |
|
241 |
this.desc = desc; |
|
242 |
this.entry = entry; |
|
243 |
} |
|
244 |
public Fixup(int loc, int fmt, Entry entry) { |
|
245 |
this.desc = makeDesc(loc, fmt); |
|
246 |
this.entry = entry; |
|
247 |
} |
|
248 |
public int location() { return descLoc(desc); } |
|
249 |
public int format() { return descFmt(desc); } |
|
250 |
public Entry entry() { return entry; } |
|
251 |
public int compareTo(Fixup that) { |
|
252 |
// Ordering depends only on location. |
|
253 |
return this.location() - that.location(); |
|
254 |
} |
|
255 |
public boolean equals(Object x) { |
|
256 |
if (!(x instanceof Fixup)) return false; |
|
257 |
Fixup that = (Fixup) x; |
|
258 |
return this.desc == that.desc && this.entry == that.entry; |
|
259 |
} |
|
260 |
public String toString() { |
|
261 |
return "@"+location()+(format()==U1_FORMAT?".1":"")+"="+entry; |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
private |
|
10115 | 266 |
class Itr implements Iterator<Fixup> { |
2 | 267 |
int index = 0; // index into entries |
268 |
int bigIndex = BIGSIZE+1; // index into bigDescs |
|
269 |
int next = head; // desc pointing to next fixup |
|
270 |
public boolean hasNext() { return index < size; } |
|
271 |
public void remove() { throw new UnsupportedOperationException(); } |
|
10115 | 272 |
public Fixup next() { |
2 | 273 |
int thisIndex = index; |
274 |
return new Fixup(nextDesc(), entries[thisIndex]); |
|
275 |
} |
|
276 |
int nextDesc() { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
277 |
index++; |
2 | 278 |
int thisDesc = next; |
279 |
if (index < size) { |
|
280 |
// Fetch next desc eagerly, in case this fixup gets finalized. |
|
281 |
int loc = descLoc(thisDesc); |
|
282 |
int fmt = descFmt(thisDesc); |
|
283 |
if (bytes != null && bytes[loc] != OVERFLOW_BYTE) { |
|
284 |
next = fetchDesc(loc, fmt); |
|
285 |
} else { |
|
286 |
// The unused extra byte is "asserted" to be equal to BI. |
|
287 |
// This helps keep the overflow descs in sync. |
|
288 |
assert(fmt==U1_FORMAT || bytes == null || bytes[loc+1]==(byte)bigIndex); |
|
289 |
next = bigDescs[bigIndex++]; |
|
290 |
} |
|
291 |
} |
|
292 |
return thisDesc; |
|
293 |
} |
|
294 |
} |
|
295 |
||
10115 | 296 |
public Iterator<Fixup> iterator() { |
2 | 297 |
return new Itr(); |
298 |
} |
|
299 |
public void add(int location, int format, Entry entry) { |
|
300 |
addDesc(makeDesc(location, format), entry); |
|
301 |
} |
|
302 |
public boolean add(Fixup f) { |
|
303 |
addDesc(f.desc, f.entry); |
|
304 |
return true; |
|
305 |
} |
|
10115 | 306 |
|
307 |
public boolean addAll(Collection<? extends Fixup> c) { |
|
2 | 308 |
if (c instanceof Fixups) { |
309 |
// Use knowledge of Itr structure to avoid building little structs. |
|
310 |
Fixups that = (Fixups) c; |
|
311 |
if (that.size == 0) return false; |
|
312 |
if (this.size == 0 && entries.length < that.size) |
|
313 |
growEntries(that.size); // presize exactly |
|
314 |
Entry[] thatEntries = that.entries; |
|
315 |
for (Itr i = that.new Itr(); i.hasNext(); ) { |
|
316 |
int ni = i.index; |
|
317 |
addDesc(i.nextDesc(), thatEntries[ni]); |
|
318 |
} |
|
319 |
return true; |
|
320 |
} else { |
|
321 |
return super.addAll(c); |
|
322 |
} |
|
323 |
} |
|
324 |
// Here is how things get added: |
|
325 |
private void addDesc(int thisDesc, Entry entry) { |
|
326 |
if (entries.length == size) |
|
327 |
growEntries(size * 2); |
|
328 |
entries[size] = entry; |
|
329 |
if (size == 0) { |
|
330 |
head = tail = thisDesc; |
|
331 |
} else { |
|
332 |
int prevDesc = tail; |
|
333 |
// Store new desc in previous tail. |
|
334 |
int prevLoc = descLoc(prevDesc); |
|
335 |
int prevFmt = descFmt(prevDesc); |
|
336 |
int prevLen = fmtLen(prevFmt); |
|
337 |
int thisLoc = descLoc(thisDesc); |
|
338 |
// The collection must go in ascending order, and not overlap. |
|
339 |
if (thisLoc < prevLoc + prevLen) |
|
340 |
badOverlap(thisLoc); |
|
341 |
tail = thisDesc; |
|
342 |
if (!storeDesc(prevLoc, prevFmt, thisDesc)) { |
|
343 |
// overflow |
|
344 |
int bigSize = bigDescs[BIGSIZE]; |
|
345 |
if (bigDescs.length == bigSize) |
|
346 |
growBigDescs(); |
|
347 |
//System.out.println("bigDescs["+bigSize+"] = "+thisDesc); |
|
348 |
bigDescs[bigSize++] = thisDesc; |
|
349 |
bigDescs[BIGSIZE] = bigSize; |
|
350 |
} |
|
351 |
} |
|
352 |
size += 1; |
|
353 |
} |
|
354 |
private void badOverlap(int thisLoc) { |
|
355 |
throw new IllegalArgumentException("locs must be ascending and must not overlap: "+thisLoc+" >> "+this); |
|
356 |
} |
|
357 |
||
358 |
private void growEntries(int newSize) { |
|
359 |
Entry[] oldEntries = entries; |
|
360 |
entries = new Entry[Math.max(3, newSize)]; |
|
361 |
System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length); |
|
362 |
} |
|
363 |
private void growBigDescs() { |
|
364 |
int[] oldBigDescs = bigDescs; |
|
365 |
bigDescs = new int[oldBigDescs.length * 2]; |
|
366 |
System.arraycopy(oldBigDescs, 0, bigDescs, 0, oldBigDescs.length); |
|
367 |
} |
|
368 |
||
369 |
/// Static methods that optimize the use of this class. |
|
370 |
public static |
|
371 |
Object add(Object prevFixups, |
|
372 |
byte[] bytes, int loc, int fmt, |
|
373 |
Entry e) { |
|
374 |
Fixups f; |
|
375 |
if (prevFixups == null) { |
|
376 |
if (loc == SPECIAL_LOC && fmt == SPECIAL_FMT) { |
|
377 |
// Special convention: If the attribute has a |
|
378 |
// U2 relocation at position zero, store the Entry |
|
379 |
// rather than building a Fixups structure. |
|
380 |
return e; |
|
381 |
} |
|
382 |
f = new Fixups(bytes); |
|
383 |
} else if (!(prevFixups instanceof Fixups)) { |
|
384 |
// Recognize the special convention: |
|
385 |
Entry firstEntry = (Entry) prevFixups; |
|
386 |
f = new Fixups(bytes); |
|
387 |
f.add(SPECIAL_LOC, SPECIAL_FMT, firstEntry); |
|
388 |
} else { |
|
389 |
f = (Fixups) prevFixups; |
|
390 |
assert(f.bytes == bytes); |
|
391 |
} |
|
392 |
f.add(loc, fmt, e); |
|
393 |
return f; |
|
394 |
} |
|
395 |
||
396 |
public static |
|
397 |
void setBytes(Object fixups, byte[] bytes) { |
|
398 |
if (fixups instanceof Fixups) { |
|
399 |
Fixups f = (Fixups) fixups; |
|
400 |
f.setBytes(bytes); |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
public static |
|
405 |
Object trimToSize(Object fixups) { |
|
406 |
if (fixups instanceof Fixups) { |
|
407 |
Fixups f = (Fixups) fixups; |
|
408 |
f.trimToSize(); |
|
409 |
if (f.size() == 0) |
|
410 |
fixups = null; |
|
411 |
} |
|
412 |
return fixups; |
|
413 |
} |
|
414 |
||
415 |
// Iterate over all the references in this set of fixups. |
|
416 |
public static |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
417 |
void visitRefs(Object fixups, Collection<Entry> refs) { |
2 | 418 |
if (fixups == null) { |
419 |
} else if (!(fixups instanceof Fixups)) { |
|
420 |
// Special convention; see above. |
|
421 |
refs.add((Entry) fixups); |
|
422 |
} else { |
|
423 |
Fixups f = (Fixups) fixups; |
|
424 |
f.visitRefs(refs); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
// Clear out this set of fixups by replacing each reference |
|
429 |
// by a hardcoded coding of its reference, drawn from ix. |
|
430 |
public static |
|
431 |
void finishRefs(Object fixups, byte[] bytes, ConstantPool.Index ix) { |
|
432 |
if (fixups == null) |
|
433 |
return; |
|
434 |
if (!(fixups instanceof Fixups)) { |
|
435 |
// Special convention; see above. |
|
436 |
int index = ix.indexOf((Entry) fixups); |
|
437 |
storeIndex(bytes, SPECIAL_LOC, SPECIAL_FMT, index); |
|
438 |
return; |
|
439 |
} |
|
440 |
Fixups f = (Fixups) fixups; |
|
441 |
assert(f.bytes == bytes); |
|
442 |
f.finishRefs(ix); |
|
443 |
} |
|
444 |
||
445 |
void finishRefs(ConstantPool.Index ix) { |
|
446 |
if (isEmpty()) |
|
447 |
return; |
|
10115 | 448 |
for (Fixup fx : this) { |
2 | 449 |
int index = ix.indexOf(fx.entry); |
450 |
//System.out.println("finish "+fx+" = "+index); |
|
451 |
// Note that the iterator has already fetched the |
|
452 |
// bytes we are about to overwrite. |
|
453 |
storeIndex(fx.location(), fx.format(), index); |
|
454 |
} |
|
455 |
// Further iterations should do nothing: |
|
456 |
bytes = null; // do not clean them |
|
457 |
clear(); |
|
458 |
} |
|
459 |
||
460 |
/* |
|
461 |
/// Testing. |
|
462 |
public static void main(String[] av) { |
|
463 |
byte[] bytes = new byte[1 << 20]; |
|
464 |
ConstantPool cp = new ConstantPool(); |
|
465 |
Fixups f = new Fixups(bytes); |
|
466 |
boolean isU1 = false; |
|
467 |
int span = 3; |
|
468 |
int nextLoc = 0; |
|
469 |
int[] locs = new int[100]; |
|
470 |
final int[] indexes = new int[100]; |
|
471 |
int iptr = 1; |
|
472 |
for (int loc = 0; loc < bytes.length; loc++) { |
|
473 |
if (loc == nextLoc && loc+1 < bytes.length) { |
|
474 |
int fmt = (isU1 ? U1_FORMAT : U2_FORMAT); |
|
475 |
Entry e = ConstantPool.getUtf8Entry("L"+loc); |
|
476 |
f.add(loc, fmt, e); |
|
477 |
isU1 ^= true; |
|
478 |
if (iptr < 10) { |
|
479 |
// Make it close in. |
|
480 |
nextLoc += fmtLen(fmt) + (iptr < 5 ? 0 : 1); |
|
481 |
} else { |
|
482 |
nextLoc += span; |
|
483 |
span = (int)(span * 1.77); |
|
484 |
} |
|
485 |
// Here are the bytes that would have gone here: |
|
486 |
locs[iptr] = loc; |
|
487 |
if (fmt == U1_FORMAT) { |
|
488 |
indexes[iptr++] = (loc & 0xFF); |
|
489 |
} else { |
|
490 |
indexes[iptr++] = ((loc & 0xFF) << 8) | ((loc+1) & 0xFF); |
|
491 |
++loc; // skip a byte |
|
492 |
} |
|
493 |
continue; |
|
494 |
} |
|
495 |
bytes[loc] = (byte)loc; |
|
496 |
} |
|
497 |
System.out.println("size="+f.size() |
|
498 |
+" overflow="+(f.bigDescs[BIGSIZE]-1)); |
|
499 |
System.out.println("Fixups: "+f); |
|
500 |
// Test collection contents. |
|
501 |
assert(iptr == 1+f.size()); |
|
502 |
List l = new ArrayList(f); |
|
503 |
Collections.sort(l); // should not change the order |
|
504 |
if (!l.equals(new ArrayList(f))) System.out.println("** disordered"); |
|
505 |
f.setBytes(null); |
|
506 |
if (!l.equals(new ArrayList(f))) System.out.println("** bad set 1"); |
|
507 |
f.setBytes(bytes); |
|
508 |
if (!l.equals(new ArrayList(f))) System.out.println("** bad set 2"); |
|
509 |
Fixups f3 = new Fixups(f); |
|
510 |
if (!l.equals(new ArrayList(f3))) System.out.println("** bad set 3"); |
|
511 |
Iterator fi = f.iterator(); |
|
512 |
for (int i = 1; i < iptr; i++) { |
|
513 |
Fixup fx = (Fixup) fi.next(); |
|
514 |
if (fx.location() != locs[i]) { |
|
515 |
System.out.println("** "+fx+" != "+locs[i]); |
|
516 |
} |
|
517 |
if (fx.format() == U1_FORMAT) |
|
518 |
System.out.println(fx+" -> "+bytes[locs[i]]); |
|
519 |
else |
|
520 |
System.out.println(fx+" -> "+bytes[locs[i]]+" "+bytes[locs[i]+1]); |
|
521 |
} |
|
522 |
assert(!fi.hasNext()); |
|
523 |
indexes[0] = 1; // like iptr |
|
524 |
Index ix = new Index("ix") { |
|
525 |
public int indexOf(Entry e) { |
|
526 |
return indexes[indexes[0]++]; |
|
527 |
} |
|
528 |
}; |
|
529 |
f.finishRefs(ix); |
|
530 |
for (int loc = 0; loc < bytes.length; loc++) { |
|
531 |
if (bytes[loc] != (byte)loc) { |
|
532 |
System.out.println("** ["+loc+"] = "+bytes[loc]+" != "+(byte)loc); |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
//*/ |
|
537 |
} |