| 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_BITS(nbits, var) \ |
| 14 | bitCnt = (nbits); \ |
| 15 | while (bitsLeft < bitCnt) \ |
| 16 | { \ |
| 17 | if (bufSrc < src) \ |
| 18 | return false; \ |
| 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 bool decrunch(uint8_t *src, uint8_t *dst, uint8_t *offsetLens, uint32_t srcLen, uint32_t dstLen, uint8_t skipBits) |
| 31 | { |
| 32 | uint8_t bitCnt; |
| 33 | uint32_t x, todo, offset; |
| 34 | |
| 35 | if (src == NULL || dst == NULL || offsetLens == NULL) |
| 36 | return false; |
| 37 | |
| 38 | uint8_t bitsLeft = 0; |
| 39 | uint32_t bitBuffer = 0; |
| 40 | uint32_t written = 0; |
| 41 | uint8_t *bufSrc = src + srcLen; |
| 42 | uint8_t *out = dst + dstLen; |
| 43 | uint8_t *dstEnd = out; |
| 44 | |
| 45 | READ_BITS(skipBits, x); |
| 46 | while (written < dstLen) |
| 47 | { |
| 48 | READ_BITS(1, x); |
| 49 | if (x == 0) |
| 50 | { |
| 51 | todo = 1; |
| 52 | |
| 53 | do |
| 54 | { |
| 55 | READ_BITS(2, x); |
| 56 | todo += x; |
| 57 | } |
| 58 | while (x == 3); |
| 59 | |
| 60 | while (todo--) |
| 61 | { |
| 62 | READ_BITS(8, x); |
| 63 | if (out <= dst) |
| 64 | return false; |
| 65 | |
| 66 | *--out = (uint8_t)x; |
| 67 | written++; |
| 68 | } |
| 69 | |
| 70 | if (written == dstLen) |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | READ_BITS(2, x); |
| 75 | uint32_t offBits = offsetLens[x]; |
| 76 | todo = x + 2; |
| 77 | |
| 78 | if (x == 3) |
| 79 | { |
| 80 | READ_BITS(1, x); |
| 81 | if (x == 0) offBits = 7; |
| 82 | |
| 83 | READ_BITS((uint8_t)offBits, offset); |
| 84 | do |
| 85 | { |
| 86 | READ_BITS(3, x); |
| 87 | todo += x; |
| 88 | } |
| 89 | while (x == 7); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | READ_BITS((uint8_t)offBits, offset); |
| 94 | } |
| 95 | |
| 96 | if (out+offset >= dstEnd) |
| 97 | return false; |
| 98 | |
| 99 | while (todo--) |
| 100 | { |
| 101 | x = out[offset]; |
| 102 | if (out <= dst) |
| 103 | return false; |
| 104 | |
| 105 | *--out = (uint8_t)x; |
| 106 | written++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | uint8_t *unpackPP(FILE *f, uint32_t *filesize) |
| 114 | { |
| 115 | uint8_t *outBuffer, ppCrunchData[4], *ppBuffer; |
| 116 | uint32_t ppPackLen, ppUnpackLen; |
| 117 | |
| 118 | ppPackLen = *filesize; |
| 119 | if ((ppPackLen & 3) != 0 || ppPackLen <= 12) |
| 120 | return NULL; |
| 121 | |
| 122 | ppBuffer = (uint8_t *)malloc(ppPackLen); |
| 123 | if (ppBuffer == NULL) |
| 124 | return NULL; |
| 125 | |
| 126 | fseek(f, ppPackLen-4, SEEK_SET); |
| 127 | fread(ppCrunchData, 1, 4, f); |
| 128 | |
| 129 | ppUnpackLen = (ppCrunchData[0] << 16) | (ppCrunchData[1] << 8) | ppCrunchData[2]; |
| 130 | |
| 131 | outBuffer = (uint8_t *)malloc(ppUnpackLen); |
| 132 | if (outBuffer == NULL) |
| 133 | { |
| 134 | free(ppBuffer); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | rewind(f); |
| 139 | fread(ppBuffer, 1, ppPackLen, f); |
| 140 | fclose(f); |
| 141 | |
| 142 | if (!decrunch(ppBuffer+8, outBuffer, ppBuffer+4, ppPackLen-12, ppUnpackLen, ppCrunchData[3])) |
| 143 | { |
| 144 | free(ppBuffer); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | free(ppBuffer); |
| 149 | *filesize = ppUnpackLen; |
| 150 | |
| 151 | return outBuffer; |
| 152 | } |