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 * @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>
44
45#define N4096/* Size of ring buffer - must be power of 2. */
46#define N_MIN_14095
47#define F18/* Upper limit for match_length. */
48#define RN - F
49#define THRESHOLD2/* Encode string into position and length if match_length is greater than this. */
50#define NILN/* Index for root of binary search trees. */
51
52
53//==============================================================================
54// Refactoring and bug fix Copyright (c) 2010 by DHP.
55
56int decompress_lzss(u_int8_t * dst, u_int8_t * src, u_int32_t srclen)
57{
58/* Four KB ring buffer with 17 extra bytes added to aid string comparisons. */
59u_int8_t text_buf[N_MIN_1 + F];
60u_int8_t * dststart = dst;
61const u_int8_t * srcend = (src + srclen);
62
63int r = R;
64int i, j, k, c;
65unsigned int flags = 0;
66
67for (i = 0; i < R; i++)
68{
69text_buf[i] = ' ';
70}
71
72while (src < srcend)
73{
74if (((flags >>= 1) & 0x100) == 0)
75{
76c = *src++;
77flags = c | 0xFF00; // Clever use of the high byte.
78 }
79
80 if ((src < srcend) && (flags & 1))
81{
82c = *src++;
83*dst++ = c;
84text_buf[r++] = c;
85r &= N_MIN_1;
86}
87else if ((src + 2) <= srcend)
88{
89i = *src++;
90j = *src++;
91
92i |= ((j & 0xF0) << 4);
93j = (j & 0x0F) + THRESHOLD;
94
95for (k = 0; k <= j; k++)
96{
97c = text_buf[(i + k) & N_MIN_1];
98*dst++ = c;
99text_buf[r++] = c;
100r &= N_MIN_1;
101}
102}
103}
104
105return dst - dststart;
106}
107

Archive Download this file

Revision: 2037