libStatGen Software 1
Loading...
Searching...
No Matches
MiniDeflate Class Reference

Public Member Functions

void Deflate (FILE *output, void *input, size_t bytes)
 
void Inflate (FILE *input, void *ouput, size_t bytes)
 

Detailed Description

Definition at line 39 of file MiniDeflate.h.

Constructor & Destructor Documentation

◆ MiniDeflate()

MiniDeflate::MiniDeflate ( )

Definition at line 29 of file MiniDeflate.cpp.

30{
31 buffer = new uchar [BUFFER_SIZE + 5];
32 hash_keys = new uchar [HASH_SIZE];
33 hash_values = new uchar * [HASH_SIZE * HASH_DEPTH];
34}

◆ ~MiniDeflate()

MiniDeflate::~MiniDeflate ( )

Definition at line 36 of file MiniDeflate.cpp.

37{
38 delete [] buffer;
39 delete [] hash_keys;
40 delete [] hash_values;
41}

Member Function Documentation

◆ Deflate()

void MiniDeflate::Deflate ( FILE *  output,
void *  input,
size_t  bytes 
)

Definition at line 153 of file MiniDeflate.cpp.

154{
155 uchar * in = (uchar *) void_input;
156 uchar * out = (uchar *) buffer;
157 int buffer_len = BUFFER_SIZE;
158
159 for (int i = 0; i < HASH_SIZE; i++) hash_keys[i] = EMPTY_KEY;
160
161 uchar * in2 = in;
162
163 while (len > 2)
164 {
165 // Hash the current input value
166 int hash = ((in[0] << 16) | (in[1] << 8) | in[2]) % HASH_SIZE;
167
168 if (hash_keys[hash] != EMPTY_KEY)
169 // Possible matches in hash table
170 {
171 int best_match = 0;
172 uchar * best_pos = NULL;
173
174 EvaluateMatch(in, len, hash, best_pos, best_match);
175
176 // If there are no decent matches
177 if (best_match < 3)
178 {
179 in++;
180 len--;
181 continue;
182 }
183
184 // Try look ahead if match isn't great
185 while (best_match < OKAY_MATCH && len > 3)
186 {
187 // Peek to see if we could get a better match
188 int next_hash = ((in[1] << 16) | (in[2] << 8) | in[3]) % HASH_SIZE;
189
190 if (hash_keys[next_hash] == EMPTY_KEY) break;
191
192 int next_match = 0;
193 uchar * next_pos = NULL;
194
195 EvaluateMatch(in + 1, len - 1, next_hash, next_pos, next_match);
196
197 // Didn't find a better match
198 if (next_match <= best_match + 1) break;
199
200 // Found a better match, so try again
201 in++;
202 len--;
203 best_match = next_match;
204 best_pos = next_pos;
205 }
206
207 int best_offset = in - best_pos - 1;
208
209 // This is where we output stuff
210 // Check if we have some literals to output first
211 OutputLiterals(in2, in - in2, out, buffer_len, output);
212
213 in2 = in += best_match;
214 len -= best_match;
215
216 if (best_match < 17 && best_offset < 0x1000)
217 {
218 *out = (uchar)(((best_match - 1) << 4) | (best_offset >> 8));
219 out++;
220 *out = (uchar)(best_offset & 0xFF);
221 out++;
222 buffer_len -= 2;
223 }
224 else if (best_match < 66)
225 {
226 *out = (uchar)(16 | (best_offset >> 10));
227 out++;
228 *out = (uchar)((best_offset >> 2) & 0xFF);
229 out++;
230 *out = (uchar)((best_offset << 6) | (best_match - 2));
231 out++;
232 buffer_len -= 3;
233 }
234 else
235 {
236 *out = (uchar)(16 | (best_offset >> 10));
237 out++;
238 *out = (uchar)((best_offset >> 2) & 0xFF);
239 out++;
240 *out = (uchar)(best_offset << 6);
241 out++;
242 best_match -= 66;
243 *out = (uchar)(best_match >> 8);
244 out++;
245 *out = (uchar)(best_match & 0xFF);
246 out++;
247 buffer_len -= 5;
248 }
249
250 if (buffer_len <= 0)
251 {
252 fwrite(buffer, out - buffer, 1, output);
253 buffer_len = BUFFER_SIZE;
254 out = buffer;
255 }
256 }
257 // Never seen this sequence before
258 else
259 {
260 hash_keys[hash] = 0;
261 for (int i = 1; i < HASH_DEPTH; i++) hash_values[hash * 8 + i] = NULL;
262 hash_values[hash * 8] = in;
263 in++;
264 len--;
265 }
266 }
267
268 // Check if we have some trailing literals to output
269 in += len;
270 OutputLiterals(in2, in - in2, out, buffer_len, output);
271
272 // Flush output
273 if (out != buffer) fwrite(buffer, out - buffer, 1, output);
274}

◆ Inflate()

void MiniDeflate::Inflate ( FILE *  input,
void *  ouput,
size_t  bytes 
)

Definition at line 303 of file MiniDeflate.cpp.

304{
305 uchar * out = (uchar *) void_output;
306 uchar * in = (uchar *) buffer + 5;
307 int buffer_len = BUFFER_SIZE;
308
309 buffer_len = fread(buffer + 5, 1, BUFFER_SIZE, input);
310
311 while (len)
312 {
313 int match_len = *in >> 4;
314
315 // Matching a literal
316 if (match_len == 0)
317 {
318 match_len = *in & 0x0F;
319 in++, buffer_len--;
320
321 // If match_len == 0 then string is longer than 30 characters
322 // Strings of 16 - 30 characters are encoded as two short strings
323 if (match_len == 0)
324 {
325 match_len = (in[0] << 8) + in[1] + 31;
326 in += 2;
327 buffer_len -= 2;
328 }
329
330 CiteLiteral(out, match_len, in, buffer_len, input);
331 len -= match_len;
332 }
333 // Long match, 14 bit offset
334 else if (match_len == 1)
335 {
336 int offset = (((in[0] & 0x0F) << 10) | (in[1] << 2) | (in[2] >> 6)) + 1;
337 match_len = (in[2] & 0x3F) + 2;
338 in += 3;
339 buffer_len -= 3;
340
341 if (match_len == 2)
342 {
343 match_len = ((in[0] << 8) | in[1]) + 66;
344 in += 2;
345 buffer_len -= 2;
346 }
347
348 uchar * match_pos = out - offset;
349 len -= match_len;
350 while (match_len--)
351 {
352 *out = *match_pos;
353 out++, match_pos++;
354 }
355 }
356 // Typical short match
357 else
358 {
359 int offset = (((in[0] & 0x0F) << 8) | in[1]) + 1;
360 in += 2;
361 buffer_len -= 2;
362
363 uchar * match_pos = out - offset;
364 len -= ++match_len;
365 while (match_len--)
366 {
367 *out = *match_pos;
368 out++, match_pos++;
369 }
370 }
371
372 if (buffer_len < 5)
373 {
374 uchar * in2 = (uchar *) buffer + 5 - buffer_len;
375 while (in2 != buffer + 5)
376 {
377 *in2 = *in;
378 in2++;
379 in++;
380 }
381
382 in = buffer + 5 - buffer_len;
383 buffer_len += fread(buffer + 5, 1, BUFFER_SIZE, input);
384 }
385 }
386
387 if (buffer_len) fseek(input, -buffer_len, SEEK_CUR);
388}

The documentation for this class was generated from the following files: