]> git.zerfleddert.de Git - rsbs2/blob - rsb-lz.c
7d20942c5af1ed230af33010b15fa66f65ab889a
[rsbs2] / rsb-lz.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include "rsb-crc.h"
8 #include "rsb-lz.h"
9 #include "filesystem.h"
10
11 void err_exit(const char *fname)
12 {
13 fprintf(stderr,"%s: error extracting...\n", fname);
14 exit(1);
15 }
16
17 struct data_in_s {
18 unsigned char *start; /* 0 */
19 unsigned char *stop; /* 4 */
20 unsigned char bit; /* 8 */
21 unsigned char x; /* 9 */
22 };
23
24 struct data_out_s {
25 unsigned char *pos;
26 unsigned char *end;
27 };
28
29 unsigned char get_next_in_byte(struct data_in_s *data_in)
30 {
31 unsigned char byte;
32
33 if (data_in->stop < data_in->start)
34 err_exit(__func__);
35
36 byte = *(data_in->start);
37 data_in->start++;
38
39 return byte;
40 }
41
42 unsigned int fn_59848(struct data_in_s *data_in)
43 {
44 unsigned int r5;
45
46 if (data_in->bit == 0x80) {
47 data_in->x = get_next_in_byte(data_in);
48 }
49
50 r5 = data_in->bit & data_in->x & 0xff;
51
52 data_in->bit >>= 1;
53 if (data_in->bit == 0) {
54 data_in->bit = 0x80;
55 }
56
57 if (r5 == 0)
58 return 0;
59
60 return 1;
61 }
62
63 unsigned int fn_598b4(struct data_in_s *r11_data, unsigned int r10_arg2)
64 {
65 unsigned int r1;
66 unsigned int r2;
67 unsigned int r6;
68 unsigned int r7;
69
70 r1 = r10_arg2 - 1;
71 r6 = 1 << r1;
72
73 r7 = 0;
74 while (r6 != 0) {
75 r1 = r11_data->bit;
76 if (r1 == 0x80) {
77 r1 = get_next_in_byte(r11_data);
78 r11_data->x = r1;
79 }
80 r1 = r11_data->bit;
81 r2 = r11_data->x;
82 r1 = r1 & r2;
83 if (r1 != 0)
84 r7 = r7 | r6;
85
86 r6 = r6 >> 1;
87
88 r2 = r11_data->bit;
89 r2 = r2 >> 1;
90 r11_data->bit = r2;
91
92 r1 = r11_data->bit;
93 if(r1 == 0) {
94 r11_data->bit = 0x80;
95 }
96 }
97
98 return r7;
99 }
100
101 void fn_5980c(unsigned int arg1, struct data_out_s *data_out)
102 {
103 unsigned char *r1;
104 unsigned char *r2;
105
106 r1 = data_out->pos;
107 r2 = data_out->end;
108
109 if (r1 > r2) {
110 printf("r1: 0x%08x, r2: 0x%08x\n", (unsigned int)r1, (unsigned int)r2);
111 err_exit(__func__);
112 }
113
114 *r1 = arg1 & 0xff;
115
116 r1++;
117 data_out->pos = r1;
118 }
119
120 void lz_expand(struct data_in_s *r10_data, struct data_out_s *data_out)
121 {
122 unsigned int r5;
123 unsigned int r2;
124 unsigned char r4;
125 unsigned int r6;
126 unsigned int r7;
127 unsigned int r11;
128 unsigned char arr_59b64[1024];
129
130 r5 = 1;
131
132 while (1) {
133 while (1) {
134 r2 = fn_59848(r10_data);
135 if (r2 == 0)
136 break;
137
138 r2 = fn_598b4(r10_data, 8) & 0xff;
139
140 fn_5980c(r2, data_out);
141 arr_59b64[r5] = r2 & 0xff;
142 r5 = (r5 + 1) & 0x3ff;
143 }
144
145 r11 = fn_598b4(r10_data, 0x0a);
146 if(r11 == 0)
147 return;
148
149 r2 = fn_598b4(r10_data, 0x04);
150 r7 = r2 + 1;
151 r6 = 0;
152 while (r6 <= r7) {
153 r2 = (r6 + r11) & 0x3ff;
154 r4 = arr_59b64[r2];
155 fn_5980c(r4, data_out);
156 arr_59b64[r5] = r4;
157 r5 = (r5 + 1) & 0x3ff;
158 r6++;
159 }
160 }
161 }
162
163 /* Checksum is only used for the compressed firmware in 'firmware' */
164 #if 0
165 unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic)
166 {
167 unsigned int r3;
168 unsigned int r4;
169 unsigned int r5;
170
171 #if 0
172 if (r0 < 0xc0000000)
173 return 1;
174 #endif
175
176 /* ??? */
177 r4 = *((unsigned int*)arg1 + 0x20);
178 r5 = *((unsigned int*)arg1 + 0x24);
179
180 printf("magic: 0x%08x <-> 0x%08x\n", r5, magic);
181 if (r5 != magic)
182 return 2;
183
184 if (arg2 >= r4)
185 r5 = 0;
186 else
187 return 3;
188
189 r5 = ~rsb_crc(~0x00, arg1, r4);
190 r3 = *((unsigned int*)(arg1 + r4));
191 printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3);
192
193 if (r3 == r5)
194 return 0;
195
196 return 4;
197 }
198 #endif
199
200 void extract_lz_file(unsigned char *inbuf, unsigned char *name)
201 {
202 unsigned int len;
203 unsigned char *outbuf;
204 struct data_in_s data_in;
205 struct data_out_s data_out;
206
207 if (*((unsigned int*)inbuf) != LZ_MAGIC)
208 err_exit(__func__);
209
210 len = *((unsigned int*)(inbuf + 4));
211 printf(", length: %d", len);
212
213 if ((outbuf = malloc(len)) == NULL) {
214 perror("malloc");
215 exit(1);
216 }
217
218 bzero(outbuf, len);
219
220 data_in.start = inbuf + 8;
221 data_in.stop = inbuf + len;
222 data_in.x = 0;
223 data_in.bit = 0x80;
224
225 data_out.pos = outbuf;
226 data_out.end = outbuf + len;
227
228 lz_expand(&data_in, &data_out);
229
230 #if 0
231 /* Checksum is only used for the compressed firmware in 'firmware' */
232 r3 = r7 + 0x20;
233 r5 = *((unsigned int*)r3);
234
235 if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) {
236 printf("crc_check return: %d\n", ret);
237 err_exit(__func__);
238 }
239 #endif
240
241 write_file((char*)name, outbuf, len);
242
243 free(outbuf);
244 }
Impressum, Datenschutz