xref: /xnu-12377.81.4/libkern/mkext.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2000-2016 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions #include <stdint.h> // For uintptr_t.
29*043036a2SApple OSS Distributions #include <string.h>
30*043036a2SApple OSS Distributions #include <libkern/mkext.h>
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions 
33*043036a2SApple OSS Distributions #define BASE 65521L /* largest prime smaller than 65536 */
34*043036a2SApple OSS Distributions #define NMAX 5552  // the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
35*043036a2SApple OSS Distributions 
36*043036a2SApple OSS Distributions #define DO1(buf, i)  {s1 += buf[i]; s2 += s1;}
37*043036a2SApple OSS Distributions #define DO2(buf, i)  DO1(buf,i); DO1(buf,i+1);
38*043036a2SApple OSS Distributions #define DO4(buf, i)  DO2(buf,i); DO2(buf,i+2);
39*043036a2SApple OSS Distributions #define DO8(buf, i)  DO4(buf,i); DO4(buf,i+4);
40*043036a2SApple OSS Distributions #define DO16(buf)   DO8(buf,0); DO8(buf,8);
41*043036a2SApple OSS Distributions 
42*043036a2SApple OSS Distributions u_int32_t
mkext_adler32(uint8_t * buf,int32_t len)43*043036a2SApple OSS Distributions mkext_adler32(uint8_t *buf, int32_t len)
44*043036a2SApple OSS Distributions {
45*043036a2SApple OSS Distributions 	unsigned long s1 = 1; // adler & 0xffff;
46*043036a2SApple OSS Distributions 	unsigned long s2 = 0; // (adler >> 16) & 0xffff;
47*043036a2SApple OSS Distributions 	int k;
48*043036a2SApple OSS Distributions 
49*043036a2SApple OSS Distributions 
50*043036a2SApple OSS Distributions 	while (len > 0) {
51*043036a2SApple OSS Distributions 		k = len < NMAX ? len : NMAX;
52*043036a2SApple OSS Distributions 		len -= k;
53*043036a2SApple OSS Distributions 		while (k >= 16) {
54*043036a2SApple OSS Distributions 			DO16(buf);
55*043036a2SApple OSS Distributions 			buf += 16;
56*043036a2SApple OSS Distributions 			k -= 16;
57*043036a2SApple OSS Distributions 		}
58*043036a2SApple OSS Distributions 		if (k != 0) {
59*043036a2SApple OSS Distributions 			do {
60*043036a2SApple OSS Distributions 				s1 += *buf++;
61*043036a2SApple OSS Distributions 				s2 += s1;
62*043036a2SApple OSS Distributions 			} while (--k);
63*043036a2SApple OSS Distributions 		}
64*043036a2SApple OSS Distributions 		s1 %= BASE;
65*043036a2SApple OSS Distributions 		s2 %= BASE;
66*043036a2SApple OSS Distributions 	}
67*043036a2SApple OSS Distributions 	return (u_int32_t)((s2 << 16) | s1);
68*043036a2SApple OSS Distributions }
69*043036a2SApple OSS Distributions 
70*043036a2SApple OSS Distributions 
71*043036a2SApple OSS Distributions /**************************************************************
72*043036a2SApple OSS Distributions *   LZSS.C -- A Data Compression Program
73*043036a2SApple OSS Distributions ***************************************************************
74*043036a2SApple OSS Distributions *    4/6/1989 Haruhiko Okumura
75*043036a2SApple OSS Distributions *    Use, distribute, and modify this program freely.
76*043036a2SApple OSS Distributions *    Please send me your improved versions.
77*043036a2SApple OSS Distributions *        PC-VAN      SCIENCE
78*043036a2SApple OSS Distributions *        NIFTY-Serve PAF01022
79*043036a2SApple OSS Distributions *        CompuServe  74050,1022
80*043036a2SApple OSS Distributions *
81*043036a2SApple OSS Distributions **************************************************************/
82*043036a2SApple OSS Distributions 
83*043036a2SApple OSS Distributions #define N         4096  /* size of ring buffer - must be power of 2 */
84*043036a2SApple OSS Distributions #define F         18    /* upper limit for match_length */
85*043036a2SApple OSS Distributions #define THRESHOLD 2     /* encode string into position and length
86*043036a2SApple OSS Distributions 	                 *  if match_length is greater than this */
87*043036a2SApple OSS Distributions #if !KERNEL
88*043036a2SApple OSS Distributions #define NIL       N     /* index for root of binary search trees */
89*043036a2SApple OSS Distributions #endif
90*043036a2SApple OSS Distributions 
91*043036a2SApple OSS Distributions struct encode_state {
92*043036a2SApple OSS Distributions 	/*
93*043036a2SApple OSS Distributions 	 * left & right children & parent. These constitute binary search trees.
94*043036a2SApple OSS Distributions 	 */
95*043036a2SApple OSS Distributions 	int lchild[N + 1], rchild[N + 257], parent[N + 1];
96*043036a2SApple OSS Distributions 
97*043036a2SApple OSS Distributions 	/* ring buffer of size N, with extra F-1 bytes to aid string comparison */
98*043036a2SApple OSS Distributions 	u_int8_t text_buf[N + F - 1];
99*043036a2SApple OSS Distributions 
100*043036a2SApple OSS Distributions 	/*
101*043036a2SApple OSS Distributions 	 * match_length of longest match.
102*043036a2SApple OSS Distributions 	 * These are set by the insert_node() procedure.
103*043036a2SApple OSS Distributions 	 */
104*043036a2SApple OSS Distributions 	int match_position, match_length;
105*043036a2SApple OSS Distributions };
106*043036a2SApple OSS Distributions 
107*043036a2SApple OSS Distributions 
108*043036a2SApple OSS Distributions int
decompress_lzss(u_int8_t * dst,u_int32_t dstlen,u_int8_t * src,u_int32_t srclen)109*043036a2SApple OSS Distributions decompress_lzss(u_int8_t *dst, u_int32_t dstlen, u_int8_t *src, u_int32_t srclen)
110*043036a2SApple OSS Distributions {
111*043036a2SApple OSS Distributions 	/* ring buffer of size N, with extra F-1 bytes to aid string comparison */
112*043036a2SApple OSS Distributions 	u_int8_t text_buf[N + F - 1];
113*043036a2SApple OSS Distributions 	u_int8_t *dststart = dst;
114*043036a2SApple OSS Distributions 	u_int8_t *dstend = dst + dstlen;
115*043036a2SApple OSS Distributions 	u_int8_t *srcend = src + srclen;
116*043036a2SApple OSS Distributions 	int  i, j, k, r;
117*043036a2SApple OSS Distributions 	u_int8_t c;
118*043036a2SApple OSS Distributions 	unsigned int flags;
119*043036a2SApple OSS Distributions 
120*043036a2SApple OSS Distributions 	dst = dststart;
121*043036a2SApple OSS Distributions 	srcend = src + srclen;
122*043036a2SApple OSS Distributions 	for (i = 0; i < N - F; i++) {
123*043036a2SApple OSS Distributions 		text_buf[i] = ' ';
124*043036a2SApple OSS Distributions 	}
125*043036a2SApple OSS Distributions 	r = N - F;
126*043036a2SApple OSS Distributions 	flags = 0;
127*043036a2SApple OSS Distributions 	for (;;) {
128*043036a2SApple OSS Distributions 		if (((flags >>= 1) & 0x100) == 0) {
129*043036a2SApple OSS Distributions 			if (src < srcend) {
130*043036a2SApple OSS Distributions 				c = *src++;
131*043036a2SApple OSS Distributions 			} else {
132*043036a2SApple OSS Distributions 				break;
133*043036a2SApple OSS Distributions 			}
134*043036a2SApple OSS Distributions 			flags = c | 0xFF00; /* uses higher byte cleverly */
135*043036a2SApple OSS Distributions 		} /* to count eight */
136*043036a2SApple OSS Distributions 		if (flags & 1) {
137*043036a2SApple OSS Distributions 			if (src < srcend) {
138*043036a2SApple OSS Distributions 				c = *src++;
139*043036a2SApple OSS Distributions 			} else {
140*043036a2SApple OSS Distributions 				break;
141*043036a2SApple OSS Distributions 			}
142*043036a2SApple OSS Distributions 			*dst++ = c;
143*043036a2SApple OSS Distributions 			if (dst >= dstend) {
144*043036a2SApple OSS Distributions 				goto finish;
145*043036a2SApple OSS Distributions 			}
146*043036a2SApple OSS Distributions 			text_buf[r++] = c;
147*043036a2SApple OSS Distributions 			r &= (N - 1);
148*043036a2SApple OSS Distributions 		} else {
149*043036a2SApple OSS Distributions 			if (src < srcend) {
150*043036a2SApple OSS Distributions 				i = *src++;
151*043036a2SApple OSS Distributions 			} else {
152*043036a2SApple OSS Distributions 				break;
153*043036a2SApple OSS Distributions 			}
154*043036a2SApple OSS Distributions 			if (src < srcend) {
155*043036a2SApple OSS Distributions 				j = *src++;
156*043036a2SApple OSS Distributions 			} else {
157*043036a2SApple OSS Distributions 				break;
158*043036a2SApple OSS Distributions 			}
159*043036a2SApple OSS Distributions 			i |= ((j & 0xF0) << 4);
160*043036a2SApple OSS Distributions 			j  =  (j & 0x0F) + THRESHOLD;
161*043036a2SApple OSS Distributions 			for (k = 0; k <= j; k++) {
162*043036a2SApple OSS Distributions 				c = text_buf[(i + k) & (N - 1)];
163*043036a2SApple OSS Distributions 				*dst++ = c;
164*043036a2SApple OSS Distributions 				if (dst >= dstend) {
165*043036a2SApple OSS Distributions 					goto finish;
166*043036a2SApple OSS Distributions 				}
167*043036a2SApple OSS Distributions 				text_buf[r++] = c;
168*043036a2SApple OSS Distributions 				r &= (N - 1);
169*043036a2SApple OSS Distributions 			}
170*043036a2SApple OSS Distributions 		}
171*043036a2SApple OSS Distributions 	}
172*043036a2SApple OSS Distributions finish:
173*043036a2SApple OSS Distributions 	return (int)(dst - dststart);
174*043036a2SApple OSS Distributions }
175*043036a2SApple OSS Distributions 
176*043036a2SApple OSS Distributions #if !KERNEL
177*043036a2SApple OSS Distributions 
178*043036a2SApple OSS Distributions /*
179*043036a2SApple OSS Distributions  * initialize state, mostly the trees
180*043036a2SApple OSS Distributions  *
181*043036a2SApple OSS Distributions  * For i = 0 to N - 1, rchild[i] and lchild[i] will be the right and left
182*043036a2SApple OSS Distributions  * children of node i.  These nodes need not be initialized.  Also, parent[i]
183*043036a2SApple OSS Distributions  * is the parent of node i.  These are initialized to NIL (= N), which stands
184*043036a2SApple OSS Distributions  * for 'not used.'  For i = 0 to 255, rchild[N + i + 1] is the root of the
185*043036a2SApple OSS Distributions  * tree for strings that begin with character i.  These are initialized to NIL.
186*043036a2SApple OSS Distributions  * Note there are 256 trees. */
187*043036a2SApple OSS Distributions static void
init_state(struct encode_state * sp)188*043036a2SApple OSS Distributions init_state(struct encode_state *sp)
189*043036a2SApple OSS Distributions {
190*043036a2SApple OSS Distributions 	int  i;
191*043036a2SApple OSS Distributions 
192*043036a2SApple OSS Distributions 	bzero(sp, sizeof(*sp));
193*043036a2SApple OSS Distributions 
194*043036a2SApple OSS Distributions 	for (i = 0; i < N - F; i++) {
195*043036a2SApple OSS Distributions 		sp->text_buf[i] = ' ';
196*043036a2SApple OSS Distributions 	}
197*043036a2SApple OSS Distributions 	for (i = N + 1; i <= N + 256; i++) {
198*043036a2SApple OSS Distributions 		sp->rchild[i] = NIL;
199*043036a2SApple OSS Distributions 	}
200*043036a2SApple OSS Distributions 	for (i = 0; i < N; i++) {
201*043036a2SApple OSS Distributions 		sp->parent[i] = NIL;
202*043036a2SApple OSS Distributions 	}
203*043036a2SApple OSS Distributions }
204*043036a2SApple OSS Distributions 
205*043036a2SApple OSS Distributions /*
206*043036a2SApple OSS Distributions  * Inserts string of length F, text_buf[r..r+F-1], into one of the trees
207*043036a2SApple OSS Distributions  * (text_buf[r]'th tree) and returns the longest-match position and length
208*043036a2SApple OSS Distributions  * via the global variables match_position and match_length.
209*043036a2SApple OSS Distributions  * If match_length = F, then removes the old node in favor of the new one,
210*043036a2SApple OSS Distributions  * because the old one will be deleted sooner. Note r plays double role,
211*043036a2SApple OSS Distributions  * as tree node and position in buffer.
212*043036a2SApple OSS Distributions  */
213*043036a2SApple OSS Distributions static void
insert_node(struct encode_state * sp,int r)214*043036a2SApple OSS Distributions insert_node(struct encode_state *sp, int r)
215*043036a2SApple OSS Distributions {
216*043036a2SApple OSS Distributions 	int  i, p, cmp;
217*043036a2SApple OSS Distributions 	u_int8_t  *key;
218*043036a2SApple OSS Distributions 
219*043036a2SApple OSS Distributions 	cmp = 1;
220*043036a2SApple OSS Distributions 	key = &sp->text_buf[r];
221*043036a2SApple OSS Distributions 	p = N + 1 + key[0];
222*043036a2SApple OSS Distributions 	sp->rchild[r] = sp->lchild[r] = NIL;
223*043036a2SApple OSS Distributions 	sp->match_length = 0;
224*043036a2SApple OSS Distributions 	for (;;) {
225*043036a2SApple OSS Distributions 		if (cmp >= 0) {
226*043036a2SApple OSS Distributions 			if (sp->rchild[p] != NIL) {
227*043036a2SApple OSS Distributions 				p = sp->rchild[p];
228*043036a2SApple OSS Distributions 			} else {
229*043036a2SApple OSS Distributions 				sp->rchild[p] = r;
230*043036a2SApple OSS Distributions 				sp->parent[r] = p;
231*043036a2SApple OSS Distributions 				return;
232*043036a2SApple OSS Distributions 			}
233*043036a2SApple OSS Distributions 		} else {
234*043036a2SApple OSS Distributions 			if (sp->lchild[p] != NIL) {
235*043036a2SApple OSS Distributions 				p = sp->lchild[p];
236*043036a2SApple OSS Distributions 			} else {
237*043036a2SApple OSS Distributions 				sp->lchild[p] = r;
238*043036a2SApple OSS Distributions 				sp->parent[r] = p;
239*043036a2SApple OSS Distributions 				return;
240*043036a2SApple OSS Distributions 			}
241*043036a2SApple OSS Distributions 		}
242*043036a2SApple OSS Distributions 		for (i = 1; i < F; i++) {
243*043036a2SApple OSS Distributions 			if ((cmp = key[i] - sp->text_buf[p + i]) != 0) {
244*043036a2SApple OSS Distributions 				break;
245*043036a2SApple OSS Distributions 			}
246*043036a2SApple OSS Distributions 		}
247*043036a2SApple OSS Distributions 		if (i > sp->match_length) {
248*043036a2SApple OSS Distributions 			sp->match_position = p;
249*043036a2SApple OSS Distributions 			if ((sp->match_length = i) >= F) {
250*043036a2SApple OSS Distributions 				break;
251*043036a2SApple OSS Distributions 			}
252*043036a2SApple OSS Distributions 		}
253*043036a2SApple OSS Distributions 	}
254*043036a2SApple OSS Distributions 	sp->parent[r] = sp->parent[p];
255*043036a2SApple OSS Distributions 	sp->lchild[r] = sp->lchild[p];
256*043036a2SApple OSS Distributions 	sp->rchild[r] = sp->rchild[p];
257*043036a2SApple OSS Distributions 	sp->parent[sp->lchild[p]] = r;
258*043036a2SApple OSS Distributions 	sp->parent[sp->rchild[p]] = r;
259*043036a2SApple OSS Distributions 	if (sp->rchild[sp->parent[p]] == p) {
260*043036a2SApple OSS Distributions 		sp->rchild[sp->parent[p]] = r;
261*043036a2SApple OSS Distributions 	} else {
262*043036a2SApple OSS Distributions 		sp->lchild[sp->parent[p]] = r;
263*043036a2SApple OSS Distributions 	}
264*043036a2SApple OSS Distributions 	sp->parent[p] = NIL; /* remove p */
265*043036a2SApple OSS Distributions }
266*043036a2SApple OSS Distributions 
267*043036a2SApple OSS Distributions /* deletes node p from tree */
268*043036a2SApple OSS Distributions static void
delete_node(struct encode_state * sp,int p)269*043036a2SApple OSS Distributions delete_node(struct encode_state *sp, int p)
270*043036a2SApple OSS Distributions {
271*043036a2SApple OSS Distributions 	int  q;
272*043036a2SApple OSS Distributions 
273*043036a2SApple OSS Distributions 	if (sp->parent[p] == NIL) {
274*043036a2SApple OSS Distributions 		return; /* not in tree */
275*043036a2SApple OSS Distributions 	}
276*043036a2SApple OSS Distributions 	if (sp->rchild[p] == NIL) {
277*043036a2SApple OSS Distributions 		q = sp->lchild[p];
278*043036a2SApple OSS Distributions 	} else if (sp->lchild[p] == NIL) {
279*043036a2SApple OSS Distributions 		q = sp->rchild[p];
280*043036a2SApple OSS Distributions 	} else {
281*043036a2SApple OSS Distributions 		q = sp->lchild[p];
282*043036a2SApple OSS Distributions 		if (sp->rchild[q] != NIL) {
283*043036a2SApple OSS Distributions 			do {
284*043036a2SApple OSS Distributions 				q = sp->rchild[q];
285*043036a2SApple OSS Distributions 			} while (sp->rchild[q] != NIL);
286*043036a2SApple OSS Distributions 			sp->rchild[sp->parent[q]] = sp->lchild[q];
287*043036a2SApple OSS Distributions 			sp->parent[sp->lchild[q]] = sp->parent[q];
288*043036a2SApple OSS Distributions 			sp->lchild[q] = sp->lchild[p];
289*043036a2SApple OSS Distributions 			sp->parent[sp->lchild[p]] = q;
290*043036a2SApple OSS Distributions 		}
291*043036a2SApple OSS Distributions 		sp->rchild[q] = sp->rchild[p];
292*043036a2SApple OSS Distributions 		sp->parent[sp->rchild[p]] = q;
293*043036a2SApple OSS Distributions 	}
294*043036a2SApple OSS Distributions 	sp->parent[q] = sp->parent[p];
295*043036a2SApple OSS Distributions 	if (sp->rchild[sp->parent[p]] == p) {
296*043036a2SApple OSS Distributions 		sp->rchild[sp->parent[p]] = q;
297*043036a2SApple OSS Distributions 	} else {
298*043036a2SApple OSS Distributions 		sp->lchild[sp->parent[p]] = q;
299*043036a2SApple OSS Distributions 	}
300*043036a2SApple OSS Distributions 	sp->parent[p] = NIL;
301*043036a2SApple OSS Distributions }
302*043036a2SApple OSS Distributions 
303*043036a2SApple OSS Distributions #endif /* !KERNEL */
304