]> git.zerfleddert.de Git - rsbs2/blob - rsb-lz.c
b45f0f8dc27939c160d958c188afd39e597b4580
[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 fn_597c8(struct data_in_s *r6_data)
30 {
31 unsigned char *r0;
32 unsigned char *r1;
33 unsigned char r5;
34
35 r5 = 0;
36
37 r0 = r6_data->start;
38 r1 = r6_data->stop;
39
40 if (r1 < r0)
41 err_exit(__func__);
42
43 r5 = *r0;
44 r0++;
45 r6_data->start = r0;
46
47 return r5;
48 }
49
50 unsigned int fn_59848(struct data_in_s *r6_data)
51 {
52 unsigned char r1;
53 unsigned char r2;
54 unsigned int r5;
55
56 r1 = r6_data->bit;
57 if (r1 == 0x80) {
58 r6_data->x = fn_597c8(r6_data);
59 }
60 r1 = r6_data->bit;
61 r2 = r6_data->x;
62 r1 = r1 & r2;
63 r5 = r1 & 0xff;
64
65 r1 = r6_data->bit;
66 r1 = r1 >> 1;
67 r6_data->bit = r1;
68 if (r1 == 0) {
69 r1 = 0x80;
70 r6_data->bit = r1;
71 }
72
73 if (r5 == 0)
74 return 0;
75
76 return 1;
77 }
78
79 unsigned int fn_598b4(struct data_in_s *r11_data, unsigned int r10_arg2)
80 {
81 unsigned int r1;
82 unsigned int r2;
83 unsigned int r6;
84 unsigned int r7;
85
86 r1 = r10_arg2 - 1;
87 r6 = 1 << r1;
88
89 r7 = 0;
90 while (r6 != 0) {
91 r1 = r11_data->bit;
92 if (r1 == 0x80) {
93 r1 = fn_597c8(r11_data);
94 r11_data->x = r1;
95 }
96 r1 = r11_data->bit;
97 r2 = r11_data->x;
98 r1 = r1 & r2;
99 if (r1 != 0)
100 r7 = r7 | r6;
101
102 r6 = r6 >> 1;
103
104 r2 = r11_data->bit;
105 r2 = r2 >> 1;
106 r11_data->bit = r2;
107
108 r1 = r11_data->bit;
109 if(r1 == 0) {
110 r11_data->bit = 0x80;
111 }
112 }
113
114 return r7;
115 }
116
117 void fn_5980c(unsigned int arg1, struct data_out_s *data_out)
118 {
119 unsigned char *r1;
120 unsigned char *r2;
121
122 r1 = data_out->pos;
123 r2 = data_out->end;
124
125 if (r1 > r2) {
126 printf("r1: 0x%08x, r2: 0x%08x\n", (unsigned int)r1, (unsigned int)r2);
127 err_exit(__func__);
128 }
129
130 *r1 = arg1 & 0xff;
131
132 r1++;
133 data_out->pos = r1;
134 }
135
136 void lz_expand(struct data_in_s *r10_data, struct data_out_s *data_out)
137 {
138 unsigned int r5;
139 unsigned int r2;
140 unsigned char r4;
141 unsigned int r6;
142 unsigned int r7;
143 unsigned int r11;
144 unsigned char arr_59b64[2048];
145
146 r5 = 1;
147
148 while (1) {
149 while (1) {
150 r2 = fn_59848(r10_data);
151 if (r2 == 0)
152 break;
153
154 r2 = fn_598b4(r10_data, 8) & 0xff;
155 r4 = r2;
156
157 fn_5980c(r4, data_out);
158 arr_59b64[r5] = r4 & 0xff;
159 r2 = r5 + 1;
160 r2 = r2 << 22;
161 r2 = r2 >> 22;
162 r5 = r2;
163 }
164
165 r11 = fn_598b4(r10_data, 0x0a);
166 if(r11 == 0)
167 return;
168
169 r2 = fn_598b4(r10_data, 0x04);
170 r7 = r2 + 1;
171 r6 = 0;
172 while (r6 <= r7) {
173 r2 = r6 + r11;
174 r2 = r2 << 22;
175 r2 = r2 >> 22;
176 r4 = arr_59b64[r2];
177 fn_5980c(r4, data_out);
178 arr_59b64[r5] = r4;
179 r2 = r5 + 1;
180 r2 = r2 << 22;
181 r2 = r2 >> 22;
182 r5 = r2;
183 r6++;
184 }
185 }
186 }
187
188 unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic)
189 {
190 unsigned int r3;
191 unsigned int r4;
192 unsigned int r5;
193
194 #if 0
195 if (r0 < 0xc0000000)
196 return 1;
197 #endif
198
199 /* ??? */
200 r4 = *((unsigned int*)arg1 + 0x20);
201 r5 = *((unsigned int*)arg1 + 0x24);
202
203 printf("magic: 0x%08x <-> 0x%08x\n", r5, magic);
204 if (r5 != magic)
205 return 2;
206
207 if (arg2 >= r4)
208 r5 = 0;
209 else
210 return 3;
211
212 r5 = ~rsb_crc(~0x00, arg1, r4);
213 r3 = *((unsigned int*)(arg1 + r4));
214 printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3);
215
216 if (r3 == r5)
217 return 0;
218
219 return 4;
220 }
221
222 void extract_lz_file(unsigned char *inbuf, unsigned char *name)
223 {
224 unsigned int len;
225 unsigned char *outbuf;
226 struct data_in_s data_in;
227 struct data_out_s data_out;
228
229 if (*((unsigned int*)inbuf) != LZ_MAGIC)
230 err_exit(__func__);
231
232 len = *((unsigned int*)(inbuf + 4));
233 printf(", length: %d", len);
234
235 if ((outbuf = malloc(len)) == NULL) {
236 perror("malloc");
237 exit(1);
238 }
239
240 bzero(outbuf, len);
241
242 data_in.start = inbuf + 8;
243 data_in.stop = inbuf + len;
244 data_in.x = 0;
245 data_in.bit = 0x80;
246
247 data_out.pos = outbuf;
248 data_out.end = outbuf + len;
249
250 lz_expand(&data_in, &data_out);
251
252 #if 0
253 /* This seems to still be completely broken */
254 r3 = r7 + 0x20;
255 r5 = *((unsigned int*)r3);
256
257 if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) {
258 printf("crc_check return: %d\n", ret);
259 err_exit(__func__);
260 }
261 #endif
262
263 write_file((char*)name, outbuf, len);
264
265 free(outbuf);
266 }
Impressum, Datenschutz