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