2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 #include <libsa/mkext.h>
33 #include <libsa/stdlib.h>
35 #include <Kernel/libsa/mkext.h>
39 #define BASE 65521L /* largest prime smaller than 65536 */
41 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
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);
49 __private_extern__ u_int32_t
50 adler32(uint8_t *buf
, int32_t len
)
52 unsigned long s1
= 1; // adler & 0xffff;
53 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
57 k
= len
< NMAX
? len
: NMAX
;
71 return (s2
<< 16) | s1
;
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.
85 **************************************************************/
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 */
95 * left & right children & parent. These constitute binary search trees.
97 int lchild
[N
+ 1], rchild
[N
+ 257], parent
[N
+ 1];
99 /* ring buffer of size N, with extra F-1 bytes to aid string comparison */
100 u_int8_t text_buf
[N
+ F
- 1];
103 * match_length of longest match.
104 * These are set by the insert_node() procedure.
106 int match_position
, match_length
;
110 __private_extern__
int
111 decompress_lzss(u_int8_t
*dst
, u_int8_t
*src
, u_int32_t srclen
)
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
;
121 srcend
= src
+ srclen
;
122 for (i
= 0; i
< N
- F
; i
++)
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 */
132 if (src
< srcend
) c
= *src
++; else break;
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)];
150 return dst
- dststart
;
156 * initialize state, mostly the trees
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. */
164 static void init_state(struct encode_state
*sp
)
168 bzero(sp
, sizeof(*sp
));
170 for (i
= 0; i
< N
- F
; i
++)
171 sp
->text_buf
[i
] = ' ';
172 for (i
= N
+ 1; i
<= N
+ 256; i
++)
174 for (i
= 0; i
< N
; i
++)
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.
186 static void insert_node(struct encode_state
*sp
, int r
)
192 key
= &sp
->text_buf
[r
];
194 sp
->rchild
[r
] = sp
->lchild
[r
] = NIL
;
195 sp
->match_length
= 0;
198 if (sp
->rchild
[p
] != NIL
)
206 if (sp
->lchild
[p
] != NIL
)
214 for (i
= 1; i
< F
; i
++) {
215 if ((cmp
= key
[i
] - sp
->text_buf
[p
+ i
]) != 0)
218 if (i
> sp
->match_length
) {
219 sp
->match_position
= p
;
220 if ((sp
->match_length
= i
) >= F
)
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
;
232 sp
->lchild
[sp
->parent
[p
]] = r
;
233 sp
->parent
[p
] = NIL
; /* remove p */
236 /* deletes node p from tree */
237 static void delete_node(struct encode_state
*sp
, int p
)
241 if (sp
->parent
[p
] == NIL
)
242 return; /* not in tree */
243 if (sp
->rchild
[p
] == NIL
)
245 else if (sp
->lchild
[p
] == NIL
)
249 if (sp
->rchild
[q
] != NIL
) {
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
;
258 sp
->rchild
[q
] = sp
->rchild
[p
];
259 sp
->parent
[sp
->rchild
[p
]] = q
;
261 sp
->parent
[q
] = sp
->parent
[p
];
262 if (sp
->rchild
[sp
->parent
[p
]] == p
)
263 sp
->rchild
[sp
->parent
[p
]] = q
;
265 sp
->lchild
[sp
->parent
[p
]] = q
;
269 __private_extern__ u_int8_t
*
270 compress_lzss(u_int8_t
*dst
, u_int32_t dstlen
, u_int8_t
*src
, u_int32_t srcLen
)
272 /* Encoding state, mostly tree but some current match stuff */
273 struct encode_state
*sp
;
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
;
280 /* initialize trees */
281 sp
= (struct encode_state
*) malloc(sizeof(*sp
));
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.
291 code_buf_ptr
= mask
= 1;
293 /* Clear the buffer with any character that will appear often. */
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
++;
300 return (void *) 0; /* text of size zero */
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.
307 for (i
= 1; i
<= F
; i
++)
308 insert_node(sp
, r
- i
);
311 * Finally, insert the whole string just read.
312 * The global variables match_length and match_position are set.
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. */
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)) );
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
++)
334 *dst
++ = code_buf
[i
];
338 code_buf_ptr
= mask
= 1;
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 */
344 sp
->text_buf
[s
] = c
; /* read new bytes */
347 * If the position is near the end of buffer, extend the buffer
348 * to make string comparison easier.
351 sp
->text_buf
[s
+ N
] = c
;
353 /* Since this is a ring buffer, increment the position modulo N. */
354 s
= (s
+ 1) & (N
- 1);
355 r
= (r
+ 1) & (N
- 1);
357 /* Register the string in text_buf[r..r+F-1] */
360 while (i
++ < last_match_length
) {
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. */
370 } while (len
> 0); /* until length of string to be processed is zero */
372 if (code_buf_ptr
> 1) { /* Send remaining code. */
373 for (i
= 0; i
< code_buf_ptr
; i
++)
375 *dst
++ = code_buf
[i
];