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 F 18 /* upper limit for match_length */␊ |
47 | #define THRESHOLD 2 /* encode string into position and length␊ |
48 | if match_length is greater than this */␊ |
49 | #define NIL N /* index for root of binary search trees */␊ |
50 | ␊ |
51 | struct encode_state {␊ |
52 | /*␊ |
53 | * left & right children & parent. These constitute binary search trees.␊ |
54 | */␊ |
55 | ␉int lchild[N + 1], rchild[N + 257], parent[N + 1];␊ |
56 | ␊ |
57 | /* ring buffer of size N, with extra F-1 bytes to aid string comparison */␊ |
58 | ␉u_int8_t text_buf[N + F - 1];␊ |
59 | ␊ |
60 | /*␊ |
61 | * match_length of longest match.␊ |
62 | * These are set by the insert_node() procedure.␊ |
63 | */␊ |
64 | ␉int match_position, match_length;␊ |
65 | };␊ |
66 | ␊ |
67 | int␊ |
68 | decompress_lzss(␊ |
69 | u_int8_t * dst,␊ |
70 | u_int8_t * src,␊ |
71 | u_int32_t srclen)␊ |
72 | {␊ |
73 | /* ring buffer of size N, with extra F-1 bytes to aid string comparison */␊ |
74 | u_int8_t text_buf[N + F - 1];␊ |
75 | u_int8_t *dststart = dst;␊ |
76 | u_int8_t *srcend = src + srclen;␊ |
77 | int i, j, k, r, c;␊ |
78 | unsigned int flags;␊ |
79 | ␊ |
80 | dst = dststart;␊ |
81 | srcend = src + srclen;␊ |
82 | for (i = 0; i < N - F; i++)␊ |
83 | text_buf[i] = ' ';␊ |
84 | r = N - F;␊ |
85 | flags = 0;␊ |
86 | for ( ; ; ) {␊ |
87 | if (((flags >>= 1) & 0x100) == 0) {␊ |
88 | if (src < srcend) c = *src++; else break;␊ |
89 | flags = c | 0xFF00; /* uses higher byte cleverly */␊ |
90 | } /* to count eight */␊ |
91 | if (flags & 1) {␊ |
92 | if (src < srcend) c = *src++; else break;␊ |
93 | *dst++ = c;␊ |
94 | text_buf[r++] = c;␊ |
95 | r &= (N - 1);␊ |
96 | } else {␊ |
97 | if (src < srcend) i = *src++; else break;␊ |
98 | if (src < srcend) j = *src++; else break;␊ |
99 | i |= ((j & 0xF0) << 4);␊ |
100 | j = (j & 0x0F) + THRESHOLD;␊ |
101 | for (k = 0; k <= j; k++) {␊ |
102 | c = text_buf[(i + k) & (N - 1)];␊ |
103 | *dst++ = c;␊ |
104 | text_buf[r++] = c;␊ |
105 | r &= (N - 1);␊ |
106 | }␊ |
107 | }␊ |
108 | }␊ |
109 | ␊ |
110 | return dst - dststart;␊ |
111 | }␊ |
112 | ␊ |
113 | /*␊ |
114 | * initialize state, mostly the trees␊ |
115 | *␊ |
116 | * For i = 0 to N - 1, rchild[i] and lchild[i] will be the right and left␊ |
117 | * children of node i. These nodes need not be initialized. Also, parent[i]␊ |
118 | * is the parent of node i. These are initialized to NIL (= N), which stands␊ |
119 | * for 'not used.' For i = 0 to 255, rchild[N + i + 1] is the root of the␊ |
120 | * tree for strings that begin with character i. These are initialized to NIL.␊ |
121 | * Note there are 256 trees. */␊ |
122 | static void init_state(struct encode_state *sp)␊ |
123 | {␊ |
124 | int i;␊ |
125 | ␊ |
126 | bzero(sp, sizeof(*sp));␊ |
127 | ␊ |
128 | for (i = 0; i < N - F; i++)␊ |
129 | sp->text_buf[i] = ' ';␊ |
130 | for (i = N + 1; i <= N + 256; i++)␊ |
131 | sp->rchild[i] = NIL;␊ |
132 | for (i = 0; i < N; i++)␊ |
133 | sp->parent[i] = NIL;␊ |
134 | }␊ |
135 | ␊ |
136 | /*␊ |
137 | * Inserts string of length F, text_buf[r..r+F-1], into one of the trees␊ |
138 | * (text_buf[r]'th tree) and returns the longest-match position and length␊ |
139 | * via the global variables match_position and match_length.␊ |
140 | * If match_length = F, then removes the old node in favor of the new one,␊ |
141 | * because the old one will be deleted sooner. Note r plays double role,␊ |
142 | * as tree node and position in buffer.␊ |
143 | */␊ |
144 | static void insert_node(struct encode_state *sp, int r)␊ |
145 | {␊ |
146 | int i, p, cmp;␊ |
147 | u_int8_t *key;␊ |
148 | ␊ |
149 | cmp = 1;␊ |
150 | key = &sp->text_buf[r];␊ |
151 | p = N + 1 + key[0];␊ |
152 | sp->rchild[r] = sp->lchild[r] = NIL;␊ |
153 | sp->match_length = 0;␊ |
154 | for ( ; ; ) {␊ |
155 | if (cmp >= 0) {␊ |
156 | if (sp->rchild[p] != NIL)␊ |
157 | p = sp->rchild[p];␊ |
158 | else {␊ |
159 | sp->rchild[p] = r;␊ |
160 | sp->parent[r] = p;␊ |
161 | return;␊ |
162 | }␊ |
163 | } else {␊ |
164 | if (sp->lchild[p] != NIL)␊ |
165 | p = sp->lchild[p];␊ |
166 | else {␊ |
167 | sp->lchild[p] = r;␊ |
168 | sp->parent[r] = p;␊ |
169 | return;␊ |
170 | }␊ |
171 | }␊ |
172 | for (i = 1; i < F; i++) {␊ |
173 | if ((cmp = key[i] - sp->text_buf[p + i]) != 0)␊ |
174 | break;␊ |
175 | }␊ |
176 | if (i > sp->match_length) {␊ |
177 | sp->match_position = p;␊ |
178 | if ((sp->match_length = i) >= F)␊ |
179 | break;␊ |
180 | }␊ |
181 | }␊ |
182 | sp->parent[r] = sp->parent[p];␊ |
183 | sp->lchild[r] = sp->lchild[p];␊ |
184 | sp->rchild[r] = sp->rchild[p];␊ |
185 | sp->parent[sp->lchild[p]] = r;␊ |
186 | sp->parent[sp->rchild[p]] = r;␊ |
187 | if (sp->rchild[sp->parent[p]] == p)␊ |
188 | sp->rchild[sp->parent[p]] = r;␊ |
189 | else␊ |
190 | sp->lchild[sp->parent[p]] = r;␊ |
191 | sp->parent[p] = NIL; /* remove p */␊ |
192 | }␊ |
193 | ␊ |
194 | /* deletes node p from tree */␊ |
195 | static void delete_node(struct encode_state *sp, int p)␊ |
196 | {␊ |
197 | int q;␊ |
198 | ␊ |
199 | if (sp->parent[p] == NIL)␊ |
200 | return; /* not in tree */␊ |
201 | if (sp->rchild[p] == NIL)␊ |
202 | q = sp->lchild[p];␊ |
203 | else if (sp->lchild[p] == NIL)␊ |
204 | q = sp->rchild[p];␊ |
205 | else {␊ |
206 | q = sp->lchild[p];␊ |
207 | if (sp->rchild[q] != NIL) {␊ |
208 | do {␊ |
209 | q = sp->rchild[q];␊ |
210 | } while (sp->rchild[q] != NIL);␊ |
211 | sp->rchild[sp->parent[q]] = sp->lchild[q];␊ |
212 | sp->parent[sp->lchild[q]] = sp->parent[q];␊ |
213 | sp->lchild[q] = sp->lchild[p];␊ |
214 | sp->parent[sp->lchild[p]] = q;␊ |
215 | }␊ |
216 | sp->rchild[q] = sp->rchild[p];␊ |
217 | sp->parent[sp->rchild[p]] = q;␊ |
218 | }␊ |
219 | sp->parent[q] = sp->parent[p];␊ |
220 | if (sp->rchild[sp->parent[p]] == p)␊ |
221 | sp->rchild[sp->parent[p]] = q;␊ |
222 | else␊ |
223 | sp->lchild[sp->parent[p]] = q;␊ |
224 | sp->parent[p] = NIL;␊ |
225 | }␊ |
226 | ␊ |
227 | u_int8_t *compress_lzss(␊ |
228 | u_int8_t *dst,␊ |
229 | u_int32_t dstlen,␊ |
230 | u_int8_t *src,␊ |
231 | u_int32_t srcLen)␊ |
232 | {␊ |
233 | /* Encoding state, mostly tree but some current match stuff */␊ |
234 | struct encode_state *sp;␊ |
235 | ␊ |
236 | int i, c, len, r, s, last_match_length, code_buf_ptr;␊ |
237 | u_int8_t code_buf[17], mask;␊ |
238 | u_int8_t *srcend = src + srcLen;␊ |
239 | u_int8_t *dstend = dst + dstlen;␊ |
240 | ␊ |
241 | /* initialize trees */␊ |
242 | sp = (struct encode_state *) malloc(sizeof(*sp));␊ |
243 | init_state(sp);␊ |
244 | ␊ |
245 | /*␊ |
246 | * code_buf[1..16] saves eight units of code, and code_buf[0] works␊ |
247 | * as eight flags, "1" representing that the unit is an unencoded␊ |
248 | * letter (1 byte), "0" a position-and-length pair (2 bytes).␊ |
249 | * Thus, eight units require at most 16 bytes of code.␊ |
250 | */␊ |
251 | code_buf[0] = 0;␊ |
252 | code_buf_ptr = mask = 1;␊ |
253 | ␊ |
254 | /* Clear the buffer with any character that will appear often. */␊ |
255 | s = 0; r = N - F;␊ |
256 | ␊ |
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 | return (void *) 0; /* text of size zero */␊ |
262 | ␊ |
263 | /*␊ |
264 | * Insert the F strings, each of which begins with one or more␊ |
265 | * 'space' characters. Note the order in which these strings are␊ |
266 | * inserted. This way, degenerate trees will be less likely to occur.␊ |
267 | */␊ |
268 | for (i = 1; i <= F; i++)␊ |
269 | insert_node(sp, r - i);␊ |
270 | ␊ |
271 | /*␊ |
272 | * Finally, insert the whole string just read.␊ |
273 | * The global variables match_length and match_position are set.␊ |
274 | */␊ |
275 | insert_node(sp, r);␊ |
276 | do {␊ |
277 | /* match_length may be spuriously long near the end of text. */␊ |
278 | if (sp->match_length > len)␊ |
279 | sp->match_length = len;␊ |
280 | if (sp->match_length <= THRESHOLD) {␊ |
281 | sp->match_length = 1; /* Not long enough match. Send one byte. */␊ |
282 | code_buf[0] |= mask; /* 'send one byte' flag */␊ |
283 | code_buf[code_buf_ptr++] = sp->text_buf[r]; /* Send uncoded. */␊ |
284 | } else {␊ |
285 | /* Send position and length pair. Note match_length > THRESHOLD. */␊ |
286 | code_buf[code_buf_ptr++] = (u_int8_t) sp->match_position;␊ |
287 | code_buf[code_buf_ptr++] = (u_int8_t)␊ |
288 | ( ((sp->match_position >> 4) & 0xF0)␊ |
289 | | (sp->match_length - (THRESHOLD + 1)) );␊ |
290 | }␊ |
291 | if ((mask <<= 1) == 0) { /* Shift mask left one bit. */␊ |
292 | /* Send at most 8 units of code together */␊ |
293 | for (i = 0; i < code_buf_ptr; i++)␊ |
294 | if (dst < dstend)␊ |
295 | *dst++ = code_buf[i];␊ |
296 | else␊ |
297 | return (void *) 0;␊ |
298 | code_buf[0] = 0;␊ |
299 | code_buf_ptr = mask = 1;␊ |
300 | }␊ |
301 | last_match_length = sp->match_length;␊ |
302 | for (i = 0; i < last_match_length && src < srcend; i++) {␊ |
303 | delete_node(sp, s); /* Delete old strings and */␊ |
304 | c = *src++;␊ |
305 | sp->text_buf[s] = c; /* read new bytes */␊ |
306 | ␊ |
307 | /*␊ |
308 | * If the position is near the end of buffer, extend the buffer␊ |
309 | * to make string comparison easier.␊ |
310 | */␊ |
311 | if (s < F - 1)␊ |
312 | sp->text_buf[s + N] = c;␊ |
313 | ␊ |
314 | /* Since this is a ring buffer, increment the position modulo N. */␊ |
315 | s = (s + 1) & (N - 1);␊ |
316 | r = (r + 1) & (N - 1);␊ |
317 | ␊ |
318 | /* Register the string in text_buf[r..r+F-1] */␊ |
319 | insert_node(sp, r);␊ |
320 | }␊ |
321 | while (i++ < last_match_length) {␊ |
322 | delete_node(sp, s);␊ |
323 | ␊ |
324 | /* After the end of text, no need to read, */␊ |
325 | s = (s + 1) & (N - 1);␊ |
326 | r = (r + 1) & (N - 1);␊ |
327 | /* but buffer may not be empty. */␊ |
328 | if (--len)␊ |
329 | insert_node(sp, r);␊ |
330 | }␊ |
331 | } while (len > 0); /* until length of string to be processed is zero */␊ |
332 | ␊ |
333 | if (code_buf_ptr > 1) { /* Send remaining code. */␊ |
334 | for (i = 0; i < code_buf_ptr; i++)␊ |
335 | if (dst < dstend)␊ |
336 | *dst++ = code_buf[i];␊ |
337 | else␊ |
338 | return (void *) 0;␊ |
339 | }␊ |
340 | ␊ |
341 | return dst;␊ |
342 | }␊ |
343 | |