]> git.zerfleddert.de Git - rsbs2/blobdiff - rsb-lz.c
add comments about compression algorithm implementation
[rsbs2] / rsb-lz.c
index 7b94aef32eee61587a2866bc23a211a7c8b8d4cc..4dddd9b37997d778f316472bad6cb7e736ab0d59 100644 (file)
--- a/rsb-lz.c
+++ b/rsb-lz.c
@@ -98,87 +98,82 @@ void write_byte(unsigned char byte, struct data_out_s *data_out)
        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 window[1024];
+       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 = get_next_bit(r10_data);
-                       if (r2 == 0)
+                       /* Compressed/uncompressed? */
+                       if (get_next_bit(data_in) == 0)
                                break;
 
-                       r2 = get_next_bits(r10_data, 8) & 0xff;
+                       /* Uncompressed byte */
+                       byte = get_next_bits(data_in, 8);
+
+                       write_byte(byte, data_out);
 
-                       write_byte(r2, data_out);
-                       window[r5] = r2 & 0xff;
-                       r5 = (r5 + 1) & 0x3ff;
+                       /* Save byte in buffer, to be reused later */
+                       buf[pos] = byte;
+                       pos = (pos + 1) & 0x3ff;
                }
 
-               r11 = get_next_bits(r10_data, 0x0a);
-               if(r11 == 0)
+               /* offset for start of dictionary word */
+               wordoffset = get_next_bits(data_in, 0x0a);
+               if(wordoffset == 0)
                        return;
 
-               r2 = get_next_bits(r10_data, 0x04);
-               r7 = r2 + 1;
-               r6 = 0;
-               while (r6 <= r7) {
-                       r2 = (r6 + r11) & 0x3ff;
-                       r4 = window[r2];
-                       write_byte(r4, data_out);
-                       window[r5] = r4;
-                       r5 = (r5 + 1) & 0x3ff;
-                       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;
                }
        }
 }
 
 /* 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 crc_check(unsigned char *buf, unsigned int len, unsigned int magic)
 {
-       unsigned int r3;
-       unsigned int r4;
-       unsigned int r5;
-
-#if 0
-       if (r0 < 0xc0000000)
-               return 1;
-#endif
+       unsigned int file_crc;
+       unsigned int my_len;
+       unsigned int crc;
+       unsigned int my_magic;
 
-       /* ??? */
-       r4 = *((unsigned int*)arg1 + 0x20);
-       r5 = *((unsigned int*)arg1 + 0x24);
+       my_len = *((unsigned int*)(buf + 0x20));
+       my_magic = *((unsigned int*)(buf + 0x24));
 
-       printf("magic: 0x%08x <-> 0x%08x\n", r5, magic);
-       if (r5 != magic)
+       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;
 }
-#endif
 
-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;
@@ -208,16 +203,17 @@ void extract_lz_file(unsigned char *inbuf, unsigned char *name)
 
        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 (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