24 warn(
"Unexpected EOF during decompression (truncated file?)");
30 for (i = 0; i < count; i++) {
65 p->
Range = 0xFFFFFFFF;
66 for (i = 0; i < 4; i++) {
74 return self->Code / (
self->Range /= total);
80 self->Low += start *
self->Range;
81 self->Code -= start *
self->Range;
84 if ((self->Low ^ (self->Low + self->Range)) >= (1 << 24)) {
85 if (self->Range >= (1 << 15))
87 self->Range = ((uint32_t)(-(int32_t)
self->Low)) & ((1 << 15) - 1);
89 self->Code = (
self->Code << 8) | self->Stream->Read(self->Stream);
98 UInt32 bit = value < size0 ? 0 : 1;
117 if (version == 29 || version == 36)
119 else if (version == 20 || version == 26)
122 warn(
"Unsupported compression version: %d", version);
126 if (uncomp->
version != version) {
127 warn(
"Compression version mismatch: %d != %d", version, uncomp->
version);
132 memset(uncomp, 0,
sizeof(*uncomp));
135 warn(
"OOM during decompression");
175 warn(
"Invalid data in bitstream");
178 if (length <= code->tablesize) {
191 bit = (uint8_t)
br_bits(rar, 1);
193 warn(
"Invalid data in bitstream");
210 for (i = 0; i < 4; i++)
218 uint8_t prelengths[19];
242 for (i = 0; i < 19; i++) {
245 prelengths[i] = (uint8_t)
br_bits(rar, 4);
248 memset(&precode, 0,
sizeof(precode));
251 for (i = 0; i < count; ) {
259 else if (val == 16) {
261 warn(
"Invalid data in bitstream");
266 n = (uint8_t)
br_bits(rar, 2) + 3;
267 for (j = 0; j < n && i < count; i++, j++) {
275 n = (uint8_t)
br_bits(rar, 3) + 3;
280 n = (uint8_t)
br_bits(rar, 7) + 11;
282 for (j = 0; j < n && i < count; i++, j++) {
314 uint8_t predbyte, byte;
323 byte = (predbyte - delta) & 0xFF;
325 prederror = delta << 3;
326 state->
error[0] += abs(prederror);
327 state->
error[1] += abs(prederror - state->
delta[0]); state->
error[2] += abs(prederror + state->
delta[0]);
328 state->
error[3] += abs(prederror - state->
delta[1]); state->
error[4] += abs(prederror + state->
delta[1]);
329 state->
error[5] += abs(prederror - state->
delta[2]); state->
error[6] += abs(prederror + state->
delta[2]);
330 state->
error[7] += abs(prederror - state->
delta[3]); state->
error[8] += abs(prederror + state->
delta[3]);
331 state->
error[9] += abs(prederror - *channeldelta); state->
error[10] += abs(prederror + *channeldelta);
336 if (!(++state->
count & 0x1F)) {
338 for (i = 1; i < 11; i++) {
342 memset(state->
error, 0,
sizeof(state->
error));
345 case 1:
if (state->
weight[0] >= -16) state->
weight[0]--;
break;
346 case 2:
if (state->
weight[0] < 16) state->
weight[0]++;
break;
347 case 3:
if (state->
weight[1] >= -16) state->
weight[1]--;
break;
348 case 4:
if (state->
weight[1] < 16) state->
weight[1]++;
break;
349 case 5:
if (state->
weight[2] >= -16) state->
weight[2]--;
break;
350 case 6:
if (state->
weight[2] < 16) state->
weight[2]++;
break;
351 case 7:
if (state->
weight[3] >= -16) state->
weight[3]--;
break;
352 case 8:
if (state->
weight[3] < 16) state->
weight[3]++;
break;
353 case 9:
if (state->
weight[4] >= -16) state->
weight[4]--;
break;
354 case 10:
if (state->
weight[4] < 16) state->
weight[4]++;
break;
363 static const uint8_t lengthbases[] =
364 { 0, 1, 2, 3, 4, 5, 6,
365 7, 8, 10, 12, 14, 16, 20,
366 24, 28, 32, 40, 48, 56, 64,
367 80, 96, 112, 128, 160, 192, 224 };
368 static const uint8_t lengthbits[] =
369 { 0, 0, 0, 0, 0, 0, 0,
372 4, 4, 4, 5, 5, 5, 5 };
373 static const int32_t offsetbases[] =
375 8, 12, 16, 24, 32, 48,
376 64, 96, 128, 192, 256, 384,
377 512, 768, 1024, 1536, 2048, 3072,
378 4096, 6144, 8192, 12288, 16384, 24576,
379 32768, 49152, 65536, 98304, 131072, 196608,
380 262144, 327680, 393216, 458752, 524288, 589824,
381 655360, 720896, 786432, 851968, 917504, 983040 };
382 static const uint8_t offsetbits[] =
383 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
384 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
385 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
386 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 };
387 static const uint8_t shortbases[] =
388 { 0, 4, 8, 16, 32, 64, 128, 192 };
389 static const uint8_t shortbits[] =
390 { 2, 2, 3, 4, 5, 6, 6, 6 };
394 int symbol, offs, len;
431 else if (symbol <= 260) {
432 int idx = symbol - 256;
435 if (lensymbol < 0 || lensymbol > (
int)(
sizeof(lengthbases) /
sizeof(lengthbases[0])) || lensymbol > (
int)(
sizeof(lengthbits) /
sizeof(lengthbits[0]))) {
436 warn(
"Invalid data in bitstream");
439 len = lengthbases[lensymbol] + 2;
440 if (lengthbits[lensymbol] > 0) {
441 if (!
br_check(rar, lengthbits[lensymbol]))
443 len += (uint8_t)
br_bits(rar, lengthbits[lensymbol]);
452 else if (symbol <= 268) {
453 int idx = symbol - 261;
454 offs = shortbases[idx] + 1;
455 if (shortbits[idx] > 0) {
458 offs += (uint8_t)
br_bits(rar, shortbits[idx]);
462 else if (symbol == 269) {
467 int idx = symbol - 270;
469 if (idx > (
int)(
sizeof(lengthbases) /
sizeof(lengthbases[0])) || idx > (
int)(
sizeof(lengthbits) /
sizeof(lengthbits[0]))) {
470 warn(
"Invalid data in bitstream");
473 len = lengthbases[idx] + 3;
474 if (lengthbits[idx] > 0) {
475 if (!
br_check(rar, lengthbits[idx]))
477 len += (uint8_t)
br_bits(rar, lengthbits[idx]);
480 if (offssymbol < 0 || offssymbol > (
int)(
sizeof(offsetbases) /
sizeof(offsetbases[0])) || offssymbol > (
int)(
sizeof(offsetbits) /
sizeof(offsetbits[0]))) {
481 warn(
"Invalid data in bitstream");
484 offs = offsetbases[offssymbol] + 1;
485 if (offsetbits[offssymbol] > 0) {
486 if (!
br_check(rar, offsetbits[offssymbol]))
488 offs += (int)
br_bits(rar, offsetbits[offssymbol]);
536 uint32_t max_alloc = 0;
540 ppmd_flags = (uint8_t)
br_bits(rar, 7);
541 if ((ppmd_flags & 0x20)) {
544 max_alloc = ((uint8_t)
br_bits(rar, 8) + 1) << 20;
546 if ((ppmd_flags & 0x40)) {
551 if ((ppmd_flags & 0x20)) {
552 uint32_t maxorder = (ppmd_flags & 0x1F) + 1;
556 maxorder = 16 + (maxorder - 16) * 3;
561 warn(
"OOM during decompression");
571 warn(
"Invalid data in bitstream");
579 uint8_t bitlengths[20];
588 memset(&bitlengths, 0,
sizeof(bitlengths));
589 for (i = 0; i <
sizeof(bitlengths); i++) {
592 bitlengths[i] = (uint8_t)
br_bits(rar, 4);
593 if (bitlengths[i] == 0x0F) {
596 zerocount = (uint8_t)
br_bits(rar, 4);
598 for (j = 0; j < zerocount + 2 && i <
sizeof(bitlengths); j++) {
606 memset(&precode, 0,
sizeof(precode));
619 warn(
"Invalid data in bitstream");
625 n = (uint8_t)
br_bits(rar, 3) + 3;
630 n = (uint8_t)
br_bits(rar, 7) + 11;
640 n = (uint8_t)
br_bits(rar, 3) + 3;
645 n = (uint8_t)
br_bits(rar, 7) + 11;
674 uint8_t flags, val, *code;
677 if (!decode_byte(rar, &flags))
679 length = (flags & 0x07) + 1;
681 if (!decode_byte(rar, &val))
685 else if (length == 8) {
686 if (!decode_byte(rar, &val))
689 if (!decode_byte(rar, &val))
694 code = malloc(length);
696 warn(
"OOM during decompression");
699 for (i = 0; i <
length; i++) {
700 if (!decode_byte(rar, &code[i])) {
721 warn(
"Invalid data in bitstream");
724 *symbol = (
Byte)value;
732 *byte = (uint8_t)
br_bits(rar, 8);
745 Byte sym, code, length;
771 lzss_offset = code << 16;
774 lzss_offset |= code << 8;
797 static const uint8_t lengthbases[] =
798 { 0, 1, 2, 3, 4, 5, 6,
799 7, 8, 10, 12, 14, 16, 20,
800 24, 28, 32, 40, 48, 56, 64,
801 80, 96, 112, 128, 160, 192, 224 };
802 static const uint8_t lengthbits[] =
803 { 0, 0, 0, 0, 0, 0, 0,
806 4, 4, 4, 5, 5, 5, 5 };
807 static const int32_t offsetbases[] =
809 8, 12, 16, 24, 32, 48,
810 64, 96, 128, 192, 256, 384,
811 512, 768, 1024, 1536, 2048, 3072,
812 4096, 6144, 8192, 12288, 16384, 24576,
813 32768, 49152, 65536, 98304, 131072, 196608,
814 262144, 327680, 393216, 458752, 524288, 589824,
815 655360, 720896, 786432, 851968, 917504, 983040,
816 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
817 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
818 static const uint8_t offsetbits[] =
819 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
820 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
821 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
822 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
823 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
824 static const uint8_t shortbases[] =
825 { 0, 4, 8, 16, 32, 64, 128, 192 };
826 static const uint8_t shortbits[] =
827 { 2, 2, 3, 4, 5, 6, 6, 6 };
831 int symbol, offs, len, i;
879 else if (symbol <= 262) {
880 int idx = symbol - 259;
883 if (lensymbol < 0 || lensymbol > (
int)(
sizeof(lengthbases) /
sizeof(lengthbases[0])) || lensymbol > (
int)(
sizeof(lengthbits) /
sizeof(lengthbits[0]))) {
884 warn(
"Invalid data in bitstream");
887 len = lengthbases[lensymbol] + 2;
888 if (lengthbits[lensymbol] > 0) {
889 if (!
br_check(rar, lengthbits[lensymbol]))
891 len += (uint8_t)
br_bits(rar, lengthbits[lensymbol]);
893 for (i = idx; i > 0; i--)
897 else if (symbol <= 270) {
898 int idx = symbol - 263;
899 offs = shortbases[idx] + 1;
900 if (shortbits[idx] > 0) {
903 offs += (uint8_t)
br_bits(rar, shortbits[idx]);
906 for (i = 3; i > 0; i--)
911 int idx = symbol - 271;
913 if (idx > (
int)(
sizeof(lengthbases) /
sizeof(lengthbases[0])) || idx > (
int)(
sizeof(lengthbits) /
sizeof(lengthbits[0]))) {
914 warn(
"Invalid data in bitstream");
917 len = lengthbases[idx] + 3;
918 if (lengthbits[idx] > 0) {
919 if (!
br_check(rar, lengthbits[idx]))
921 len += (uint8_t)
br_bits(rar, lengthbits[idx]);
924 if (offssymbol < 0 || offssymbol > (
int)(
sizeof(offsetbases) /
sizeof(offsetbases[0])) || offssymbol > (
int)(
sizeof(offsetbits) /
sizeof(offsetbits[0]))) {
925 warn(
"Invalid data in bitstream");
928 offs = offsetbases[offssymbol] + 1;
929 if (offsetbits[offssymbol] > 0) {
930 if (offssymbol > 9) {
931 if (offsetbits[offssymbol] > 4) {
932 if (!
br_check(rar, offsetbits[offssymbol] - 4))
934 offs += (int)
br_bits(rar, offsetbits[offssymbol] - 4) << 4;
942 if (lowoffsetsymbol < 0)
944 if (lowoffsetsymbol == 16) {
949 offs += lowoffsetsymbol;
955 if (!
br_check(rar, offsetbits[offssymbol]))
957 offs += (int)
br_bits(rar, offsetbits[offssymbol]);
966 for (i = 3; i > 0; i--)
996 buffer_size -= count;
997 buffer = (uint8_t *)buffer + count;
1006 buffer_size -= count;
1007 buffer = (uint8_t *)buffer + count;
1009 if (buffer_size == 0)