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