Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/boot2/lzss.c

1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 *
5 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
6 * Reserved. This file contains Original Code and/or Modifications of
7 * Original Code as defined in and that are subject to the Apple Public
8 * Source License Version 2.0 (the "License"). You may not use this file
9 * except in compliance with the License. Please obtain a copy of the
10 * License at http://www.apple.com/publicsource and read it before using
11 * this 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 OR NON- INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
19 * under the License.
20 *
21 *
22***************************************************************
23 LZSS.C -- A Data Compression Program
24***************************************************************
25 4/6/1989 Haruhiko Okumura
26 Use, distribute, and modify this program freely.
27 Please send me your improved versions.
28 PC-VAN SCIENCE
29 NIFTY-Serve PAF01022
30 CompuServe 74050,1022
31
32**************************************************************+
33 *
34 * lzss.c - Package for decompressing lzss compressed objects
35 *
36 * Copyright (c) 2003 Apple Computer, Inc.
37 *
38 * DRI: Josh de Cesare
39 */
40
41#include <sl.h>
42
43#define N4096/* Size of ring buffer - must be power of 2. */
44#define N_MIN_14095
45#define F18/* Upper limit for match_length. */
46#define RN - F
47#define THRESHOLD2/* Encode string into position and length if match_length is greater than this. */
48#define NILN/* Index for root of binary search trees. */
49
50
51//==============================================================================
52// Refactoring and bug fix Copyright (c) 2010 by DHP.
53
54int decompress_lzss(u_int8_t * dst, u_int8_t * src, u_int32_t srclen)
55{
56/* Four KB ring buffer with 17 extra bytes added to aid string comparisons. */
57u_int8_t text_buf[N_MIN_1 + F];
58u_int8_t * dststart = dst;
59const u_int8_t * srcend = (src + srclen);
60
61int r = R;
62int i, j, k, c;
63unsigned int flags = 0;
64
65for (i = 0; i < R; i++)
66{
67text_buf[i] = ' ';
68}
69
70while (src < srcend)
71{
72if (((flags >>= 1) & 0x100) == 0)
73{
74c = *src++;
75flags = c | 0xFF00; // Clever use of the high byte.
76 }
77
78 if ((src < srcend) && (flags & 1))
79{
80c = *src++;
81*dst++ = c;
82text_buf[r++] = c;
83r &= N_MIN_1;
84}
85else if ((src + 2) <= srcend)
86{
87i = *src++;
88j = *src++;
89
90i |= ((j & 0xF0) << 4);
91j = (j & 0x0F) + THRESHOLD;
92
93for (k = 0; k <= j; k++)
94{
95c = text_buf[(i + k) & N_MIN_1];
96*dst++ = c;
97text_buf[r++] = c;
98r &= N_MIN_1;
99}
100}
101}
102
103return dst - dststart;
104}
105

Archive Download this file

Revision: 2045