]> cvs.zerfleddert.de Git - rsbs2/blobdiff - rsb-lz.c
implement command line parsing for rsbs2.pl, now it's useable
[rsbs2] / rsb-lz.c
index b45f0f8dc27939c160d958c188afd39e597b4580..2e50252d2f1786266a114ac3bdd197a7147ff91e 100644 (file)
--- a/rsb-lz.c
+++ b/rsb-lz.c
@@ -15,10 +15,10 @@ void err_exit(const char *fname)
 }
 
 struct data_in_s {
-       unsigned char *start; /* 0 */
-       unsigned char *stop; /* 4 */
-       unsigned char bit; /* 8 */
-       unsigned char x; /* 9 */
+       unsigned char *start;
+       unsigned char *stop;
+       unsigned char bitpos;
+       unsigned char byte;
 };
 
 struct data_out_s {
@@ -26,200 +26,252 @@ struct data_out_s {
        unsigned char *end;
 };
 
-unsigned char fn_597c8(struct data_in_s *r6_data)
+unsigned char get_next_in_byte(struct data_in_s *data_in)
 {
-       unsigned char *r0;
-       unsigned char *r1;
-       unsigned char r5;
+       unsigned char byte;
 
-       r5 = 0;
-
-       r0 = r6_data->start;
-       r1 = r6_data->stop;
-
-       if (r1 < r0)
+       if (data_in->stop < data_in->start)
                err_exit(__func__);
 
-       r5 = *r0;
-       r0++;
-       r6_data->start = r0;
+       byte = *(data_in->start);
+       data_in->start++;
 
-       return r5;
+       return byte;
 }
 
-unsigned int fn_59848(struct data_in_s *r6_data)
+unsigned char get_next_bit(struct data_in_s *data_in)
 {
-       unsigned char r1;
-       unsigned char r2;
-       unsigned int r5;
+       unsigned char bitval;
 
-       r1 = r6_data->bit;
-       if (r1 == 0x80) {
-               r6_data->x = fn_597c8(r6_data);
+       if (data_in->bitpos == 0x80) {
+               data_in->byte = get_next_in_byte(data_in);
        }
-       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;
+
+       bitval = data_in->bitpos & data_in->byte;
+
+       data_in->bitpos >>= 1;
+       if (data_in->bitpos == 0) {
+               data_in->bitpos = 0x80;
        }
 
-       if (r5 == 0)
+       if (bitval == 0)
                return 0;
 
        return 1;
 }
 
-unsigned int fn_598b4(struct data_in_s *r11_data, unsigned int r10_arg2)
+unsigned int get_next_bits(struct data_in_s *data_in, unsigned int bits)
 {
-       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;
+       unsigned int bit;
+       unsigned int next_bits;
+
+       bit = 1 << (bits - 1);
+
+       next_bits = 0;
+       while (bit != 0) {
+               if (data_in->bitpos == 0x80) {
+                       data_in->byte = get_next_in_byte(data_in);
                }
-               r1 = r11_data->bit;
-               r2 = r11_data->x;
-               r1 = r1 & r2;
-               if (r1 != 0)
-                       r7 = r7 | r6;
 
-               r6 = r6 >> 1;
+               if ((data_in->bitpos & data_in->byte) != 0)
+                       next_bits = next_bits | bit;
 
-               r2 = r11_data->bit;
-               r2 = r2 >> 1;
-               r11_data->bit = r2;
+               bit = bit >> 1;
 
-               r1 = r11_data->bit;
-               if(r1 == 0) {
-                       r11_data->bit = 0x80;
+               data_in->bitpos >>= 1;
+
+               if(data_in->bitpos == 0) {
+                       data_in->bitpos = 0x80;
                }
        }
 
-       return r7;
+       return next_bits;
 }
 
-void fn_5980c(unsigned int arg1, struct data_out_s *data_out)
+void write_byte(unsigned char byte, 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);
+       if (data_out->pos > data_out->end) {
                err_exit(__func__);
        }
 
-       *r1 = arg1 & 0xff;
-
-       r1++;
-       data_out->pos = r1;
+       *(data_out->pos) = byte;
+       data_out->pos++;
 }
 
-void lz_expand(struct data_in_s *r10_data, struct data_out_s *data_out)
+void lz_expand(struct data_in_s *data_in, 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];
+       unsigned int pos;
+       unsigned int wordoffset;
+       unsigned int i;
+       unsigned char byte;
+       unsigned int wordlen;
+       unsigned char buf[1024];
 
-       r5 = 1;
+       pos = 1;
 
        while (1) {
                while (1) {
-                       r2 = fn_59848(r10_data);
-                       if (r2 == 0)
+                       /* Compressed/uncompressed? */
+                       if (get_next_bit(data_in) == 0)
                                break;
 
-                       r2 = fn_598b4(r10_data, 8) & 0xff;
-                       r4 = r2;
+                       /* Uncompressed byte */
+                       byte = get_next_bits(data_in, 8);
+
+                       write_byte(byte, data_out);
 
-                       fn_5980c(r4, data_out);
-                       arr_59b64[r5] = r4 & 0xff;
-                       r2 = r5 + 1;
-                       r2 = r2 << 22;
-                       r2 = r2 >> 22;
-                       r5 = r2;
+                       /* Save byte in buffer, to be reused later */
+                       buf[pos] = byte;
+                       pos = (pos + 1) & 0x3ff;
                }
 
-               r11 = fn_598b4(r10_data, 0x0a);
-               if(r11 == 0)
+               /* offset for start of dictionary word */
+               wordoffset = get_next_bits(data_in, 0x0a);
+               if(wordoffset == 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++;
+               /* length of dictionary word used */
+               wordlen = get_next_bits(data_in, 0x04) + 1;
+               for (i = 0; i <= wordlen ; i++) {
+                       /* lookup dictionary byte */
+                       byte = buf[(wordoffset + i) & 0x3ff];
+                       write_byte(byte, data_out);
+                       /* Save byte in buffer, to be reused later */
+                       buf[pos] = byte;
+                       pos = (pos + 1) & 0x3ff;
                }
        }
 }
 
-unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic)
+void set_next_bit(unsigned char *buf, unsigned int set, unsigned int *currbit) {
+       unsigned char *pos;
+       unsigned char bitpos;
+
+       if (set) {
+               pos = buf + ((*currbit) / 8);
+               bitpos = 0x80 >> ((*currbit) % 8);
+               *pos |= bitpos;
+       }
+
+       *currbit = *currbit + 1;
+}
+
+void write_bits(unsigned char *buf, unsigned int data, unsigned int bits, unsigned int *currbit) {
+       int i;
+       unsigned int bitpos;
+
+       bitpos = 1 << (bits - 1);
+
+       for (i = 0; i < bits; i++) {
+               set_next_bit(buf, data & bitpos, currbit);
+               bitpos >>= 1;
+       }
+}
+
+unsigned char *compress_lz(unsigned char *inbuf, int inlen, int *outlen)
 {
-       unsigned int r3;
-       unsigned int r4;
-       unsigned int r5;
+       unsigned char *end = inbuf + inlen;
+       unsigned char *outbuf;
+       unsigned char window[1024];
+       int pos = 0;
+       int fill = 0;
+       unsigned int currbit = 0;
+       int offset;
+       int wordlen;
+       int found;
+       int i;
+
+       if ((outbuf = malloc((inlen * 2) + 4)) == NULL) {
+               perror("malloc");
+       }
 
-#if 0
-       if (r0 < 0xc0000000)
-               return 1;
-#endif
+       *((unsigned int*)outbuf) = LZ_MAGIC;
+       currbit = 8 * 8;
+
+       while(inbuf < end) {
+               found = 0;
+               for (wordlen = 17; wordlen > 1; wordlen--) {
+                       for (offset = 1; offset < ((fill < 1023) ? fill : 1023); offset++) {
+                               if ((fill < 1023) &&
+                                   (wordlen + offset > fill))
+                                       break;
+
+                               for (i = 0; i < wordlen; i++) {
+                                       if (inbuf[i] != window[(offset + i) & 0x3ff]) {
+                                               break;
+                                       }
+                               }
+                               if (i == wordlen)
+                                       found = 1;
+                       }
+                       if (found)
+                               break;
+               }
 
-       /* ??? */
-       r4 = *((unsigned int*)arg1 + 0x20);
-       r5 = *((unsigned int*)arg1 + 0x24);
+               if (found) {
+                       write_bits(outbuf, 0x00, 0x01, &currbit);
+                       write_bits(outbuf, offset, 0x0a, &currbit);
+                       write_bits(outbuf, wordlen - 1, 0x04, &currbit);
+                       for (i = 0; i < wordlen; i++) {
+                               window[pos] = *(inbuf + i);
+                               pos = (pos + 1) & 0x3ff;
+                       }
+                       inbuf += wordlen;
+
+                       if (fill < sizeof(window))
+                               fill += wordlen;
+               } else {
+                       write_bits(outbuf, 0x01, 0x01, &currbit);
+                       write_bits(outbuf, *inbuf, 0x08, &currbit);
+                       window[pos] = *inbuf;
+                       pos = (pos + 1) & 0x3ff;
+                       inbuf++;
+                       if (fill < sizeof(window))
+                               fill++;
+               }
+       }
+
+       write_bits(outbuf, 0x00, 0x01, &currbit);
+       write_bits(outbuf, 0x00, 0x0a, &currbit);
+
+       *outlen = (currbit / 8) + 1;
 
-       printf("magic: 0x%08x <-> 0x%08x\n", r5, magic);
-       if (r5 != magic)
+       *((unsigned int*)(outbuf + 4)) = *outlen;
+
+       return outbuf;
+}
+
+/* Checksum is only used for the compressed firmware in 'firmware' */
+unsigned int crc_check(unsigned char *buf, unsigned int len, unsigned int magic)
+{
+       unsigned int file_crc;
+       unsigned int my_len;
+       unsigned int crc;
+       unsigned int my_magic;
+
+       my_len = *((unsigned int*)(buf + 0x20));
+       my_magic = *((unsigned int*)(buf + 0x24));
+
+       if (my_magic != magic) {
+               printf("\nmagic: 0x%08x <-> 0x%08x\n", my_magic, magic);
                return 2;
+       }
        
-       if (arg2 >= r4)
-               r5 = 0;
-       else
+       if (len < my_len)
                return 3;
 
-       r5 = ~rsb_crc(~0x00, arg1, r4);
-       r3 = *((unsigned int*)(arg1 + r4));
-       printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3);
+       crc = ~rsb_crc(~0x00, buf, len);
+       file_crc = *((unsigned int*)(buf + len));
 
-       if (r3 == r5)
-               return 0;
+       if (file_crc != crc) {
+               printf("\nChecksums: 0x%08x <-> 0x%08x!\n", crc, file_crc);
+               return 4;
+       }
 
-       return 4;
+       return 0;
 }
 
-void extract_lz_file(unsigned char *inbuf, unsigned char *name)
+void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char check_crc)
 {
        unsigned int len;
        unsigned char *outbuf;
@@ -241,24 +293,25 @@ void extract_lz_file(unsigned char *inbuf, unsigned char *name)
 
        data_in.start = inbuf + 8;
        data_in.stop = inbuf + len;
-       data_in.x = 0;
-       data_in.bit = 0x80;
+       data_in.byte = 0x00;
+       data_in.bitpos = 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 (check_crc) {
+               unsigned int crclen;
+               int ret;
 
-       if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) {
-               printf("crc_check return: %d\n", ret);
-               err_exit(__func__);
+               crclen = *((unsigned int*)(outbuf + 0x20));
+
+               if ((ret = crc_check(outbuf, crclen, 0x46335053)) != 0) {
+                       printf("crc_check return: %d\n", ret);
+                       err_exit(__func__);
+               }
        }
-#endif
 
        write_file((char*)name, outbuf, len);
        
Impressum, Datenschutz