jdk/src/share/classes/com/sun/java/util/jar/pack/AdaptiveCoding.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;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    32
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Adaptive coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * See the section "Adaptive Encodings" in the Pack200 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    39
class AdaptiveCoding implements CodingMethod {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    CodingMethod headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int          headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    CodingMethod tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public AdaptiveCoding(int headLength, CodingMethod headCoding, CodingMethod tailCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        assert(isCodableLength(headLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        this.headLength = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.headCoding = headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.tailCoding = tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void setHeadCoding(CodingMethod headCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.headCoding = headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public void setHeadLength(int headLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        assert(isCodableLength(headLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.headLength = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void setTailCoding(CodingMethod tailCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.tailCoding = tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public boolean isTrivial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return headCoding == tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // CodingMethod methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        writeArray(this, out, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // writeArrayTo must be coded iteratively, not recursively:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static void writeArray(AdaptiveCoding run, OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int mid = start+run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            assert(mid <= end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            run.headCoding.writeArrayTo(out, a, start, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            start = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        run.tailCoding.writeArrayTo(out, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        readArray(this, in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static void readArray(AdaptiveCoding run, InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int mid = start+run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            assert(mid <= end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            run.headCoding.readArrayFrom(in, a, start, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            start = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        run.tailCoding.readArrayFrom(in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final int KX_MIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static final int KX_MAX = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final int KX_LG2BASE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int KX_BASE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int KB_MIN = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static final int KB_MAX = 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static final int KB_OFFSET = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static final int KB_DEFAULT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static int getKXOf(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int KX = KX_MIN; KX <= KX_MAX; KX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (((K - KB_OFFSET) & ~KB_MAX) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return KX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            K >>>= KX_LG2BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static int getKBOf(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (KX < 0)  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        K >>>= (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return K-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static int decodeK(int KX, int KB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        assert(KX_MIN <= KX && KX <= KX_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        assert(KB_MIN <= KB && KB <= KB_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return (KB+KB_OFFSET) << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static int getNextK(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (K <= 0)  return 1;  // 1st K value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (KX < 0)  return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // This is the increment we expect to apply:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int unit = 1      << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int mask = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int K1 = K + unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        K1 &= ~(unit-1);  // cut off stray low-order bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (((K1 - unit) & ~mask) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            assert(getKXOf(K1) == KX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return K1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (KX == KX_MAX)  return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        KX += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int mask2 = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        K1 |= (mask & ~mask2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        K1 += unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        assert(getKXOf(K1) == KX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return K1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // Is K of the form ((KB:[0..255])+1) * 16^(KX:{0..3])?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static boolean isCodableLength(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (KX < 0)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int unit = 1      << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int mask = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return ((K - unit) & ~mask) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //assert(!isTrivial()); // can happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // See the isCodableLength restriction in CodingChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            makeMetaCoding(this, dflt, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new RuntimeException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static void makeMetaCoding(AdaptiveCoding run, Coding dflt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                       ByteArrayOutputStream bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            CodingMethod headCoding = run.headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            int          headLength = run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            CodingMethod tailCoding = run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int K = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            assert(isCodableLength(K));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            int ADef   = (headCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int BDef   = (tailCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (ADef+BDef > 1)  BDef = 0;  // arbitrary choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            int ABDef  = 1*ADef + 2*BDef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            assert(ABDef < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int KX     = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int KB     = getKBOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            assert(decodeK(KX, KB) == K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            int KBFlag = (KB != KB_DEFAULT)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            bytes.write(_meta_run + KX + 4*KBFlag + 8*ABDef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (KBFlag != 0)    bytes.write(KB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (ADef == 0)  bytes.write(headCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                run = (AdaptiveCoding) tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                continue; // tail call, to avoid deep stack recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (BDef == 0)  bytes.write(tailCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (op < _meta_run || op >= _meta_pop)  return pos-1; // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        AdaptiveCoding prevc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (boolean keepGoing = true; keepGoing; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            keepGoing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            assert(op >= _meta_run);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            op -= _meta_run;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            int KX = op % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            int KBFlag = (op / 4) % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            int ABDef = (op / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            assert(ABDef < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            int ADef = (ABDef & 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            int BDef = (ABDef & 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            CodingMethod[] ACode = {dflt}, BCode = {dflt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int KB = KB_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (KBFlag != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                KB = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (ADef == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                pos = BandStructure.parseMetaCoding(bytes, pos, dflt, ACode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (BDef == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                ((op = bytes[pos] & 0xFF) >= _meta_run) && op < _meta_pop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                keepGoing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else if (BDef == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                pos = BandStructure.parseMetaCoding(bytes, pos, dflt, BCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            AdaptiveCoding newc = new AdaptiveCoding(decodeK(KX, KB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                                     ACode[0], BCode[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (prevc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                res[0] = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                prevc.tailCoding = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            prevc = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private String keyString(CodingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (m instanceof Coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return ((Coding)m).keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return m.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public String toString() {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   253
        StringBuilder res = new StringBuilder(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        AdaptiveCoding run = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        res.append("run(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            res.append(run.headLength).append("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            res.append(keyString(run.headCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                res.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        res.append(" **").append(keyString(run.tailCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        res.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return res.toString();
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 static void main(String av[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int[][] samples = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            {1,2,3,4,5},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            {254,255,256,256+1*16,256+2*16},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            {0xfd,0xfe,0xff,0x100,0x110,0x120,0x130},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            {0xfd0,0xfe0,0xff0,0x1000,0x1100,0x1200,0x1300},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            {0xfd00,0xfe00,0xff00,0x10000,0x11000,0x12000,0x13000},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            {0xfd000,0xfe000,0xff000,0x100000}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i = 0; i < samples.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            for (int j = 0; j < samples[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int K = samples[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                int KB = getKBOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                System.out.println("K="+Integer.toHexString(K)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                   " KX="+KX+" KB="+KB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                assert(isCodableLength(K));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                assert(K == decodeK(KX, KB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (j == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                int K1 = samples[i][j-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                assert(K == getNextK(K1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}