| 1 | /* PowerPacker unpack code taken from Heikki Orsila's amigadepack. Seems to have no license, |
| 2 | ** so I'll assume it fits into BSD 3-Clause. If not, feel free to contact me at my email |
| 3 | ** address found at the bottom of 16-bits.org. |
| 4 | ** |
| 5 | ** Edited by me (8bitbubsy). |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdbool.h> |
| 12 | |
| 13 | #define READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(nbits, var) \ |
| 14 | bitCnt = (nbits); \ |
| 15 | while (bitsLeft < bitCnt) \ |
| 16 | { \ |
| 17 | if (bufSrc < src) \ |
| 18 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; \ |
| 19 | bitBuffer |= ((*--bufSrc) << bitsLeft); \ |
| 20 | bitsLeft += 8; \ |
| 21 | } \ |
| 22 | (var) = 0; \ |
| 23 | bitsLeft -= bitCnt; \ |
| 24 | while (bitCnt--) \ |
| 25 | { \ |
| 26 | (var) = ((var) << 1) | (bitBuffer & 1); \ |
| 27 | bitBuffer >>= 1; \ |
| 28 | } |
| 29 | |
| 30 | static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! decrunchcxx . . $ decrunch(22e87c8a50291263).(uint8_tcxx . . $ uint8_t# *src, uint8_tcxx . . $ uint8_t# *dst, uint8_tcxx . . $ uint8_t# *offsetLens, uint32_tcxx . . $ uint32_t# srcLen, uint32_tcxx . . $ uint32_t# dstLen, uint8_tcxx . . $ uint8_t# skipBits) |
| 31 | { |
| 32 | uint8_tcxx . . $ uint8_t# bitCnt; |
| 33 | uint32_tcxx . . $ uint32_t# x, todo, offset; |
| 34 | |
| 35 | if (src == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! || dst == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! || offsetLens == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 36 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 37 | |
| 38 | uint8_tcxx . . $ uint8_t# bitsLeft = 0; |
| 39 | uint32_tcxx . . $ uint32_t# bitBuffer = 0; |
| 40 | uint32_tcxx . . $ uint32_t# written = 0; |
| 41 | uint8_tcxx . . $ uint8_t# *bufSrc = src + srcLen; |
| 42 | uint8_tcxx . . $ uint8_t# *out = dst + dstLen; |
| 43 | uint8_tcxx . . $ uint8_t# *dstEnd = out; |
| 44 | |
| 45 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(skipBits, x); |
| 46 | while (written < dstLen) |
| 47 | { |
| 48 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(1, x); |
| 49 | if (x == 0) |
| 50 | { |
| 51 | todo = 1; |
| 52 | |
| 53 | do |
| 54 | { |
| 55 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(2, x); |
| 56 | todo += x; |
| 57 | } |
| 58 | while (x == 3); |
| 59 | |
| 60 | while (todo--) |
| 61 | { |
| 62 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(8, x); |
| 63 | if (out <= dst) |
| 64 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 65 | |
| 66 | *--out = (uint8_tcxx . . $ uint8_t#)x; |
| 67 | written++; |
| 68 | } |
| 69 | |
| 70 | if (written == dstLen) |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(2, x); |
| 75 | uint32_tcxx . . $ uint32_t# offBits = offsetLens[x]; |
| 76 | todo = x + 2; |
| 77 | |
| 78 | if (x == 3) |
| 79 | { |
| 80 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(1, x); |
| 81 | if (x == 0) offBits = 7; |
| 82 | |
| 83 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!((uint8_t)offBits, offset); |
| 84 | do |
| 85 | { |
| 86 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!(3, x); |
| 87 | todo += x; |
| 88 | } |
| 89 | while (x == 7); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | READ_BITScxx . . $ `src/modloaders/pt2_pp_unpack.c:13:9`!((uint8_t)offBits, offset); |
| 94 | } |
| 95 | |
| 96 | if (out+offset >= dstEnd) |
| 97 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 98 | |
| 99 | while (todo--) |
| 100 | { |
| 101 | x = out[offset]; |
| 102 | if (out <= dst) |
| 103 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 104 | |
| 105 | *--out = (uint8_tcxx . . $ uint8_t#)x; |
| 106 | written++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 111 | } |
| 112 | |
| 113 | uint8_tcxx . . $ uint8_t# *unpackPPcxx . . $ unpackPP(194f2cbfed1acdc7).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# *filesize) |
| 114 | { |
| 115 | uint8_tcxx . . $ uint8_t# *outBuffer, ppCrunchData[4], *ppBuffer; |
| 116 | uint32_tcxx . . $ uint32_t# ppPackLen, ppUnpackLen; |
| 117 | |
| 118 | ppPackLen = *filesize; |
| 119 | if ((ppPackLen & 3) != 0 || ppPackLen <= 12) |
| 120 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 121 | |
| 122 | ppBuffer = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(ppPackLen); |
| 123 | if (ppBuffer == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 124 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 125 | |
| 126 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, ppPackLen-4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 127 | freadcxx . . $ fread(738fc8ac1d711e58).(ppCrunchData, 1, 4, f); |
| 128 | |
| 129 | ppUnpackLen = (ppCrunchData[0] << 16) | (ppCrunchData[1] << 8) | ppCrunchData[2]; |
| 130 | |
| 131 | outBuffer = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(ppUnpackLen); |
| 132 | if (outBuffer == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 133 | { |
| 134 | freecxx . . $ free(5d22bdacc48458e8).(ppBuffer); |
| 135 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 136 | } |
| 137 | |
| 138 | rewindcxx . . $ rewind(8d4262d45fc644ab).(f); |
| 139 | freadcxx . . $ fread(738fc8ac1d711e58).(ppBuffer, 1, ppPackLen, f); |
| 140 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 141 | |
| 142 | if (!decrunchcxx . . $ decrunch(22e87c8a50291263).(ppBuffer+8, outBuffer, ppBuffer+4, ppPackLen-12, ppUnpackLen, ppCrunchData[3])) |
| 143 | { |
| 144 | freecxx . . $ free(5d22bdacc48458e8).(ppBuffer); |
| 145 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 146 | } |
| 147 | |
| 148 | freecxx . . $ free(5d22bdacc48458e8).(ppBuffer); |
| 149 | *filesize = ppUnpackLen; |
| 150 | |
| 151 | return outBuffer; |
| 152 | } |