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