jdk/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java
author ksrini
Fri, 04 Mar 2011 09:32:20 -0800
changeset 8570 c9767adab3d2
parent 7816 55a18147b4bf
child 8803 b3f57bfca459
permissions -rw-r--r--
7023963: (misc) fix diamond anon instances in the jdk Reviewed-by: alanb, mchung, mcimadamore, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8570
c9767adab3d2 7023963: (misc) fix diamond anon instances in the jdk
ksrini
parents: 7816
diff changeset
     2
 * Copyright (c) 2001, 2011, 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: 4919
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: 4919
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: 4919
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4919
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4919
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.ClassEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.Entry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import com.sun.java.util.jar.pack.ConstantPool.Index;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    32
import com.sun.java.util.jar.pack.ConstantPool.MemberEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    33
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    34
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.java.util.jar.pack.Package.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.java.util.jar.pack.Package.InnerClass;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    38
import java.io.ByteArrayOutputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    39
import java.io.EOFException;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    40
import java.io.PrintStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    41
import java.io.FilterInputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    42
import java.io.BufferedInputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    43
import java.io.InputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    44
import java.io.IOException;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    45
import java.util.ArrayList;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    46
import java.util.Map;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    47
import java.util.Arrays;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    48
import java.util.Collection;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    49
import java.util.Comparator;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    50
import java.util.HashSet;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    51
import java.util.HashMap;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    52
import java.util.Iterator;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    53
import java.util.List;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    54
import java.util.ListIterator;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    55
import java.util.Set;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    56
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Reader for a package file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see PackageWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
class PackageReader extends BandStructure {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    Package pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    LimitedBuffer in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    PackageReader(Package pkg, InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.pkg = pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.in = new LimitedBuffer(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** A buffered input stream which is careful not to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *  read its underlying stream ahead of a given mark,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *  called the 'readLimit'.  This property declares
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *  the maximum number of characters that future reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *  can consume from the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    class LimitedBuffer extends BufferedInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        long served;     // total number of charburgers served
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int  servedPos;  // ...as of this value of super.pos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        long limit;      // current declared limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        long buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public boolean atLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            boolean z = (getBytesServed() == limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            assert(!z || limit == buffered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public long getBytesServed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return served + (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public void setReadLimit(long newLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (newLimit == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                limit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                limit = getBytesServed() + newLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public long getReadLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (limit == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                return limit - getBytesServed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (pos < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // fast path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return buf[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            served += (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int ch = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (ch >= 0)  served += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            assert(served <= limit || limit == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            served += (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (nr >= 0)  served += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            assert(served <= limit || limit == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new RuntimeException("no skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        LimitedBuffer(InputStream originalIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            super(null, 1<<14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            super.in = new FilterInputStream(originalIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    if (buffered == limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    ++buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    if (buffered == limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    if (limit != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        long remaining = limit - buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        if (len > remaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            len = (int)remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (nr >= 0)  buffered += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            //  pack200_archive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            //        file_header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            //        *band_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            //        cp_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            //        attr_definition_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            //        ic_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            //        class_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            //        bc_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            //        file_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            readFileHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            readBandHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            readConstantPool();  // cp_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            readAttrDefs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            readInnerClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Class[] classes = readClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            readByteCodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            readFiles();     // file_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            assert(archiveSize1 == 0 || in.atLimit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            assert(archiveSize1 == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                   in.getBytesServed() == archiveSize0+archiveSize1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            all_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // As a post-pass, build constant pools and inner classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                reconstructClass(classes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (Exception ee) {
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   187
            Utils.log.warning("Error on input: "+ee, ee);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                Utils.log.info("Stream offsets:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                 " served="+in.getBytesServed()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                 " buffered="+in.buffered+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                 " limit="+in.limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            //if (verbose > 0)  ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (ee instanceof IOException)  throw (IOException)ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (ee instanceof RuntimeException)  throw (RuntimeException)ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new Error("error unpacking", ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // Temporary count values, until band decoding gets rolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    int[] tagCount = new int[CONSTANT_Limit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    int numFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    int numAttrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    int numInnerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    int numClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    void readFileHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //  file_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        //        archive_magic archive_header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        readArchiveMagic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        readArchiveHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // Local routine used to parse fixed-format scalars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // in the file_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private int getMagicInt32() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int res = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            res <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            res |= (archive_magic.getByte() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    final static int MAGIC_BYTES = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    void readArchiveMagic() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Read a minimum of bytes in the first gulp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        in.setReadLimit(MAGIC_BYTES + AH_LENGTH_MIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        //  archive_magic:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //        #archive_magic_word :BYTE1[4]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        archive_magic.expectLength(MAGIC_BYTES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        archive_magic.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // read and check magic numbers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        pkg.magic = getMagicInt32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        archive_magic.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    void readArchiveHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //  archive_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        //        #archive_minver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //        #archive_majver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        //        #archive_options :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        //        (archive_file_counts) ** (#have_file_headers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //        (archive_special_counts) ** (#have_special_formats)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        //        cp_counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        //        class_counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        //  archive_file_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //        #archive_size_hi :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        //        #archive_size_lo :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        //        #archive_next_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        //        #archive_modtime :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        //        #file_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        //  class_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        //        #ic_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        //        #default_class_minver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        //        #default_class_majver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        //        #class_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        //  archive_special_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        //        #band_headers_size :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        //        #attr_definition_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        assert(AH_LENGTH == 8+(ConstantPool.TAGS_IN_ORDER.length)+6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        archive_header_0.expectLength(AH_LENGTH_0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        archive_header_0.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        pkg.package_minver = archive_header_0.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        pkg.package_majver = archive_header_0.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        pkg.checkVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        this.initPackageMajver(pkg.package_majver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        archiveOptions = archive_header_0.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        archive_header_0.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // detect archive optional fields in archive header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        boolean haveSpecial = testBit(archiveOptions, AO_HAVE_SPECIAL_FORMATS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        boolean haveFiles   = testBit(archiveOptions, AO_HAVE_FILE_HEADERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean haveNumbers = testBit(archiveOptions, AO_HAVE_CP_NUMBERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        initAttrIndexLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // now we are ready to use the data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        archive_header_S.expectLength(haveFiles? AH_LENGTH_S: 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        archive_header_S.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (haveFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            long sizeHi = archive_header_S.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            long sizeLo = archive_header_S.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            archiveSize1 = (sizeHi << 32) + ((sizeLo << 32) >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // Set the limit, now, up to the file_bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            in.setReadLimit(archiveSize1);  // for debug only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            archiveSize1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            in.setReadLimit(-1);  // remove limitation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        archive_header_S.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        archiveSize0 = in.getBytesServed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int remainingHeaders = AH_LENGTH - AH_LENGTH_0 - AH_LENGTH_S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!haveFiles)    remainingHeaders -= AH_FILE_HEADER_LEN-AH_LENGTH_S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (!haveSpecial)  remainingHeaders -= AH_SPECIAL_FORMAT_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (!haveNumbers)  remainingHeaders -= AH_CP_NUMBER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        assert(remainingHeaders >= AH_LENGTH_MIN - AH_LENGTH_0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        archive_header_1.expectLength(remainingHeaders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        archive_header_1.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (haveFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            archiveNextCount = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            pkg.default_modtime = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            numFiles = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            archiveNextCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            numFiles = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (haveSpecial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            band_headers.expectLength(archive_header_1.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            numAttrDefs = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            band_headers.expectLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            numAttrDefs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        readConstantPoolCounts(haveNumbers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        numInnerClasses = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        pkg.default_class_minver = (short) archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        pkg.default_class_majver = (short) archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        numClasses = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        archive_header_1.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // set some derived archive bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (testBit(archiveOptions, AO_DEFLATE_HINT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            pkg.default_options |= FO_DEFLATE_HINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    void readBandHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        band_headers.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        bandHeaderBytePos = 1;  // Leave room to pushback the initial XB byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        bandHeaderBytes = new byte[bandHeaderBytePos + band_headers.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        for (int i = bandHeaderBytePos; i < bandHeaderBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            bandHeaderBytes[i] = (byte) band_headers.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        band_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    void readConstantPoolCounts(boolean haveNumbers) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // size the constant pools:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        for (int k = 0; k < ConstantPool.TAGS_IN_ORDER.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            //  cp_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            //        #cp_Utf8_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            //        (cp_number_counts) ** (#have_cp_numbers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            //        #cp_String_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            //        #cp_Class_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            //        #cp_Signature_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            //        #cp_Descr_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            //        #cp_Field_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            //        #cp_Method_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            //        #cp_Imethod_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            //  cp_number_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            //        #cp_Int_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            //        #cp_Float_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            //        #cp_Long_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            //        #cp_Double_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            byte tag = ConstantPool.TAGS_IN_ORDER[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (!haveNumbers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // These four counts are optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            tagCount[tag] = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    protected Index getCPIndex(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return pkg.cp.getIndexByTag(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    Index initCPIndex(byte tag, Entry[] cpMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                Utils.log.fine("cp.add "+cpMap[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        Index index = ConstantPool.makeIndex(ConstantPool.tagName(tag), cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (verbose > 1)  Utils.log.fine("Read "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        pkg.cp.initIndexByTag(tag, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    void readConstantPool() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        //  cp_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        //        cp_Utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        //        *cp_Int :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        //        *cp_Float :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        //        cp_Long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        //        cp_Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        //        *cp_String :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        //        *cp_Class :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        //        cp_Signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        //        cp_Descr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        //        cp_Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        //        cp_Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        //        cp_Imethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (verbose > 0)  Utils.log.info("Reading CP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        for (int k = 0; k < ConstantPool.TAGS_IN_ORDER.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            byte tag = ConstantPool.TAGS_IN_ORDER[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            int  len = tagCount[tag];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            Entry[] cpMap = new Entry[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                Utils.log.info("Reading "+cpMap.length+" "+ConstantPool.tagName(tag)+" entries...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case CONSTANT_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                readUtf8Bands(cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                cp_Int.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                cp_Int.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    int x = cp_Int.getInt();  // coding handles signs OK
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   438
                    cpMap[i] = ConstantPool.getLiteralEntry(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                cp_Int.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                cp_Float.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                cp_Float.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    int x = cp_Float.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    float fx = Float.intBitsToFloat(x);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   448
                    cpMap[i] = ConstantPool.getLiteralEntry(fx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                cp_Float.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                //  cp_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                //        *cp_Long_hi :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                //        *cp_Long_lo :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                cp_Long_hi.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                cp_Long_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                cp_Long_lo.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                cp_Long_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    long hi = cp_Long_hi.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    long lo = cp_Long_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    long x = (hi << 32) + ((lo << 32) >>> 32);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   464
                    cpMap[i] = ConstantPool.getLiteralEntry(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                cp_Long_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                cp_Long_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                //  cp_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                //        *cp_Double_hi :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                //        *cp_Double_lo :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                cp_Double_hi.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                cp_Double_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                cp_Double_lo.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                cp_Double_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    long hi = cp_Double_hi.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    long lo = cp_Double_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    long x = (hi << 32) + ((lo << 32) >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    double dx = Double.longBitsToDouble(x);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   482
                    cpMap[i] = ConstantPool.getLiteralEntry(dx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                cp_Double_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                cp_Double_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            case CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                cp_String.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                cp_String.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                cp_String.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    cpMap[i] = ConstantPool.getLiteralEntry(cp_String.getRef().stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                cp_String.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                cp_Class.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                cp_Class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                cp_Class.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    cpMap[i] = ConstantPool.getClassEntry(cp_Class.getRef().stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                cp_Class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            case CONSTANT_Signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                readSignatureBands(cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                //  cp_Descr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                //        *cp_Descr_type :DELTA5  (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                //        *cp_Descr_name :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                cp_Descr_name.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                cp_Descr_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                cp_Descr_name.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                cp_Descr_type.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                cp_Descr_type.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                cp_Descr_type.setIndex(getCPIndex(CONSTANT_Signature));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    Entry ref  = cp_Descr_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    Entry ref2 = cp_Descr_type.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    cpMap[i] = ConstantPool.getDescriptorEntry((Utf8Entry)ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                                        (SignatureEntry)ref2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                cp_Descr_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                cp_Descr_type.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                readMemberRefs(tag, cpMap, cp_Field_class, cp_Field_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                readMemberRefs(tag, cpMap, cp_Method_class, cp_Method_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                readMemberRefs(tag, cpMap, cp_Imethod_class, cp_Imethod_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            Index index = initCPIndex(tag, cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (optDumpBands) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                PrintStream ps = new PrintStream(getDumpStream(index, ".idx"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                ps.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        cp_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        setBandIndexes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    void readUtf8Bands(Entry[] cpMap) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        //  cp_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        //        *cp_Utf8_prefix :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        //        *cp_Utf8_suffix :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        //        *cp_Utf8_chars :CHAR3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        //        *cp_Utf8_big_suffix :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        //        (*cp_Utf8_big_chars :DELTA5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        //          ** length(cp_Utf8_big_suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        int len = cpMap.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return;  // nothing to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // Bands have implicit leading zeroes, for the empty string:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        final int SUFFIX_SKIP_1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        final int PREFIX_SKIP_2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // First band:  Read lengths of shared prefixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        cp_Utf8_prefix.expectLength(Math.max(0, len - PREFIX_SKIP_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        cp_Utf8_prefix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // Second band:  Read lengths of unshared suffixes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        cp_Utf8_suffix.expectLength(Math.max(0, len - SUFFIX_SKIP_1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        cp_Utf8_suffix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        char[][] suffixChars = new char[len][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int bigSuffixCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // Third band:  Read the char values in the unshared suffixes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        cp_Utf8_chars.expectLength(cp_Utf8_suffix.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        cp_Utf8_chars.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (suffix == 0 && i >= SUFFIX_SKIP_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // chars are packed in cp_Utf8_big_chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                bigSuffixCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            suffixChars[i] = new char[suffix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            for (int j = 0; j < suffix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                int ch = cp_Utf8_chars.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                assert(ch == (char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                suffixChars[i][j] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        cp_Utf8_chars.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // Fourth band:  Go back and size the specially packed strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int maxChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        cp_Utf8_big_suffix.expectLength(bigSuffixCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        cp_Utf8_big_suffix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            if (suffix == 0 && i >= SUFFIX_SKIP_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                assert(suffixChars[i] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                assert(suffixChars[i] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (maxChars < prefix + suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                maxChars = prefix + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        char[] buf = new char[maxChars];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // Fifth band(s):  Get the specially packed characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        cp_Utf8_big_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (i < SUFFIX_SKIP_1)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            int suffix = cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (suffix != 0)  continue;  // already input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            suffixChars[i] = new char[suffix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (suffix == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                // Do not bother to add an empty "(Utf8_big_0)" band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            IntBand packed = cp_Utf8_big_chars.newIntBand("(Utf8_big_"+i+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            packed.expectLength(suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            packed.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            for (int j = 0; j < suffix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                int ch = packed.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                assert(ch == (char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                suffixChars[i][j] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            packed.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        cp_Utf8_big_chars.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // Finally, sew together all the prefixes and suffixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        cp_Utf8_prefix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        cp_Utf8_big_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (suffix == 0 && i >= SUFFIX_SKIP_1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // by induction, the buffer is already filled with the prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            System.arraycopy(suffixChars[i], 0, buf, prefix, suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            cpMap[i] = ConstantPool.getUtf8Entry(new String(buf, 0, prefix+suffix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        cp_Utf8_prefix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        cp_Utf8_suffix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        cp_Utf8_big_suffix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   665
    Map<Utf8Entry, SignatureEntry> utf8Signatures;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    void readSignatureBands(Entry[] cpMap) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        //  cp_Signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        //        *cp_Signature_form :DELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        //        *cp_Signature_classes :UDELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        cp_Signature_form.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        cp_Signature_form.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        cp_Signature_form.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        int[] numSigClasses = new int[cpMap.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            Utf8Entry formRef = (Utf8Entry) cp_Signature_form.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            numSigClasses[i] = ConstantPool.countClassParts(formRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        cp_Signature_form.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        cp_Signature_classes.expectLength(getIntTotal(numSigClasses));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        cp_Signature_classes.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        cp_Signature_classes.setIndex(getCPIndex(CONSTANT_Class));
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   683
        utf8Signatures = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            Utf8Entry formRef = (Utf8Entry) cp_Signature_form.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            ClassEntry[] classRefs = new ClassEntry[numSigClasses[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            for (int j = 0; j < classRefs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                classRefs[j] = (ClassEntry) cp_Signature_classes.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            SignatureEntry se = ConstantPool.getSignatureEntry(formRef, classRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            cpMap[i] = se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            utf8Signatures.put(se.asUtf8Entry(), se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        cp_Signature_form.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        cp_Signature_classes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    void readMemberRefs(byte tag, Entry[] cpMap, CPRefBand cp_class, CPRefBand cp_desc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        //  cp_Field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        //        *cp_Field_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        //        *cp_Field_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        //  cp_Method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        //        *cp_Method_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        //        *cp_Method_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        //  cp_Imethod:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        //        *cp_Imethod_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        //        *cp_Imethod_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        cp_class.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        cp_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        cp_class.setIndex(getCPIndex(CONSTANT_Class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        cp_desc.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        cp_desc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        cp_desc.setIndex(getCPIndex(CONSTANT_NameandType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            ClassEntry      mclass = (ClassEntry     ) cp_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            DescriptorEntry mdescr = (DescriptorEntry) cp_desc.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            cpMap[i] = ConstantPool.getMemberEntry(tag, mclass, mdescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        cp_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        cp_desc.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    void readFiles() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        //  file_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        //        *file_name :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        //        *file_size_hi :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        //        *file_size_lo :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        //        *file_modtime :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        //        *file_options :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        //        *file_bits :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            Utils.log.info("  ...building "+numFiles+" files...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        file_name.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        file_size_lo.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        int options = archiveOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        boolean haveSizeHi  = testBit(options, AO_HAVE_FILE_SIZE_HI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        boolean haveModtime = testBit(options, AO_HAVE_FILE_MODTIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        boolean haveOptions = testBit(options, AO_HAVE_FILE_OPTIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (haveSizeHi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            file_size_hi.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (haveModtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            file_modtime.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (haveOptions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            file_options.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        file_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        file_size_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        file_size_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        file_modtime.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        file_options.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        file_bits.setInputStreamFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        Iterator nextClass = pkg.getClasses().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // Compute file lengths before reading any file bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        long totalFileLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        long[] fileLengths = new long[numFiles];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        for (int i = 0; i < numFiles; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            long size = ((long)file_size_lo.getInt() << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (haveSizeHi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                size += (long)file_size_hi.getInt() << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            fileLengths[i] = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            totalFileLength += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        assert(in.getReadLimit() == -1 || in.getReadLimit() == totalFileLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        byte[] buf = new byte[1<<16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        for (int i = 0; i < numFiles; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // %%% Use a big temp file for file bits?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            Utf8Entry name = (Utf8Entry) file_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            long size = fileLengths[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            File file = pkg.new File(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            file.modtime = pkg.default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            file.options = pkg.default_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (haveModtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                file.modtime += file_modtime.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (haveOptions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                file.options |= file_options.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                Utils.log.fine("Reading "+size+" bytes of "+name.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            long toRead = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            while (toRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                int nr = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (nr > toRead)  nr = (int) toRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                nr = file_bits.getInputStream().read(buf, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                if (nr < 0)  throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                file.addBytes(buf, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                toRead -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            pkg.addFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (file.isClassStub()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                assert(file.getFileLength() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                Class cls = (Class) nextClass.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                cls.initFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        // Do the rest of the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        while (nextClass.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            Class cls = (Class) nextClass.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            cls.initFile(null);  // implicitly initialize to a trivial one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            cls.file.modtime = pkg.default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        file_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        file_size_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        file_size_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        file_modtime.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        file_options.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        file_bits.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        file_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (archiveSize1 != 0 && !in.atLimit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            throw new RuntimeException("Predicted archive_size "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                       archiveSize1+" != "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                       (in.getBytesServed()-archiveSize0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    void readAttrDefs() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        //  attr_definition_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        //        *attr_definition_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        //        *attr_definition_name :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        //        *attr_definition_layout :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        attr_definition_headers.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        attr_definition_name.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        attr_definition_layout.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        attr_definition_headers.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        attr_definition_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        attr_definition_layout.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        PrintStream dump = !optDumpBands ? null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            : new PrintStream(getDumpStream(attr_definition_headers, ".def"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        for (int i = 0; i < numAttrDefs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            int       header = attr_definition_headers.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            Utf8Entry name   = (Utf8Entry) attr_definition_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            int       ctype  = (header &  ADH_CONTEXT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            int       index  = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            Attribute.Layout def = new Attribute.Layout(ctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                        name.stringValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                                        layout.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            // Check layout string for Java 6 extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            String pvLayout = def.layoutForPackageMajver(getPackageMajver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            if (!pvLayout.equals(def.layout())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                throw new IOException("Bad attribute layout in version 150 archive: "+def.layout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            this.setAttributeLayoutIndex(def, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            if (dump != null)  dump.println(index+" "+def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        if (dump != null)  dump.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        attr_definition_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        attr_definition_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        attr_definition_layout.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        // Attribute layouts define bands, one per layout element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        // Create them now, all at once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        makeNewAttributeBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        attr_definition_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    void readInnerClasses() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        //  ic_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        //        *ic_this_class :UDELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        //        *ic_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        //        *ic_outer_class :DELTA5  (null or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        //        *ic_name :DELTA5  (null or cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        ic_this_class.expectLength(numInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        ic_this_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        ic_flags.expectLength(numInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        ic_flags.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        int longICCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        for (int i = 0; i < numInnerClasses; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            int flags = ic_flags.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            boolean longForm = (flags & ACC_IC_LONG_FORM) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (longForm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                longICCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        ic_outer_class.expectLength(longICCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        ic_outer_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        ic_name.expectLength(longICCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        ic_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        ic_flags.resetForSecondPass();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   883
        List<InnerClass> icList = new ArrayList<>(numInnerClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        for (int i = 0; i < numInnerClasses; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            int flags = ic_flags.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            boolean longForm = (flags & ACC_IC_LONG_FORM) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            flags &= ~ACC_IC_LONG_FORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            ClassEntry thisClass = (ClassEntry) ic_this_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            ClassEntry outerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            Utf8Entry  thisName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if (longForm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                outerClass = (ClassEntry) ic_outer_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                thisName   = (Utf8Entry)  ic_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                String n = thisClass.stringValue();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   896
                String[] parse = Package.parseInnerClassName(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                assert(parse != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                String pkgOuter = parse[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                //String number = parse[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                String name     = parse[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                if (pkgOuter == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    outerClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    outerClass = ConstantPool.getClassEntry(pkgOuter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    thisName   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    thisName   = ConstantPool.getUtf8Entry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            InnerClass ic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                new InnerClass(thisClass, outerClass, thisName, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            assert(longForm || ic.predictable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            icList.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        ic_flags.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        ic_this_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        ic_outer_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        ic_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        pkg.setAllInnerClasses(icList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        ic_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    void readLocalInnerClasses(Class cls) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        int nc = class_InnerClasses_N.getInt();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   925
        List<InnerClass> localICs = new ArrayList<>(nc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        for (int i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            ClassEntry thisClass = (ClassEntry) class_InnerClasses_RC.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            int        flags     =              class_InnerClasses_F.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            if (flags == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                // A zero flag means copy a global IC here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                InnerClass ic = pkg.getGlobalInnerClass(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                assert(ic != null);  // must be a valid global IC reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                localICs.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                if (flags == ACC_IC_LONG_FORM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    flags = 0;  // clear the marker bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                ClassEntry outer = (ClassEntry) class_InnerClasses_outer_RCN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                Utf8Entry name   = (Utf8Entry)  class_InnerClasses_name_RUN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                localICs.add(new InnerClass(thisClass, outer, name, flags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        cls.setInnerClasses(localICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        // cls.expandLocalICs may add more tuples to ics also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        // or may even delete tuples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        // We cannot do that now, because we do not know the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        // full contents of the local constant pool yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    static final int NO_FLAGS_YET = 0;  // placeholder for later flag read-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    Class[] readClasses() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        //  class_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        //        *class_this :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        //        *class_super :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        //        *class_interface_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        //        *class_interface :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        //        ...(member bands)...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        //        class_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        //        code_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        Class[] classes = new Class[numClasses];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            Utils.log.info("  ...building "+classes.length+" classes...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        class_this.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        class_super.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        class_interface_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        class_this.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        class_super.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        class_interface_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        class_interface.expectLength(class_interface_count.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        class_interface.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            ClassEntry   thisClass  = (ClassEntry) class_this.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            ClassEntry   superClass = (ClassEntry) class_super.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            ClassEntry[] interfaces = new ClassEntry[class_interface_count.getInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            for (int j = 0; j < interfaces.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                interfaces[j] = (ClassEntry) class_interface.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            // Packer encoded rare case of null superClass as thisClass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            if (superClass == thisClass)  superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            Class cls = pkg.new Class(NO_FLAGS_YET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                      thisClass, superClass, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            classes[i] = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        class_this.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        class_super.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        class_interface_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        class_interface.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        readMembers(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        countAndReadAttrs(ATTR_CONTEXT_CLASS, Arrays.asList(classes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        pkg.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        readCodeHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        //code_bands.doneDisbursing(); // still need to read code attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        //class_bands.doneDisbursing(); // still need to read code attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    private int getOutputIndex(Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        // Output CPs do not contain signatures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        assert(e.tag != CONSTANT_Signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        int k = pkg.cp.untypedIndexOf(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        // In the output ordering, input signatures can serve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // in place of Utf8s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        if (k >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        if (e.tag == CONSTANT_Utf8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            Entry se = (Entry) utf8Signatures.get(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            return pkg.cp.untypedIndexOf(se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
8570
c9767adab3d2 7023963: (misc) fix diamond anon instances in the jdk
ksrini
parents: 7816
diff changeset
  1014
    Comparator<Entry> entryOutputOrder = new Comparator<Entry>() {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1015
        public int compare(Entry  e0, Entry e1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            int k0 = getOutputIndex(e0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            int k1 = getOutputIndex(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            if (k0 >= 0 && k1 >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                // If both have keys, use the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                return k0 - k1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (k0 == k1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                // If neither have keys, use their native tags & spellings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                return e0.compareTo(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            // Otherwise, the guy with the key comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            return (k0 >= 0)? 0-1: 1-0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    void reconstructClass(Class cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (verbose > 1)  Utils.log.fine("reconstruct "+cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        // check for local .ClassFile.version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        Attribute retroVersion = cls.getAttribute(attrClassFileVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (retroVersion != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            cls.removeAttribute(retroVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            short[] minmajver = parseClassFileVersionAttr(retroVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            cls.minver = minmajver[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            cls.majver = minmajver[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            cls.minver = pkg.default_class_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            cls.majver = pkg.default_class_majver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        // Replace null SourceFile by "obvious" string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        cls.expandSourceFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        // record the local cp:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        cls.setCPMap(reconstructLocalCPMap(cls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    Entry[] reconstructLocalCPMap(Class cls) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1052
        Set<Entry> ldcRefs = ldcRefMap.get(cls);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1053
        Set<Entry> cpRefs = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        // look for constant pool entries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        cls.visitRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        // flesh out the local constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        ConstantPool.completeReferencesIn(cpRefs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // Now that we know all our local class references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        // compute the InnerClasses attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        int changed = cls.expandLocalICs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (changed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            if (changed > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                // Just visit the expanded InnerClasses attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                cls.visitInnerClassRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                // Have to recompute from scratch, because of deletions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                cpRefs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                cls.visitRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            // flesh out the local constant pool, again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            ConstantPool.completeReferencesIn(cpRefs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        // construct a local constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        int numDoubles = 0;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1081
        for (Entry e : cpRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (e.isDoubleWord())  numDoubles++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            assert(e.tag != CONSTANT_Signature) : (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        Entry[] cpMap = new Entry[1+numDoubles+cpRefs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int fillp = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // Add all ldc operands first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (ldcRefs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            assert(cpRefs.containsAll(ldcRefs));
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1091
            for (Entry e : ldcRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                cpMap[fillp++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            assert(fillp == 1+ldcRefs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            cpRefs.removeAll(ldcRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            ldcRefs = null;  // done with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // Next add all the two-byte references.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1100
        Set<Entry> wideRefs = cpRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        cpRefs = null;  // do not use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        int narrowLimit = fillp;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1103
        for (Entry e : wideRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            cpMap[fillp++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        assert(fillp == narrowLimit+wideRefs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        Arrays.sort(cpMap, 1, narrowLimit, entryOutputOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        Arrays.sort(cpMap, narrowLimit, fillp, entryOutputOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            Utils.log.fine("CP of "+this+" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            for (int i = 0; i < fillp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                Entry e = cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                Utils.log.fine("  "+((e==null)?-1:getOutputIndex(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                                   +" : "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            Utils.log.fine("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        // Now repack backwards, introducing null elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        int revp = cpMap.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        for (int i = fillp; --i >= 1; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            Entry e = cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            if (e.isDoubleWord())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                cpMap[--revp] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            cpMap[--revp] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        assert(revp == 1);  // do not process the initial null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    void readMembers(Class[] classes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        //  class_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        //        *class_field_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        //        *class_method_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        //        *field_descr :DELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        //        field_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        //        *method_descr :MDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        //        method_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        assert(classes.length == numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        class_field_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        class_method_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        class_field_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        class_method_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // Make a pre-pass over field and method counts to size the descrs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        int totalNF = class_field_count.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        int totalNM = class_method_count.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        field_descr.expectLength(totalNF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        method_descr.expectLength(totalNM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (verbose > 1)  Utils.log.fine("expecting #fields="+totalNF+" and #methods="+totalNM+" in #classes="+numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1158
        List<Class.Field> fields = new ArrayList<>(totalNF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        field_descr.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            Class c = classes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            int nf = class_field_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            for (int j = 0; j < nf; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                Class.Field f = c.new Field(NO_FLAGS_YET, (DescriptorEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                                            field_descr.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                fields.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        class_field_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        field_descr.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        countAndReadAttrs(ATTR_CONTEXT_FIELD, fields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        fields = null;  // release to GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1174
        List<Class.Method> methods = new ArrayList<>(totalNM);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        method_descr.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            Class c = classes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            int nm = class_method_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            for (int j = 0; j < nm; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                Class.Method m = c.new Method(NO_FLAGS_YET, (DescriptorEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                                              method_descr.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                methods.add(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        class_method_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        method_descr.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        countAndReadAttrs(ATTR_CONTEXT_METHOD, methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        // Up to this point, Code attributes look like empty attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        // Now we start to special-case them.  The empty canonical Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        // attributes stay in the method attribute lists, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        allCodes = buildCodeAttrs(methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    Code[] allCodes;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1196
    List<Code> codesWithFlags;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1197
    Map<Class, Set<Entry>> ldcRefMap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1199
    Code[] buildCodeAttrs(List<Class.Method> methods) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1200
        List<Code> codes = new ArrayList<>(methods.size());
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1201
        for (Class.Method m : methods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            if (m.getAttribute(attrCodeEmpty) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                m.code = new Code(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                codes.add(m.code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        Code[] a = new Code[codes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        codes.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    void readCodeHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        //  code_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        //        *code_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        //        *code_max_stack :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        //        *code_max_na_locals :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        //        *code_handler_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        //        code_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        boolean attrsOK = testBit(archiveOptions, AO_HAVE_ALL_CODE_FLAGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        code_headers.expectLength(allCodes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        code_headers.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1224
        List<Code> longCodes = new ArrayList<>(allCodes.length / 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            int sc = code_headers.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            assert(sc == (sc & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                Utils.log.fine("codeHeader "+c+" = "+sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if (sc == LONG_CODE_HEADER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                // We will read ms/ml/nh/flags from bands shortly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                longCodes.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            // Short code header is the usual case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            c.setMaxStack(     shortCodeHeader_max_stack(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            c.setMaxNALocals(  shortCodeHeader_max_na_locals(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            c.setHandlerCount( shortCodeHeader_handler_count(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            assert(shortCodeHeader(c) == sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        code_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        code_max_stack.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        code_max_na_locals.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        code_handler_count.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        // Do the long headers now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        code_max_stack.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        code_max_na_locals.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        code_handler_count.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1251
        for (Code c : longCodes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            c.setMaxStack(     code_max_stack.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            c.setMaxNALocals(  code_max_na_locals.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            c.setHandlerCount( code_handler_count.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        code_max_stack.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        code_max_na_locals.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        code_handler_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        readCodeHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (attrsOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            // Code attributes are common (debug info not stripped).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            codesWithFlags = Arrays.asList(allCodes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            // Code attributes are very sparse (debug info is stripped).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            codesWithFlags = longCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        countAttrs(ATTR_CONTEXT_CODE, codesWithFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        // do readAttrs later, after BCs are scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    void readCodeHandlers() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        //  code_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        //        *code_handler_start_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        //        *code_handler_end_PO :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        //        *code_handler_catch_PO :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        //        *code_handler_class_RCN :UNSIGNED5  (null or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        int nh = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            nh += c.getHandlerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        ValueBand[] code_handler_bands = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            code_handler_start_P,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            code_handler_end_PO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            code_handler_catch_PO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            code_handler_class_RCN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        for (int i = 0; i < code_handler_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            code_handler_bands[i].expectLength(nh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            code_handler_bands[i].readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            for (int j = 0, jmax = c.getHandlerCount(); j < jmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                c.handler_class[j] = code_handler_class_RCN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                // For now, just record the raw BCI codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                // We must wait until we have instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                c.handler_start[j] = code_handler_start_P.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                c.handler_end[j]   = code_handler_end_PO.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                c.handler_catch[j] = code_handler_catch_PO.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        for (int i = 0; i < code_handler_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            code_handler_bands[i].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    void fixupCodeHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // Actually decode (renumber) the BCIs now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            for (int j = 0, jmax = c.getHandlerCount(); j < jmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                int sum = c.handler_start[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                c.handler_start[j] = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                sum += c.handler_end[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                c.handler_end[j]   = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                sum += c.handler_catch[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                c.handler_catch[j] = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // Generic routines for reading attributes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // classes, fields, methods, and codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    // The holders is a global list, already collected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    // of attribute "customers".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    void countAndReadAttrs(int ctype, Collection holders) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        //  class_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        //        *class_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        //        *class_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        //        *class_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        //        *class_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        //        *class_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        //        class_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        //        *class_SourceFile_RU :UNSIGNED5 (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        //        *class_EnclosingMethod_RM :UNSIGNED5 (cp_Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        //        ic_local_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        //        *class_ClassFile_version_minor_H :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        //        *class_ClassFile_version_major_H :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        //  field_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        //        *field_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        //        *field_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        //        *field_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        //        *field_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        //        *field_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        //        field_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        //        *field_ConstantValue_KQ :UNSIGNED5 (cp_Int, etc.; see note)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        //  method_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        //        *method_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        //        *method_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        //        *method_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        //        *method_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        //        *method_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        //        method_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        //        *method_Exceptions_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        //        *method_Exceptions_RC :UNSIGNED5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        //  code_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        //        *code_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        //        *code_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        //        *code_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        //        *code_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        //        *code_LineNumberTable_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        //        *code_LineNumberTable_bci_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        //        *code_LineNumberTable_line :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        //        *code_LocalVariableTable_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        //        *code_LocalVariableTable_bci_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        //        *code_LocalVariableTable_span_O :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        //        *code_LocalVariableTable_name_RU :UNSIGNED5 (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        //        *code_LocalVariableTable_type_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        //        *code_LocalVariableTable_slot :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        countAttrs(ctype, holders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        readAttrs(ctype, holders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    // Read flags and count the attributes that are to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    // on the given holders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    void countAttrs(int ctype, Collection holders) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        // Here, xxx stands for one of class, field, method, code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        MultiBand xxx_attr_bands = attrBands[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        long flagMask = attrFlagMask[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            Utils.log.fine("scanning flags and attrs for "+Attribute.contextName(ctype)+"["+holders.size()+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        // Fetch the attribute layout definitions which govern the bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        // we are about to read.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1398
        List<Attribute.Layout> defList = attrDefs.get(ctype);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1399
        Attribute.Layout[] defs = new Attribute.Layout[defList.size()];
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1400
        defList.toArray(defs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        IntBand xxx_flags_hi = getAttrBand(xxx_attr_bands, AB_FLAGS_HI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        IntBand xxx_flags_lo = getAttrBand(xxx_attr_bands, AB_FLAGS_LO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        IntBand xxx_attr_count = getAttrBand(xxx_attr_bands, AB_ATTR_COUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        IntBand xxx_attr_indexes = getAttrBand(xxx_attr_bands, AB_ATTR_INDEXES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        IntBand xxx_attr_calls = getAttrBand(xxx_attr_bands, AB_ATTR_CALLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        // Count up the number of holders which have overflow attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        int overflowMask = attrOverflowMask[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        int overflowHolderCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        boolean haveLongFlags = haveFlagsHi(ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        xxx_flags_hi.expectLength(haveLongFlags? holders.size(): 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        xxx_flags_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        xxx_flags_lo.expectLength(holders.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        xxx_flags_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        assert((flagMask & overflowMask) == overflowMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        for (Iterator i = holders.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            Attribute.Holder h = (Attribute.Holder) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            int flags = xxx_flags_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            h.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            if ((flags & overflowMask) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                overflowHolderCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        // For each holder with overflow attrs, read a count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        xxx_attr_count.expectLength(overflowHolderCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        xxx_attr_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        xxx_attr_indexes.expectLength(xxx_attr_count.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        xxx_attr_indexes.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        // Now it's time to check flag bits that indicate attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        // We accumulate (a) a list of attribute types for each holder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        // (class/field/method/code), and also we accumulate (b) a total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        // count for each attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        int[] totalCounts = new int[defs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        for (Iterator i = holders.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            Attribute.Holder h = (Attribute.Holder) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            assert(h.attributes == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            // System.out.println("flags="+h.flags+" using fm="+flagMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            long attrBits = ((h.flags & flagMask) << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            // Clean up the flags now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            h.flags -= (int)attrBits;   // strip attr bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            assert(h.flags == (char)h.flags);  // 16 bits only now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            assert((ctype != ATTR_CONTEXT_CODE) || h.flags == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            if (haveLongFlags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                attrBits += (long)xxx_flags_hi.getInt() << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            if (attrBits == 0)  continue;  // no attrs on this guy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            int noa = 0;  // number of overflow attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            long overflowBit = (attrBits & overflowMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            assert(overflowBit >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            attrBits -= overflowBit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            if (overflowBit != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                noa = xxx_attr_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            int nfa = 0;  // number of flag attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            long bits = attrBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            for (int ai = 0; bits != 0; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                if ((bits & (1L<<ai)) == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                bits -= (1L<<ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                nfa += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1463
            List<Attribute> ha = new ArrayList<>(nfa + noa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            h.attributes = ha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            bits = attrBits;  // iterate again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            for (int ai = 0; bits != 0; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                if ((bits & (1L<<ai)) == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                bits -= (1L<<ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                totalCounts[ai] += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                // This definition index is live in this holder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                if (defs[ai] == null)  badAttrIndex(ai, ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                Attribute canonical = defs[ai].canonicalInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                ha.add(canonical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                nfa -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            assert(nfa == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            for (; noa > 0; noa--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                int ai = xxx_attr_indexes.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                totalCounts[ai] += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                // This definition index is live in this holder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                if (defs[ai] == null)  badAttrIndex(ai, ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                Attribute canonical = defs[ai].canonicalInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                ha.add(canonical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        xxx_flags_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        xxx_flags_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        xxx_attr_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        xxx_attr_indexes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        // Now each holder has a list of canonical attribute instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        // For layouts with no elements, we are done.  However, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        // layouts with bands, we must replace each canonical (empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        // instance with a value-bearing one, initialized from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        // appropriate bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        // Make a small pass to detect and read backward call counts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        int callCounts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        for (boolean predef = true; ; predef = false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            for (int ai = 0; ai < defs.length; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                Attribute.Layout def = defs[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                if (def == null)  continue;  // unused index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                if (predef != isPredefinedAttr(ctype, ai))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                    continue;  // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                int totalCount = totalCounts[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                if (totalCount == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    continue;  // irrelevant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                Attribute.Layout.Element[] cbles = def.getCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                for (int j = 0; j < cbles.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                    assert(cbles[j].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    if (cbles[j].flagTest(Attribute.EF_BACK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                        callCounts += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (!predef)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        xxx_attr_calls.expectLength(callCounts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        xxx_attr_calls.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        // Finally, size all the attribute bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        for (boolean predef = true; ; predef = false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            for (int ai = 0; ai < defs.length; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                Attribute.Layout def = defs[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                if (def == null)  continue;  // unused index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                if (predef != isPredefinedAttr(ctype, ai))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    continue;  // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                int totalCount = totalCounts[ai];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1529
                Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                if (def == attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    // Special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    // Size the bands as if using the following layout:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                    //    [RCH TI[ (0)[] ()[RCNH RUNH] ]].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    class_InnerClasses_N.expectLength(totalCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    class_InnerClasses_N.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    int tupleCount = class_InnerClasses_N.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    class_InnerClasses_RC.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    class_InnerClasses_RC.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    class_InnerClasses_F.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    class_InnerClasses_F.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    // Drop remaining columns wherever flags are zero:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    tupleCount -= class_InnerClasses_F.getIntCount(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    class_InnerClasses_outer_RCN.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    class_InnerClasses_outer_RCN.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    class_InnerClasses_name_RUN.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    class_InnerClasses_name_RUN.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                } else if (totalCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    // Expect no elements at all.  Skip quickly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    for (int j = 0; j < ab.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                        ab[j].doneWithUnusedBand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    // Read these bands in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    boolean hasCallables = def.hasCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    if (!hasCallables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                        readAttrBands(def.elems, totalCount, new int[0], ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                        Attribute.Layout.Element[] cbles = def.getCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                        // At first, record initial calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                        // Later, forward calls may also accumulate here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                        int[] forwardCounts = new int[cbles.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                        forwardCounts[0] = totalCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                        for (int j = 0; j < cbles.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                            assert(cbles[j].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                            int entryCount = forwardCounts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                            forwardCounts[j] = -1;  // No more, please!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                            if (cbles[j].flagTest(Attribute.EF_BACK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                                entryCount += xxx_attr_calls.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                            readAttrBands(cbles[j].body, entryCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            if (!predef)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        xxx_attr_calls.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    void badAttrIndex(int ai, int ctype) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        throw new IOException("Unknown attribute index "+ai+" for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                   ATTR_CONTEXT_NAME[ctype]+" attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1584
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    void readAttrs(int ctype, Collection holders) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        // Decode band values into attributes.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1587
        Set<Attribute.Layout> sawDefs = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        for (Iterator i = holders.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            final Attribute.Holder h = (Attribute.Holder) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            if (h.attributes == null)  continue;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1592
            for (ListIterator<Attribute> j = h.attributes.listIterator(); j.hasNext(); ) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1593
                Attribute a = j.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                Attribute.Layout def = a.layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                if (def.bandCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    if (def == attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                        // Special logic to read this attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                        readLocalInnerClasses((Class) h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                    // Canonical empty attr works fine (e.g., Synthetic).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                sawDefs.add(def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                boolean isCV = (ctype == ATTR_CONTEXT_FIELD && def == attrConstantValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                if (isCV)  setConstantValueIndex((Class.Field)h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                    Utils.log.fine("read "+a+" in "+h);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1609
                final Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                // Read one attribute of type def from ab into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                buf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                Object fixups = a.unparse(new Attribute.ValueStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                    public int getInt(int bandIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                        return ((IntBand) ab[bandIndex]).getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                    public Entry getRef(int bandIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                        return ((CPRefBand) ab[bandIndex]).getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    public int decodeBCI(int bciCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                        Code code = (Code) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        return code.decodeBCI(bciCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                }, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                // Replace the canonical attr with the one just read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                j.set(a.addContent(buf.toByteArray(), fixups));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                if (isCV)  setConstantValueIndex(null);  // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        // Mark the bands we just used as done disbursing.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1631
        for (Attribute.Layout def : sawDefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            if (def == null)  continue;  // unused index
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1633
            Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            for (int j = 0; j < ab.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                ab[j].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (ctype == ATTR_CONTEXT_CLASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            class_InnerClasses_N.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            class_InnerClasses_RC.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            class_InnerClasses_F.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            class_InnerClasses_outer_RCN.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            class_InnerClasses_name_RUN.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        MultiBand xxx_attr_bands = attrBands[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        for (int i = 0; i < xxx_attr_bands.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            Band b = xxx_attr_bands.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            if (b instanceof MultiBand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                b.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        xxx_attr_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    void readAttrBands(Attribute.Layout.Element[] elems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                       int count, int[] forwardCounts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                       Band[] ab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        for (int i = 0; i < elems.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            Attribute.Layout.Element e = elems[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            Band eBand = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            if (e.hasBand()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                eBand = ab[e.bandIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                eBand.expectLength(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                eBand.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            switch (e.kind) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            case Attribute.EK_REPL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                // Recursive call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                int repCount = ((IntBand)eBand).getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                // Note:  getIntTotal makes an extra pass over this band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                readAttrBands(e.body, repCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            case Attribute.EK_UN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                int remainingCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                for (int j = 0; j < e.body.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                    int caseCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    if (j == e.body.length-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                        caseCount = remainingCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                        caseCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                        for (int j0 = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                             (j == j0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                             || (j < e.body.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                                 && e.body[j].flagTest(Attribute.EF_BACK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                             j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                            caseCount += ((IntBand)eBand).getIntCount(e.body[j].value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        --j;  // back up to last occurrence of this body
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    remainingCount -= caseCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                    readAttrBands(e.body[j].body, caseCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                assert(remainingCount == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            case Attribute.EK_CALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                assert(e.body.length == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                assert(e.body[0].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                if (!e.flagTest(Attribute.EF_BACK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    // Backward calls are pre-counted, but forwards are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    // Push the present count forward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    assert(forwardCounts[e.value] >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    forwardCounts[e.value] += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            case Attribute.EK_CBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    void readByteCodes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        //  bc_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        //        *bc_codes :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        //        *bc_case_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        //        *bc_case_value :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        //        *bc_byte :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        //        *bc_short :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        //        *bc_local :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        //        *bc_label :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        //        *bc_intref :DELTA5  (cp_Int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        //        *bc_floatref :DELTA5  (cp_Float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        //        *bc_longref :DELTA5  (cp_Long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        //        *bc_doubleref :DELTA5  (cp_Double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        //        *bc_stringref :DELTA5  (cp_String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        //        *bc_classref :UNSIGNED5  (current class or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        //        *bc_fieldref :DELTA5  (cp_Field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        //        *bc_methodref :UNSIGNED5  (cp_Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        //        *bc_imethodref :DELTA5  (cp_Imethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        //        *bc_thisfield :UNSIGNED5 (cp_Field, only for current class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        //        *bc_superfield :UNSIGNED5 (cp_Field, only for current super)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        //        *bc_thismethod :UNSIGNED5 (cp_Method, only for current class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        //        *bc_supermethod :UNSIGNED5 (cp_Method, only for current super)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        //        *bc_initref :UNSIGNED5 (cp_Field, only for most recent new)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        //        *bc_escref :UNSIGNED5 (cp_All)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        //        *bc_escrefsize :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        //        *bc_escsize :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        //        *bc_escbyte :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        bc_codes.elementCountForDebug = allCodes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        bc_codes.setInputStreamFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        readByteCodeOps();  // reads from bc_codes and bc_case_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        bc_codes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        // All the operand bands have now been sized.  Read them all in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        Band[] operand_bands = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            bc_case_value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            bc_byte, bc_short,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            bc_local, bc_label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            bc_intref, bc_floatref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            bc_longref, bc_doubleref, bc_stringref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            bc_classref, bc_fieldref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            bc_methodref, bc_imethodref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            bc_thisfield, bc_superfield,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            bc_thismethod, bc_supermethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            bc_initref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            bc_escref, bc_escrefsize, bc_escsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        for (int i = 0; i < operand_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            operand_bands[i].readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        bc_escbyte.expectLength(bc_escsize.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        bc_escbyte.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        expandByteCodeOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        // Done fetching values from operand bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        bc_case_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        for (int i = 0; i < operand_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            operand_bands[i].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        bc_escbyte.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        bc_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        // We must delay the parsing of Code attributes until we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        // have a complete model of bytecodes, for BCI encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        readAttrs(ATTR_CONTEXT_CODE, codesWithFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        // Ditto for exception handlers in codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        fixupCodeHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        // Now we can finish with class_bands; cf. readClasses().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        code_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        class_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    private void readByteCodeOps() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        // scratch buffer for collecting code::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        byte[] buf = new byte[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        // record of all switch opcodes (these are variable-length)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1791
        List<Integer> allSwitchOps = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        for (int k = 0; k < allCodes.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            Code c = allCodes[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        scanOneMethod:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                int bc = bc_codes.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                if (i + 10 > buf.length)  buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                buf[i] = (byte)bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                boolean isWide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                if (bc == _wide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                    bc = bc_codes.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                    buf[++i] = (byte)bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                    isWide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                assert(bc == (0xFF & bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                // Adjust expectations of various band sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                case _tableswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                case _lookupswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                    bc_case_count.expectMoreLength(1);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1811
                    allSwitchOps.add(bc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                case _iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                    bc_local.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                    if (isWide)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                        bc_short.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                        bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                case _sipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    bc_short.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                case _bipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                case _newarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                case _multianewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                    assert(getCPRefOpBand(bc) == bc_classref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                    bc_classref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                case _ref_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    bc_escrefsize.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    bc_escref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                case _byte_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    bc_escsize.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    // bc_escbyte will have to be counted too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                    if (Instruction.isInvokeInitOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                        bc_initref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    if (Instruction.isSelfLinkerOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                        CPRefBand bc_which = selfOpRefBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                        bc_which.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    if (Instruction.isBranchOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                        bc_label.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                    if (Instruction.isCPRefOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                        CPRefBand bc_which = getCPRefOpBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                        bc_which.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                        assert(bc != _multianewarray);  // handled elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    if (Instruction.isLocalSlotOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                        bc_local.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                case _end_marker:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        // Transfer from buf to a more permanent place:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                        c.bytes = realloc(buf, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                        break scanOneMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        // To size instruction bands correctly, we need info on switches:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        bc_case_count.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1879
        for (Integer i : allSwitchOps) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1880
            int bc = i.intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            int caseCount = bc_case_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            bc_label.expectMoreLength(1+caseCount); // default label + cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            bc_case_value.expectMoreLength(bc == _tableswitch ? 1 : caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        bc_case_count.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    private void expandByteCodeOps() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        // scratch buffer for collecting code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        byte[] buf = new byte[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        // scratch buffer for collecting instruction boundaries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        int[] insnMap = new int[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        // list of label carriers, for label decoding post-pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        int[] labels = new int[1<<10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        // scratch buffer for registering CP refs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        Fixups fixupBuf = new Fixups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        for (int k = 0; k < allCodes.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            Code code = allCodes[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            byte[] codeOps = code.bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            code.bytes = null;  // just for now, while we accumulate bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            Class curClass = code.thisClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1905
            Set<Entry> ldcRefSet = ldcRefMap.get(curClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            if (ldcRefSet == null)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1907
                ldcRefMap.put(curClass, ldcRefSet = new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            ClassEntry thisClass  = curClass.thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            ClassEntry superClass = curClass.superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            ClassEntry newClass   = null;  // class of last _new opcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            int pc = 0;  // fill pointer in buf; actual bytecode PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            int numInsns = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            int numLabels = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            boolean hasEscs = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            fixupBuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            for (int i = 0; i < codeOps.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                int bc = Instruction.getByte(codeOps, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                int curPC = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                insnMap[numInsns++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                if (pc + 10 > buf.length)  buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                if (numInsns+10 > insnMap.length)  insnMap = realloc(insnMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                if (numLabels+10 > labels.length)  labels = realloc(labels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                boolean isWide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                if (bc == _wide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                    buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                    bc = Instruction.getByte(codeOps, ++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                    isWide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                case _tableswitch: // apc:  (df, lo, hi, (hi-lo+1)*(label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                case _lookupswitch: // apc:  (df, nc, nc*(case, label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                        int caseCount = bc_case_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                        while ((pc + 30 + caseCount*8) > buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                            buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                        //initialize apc, df, lo, hi bytes to reasonable bits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                        Arrays.fill(buf, pc, pc+30, (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                        Instruction.Switch isw = (Instruction.Switch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                            Instruction.at(buf, curPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                        //isw.setDefaultLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                        isw.setCaseCount(caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                        if (bc == _tableswitch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                            isw.setCaseValue(0, bc_case_value.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                            for (int j = 0; j < caseCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                                isw.setCaseValue(j, bc_case_value.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                        // Make our getLabel calls later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                        labels[numLabels++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                        pc = isw.getNextPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                case _iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                        int local = bc_local.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        int delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                        if (isWide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                            delta = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                            Instruction.setShort(buf, pc, local); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                            Instruction.setShort(buf, pc, delta); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                            delta = (byte) bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                            buf[pc++] = (byte)local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                            buf[pc++] = (byte)delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                case _sipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                        int val = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                        Instruction.setShort(buf, pc, val); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                case _bipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                case _newarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                        int val = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                        buf[pc++] = (byte) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                case _ref_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                        // Note that insnMap has one entry for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                        hasEscs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                        int size = bc_escrefsize.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                        Entry ref = bc_escref.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                        if (size == 1)  ldcRefSet.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                        int fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                        case 1: fmt = Fixups.U1_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                        case 2: fmt = Fixups.U2_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                        default: assert(false); fmt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                        fixupBuf.add(pc, fmt, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                        pc += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                case _byte_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                        // Note that insnMap has one entry for all these bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                        hasEscs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                        int size = bc_escsize.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                        while ((pc + size) > buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                            buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                        while (size-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                            buf[pc++] = (byte) bc_escbyte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    if (Instruction.isInvokeInitOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                        int idx = (bc - _invokeinit_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                        int origBC = _invokespecial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                        ClassEntry classRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                        switch (idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        case _invokeinit_self_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                            classRef = thisClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                        case _invokeinit_super_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                            classRef = superClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                            assert(idx == _invokeinit_new_option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                            classRef = newClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                        int coding = bc_initref.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                        // Find the nth overloading of <init> in classRef.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                        MemberEntry ref = pkg.cp.getOverloadingForIndex(CONSTANT_Methodref, classRef, "<init>", coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        fixupBuf.add(pc, Fixups.U2_FORMAT, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                        pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    if (Instruction.isSelfLinkerOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                        int idx = (bc - _self_linker_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                        boolean isSuper = (idx >= _self_linker_super_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                        if (isSuper)  idx -= _self_linker_super_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                        boolean isAload = (idx >= _self_linker_aload_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                        if (isAload)  idx -= _self_linker_aload_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                        int origBC = _first_linker_op + idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        boolean isField = Instruction.isFieldOp(origBC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                        CPRefBand bc_which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                        ClassEntry which_cls  = isSuper ? superClass : thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                        Index which_ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                        if (isField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                            bc_which = isSuper ? bc_superfield  : bc_thisfield;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                            which_ix = pkg.cp.getMemberIndex(CONSTANT_Fieldref, which_cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                            bc_which = isSuper ? bc_supermethod : bc_thismethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                            which_ix = pkg.cp.getMemberIndex(CONSTANT_Methodref, which_cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                        assert(bc_which == selfOpRefBand(bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                        MemberEntry ref = (MemberEntry) bc_which.getRef(which_ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                        if (isAload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                            buf[pc++] = (byte) _aload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                            curPC = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                            // Note: insnMap keeps the _aload_0 separate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                            insnMap[numInsns++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                        fixupBuf.add(pc, Fixups.U2_FORMAT, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                        pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                    if (Instruction.isBranchOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                        assert(!isWide);  // no wide prefix for branches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        int nextPC = curPC + Instruction.opLength(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        // Make our getLabel calls later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                        labels[numLabels++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                        //Instruction.at(buf, curPC).setBranchLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                        while (pc < nextPC)  buf[pc++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    if (Instruction.isCPRefOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                        CPRefBand bc_which = getCPRefOpBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                        Entry ref = bc_which.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                            if (bc_which == bc_classref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                                // Shorthand for class self-references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                                ref = thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                        int origBC = bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                        int size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                        switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        case _ildc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                        case _cldc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        case _fldc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                        case _aldc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                            origBC = _ldc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                            size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                            ldcRefSet.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                        case _ildc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        case _cldc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                        case _fldc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        case _aldc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                            origBC = _ldc_w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                        case _lldc2_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        case _dldc2_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                            origBC = _ldc2_w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                        case _new:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                            newClass = (ClassEntry) ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                        int fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        case 1: fmt = Fixups.U1_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                        case 2: fmt = Fixups.U2_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        default: assert(false); fmt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                        fixupBuf.add(pc, fmt, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                        pc += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                        if (origBC == _multianewarray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                            // Copy the trailing byte also.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                            int val = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                            buf[pc++] = (byte) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                        } else if (origBC == _invokeinterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                            int argSize = ((MemberEntry)ref).descRef.typeRef.computeSize(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                            buf[pc++] = (byte)( 1 + argSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                            buf[pc++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    if (Instruction.isLocalSlotOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                        int local = bc_local.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        if (isWide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                            Instruction.setShort(buf, pc, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                            pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                            if (bc == _iinc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                                int iVal = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                                Instruction.setShort(buf, pc, iVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                            Instruction.setByte(buf, pc, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                            pc += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                            if (bc == _iinc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                                int iVal = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                                Instruction.setByte(buf, pc, iVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                                pc += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                        assert(Instruction.opLength(bc) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    // Random bytecode.  Just copy it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                    if (bc >= _bytecode_limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                        Utils.log.warning("unrecognized bytescode "+bc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                                            +" "+Instruction.byteName(bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                    assert(bc < _bytecode_limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                    buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    assert(Instruction.opLength(bc) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            // now make a permanent copy of the bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            code.setBytes(realloc(buf, pc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            code.setInstructionMap(insnMap, numInsns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            // fix up labels, now that code has its insnMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            Instruction ibr = null;  // temporary branch instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            for (int i = 0; i < numLabels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                int curPC = labels[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                // (Note:  Passing ibr in allows reuse, a speed hack.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                ibr = Instruction.at(code.bytes, curPC, ibr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                if (ibr instanceof Instruction.Switch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    Instruction.Switch isw = (Instruction.Switch) ibr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    isw.setDefaultLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    int caseCount = isw.getCaseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    for (int j = 0; j < caseCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                        isw.setCaseLabel(j, getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    ibr.setBranchLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (fixupBuf.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    Utils.log.fine("Fixups in code: "+fixupBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                code.addFixups(fixupBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
}