This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
/*
LZ2K decompressor
(c) Trevor Natiuk (pianistrevor)
https://github.com/pianistrevor/unlz2k
unknown license
*/
#include <stdint.h>
#include <string.h>
/* Globals that are used by other functions */
static char *compressedFile;
static size_t tmpSrcOffs;
static size_t tmpSrcSize;
static uint32_t bitstream;
static uint8_t lastByteRead;
static uint8_t previousBitAlign;
static uint16_t chunksWithCurrentSetupLeft;
static uint32_t readOffset;
static int32_t literalsToCopy;
static uint8_t tmpChunk[8192];
static uint8_t smallByteDict[20];
static uint8_t largeByteDict[510];
static uint16_t smallWordDict[256];
static uint16_t parallelDict0[1024];
static uint16_t parallelDict1[1024];
static uint16_t largeWordDict[4096];
static void loadIntoBitstream(uint8_t bits) {
bitstream <<= bits;
if (bits > previousBitAlign) {
do {
bits -= previousBitAlign;
bitstream |= lastByteRead << bits;
if (tmpSrcOffs == tmpSrcSize) {
lastByteRead = 0;
} else {
lastByteRead = compressedFile[tmpSrcOffs++];
}
previousBitAlign = 8;
} while (bits > previousBitAlign);
}
previousBitAlign -= bits;
bitstream |= lastByteRead >> previousBitAlign;
}
static void fillWordsUsingBytes(uint16_t bytesLen, uint8_t *bytes, uint8_t pivot, uint16_t *words) {
uint8_t shift, idx, tmpVal, tmpValCopy;
uint16_t low, high, comp1, mask, bytesLenCopy;
uint16_t destDict[18];
uint16_t srcDict[17];
int i, j;
memset(srcDict, 0, sizeof(srcDict));
destDict[1] = 0;
for (i = 0; i < bytesLen; ++i) {
uint8_t tmp = bytes[i];
srcDict[tmp]++;
}
shift = 14;
idx = 1;
while (idx <= 16) {
low = srcDict[idx] << (shift + 1);
high = srcDict[idx + 1] << shift;
low += destDict[idx];
idx += 4;
high += low;
destDict[idx - 3] = low;
destDict[idx - 2] = high;
low = srcDict[idx - 2] << (shift - 1);
low += high;
high = srcDict[idx - 1] << (shift - 2);
high += low;
destDict[idx - 1] = low;
destDict[idx] = high;
shift -= 4;
}
/*if (destDict[17]) {
printf("Bad table\n");
throw 5;
}*/
shift = pivot - 1;
tmpVal = 16 - pivot;
tmpValCopy = tmpVal;
for (i = 1; i <= pivot; ++i) {
destDict[i] >>= tmpValCopy;
srcDict[i] = 1 << shift--;
}
tmpValCopy--;
for (i = pivot + 1; i <= 16; ++i) {
srcDict[i] = 1 << tmpValCopy--;
}
comp1 = destDict[pivot + 1];
comp1 >>= 16 - pivot;
if (comp1) {
uint16_t comp2 = 1 << pivot;
if (comp1 != comp2) {
for (i = comp1; i < comp2; ++i) {
words[i] = 0;
}
}
}
if (bytesLen <= 0)
return;
shift = 15 - pivot;
mask = 1 << shift;
bytesLenCopy = bytesLen;
for (i = 0; i < bytesLen; ++i) {
uint8_t tmpByte = bytes[i];
if (tmpByte) {
uint16_t destVal = destDict[tmpByte];
uint16_t srcVal = srcDict[tmpByte] + destVal;
if (tmpByte > pivot) {
uint16_t *dictPtr = words;
uint16_t tmpOffs = destVal >> tmpVal;
uint8_t newLen = tmpByte - pivot;
while (newLen) {
if (!dictPtr[tmpOffs]) {
parallelDict0[bytesLenCopy] = 0;
parallelDict1[bytesLenCopy] = 0;
dictPtr[tmpOffs] = bytesLenCopy++;
}
tmpOffs = dictPtr[tmpOffs];
if (!(destVal & mask)) {
dictPtr = parallelDict0;
} else {
dictPtr = parallelDict1;
}
destVal += destVal;
newLen--;
}
dictPtr[tmpOffs] = i;
} else if (destVal < srcVal) {
for (j = destVal; j < srcVal; ++j) {
words[j] = i;
}
}
destDict[tmpByte] = srcVal;
}
}
}
static void fillSmallDicts(uint8_t length, uint8_t bits, uint8_t specialIdx) {
uint32_t tmpVal, tmpVal2, mask;
uint8_t specialLen;
int i;
tmpVal = bitstream >> (32 - bits);
loadIntoBitstream(bits);
if (!tmpVal) {
tmpVal = bitstream >> (32 - bits);
loadIntoBitstream(bits);
if (length > 0) {
memset(smallByteDict, 0, length);
}
for (i = 0; i < 256; ++i) {
smallWordDict[i] = tmpVal;
}
return;
}
tmpVal2 = 0;
while (tmpVal2 < tmpVal) {
uint8_t tmpByte = bitstream >> 29;
uint8_t bits = 3;
if (tmpByte == 7) {
mask = 0x10000000;
if (!(bitstream & mask)) {
bits = 4;
} else {
uint8_t counter = 0;
do {
mask >>= 1;
counter++;
} while (bitstream & mask);
bits = counter + 4;
tmpByte += counter;
}
}
loadIntoBitstream(bits);
smallByteDict[tmpVal2++] = tmpByte;
if (tmpVal2 == specialIdx) {
specialLen = bitstream >> 30;
loadIntoBitstream(2);
if (specialLen) {
memset(smallByteDict + tmpVal2, 0, specialLen);
tmpVal2 += specialLen;
}
}
}
if (tmpVal2 < length) {
memset(smallByteDict + tmpVal2, 0, length - tmpVal2);
}
fillWordsUsingBytes(length, smallByteDict, 8, smallWordDict);
}
static void fillLargeDicts(void) {
uint16_t bytes, tmpLen, tmpVal2;
int16_t tmpVal;
uint8_t bits;
int i;
tmpVal = bitstream >> 23;
loadIntoBitstream(9);
if (!tmpVal) {
tmpVal = bitstream >> 23;
loadIntoBitstream(9);
memset(largeByteDict, 0, 510);
for (i = 0; i < 4096; ++i) {
largeWordDict[i] = tmpVal;
}
return;
}
bytes = 0;
if (tmpVal < 0) {
/* Does this ever happen? */
memset(largeByteDict, 0, 510);
fillWordsUsingBytes(510, largeByteDict, 12, largeWordDict);
return;
}
while (bytes < tmpVal) {
tmpLen = bitstream >> 24;
tmpVal2 = smallWordDict[tmpLen];
if (tmpVal2 >= 19) {
uint32_t mask = 0x800000;
do {
if (!(bitstream & mask)) {
tmpVal2 = parallelDict0[tmpVal2];
} else {
tmpVal2 = parallelDict1[tmpVal2];
}
mask >>= 1;
} while (tmpVal2 >= 19);
}
bits = smallByteDict[tmpVal2];
loadIntoBitstream(bits);
if (tmpVal2 > 2) {
tmpVal2 -= 2;
largeByteDict[bytes++] = (uint8_t) tmpVal2;
} else {
if (!tmpVal2) {
tmpLen = 1;
} else if (tmpVal2 == 1) {
tmpVal2 = bitstream >> 28;
loadIntoBitstream(4);
tmpLen = tmpVal2 + 3;
} else {
tmpVal2 = bitstream >> 23;
loadIntoBitstream(9);
tmpLen = tmpVal2 + 20;
}
if (tmpLen) {
memset(largeByteDict + bytes, 0, tmpLen);
bytes += tmpLen;
}
}
}
if (bytes < 510) {
memset(largeByteDict + bytes, 0, 510 - bytes);
}
fillWordsUsingBytes(510, largeByteDict, 12, largeWordDict);
}
static uint32_t decodeBitstream(void) {
uint16_t tmpVal;
uint32_t mask;
uint8_t bits;
if (!chunksWithCurrentSetupLeft) {
chunksWithCurrentSetupLeft = bitstream >> 16;
loadIntoBitstream(16);
fillSmallDicts(19, 5, 3);
fillLargeDicts();
fillSmallDicts(14, 4, -1);
}
chunksWithCurrentSetupLeft--;
tmpVal = largeWordDict[bitstream >> 20];
if (tmpVal >= 510) {
mask = 0x80000;
do {
if (!(bitstream & mask)) {
tmpVal = parallelDict0[tmpVal];
} else {
tmpVal = parallelDict1[tmpVal];
}
mask >>= 1;
} while (tmpVal >= 510);
}
bits = largeByteDict[tmpVal];
loadIntoBitstream(bits);
return(tmpVal);
}
static uint32_t decodeBitstreamForLiterals(void) {
uint8_t tmpOffs, bits;
uint16_t tmpVal;
uint32_t tmpBitstream;
tmpOffs = bitstream >> 24;
tmpVal = smallWordDict[tmpOffs];
if (tmpVal >= 14) {
uint32_t mask = 0x800000;
do {
if (!(bitstream & mask)) {
tmpVal = parallelDict0[tmpVal];
} else {
tmpVal = parallelDict1[tmpVal];
}
mask >>= 1;
} while (tmpVal >= 14);
}
bits = smallByteDict[tmpVal];
loadIntoBitstream(bits);
if (!tmpVal) {
return(0);
}
if (tmpVal == 1) {
return(2);
}
tmpVal--;
tmpBitstream = bitstream >> (32 - tmpVal);
loadIntoBitstream((uint8_t) tmpVal);
return(tmpBitstream + (1 << tmpVal));
}
static void readAndDecrypt(size_t chunkSize, uint8_t *out) {
uint32_t outputOffs, tmpVal, lbOffs;
outputOffs = 0;
--literalsToCopy;
while (literalsToCopy >= 0) {
out[outputOffs++] = out[readOffset++];
readOffset &= 0x1FFF;
if (outputOffs == chunkSize) {
return;
}
literalsToCopy--;
}
while (outputOffs < chunkSize) {
tmpVal = decodeBitstream();
if (tmpVal <= 255) {
out[outputOffs++] = tmpVal;
if (outputOffs == chunkSize) { return; }
} else {
lbOffs = decodeBitstreamForLiterals();
readOffset = (outputOffs - lbOffs - 1) & 0x1FFF;
literalsToCopy = tmpVal - 254;
while (literalsToCopy >= 0) {
out[outputOffs++] = out[readOffset++];
readOffset &= 0x1FFF;
if (outputOffs == chunkSize) { return; }
literalsToCopy--;
}
}
}
/*if (outputOffs > chunkSize) {
printf("Error: read farther than given length\n");
throw 4;
}*/
}
static void unlz2k(uint8_t *p, uint32_t ps, uint8_t *u, uint32_t us) {
uint32_t l;
if (p && ps && u && us) {
compressedFile = (char *) p;
tmpSrcOffs = 0;
tmpSrcSize = ps;
bitstream = 0;
lastByteRead = 0;
previousBitAlign = 0;
chunksWithCurrentSetupLeft = 0;
readOffset = 0;
literalsToCopy = 0;
loadIntoBitstream(32);
while (us > 0) {
l = (us < 8192) ? us : 8192;
readAndDecrypt(l, tmpChunk);
memcpy(u, tmpChunk, l);
u += l;
us -= l;
}
}
}