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