-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzw06pack.c
302 lines (243 loc) · 6.76 KB
/
lzw06pack.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* This code is based on Mark Nelson's 1995 book. */
/* MY ORIGINAL 1996 COMMENT: */
/*************************************************/
/* Программа упаковщика для алгоритма LZW */
/* полная очистка словаря при заполнении */
/* длина выходных кодов постоянна (12 бит) */
/*************************************************/
/* TRANSLATION: */
/**************************************************/
/* LZW compression program with full dictionary */
/* reset when filled up. Constant 12-bit codes */
/* in output. */
/**************************************************/
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#define HT_GET_KEY(l) (l >> 12)
#define HT_GET_CODE(l) (l & 0x0FFF)
#define HT_PUT_KEY(l) (l << 12)
#define HT_PUT_CODE(l) (l & 0x0FFF)
struct packHelper {
uint32_t *table ;
uint8_t outline[OUTLEN];
FILE *fp ;
FILE *fout ;
int bpos;
size_t len;
} ;
static void initializeHelper (struct packHelper *ph)
{
ph->table = NULL;
ph->fp = NULL;
ph->fout = NULL;
ph->bpos = 0;
ph->len = 0;
memset(ph->outline, 0, OUTLEN);
}
/*------------------------------------*/
static void ClearHashTable(struct packHelper *ph)
{
memset(ph->table, 0xFF, HT_SIZE * sizeof(uint32_t));
}
/*-----------------------------------*/
static void DeleteHashTable (struct packHelper *ph)
{
free (ph->table);
ph->table = NULL;
}
/*-----------------------------------*/
static int OutCode (const int16_t code, struct packHelper *ph)
{
if (ph->bpos == 0)
{
*(uint16_t *)(ph->outline + ph->len++) = code; /* assuming little endian */
ph->bpos = 4;
if (code == EOF_CODE)
{
size_t written = fwrite(ph->outline, 1, ph->len + 1, ph->fout);
if (written != ph->len + 1)
{
fprintf (stderr, "Write error. Out of disk space? \n");
return 0;
}
}
}
else
{
*(uint16_t *)(ph->outline + ph->len) |= (code << 4); /* assuming little endian */
ph->len += 2;
ph->bpos = 0;
if (ph->len == OUTLEN || code == EOF_CODE)
{
size_t written = fwrite(ph->outline, 1, ph->len, ph->fout);
if (written != ph->len)
{
fprintf (stderr, "Write error. Out of disk space? \n");
return 0;
}
ph->len = 0;
memset(ph->outline, 0, OUTLEN);
}
}
return 1;
}
/*-----------------------------------*/
static int InitHashTable(struct packHelper *ph)
{
ph->table = (uint32_t *)malloc(HT_SIZE * sizeof(uint32_t));
if (ph->table == NULL)
return 0;
ClearHashTable(ph);
return 1;
}
/*------------------------------------*/
static int16_t KeyItem (const uint16_t Item)
{
return ((Item >> 12) ^ Item) & HT_KEY_MASK;
}
/*-------------------------------------*/
static void InsertHashTable (const uint32_t Key, int16_t Code, struct packHelper *ph)
{
int HKey = KeyItem(Key);
while (HT_GET_KEY(ph->table[HKey]) != 0xFFFFFL)
HKey = (HKey + 1) & HT_KEY_MASK;
ph->table[HKey] = HT_PUT_KEY(Key) | HT_PUT_CODE(Code);
}
/*--------------------------------------------*/
static int ExistHashTable (const uint32_t Key, struct packHelper *ph)
{
int16_t HKey = KeyItem(Key);
uint32_t HTKey;
while ((HTKey = HT_GET_KEY(ph->table[HKey])) != 0xFFFFFL)
{
if (Key == HTKey)
return HT_GET_CODE(ph->table[HKey]);
HKey = (HKey + 1) & HT_KEY_MASK;
}
return -1;
}
/*-------------------------------------------------*/
int Compress(const char *filename, const char *outfile, int flags)
{
uint8_t *buffer;
int16_t CurCode, RunCode = 256, NewCode;
uint32_t NewKey;
int len, i;
const char label[4] = "LZW";
const uint8_t version = PACKER_VERSION;
uint8_t infoBits = 0;
uint32_t inputSize = 0, outputSize = 0;
int compress_ok = true;
struct packHelper ph;
if (is_big_endian())
{
fprintf (stderr, "Not supported on big endian machines.\n");
return 0;
}
initializeHelper (&ph);
ph.fp = fopen(filename, "rb");
if (NULL == ph.fp)
{
fprintf (stderr, "Cannot open input file \'%s\'.\n", filename);
perror (NULL);
return 0;
}
ph.fout = fopen (outfile, "wb");
if (NULL == ph.fout)
{
fprintf (stderr, "Cannot open output file \'%s\'.\n", outfile);
perror (NULL);
fclose (ph.fp);
return 0;
}
if (!InitHashTable(&ph))
{
perror(NULL);
fclose (ph.fp);
fclose (ph.fout);
cleanup (outfile, flags);
return 0;
}
buffer = (unsigned char *)malloc(BUFFLEN);
if (!buffer)
{
fclose (ph.fp);
fclose (ph.fout);
perror (NULL);
DeleteHashTable(&ph);
cleanup (outfile, flags);
return 0;
}
fwrite(label, 1, 4, ph.fout);
fwrite (&version, 1, 1, ph.fout);
infoBits |= (is_big_endian() ? 1 : 0);
infoBits |= VARIABLE_WIDTH ? 2 : 0;
/* leaving 2 bits reserved. */
infoBits |= ((MAX_BITS - 8) << 4); /* we use left 4 bits for MAX_BITS information; can be between 8 and 23. */
fwrite (&infoBits, 1, 1, ph.fout);
/* write size of input file. */
fseek (ph.fp, 0, SEEK_END);
inputSize = ftell (ph.fp);
fseek (ph.fp, 0, SEEK_SET);
fwrite (&inputSize, 1, sizeof(uint32_t), ph.fout);
while (compress_ok)
{
len = (int)fread(buffer, 1, BUFFLEN, ph.fp);
if (len == 0)
break;
CurCode = *buffer;
for (i = 1; i < len; i++)
{
NewKey = (((uint32_t)CurCode) << 8) + buffer[i];
if ((NewCode = ExistHashTable(NewKey, &ph)) >= 0)
{
CurCode = NewCode;
}
else
{
if (!OutCode (CurCode, &ph))
{
compress_ok = false;
break;
}
CurCode = buffer[i];
if (RunCode == HT_CLEAR_CODE)
{
ClearHashTable(&ph);
RunCode = 256;
if (!OutCode (HT_CLEAR_CODE, &ph))
{
compress_ok = false;
break;
}
}
else
{
InsertHashTable (NewKey, RunCode++, &ph);
}
}
}
if (!OutCode (CurCode, &ph))
compress_ok = false;
}
if (compress_ok)
OutCode (EOF_CODE, &ph);
DeleteHashTable(&ph);
free(buffer);
outputSize = ftell (ph.fout);
fclose(ph.fp);
fclose (ph.fout);
if (!compress_ok)
{
cleanup (outfile, flags);
}
if (compress_ok && (VERBOSE_OUTPUT & flags))
{
printf ("Compression ratio %.2f%%\n", 100.0 * (inputSize - outputSize) / inputSize );
}
return compress_ok ? 1 : 0;
}