1 | /*␊ |
2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /**************************************************************␊ |
25 | LZSS.C -- A Data Compression Program␊ |
26 | ***************************************************************␊ |
27 | 4/6/1989 Haruhiko Okumura␊ |
28 | Use, distribute, and modify this program freely.␊ |
29 | Please send me your improved versions.␊ |
30 | PC-VAN SCIENCE␊ |
31 | NIFTY-Serve PAF01022␊ |
32 | CompuServe 74050,1022␊ |
33 | ␊ |
34 | **************************************************************/␊ |
35 | /*␊ |
36 | * lzss.c - Package for decompressing lzss compressed objects␊ |
37 | *␊ |
38 | * Copyright (c) 2003 Apple Computer, Inc.␊ |
39 | *␊ |
40 | * DRI: Josh de Cesare␊ |
41 | */␊ |
42 | ␊ |
43 | #include <sl.h>␊ |
44 | ␊ |
45 | #define N␉␉4096␉/* Size of ring buffer - must be power of 2. */␊ |
46 | #define N_MIN_1␉␉4095␊ |
47 | #define F␉␉18␉/* Upper limit for match_length. */␊ |
48 | #define R␉␉N - F␊ |
49 | #define THRESHOLD␉2␉/* Encode string into position and length if match_length is greater than this. */␊ |
50 | #define NIL␉␉N␉/* Index for root of binary search trees. */␊ |
51 | ␊ |
52 | struct encode_state {␊ |
53 | /*␊ |
54 | * left & right children & parent. These constitute binary search trees.␊ |
55 | */␊ |
56 | ␉int lchild[N + 1], rchild[N + 257], parent[N + 1];␊ |
57 | ␊ |
58 | /* ring buffer of size N, with extra F-1 bytes to aid string comparison */␊ |
59 | ␉u_int8_t text_buf[N + F - 1];␊ |
60 | ␊ |
61 | /*␊ |
62 | * match_length of longest match.␊ |
63 | * These are set by the insert_node() procedure.␊ |
64 | */␊ |
65 | ␉int match_position, match_length;␊ |
66 | };␊ |
67 | ␊ |
68 | //==============================================================================␊ |
69 | // Refactoring and bug fix Copyright (c) 2010 by DHP.␊ |
70 | ␊ |
71 | int decompress_lzss(u_int8_t * dst, u_int8_t * src, u_int32_t srclen)␊ |
72 | {␊ |
73 | ␉/* Four KB ring buffer with 17 extra bytes added to aid string comparisons. */␊ |
74 | ␉u_int8_t text_buf[N_MIN_1 + F];␊ |
75 | ␉u_int8_t * dststart = dst;␊ |
76 | ␉const u_int8_t * srcend = (src + srclen);␊ |
77 | ␊ |
78 | ␉int r = R;␊ |
79 | ␉int i, j, k, c;␊ |
80 | ␉unsigned int flags = 0;␊ |
81 | ␊ |
82 | ␉for (i = 0; i < R; i++) {␊ |
83 | ␉␉text_buf[i] = ' ';␊ |
84 | ␉}␊ |
85 | ␊ |
86 | ␉while (src < srcend) {␊ |
87 | ␉␉if (((flags >>= 1) & 0x100) == 0) {␊ |
88 | ␉␉␉c = *src++;␊ |
89 | ␉␉␉flags = c | 0xFF00; // Clever use of the high byte.␊ |
90 | ␉}␊ |
91 | ␊ |
92 | ␉if ((src < srcend) && (flags & 1)) {␊ |
93 | ␉␉␉c = *src++;␊ |
94 | ␉␉␉*dst++ = c;␊ |
95 | ␉␉␉text_buf[r++] = c;␊ |
96 | ␉␉␉r &= N_MIN_1;␊ |
97 | ␉␉} else if ((src + 2) <= srcend) {␊ |
98 | ␉␉␉i = *src++;␊ |
99 | ␉␉␉j = *src++;␊ |
100 | ␊ |
101 | ␉␉␉i |= ((j & 0xF0) << 4);␊ |
102 | ␉␉␉j = (j & 0x0F) + THRESHOLD;␊ |
103 | ␊ |
104 | ␉␉␉for (k = 0; k <= j; k++) {␊ |
105 | ␉␉␉␉c = text_buf[(i + k) & N_MIN_1];␊ |
106 | ␉␉␉␉*dst++ = c;␊ |
107 | ␉␉␉␉text_buf[r++] = c;␊ |
108 | ␉␉␉␉r &= N_MIN_1;␊ |
109 | ␉␉␉}␊ |
110 | ␉␉}␊ |
111 | ␉}␊ |
112 | ␊ |
113 | ␉return dst - dststart;␊ |
114 | }␊ |
115 | ␊ |
116 | /*␊ |
117 | * initialize state, mostly the trees␊ |
118 | *␊ |
119 | * For i = 0 to N - 1, rchild[i] and lchild[i] will be the right and left␊ |
120 | * children of node i. These nodes need not be initialized. Also, parent[i]␊ |
121 | * is the parent of node i. These are initialized to NIL (= N), which stands␊ |
122 | * for 'not used.' For i = 0 to 255, rchild[N + i + 1] is the root of the␊ |
123 | * tree for strings that begin with character i. These are initialized to NIL.␊ |
124 | * Note there are 256 trees. */␊ |
125 | static void init_state(struct encode_state *sp)␊ |
126 | {␊ |
127 | int i;␊ |
128 | ␊ |
129 | bzero(sp, sizeof(*sp));␊ |
130 | ␊ |
131 | for (i = 0; i < N - F; i++)␊ |
132 | sp->text_buf[i] = ' ';␊ |
133 | for (i = N + 1; i <= N + 256; i++)␊ |
134 | sp->rchild[i] = NIL;␊ |
135 | for (i = 0; i < N; i++)␊ |
136 | sp->parent[i] = NIL;␊ |
137 | }␊ |
138 | ␊ |
139 | /*␊ |
140 | * Inserts string of length F, text_buf[r..r+F-1], into one of the trees␊ |
141 | * (text_buf[r]'th tree) and returns the longest-match position and length␊ |
142 | * via the global variables match_position and match_length.␊ |
143 | * If match_length = F, then removes the old node in favor of the new one,␊ |
144 | * because the old one will be deleted sooner. Note r plays double role,␊ |
145 | * as tree node and position in buffer.␊ |
146 | */␊ |
147 | static void insert_node(struct encode_state *sp, int r)␊ |
148 | {␊ |
149 | int i, p, cmp;␊ |
150 | u_int8_t *key;␊ |
151 | ␊ |
152 | cmp = 1;␊ |
153 | key = &sp->text_buf[r];␊ |
154 | p = N + 1 + key[0];␊ |
155 | sp->rchild[r] = sp->lchild[r] = NIL;␊ |
156 | sp->match_length = 0;␊ |
157 | for ( ; ; ) {␊ |
158 | if (cmp >= 0) {␊ |
159 | if (sp->rchild[p] != NIL)␊ |
160 | p = sp->rchild[p];␊ |
161 | else {␊ |
162 | sp->rchild[p] = r;␊ |
163 | sp->parent[r] = p;␊ |
164 | return;␊ |
165 | }␊ |
166 | } else {␊ |
167 | if (sp->lchild[p] != NIL)␊ |
168 | p = sp->lchild[p];␊ |
169 | else {␊ |
170 | sp->lchild[p] = r;␊ |
171 | sp->parent[r] = p;␊ |
172 | return;␊ |
173 | }␊ |
174 | }␊ |
175 | for (i = 1; i < F; i++) {␊ |
176 | if ((cmp = key[i] - sp->text_buf[p + i]) != 0)␊ |
177 | break;␊ |
178 | }␊ |
179 | if (i > sp->match_length) {␊ |
180 | sp->match_position = p;␊ |
181 | if ((sp->match_length = i) >= F)␊ |
182 | break;␊ |
183 | }␊ |
184 | }␊ |
185 | sp->parent[r] = sp->parent[p];␊ |
186 | sp->lchild[r] = sp->lchild[p];␊ |
187 | sp->rchild[r] = sp->rchild[p];␊ |
188 | sp->parent[sp->lchild[p]] = r;␊ |
189 | sp->parent[sp->rchild[p]] = r;␊ |
190 | if (sp->rchild[sp->parent[p]] == p)␊ |
191 | sp->rchild[sp->parent[p]] = r;␊ |
192 | else␊ |
193 | sp->lchild[sp->parent[p]] = r;␊ |
194 | sp->parent[p] = NIL; /* remove p */␊ |
195 | }␊ |
196 | ␊ |
197 | /* deletes node p from tree */␊ |
198 | static void delete_node(struct encode_state *sp, int p)␊ |
199 | {␊ |
200 | int q;␊ |
201 | ␊ |
202 | if (sp->parent[p] == NIL)␊ |
203 | return; /* not in tree */␊ |
204 | if (sp->rchild[p] == NIL)␊ |
205 | q = sp->lchild[p];␊ |
206 | else if (sp->lchild[p] == NIL)␊ |
207 | q = sp->rchild[p];␊ |
208 | else {␊ |
209 | q = sp->lchild[p];␊ |
210 | if (sp->rchild[q] != NIL) {␊ |
211 | do {␊ |
212 | q = sp->rchild[q];␊ |
213 | } while (sp->rchild[q] != NIL);␊ |
214 | sp->rchild[sp->parent[q]] = sp->lchild[q];␊ |
215 | sp->parent[sp->lchild[q]] = sp->parent[q];␊ |
216 | sp->lchild[q] = sp->lchild[p];␊ |
217 | sp->parent[sp->lchild[p]] = q;␊ |
218 | }␊ |
219 | sp->rchild[q] = sp->rchild[p];␊ |
220 | sp->parent[sp->rchild[p]] = q;␊ |
221 | }␊ |
222 | sp->parent[q] = sp->parent[p];␊ |
223 | if (sp->rchild[sp->parent[p]] == p)␊ |
224 | sp->rchild[sp->parent[p]] = q;␊ |
225 | else␊ |
226 | sp->lchild[sp->parent[p]] = q;␊ |
227 | sp->parent[p] = NIL;␊ |
228 | }␊ |
229 | ␊ |
230 | u_int8_t *compress_lzss(␊ |
231 | u_int8_t * dst,␊ |
232 | u_int32_t dstlen,␊ |
233 | u_int8_t * src,␊ |
234 | u_int32_t srclen)␊ |
235 | {␊ |
236 | u_int8_t * result = NULL;␊ |
237 | /* Encoding state, mostly tree but some current match stuff */␊ |
238 | struct encode_state *sp;␊ |
239 | int i, c, len, r, s, last_match_length, code_buf_ptr;␊ |
240 | u_int8_t code_buf[17], mask;␊ |
241 | u_int8_t * srcend = src + srclen;␊ |
242 | u_int8_t *dstend = dst + dstlen;␊ |
243 | /* initialize trees */␊ |
244 | sp = (struct encode_state *) malloc(sizeof(*sp));␊ |
245 | if (!sp) goto finish;␊ |
246 | init_state(sp);␊ |
247 | /*␊ |
248 | * code_buf[1..16] saves eight units of code, and code_buf[0] works␊ |
249 | * as eight flags, "1" representing that the unit is an unencoded␊ |
250 | * letter (1 byte), "0" a position-and-length pair (2 bytes).␊ |
251 | * Thus, eight units require at most 16 bytes of code.␊ |
252 | */␊ |
253 | code_buf[0] = 0;␊ |
254 | code_buf_ptr = mask = 1;␊ |
255 | /* Clear the buffer with any character that will appear often. */␊ |
256 | s = 0; r = N - F;␊ |
257 | /* Read F bytes into the last F bytes of the buffer */␊ |
258 | for (len = 0; len < F && src < srcend; len++)␊ |
259 | sp->text_buf[r + len] = *src++;␊ |
260 | if (!len)␊ |
261 | goto finish;␊ |
262 | /*␊ |
263 | * Insert the F strings, each of which begins with one or more␊ |
264 | * 'space' characters. Note the order in which these strings are␊ |
265 | * inserted. This way, degenerate trees will be less likely to occur.␊ |
266 | */␊ |
267 | for (i = 1; i <= F; i++)␊ |
268 | insert_node(sp, r - i);␊ |
269 | /*␊ |
270 | * Finally, insert the whole string just read.␊ |
271 | * The global variables match_length and match_position are set.␊ |
272 | */␊ |
273 | insert_node(sp, r);␊ |
274 | do {␊ |
275 | /* match_length may be spuriously long near the end of text. */␊ |
276 | if (sp->match_length > len)␊ |
277 | sp->match_length = len;␊ |
278 | if (sp->match_length <= THRESHOLD) {␊ |
279 | sp->match_length = 1; /* Not long enough match. Send one byte. */␊ |
280 | code_buf[0] |= mask; /* 'send one byte' flag */␊ |
281 | code_buf[code_buf_ptr++] = sp->text_buf[r]; /* Send uncoded. */␊ |
282 | } else {␊ |
283 | /* Send position and length pair. Note match_length > THRESHOLD. */␊ |
284 | code_buf[code_buf_ptr++] = (u_int8_t) sp->match_position;␊ |
285 | code_buf[code_buf_ptr++] = (u_int8_t)␊ |
286 | ( ((sp->match_position >> 4) & 0xF0)␊ |
287 | | (sp->match_length - (THRESHOLD + 1)) );␊ |
288 | }␊ |
289 | if ((mask <<= 1) == 0) { /* Shift mask left one bit. */␊ |
290 | /* Send at most 8 units of code together */␊ |
291 | for (i = 0; i < code_buf_ptr; i++)␊ |
292 | if (dst < dstend)␊ |
293 | *dst++ = code_buf[i];␊ |
294 | else␊ |
295 | goto finish;␊ |
296 | code_buf[0] = 0;␊ |
297 | code_buf_ptr = mask = 1;␊ |
298 | }␊ |
299 | last_match_length = sp->match_length;␊ |
300 | for (i = 0; i < last_match_length && src < srcend; i++) {␊ |
301 | delete_node(sp, s); /* Delete old strings and */␊ |
302 | c = *src++;␊ |
303 | sp->text_buf[s] = c; /* read new bytes */␊ |
304 | /*␊ |
305 | * If the position is near the end of buffer, extend the buffer␊ |
306 | * to make string comparison easier.␊ |
307 | */␊ |
308 | if (s < F - 1)␊ |
309 | sp->text_buf[s + N] = c;␊ |
310 | /* Since this is a ring buffer, increment the position modulo N. */␊ |
311 | s = (s + 1) & (N - 1);␊ |
312 | r = (r + 1) & (N - 1);␊ |
313 | /* Register the string in text_buf[r..r+F-1] */␊ |
314 | insert_node(sp, r);␊ |
315 | }␊ |
316 | while (i++ < last_match_length) {␊ |
317 | delete_node(sp, s);␊ |
318 | /* After the end of text, no need to read, */␊ |
319 | s = (s + 1) & (N - 1);␊ |
320 | r = (r + 1) & (N - 1);␊ |
321 | /* but buffer may not be empty. */␊ |
322 | if (--len)␊ |
323 | insert_node(sp, r);␊ |
324 | }␊ |
325 | } while (len > 0); /* until length of string to be processed is zero */␊ |
326 | if (code_buf_ptr > 1) { /* Send remaining code. */␊ |
327 | for (i = 0; i < code_buf_ptr; i++)␊ |
328 | if (dst < dstend)␊ |
329 | *dst++ = code_buf[i];␊ |
330 | else␊ |
331 | goto finish;␊ |
332 | }␊ |
333 | result = dst;␊ |
334 | finish:␊ |
335 | if (sp) free(sp);␊ |
336 | return result;␊ |
337 | }␊ |
338 | |