jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Coding.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 28059 e576535359cc
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
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: 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.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.HashMap;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    32
import java.util.Map;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    33
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Define the conversions between sequences of small integers and raw bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This is a schema of encodings which incorporates varying lengths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * varying degrees of length variability, and varying amounts of signed-ness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
    40
class Coding implements Comparable<Coding>, CodingMethod, Histogram.BitMetric {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
      Coding schema for single integers, parameterized by (B,H,S):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
      Let B in [1,5], H in [1,256], S in [0,3].
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
      (S limit is arbitrary.  B follows the 32-bit limit.  H is byte size.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
      A given (B,H,S) code varies in length from 1 to B bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
      The 256 values a byte may take on are divided into L=(256-H) and H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
      values, with all the H values larger than the L values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
      (That is, the L values are [0,L) and the H are [L,256).)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
      The last byte is always either the B-th byte, a byte with "L value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
      (<L), or both.  There is no other byte that satisfies these conditions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
      All bytes before the last always have "H values" (>=L).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      Therefore, if L==0, the code always has the full length of B bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      The coding then becomes a classic B-byte little-endian unsigned integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      (Also, if L==128, the high bit of each byte acts signals the presence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
      of a following byte, up to the maximum length.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      In the unsigned case (S==0), the coding is compact and monotonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      in the ordering of byte sequences defined by appending zero bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      to pad them to a common length B, reversing them, and ordering them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      lexicographically.  (This agrees with "little-endian" byte order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      Therefore, the unsigned value of a byte sequence may be defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        U(b0)           == b0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                           in [0..L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                           or [0..256) if B==1 (**)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        U(b0,b1)        == b0 + b1*H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                           in [L..L*(1+H))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                           or [L..L*(1+H) + H^2) if B==2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        U(b0,b1,b2)     == b0 + b1*H + b2*H^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                           in [L*(1+H)..L*(1+H+H^2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                           or [L*(1+H)..L*(1+H+H^2) + H^3) if B==3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        U(b[i]: i<n)    == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           up to  L*Sum[i<n]( H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                           or to  L*Sum[i<n]( H^i ) + H^n if n==B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      (**) If B==1, the values H,L play no role in the coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      As a convention, we require that any (1,H,S) code must always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      encode values less than H.  Thus, a simple unsigned byte is coded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      specifically by the code (1,256,0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      (Properly speaking, the unsigned case should be parameterized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      S==Infinity.  If the schema were regular, the case S==0 would really
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      denote a numbering in which all coded values are negative.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      If S>0, the unsigned value of a byte sequence is regarded as a binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      integer.  If any of the S low-order bits are zero, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      signed value will be non-negative.  If all of the S low-order bits
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    98
      (S>0) are one, the corresponding signed value will be negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      The non-negative signed values are compact and monotonically increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      (from 0) in the ordering of the corresponding unsigned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      The negative signed values are compact and monotonically decreasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      (from -1) in the ordering of the corresponding unsigned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      In essence, the low-order S bits function as a collective sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      for negative signed numbers, and as a low-order base-(2^S-1) digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      for non-negative signed numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      Therefore, the signed value corresponding to an unsigned value is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Sgn(x)  == x                               if S==0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Sgn(x)  == (x / 2^S)*(2^S-1) + (x % 2^S),  if S>0, (x % 2^S) < 2^S-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        Sgn(x)  == -(x / 2^S)-1,                   if S>0, (x % 2^S) == 2^S-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      Finally, the value of a byte sequence, given the coding parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      (B,H,S), is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        V(b[i]: i<n)  == Sgn(U(b[i]: i<n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      The extremal positive and negative signed value for a given range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      of unsigned values may be found by sign-encoding the largest unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
      value which is not 2^S-1 mod 2^S, and that which is, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      Because B,H,S are variable, this is not a single coding but a schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      of codings.  For optimal compression, it is necessary to adaptively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      select specific codings to the data being compressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      For example, if a sequence of values happens never to be negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      S==0 is the best choice.  If the values are equally balanced between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      negative and positive, S==1.  If negative values are rare, then S>1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      is more appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      A (B,H,S) encoding is called a "subrange" if it does not encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      the largest 32-bit value, and if the number R of values it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      encode can be expressed as a positive 32-bit value.  (Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      B=1 implies R<=256, B=2 implies R<=65536, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      A delta version of a given (B,H,S) coding encodes an array of integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      by writing their successive differences in the (B,H,S) coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      The original integers themselves may be recovered by making a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      running accumulation of sum of the differences as they are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      As a special case, if a (B,H,S) encoding is a subrange, its delta
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      version will only encode arrays of numbers in the coding's unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      range, [0..R-1].  The coding of deltas is still in the normal signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      range, if S!=0.  During delta encoding, all subtraction results are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
      reduced to the signed range, by adding multiples of R.  Likewise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
.     during encoding, all addition results are reduced to the unsigned range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      This special case for subranges allows the benefits of wraparound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      when encoding correlated sequences of very small positive numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // Code-specific limits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static int saturate32(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (x > Integer.MAX_VALUE)   return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (x < Integer.MIN_VALUE)   return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (int)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static long codeRangeLong(int B, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return codeRangeLong(B, H, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static long codeRangeLong(int B, int H, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Code range for a all (B,H) codes of length <=nMax (<=B).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // n < B:   L*Sum[i<n]( H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // n == B:  L*Sum[i<B]( H^i ) + H^B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        assert(nMax >= 0 && nMax <= B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        assert(B >= 1 && B <= 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        assert(H >= 1 && H <= 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (nMax == 0)  return 0;  // no codes of zero length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (B == 1)     return H;  // special case; see (**) above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int n = 1; n <= nMax; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            sum += H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        sum *= L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (nMax == B)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            sum += H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /** Largest int representable by (B,H,S) in up to nMax bytes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static int codeMax(int B, int H, int S, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //assert(S >= 0 && S <= S_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        long range = codeRangeLong(B, H, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (range == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return -1;  // degenerate max value for empty set of codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (S == 0 || range >= (long)1<<32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return saturate32(range-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        long maxPos = range-1;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   195
        while (isNegativeCode(maxPos, S)) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   196
            --maxPos;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   197
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (maxPos < 0)  return -1;  // No positive codings at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int smax = decodeSign32(maxPos, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // check for 32-bit wraparound:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (smax < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return smax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /** Smallest int representable by (B,H,S) in up to nMax bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Returns Integer.MIN_VALUE if 32-bit wraparound covers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        the entire negative range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public static int codeMin(int B, int H, int S, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        //assert(S >= 0 && S <= S_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        long range = codeRangeLong(B, H, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (range >= (long)1<<32 && nMax == B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // Can code negative values via 32-bit wraparound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        long maxNeg = range-1;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   220
        while (!isNegativeCode(maxNeg, S))
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   221
            --maxNeg;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   222
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (maxNeg < 0)  return 0;  // No negative codings at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return decodeSign32(maxNeg, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    // Some of the arithmetic below is on unsigned 32-bit integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // These must be represented in Java as longs in the range [0..2^32-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // The conversion to a signed int is just the Java cast (int), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // the conversion to an unsigned int is the following little method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static long toUnsigned32(int sx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return ((long)sx << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // Sign encoding:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private static boolean isNegativeCode(long ux, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        assert(S > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        assert(ux >= -1);  // can be out of 32-bit range; who cares
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int Smask = (1<<S)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return (((int)ux+1) & Smask) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static boolean hasNegativeCode(int sx, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        assert(S > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // If S>=2 very low negatives are coded by 32-bit-wrapped positives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // The lowest negative representable by a negative coding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // ~(umax32 >> S), and the next lower number is coded by wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // the highest positive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //    CodePos(umax32-1)  ->  (umax32-1)-((umax32-1)>>S)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // which simplifies to ~(umax32 >> S)-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return (0 > sx) && (sx >= ~(-1>>>S));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static int decodeSign32(long ux, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        assert(ux == toUnsigned32((int)ux))  // must be unsigned 32-bit number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            : (Long.toHexString(ux));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return (int) ux;  // cast to signed int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (isNegativeCode(ux, S)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // Sgn(x)  == -(x / 2^S)-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            sx = ~((int)ux >>> S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // Sgn(x)  == (x / 2^S)*(2^S-1) + (x % 2^S)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            sx = (int)ux - ((int)ux >>> S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // Assert special case of S==1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        assert(!(S == 1) || sx == (((int)ux >>> 1) ^ -((int)ux & 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static long encodeSign32(int sx, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return toUnsigned32(sx);  // unsigned 32-bit int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int Smask = (1<<S)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        long ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (!hasNegativeCode(sx, S)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // InvSgn(sx) = (sx / (2^S-1))*2^S + (sx % (2^S-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            ux = sx + (toUnsigned32(sx) / Smask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // InvSgn(sx) = (-sx-1)*2^S + (2^S-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ux = (-sx << S) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        ux = toUnsigned32((int)ux);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        assert(sx == decodeSign32(ux, S))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            : (Long.toHexString(ux)+" -> "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
               Integer.toHexString(sx)+" != "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
               Integer.toHexString(decodeSign32(ux, S)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Top-level coding of single integers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static void writeInt(byte[] out, int[] outpos, int sx, int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        long ux = encodeSign32(sx, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        assert(ux == toUnsigned32((int)ux));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        assert(ux < codeRangeLong(B, H))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            : Long.toHexString(ux);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        long sum = ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int pos = outpos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        for (int i = 0; i < B-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (sum < L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            sum -= L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            int b_i = (int)( L + (sum % H) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            sum /= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            out[pos++] = (byte)b_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        out[pos++] = (byte)sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // Report number of bytes written by updating outpos[0]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        outpos[0] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Check right away for mis-coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        //assert(sx == readInt(out, new int[1], B, H, S));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public static int readInt(byte[] in, int[] inpos, int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int pos = inpos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        for (int i = 0; i < B; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int b_i = in[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            sum += b_i*H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (b_i < L)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        //assert(sum >= 0 && sum < codeRangeLong(B, H));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // Report number of bytes read by updating inpos[0]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        inpos[0] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return decodeSign32(sum, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    // The Stream version doesn't fetch a byte unless it is needed for coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public static int readIntFrom(InputStream in, int B, int H, int S) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for (int i = 0; i < B; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int b_i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (b_i < 0)  throw new RuntimeException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            sum += b_i*H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (b_i < L)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        assert(sum >= 0 && sum < codeRangeLong(B, H));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return decodeSign32(sum, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public static final int B_MAX = 5;    /* B: [1,5] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public static final int H_MAX = 256;  /* H: [1,256] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public static final int S_MAX = 2;    /* S: [0,2] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    // END OF STATICS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private final int B; /*1..5*/       // # bytes (1..5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private final int H; /*1..256*/     // # codes requiring a higher byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private final int L; /*0..255*/     // # codes requiring a higher byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private final int S; /*0..3*/       // # low-order bits representing sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private final int del; /*0..2*/     // type of delta encoding (0 == none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private final int min;              // smallest representable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private final int max;              // largest representable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private final int umin;             // smallest representable uns. value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private final int umax;             // largest representable uns. value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private final int[] byteMin;        // smallest repr. value, given # bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private final int[] byteMax;        // largest repr. value, given # bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private Coding(int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        this(B, H, S, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private Coding(int B, int H, int S, int del) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        this.B = B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        this.H = H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        this.L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        this.S = S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        this.del = del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        this.min = codeMin(B, H, S, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        this.max = codeMax(B, H, S, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        this.umin = codeMin(B, H, 0, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        this.umax = codeMax(B, H, 0, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        this.byteMin = new int[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.byteMax = new int[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        for (int nMax = 1; nMax <= B; nMax++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            byteMin[nMax-1] = codeMin(B, H, S, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            byteMax[nMax-1] = codeMax(B, H, S, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (!(x instanceof Coding))  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Coding that = (Coding) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (this.B != that.B)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (this.H != that.H)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (this.S != that.S)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (this.del != that.del)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return (del<<14)+(S<<11)+(B<<8)+(H<<0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   402
    private static Map<Coding, Coding> codeMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private static synchronized Coding of(int B, int H, int S, int del) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   405
        if (codeMap == null)  codeMap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        Coding x0 = new Coding(B, H, S, del);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   407
        Coding x1 = codeMap.get(x0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (x1 == null)  codeMap.put(x0, x1 = x0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public static Coding of(int B, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return of(B, H, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public static Coding of(int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return of(B, H, S, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public boolean canRepresentValue(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return canRepresentUnsigned(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return canRepresentSigned(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /** Can this coding represent a single value, possibly a delta?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *  This ignores the D property.  That is, for delta codings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *  this tests whether a delta value of 'x' can be coded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *  For signed delta codings which produce unsigned end values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *  use canRepresentUnsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public boolean canRepresentSigned(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return (x >= min && x <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /** Can this coding, apart from its S property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *  represent a single value?  (Negative values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *  can only be represented via 32-bit overflow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *  so this returns true for negative values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *  if isFullRange is true.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public boolean canRepresentUnsigned(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return (x >= umin && x <= umax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    // object-oriented code/decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public int readFrom(byte[] in, int[] inpos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return readInt(in, inpos, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public void writeTo(byte[] out, int[] outpos, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        writeInt(out, outpos, x, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // Stream versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public int readFrom(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return readIntFrom(in, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public void writeTo(OutputStream out, int x) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        byte[] buf = new byte[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int[] pos = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        writeInt(buf, pos, x, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        out.write(buf, 0, pos[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    // Stream/array versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // %%% use byte[] buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            a[i] = readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   469
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        for (int dstep = 0; dstep < del; dstep++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            long state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                state += a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                // Reduce array values to the required range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    state = reduceToUnsignedRange(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                a[i] = (int) state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (end <= start)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        for (int dstep = 0; dstep < del; dstep++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            int[] deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (!isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                deltas = makeDeltas(a, start, end, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                deltas = makeDeltas(a, start, end, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            a = deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            end = deltas.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // The following code is a buffered version of this loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        //    for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        //        writeTo(out, a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        byte[] buf = new byte[1<<8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        final int bufmax = buf.length-B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int[] pos = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        for (int i = start; i < end; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            while (pos[0] <= bufmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                writeTo(buf, pos, a[i++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (i >= end)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            out.write(buf, 0, pos[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            pos[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /** Tell if the range of this coding (number of distinct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *  representable values) can be expressed in 32 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    boolean isSubrange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return max < Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            && ((long)max - (long)min + 1) <= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /** Tell if this coding can represent all 32-bit values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *  Note:  Some codings, such as unsigned ones, can be neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *  subranges nor full-range codings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    boolean isFullRange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return max == Integer.MAX_VALUE && min == Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /** Return the number of values this coding (a subrange) can represent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    int getRange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        assert(isSubrange());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return (max - min) + 1;  // range includes both min & max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    Coding setB(int B) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    Coding setH(int H) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    Coding setS(int S) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    Coding setL(int L) { return setH(256-L); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    Coding setD(int del) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    Coding getDeltaCoding() { return setD(del+1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /** Return a coding suitable for representing summed, modulo-reduced values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    Coding getValueCoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (isDelta())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return Coding.of(B, H, 0, del-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /** Reduce the given value to be within this coding's unsigned range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *  by adding or subtracting a multiple of (max-min+1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    int reduceToUnsignedRange(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (value == (int)value && canRepresentUnsigned((int)value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // already in unsigned range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int range = getRange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        assert(range > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        value %= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (value < 0)  value += range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        assert(canRepresentUnsigned((int)value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    int reduceToSignedRange(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (canRepresentSigned(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            // already in signed range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return reduceToSignedRange(value, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    static int reduceToSignedRange(int value, int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int range = (max-min+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        assert(range > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        int value0 = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        value -= min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (value < 0 && value0 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // 32-bit overflow, but the next '%=' op needs to be unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            value -= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            assert(value >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        value %= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (value < 0)  value += range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        value += min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        assert(min <= value && value <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /** Does this coding support at least one negative value?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        Includes codings that can do so via 32-bit wraparound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    boolean isSigned() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return min < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /** Does this coding code arrays by making successive differences? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    boolean isDelta() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return del != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public int B() { return B; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public int H() { return H; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public int L() { return L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public int S() { return S; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public int del() { return del; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public int min() { return min; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public int max() { return max; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public int umin() { return umin; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public int umax() { return umax; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public int byteMin(int b) { return byteMin[b-1]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public int byteMax(int b) { return byteMax[b-1]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   608
    public int compareTo(Coding that) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int dkey = this.del - that.del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            dkey = this.B - that.B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            dkey = this.H - that.H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            dkey = this.S - that.S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return dkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /** Heuristic measure of the difference between two codings. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public int distanceFrom(Coding that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int diffdel = this.del - that.del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (diffdel < 0)  diffdel = -diffdel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        int diffS = this.S - that.S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (diffS < 0)  diffS = -diffS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        int diffB = this.B - that.B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (diffB < 0)  diffB = -diffB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int diffHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (this.H == that.H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            diffHL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            // Distance in log space of H (<=128) and L (<128).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            int thisHL = this.getHL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            int thatHL = that.getHL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            // Double the accuracy of the log:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            thisHL *= thisHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            thatHL *= thatHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (thisHL > thatHL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                diffHL = ceil_lg2(1+(thisHL-1)/thatHL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                diffHL = ceil_lg2(1+(thatHL-1)/thisHL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int norm = 5*(diffdel + diffS + diffB) + diffHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        assert(norm != 0 || this.compareTo(that) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return norm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    private int getHL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // Follow H in log space by the multiplicative inverse of L.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (H <= 128)  return H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (L >= 1)    return 128*128/L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return 128*256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /** ceiling(log[2](x)): {1->0, 2->1, 3->2, 4->2, ...} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    static int ceil_lg2(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        assert(x-1 >= 0);  // x in range (int.MIN_VALUE -> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        x -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        int lg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        while (x != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            lg++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            x >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return lg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 28059
diff changeset
   665
    private static final byte[] byteBitWidths = new byte[0x100];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        for (int b = 0; b < byteBitWidths.length; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            byteBitWidths[b] = (byte) ceil_lg2(b + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        for (int i = 10; i >= 0; i = (i << 1) - (i >> 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            assert(bitWidth(i) == ceil_lg2(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /** Number of significant bits in i, not counting sign bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *  For positive i, it is ceil_lg2(i + 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    static int bitWidth(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (i < 0)  i = ~i;  // change sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        int lo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (lo < byteBitWidths.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return byteBitWidths[lo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        int hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        hi = (lo >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (hi != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            lo = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            w += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        hi = (lo >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (hi != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            lo = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            w += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        w += byteBitWidths[lo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        //assert(w == ceil_lg2(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /** Create an array of successive differences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *  If min==max, accept any and all 32-bit overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *  Otherwise, avoid 32-bit overflow, and reduce all differences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *  to a value in the given range, by adding or subtracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *  multiples of the range cardinality (max-min+1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *  Also, the values are assumed to be in the range [0..(max-min)].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    static int[] makeDeltas(int[] values, int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                            int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        assert(max >= min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        int count = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int[] deltas = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        int state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (min == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                deltas[i] = value - state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                state = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                assert(value >= 0 && value+min <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                int delta = value - state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                assert(delta == (long)value - (long)state); // no overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                state = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                // Reduce delta values to the required range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                delta = reduceToSignedRange(delta, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                deltas[i] = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    boolean canRepresent(int minValue, int maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        assert(minValue <= maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (del > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // We will force the values to reduce to the right subrange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                return canRepresentUnsigned(maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    && canRepresentUnsigned(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                // Huge range; delta values must assume full 32-bit range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                return isFullRange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            // final values must be representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return canRepresentSigned(maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                && canRepresentSigned(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    boolean canRepresent(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (len == 0)       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (isFullRange())  return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // Calculate max, min:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   757
        int lmax = values[start];
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   758
        int lmin = lmax;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        for (int i = 1; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            int value = values[start+i];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   761
            if (lmax < value)  lmax = value;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   762
            if (lmin > value)  lmin = value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   764
        return canRepresent(lmin, lmax);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public double getBitLength(int value) {  // implements BitMetric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        return (double) getLength(value) * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    /** How many bytes are in the coding of this value?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *  Returns Integer.MAX_VALUE if the value has no coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *  The coding must not be a delta coding, since there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *  definite size for a single value apart from its context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    public int getLength(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (isDelta() && isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (!canRepresentUnsigned(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            value = reduceToSignedRange(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            for (int n = 0; n < B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (value <= byteMax[n])  return n+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            for (int n = 0; n < B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                if (value >= byteMin[n])  return n+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public int getLength(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (B == 1)  return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (L == 0)  return len * B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            int[] deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (!isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                deltas = makeDeltas(values, start, end, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                deltas = makeDeltas(values, start, end, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            //return Coding.of(B, H, S).getLength(deltas, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            values = deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        int sum = len;  // at least 1 byte per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // add extra bytes for extra-long values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        for (int n = 1; n <= B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            // what is the coding interval [min..max] for n bytes?
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   812
            int lmax = byteMax[n-1];
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   813
            int lmin = byteMin[n-1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            int longer = 0;  // count of guys longer than n bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                if (value >= 0) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   818
                    if (value > lmax)  longer++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                } else {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   820
                    if (value < lmin)  longer++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            if (longer == 0)  break;  // no more passes needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            if (n == B)  return Integer.MAX_VALUE;  // cannot represent!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            sum += longer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (dflt == this)  return new byte[]{ (byte) _meta_default };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        int canonicalIndex = BandStructure.indexOf(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (canonicalIndex > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            return new byte[]{ (byte) canonicalIndex };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        return new byte[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            (byte)_meta_arb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            (byte)(del + 2*S + 8*(B-1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            (byte)(H-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (_meta_canon_min <= op && op <= _meta_canon_max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            Coding c = BandStructure.codingForIndex(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            assert(c != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            res[0] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (op == _meta_arb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            int dsb = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            int H_1 = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            int del = dsb % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            int S = (dsb / 2) % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            int B = (dsb / 8)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            int H = H_1+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            if (!((1 <= B && B <= B_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                  (0 <= S && S <= S_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                  (1 <= H && H <= H_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                  (0 <= del && del <= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                || (B == 1 && H != 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                || (B == 5 && H == 256)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                throw new RuntimeException("Bad arb. coding: ("+B+","+H+","+S+","+del);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            res[0] = Coding.of(B, H, S, del);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        return pos-1;  // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public String keyString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        return "("+B+","+H+","+S+","+del+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        String str = "Coding"+keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        // If -ea, print out more informative strings!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        //assert((str = stringForDebug()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    static boolean verboseStringForDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    String stringForDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        String minS = (min == Integer.MIN_VALUE ? "min" : ""+min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        String maxS = (max == Integer.MAX_VALUE ? "max" : ""+max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        String str = keyString()+" L="+L+" r=["+minS+","+maxS+"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        if (isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            str += " subrange";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        else if (!isFullRange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            str += " MIDRANGE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (verboseStringForDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            str += " {";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            int prev_range = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            for (int n = 1; n <= B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                int range_n = saturate32((long)byteMax[n-1] - byteMin[n-1] + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                assert(range_n == saturate32(codeRangeLong(B, H, n)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                range_n -= prev_range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                prev_range = range_n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                String rngS = (range_n == Integer.MAX_VALUE ? "max" : ""+range_n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                str += " #"+n+"="+rngS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            str += " }";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
}