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).
/*
This code by Lumis
(c) Lumis 2026
*/
#define LZW_FIRST_CODE 256
#define LZW_MAX_BITS 14
#define LZW_DICT_CAP 0x4680u /* from original buffers: 0x8D00 bytes / 2 */
#define LZW_STACK_CAP 0x0FA0u /* original hard stop */
/*
* This is not textbook LZW.
*
* The reset code is (maxvalue - 1), end code is maxvalue.
* The key quirk, matching the original game code:
* reset clears nextcode back to 256, but DOES NOT reset bit width to 9.
*
* If you "fix" that into normal LZW, several real ALADDIN.EPF members fail
* with bogus recursive chains and depth overflow.
*/
static int epfs_unlzw(const uint8_t *src, uint32_t src_size, uint8_t *dst, uint32_t out_size) {
uint16_t prefix[LZW_DICT_CAP];
uint8_t suffix[LZW_DICT_CAP];
uint8_t stack[LZW_STACK_CAP];
const uint8_t *sp;
const uint8_t *src_end;
uint8_t *dp;
uint8_t *dst_end;
uint32_t bitbuf;
unsigned bitcount;
unsigned curbits;
uint16_t maxvalue;
uint16_t dicreset;
uint16_t incbits;
uint16_t nextcode;
uint16_t prev_code;
uint16_t code;
uint16_t cur;
uint8_t first_char;
sp = src;
src_end = src + src_size;
dp = dst;
dst_end = dst + out_size;
bitbuf = 0;
bitcount = 0;
curbits = 9;
maxvalue = (uint16_t)((1u << curbits) - 1u);
dicreset = (uint16_t)(maxvalue - 1u);
incbits = (uint16_t)(maxvalue - 2u);
nextcode = LZW_FIRST_CODE;
#define GET_CODE(out_code) \
do { \
while (bitcount < curbits) { \
if (sp >= src_end) { \
return 2; \
} \
bitbuf = ((bitbuf << 8) | (uint32_t)(*sp++)) & 0xFFFFFFFFUL; \
bitcount += 8; \
} \
bitcount -= curbits; \
(out_code) = (uint16_t)((bitbuf >> bitcount) & maxvalue); \
} while (0)
if (out_size == 0) {
return 0;
}
GET_CODE(code);
if (code > 0xFFu) {
return 3;
}
*dp++ = (uint8_t)code;
prev_code = code;
first_char = (uint8_t)code;
for (;;) {
unsigned sptr;
if (dp >= dst_end) {
return 0;
}
GET_CODE(code);
if (code == maxvalue) {
return 0;
}
if (code == dicreset) {
nextcode = LZW_FIRST_CODE;
GET_CODE(code);
if (code == maxvalue) {
return 0;
}
if (code > 0xFFu) {
return 3;
}
*dp++ = (uint8_t)code;
prev_code = code;
first_char = (uint8_t)code;
continue;
}
sptr = 0;
cur = code;
if (cur >= nextcode) {
if (sptr >= LZW_STACK_CAP) {
return 1;
}
stack[sptr++] = first_char;
cur = prev_code;
}
while (cur > 0xFFu) {
if (cur >= LZW_DICT_CAP || sptr >= LZW_STACK_CAP) {
return 1;
}
stack[sptr++] = suffix[cur];
cur = prefix[cur];
}
stack[sptr++] = (uint8_t)cur;
first_char = (uint8_t)cur;
while (sptr != 0) {
if (dp >= dst_end) {
return 4;
}
*dp++ = stack[--sptr];
}
if (nextcode < LZW_DICT_CAP) {
prefix[nextcode] = prev_code;
suffix[nextcode] = first_char;
}
++nextcode;
if (nextcode > incbits && curbits < LZW_MAX_BITS) {
++curbits;
maxvalue = (uint16_t)((1u << curbits) - 1u);
dicreset = (uint16_t)(maxvalue - 1u);
incbits = (uint16_t)(maxvalue - 2u);
}
prev_code = code;
}
#undef GET_CODE
}