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