-
Notifications
You must be signed in to change notification settings - Fork 5
/
dzx7b.c
263 lines (230 loc) · 7.19 KB
/
dzx7b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* (c) Copyright 2015 by Einar Saukas. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of its author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *ifp;
FILE *ofp;
char *input_name;
char *output_name;
unsigned char *input_data;
unsigned char *output_data;
size_t input_index;
size_t output_index;
size_t input_size;
size_t output_size;
size_t partial_counter;
size_t total_counter;
int bit_mask;
int bit_value;
int read_byte() {
return input_data[input_index++];
}
int read_bit() {
bit_mask >>= 1;
if (bit_mask == 0) {
bit_mask = 128;
bit_value = read_byte();
}
return bit_value & bit_mask ? 1 : 0;
}
int read_elias_gamma() {
int i;
int value;
value = 1;
while (!read_bit()) {
value = value << 1 | read_bit();
}
if( (value&255)==255 )
value= -1;
return value;
}
int read_offset() {
int value;
int i;
value = read_byte();
if (value < 128) {
return value;
} else {
i = read_bit();
i = i << 1 | read_bit();
i = i << 1 | read_bit();
i = i << 1 | read_bit();
return (value & 127 | i << 7) + 128;
}
}
void write_byte(int value) {
output_data[output_index++] = value;
}
void write_bytes(int offset, int length) {
if (offset > output_size+output_index) {
fprintf(stderr, "Error: Invalid data in input file %s\n", input_name);
exit(1);
}
while (length-- > 0) {
write_byte(output_data[output_index-offset]);
}
}
void decompress() {
int length,i;
input_index = 0;
partial_counter = 0;
output_index = 0;
bit_mask = 0;
write_byte(read_byte());
while (1) {
if (!read_bit()) {
write_byte(read_byte());
} else {
length = read_elias_gamma()+1;
if (length == 0) {
return;
}
write_bytes(read_offset()+1, length);
}
}
}
int main(int argc, char *argv[]) {
int forced_mode = 0;
int i;
printf("DZX7B: LZ77/LZSS decompression by Einar Saukas\n");
/* process hidden optional parameters */
for (i = 1; i < argc && *argv[i] == '-'; i++) {
if (!strcmp(argv[i], "-f")) {
forced_mode = 1;
} else {
fprintf(stderr, "Error: Invalid parameter %s\n", argv[i]);
exit(1);
}
}
/* determine output filename */
if (argc == i+1) {
input_name = argv[i];
input_size = strlen(input_name);
if (input_size > 4 && !strcmp(input_name+input_size-4, ".zx7")) {
output_name = (char *)malloc(input_size);
strcpy(output_name, input_name);
output_name[input_size-4] = '\0';
} else {
fprintf(stderr, "Error: Cannot infer output filename\n");
exit(1);
}
} else if (argc == i+2) {
input_name = argv[i];
output_name = argv[i+1];
} else {
fprintf(stderr, "Usage: %s [-f] input.zx7 [output]\n"
" -f Force overwrite of output file\n", argv[0]);
exit(1);
}
/* open input file */
ifp = fopen(input_name, "rb");
if (!ifp) {
fprintf(stderr, "Error: Cannot access input file %s\n", input_name);
exit(1);
}
/* determine input size */
fseek(ifp, 0L, SEEK_END);
input_size = ftell(ifp);
fseek(ifp, 0L, SEEK_SET);
if (!input_size) {
fprintf(stderr, "Error: Empty input file %s\n", argv[1]);
exit(1);
}
/* allocate input buffer */
input_data = (unsigned char *)malloc(input_size);
if (!input_data) {
fprintf(stderr, "Error: Insufficient memory\n");
exit(1);
}
/* read input file */
total_counter = 0;
do {
partial_counter = fread(input_data+total_counter, sizeof(char), input_size-total_counter, ifp);
total_counter += partial_counter;
} while ( partial_counter > 0 );
if (total_counter != input_size) {
fprintf(stderr, "Error: Cannot read input file %s\n", argv[1]);
exit(1);
}
/* check output file */
if (!forced_mode && fopen(output_name, "rb") != NULL) {
fprintf(stderr, "Error: Already existing output file %s\n", output_name);
exit(1);
}
/* create output file */
ofp = fopen(output_name, "wb");
if (!ofp) {
fprintf(stderr, "Error: Cannot create output file %s\n", output_name);
exit(1);
}
/* reverse input data */
for ( i= 0; i<input_size>>1; i++ ){
partial_counter = input_data[i];
input_data[i] = input_data[input_size-1-i];
input_data[input_size-1-i] = partial_counter;
}
/* calculate output file size and allocate memory */
output_size = 1;
read_byte();
while (1) {
if (!read_bit()) {
output_size++;
read_byte();
} else {
i = read_elias_gamma()+1;
if (i == 0) {
break;
}
read_offset();
output_size+= i;
}
}
output_data = (unsigned char *)malloc(output_size);
if (!output_data) {
fprintf(stderr, "Error: Insufficient memory\n");
exit(1);
}
/* decompress */
decompress();
/* reverse output data */
for ( i= 0; i<output_size>>1; i++ ) {
partial_counter= output_data[i];
output_data[i]= output_data[output_size-1-i];
output_data[output_size-1-i]= partial_counter;
}
/* write output file */
if (fwrite(output_data, sizeof(char), output_size, ofp) != output_size) {
fprintf(stderr, "Error: Cannot write output file %s\n", output_name);
exit(1);
}
/* close input file */
fclose(ifp);
/* close output file */
fclose(ofp);
/* done! */
printf("File converted from %lu to %lu bytes!\n", (unsigned long)input_size, (unsigned long)output_size);
return 0;
}