1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.harmony.pack200;
20
21 import java.io.EOFException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.commons.compress.utils.ExactMath;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public final class BHSDCodec extends Codec {
66
67
68
69
70
71 private final int b;
72
73
74
75
76 private final int d;
77
78
79
80
81 private final int h;
82
83
84
85
86 private final int l;
87
88
89
90
91 private final int s;
92
93 private long cardinality;
94
95 private final long smallest;
96
97 private final long largest;
98
99
100
101
102 private final long[] powers;
103
104
105
106
107
108
109
110 public BHSDCodec(final int b, final int h) {
111 this(b, h, 0, 0);
112 }
113
114
115
116
117
118
119
120
121 public BHSDCodec(final int b, final int h, final int s) {
122 this(b, h, s, 0);
123 }
124
125
126
127
128
129
130
131
132
133 public BHSDCodec(final int b, final int h, final int s, final int d) {
134 if (b < 1 || b > 5) {
135 throw new IllegalArgumentException("1<=b<=5");
136 }
137 if (h < 1 || h > 256) {
138 throw new IllegalArgumentException("1<=h<=256");
139 }
140 if (s < 0 || s > 2) {
141 throw new IllegalArgumentException("0<=s<=2");
142 }
143 if (d < 0 || d > 1) {
144 throw new IllegalArgumentException("0<=d<=1");
145 }
146 if (b == 1 && h != 256) {
147 throw new IllegalArgumentException("b=1 -> h=256");
148 }
149 if (h == 256 && b == 5) {
150 throw new IllegalArgumentException("h=256 -> b!=5");
151 }
152 this.b = b;
153 this.h = h;
154 this.s = s;
155 this.d = d;
156 this.l = 256 - h;
157 if (h == 1) {
158 cardinality = b * 255 + 1;
159 } else {
160 cardinality = (long) ((long) (l * (1 - Math.pow(h, b)) / (1 - h)) + Math.pow(h, b));
161 }
162 smallest = calculateSmallest();
163 largest = calculateLargest();
164
165 powers = new long[b];
166 Arrays.setAll(powers, c -> (long) Math.pow(h, c));
167 }
168
169 private long calculateLargest() {
170 final long result;
171
172 if (d == 1) {
173 return new BHSDCodec(b, h).largest();
174 }
175 switch (s) {
176 case 0:
177 result = cardinality() - 1;
178 break;
179 case 1:
180 result = cardinality() / 2 - 1;
181 break;
182 case 2:
183 result = 3L * cardinality() / 4 - 1;
184 break;
185 default:
186 throw new Error("Unknown s value");
187 }
188 return Math.min((s == 0 ? (long) Integer.MAX_VALUE << 1 : Integer.MAX_VALUE) - 1, result);
189 }
190
191 private long calculateSmallest() {
192 final long result;
193 if (d == 1 || !isSigned()) {
194 if (cardinality >= 4294967296L) {
195 result = Integer.MIN_VALUE;
196 } else {
197 result = 0;
198 }
199 } else {
200 result = Math.max(Integer.MIN_VALUE, -cardinality() / (1 << s));
201 }
202 return result;
203 }
204
205
206
207
208
209
210 public long cardinality() {
211 return cardinality;
212 }
213
214 @Override
215 public int decode(final InputStream in) throws IOException, Pack200Exception {
216 if (d != 0) {
217 throw new Pack200Exception("Delta encoding used without passing in last value; this is a coding error");
218 }
219 return decode(in, 0);
220 }
221
222 @Override
223 public int decode(final InputStream in, final long last) throws IOException, Pack200Exception {
224 int n = 0;
225 long z = 0;
226 long x = 0;
227
228 do {
229 x = in.read();
230 lastBandLength++;
231 z += x * powers[n];
232 n++;
233 } while (x >= l && n < b);
234
235 if (x == -1) {
236 throw new EOFException("End of stream reached whilst decoding");
237 }
238
239 if (isSigned()) {
240 final int u = (1 << s) - 1;
241 if ((z & u) == u) {
242 z = z >>> s ^ -1L;
243 } else {
244 z -= z >>> s;
245 }
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 if (isDelta()) {
264 z += last;
265 }
266 return (int) z;
267 }
268
269
270
271
272
273
274
275 @Override
276 public int[] decodeInts(final int n, final InputStream in) throws IOException, Pack200Exception {
277 final int[] band = super.decodeInts(n, in);
278 if (isDelta()) {
279 for (int i = 0; i < band.length; i++) {
280 while (band[i] > largest) {
281 band[i] -= cardinality;
282 }
283 while (band[i] < smallest) {
284 band[i] = ExactMath.add(band[i], cardinality);
285 }
286 }
287 }
288 return band;
289 }
290
291 @Override
292 public int[] decodeInts(final int n, final InputStream in, final int firstValue) throws IOException, Pack200Exception {
293 final int[] band = super.decodeInts(n, in, firstValue);
294 if (isDelta()) {
295 for (int i = 0; i < band.length; i++) {
296 while (band[i] > largest) {
297 band[i] -= cardinality;
298 }
299 while (band[i] < smallest) {
300 band[i] = ExactMath.add(band[i], cardinality);
301 }
302 }
303 }
304 return band;
305 }
306
307 @Override
308 public byte[] encode(final int value) throws Pack200Exception {
309 return encode(value, 0);
310 }
311
312 @Override
313 public byte[] encode(final int value, final int last) throws Pack200Exception {
314 if (!encodes(value)) {
315 throw new Pack200Exception("The codec " + this + " does not encode the value " + value);
316 }
317
318 long z = value;
319 if (isDelta()) {
320 z -= last;
321 }
322 if (isSigned()) {
323 if (z < Integer.MIN_VALUE) {
324 z += 4294967296L;
325 } else if (z > Integer.MAX_VALUE) {
326 z -= 4294967296L;
327 }
328 if (z < 0) {
329 z = (-z << s) - 1;
330 } else if (s == 1) {
331 z = z << s;
332 } else {
333 z += (z - z % 3) / 3;
334 }
335 } else if (z < 0) {
336
337
338 z += Math.min(cardinality, 4294967296L);
339 }
340 if (z < 0) {
341 throw new Pack200Exception("unable to encode");
342 }
343
344 final List<Byte> byteList = new ArrayList<>();
345 for (int n = 0; n < b; n++) {
346 long byteN;
347 if (z < l) {
348 byteN = z;
349 } else {
350 byteN = z % h;
351 while (byteN < l) {
352 byteN += h;
353 }
354 }
355 byteList.add(Byte.valueOf((byte) byteN));
356 if (byteN < l) {
357 break;
358 }
359 z -= byteN;
360 z /= h;
361 }
362 final byte[] bytes = new byte[byteList.size()];
363 for (int i = 0; i < bytes.length; i++) {
364 bytes[i] = byteList.get(i).byteValue();
365 }
366 return bytes;
367 }
368
369
370
371
372
373
374
375 public boolean encodes(final long value) {
376 return value >= smallest && value <= largest;
377 }
378
379 @Override
380 public boolean equals(final Object o) {
381 if (o instanceof BHSDCodec) {
382 final BHSDCodec codec = (BHSDCodec) o;
383 return codec.b == b && codec.h == h && codec.s == s && codec.d == d;
384 }
385 return false;
386 }
387
388
389
390
391
392
393 public int getB() {
394 return b;
395 }
396
397
398
399
400
401
402 public int getH() {
403 return h;
404 }
405
406
407
408
409
410
411 public int getL() {
412 return l;
413 }
414
415
416
417
418
419
420 public int getS() {
421 return s;
422 }
423
424 @Override
425 public int hashCode() {
426 return ((b * 37 + h) * 37 + s) * 37 + d;
427 }
428
429
430
431
432
433
434 public boolean isDelta() {
435 return d != 0;
436 }
437
438
439
440
441
442
443 public boolean isSigned() {
444 return s != 0;
445 }
446
447
448
449
450
451
452 public long largest() {
453 return largest;
454 }
455
456
457
458
459
460
461 public long smallest() {
462 return smallest;
463 }
464
465
466
467
468 @Override
469 public String toString() {
470 final StringBuilder buffer = new StringBuilder(11);
471 buffer.append('(');
472 buffer.append(b);
473 buffer.append(',');
474 buffer.append(h);
475 if (s != 0 || d != 0) {
476 buffer.append(',');
477 buffer.append(s);
478 }
479 if (d != 0) {
480 buffer.append(',');
481 buffer.append(d);
482 }
483 buffer.append(')');
484 return buffer.toString();
485 }
486 }