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