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