-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaldc.c
450 lines (396 loc) · 14.9 KB
/
aldc.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
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
/***************************************************************************
* ALDC encoding and decoding
*
* File : aldc.c
* Purpose : Use Aldc compress and decompress data files.
* Author : Michael Dipperstein
* Date : November 28, 2014
* Ported to ALDC: Michael Fox
* Date : May 25, 2017
*
****************************************************************************
*
* Aldc: Aldc Encoding/Decoding Routines
* Copyright (C) 2003 - 2007, 2014 by
* Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
* Copyright (C) 2017 by Michael Fox
*
* This file is part of the ALDC library.
*
* The lzss library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* The lzss library 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "lzlocal.h"
#include "bitfile.h"
#if __APPLE__ || __FreeBSD__
#include "memstream.h"
#include "fmemopen.h"
#endif
/***************************************************************************
* TYPE DEFINITIONS
***************************************************************************/
/***************************************************************************
* CONSTANTS
***************************************************************************/
/***************************************************************************
* GLOBAL VARIABLES
***************************************************************************/
/* cyclic buffer sliding window of already read characters */
unsigned char slidingWindow[WINDOW_SIZE];
unsigned char uncodedLookahead[MAX_CODED];
/***************************************************************************
* PROTOTYPES
***************************************************************************/
/***************************************************************************
* FUNCTIONS
***************************************************************************/
typedef struct
{
unsigned int code;
unsigned int bits;
}length_code_t;
length_code_t lengthCode(unsigned int length)
{
if(length < 4) {
length_code_t r;
r.code = length - 2;
r.bits = 2;
return r;
}
else if(length < 8) {
length_code_t r;
r.code = 0x8 | length - 4;
r.bits = 4;
return r;
}
else if(length < 16) {
length_code_t r;
r.code =0x30 | length - 8;
r.bits = 6;
return r;
}
else if(length < 32) {
length_code_t r;
r.code = 0xE0 | length - 16;
r.bits = 8;
return r;
}
else {
length_code_t r;
r.code = 0xF00 | length - 32;
r.bits = 12;
return r;
}
}
/****************************************************************************
* Function : EncodeAldc
* Description: This function will read an input file and write an output
* file encoded according to the Aldc algorithm.
* Parameters : fpIn - pointer to the open binary file to encode
* fpOut - pointer to the open binary file to write encoded
* output
* Effects : fpIn is encoded and written to fpOut. Neither file is
* closed after exit.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
****************************************************************************/
int EncodeAldc(FILE *fpIn, FILE *fpOut)
{
bit_file_t *bfpOut;
encoded_string_t matchData;
int c;
unsigned int i;
unsigned int len; /* length of string */
/* head of sliding window and lookahead */
unsigned int windowHead, uncodedHead;
/* validate arguments */
if ((NULL == fpIn) || (NULL == fpOut))
{
errno = ENOENT;
return -1;
}
/* convert output file to bitfile */
bfpOut = MakeBitFile(fpOut, BF_WRITE);
if (NULL == bfpOut)
{
perror("Making Output File a BitFile");
return -1;
}
windowHead = 0;
uncodedHead = 0;
/************************************************************************
* Fill the sliding window buffer with some known vales. DecodeAldc must
* use the same values. If common characters are used, there's an
* increased chance of matching to the earlier strings.
************************************************************************/
memset(slidingWindow, 0, WINDOW_SIZE * sizeof(unsigned char));
/************************************************************************
* Copy MAX_CODED bytes from the input file into the uncoded lookahead
* buffer.
************************************************************************/
for (len = 0; len < MAX_CODED && (c = getc(fpIn)) != EOF; len++)
{
uncodedLookahead[len] = c;
}
if (0 == len)
{
goto ALDC_END; /* inFile was empty */
}
/* Look for matching string in sliding window */
i = InitializeSearchStructures();
if (0 != i)
{
return i; /* InitializeSearchStructures returned an error */
}
matchData = FindMatch(windowHead, uncodedHead);
int init = 0;
/* now encode the rest of the file until an EOF is read */
while (len > 0)
{
if (matchData.length > len)
{
/* garbage beyond last data happened to extend match length */
matchData.length = len;
}
/* fprintf(stderr, "matchData: offset %d length %d\n", matchData.offset, matchData.length); */
/* XXX Aparently the buffer is not necessarily reset to all 0s so
I guess fill it with literals as a work-around */
if (init < WINDOW_SIZE || matchData.length <= MAX_UNCODED)
{
init++;
/* not long enough match. write uncoded flag and character */
BitFilePutBit(UNCODED, bfpOut);
BitFilePutChar(uncodedLookahead[uncodedHead], bfpOut);
/* fprintf(stderr,"Put literal: %c\n", uncodedLookahead[uncodedHead]); */
matchData.length = 1; /* set to 1 for 1 byte uncoded */
}
else
{
length_code_t length_code = lengthCode(matchData.length);
/* match length > MAX_UNCODED. Encode as offset and length. */
BitFilePutBit(ENCODED, bfpOut);
BitFilePutBits(bfpOut, length_code.code, length_code.bits);
/* fprintf(stderr,"Put length: 0x%x, %d bits\n", length_code.code, length_code.bits); */
/* ALDC calls this displacement - hope it means the same thing */
BitFilePutBits(bfpOut, matchData.offset, OFFSET_BITS);
/* fprintf(stderr,"Put offset: %d\n", matchData.offset); */
}
/********************************************************************
* Replace the matchData.length worth of bytes we've matched in the
* sliding window with new bytes from the input file.
********************************************************************/
i = 0;
char buf[2 * WINDOW_SIZE];
size_t bytes_read;
bytes_read = fread(buf, 1, matchData.length, fpIn);
while (i < bytes_read)
{
char c = buf[i];
/* add old byte into sliding window and new into lookahead */
/* getc optimization may have caused a subtle bug */
/* i = 0; */
/* while ((i < matchData.length) && ((c = getc(fpIn)) != EOF)) */
/* { */
ReplaceChar(windowHead, uncodedLookahead[uncodedHead]);
uncodedLookahead[uncodedHead] = c;
windowHead = Wrap((windowHead + 1), WINDOW_SIZE);
uncodedHead = Wrap((uncodedHead + 1), MAX_CODED);
i++;
}
/* handle case where we hit EOF before filling lookahead */
while (i < matchData.length)
{
ReplaceChar(windowHead, uncodedLookahead[uncodedHead]);
/* nothing to add to lookahead here */
windowHead = Wrap((windowHead + 1), WINDOW_SIZE);
uncodedHead = Wrap((uncodedHead + 1), MAX_CODED);
len--;
i++;
}
/* find match for the remaining characters */
matchData = FindMatch(windowHead, uncodedHead);
}
ALDC_END:
{
/* ALDC end marker */
unsigned int end_marker = 0x1FFF;
/* ALDC end marker */
BitFilePutBits(bfpOut, end_marker, 13);
}
/* we've encoded everything, free bitfile structure */
BitFileToFILE(bfpOut);
return 0;
}
/****************************************************************************
* Function : DecodeAldc
* Description: This function will read an ALDC encoded input file and
* write an output file. This algorithm encodes strings as 16
* bits (a 12 bit offset + a 4 bit length).
* Parameters : fpIn - pointer to the open binary file to decode
* fpOut - pointer to the open binary file to write decoded
* output
* Effects : fpIn is decoded and written to fpOut. Neither file is
* closed after exit.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
****************************************************************************/
int DecodeAldc(FILE *fpIn, FILE *fpOut)
{
bit_file_t *bfpIn;
int c;
unsigned int i, nextChar;
encoded_string_t code; /* offset/length code for string */
/* use stdin if no input file */
if ((NULL == fpIn) || (NULL == fpOut))
{
errno = ENOENT;
return -1;
}
/* convert input file to bitfile */
bfpIn = MakeBitFile(fpIn, BF_READ);
if (NULL == bfpIn)
{
perror("Making Input File a BitFile");
return -1;
}
/************************************************************************
* Fill the sliding window buffer with some known vales. EncodeAldc must
* use the same values. If common characters are used, there's an
* increased chance of matching to the earlier strings.
************************************************************************/
memset(slidingWindow, 0, WINDOW_SIZE * sizeof(unsigned char));
nextChar = 0;
while (1)
{
if ((c = BitFileGetBit(bfpIn)) == EOF)
{
/* we hit the EOF */
break;
}
if (c == UNCODED)
{
/* uncoded character */
if ((c = BitFileGetChar(bfpIn)) == EOF)
{
break;
}
/* write out byte and put it in sliding window */
putc(c, fpOut);
slidingWindow[nextChar] = c;
nextChar = Wrap((nextChar + 1), WINDOW_SIZE);
}
else
{
unsigned int length_bits = 0;
unsigned int prefix = 0;
int bit = 0;
/* offset and length */
code.offset = 0;
code.length = 0;
for(i = 0; i < 4; i++)
{
if ((bit = BitFileGetBit(bfpIn)) == EOF)
{
goto BREAK_OUTER;
}
/* fprintf(stderr, "bit: %d\n", bit); */
if (bit == 1) { prefix++; }
else { goto BREAK_INNER; }
}
BREAK_INNER:
if (prefix == 0) { length_bits = 1; }
else if (prefix == 1) { length_bits = 2; }
else if (prefix == 2) { length_bits = 3; }
else if (prefix == 3) { length_bits = 4; }
else /* if (prefix == 4) */ { length_bits = 8; }
if ((BitFileGetBits(bfpIn, &( code.length ), length_bits
)) == EOF)
{
break;
}
/* fprintf(stderr, "READ code.length: %d\n", code.length); */
if (code.length == 0xFF) { break ; } /* end code 0xFFF */
if (prefix == 0) { code.length += 2; }
else if (prefix == 1) { code.length += 4; }
else if (prefix == 2) { code.length += 8; }
else if (prefix == 3) { code.length += 16; }
else if (prefix == 4) { code.length += 32; }
if ((BitFileGetBits(bfpIn, &( code.offset ), OFFSET_BITS
)) == EOF)
{
break;
}
/* fprintf(stderr, "prefix length_bits code.length code.offset slidingWindow\n"); */
/* fprintf(stderr, "%d %d %d %d %s\n" */
/* , prefix, length_bits, code.length, code.offset, slidingWindow */
/* ); */
/****************************************************************
* Write out decoded string to file and lookahead. It would be
* nice to write to the sliding window instead of the lookahead,
* but we could end up overwriting the matching string with the
* new string if abs(offset - next char) < match length.
****************************************************************/
for (i = 0; i < code.length; i++)
{
c = slidingWindow[Wrap((code.offset + i), WINDOW_SIZE)];
putc(c, fpOut);
slidingWindow[Wrap((nextChar + i), WINDOW_SIZE)] = c;
}
nextChar = Wrap((nextChar + code.length), WINDOW_SIZE);
}
}
BREAK_OUTER:
/* we've decoded everything, free bitfile structure */
BitFileToFILE(bfpIn);
return 0;
}
#include <stdlib.h>
/* Caller frees *sOut */
int EncodeAldcString(char *sIn, size_t inLen, char **sOut, size_t *outLen) {
FILE *fIn, *fOut;
char *memOut;
size_t memOutSz;
fIn = fmemopen(sIn, inLen, "r");
fOut = open_memstream(&memOut, &memOutSz);
EncodeAldc(fIn, fOut);
fclose(fIn);
fclose(fOut);
*outLen = memOutSz;
*sOut = memOut;
/* TODO check errors */
/* TODO free something? */
return 0;
}
/* Caller frees *sOut */
int DecodeAldcString(char *sIn, size_t inLen, char **sOut, size_t *outLen) {
FILE *fIn, *fOut;
char *memOut;
size_t memOutSz;
fIn = fmemopen(sIn, inLen, "r");
fOut = open_memstream(&memOut, &memOutSz);
DecodeAldc(fIn, fOut);
fclose(fIn);
fclose(fOut);
*outLen = memOutSz;
*sOut = memOut;
/* TODO check errors */
/* TODO free something? */
return 0;
}