Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/i386/boot2/lzss.c

1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/**************************************************************
25 LZSS.C -- A Data Compression Program
26***************************************************************
27 4/6/1989 Haruhiko Okumura
28 Use, distribute, and modify this program freely.
29 Please send me your improved versions.
30 PC-VAN SCIENCE
31 NIFTY-Serve PAF01022
32 CompuServe 74050,1022
33
34**************************************************************/
35/*
36 * lzss.c - Package for decompressing lzss compressed objects
37 *
38 * Copyright (c) 2003 Apple Computer, Inc.
39 *
40 * DRI: Josh de Cesare
41 */
42
43//#include <sl.h> // can't find this guy on the system; maybe we should stop looking for it there first!?
44#include "sl.h" //Azi:???
45
46#define N 4096 /* size of ring buffer - must be power of 2 */
47#define F 18 /* upper limit for match_length */
48#define THRESHOLD 2 /* encode string into position and length
49 if match_length is greater than this */
50#define NIL N /* index for root of binary search trees */
51
52int
53decompress_lzss(u_int8_t *dst, u_int8_t *src, u_int32_t srclen)
54{
55 /* ring buffer of size N, with extra F-1 bytes to aid string comparison */
56 u_int8_t text_buf[N + F - 1];
57 u_int8_t *dststart = dst;
58 u_int8_t *srcend = src + srclen;
59 int i, j, k, r, c;
60 unsigned int flags;
61
62 dst = dststart;
63 srcend = src + srclen;
64 for (i = 0; i < N - F; i++)
65 text_buf[i] = ' ';
66 r = N - F;
67 flags = 0;
68 for ( ; ; ) {
69 if (((flags >>= 1) & 0x100) == 0) {
70 if (src < srcend) c = *src++; else break;
71 flags = c | 0xFF00; /* uses higher byte cleverly */
72 } /* to count eight */
73 if (flags & 1) {
74 if (src < srcend) c = *src++; else break;
75 *dst++ = c;
76 text_buf[r++] = c;
77 r &= (N - 1);
78 } else {
79 if (src < srcend) i = *src++; else break;
80 if (src < srcend) j = *src++; else break;
81 i |= ((j & 0xF0) << 4);
82 j = (j & 0x0F) + THRESHOLD;
83 for (k = 0; k <= j; k++) {
84 c = text_buf[(i + k) & (N - 1)];
85 *dst++ = c;
86 text_buf[r++] = c;
87 r &= (N - 1);
88 }
89 }
90 }
91
92 return dst - dststart;
93}
94

Archive Download this file

Revision: 840