Created
September 11, 2017 00:31
-
-
Save jblang/9fd38f21d64d1cba4c5d549733219d93 to your computer and use it in GitHub Desktop.
Decrunches programs crunched with ByteBoozer 1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#define MEMSIZ 64*1024 | |
#define GETLOC 0x20 | |
uint16_t get, put; | |
uint8_t cur, mem[MEMSIZ]; | |
uint8_t tab[] = { 4, 2, 2, 2, 5, 2, 2, 3 }; | |
uint8_t nextbit() { | |
uint8_t bit = (cur & 0x80) >> 7; | |
cur <<= 1; | |
if (cur == 0) { | |
cur = mem[get++]; | |
bit = (cur & 0x80) >> 7; | |
cur = (cur << 1) | 1; | |
} | |
return bit; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 3) { | |
printf("usage: debooze <input> <output>\n"); | |
exit(1); | |
} | |
FILE *fp = fopen(argv[1], "rb"); | |
if (fp == NULL) { | |
printf("error: unable to open %s\n", argv[1]); | |
exit(1); | |
} | |
size_t flen = fread(mem, 1, MEMSIZ, fp); | |
printf("read %d bytes from %s\n", flen, argv[1]); | |
fclose(fp); | |
get = mem[GETLOC] | mem[GETLOC + 1] << 8; | |
uint16_t src = flen - 1, dst = MEMSIZ - 1; | |
while (dst >= get) | |
mem[dst--] = mem[src--]; | |
cur = mem[get]; | |
dst = put = mem[get + 1] | mem[get + 2] << 8; | |
printf("source: $%04X length: %d bytes\n", get, MEMSIZ - 1 - get); | |
get += 3; | |
uint8_t copy = nextbit(); | |
for (;;) { | |
uint8_t copylen = 1; | |
while (copylen < 0x80 && nextbit() == 1) | |
copylen = copylen << 1 | nextbit(); | |
if (copy == 1) { | |
if (copylen == 0xff) | |
break; | |
copylen++; | |
uint8_t tabidx = (copylen >= 3) << 2 | nextbit() << 1 | nextbit(); | |
uint16_t copysrc = 0; | |
do { | |
for (int i = 0; i < tab[tabidx]; i++) | |
copysrc = copysrc << 1 | nextbit(); | |
if ((tabidx & 3) == 0) | |
break; | |
tabidx--; | |
copysrc++; | |
} while (copysrc != 0); | |
copysrc = put - copysrc; | |
for (int i = 0; i < copylen; i++) | |
mem[put++] = mem[copysrc++]; | |
} else { | |
for (int i = 0; i < copylen; i++) | |
mem[put++] = mem[get++]; | |
if (copylen < 0xff) { | |
copy = 1; | |
continue; | |
} | |
} | |
copy = nextbit(); | |
} | |
uint16_t dlen = put - dst; | |
printf("dest: $%04X length: %d bytes\n", dst, dlen); | |
fp = fopen(argv[2], "wb"); | |
fputc(dst & 0xff, fp); | |
fputc((dst & 0xff00) >> 8, fp); | |
if ((flen = fwrite(mem + dst, 1, dlen, fp)) != dlen) | |
printf("error: unable to write output"); | |
fclose(fp); | |
printf("wrote %d bytes to %s\n", flen, argv[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment