-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.c
More file actions
245 lines (214 loc) · 8.77 KB
/
list.c
File metadata and controls
245 lines (214 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/***************************************************************************
* Lempel, Ziv, Storer, and Szymanski Encoding and Decoding
*
* File : list.c
* Purpose : Implement linked list optimized matching of uncoded strings
* for LZSS algorithm.
* Author : Michael Dipperstein
* Date : February 18, 2004
*
****************************************************************************
*
* List: Linked list optimized matching routines used by LZSS
* Encoding/Decoding Routine
* Copyright (C) 2004 - 2007, 2014 by
* Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
*
* This file is part of the lzss library.
*
* The lzss library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* The lzss library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include "lzlocal.h"
/***************************************************************************
* CONSTANTS
***************************************************************************/
#define NULL_INDEX (WINDOW_SIZE + 1)
/***************************************************************************
* GLOBAL VARIABLES
***************************************************************************/
/* cyclic buffer sliding window of already read characters */
extern unsigned char slidingWindow[];
extern unsigned char uncodedLookahead[];
unsigned int lists[UCHAR_MAX]; /* heads of linked lists */
unsigned int next[WINDOW_SIZE]; /* indices of next in list */
/***************************************************************************
* FUNCTIONS
***************************************************************************/
/****************************************************************************
* Function : InitializeSearchStructures
* Description: This function initializes structures used to speed up the
* process of mathcing uncoded strings to strings in the
* sliding window. For link list optimized searches, this
* means that linked lists of strings all starting with
* the same character are initialized.
* Parameters : None
* Effects : Initializes lists and next array
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
*
* NOTE: This function assumes that the sliding window is initially filled
* with all identical characters.
****************************************************************************/
int InitializeSearchStructures(void)
{
unsigned int i;
for (i = 0; i < WINDOW_SIZE; i++)
{
next[i] = i + 1;
}
/* there's no next for the last character */
next[WINDOW_SIZE - 1] = NULL_INDEX;
/* the only list right now is the slidingWindow[0] list */
for (i = 0; i < 256; i++)
{
lists[i] = NULL_INDEX;
}
lists[slidingWindow[0]] = 0;
return 0;
}
/****************************************************************************
* Function : FindMatch
* Description: This function will search through the slidingWindow
* dictionary for the longest sequence matching the MAX_CODED
* long string stored in uncodedLookahed.
* Parameters : windowHead - head of sliding window (unused)
* uncodedHead - head of uncoded lookahead buffer
* Effects : None
* Returned : The sliding window index where the match starts and the
* length of the match. If there is no match a length of
* zero will be returned.
****************************************************************************/
encoded_string_t FindMatch(const unsigned int windowHead,
const unsigned int uncodedHead)
{
encoded_string_t matchData;
unsigned int i;
unsigned int j;
(void)windowHead; /* prevents unused variable warning */
matchData.length = 0;
matchData.offset = 0;
i = lists[uncodedLookahead[uncodedHead]]; /* start of proper list */
while (i != NULL_INDEX)
{
/* the list insures we matched one, how many more match? */
j = 1;
while(slidingWindow[Wrap((i + j), WINDOW_SIZE)] ==
uncodedLookahead[Wrap((uncodedHead + j), MAX_CODED)])
{
if (j >= MAX_CODED)
{
break;
}
j++;
}
if (j > matchData.length)
{
matchData.length = j;
matchData.offset = i;
}
if (j >= MAX_CODED)
{
matchData.length = MAX_CODED;
break;
}
i = next[i]; /* try next in list */
}
return matchData;
}
/****************************************************************************
* Function : AddChar
* Description: This function adds the character stored in
* slidingWindow[charIndex] to the linked lists.
* Parameters : charIndex - sliding window index of the character to be
* added to the linked list.
* Effects : slidingWindow[charIndex] appended to the end of the
* appropriate linked list.
* Returned : NONE
****************************************************************************/
static void AddChar(const unsigned int charIndex)
{
unsigned int i;
/* inserted character will be at the end of the list */
next[charIndex] = NULL_INDEX;
if (lists[slidingWindow[charIndex]] == NULL_INDEX)
{
/* this is the only character in it's list */
lists[slidingWindow[charIndex]] = charIndex;
return;
}
/* find the end of the list */
i = lists[slidingWindow[charIndex]];
while(next[i] != NULL_INDEX)
{
i = next[i];
}
/* add new character to the list end */
next[i] = charIndex;
}
/****************************************************************************
* Function : RemoveChar
* Description: This function removes the character stored in
* slidingWindow[charIndex] from the linked lists.
* Parameters : charIndex - sliding window index of the character to be
* removed from the linked list.
* Effects : slidingWindow[charIndex] is removed from it's linked list
* and the list is appropriately reconnected.
* Returned : NONE
****************************************************************************/
static void RemoveChar(const unsigned int charIndex)
{
unsigned int i;
unsigned int nextIndex;
nextIndex = next[charIndex]; /* remember where this points to */
next[charIndex] = NULL_INDEX;
if (lists[slidingWindow[charIndex]] == charIndex)
{
/* we're deleting a list head */
lists[slidingWindow[charIndex]] = nextIndex;
return;
}
/* find character pointing to ours */
i = lists[slidingWindow[charIndex]];
while(next[i] != charIndex)
{
i = next[i];
}
/* point the previous next */
next[i] = nextIndex;
}
/****************************************************************************
* Function : ReplaceChar
* Description: This function replaces the character stored in
* slidingWindow[charIndex] with the one specified by
* replacement. The linked list entries effected by the
* replacement are also corrected.
* Parameters : charIndex - sliding window index of the character to be
* removed from the linked list.
* Effects : slidingWindow[charIndex] is replaced by replacement. Old
* list entries for strings containing slidingWindow[charIndex]
* are removed and new ones are added.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
****************************************************************************/
int ReplaceChar(const unsigned int charIndex, const unsigned char replacement)
{
RemoveChar(charIndex);
slidingWindow[charIndex] = replacement;
AddChar(charIndex);
return 0;
}