jdk/src/share/classes/com/sun/java/util/jar/pack/PopulationCoding.java
author ksrini
Tue, 29 May 2012 14:56:48 -0700
changeset 12857 0a5f341c2a28
parent 7816 55a18147b4bf
permissions -rw-r--r--
7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7192
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    28
import java.io.ByteArrayOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    32
import java.util.Arrays;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    33
import java.util.HashSet;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    34
import java.util.Set;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    35
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Population-based coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * See the section "Encodings of Uncorrelated Values" in the Pack200 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// This tactic alone reduces the final zipped rt.jar by about a percent.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    43
class PopulationCoding implements CodingMethod {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    Histogram vHist;   // histogram of all values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int[]     fValues; // list of favored values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int       fVlen;   // inclusive max index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    long[]    symtab;  // int map of favored value -> token [1..#fValues]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    CodingMethod favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    CodingMethod tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    CodingMethod unfavoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int L = -1;  //preferred L value for tokenCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void setFavoredValues(int[] fValues, int fVlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // Note:  {f} is allFavoredValues[1..fvlen], not [0..fvlen-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // This is because zero is an exceptional favored value index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        assert(fValues[0] == 0);  // must be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        assert(this.fValues == null);  // do not do this twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.fValues = fValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.fVlen   = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (L >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            setL(L);  // reassert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void setFavoredValues(int[] fValues) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    67
        int lfVlen = fValues.length-1;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    68
        setFavoredValues(fValues, lfVlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void setHistogram(Histogram vHist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.vHist = vHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void setL(int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.L = L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (L >= 0 && fValues != null && tokenCoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            tokenCoding = fitTokenCoding(fVlen, L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            assert(tokenCoding != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static Coding fitTokenCoding(int fVlen, int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Find the smallest B s.t. (B,H,0) covers fVlen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (fVlen < 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // H/L do not matter when B==1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return BandStructure.BYTE1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Coding longest = BandStructure.UNSIGNED5.setL(L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!longest.canRepresentUnsigned(fVlen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return null;  // failure; L is too sharp and fVlen too large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Coding tc = longest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (Coding shorter = longest; ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            shorter = shorter.setB(shorter.B()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (shorter.umax() < fVlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            tc = shorter;  // shorten it by reducing B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void setFavoredCoding(CodingMethod favoredCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.favoredCoding = favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public void setTokenCoding(CodingMethod tokenCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.tokenCoding = tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.L = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (tokenCoding instanceof Coding && fValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (tc == fitTokenCoding(fVlen, tc.L()))
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   108
                this.L = tc.L();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // Otherwise, it's a non-default coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void setUnfavoredCoding(CodingMethod unfavoredCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.unfavoredCoding = unfavoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public int favoredValueMaxLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (L == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return BandStructure.UNSIGNED5.setL(L).umax();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void resortFavoredValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Make a local copy before reordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        fValues = BandStructure.realloc(fValues, 1+fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // Resort favoredValues within each byte-size cadre.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int fillp = 1;  // skip initial zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int n = 1; n <= tc.B(); n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int nmax = tc.byteMax(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (nmax > fVlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                nmax = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (nmax < tc.byteMin(n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int low = fillp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int high = nmax+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (high == low)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            assert(high > low)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                : high+"!>"+low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            assert(tc.getLength(low) == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                : n+" != len("+(low)+") == "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                  tc.getLength(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            assert(tc.getLength(high-1) == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                : n+" != len("+(high-1)+") == "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                  tc.getLength(high-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int midTarget = low + (high-low)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int mid = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Divide the values into cadres, and sort within each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            int prevCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            int prevLimit = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            for (int i = low; i < high; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                int val = fValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                int count = vHist.getFrequency(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (prevCount != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        // For the single-byte encoding, keep strict order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        // among frequency groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        Arrays.sort(fValues, prevLimit, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    } else if (Math.abs(mid - midTarget) >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                               Math.abs(i   - midTarget)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        // Find a single inflection point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        // close to the middle of the byte-size cadre.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        mid = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    prevCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    prevLimit = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                Arrays.sort(fValues, prevLimit, high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // Sort up to the midpoint, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Arrays.sort(fValues, low, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                Arrays.sort(fValues, mid, high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            assert(tc.getLength(low) == tc.getLength(mid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            assert(tc.getLength(low) == tc.getLength(high-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            fillp = nmax+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        assert(fillp == fValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // Reset symtab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        symtab = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int getToken(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (symtab == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            symtab = makeSymtab();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int pos = Arrays.binarySearch(symtab, (long)value << 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (pos < 0)  pos = -pos-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (pos < symtab.length && value == (int)(symtab[pos] >>> 32))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return (int)symtab[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public int[][] encodeValues(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Compute token sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int[] tokens = new int[end-start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int nuv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i = 0; i < tokens.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int val = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            int tok = getToken(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (tok != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                tokens[i] = tok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                nuv += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Compute unfavored value sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int[] unfavoredValues = new int[nuv];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        nuv = 0;  // reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        for (int i = 0; i < tokens.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (tokens[i] != 0)  continue;  // already covered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            int val = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            unfavoredValues[nuv++] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        assert(nuv == unfavoredValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return new int[][]{ tokens, unfavoredValues };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private long[] makeSymtab() {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   222
        long[] lsymtab = new long[fVlen];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (int token = 1; token <= fVlen; token++) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   224
            lsymtab[token-1] = ((long)fValues[token] << 32) | token;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // Index by value:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   227
        Arrays.sort(lsymtab);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   228
        return lsymtab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private Coding getTailCoding(CodingMethod c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        while (c instanceof AdaptiveCoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            c = ((AdaptiveCoding)c).tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return (Coding) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // CodingMethod methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int[][] vals = encodeValues(a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        writeSequencesTo(out, vals[0], vals[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    void writeSequencesTo(OutputStream out, int[] tokens, int[] uValues) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        favoredCoding.writeArrayTo(out, fValues, 1, 1+fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        getTailCoding(favoredCoding).writeTo(out, computeSentinelValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        tokenCoding.writeArrayTo(out, tokens, 0, tokens.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (uValues.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            unfavoredCoding.writeArrayTo(out, uValues, 0, uValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
   int computeSentinelValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Coding fc = getTailCoding(favoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (fc.isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // repeat the last favored value, using delta=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // else repeat the shorter of the min or last value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int min = fValues[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int last = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // (remember that fVlen is an inclusive limit in fValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int i = 2; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                last = fValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                min = moreCentral(min, last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            int endVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (fc.getLength(min) <= fc.getLength(last))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // Parameters are fCode, L, uCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        setFavoredValues(readFavoredValuesFrom(in, end-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Read the tokens.  Read them into the final array, for the moment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        tokenCoding.readArrayFrom(in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Decode the favored tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int headp = 0, tailp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int uVlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            int tok = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (tok == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                // Make a linked list, and decode in a second pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (tailp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    headp = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    a[tailp] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                tailp = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                uVlen += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                a[i] = fValues[tok];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // Walk the linked list of "zero" locations, decoding unfavored vals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int[] uValues = new int[uVlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (uVlen > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            unfavoredCoding.readArrayFrom(in, uValues, 0, uVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for (int i = 0; i < uVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int nextp = a[headp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            a[headp] = uValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            headp = nextp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    int[] readFavoredValuesFrom(InputStream in, int maxForDebug) throws IOException {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   307
        int[] lfValues = new int[1000];  // realloc as needed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // The set uniqueValuesForDebug records all favored values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // As each new value is added, we assert that the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // was not already in the set.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   311
        Set<Integer> uniqueValuesForDebug = null;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   312
        assert((uniqueValuesForDebug = new HashSet<>()) != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int fillp = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        maxForDebug += fillp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int min = Integer.MIN_VALUE;  // farthest from the center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        //int min2 = Integer.MIN_VALUE;  // emulate buggy 150.7 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        CodingMethod fcm = favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        while (fcm instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            AdaptiveCoding ac = (AdaptiveCoding) fcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int len = ac.headLength;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   322
            while (fillp + len > lfValues.length) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   323
                lfValues = BandStructure.realloc(lfValues);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   324
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            int newFillp = fillp + len;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   326
            ac.headCoding.readArrayFrom(in, lfValues, fillp, newFillp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            while (fillp < newFillp) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   328
                int val = lfValues[fillp++];
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   329
                assert(uniqueValuesForDebug.add(val));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                //min2 = moreCentral2(min2, val, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            fcm = ac.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Coding fc = (Coding) fcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (fc.isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            for (long state = 0;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                // Read a new value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                state += fc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (fc.isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    val = fc.reduceToUnsignedRange(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    val = (int)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                state = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (fillp > 1 && (val == last || val == min)) //|| val == min2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    break;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   350
                if (fillp == lfValues.length)
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   351
                    lfValues = BandStructure.realloc(lfValues);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   352
                lfValues[fillp++] = val;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   353
                assert(uniqueValuesForDebug.add(val));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                //min2 = moreCentral(min2, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                int val = fc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (fillp > 1 && (val == last || val == min)) //|| val == min2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    break;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   364
                if (fillp == lfValues.length)
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   365
                    lfValues = BandStructure.realloc(lfValues);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   366
                lfValues[fillp++] = val;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   367
                assert(uniqueValuesForDebug.add(val));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                //min2 = moreCentral2(min2, val, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   374
        return BandStructure.realloc(lfValues, fillp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static int moreCentral(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int kx = (x >> 31) ^ (x << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int ky = (y >> 31) ^ (y << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // bias kx/ky to get an unsigned comparison:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        kx -= Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        ky -= Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int xy = (kx < ky? x: y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // assert that this ALU-ish version is the same:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        assert(xy == moreCentralSlow(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return xy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
//  private static int moreCentral2(int x, int y, int min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
//      // Strict implementation of buggy 150.7 specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
//      // The bug is that the spec. says absolute-value ties are broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
//      // in favor of positive numbers, but the suggested implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
//      // (also mentioned in the spec.) breaks ties in favor of negatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
//      if (x + y == 0)  return (x > y? x : y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
//      return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static int moreCentralSlow(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int ax = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (ax < 0)  ax = -ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (ax < 0)  return y;  //x is MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        int ay = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (ay < 0)  ay = -ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (ay < 0)  return x;  //y is MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (ax < ay)  return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (ax > ay)  return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // At this point the absolute values agree, and the negative wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return x < y ? x : y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    static final int[] LValuesCoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        = { -1, 4, 8, 16, 32, 64, 128, 192, 224, 240, 248, 252 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int K = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int LCoded = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (tokenCoding instanceof Coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (tc.B() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                LCoded = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } else if (L >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                assert(L == tc.L());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                for (int i = 1; i < LValuesCoded.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    if (LValuesCoded[i] == L) { LCoded = i; break; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        CodingMethod tokenDflt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (LCoded != 0 && tokenCoding == fitTokenCoding(fVlen, L)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // A simple L value is enough to recover the tokenCoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            tokenDflt = tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int FDef = (favoredCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int UDef = (unfavoredCoding == dflt || unfavoredCoding == null)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int TDef = (tokenCoding == tokenDflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int TDefL = (TDef == 1) ? LCoded : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        assert(TDef == ((TDefL>0)?1:0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        bytes.write(_meta_pop + FDef + 2*UDef + 4*TDefL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (FDef == 0)  bytes.write(favoredCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (TDef == 0)  bytes.write(tokenCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (UDef == 0)  bytes.write(unfavoredCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new RuntimeException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (op < _meta_pop || op >= _meta_limit)  return pos-1; // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        op -= _meta_pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int FDef = op % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        int UDef = (op / 2) % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int TDefL = (op / 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int TDef = (TDefL > 0)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int L = LValuesCoded[TDefL];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        CodingMethod[] FCode = {dflt}, TCode = {null}, UCode = {dflt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (FDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, FCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (TDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, TCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (UDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, UCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        PopulationCoding pop = new PopulationCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        pop.L = L;  // might be -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        pop.favoredCoding   = FCode[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        pop.tokenCoding     = TCode[0];  // might be null!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        pop.unfavoredCoding = UCode[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        res[0] = pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private String keyString(CodingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (m instanceof Coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return ((Coding)m).keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (m == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return "none";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return m.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        PropMap p200 = Utils.currentPropMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        boolean verbose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            = (p200 != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
               p200.getBoolean(Utils.COM_PREFIX+"verbose.pop"));
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   484
        StringBuilder res = new StringBuilder(100);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        res.append("pop(").append("fVlen=").append(fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (verbose && fValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            res.append(" fV=[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            for (int i = 1; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                res.append(i==1?"":",").append(fValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            res.append(";").append(computeSentinelValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            res.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        res.append(" fc=").append(keyString(favoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        res.append(" tc=").append(keyString(tokenCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        res.append(" uc=").append(keyString(unfavoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        res.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return res.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
}