]>
git.zerfleddert.de Git - rsbs2/blob - rsb-lz.c
7b94aef32eee61587a2866bc23a211a7c8b8d4cc
9 #include "filesystem.h"
11 void err_exit(const char *fname
)
13 fprintf(stderr
,"%s: error extracting...\n", fname
);
29 unsigned char get_next_in_byte(struct data_in_s
*data_in
)
33 if (data_in
->stop
< data_in
->start
)
36 byte
= *(data_in
->start
);
42 unsigned char get_next_bit(struct data_in_s
*data_in
)
46 if (data_in
->bitpos
== 0x80) {
47 data_in
->byte
= get_next_in_byte(data_in
);
50 bitval
= data_in
->bitpos
& data_in
->byte
;
52 data_in
->bitpos
>>= 1;
53 if (data_in
->bitpos
== 0) {
54 data_in
->bitpos
= 0x80;
63 unsigned int get_next_bits(struct data_in_s
*data_in
, unsigned int bits
)
66 unsigned int next_bits
;
68 bit
= 1 << (bits
- 1);
72 if (data_in
->bitpos
== 0x80) {
73 data_in
->byte
= get_next_in_byte(data_in
);
76 if ((data_in
->bitpos
& data_in
->byte
) != 0)
77 next_bits
= next_bits
| bit
;
81 data_in
->bitpos
>>= 1;
83 if(data_in
->bitpos
== 0) {
84 data_in
->bitpos
= 0x80;
91 void write_byte(unsigned char byte
, struct data_out_s
*data_out
)
93 if (data_out
->pos
> data_out
->end
) {
97 *(data_out
->pos
) = byte
;
101 void lz_expand(struct data_in_s
*r10_data
, struct data_out_s
*data_out
)
109 unsigned char window
[1024];
115 r2
= get_next_bit(r10_data
);
119 r2
= get_next_bits(r10_data
, 8) & 0xff;
121 write_byte(r2
, data_out
);
122 window
[r5
] = r2
& 0xff;
123 r5
= (r5
+ 1) & 0x3ff;
126 r11
= get_next_bits(r10_data
, 0x0a);
130 r2
= get_next_bits(r10_data
, 0x04);
134 r2
= (r6
+ r11
) & 0x3ff;
136 write_byte(r4
, data_out
);
138 r5
= (r5
+ 1) & 0x3ff;
144 /* Checksum is only used for the compressed firmware in 'firmware' */
146 unsigned int crc_check_59684(unsigned char *arg1
, unsigned int arg2
, unsigned int magic
)
158 r4
= *((unsigned int*)arg1
+ 0x20);
159 r5
= *((unsigned int*)arg1
+ 0x24);
161 printf("magic: 0x%08x <-> 0x%08x\n", r5
, magic
);
170 r5
= ~rsb_crc(~0x00, arg1
, r4
);
171 r3
= *((unsigned int*)(arg1
+ r4
));
172 printf("Checksums: 0x%02x <-> 0x%02x\n", r5
, r3
);
181 void extract_lz_file(unsigned char *inbuf
, unsigned char *name
)
184 unsigned char *outbuf
;
185 struct data_in_s data_in
;
186 struct data_out_s data_out
;
188 if (*((unsigned int*)inbuf
) != LZ_MAGIC
)
191 len
= *((unsigned int*)(inbuf
+ 4));
192 printf(", length: %d", len
);
194 if ((outbuf
= malloc(len
)) == NULL
) {
201 data_in
.start
= inbuf
+ 8;
202 data_in
.stop
= inbuf
+ len
;
204 data_in
.bitpos
= 0x80;
206 data_out
.pos
= outbuf
;
207 data_out
.end
= outbuf
+ len
;
209 lz_expand(&data_in
, &data_out
);
212 /* Checksum is only used for the compressed firmware in 'firmware' */
214 r5
= *((unsigned int*)r3
);
216 if ((ret
= crc_check_59684(r7
, r5
, 0x46335053)) != 0) {
217 printf("crc_check return: %d\n", ret
);
222 write_file((char*)name
, outbuf
, len
);