2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 #include <libsa/mkext.h>
25 #include <libsa/stdlib.h>
27 #include <Kernel/libsa/mkext.h>
32 __private_extern__ u_int32_t
33 adler32(u_int8_t
*buffer
, int32_t length
)
36 u_int32_t result
, lowHalf
, highHalf
;
41 for (cnt
= 0; cnt
< length
; cnt
++) {
42 if ((cnt
% 5000) == 0) {
47 lowHalf
+= buffer
[cnt
];
54 result
= (highHalf
<< 16) | lowHalf
;
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.
69 **************************************************************/
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 */
79 * left & right children & parent. These constitute binary search trees.
81 int lchild
[N
+ 1], rchild
[N
+ 257], parent
[N
+ 1];
83 /* ring buffer of size N, with extra F-1 bytes to aid string comparison */
84 u_int8_t text_buf
[N
+ F
- 1];
87 * match_length of longest match.
88 * These are set by the insert_node() procedure.
90 int match_position
, match_length
;
94 __private_extern__
int
95 decompress_lzss(u_int8_t
*dst
, u_int8_t
*src
, u_int32_t srclen
)
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
;
105 srcend
= src
+ srclen
;
106 for (i
= 0; i
< N
- F
; i
++)
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 */
116 if (src
< srcend
) c
= *src
++; else break;
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)];
134 return dst
- dststart
;
140 * initialize state, mostly the trees
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. */
148 static void init_state(struct encode_state
*sp
)
152 bzero(sp
, sizeof(*sp
));
154 for (i
= 0; i
< N
- F
; i
++)
155 sp
->text_buf
[i
] = ' ';
156 for (i
= N
+ 1; i
<= N
+ 256; i
++)
158 for (i
= 0; i
< N
; i
++)
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.
170 static void insert_node(struct encode_state
*sp
, int r
)
176 key
= &sp
->text_buf
[r
];
178 sp
->rchild
[r
] = sp
->lchild
[r
] = NIL
;
179 sp
->match_length
= 0;
182 if (sp
->rchild
[p
] != NIL
)
190 if (sp
->lchild
[p
] != NIL
)
198 for (i
= 1; i
< F
; i
++) {
199 if ((cmp
= key
[i
] - sp
->text_buf
[p
+ i
]) != 0)
202 if (i
> sp
->match_length
) {
203 sp
->match_position
= p
;
204 if ((sp
->match_length
= i
) >= F
)
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
;
216 sp
->lchild
[sp
->parent
[p
]] = r
;
217 sp
->parent
[p
] = NIL
; /* remove p */
220 /* deletes node p from tree */
221 static void delete_node(struct encode_state
*sp
, int p
)
225 if (sp
->parent
[p
] == NIL
)
226 return; /* not in tree */
227 if (sp
->rchild
[p
] == NIL
)
229 else if (sp
->lchild
[p
] == NIL
)
233 if (sp
->rchild
[q
] != NIL
) {
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
;
242 sp
->rchild
[q
] = sp
->rchild
[p
];
243 sp
->parent
[sp
->rchild
[p
]] = q
;
245 sp
->parent
[q
] = sp
->parent
[p
];
246 if (sp
->rchild
[sp
->parent
[p
]] == p
)
247 sp
->rchild
[sp
->parent
[p
]] = q
;
249 sp
->lchild
[sp
->parent
[p
]] = q
;
253 __private_extern__ u_int8_t
*
254 compress_lzss(u_int8_t
*dst
, u_int32_t dstlen
, u_int8_t
*src
, u_int32_t srcLen
)
256 /* Encoding state, mostly tree but some current match stuff */
257 struct encode_state
*sp
;
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
;
264 /* initialize trees */
265 sp
= (struct encode_state
*) malloc(sizeof(*sp
));
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.
275 code_buf_ptr
= mask
= 1;
277 /* Clear the buffer with any character that will appear often. */
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
++;
284 return (void *) 0; /* text of size zero */
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.
291 for (i
= 1; i
<= F
; i
++)
292 insert_node(sp
, r
- i
);
295 * Finally, insert the whole string just read.
296 * The global variables match_length and match_position are set.
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. */
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)) );
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
++)
318 *dst
++ = code_buf
[i
];
322 code_buf_ptr
= mask
= 1;
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 */
328 sp
->text_buf
[s
] = c
; /* read new bytes */
331 * If the position is near the end of buffer, extend the buffer
332 * to make string comparison easier.
335 sp
->text_buf
[s
+ N
] = c
;
337 /* Since this is a ring buffer, increment the position modulo N. */
338 s
= (s
+ 1) & (N
- 1);
339 r
= (r
+ 1) & (N
- 1);
341 /* Register the string in text_buf[r..r+F-1] */
344 while (i
++ < last_match_length
) {
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. */
354 } while (len
> 0); /* until length of string to be processed is zero */
356 if (code_buf_ptr
> 1) { /* Send remaining code. */
357 for (i
= 0; i
< code_buf_ptr
; i
++)
359 *dst
++ = code_buf
[i
];