-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzw06unpack.c
277 lines (237 loc) · 6.55 KB
/
lzw06unpack.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* This code is based on Mark Nelson's 1995 book. */
/* MY ORIGINAL 1996 COMMENT: */
/*************************************************/
/* Программа распаковщика для алгоритма LZW */
/* полная очистка словаря при заполнении */
/* длина выходных кодов постоянна (12 бит) */
/*************************************************/
/* TRANSLATION: */
/**************************************************/
/* LZW decompression program with full */
/* dictionary reset when filled up. Constant */
/* 12-bit codes in input. */
/**************************************************/
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#define CLEAR_BYTE 0x10 /* it can be any value between 0x10 and 0xFF */
#define NOT_CODE (CLEAR_BYTE | (CLEAR_BYTE << 8))
static int16_t GetPrefixChar(int16_t code, const uint16_t * prefix)
{
while (code >= 256)
{
assert (code < HT_MAX_CODE && code >= 0);
code = prefix[code];
}
return code;
}
int Decompress(const char *filename, const char *outfile, int flags)
{
int i = 0, k, len, bpos = 0;
int16_t RunCode = 256, OldCode = NOT_CODE, CurPrefix;
uint16_t code;
uint8_t *buffer = NULL;
uint8_t infoBits = 0;
uint8_t infoFlag = 0;
uint32_t expectedSize = 0;
FILE *fp = NULL;
FILE *fout = NULL;
uint16_t StackCount = 0;
char label[4] = { 0 };
uint8_t version = 255;
uint16_t stack [BUFFLEN];
uint16_t suffix [HT_MAX_CODE];
uint8_t outline [BUFFLEN];
uint16_t prefix [HT_MAX_CODE];
if (is_big_endian())
{
fprintf (stderr, "Not supported on big endian machines.\n");
return 0;
}
if (!(flags & OVERWRITE_FLAG) && file_exists(outfile))
{
/* file exists and no overwrite flag set */
fprintf (stderr, "File \'%s\' already exists. Use overwrite flag.\n", outfile);
return 0;
}
fp = fopen(filename, "rb");
if (NULL == fp)
{
fprintf (stderr, "Cannot open file \'%s\'.\n", filename);
perror (NULL);
return 0;
}
if (4 != fread(label, 1, 4, fp))
{
printf("Not an LZW file!\n");
fclose (fp);
return 0;
}
if (memcmp(label, "LZW", 3) != 0)
{
printf("Not an LZW file!\n");
fclose (fp);
return 0;
}
if (1 != fread (&version, 1, 1, fp))
{
fprintf(stderr, "Unexpected read error.\n");
fclose (fp);
return 0;
}
if (version != PACKER_VERSION)
{
fprintf(stderr, "Packer/unpacker version mismatch.\n");
fclose (fp);
return 0;
}
infoBits |= (is_big_endian() ? 1 : 0);
infoBits |= VARIABLE_WIDTH ? 2 : 0;
infoBits |= ((MAX_BITS - 8) << 4);
/* get infoFlags byte: */
if (1 != fread (&infoFlag, 1, 1, fp))
{
fprintf(stderr, "Unexpected read error.\n");
fclose (fp);
return 0;
}
if (infoBits != infoFlag)
{
fprintf(stderr, "Encoding flags mismatch.\n");
fclose (fp);
return 0;
}
/* get expected output size: */
expectedSize = 0;
if (4 != fread (&expectedSize, 1, sizeof(uint32_t), fp))
{
fprintf(stderr, "Unexpected read error.\n");
fclose (fp);
return 0;
}
if (flags & VERBOSE_OUTPUT)
{
printf ("expected output size: %ld.\n", (long)expectedSize);
}
fout = fopen(outfile, "wb");
if (NULL == fout)
{
fprintf (stderr, "Cannot open file \'%s\'.\n", outfile);
perror (NULL);
fclose (fp);
return 0;
}
buffer = (unsigned char *)malloc(OUTLEN);
if (!buffer)
{
perror (NULL);
fclose(fout);
fclose(fp);
cleanup (outfile, flags);
return 0;
}
memset(prefix, CLEAR_BYTE, HT_SIZE);
memset (suffix, 0, sizeof(suffix)); /* this is just to make static analyzer happy */
while (true)
{
len = (int)fread(buffer, 1, OUTLEN, fp);
for (k = 0; k < len;)
{
if (bpos == 0)
{
code = *(uint16_t *)(buffer + k) & 0x0FFF; /* assuming little endian */
bpos = 4;
k++;
}
else
{
code = *(uint16_t *)(buffer + k) >> 4; /* assuming little endian */
bpos = 0;
k += 2;
}
if (code == EOF_CODE)
{
if (i != (int)fwrite (outline, 1, i, fout))
{
/* write error */
free (buffer);
fclose (fout);
fclose (fp);
cleanup (outfile, flags);
fprintf (stderr, "Write error. Out of disk space?\n");
return 0;
}
free (buffer);
fflush (fout);
fclose (fp);
/* compare expected size with actual size. */
if (expectedSize != ftell (fout))
{
fprintf (stderr, "Expected and actual sizes dont match.\n");
fclose (fout);
cleanup (outfile, flags);
return 0;
}
fclose(fout);
return 1;
}
else if (code == HT_CLEAR_CODE)
{
memset(prefix, CLEAR_BYTE, HT_SIZE);
RunCode = 256;
OldCode = NOT_CODE;
}
else
{
if (code < 256)
outline[i++] = (uint8_t)code;
else
{
if (prefix[code] == NOT_CODE)
{
CurPrefix = OldCode;
suffix[RunCode] = GetPrefixChar(OldCode, prefix);
stack[StackCount++] = suffix[RunCode];
}
else
CurPrefix = code;
while (CurPrefix > 255)
{
stack[StackCount++] = suffix[CurPrefix];
CurPrefix = prefix[CurPrefix];
}
stack[StackCount++] = CurPrefix;
while (StackCount != 0)
outline[i++] = (uint8_t)stack[--StackCount];
}
if ((OldCode != NOT_CODE))
{
prefix[RunCode] = OldCode;
if (code != RunCode)
suffix[RunCode] = GetPrefixChar(code, prefix);
RunCode++;
}
OldCode = code;
if (i == BUFFLEN)
{
if (BUFFLEN != fwrite(outline, 1, BUFFLEN, fout))
{
/* write error */
free (buffer);
fclose (fout);
fclose (fp);
cleanup (outfile, flags);
fprintf (stderr, "Write error. Out of disk space?\n");
return 0;
}
i = 0;
OldCode = NOT_CODE;
}
}
}
}
}