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