/* decompile.c - Don Yang (uguu.org) Decode huffman bytecode (for sanity check). 10/19/01 */ #include #include #include"hhenc.h" #define LISTING_NAME "hhdec.lst" #define OFFSET ';' static int bit; static char *p; static FILE *list; static int Constant[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, -2, -3, 256, 0xdeadbeef }; /****************************************************************** GetBit */ static int GetBit() { int rbit; rbit = (int)(*p - OFFSET) & (1 << (bit++)); if( bit > 5 ) { bit = 0; p++; } fputc(rbit ? '1' : '0', list); return rbit ? 1 : 0; } /* GetBit() */ /******************************************************************** main */ int main(void) { int i, ip, data; if( (list = fopen(LISTING_NAME, "wt+")) == NULL ) { puts("Can not create " LISTING_NAME); return 1; } ip = bit = 0; p = bytecode; for(;;) { fprintf(list, "%4d: ", ip); ip++; if( GetBit() ) { /* 1 */ if( GetBit() ) { /* 11 */ if( GetBit() ) { /* 111 */ if( GetBit() ) { /* 1111 */ fputs("\t\trget\n", list); } else { /* 1110 */ fputs("\t\tpop\n", list); } } else { /* 110 */ if( GetBit() ) { /* 1101 */ if( GetBit() ) { /* 11011 */ if( GetBit() ) { /* 110111 */ fputs("\t\tinc\n", list); } else { /* 110110 */ fputs("\t\tsub\n", list); } } else { /* 11010 */ if( GetBit() ) { /* 110101 */ fputs("\t\tadd\n", list); } else { /* 110100 */ fputs("\t\tcmp\n", list); } } } else { /* 1100 */ if( GetBit() ) { /* 11001 */ if( GetBit() ) { /* 110011 */ fputs("\t\tsget\n", list); } else { /* 110010 */ fputs("\t\tmput\n", list); } } else { /* 11000 */ fputs("\t\tmget\n", list); } } } } else { /* 10 */ if( GetBit() ) { /* 101 */ if( GetBit() ) { /* 1011 */ if( GetBit() ) { /* 10111 */ fputs("\t\tend\n", list); } else { /* 10110 */ fputs("\t\tbegin\n", list); } } else { /* 1010 */ fputs("\t\trput\n", list); } } else { /* 100 */ if( GetBit() ) { /* 1001 */ if( GetBit() ) { /* 10011 */ if( GetBit() ) { /* 100111 */ fputs("\t\tifcond\n", list); } else { /* 100110 */ fputs("\t\twhilecond\n", list); } } else { /* 10010 */ if( GetBit() ) { /* 100101 */ if( GetBit() ) { /* 1001011 */ if( GetBit() ) { /* 10010111 */ if( GetBit() ) { /* 100101111 */ fputs("\t\tret\n", list); } else { /* 100101110 */ fputs("\t\tend_bytecode\n", list); break; } } else { /* 10010110 */ fputs("\t\toutput\n", list); } } else { /* 1001010 */ fputs("\t\tcall\n", list); } } else { /* 100100 */ if( GetBit() ) { /* 1001001 */ if( GetBit() ) { /* 10010011 */ fputs("\t\tfsek\n", list); } else { /* 10010010 */ fputs("\t\tfget\n", list); } } else { /* 1001000 */ fputs("\t\tfput\n", list); } } } } else { /* 1000 */ if( GetBit() ) { /* 10001 */ if( GetBit() ) { /* 100011 */ fputs("\t\totherwise\n", list); } else { /* 100010 */ if( GetBit() ) { /* 1000101 */ if( GetBit() ) { /* 10001011 */ fputs("\t\tlabel_heappush\n", list); } else { /* 10001010 */ fputs("\t\tlabel_assignid\n", list); } } else { /* 1000100 */ if( GetBit() ) { /* 10001001 */ fputs("\t\tlabel_writebits\n", list); } else { /* 10001000 */ fputs("\t\tlabel_break\n", list); } } } } else { /* 10000 */ if( GetBit() ) { /* 100001 */ if( GetBit() ) { /* 1000011 */ fputs("\t\tand\n", list); } else { /* 1000010 */ fputs("\t\tshr\n", list); } } else { /* 100000 */ if( GetBit() ) { /* 1000001 */ fputs("\t\tshl\n", list); } else { /* 1000000 */ fputs("\t\tor\n", list); } } } } } } } else { /* 0 */ for(i = data = 0; i < 4; i++) data |= GetBit() << i; fprintf(list, "\t\tdata = %d\n", Constant[data]); } } fclose(list); puts("Wrote " LISTING_NAME); return 0; } /* main() */