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