/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.commons.compress.harmony.pack200; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.compress.utils.ExactMath; /** * A BHSD codec is a means of encoding integer values as a sequence of bytes or vice versa using a specified "BHSD" encoding mechanism. It uses a * variable-length encoding and a modified sign representation such that small numbers are represented as a single byte, whilst larger numbers take more bytes * to encode. The number may be signed or unsigned; if it is unsigned, it can be weighted towards positive numbers or equally distributed using a one's * complement. The Codec also supports delta coding, where a sequence of numbers is represented as a series of first-order differences. So a delta encoding of * the integers [1..10] would be represented as a sequence of 10x1s. This allows the absolute value of a coded integer to fall outside of the 'small number' * range, whilst still being encoded as a single byte. *
* A BHSD codec is configured with four parameters: *
** Codecs are notated as (B,H,S,D) and either D or S,D may be omitted if zero. Thus {@link #BYTE1} is denoted (1,256,0,0) or (1,256). The {@link #toString()} * method prints out the condensed form of the encoding. Often, the last character in the name ({@link #BYTE1}, {@link #UNSIGNED5}) gives a clue as to the B * value. Those that start with U ({@link #UDELTA5}, {@link #UNSIGNED5}) are unsigned; otherwise, in most cases, they are signed. The presence of the word Delta * ({@link #DELTA5}, {@link #UDELTA5}) indicates a delta encoding is used. *
*/ public final class BHSDCodec extends Codec { /** * The maximum number of bytes in each coding word. B must be a value between [1..5]. For a pass-through coding (where each byte is encoded as itself, aka * {@link #BYTE1}, B is 1 (each byte takes a maximum of 1 byte). */ private final int b; /** * Whether delta encoding is used (0=false,1=true). */ private final int d; /** * The radix of the encoding. */ private final int h; /** * The co-parameter of h; 256-h. */ private final int l; /** * Represents signed numbers or not, 0 (unsigned), 1 (signed, one's complement) or 2 (signed, two's complement). */ private final int s; private long cardinality; private final long smallest; private final long largest; /** * radix^i powers */ private final long[] powers; /** * Constructs an unsigned, non-delta Codec with the given B and H values. * * @param b the maximum number of bytes that a value can be encoded as [1..5] * @param h the radix of the encoding [1..256] */ public BHSDCodec(final int b, final int h) { this(b, h, 0, 0); } /** * Constructs a non-delta Codec with the given B, H and S values. * * @param b the maximum number of bytes that a value can be encoded as [1..5] * @param h the radix of the encoding [1..256] * @param s whether the encoding represents signed numbers (s=0 is unsigned; s=1 is signed with 1s complement; s=2 is signed with ?) */ public BHSDCodec(final int b, final int h, final int s) { this(b, h, s, 0); } /** * Constructs a Codec with the given B, H, S and D values. * * @param b the maximum number of bytes that a value can be encoded as [1..5] * @param h the radix of the encoding [1..256] * @param s whether the encoding represents signed numbers (s=0 is unsigned; s=1 is signed with 1s complement; s=2 is signed with ?) * @param d whether this is a delta encoding (d=0 is non-delta; d=1 is delta) */ public BHSDCodec(final int b, final int h, final int s, final int d) { if (b < 1 || b > 5) { throw new IllegalArgumentException("1<=b<=5"); } if (h < 1 || h > 256) { throw new IllegalArgumentException("1<=h<=256"); } if (s < 0 || s > 2) { throw new IllegalArgumentException("0<=s<=2"); } if (d < 0 || d > 1) { throw new IllegalArgumentException("0<=d<=1"); } if (b == 1 && h != 256) { throw new IllegalArgumentException("b=1 -> h=256"); } if (h == 256 && b == 5) { throw new IllegalArgumentException("h=256 -> b!=5"); } this.b = b; this.h = h; this.s = s; this.d = d; this.l = 256 - h; if (h == 1) { cardinality = b * 255 + 1; } else { cardinality = (long) ((long) (l * (1 - Math.pow(h, b)) / (1 - h)) + Math.pow(h, b)); } smallest = calculateSmallest(); largest = calculateLargest(); powers = new long[b]; Arrays.setAll(powers, c -> (long) Math.pow(h, c)); } private long calculateLargest() { final long result; // TODO This can probably be optimized into a better mathematical statement if (d == 1) { return new BHSDCodec(b, h).largest(); } switch (s) { case 0: result = cardinality() - 1; break; case 1: result = cardinality() / 2 - 1; break; case 2: result = 3L * cardinality() / 4 - 1; break; default: throw new Error("Unknown s value"); } return Math.min((s == 0 ? (long) Integer.MAX_VALUE << 1 : Integer.MAX_VALUE) - 1, result); } private long calculateSmallest() { final long result; if (d == 1 || !isSigned()) { if (cardinality >= 4294967296L) { // 2^32 result = Integer.MIN_VALUE; } else { result = 0; } } else { result = Math.max(Integer.MIN_VALUE, -cardinality() / (1 << s)); } return result; } /** * Returns the cardinality of this codec; that is, the number of distinct values that it can contain. * * @return the cardinality of this codec */ public long cardinality() { return cardinality; } @Override public int decode(final InputStream in) throws IOException, Pack200Exception { if (d != 0) { throw new Pack200Exception("Delta encoding used without passing in last value; this is a coding error"); } return decode(in, 0); } @Override public int decode(final InputStream in, final long last) throws IOException, Pack200Exception { int n = 0; long z = 0; long x = 0; do { x = in.read(); lastBandLength++; z += x * powers[n]; n++; } while (x >= l && n < b); if (x == -1) { throw new EOFException("End of stream reached whilst decoding"); } if (isSigned()) { final int u = (1 << s) - 1; if ((z & u) == u) { z = z >>> s ^ -1L; } else { z -= z >>> s; } } // This algorithm does the same thing, but is probably slower. Leaving // in for now for readability // if (isSigned()) { // long u = z; // long twoPowS = (long) Math.pow(2, s); // double twoPowSMinusOne = twoPowS-1; // if (u % twoPowS < twoPowSMinusOne) { // if (cardinality < Math.pow(2, 32)) { // z = (long) (u - (Math.floor(u/ twoPowS))); // } else { // z = cast32((long) (u - (Math.floor(u/ twoPowS)))); // } // } else { // z = (long) (-Math.floor(u/ twoPowS) - 1); // } // } if (isDelta()) { z += last; } return (int) z; } // private long cast32(long u) { // u = (long) ((long) ((u + Math.pow(2, 31)) % Math.pow(2, 32)) - // Math.pow(2, 31)); // return u; // } @Override public int[] decodeInts(final int n, final InputStream in) throws IOException, Pack200Exception { final int[] band = super.decodeInts(n, in); if (isDelta()) { for (int i = 0; i < band.length; i++) { while (band[i] > largest) { band[i] -= cardinality; } while (band[i] < smallest) { band[i] = ExactMath.add(band[i], cardinality); } } } return band; } @Override public int[] decodeInts(final int n, final InputStream in, final int firstValue) throws IOException, Pack200Exception { final int[] band = super.decodeInts(n, in, firstValue); if (isDelta()) { for (int i = 0; i < band.length; i++) { while (band[i] > largest) { band[i] -= cardinality; } while (band[i] < smallest) { band[i] = ExactMath.add(band[i], cardinality); } } } return band; } @Override public byte[] encode(final int value) throws Pack200Exception { return encode(value, 0); } @Override public byte[] encode(final int value, final int last) throws Pack200Exception { if (!encodes(value)) { throw new Pack200Exception("The codec " + this + " does not encode the value " + value); } long z = value; if (isDelta()) { z -= last; } if (isSigned()) { if (z < Integer.MIN_VALUE) { z += 4294967296L; } else if (z > Integer.MAX_VALUE) { z -= 4294967296L; } if (z < 0) { z = (-z << s) - 1; } else if (s == 1) { z = z << s; } else { z += (z - z % 3) / 3; } } else if (z < 0) { // Need to use integer overflow here to represent negatives. // 4294967296L is the 1 << 32. z += Math.min(cardinality, 4294967296L); } if (z < 0) { throw new Pack200Exception("unable to encode"); } final List