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