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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This code is based on the original source code of Lord Avalot d'Argent version 1.3.
* Copyright (c) 1994-1995 Mike, Mark and Thomas Thurman.
*/
/* This is the first version. Thanks to Pib. */
/*--------------------------------------------------------------------------*/
/* */
/* Global declarations for PIBLZW */
/* */
/*--------------------------------------------------------------------------*/
namespace Avalanche {
const integer maxbuff = 8192 /* Buffer size for input and output files */;
const integer maxtab = 4095 /* Table size - 1 ==> 2**10-1 ==> 12 bits */;
const integer no_prev = 0x7fff /* Special code for no previous character */;
const integer eof_char = -2 /* Marks end of file */;
const integer end_list = -1 /* Marks end of a list */;
const integer empty = -3 /* Indicates empty */;
typedef varying_string<255> anystr/* General string type */;
/* One node in parsing table. */
struct string_table_entry {
boolean used /* Is this node used yet? */;
integer prevchar /* Code for preceding string */;
integer follchar /* Code for current character */;
integer next /* Next dupl in collision list */;
};
untyped_file input_file /* Input file */;
untyped_file output_file /* Output file */;
integer inbufsize /* Count of chars in input buffer */;
array<1, maxbuff, byte> input_buffer /* Input buffer area */;
array<1, maxbuff, byte> output_buffer /* Output buffer area */;
integer input_pos /* Cur. pos. in input buffer */;
integer output_pos /* Cur. pos. in output buffer */;
/* String table */
array<0, maxtab, string_table_entry> string_table;
integer table_used /* # string table entries used */;
integer output_code /* Output compressed code */;
integer input_code /* Input compressed code */;
boolean if_compressing /* TRUE if compressing file */;
integer ierr /* Input/output error */;
string header;
varying_string<30> describe;
byte method;
void terminate()
{
/* Terminate */
/* Write any remaining characters */
/* to output file. */
if (output_pos > 0)
blockwrite(output_file, output_buffer, output_pos);
ierr = ioresult;
/* Close input and output files */
close(input_file);
ierr = ioresult;
close(output_file);
ierr = ioresult;
output << "done." << NL;
} /* Terminate */
/*--------------------------------------------------------------------------*/
/* Get_Hash_Code --- Gets hash code for given <w>C string */
/*--------------------------------------------------------------------------*/
integer get_hash_code(integer prevc, integer follc)
{
integer index;
integer index2;
/* Get_Hash_Code */
/* Get initial index using hashing */
integer get_hash_code_result;
index = ((prevc << 5) ^ follc) & maxtab;
/* If entry not already used, return */
/* its index as hash code for <w>C. */
if (! string_table[index].used)
get_hash_code_result = index;
else
/* If entry already used, search to */
/* end of list of hash collision */
/* entries for this hash code. */
/* Do linear probe to find an */
/* available slot. */
{
/* Skip to end of collision list ... */
while (string_table[index].next != end_list)
index = string_table[index].next;
/* Begin linear probe down a bit from */
/* last entry in collision list ... */
index2 = (index + 101) & maxtab;
/* Look for unused entry using linear */
/* probing ... */
while (string_table[index2].used)
index2 = succ(integer, index2) & maxtab;
/* Point prior end of collision list */
/* to this new node. */
string_table[index].next = index2;
/* Return hash code for <w>C */
get_hash_code_result = index2;
}
return get_hash_code_result;
} /* Get_Hash_Code */
/*--------------------------------------------------------------------------*/
/* Make_Table_Entry --- Enter <w>C string in string table */
/*--------------------------------------------------------------------------*/
void make_table_entry(integer prevc, integer follc)
{
/* Make_Table_Entry */
/* Only enter string if there is room left */
if (table_used <= maxtab) {
{
string_table_entry &with = string_table[ get_hash_code(prevc , follc) ];
with.used = true;
with.next = end_list;
with.prevchar = prevc;
with.follchar = follc;
}
/* Increment count of items used */
table_used += 1;
/*
IF ( Table_Used > ( MaxTab + 1 ) ) THEN
BEGIN
WRITELN('Hash table full.');
END;
*/
}
} /* Make_Table_Entry */
/*--------------------------------------------------------------------------*/
/* Initialize_String_Table --- Initialize string table */
/*--------------------------------------------------------------------------*/
void initialize_string_table()
{
integer i;
/* Initialize_String_Table */
/* No entries used in table yet */
table_used = 0;
/* Clear all table entries */
for (i = 0; i <= maxtab; i ++) {
string_table_entry &with = string_table[i];
with.prevchar = no_prev;
with.follchar = no_prev;
with.next = -1;
with.used = false;
}
/* Enter all single characters into */
/* table */
for (i = 0; i <= 255; i ++)
make_table_entry(no_prev , i);
} /* Initialize_String_Table */
/*--------------------------------------------------------------------------*/
/* Initialize --- Initialize compression/decompression */
/*--------------------------------------------------------------------------*/
void initialize()
{
anystr input_name /* Input file name */;
anystr output_name /* Output file name */;
/* Initialize */
output << "Number of file to compress:";
input >> input_name >> NL;
output << "For the moment, I'm writing the compressed version to v:compr" <<
input_name << ".avd." << NL;
output_name = string("d:compr") + input_name + ".avd";
input_name = string("d:place") + input_name + ".avd";
output << "Wait... ";
/* Open input file */
assign(input_file , input_name);
reset(input_file , 1);
ierr = ioresult;
blockread(input_file, header, 146);
blockread(input_file, describe, 30);
blockread(input_file, method, 1);
if (method == 177) {
output << "It's already compressed!" << NL;
exit(177);
}
/* Open output file */
assign(output_file , output_name);
rewrite(output_file , 1);
ierr = ioresult;
method = 177;
blockwrite(output_file, header, 146);
blockwrite(output_file, describe, 30);
blockwrite(output_file, method, 1);
/* Point input point past end of */
/* buffer to force initial read */
input_pos = maxbuff + 1;
/* Nothing written out yet */
output_pos = 0;
/* Nothing read in yet */
inbufsize = 0;
/* No input or output codes yet */
/* constructed */
output_code = empty;
input_code = empty;
/* Initialize string hash table */
initialize_string_table();
} /* Initialize */
/*--------------------------------------------------------------------------*/
/* Lookup_String --- Look for string <w>C in string table */
/*--------------------------------------------------------------------------*/
integer lookup_string(integer prevc, integer follc)
{
integer index;
integer index2;
boolean found;
/* Lookup_String */
/* Initialize index to check from hash */
integer lookup_string_result;
index = ((prevc << 5) ^ follc) & maxtab;
/* Assume we won't find string */
lookup_string_result = end_list;
/* Search through list of hash collision */
/* entries for one that matches <w>C */
do {
found = (string_table[index].prevchar == prevc) &&
(string_table[index].follchar == follc);
if (! found)
index = string_table[index].next;
} while (!(found || (index == end_list)));
/* Return index if <w>C found in table. */
if (found)
lookup_string_result = index;
return lookup_string_result;
} /* Lookup_String */
/*--------------------------------------------------------------------------*/
/* Get_Char --- Read character from input file */
/*--------------------------------------------------------------------------*/
void get_char(integer &c)
{
/* Get_Char */
/* Point to next character in buffer */
input_pos += 1;
/* If past end of block read in, then */
/* reset input pointer and read in */
/* next block. */
if (input_pos > inbufsize) {
blockread(input_file, input_buffer, maxbuff, inbufsize);
input_pos = 1;
ierr = ioresult;
}
/* If end of file hit, return EOF_Char */
/* otherwise return next character in */
/* input buffer. */
if (inbufsize == 0)
c = eof_char;
else
c = input_buffer[input_pos];
} /* Get_Char */
/*--------------------------------------------------------------------------*/
/* Write_Char --- Write character to output file */
/*--------------------------------------------------------------------------*/
void put_char(integer c)
{
/* Put_Char */
/* If buffer full, write it out and */
/* reset output buffer pointer. */
if (output_pos >= maxbuff) {
blockwrite(output_file, output_buffer, maxbuff);
output_pos = 0;
ierr = ioresult;
}
/* Place character in next slot in */
/* output buffer. */
output_pos += 1;
output_buffer[output_pos] = c;
} /* Put_Char */
/*--------------------------------------------------------------------------*/
/* Put_Code --- Write hash code to output file. */
/*--------------------------------------------------------------------------*/
void put_code(integer hash_code)
{
/* Put_Code */
/* Output code word is empty. */
/* Put out 1st 8 bits of compression */
/* code and save last 4 bit for next */
/* time through. */
if (output_code == empty) {
put_char(((cardinal)hash_code >> 4) & 0xff);
output_code = hash_code & 0xf;
} else
/* Output code word not empty. */
/* Put out last 4 bits of previous */
/* code appended to 1st 4 bits of this */
/* code. Then put out last 8 bits of */
/* this code. */
{
put_char(((output_code << 4) & 0xff0) +
(((cardinal)hash_code >> 8) & 0xf));
put_char(hash_code & 0xff);
output_code = empty;
}
} /* Put_Code */
/*--------------------------------------------------------------------------*/
/* Do_Compression --- Perform Lempel-Ziv-Welch compression */
/*--------------------------------------------------------------------------*/
void do_compression()
{
integer c /* Current input character = C */;
integer wc /* Hash code value for <w>C */;
integer w /* Hash code value for <w> */;
/* Do_Compression */
/* Read first character ==> Step 2 */
get_char(c);
/* Initial hash code -- first character */
/* has no previous string (<w> is null) */
w = lookup_string(no_prev , c);
/* Get next character ==> Step 3 */
get_char(c);
/* Loop over input characters until */
/* end of file reached ==> Step 4. */
while (c != eof_char) {
/* See if <w>C is in table. */
wc = lookup_string(w , c);
/* If <w>C is not in the table, */
/* enter it into the table and */
/* output <w>. Reset <w> to */
/* be the code for C ==> Step 6 */
if (wc == end_list) {
make_table_entry(w , c);
put_code(w);
w = lookup_string(no_prev , c);
} else /* If <w>C is in table, keep looking */
/* for longer strings == Step 5 */
w = wc;
/* Get next input character ==> Step 3 */
get_char(c);
}
/* Make sure last code is */
/* written out ==> Step 4. */
put_code(w);
} /* Do_Compression */
/*--------------------------------------------------------------------------*/
/* PibCompr --- Main program */
/*--------------------------------------------------------------------------*/
int main(int argc, const char *argv[]) {
/* PibCompr */
/* We are doing compression */
pio_initialize(argc, argv);
if_compressing = true;
/* Initialize compression */
initialize();
/* Perform compression */
do_compression();
/* Clean up and exit */
terminate();
return EXIT_SUCCESS;
} /* PibCompr */
} // End of namespace Avalanche.
|