X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/rsbs2/blobdiff_plain/650ebf3b096355d69644d60d9d8da84d947dd3a4..6b2c0993a746ebda6ae633e75a8a82533c50b506:/rsb-lz.c?ds=sidebyside diff --git a/rsb-lz.c b/rsb-lz.c index b0129b5..7d20942 100644 --- a/rsb-lz.c +++ b/rsb-lz.c @@ -1,87 +1,244 @@ -/* TODO: IMPLEMET THIS! */ -/* Probably very broken lzw implementation by Agilent: - * - * 59a38: e92d4ff0 push {r4, r5, r6, r7, r8, r9, r10, r11, r14} - * 59a3c: e1a07000 mov r7, r0 - * 59a40: e1a0a001 mov r10, r1 - * 59a44: e1a0b002 mov r11, r2 - * 59a48: ebfffeae bl 0x59508 - * 59a4c: e3a03033 mov r3, #51 ; 0x33 - * 59a50: e3a0203f mov r2, #63 ; 0x3f - * 59a54: e3a01000 mov r1, #0 ; 0x0 - * 59a58: e3a00000 mov r0, #0 ; 0x0 - * 59a5c: ebfffec5 bl 0x59578 - * 59a60: e59f3100 ldr r3, [pc, #256] ; 0x59b68 - * 59a64: e3a02033 mov r2, #51 ; 0x33 - * 59a68: e3a01540 mov r1, #268435456 ; 0x10000000 - * 59a6c: e3a00004 mov r0, #4 ; 0x4 - * 59a70: ebfffec0 bl 0x59578 - * 59a74: e59f30f0 ldr r3, [pc, #240] ; 0x59b6c - * 59a78: e3a0201b mov r2, #27 ; 0x1b - * 59a7c: e3a01550 mov r1, #335544320 ; 0x14000000 - * 59a80: e3a00005 mov r0, #5 ; 0x5 - * 59a84: ebfffebb bl 0x59578 - * 59a88: e3a03033 mov r3, #51 ; 0x33 - * 59a8c: e3a0203b mov r2, #59 ; 0x3b - * 59a90: e3a014c0 mov r1, #-1073741824 ; 0xc0000000 - * 59a94: e3a00007 mov r0, #7 ; 0x7 - * 59a98: ebfffeb6 bl 0x59578 - * 59a9c: e59f00cc ldr r0, [pc, #204] ; 0x59b70 - * 59aa0: ebfffeae bl 0x59560 - * 59aa4: e59b5000 ldr r5, [r11] - * 59aa8: e59f20c4 ldr r2, [pc, #196] ; 0x59b74 - * 59aac: e1550002 cmp r5, r2 - * 59ab0: 0a000000 beq 0x59ab8 - * 59ab4: ebffff33 bl 0x59788 - * 59ab8: e28b3004 add r3, r11, #4 ; 0x4 - * 59abc: e5935000 ldr r5, [r3] - * 59ac0: e0853007 add r3, r5, r7 - * 59ac4: e153000a cmp r3, r10 - * 59ac8: 3a000000 bcc 0x59ad0 - * 59acc: ebffff2d bl 0x59788 - * 59ad0: e28b2008 add r2, r11, #8 ; 0x8 - * 59ad4: e59f809c ldr r8, [pc, #156] ; 0x59b78 - * 59ad8: e5882000 str r2, [r8] - * 59adc: e085200b add r2, r5, r11 - * 59ae0: e59f8090 ldr r8, [pc, #144] ; 0x59b78 - * 59ae4: e5882004 str r2, [r8, #4] - * 59ae8: e3a02000 mov r2, #0 ; 0x0 - * 59aec: e59f8084 ldr r8, [pc, #132] ; 0x59b78 - * 59af0: e5c82009 strb r2, [r8, #9] - * 59af4: e3a02080 mov r2, #128 ; 0x80 - * 59af8: e59f8078 ldr r8, [pc, #120] ; 0x59b78 - * 59afc: e5c82008 strb r2, [r8, #8] - * 59b00: e59f8074 ldr r8, [pc, #116] ; 0x59b7c - * 59b04: e5887000 str r7, [r8] - * 59b08: e0853007 add r3, r5, r7 - * 59b0c: e59f8068 ldr r8, [pc, #104] ; 0x59b7c - * 59b10: e5883004 str r3, [r8, #4] - * 59b14: e59f1060 ldr r1, [pc, #96] ; 0x59b7c - * 59b18: e59f0058 ldr r0, [pc, #88] ; 0x59b78 - * 59b1c: ebffff86 bl 0x5993c - * 59b20: e2873020 add r3, r7, #32 ; 0x20 - * 59b24: e5935000 ldr r5, [r3] - * 59b28: e59f2050 ldr r2, [pc, #80] ; 0x59b80 - * 59b2c: e1a01005 mov r1, r5 - * 59b30: e1a00007 mov r0, r7 - * 59b34: ebfffed2 bl 0x59684 - * 59b38: e1a01000 mov r1, r0 - * 59b3c: e1a04001 mov r4, r1 - * 59b40: e3540000 cmp r4, #0 ; 0x0 - * 59b44: 0a000000 beq 0x59b4c - * 59b48: ebffff0e bl 0x59788 - * 59b4c: ebfffe6d bl 0x59508 - * 59b50: e8bd8ff0 pop {r4, r5, r6, r7, r8, r9, r10, r11, r15} - * 59b54: c00b003c andgt r0, r11, r12, lsr r0 - * 59b58: c00b0038 andgt r0, r11, r8, lsr r0 - * 59b5c: c00b0034 andgt r0, r11, r4, lsr r0 - * 59b60: c00b0030 andgt r0, r11, r0, lsr r0 - * 59b64: 14000418 strne r0, [r0], #-1048 - * 59b68: 00000366 andeq r0, r0, r6, ror #6 - * 59b6c: 00000733 andeq r0, r0, r3, lsr r7 - * 59b70: 00001005 andeq r1, r0, r5 - * 59b74: 6110beef tstvs r0, r15, ror #29 <- LZW Header... - * 59b78: 14000404 strne r0, [r0], #-1028 - * 59b7c: 14000410 strne r0, [r0], #-1040 - * 59b80: 46335053 undefined - */ +#include +#include +#include +#include +#include +#include +#include "rsb-crc.h" +#include "rsb-lz.h" +#include "filesystem.h" + +void err_exit(const char *fname) +{ + fprintf(stderr,"%s: error extracting...\n", fname); + exit(1); +} + +struct data_in_s { + unsigned char *start; /* 0 */ + unsigned char *stop; /* 4 */ + unsigned char bit; /* 8 */ + unsigned char x; /* 9 */ +}; + +struct data_out_s { + unsigned char *pos; + unsigned char *end; +}; + +unsigned char get_next_in_byte(struct data_in_s *data_in) +{ + unsigned char byte; + + if (data_in->stop < data_in->start) + err_exit(__func__); + + byte = *(data_in->start); + data_in->start++; + + return byte; +} + +unsigned int fn_59848(struct data_in_s *data_in) +{ + unsigned int r5; + + if (data_in->bit == 0x80) { + data_in->x = get_next_in_byte(data_in); + } + + r5 = data_in->bit & data_in->x & 0xff; + + data_in->bit >>= 1; + if (data_in->bit == 0) { + data_in->bit = 0x80; + } + + if (r5 == 0) + return 0; + + return 1; +} + +unsigned int fn_598b4(struct data_in_s *r11_data, unsigned int r10_arg2) +{ + unsigned int r1; + unsigned int r2; + unsigned int r6; + unsigned int r7; + + r1 = r10_arg2 - 1; + r6 = 1 << r1; + + r7 = 0; + while (r6 != 0) { + r1 = r11_data->bit; + if (r1 == 0x80) { + r1 = get_next_in_byte(r11_data); + r11_data->x = r1; + } + r1 = r11_data->bit; + r2 = r11_data->x; + r1 = r1 & r2; + if (r1 != 0) + r7 = r7 | r6; + + r6 = r6 >> 1; + + r2 = r11_data->bit; + r2 = r2 >> 1; + r11_data->bit = r2; + + r1 = r11_data->bit; + if(r1 == 0) { + r11_data->bit = 0x80; + } + } + + return r7; +} + +void fn_5980c(unsigned int arg1, struct data_out_s *data_out) +{ + unsigned char *r1; + unsigned char *r2; + + r1 = data_out->pos; + r2 = data_out->end; + + if (r1 > r2) { + printf("r1: 0x%08x, r2: 0x%08x\n", (unsigned int)r1, (unsigned int)r2); + err_exit(__func__); + } + + *r1 = arg1 & 0xff; + + r1++; + data_out->pos = r1; +} + +void lz_expand(struct data_in_s *r10_data, struct data_out_s *data_out) +{ + unsigned int r5; + unsigned int r2; + unsigned char r4; + unsigned int r6; + unsigned int r7; + unsigned int r11; + unsigned char arr_59b64[1024]; + + r5 = 1; + + while (1) { + while (1) { + r2 = fn_59848(r10_data); + if (r2 == 0) + break; + + r2 = fn_598b4(r10_data, 8) & 0xff; + + fn_5980c(r2, data_out); + arr_59b64[r5] = r2 & 0xff; + r5 = (r5 + 1) & 0x3ff; + } + + r11 = fn_598b4(r10_data, 0x0a); + if(r11 == 0) + return; + + r2 = fn_598b4(r10_data, 0x04); + r7 = r2 + 1; + r6 = 0; + while (r6 <= r7) { + r2 = (r6 + r11) & 0x3ff; + r4 = arr_59b64[r2]; + fn_5980c(r4, data_out); + arr_59b64[r5] = r4; + r5 = (r5 + 1) & 0x3ff; + r6++; + } + } +} + +/* Checksum is only used for the compressed firmware in 'firmware' */ +#if 0 +unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic) +{ + unsigned int r3; + unsigned int r4; + unsigned int r5; + +#if 0 + if (r0 < 0xc0000000) + return 1; +#endif + + /* ??? */ + r4 = *((unsigned int*)arg1 + 0x20); + r5 = *((unsigned int*)arg1 + 0x24); + + printf("magic: 0x%08x <-> 0x%08x\n", r5, magic); + if (r5 != magic) + return 2; + + if (arg2 >= r4) + r5 = 0; + else + return 3; + + r5 = ~rsb_crc(~0x00, arg1, r4); + r3 = *((unsigned int*)(arg1 + r4)); + printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3); + + if (r3 == r5) + return 0; + + return 4; +} +#endif + +void extract_lz_file(unsigned char *inbuf, unsigned char *name) +{ + unsigned int len; + unsigned char *outbuf; + struct data_in_s data_in; + struct data_out_s data_out; + + if (*((unsigned int*)inbuf) != LZ_MAGIC) + err_exit(__func__); + + len = *((unsigned int*)(inbuf + 4)); + printf(", length: %d", len); + + if ((outbuf = malloc(len)) == NULL) { + perror("malloc"); + exit(1); + } + + bzero(outbuf, len); + + data_in.start = inbuf + 8; + data_in.stop = inbuf + len; + data_in.x = 0; + data_in.bit = 0x80; + + data_out.pos = outbuf; + data_out.end = outbuf + len; + + lz_expand(&data_in, &data_out); + +#if 0 + /* Checksum is only used for the compressed firmware in 'firmware' */ + r3 = r7 + 0x20; + r5 = *((unsigned int*)r3); + + if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) { + printf("crc_check return: %d\n", ret); + err_exit(__func__); + } +#endif + + write_file((char*)name, outbuf, len); + + free(outbuf); +}