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