#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include "rsb-crc.h"
#include "rsb-lz.h"
+#include "filesystem.h"
/* TODO: IMPLEMET THIS! */
/* Probably very broken lzw implementation by Agilent:
* 59b80: 46335053 undefined
*/
-void search_lz_sections(unsigned char *fw, int len)
+void err_exit(const char *fname)
{
- int i;
- unsigned char *j;
-
- for(i = 0; i < len - 4; i++) {
- if (*((unsigned int*)(fw+i)) == LZ_MAGIC) {
- j = fw + i - 1;
- if (*j != 0x00)
- continue;
- printf("0x%02x: ", i);
- j--;
- while (j > fw) {
- if (*j == 0x00) {
- printf("%s", j+1);
- break;
- }
- j--;
- }
- printf("\n");
+ 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 fn_597c8(struct data_in_s *r6_data)
+{
+ unsigned char *r0;
+ unsigned char *r1;
+ unsigned char r5;
+
+ r5 = 0;
+
+ r0 = r6_data->start;
+ r1 = r6_data->stop;
+
+ if (r1 < r0)
+ err_exit(__func__);
+
+ r5 = *r0;
+ r0++;
+ r6_data->start = r0;
+
+ return r5;
+}
+
+unsigned int fn_59848(struct data_in_s *r6_data)
+{
+ unsigned char r1;
+ unsigned char r2;
+ unsigned int r5;
+
+ r1 = r6_data->bit;
+ if (r1 == 0x80) {
+ r6_data->x = fn_597c8(r6_data);
+ }
+ r1 = r6_data->bit;
+ r2 = r6_data->x;
+ r1 = r1 & r2;
+ r5 = r1 & 0xff;
+
+ r1 = r6_data->bit;
+ r1 = r1 >> 1;
+ r6_data->bit = r1;
+ if (r1 == 0) {
+ r1 = 0x80;
+ r6_data->bit = r1;
+ }
+
+ 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 = fn_597c8(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[2048];
+
+ r5 = 1;
+
+ while (1) {
+ while (1) {
+ r2 = fn_59848(r10_data);
+ if (r2 == 0)
+ break;
+
+ r2 = fn_598b4(r10_data, 8) & 0xff;
+ r4 = r2;
+
+ fn_5980c(r4, data_out);
+ arr_59b64[r5] = r4 & 0xff;
+ r2 = r5 + 1;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r5 = r2;
+ }
+
+ 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;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r4 = arr_59b64[r2];
+ fn_5980c(r4, data_out);
+ arr_59b64[r5] = r4;
+ r2 = r5 + 1;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r5 = r2;
+ r6++;
+ }
+ }
+}
+
+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;
+}
+
+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
+ /* This seems to still be completely broken */
+ 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);
}