001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.harmony.pack200;
018
019import java.io.EOFException;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025
026import org.apache.commons.compress.utils.ExactMath;
027
028/**
029 * A BHSD codec is a means of encoding integer values as a sequence of bytes or vice versa using a specified "BHSD"
030 * encoding mechanism. It uses a variable-length encoding and a modified sign representation such that small numbers are
031 * represented as a single byte, whilst larger numbers take more bytes to encode. The number may be signed or unsigned;
032 * if it is unsigned, it can be weighted towards positive numbers or equally distributed using a one's complement. The
033 * Codec also supports delta coding, where a sequence of numbers is represented as a series of first-order differences.
034 * So a delta encoding of the integers [1..10] would be represented as a sequence of 10x1s. This allows the absolute
035 * value of a coded integer to fall outside of the 'small number' range, whilst still being encoded as a single byte.
036 *
037 * A BHSD codec is configured with four parameters:
038 * <dl>
039 * <dt>B</dt>
040 * <dd>The maximum number of bytes that each value is encoded as. B must be a value between [1..5]. For a pass-through
041 * coding (where each byte is encoded as itself, aka {@link #BYTE1}, B is 1 (each byte takes a maximum of 1 byte).</dd>
042 * <dt>H</dt>
043 * <dd>The radix of the integer. Values are defined as a sequence of values, where value {@code n} is multiplied by
044 * {@code H^<sup>n</sup>}. So the number 1234 may be represented as the sequence 4 3 2 1 with a radix (H) of 10.
045 * Note that other permutations are also possible; 43 2 1 will also encode 1234. The co-parameter L is defined as 256-H.
046 * This is important because only the last value in a sequence may be &lt; L; all prior values must be &gt; L.</dd>
047 * <dt>S</dt>
048 * <dd>Whether the codec represents signed values (or not). This may have 3 values; 0 (unsigned), 1 (signed, one's
049 * complement) or 2 (signed, two's complement)</dd>
050 * <dt>D</dt>
051 * <dd>Whether the codec represents a delta encoding. This may be 0 (no delta) or 1 (delta encoding). A delta encoding
052 * of 1 indicates that values are cumulative; a sequence of {@code 1 1 1 1 1} will represent the sequence
053 * {@code 1 2 3 4 5}. For this reason, the codec supports two variants of decode; one
054 * {@link #decode(InputStream, long) with} and one {@link #decode(InputStream) without} a {@code last} parameter.
055 * If the codec is a non-delta encoding, then the value is ignored if passed. If the codec is a delta encoding, it is a
056 * run-time error to call the value without the extra parameter, and the previous value should be returned. (It was
057 * designed this way to support multi-threaded access without requiring a new instance of the Codec to be cloned for
058 * each use.)
059 * <dt>
060 * </dl>
061 *
062 * Codecs are notated as (B,H,S,D) and either D or S,D may be omitted if zero. Thus {@link #BYTE1} is denoted
063 * (1,256,0,0) or (1,256). The {@link #toString()} method prints out the condensed form of the encoding. Often, the last
064 * character in the name ({@link #BYTE1}, {@link #UNSIGNED5}) gives a clue as to the B value. Those that start with U
065 * ({@link #UDELTA5}, {@link #UNSIGNED5}) are unsigned; otherwise, in most cases, they are signed. The presence of the
066 * word Delta ({@link #DELTA5}, {@link #UDELTA5}) indicates a delta encoding is used.
067 *
068 */
069public final class BHSDCodec extends Codec {
070
071    /**
072     * The maximum number of bytes in each coding word
073     */
074    private final int b;
075
076    /**
077     * Whether delta encoding is used (0=false,1=true)
078     */
079    private final int d;
080
081    /**
082     * The radix of the encoding
083     */
084    private final int h;
085
086    /**
087     * The co-parameter of h; 256-h
088     */
089    private final int l;
090
091    /**
092     * Represents signed numbers or not (0=unsigned,1/2=signed)
093     */
094    private final int s;
095
096    private long cardinality;
097
098    private final long smallest;
099
100    private final long largest;
101
102    /**
103     * radix^i powers
104     */
105    private final long[] powers;
106
107    /**
108     * Constructs an unsigned, non-delta Codec with the given B and H values.
109     *
110     * @param b the maximum number of bytes that a value can be encoded as [1..5]
111     * @param h the radix of the encoding [1..256]
112     */
113    public BHSDCodec(final int b, final int h) {
114        this(b, h, 0, 0);
115    }
116
117    /**
118     * Constructs a non-delta Codec with the given B, H and S values.
119     *
120     * @param b the maximum number of bytes that a value can be encoded as [1..5]
121     * @param h the radix of the encoding [1..256]
122     * @param s whether the encoding represents signed numbers (s=0 is unsigned; s=1 is signed with 1s complement; s=2
123     *        is signed with ?)
124     */
125    public BHSDCodec(final int b, final int h, final int s) {
126        this(b, h, s, 0);
127    }
128
129    /**
130     * Constructs a Codec with the given B, H, S and D values.
131     *
132     * @param b the maximum number of bytes that a value can be encoded as [1..5]
133     * @param h the radix of the encoding [1..256]
134     * @param s whether the encoding represents signed numbers (s=0 is unsigned; s=1 is signed with 1s complement; s=2
135     *        is signed with ?)
136     * @param d whether this is a delta encoding (d=0 is non-delta; d=1 is delta)
137     */
138    public BHSDCodec(final int b, final int h, final int s, final int d) {
139        if (b < 1 || b > 5) {
140            throw new IllegalArgumentException("1<=b<=5");
141        }
142        if (h < 1 || h > 256) {
143            throw new IllegalArgumentException("1<=h<=256");
144        }
145        if (s < 0 || s > 2) {
146            throw new IllegalArgumentException("0<=s<=2");
147        }
148        if (d < 0 || d > 1) {
149            throw new IllegalArgumentException("0<=d<=1");
150        }
151        if (b == 1 && h != 256) {
152            throw new IllegalArgumentException("b=1 -> h=256");
153        }
154        if (h == 256 && b == 5) {
155            throw new IllegalArgumentException("h=256 -> b!=5");
156        }
157        this.b = b;
158        this.h = h;
159        this.s = s;
160        this.d = d;
161        this.l = 256 - h;
162        if (h == 1) {
163            cardinality = b * 255 + 1;
164        } else {
165            cardinality = (long) ((long) (l * (1 - Math.pow(h, b)) / (1 - h)) + Math.pow(h, b));
166        }
167        smallest = calculateSmallest();
168        largest = calculateLargest();
169
170        powers = new long[b];
171        Arrays.setAll(powers, c -> (long) Math.pow(h, c));
172    }
173
174    /**
175     * Returns the cardinality of this codec; that is, the number of distinct values that it can contain.
176     *
177     * @return the cardinality of this codec
178     */
179    public long cardinality() {
180        return cardinality;
181    }
182
183    @Override
184    public int decode(final InputStream in) throws IOException, Pack200Exception {
185        if (d != 0) {
186            throw new Pack200Exception("Delta encoding used without passing in last value; this is a coding error");
187        }
188        return decode(in, 0);
189    }
190
191    @Override
192    public int decode(final InputStream in, final long last) throws IOException, Pack200Exception {
193        int n = 0;
194        long z = 0;
195        long x = 0;
196
197        do {
198            x = in.read();
199            lastBandLength++;
200            z += x * powers[n];
201            n++;
202        } while (x >= l && n < b);
203
204        if (x == -1) {
205            throw new EOFException("End of stream reached whilst decoding");
206        }
207
208        if (isSigned()) {
209            final int u = ((1 << s) - 1);
210            if ((z & u) == u) {
211                z = z >>> s ^ -1L;
212            } else {
213                z = z - (z >>> s);
214            }
215        }
216        // This algorithm does the same thing, but is probably slower. Leaving
217        // in for now for readability
218        // if(isSigned()) {
219        // long u = z;
220        // long twoPowS = (long)Math.pow(2, s);
221        // double twoPowSMinusOne = twoPowS-1;
222        // if(u % twoPowS < twoPowSMinusOne) {
223        // if(cardinality < Math.pow(2, 32)) {
224        // z = (long) (u - (Math.floor(u/ twoPowS)));
225        // } else {
226        // z = cast32((long) (u - (Math.floor(u/ twoPowS))));
227        // }
228        // } else {
229        // z = (long) (-Math.floor(u/ twoPowS) - 1);
230        // }
231        // }
232        if (isDelta()) {
233            z += last;
234        }
235        return (int) z;
236    }
237
238    @Override
239    public int[] decodeInts(final int n, final InputStream in) throws IOException, Pack200Exception {
240        final int[] band = super.decodeInts(n, in);
241        if (isDelta()) {
242            for (int i = 0; i < band.length; i++) {
243                while (band[i] > largest) {
244                    band[i] -= cardinality;
245                }
246                while (band[i] < smallest) {
247                    band[i] = ExactMath.add(band[i], cardinality);
248                }
249            }
250        }
251        return band;
252    }
253
254    @Override
255    public int[] decodeInts(final int n, final InputStream in, final int firstValue)
256        throws IOException, Pack200Exception {
257        final int[] band = super.decodeInts(n, in, firstValue);
258        if (isDelta()) {
259            for (int i = 0; i < band.length; i++) {
260                while (band[i] > largest) {
261                    band[i] -= cardinality;
262                }
263                while (band[i] < smallest) {
264                    band[i] = ExactMath.add(band[i], cardinality);
265                }
266            }
267        }
268        return band;
269    }
270
271    // private long cast32(long u) {
272    // u = (long) ((long) ((u + Math.pow(2, 31)) % Math.pow(2, 32)) -
273    // Math.pow(2, 31));
274    // return u;
275    // }
276
277    /**
278     * True if this encoding can code the given value
279     *
280     * @param value the value to check
281     * @return {@code true} if the encoding can encode this value
282     */
283    public boolean encodes(final long value) {
284        return value >= smallest && value <= largest;
285    }
286
287    @Override
288    public byte[] encode(final int value, final int last) throws Pack200Exception {
289        if (!encodes(value)) {
290            throw new Pack200Exception("The codec " + this + " does not encode the value " + value);
291        }
292
293        long z = value;
294        if (isDelta()) {
295            z -= last;
296        }
297        if (isSigned()) {
298            if (z < Integer.MIN_VALUE) {
299                z += 4294967296L;
300            } else if (z > Integer.MAX_VALUE) {
301                z -= 4294967296L;
302            }
303            if (z < 0) {
304                z = (-z << s) - 1;
305            } else if (s == 1) {
306                z = z << s;
307            } else {
308                z += (z - z % 3) / 3;
309            }
310        } else if (z < 0) {
311            // Need to use integer overflow here to represent negatives.
312            // 4294967296L is the 1 << 32.
313            z += Math.min(cardinality, 4294967296L);
314        }
315        if (z < 0) {
316            throw new Pack200Exception("unable to encode");
317        }
318
319        final List<Byte> byteList = new ArrayList<>();
320        for (int n = 0; n < b; n++) {
321            long byteN;
322            if (z < l) {
323                byteN = z;
324            } else {
325                byteN = z % h;
326                while (byteN < l) {
327                    byteN += h;
328                }
329            }
330            byteList.add(Byte.valueOf((byte) byteN));
331            if (byteN < l) {
332                break;
333            }
334            z -= byteN;
335            z /= h;
336        }
337        final byte[] bytes = new byte[byteList.size()];
338        for (int i = 0; i < bytes.length; i++) {
339            bytes[i] = byteList.get(i).byteValue();
340        }
341        return bytes;
342    }
343
344    @Override
345    public byte[] encode(final int value) throws Pack200Exception {
346        return encode(value, 0);
347    }
348
349    /**
350     * Returns true if this codec is a delta codec
351     *
352     * @return true if this codec is a delta codec
353     */
354    public boolean isDelta() {
355        return d != 0;
356    }
357
358    /**
359     * Returns true if this codec is a signed codec
360     *
361     * @return true if this codec is a signed codec
362     */
363    public boolean isSigned() {
364        return s != 0;
365    }
366
367    /**
368     * Returns the largest value that this codec can represent.
369     *
370     * @return the largest value that this codec can represent.
371     */
372    public long largest() {
373        return largest;
374    }
375
376    private long calculateLargest() {
377        long result;
378        // TODO This can probably be optimized into a better mathematical
379        // statement
380        if (d == 1) {
381            final BHSDCodec bh0 = new BHSDCodec(b, h);
382            return bh0.largest();
383        }
384        if (s == 0) {
385            result = cardinality() - 1;
386        } else if (s == 1) {
387            result = cardinality() / 2 - 1;
388        } else if (s == 2) {
389            result = (3L * cardinality()) / 4 - 1;
390        } else {
391            throw new Error("Unknown s value");
392        }
393        return Math.min((s == 0 ? ((long) Integer.MAX_VALUE) << 1 : Integer.MAX_VALUE) - 1, result);
394    }
395
396    /**
397     * Returns the smallest value that this codec can represent.
398     *
399     * @return the smallest value that this codec can represent.
400     */
401    public long smallest() {
402        return smallest;
403    }
404
405    private long calculateSmallest() {
406        long result;
407        if (d == 1 || !isSigned()) {
408            if (cardinality >= 4294967296L) { // 2^32
409                result = Integer.MIN_VALUE;
410            } else {
411                result = 0;
412            }
413        } else {
414            result = Math.max(Integer.MIN_VALUE, -cardinality() / (1 << s));
415        }
416        return result;
417    }
418
419    /**
420     * Returns the codec in the form (1,256) or (1,64,1,1). Note that trailing zero fields are not shown.
421     */
422    @Override
423    public String toString() {
424        final StringBuilder buffer = new StringBuilder(11);
425        buffer.append('(');
426        buffer.append(b);
427        buffer.append(',');
428        buffer.append(h);
429        if (s != 0 || d != 0) {
430            buffer.append(',');
431            buffer.append(s);
432        }
433        if (d != 0) {
434            buffer.append(',');
435            buffer.append(d);
436        }
437        buffer.append(')');
438        return buffer.toString();
439    }
440
441    /**
442     * @return the b
443     */
444    public int getB() {
445        return b;
446    }
447
448    /**
449     * @return the h
450     */
451    public int getH() {
452        return h;
453    }
454
455    /**
456     * @return the s
457     */
458    public int getS() {
459        return s;
460    }
461
462    /**
463     * @return the l
464     */
465    public int getL() {
466        return l;
467    }
468
469    @Override
470    public boolean equals(final Object o) {
471        if (o instanceof BHSDCodec) {
472            final BHSDCodec codec = (BHSDCodec) o;
473            return codec.b == b && codec.h == h && codec.s == s && codec.d == d;
474        }
475        return false;
476    }
477
478    @Override
479    public int hashCode() {
480        return ((b * 37 + h) * 37 + s) * 37 + d;
481    }
482}