forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzlib2.c
305 lines (240 loc) · 6.19 KB
/
lzlib2.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
303
304
305
#include <stdio.h>
#define WINDOW_SIZE 4096
#define LENSHIFT 4 // this must be log2(LOOKAHEAD_SIZE)
#define LOOKAHEAD_SIZE (1<<LENSHIFT)
extern char *malloc(int);
typedef struct node_struct node_t;
///////////////////////////////////////////////////////////////////////////
// IMPORTANT: FOLLOWING STRUCTURE MUST BE 16 BYTES IN LENGTH //
///////////////////////////////////////////////////////////////////////////
struct node_struct
{
unsigned char *pointer;
node_t *prev;
node_t *next;
int pad;
};
typedef struct list_struct
{
node_t *start;
node_t *end;
} list_t;
static list_t hashtable[256]; // used for faster encoding
static node_t hashtarget[WINDOW_SIZE]; // what the hash points to
//
// Adds a node to the hash table at the beginning of a particular index
// Removes the node in its place before.
//
void addnode(unsigned char *pointer)
{
list_t *list;
int targetindex;
node_t *target;
targetindex = (int) pointer & ( WINDOW_SIZE - 1 );
// remove the target node at this index
target = &hashtarget[targetindex];
if (target->pointer)
{
list = &hashtable[*target->pointer];
if (target->prev)
{
list->end = target->prev;
target->prev->next = 0;
}
else
{
list->end = 0;
list->start = 0;
}
}
// add a new node to the start of the hashtable list
list = &hashtable[*pointer];
target->pointer = pointer;
target->prev = 0;
target->next = list->start;
if (list->start)
list->start->prev = target;
else
list->end = target;
list->start = target;
}
void derror(char *msg)
{
fprintf(stderr, "\nerror: %s\n\n", msg);
exit(-1);
}
unsigned char *encode(unsigned char *input, int inputlen, int *size)
{
int putidbyte = 0;
unsigned char *encodedpos;
int encodedlen;
int i, pacifier=0;
int len;
int numbytes, numcodes;
int codelencount;
unsigned char *window;
unsigned char *lookahead;
unsigned char *idbyte;
unsigned char *output, *ostart;
node_t *hashp;
int lookaheadlen;
int samelen;
// initialize the hash table to the occurences of bytes
for (i=0 ; i<256 ; i++)
{
hashtable[i].start = 0;
hashtable[i].end = 0;
}
// initialize the hash table target
for (i=0 ; i<WINDOW_SIZE ; i++)
{
hashtarget[i].pointer = 0;
hashtarget[i].next = 0;
hashtarget[i].prev = 0;
}
// create the output
ostart = output = (unsigned char *) malloc((inputlen * 9)/8+1);
// initialize the window & lookahead
lookahead = window = input;
numbytes = numcodes = codelencount = 0;
while (inputlen > 0)
{
// set the window position and size
window = lookahead - WINDOW_SIZE;
if (window < input) window = input;
// decide whether to allocate a new id byte
if (!putidbyte)
{
idbyte = output++;
*idbyte = 0;
}
putidbyte = (putidbyte + 1) & 7;
// go through the hash table of linked lists to find the strings
// starting with the first character in the lookahead
encodedlen = 0;
lookaheadlen = inputlen < LOOKAHEAD_SIZE ? inputlen : LOOKAHEAD_SIZE;
hashp = hashtable[lookahead[0]].start;
while (hashp)
{
samelen = 0;
len = lookaheadlen;
while (len-- && hashp->pointer[samelen] == lookahead[samelen])
samelen++;
if (samelen > encodedlen)
{
encodedlen = samelen;
encodedpos = hashp->pointer;
}
if (samelen == lookaheadlen) break;
hashp = hashp->next;
}
// encode the match and specify the length of the encoding
if (encodedlen >= 3)
{
*idbyte = (*idbyte >> 1) | 0x80;
*output++ = ((lookahead-encodedpos-1) >> LENSHIFT);
*output++ = ((lookahead-encodedpos-1) << LENSHIFT) | (encodedlen-1);
numcodes++;
codelencount+=encodedlen;
} else { // or just store the unmatched byte
encodedlen = 1;
*idbyte = (*idbyte >> 1);
*output++ = *lookahead;
numbytes++;
}
// update the hash table as the window slides
for (i=0 ; i<encodedlen ; i++)
addnode(lookahead++);
// reduce the input size
inputlen -= encodedlen;
// print pacifier dots
pacifier -= encodedlen;
if (pacifier<=0)
{
fprintf(stderr, ".");
pacifier += 10000;
}
}
// done with encoding- now wrap up
if (inputlen != 0)
fprintf(stderr, "warning: inputlen != 0\n");
// put the end marker on the file
if (!putidbyte)
{
idbyte = output++;
*idbyte = 1;
}
else
*idbyte = ((*idbyte>>1)|0x80)>>(7-putidbyte);
*output++ = 0;
*output++ = 0;
*size = output - ostart;
/*
fprintf(stderr, "\nnum bytes = %d\n", numbytes);
fprintf(stderr, "num codes = %d\n", numcodes);
fprintf(stderr, "ave code length = %f\n", (double) codelencount/numcodes);
fprintf(stderr, "size = %d\n", *size);
*/
return ostart;
}
//
// Return the size of compressed data
//
int decodedsize(unsigned char *input)
{
typedef unsigned short ushort;
typedef unsigned long ulong;
typedef unsigned char uchar;
ushort getidbyte = 0;
short len;
uchar idbyte;
int accum = 0;
while (1)
{
// get a new idbyte if necessary
if (!getidbyte) idbyte = *input++;
getidbyte = (getidbyte + 1) & 7;
if (idbyte&1)
{
// decompress
input++;
len = *input++ & 0xf;
if (!len) break;
accum += len+1;
}
else
accum++;
input++;
idbyte = idbyte >> 1;
}
return accum;
}
void decode(unsigned char *input, unsigned char *output)
{
int getidbyte = 0;
int len;
int pos;
int i;
unsigned char *source;
int idbyte;
while (1)
{
// get a new idbyte if necessary
if (!getidbyte) idbyte = *input++;
getidbyte = (getidbyte + 1) & 7;
if (idbyte&1)
{
// decompress
pos = *input++ << LENSHIFT;
pos = pos | (*input >> LENSHIFT);
source = output - pos - 1;
len = (*input++ & 0xf)+1;
if (len==1) break;
for (i=0 ; i<len ; i++)
*output++ = *source++;
} else {
*output++ = *input++;
}
idbyte = idbyte >> 1;
}
}