/src/lzma-fuzz/sdk/C/LzFind.c
Line  | Count  | Source  | 
1  |  | /* LzFind.c -- Match finder for LZ algorithms  | 
2  |  | 2018-07-08 : Igor Pavlov : Public domain */  | 
3  |  |  | 
4  |  | #include "Precomp.h"  | 
5  |  |  | 
6  |  | #include <string.h>  | 
7  |  |  | 
8  |  | #include "LzFind.h"  | 
9  |  | #include "LzHash.h"  | 
10  |  |  | 
11  | 53.9G  | #define kEmptyHashValue 0  | 
12  | 1.61M  | #define kMaxValForNormalize ((UInt32)0xFFFFFFFF)  | 
13  | 0  | #define kNormalizeStepMin (1 << 10) /* it must be power of 2 */  | 
14  | 0  | #define kNormalizeMask (~(UInt32)(kNormalizeStepMin - 1))  | 
15  | 6.34k  | #define kMaxHistorySize ((UInt32)7 << 29)  | 
16  |  |  | 
17  |  | #define kStartMaxLen 3  | 
18  |  |  | 
19  |  | static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc)  | 
20  | 12.6k  | { | 
21  | 12.6k  |   if (!p->directInput)  | 
22  | 12.6k  |   { | 
23  | 12.6k  |     ISzAlloc_Free(alloc, p->bufferBase);  | 
24  | 12.6k  |     p->bufferBase = NULL;  | 
25  | 12.6k  |   }  | 
26  | 12.6k  | }  | 
27  |  |  | 
28  |  | /* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */  | 
29  |  |  | 
30  |  | static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAllocPtr alloc)  | 
31  | 6.34k  | { | 
32  | 6.34k  |   UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;  | 
33  | 6.34k  |   if (p->directInput)  | 
34  | 0  |   { | 
35  | 0  |     p->blockSize = blockSize;  | 
36  | 0  |     return 1;  | 
37  | 0  |   }  | 
38  | 6.34k  |   if (!p->bufferBase || p->blockSize != blockSize)  | 
39  | 6.34k  |   { | 
40  | 6.34k  |     LzInWindow_Free(p, alloc);  | 
41  | 6.34k  |     p->blockSize = blockSize;  | 
42  | 6.34k  |     p->bufferBase = (Byte *)ISzAlloc_Alloc(alloc, (size_t)blockSize);  | 
43  | 6.34k  |   }  | 
44  | 6.34k  |   return (p->bufferBase != NULL);  | 
45  | 6.34k  | }  | 
46  |  |  | 
47  | 466M  | Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; } | 
48  |  |  | 
49  | 447M  | UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; } | 
50  |  |  | 
51  |  | void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)  | 
52  | 0  | { | 
53  | 0  |   p->posLimit -= subValue;  | 
54  | 0  |   p->pos -= subValue;  | 
55  | 0  |   p->streamPos -= subValue;  | 
56  | 0  | }  | 
57  |  |  | 
58  |  | static void MatchFinder_ReadBlock(CMatchFinder *p)  | 
59  | 9.04k  | { | 
60  | 9.04k  |   if (p->streamEndWasReached || p->result != SZ_OK)  | 
61  | 0  |     return;  | 
62  |  |  | 
63  |  |   /* We use (p->streamPos - p->pos) value. (p->streamPos < p->pos) is allowed. */  | 
64  |  |  | 
65  | 9.04k  |   if (p->directInput)  | 
66  | 0  |   { | 
67  | 0  |     UInt32 curSize = 0xFFFFFFFF - (p->streamPos - p->pos);  | 
68  | 0  |     if (curSize > p->directInputRem)  | 
69  | 0  |       curSize = (UInt32)p->directInputRem;  | 
70  | 0  |     p->directInputRem -= curSize;  | 
71  | 0  |     p->streamPos += curSize;  | 
72  | 0  |     if (p->directInputRem == 0)  | 
73  | 0  |       p->streamEndWasReached = 1;  | 
74  | 0  |     return;  | 
75  | 0  |   }  | 
76  |  |     | 
77  | 9.04k  |   for (;;)  | 
78  | 14.0k  |   { | 
79  | 14.0k  |     Byte *dest = p->buffer + (p->streamPos - p->pos);  | 
80  | 14.0k  |     size_t size = (p->bufferBase + p->blockSize - dest);  | 
81  | 14.0k  |     if (size == 0)  | 
82  | 0  |       return;  | 
83  |  |  | 
84  | 14.0k  |     p->result = ISeqInStream_Read(p->stream, dest, &size);  | 
85  | 14.0k  |     if (p->result != SZ_OK)  | 
86  | 0  |       return;  | 
87  | 14.0k  |     if (size == 0)  | 
88  | 6.34k  |     { | 
89  | 6.34k  |       p->streamEndWasReached = 1;  | 
90  | 6.34k  |       return;  | 
91  | 6.34k  |     }  | 
92  | 7.71k  |     p->streamPos += (UInt32)size;  | 
93  | 7.71k  |     if (p->streamPos - p->pos > p->keepSizeAfter)  | 
94  | 2.69k  |       return;  | 
95  | 7.71k  |   }  | 
96  | 9.04k  | }  | 
97  |  |  | 
98  |  | void MatchFinder_MoveBlock(CMatchFinder *p)  | 
99  | 0  | { | 
100  | 0  |   memmove(p->bufferBase,  | 
101  | 0  |       p->buffer - p->keepSizeBefore,  | 
102  | 0  |       (size_t)(p->streamPos - p->pos) + p->keepSizeBefore);  | 
103  | 0  |   p->buffer = p->bufferBase + p->keepSizeBefore;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | int MatchFinder_NeedMove(CMatchFinder *p)  | 
107  | 2.69k  | { | 
108  | 2.69k  |   if (p->directInput)  | 
109  | 0  |     return 0;  | 
110  |  |   /* if (p->streamEndWasReached) return 0; */  | 
111  | 2.69k  |   return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);  | 
112  | 2.69k  | }  | 
113  |  |  | 
114  |  | void MatchFinder_ReadIfRequired(CMatchFinder *p)  | 
115  | 0  | { | 
116  | 0  |   if (p->streamEndWasReached)  | 
117  | 0  |     return;  | 
118  | 0  |   if (p->keepSizeAfter >= p->streamPos - p->pos)  | 
119  | 0  |     MatchFinder_ReadBlock(p);  | 
120  | 0  | }  | 
121  |  |  | 
122  |  | static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)  | 
123  | 2.69k  | { | 
124  | 2.69k  |   if (MatchFinder_NeedMove(p))  | 
125  | 0  |     MatchFinder_MoveBlock(p);  | 
126  | 2.69k  |   MatchFinder_ReadBlock(p);  | 
127  | 2.69k  | }  | 
128  |  |  | 
129  |  | static void MatchFinder_SetDefaultSettings(CMatchFinder *p)  | 
130  | 6.34k  | { | 
131  | 6.34k  |   p->cutValue = 32;  | 
132  | 6.34k  |   p->btMode = 1;  | 
133  | 6.34k  |   p->numHashBytes = 4;  | 
134  | 6.34k  |   p->bigHash = 0;  | 
135  | 6.34k  | }  | 
136  |  |  | 
137  | 12.9M  | #define kCrcPoly 0xEDB88320  | 
138  |  |  | 
139  |  | void MatchFinder_Construct(CMatchFinder *p)  | 
140  | 6.34k  | { | 
141  | 6.34k  |   unsigned i;  | 
142  | 6.34k  |   p->bufferBase = NULL;  | 
143  | 6.34k  |   p->directInput = 0;  | 
144  | 6.34k  |   p->hash = NULL;  | 
145  | 6.34k  |   p->expectedDataSize = (UInt64)(Int64)-1;  | 
146  | 6.34k  |   MatchFinder_SetDefaultSettings(p);  | 
147  |  |  | 
148  | 1.63M  |   for (i = 0; i < 256; i++)  | 
149  | 1.62M  |   { | 
150  | 1.62M  |     UInt32 r = (UInt32)i;  | 
151  | 1.62M  |     unsigned j;  | 
152  | 14.6M  |     for (j = 0; j < 8; j++)  | 
153  | 12.9M  |       r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));  | 
154  | 1.62M  |     p->crc[i] = r;  | 
155  | 1.62M  |   }  | 
156  | 6.34k  | }  | 
157  |  |  | 
158  |  | static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc)  | 
159  | 12.6k  | { | 
160  | 12.6k  |   ISzAlloc_Free(alloc, p->hash);  | 
161  | 12.6k  |   p->hash = NULL;  | 
162  | 12.6k  | }  | 
163  |  |  | 
164  |  | void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc)  | 
165  | 6.34k  | { | 
166  | 6.34k  |   MatchFinder_FreeThisClassMemory(p, alloc);  | 
167  | 6.34k  |   LzInWindow_Free(p, alloc);  | 
168  | 6.34k  | }  | 
169  |  |  | 
170  |  | static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc)  | 
171  | 6.34k  | { | 
172  | 6.34k  |   size_t sizeInBytes = (size_t)num * sizeof(CLzRef);  | 
173  | 6.34k  |   if (sizeInBytes / sizeof(CLzRef) != num)  | 
174  | 0  |     return NULL;  | 
175  | 6.34k  |   return (CLzRef *)ISzAlloc_Alloc(alloc, sizeInBytes);  | 
176  | 6.34k  | }  | 
177  |  |  | 
178  |  | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,  | 
179  |  |     UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,  | 
180  |  |     ISzAllocPtr alloc)  | 
181  | 6.34k  | { | 
182  | 6.34k  |   UInt32 sizeReserv;  | 
183  |  |     | 
184  | 6.34k  |   if (historySize > kMaxHistorySize)  | 
185  | 0  |   { | 
186  | 0  |     MatchFinder_Free(p, alloc);  | 
187  | 0  |     return 0;  | 
188  | 0  |   }  | 
189  |  |     | 
190  | 6.34k  |   sizeReserv = historySize >> 1;  | 
191  | 6.34k  |        if (historySize >= ((UInt32)3 << 30)) sizeReserv = historySize >> 3;  | 
192  | 6.34k  |   else if (historySize >= ((UInt32)2 << 30)) sizeReserv = historySize >> 2;  | 
193  |  |     | 
194  | 6.34k  |   sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);  | 
195  |  |  | 
196  | 6.34k  |   p->keepSizeBefore = historySize + keepAddBufferBefore + 1;  | 
197  | 6.34k  |   p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;  | 
198  |  |     | 
199  |  |   /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */  | 
200  |  |     | 
201  | 6.34k  |   if (LzInWindow_Create(p, sizeReserv, alloc))  | 
202  | 6.34k  |   { | 
203  | 6.34k  |     UInt32 newCyclicBufferSize = historySize + 1;  | 
204  | 6.34k  |     UInt32 hs;  | 
205  | 6.34k  |     p->matchMaxLen = matchMaxLen;  | 
206  | 6.34k  |     { | 
207  | 6.34k  |       p->fixedHashSize = 0;  | 
208  | 6.34k  |       if (p->numHashBytes == 2)  | 
209  | 0  |         hs = (1 << 16) - 1;  | 
210  | 6.34k  |       else  | 
211  | 6.34k  |       { | 
212  | 6.34k  |         hs = historySize;  | 
213  | 6.34k  |         if (hs > p->expectedDataSize)  | 
214  | 0  |           hs = (UInt32)p->expectedDataSize;  | 
215  | 6.34k  |         if (hs != 0)  | 
216  | 6.34k  |           hs--;  | 
217  | 6.34k  |         hs |= (hs >> 1);  | 
218  | 6.34k  |         hs |= (hs >> 2);  | 
219  | 6.34k  |         hs |= (hs >> 4);  | 
220  | 6.34k  |         hs |= (hs >> 8);  | 
221  | 6.34k  |         hs >>= 1;  | 
222  | 6.34k  |         hs |= 0xFFFF; /* don't change it! It's required for Deflate */  | 
223  | 6.34k  |         if (hs > (1 << 24))  | 
224  | 0  |         { | 
225  | 0  |           if (p->numHashBytes == 3)  | 
226  | 0  |             hs = (1 << 24) - 1;  | 
227  | 0  |           else  | 
228  | 0  |             hs >>= 1;  | 
229  |  |           /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */  | 
230  | 0  |         }  | 
231  | 6.34k  |       }  | 
232  | 6.34k  |       p->hashMask = hs;  | 
233  | 6.34k  |       hs++;  | 
234  | 6.34k  |       if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;  | 
235  | 6.34k  |       if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;  | 
236  | 6.34k  |       if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;  | 
237  | 6.34k  |       hs += p->fixedHashSize;  | 
238  | 6.34k  |     }  | 
239  |  |  | 
240  | 6.34k  |     { | 
241  | 6.34k  |       size_t newSize;  | 
242  | 6.34k  |       size_t numSons;  | 
243  | 6.34k  |       p->historySize = historySize;  | 
244  | 6.34k  |       p->hashSizeSum = hs;  | 
245  | 6.34k  |       p->cyclicBufferSize = newCyclicBufferSize;  | 
246  |  |         | 
247  | 6.34k  |       numSons = newCyclicBufferSize;  | 
248  | 6.34k  |       if (p->btMode)  | 
249  | 6.34k  |         numSons <<= 1;  | 
250  | 6.34k  |       newSize = hs + numSons;  | 
251  |  |  | 
252  | 6.34k  |       if (p->hash && p->numRefs == newSize)  | 
253  | 0  |         return 1;  | 
254  |  |         | 
255  | 6.34k  |       MatchFinder_FreeThisClassMemory(p, alloc);  | 
256  | 6.34k  |       p->numRefs = newSize;  | 
257  | 6.34k  |       p->hash = AllocRefs(newSize, alloc);  | 
258  |  |         | 
259  | 6.34k  |       if (p->hash)  | 
260  | 6.34k  |       { | 
261  | 6.34k  |         p->son = p->hash + p->hashSizeSum;  | 
262  | 6.34k  |         return 1;  | 
263  | 6.34k  |       }  | 
264  | 6.34k  |     }  | 
265  | 6.34k  |   }  | 
266  |  |  | 
267  | 0  |   MatchFinder_Free(p, alloc);  | 
268  | 0  |   return 0;  | 
269  | 6.34k  | }  | 
270  |  |  | 
271  |  | static void MatchFinder_SetLimits(CMatchFinder *p)  | 
272  | 812k  | { | 
273  | 812k  |   UInt32 limit = kMaxValForNormalize - p->pos;  | 
274  | 812k  |   UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;  | 
275  |  |     | 
276  | 812k  |   if (limit2 < limit)  | 
277  | 812k  |     limit = limit2;  | 
278  | 812k  |   limit2 = p->streamPos - p->pos;  | 
279  |  |     | 
280  | 812k  |   if (limit2 <= p->keepSizeAfter)  | 
281  | 809k  |   { | 
282  | 809k  |     if (limit2 > 0)  | 
283  | 803k  |       limit2 = 1;  | 
284  | 809k  |   }  | 
285  | 2.69k  |   else  | 
286  | 2.69k  |     limit2 -= p->keepSizeAfter;  | 
287  |  |     | 
288  | 812k  |   if (limit2 < limit)  | 
289  | 812k  |     limit = limit2;  | 
290  |  |     | 
291  | 812k  |   { | 
292  | 812k  |     UInt32 lenLimit = p->streamPos - p->pos;  | 
293  | 812k  |     if (lenLimit > p->matchMaxLen)  | 
294  | 654k  |       lenLimit = p->matchMaxLen;  | 
295  | 812k  |     p->lenLimit = lenLimit;  | 
296  | 812k  |   }  | 
297  | 812k  |   p->posLimit = p->pos + limit;  | 
298  | 812k  | }  | 
299  |  |  | 
300  |  |  | 
301  |  | void MatchFinder_Init_LowHash(CMatchFinder *p)  | 
302  | 6.34k  | { | 
303  | 6.34k  |   size_t i;  | 
304  | 6.34k  |   CLzRef *items = p->hash;  | 
305  | 6.34k  |   size_t numItems = p->fixedHashSize;  | 
306  | 422M  |   for (i = 0; i < numItems; i++)  | 
307  | 422M  |     items[i] = kEmptyHashValue;  | 
308  | 6.34k  | }  | 
309  |  |  | 
310  |  |  | 
311  |  | void MatchFinder_Init_HighHash(CMatchFinder *p)  | 
312  | 6.34k  | { | 
313  | 6.34k  |   size_t i;  | 
314  | 6.34k  |   CLzRef *items = p->hash + p->fixedHashSize;  | 
315  | 6.34k  |   size_t numItems = (size_t)p->hashMask + 1;  | 
316  | 53.2G  |   for (i = 0; i < numItems; i++)  | 
317  | 53.2G  |     items[i] = kEmptyHashValue;  | 
318  | 6.34k  | }  | 
319  |  |  | 
320  |  |  | 
321  |  | void MatchFinder_Init_3(CMatchFinder *p, int readData)  | 
322  | 6.34k  | { | 
323  | 6.34k  |   p->cyclicBufferPos = 0;  | 
324  | 6.34k  |   p->buffer = p->bufferBase;  | 
325  | 6.34k  |   p->pos =  | 
326  | 6.34k  |   p->streamPos = p->cyclicBufferSize;  | 
327  | 6.34k  |   p->result = SZ_OK;  | 
328  | 6.34k  |   p->streamEndWasReached = 0;  | 
329  |  |     | 
330  | 6.34k  |   if (readData)  | 
331  | 6.34k  |     MatchFinder_ReadBlock(p);  | 
332  |  |     | 
333  | 6.34k  |   MatchFinder_SetLimits(p);  | 
334  | 6.34k  | }  | 
335  |  |  | 
336  |  |  | 
337  |  | void MatchFinder_Init(CMatchFinder *p)  | 
338  | 6.34k  | { | 
339  | 6.34k  |   MatchFinder_Init_HighHash(p);  | 
340  | 6.34k  |   MatchFinder_Init_LowHash(p);  | 
341  | 6.34k  |   MatchFinder_Init_3(p, True);  | 
342  | 6.34k  | }  | 
343  |  |  | 
344  |  |     | 
345  |  | static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)  | 
346  | 0  | { | 
347  | 0  |   return (p->pos - p->historySize - 1) & kNormalizeMask;  | 
348  | 0  | }  | 
349  |  |  | 
350  |  | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)  | 
351  | 0  | { | 
352  | 0  |   size_t i;  | 
353  | 0  |   for (i = 0; i < numItems; i++)  | 
354  | 0  |   { | 
355  | 0  |     UInt32 value = items[i];  | 
356  | 0  |     if (value <= subValue)  | 
357  | 0  |       value = kEmptyHashValue;  | 
358  | 0  |     else  | 
359  | 0  |       value -= subValue;  | 
360  | 0  |     items[i] = value;  | 
361  | 0  |   }  | 
362  | 0  | }  | 
363  |  |  | 
364  |  | static void MatchFinder_Normalize(CMatchFinder *p)  | 
365  | 0  | { | 
366  | 0  |   UInt32 subValue = MatchFinder_GetSubValue(p);  | 
367  | 0  |   MatchFinder_Normalize3(subValue, p->hash, p->numRefs);  | 
368  | 0  |   MatchFinder_ReduceOffsets(p, subValue);  | 
369  | 0  | }  | 
370  |  |  | 
371  |  |  | 
372  |  | MY_NO_INLINE  | 
373  |  | static void MatchFinder_CheckLimits(CMatchFinder *p)  | 
374  | 805k  | { | 
375  | 805k  |   if (p->pos == kMaxValForNormalize)  | 
376  | 0  |     MatchFinder_Normalize(p);  | 
377  | 805k  |   if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)  | 
378  | 2.69k  |     MatchFinder_CheckAndMoveAndRead(p);  | 
379  | 805k  |   if (p->cyclicBufferPos == p->cyclicBufferSize)  | 
380  | 0  |     p->cyclicBufferPos = 0;  | 
381  | 805k  |   MatchFinder_SetLimits(p);  | 
382  | 805k  | }  | 
383  |  |  | 
384  |  |  | 
385  |  | /*  | 
386  |  |   (lenLimit > maxLen)  | 
387  |  | */  | 
388  |  | MY_FORCE_INLINE  | 
389  |  | static UInt32 * Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,  | 
390  |  |     UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,  | 
391  |  |     UInt32 *distances, unsigned maxLen)  | 
392  | 0  | { | 
393  |  |   /*  | 
394  |  |   son[_cyclicBufferPos] = curMatch;  | 
395  |  |   for (;;)  | 
396  |  |   { | 
397  |  |     UInt32 delta = pos - curMatch;  | 
398  |  |     if (cutValue-- == 0 || delta >= _cyclicBufferSize)  | 
399  |  |       return distances;  | 
400  |  |     { | 
401  |  |       const Byte *pb = cur - delta;  | 
402  |  |       curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];  | 
403  |  |       if (pb[maxLen] == cur[maxLen] && *pb == *cur)  | 
404  |  |       { | 
405  |  |         UInt32 len = 0;  | 
406  |  |         while (++len != lenLimit)  | 
407  |  |           if (pb[len] != cur[len])  | 
408  |  |             break;  | 
409  |  |         if (maxLen < len)  | 
410  |  |         { | 
411  |  |           maxLen = len;  | 
412  |  |           *distances++ = len;  | 
413  |  |           *distances++ = delta - 1;  | 
414  |  |           if (len == lenLimit)  | 
415  |  |             return distances;  | 
416  |  |         }  | 
417  |  |       }  | 
418  |  |     }  | 
419  |  |   }  | 
420  |  |   */  | 
421  |  | 
  | 
422  | 0  |   const Byte *lim = cur + lenLimit;  | 
423  | 0  |   son[_cyclicBufferPos] = curMatch;  | 
424  | 0  |   do  | 
425  | 0  |   { | 
426  | 0  |     UInt32 delta = pos - curMatch;  | 
427  | 0  |     if (delta >= _cyclicBufferSize)  | 
428  | 0  |       break;  | 
429  | 0  |     { | 
430  | 0  |       ptrdiff_t diff;  | 
431  | 0  |       curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];  | 
432  | 0  |       diff = (ptrdiff_t)0 - delta;  | 
433  | 0  |       if (cur[maxLen] == cur[maxLen + diff])  | 
434  | 0  |       { | 
435  | 0  |         const Byte *c = cur;  | 
436  | 0  |         while (*c == c[diff])  | 
437  | 0  |         { | 
438  | 0  |           if (++c == lim)  | 
439  | 0  |           { | 
440  | 0  |             distances[0] = (UInt32)(lim - cur);  | 
441  | 0  |             distances[1] = delta - 1;  | 
442  | 0  |             return distances + 2;  | 
443  | 0  |           }  | 
444  | 0  |         }  | 
445  | 0  |         { | 
446  | 0  |           unsigned len = (unsigned)(c - cur);  | 
447  | 0  |           if (maxLen < len)  | 
448  | 0  |           { | 
449  | 0  |             maxLen = len;  | 
450  | 0  |             distances[0] = (UInt32)len;  | 
451  | 0  |             distances[1] = delta - 1;  | 
452  | 0  |             distances += 2;  | 
453  | 0  |           }  | 
454  | 0  |         }  | 
455  | 0  |       }  | 
456  | 0  |     }  | 
457  | 0  |   }  | 
458  | 0  |   while (--cutValue);  | 
459  |  |     | 
460  | 0  |   return distances;  | 
461  | 0  | }  | 
462  |  |  | 
463  |  |  | 
464  |  | MY_FORCE_INLINE  | 
465  |  | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,  | 
466  |  |     UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,  | 
467  |  |     UInt32 *distances, UInt32 maxLen)  | 
468  | 264M  | { | 
469  | 264M  |   CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;  | 
470  | 264M  |   CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);  | 
471  | 264M  |   unsigned len0 = 0, len1 = 0;  | 
472  | 264M  |   for (;;)  | 
473  | 541M  |   { | 
474  | 541M  |     UInt32 delta = pos - curMatch;  | 
475  | 541M  |     if (cutValue-- == 0 || delta >= _cyclicBufferSize)  | 
476  | 264M  |     { | 
477  | 264M  |       *ptr0 = *ptr1 = kEmptyHashValue;  | 
478  | 264M  |       return distances;  | 
479  | 264M  |     }  | 
480  | 276M  |     { | 
481  | 276M  |       CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);  | 
482  | 276M  |       const Byte *pb = cur - delta;  | 
483  | 276M  |       unsigned len = (len0 < len1 ? len0 : len1);  | 
484  | 276M  |       UInt32 pair0 = pair[0];  | 
485  | 276M  |       if (pb[len] == cur[len])  | 
486  | 111M  |       { | 
487  | 111M  |         if (++len != lenLimit && pb[len] == cur[len])  | 
488  | 517M  |           while (++len != lenLimit)  | 
489  | 517M  |             if (pb[len] != cur[len])  | 
490  | 91.4M  |               break;  | 
491  | 111M  |         if (maxLen < len)  | 
492  | 34.2M  |         { | 
493  | 34.2M  |           maxLen = (UInt32)len;  | 
494  | 34.2M  |           *distances++ = (UInt32)len;  | 
495  | 34.2M  |           *distances++ = delta - 1;  | 
496  | 34.2M  |           if (len == lenLimit)  | 
497  | 405k  |           { | 
498  | 405k  |             *ptr1 = pair0;  | 
499  | 405k  |             *ptr0 = pair[1];  | 
500  | 405k  |             return distances;  | 
501  | 405k  |           }  | 
502  | 34.2M  |         }  | 
503  | 111M  |       }  | 
504  | 276M  |       if (pb[len] < cur[len])  | 
505  | 92.0M  |       { | 
506  | 92.0M  |         *ptr1 = curMatch;  | 
507  | 92.0M  |         ptr1 = pair + 1;  | 
508  | 92.0M  |         curMatch = *ptr1;  | 
509  | 92.0M  |         len1 = len;  | 
510  | 92.0M  |       }  | 
511  | 184M  |       else  | 
512  | 184M  |       { | 
513  | 184M  |         *ptr0 = curMatch;  | 
514  | 184M  |         ptr0 = pair;  | 
515  | 184M  |         curMatch = *ptr0;  | 
516  | 184M  |         len0 = len;  | 
517  | 184M  |       }  | 
518  | 276M  |     }  | 
519  | 276M  |   }  | 
520  | 264M  | }  | 
521  |  |  | 
522  |  | static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,  | 
523  |  |     UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)  | 
524  | 119M  | { | 
525  | 119M  |   CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;  | 
526  | 119M  |   CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);  | 
527  | 119M  |   unsigned len0 = 0, len1 = 0;  | 
528  | 119M  |   for (;;)  | 
529  | 507M  |   { | 
530  | 507M  |     UInt32 delta = pos - curMatch;  | 
531  | 507M  |     if (cutValue-- == 0 || delta >= _cyclicBufferSize)  | 
532  | 16.9M  |     { | 
533  | 16.9M  |       *ptr0 = *ptr1 = kEmptyHashValue;  | 
534  | 16.9M  |       return;  | 
535  | 16.9M  |     }  | 
536  | 490M  |     { | 
537  | 490M  |       CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);  | 
538  | 490M  |       const Byte *pb = cur - delta;  | 
539  | 490M  |       unsigned len = (len0 < len1 ? len0 : len1);  | 
540  | 490M  |       if (pb[len] == cur[len])  | 
541  | 304M  |       { | 
542  | 4.36G  |         while (++len != lenLimit)  | 
543  | 4.25G  |           if (pb[len] != cur[len])  | 
544  | 201M  |             break;  | 
545  | 304M  |         { | 
546  | 304M  |           if (len == lenLimit)  | 
547  | 102M  |           { | 
548  | 102M  |             *ptr1 = pair[0];  | 
549  | 102M  |             *ptr0 = pair[1];  | 
550  | 102M  |             return;  | 
551  | 102M  |           }  | 
552  | 304M  |         }  | 
553  | 304M  |       }  | 
554  | 388M  |       if (pb[len] < cur[len])  | 
555  | 111M  |       { | 
556  | 111M  |         *ptr1 = curMatch;  | 
557  | 111M  |         ptr1 = pair + 1;  | 
558  | 111M  |         curMatch = *ptr1;  | 
559  | 111M  |         len1 = len;  | 
560  | 111M  |       }  | 
561  | 277M  |       else  | 
562  | 277M  |       { | 
563  | 277M  |         *ptr0 = curMatch;  | 
564  | 277M  |         ptr0 = pair;  | 
565  | 277M  |         curMatch = *ptr0;  | 
566  | 277M  |         len0 = len;  | 
567  | 277M  |       }  | 
568  | 388M  |     }  | 
569  | 388M  |   }  | 
570  | 119M  | }  | 
571  |  |  | 
572  |  | #define MOVE_POS \  | 
573  | 384M  |   ++p->cyclicBufferPos; \  | 
574  | 384M  |   p->buffer++; \  | 
575  | 384M  |   if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);  | 
576  |  |  | 
577  | 265M  | #define MOVE_POS_RET MOVE_POS return (UInt32)offset;  | 
578  |  |  | 
579  | 19.0k  | static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; } | 
580  |  |  | 
581  |  | #define GET_MATCHES_HEADER2(minLen, ret_op) \  | 
582  | 384M  |   unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \  | 
583  | 384M  |   lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \ | 
584  | 384M  |   cur = p->buffer;  | 
585  |  |  | 
586  | 265M  | #define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)  | 
587  | 118M  | #define SKIP_HEADER(minLen)        GET_MATCHES_HEADER2(minLen, continue)  | 
588  |  |  | 
589  | 384M  | #define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue  | 
590  |  |  | 
591  |  | #define GET_MATCHES_FOOTER(offset, maxLen) \  | 
592  | 264M  |   offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \  | 
593  | 264M  |   distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET;  | 
594  |  |  | 
595  |  | #define SKIP_FOOTER \  | 
596  | 118M  |   SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;  | 
597  |  |  | 
598  | 71.4M  | #define UPDATE_maxLen { \ | 
599  | 71.4M  |     ptrdiff_t diff = (ptrdiff_t)0 - d2; \  | 
600  | 71.4M  |     const Byte *c = cur + maxLen; \  | 
601  | 71.4M  |     const Byte *lim = cur + lenLimit; \  | 
602  | 200M  |     for (; c != lim; c++) if (*(c + diff) != *c) break; \  | 
603  | 71.4M  |     maxLen = (unsigned)(c - cur); }  | 
604  |  |  | 
605  |  | static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
606  | 0  | { | 
607  | 0  |   unsigned offset;  | 
608  | 0  |   GET_MATCHES_HEADER(2)  | 
609  | 0  |   HASH2_CALC;  | 
610  | 0  |   curMatch = p->hash[hv];  | 
611  | 0  |   p->hash[hv] = p->pos;  | 
612  | 0  |   offset = 0;  | 
613  | 0  |   GET_MATCHES_FOOTER(offset, 1)  | 
614  | 0  | }  | 
615  |  |  | 
616  |  | UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
617  | 0  | { | 
618  | 0  |   unsigned offset;  | 
619  | 0  |   GET_MATCHES_HEADER(3)  | 
620  | 0  |   HASH_ZIP_CALC;  | 
621  | 0  |   curMatch = p->hash[hv];  | 
622  | 0  |   p->hash[hv] = p->pos;  | 
623  | 0  |   offset = 0;  | 
624  | 0  |   GET_MATCHES_FOOTER(offset, 2)  | 
625  | 0  | }  | 
626  |  |  | 
627  |  | static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
628  | 0  | { | 
629  | 0  |   UInt32 h2, d2, pos;  | 
630  | 0  |   unsigned maxLen, offset;  | 
631  | 0  |   UInt32 *hash;  | 
632  | 0  |   GET_MATCHES_HEADER(3)  | 
633  |  | 
  | 
634  | 0  |   HASH3_CALC;  | 
635  |  | 
  | 
636  | 0  |   hash = p->hash;  | 
637  | 0  |   pos = p->pos;  | 
638  |  | 
  | 
639  | 0  |   d2 = pos - hash[h2];  | 
640  |  | 
  | 
641  | 0  |   curMatch = (hash + kFix3HashSize)[hv];  | 
642  |  |     | 
643  | 0  |   hash[h2] = pos;  | 
644  | 0  |   (hash + kFix3HashSize)[hv] = pos;  | 
645  |  | 
  | 
646  | 0  |   maxLen = 2;  | 
647  | 0  |   offset = 0;  | 
648  |  | 
  | 
649  | 0  |   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)  | 
650  | 0  |   { | 
651  | 0  |     UPDATE_maxLen  | 
652  | 0  |     distances[0] = (UInt32)maxLen;  | 
653  | 0  |     distances[1] = d2 - 1;  | 
654  | 0  |     offset = 2;  | 
655  | 0  |     if (maxLen == lenLimit)  | 
656  | 0  |     { | 
657  | 0  |       SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));  | 
658  | 0  |       MOVE_POS_RET;  | 
659  | 0  |     }  | 
660  | 0  |   }  | 
661  |  |     | 
662  | 0  |   GET_MATCHES_FOOTER(offset, maxLen)  | 
663  | 0  | }  | 
664  |  |  | 
665  |  | static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
666  | 265M  | { | 
667  | 265M  |   UInt32 h2, h3, d2, d3, pos;  | 
668  | 265M  |   unsigned maxLen, offset;  | 
669  | 265M  |   UInt32 *hash;  | 
670  | 265M  |   GET_MATCHES_HEADER(4)  | 
671  |  |  | 
672  | 265M  |   HASH4_CALC;  | 
673  |  |  | 
674  | 265M  |   hash = p->hash;  | 
675  | 265M  |   pos = p->pos;  | 
676  |  |  | 
677  | 265M  |   d2 = pos - hash                  [h2];  | 
678  | 265M  |   d3 = pos - (hash + kFix3HashSize)[h3];  | 
679  |  |  | 
680  | 265M  |   curMatch = (hash + kFix4HashSize)[hv];  | 
681  |  |  | 
682  | 265M  |   hash                  [h2] = pos;  | 
683  | 265M  |   (hash + kFix3HashSize)[h3] = pos;  | 
684  | 265M  |   (hash + kFix4HashSize)[hv] = pos;  | 
685  |  |  | 
686  | 265M  |   maxLen = 0;  | 
687  | 265M  |   offset = 0;  | 
688  |  |     | 
689  | 265M  |   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)  | 
690  | 57.8M  |   { | 
691  | 57.8M  |     maxLen = 2;  | 
692  | 57.8M  |     distances[0] = 2;  | 
693  | 57.8M  |     distances[1] = d2 - 1;  | 
694  | 57.8M  |     offset = 2;  | 
695  | 57.8M  |   }  | 
696  |  |     | 
697  | 265M  |   if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
698  | 32.0M  |   { | 
699  | 32.0M  |     maxLen = 3;  | 
700  | 32.0M  |     distances[(size_t)offset + 1] = d3 - 1;  | 
701  | 32.0M  |     offset += 2;  | 
702  | 32.0M  |     d2 = d3;  | 
703  | 32.0M  |   }  | 
704  |  |     | 
705  | 265M  |   if (offset != 0)  | 
706  | 71.4M  |   { | 
707  | 71.4M  |     UPDATE_maxLen  | 
708  | 71.4M  |     distances[(size_t)offset - 2] = (UInt32)maxLen;  | 
709  | 71.4M  |     if (maxLen == lenLimit)  | 
710  | 532k  |     { | 
711  | 532k  |       SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));  | 
712  | 532k  |       MOVE_POS_RET;  | 
713  | 0  |     }  | 
714  | 71.4M  |   }  | 
715  |  |     | 
716  | 264M  |   if (maxLen < 3)  | 
717  | 209M  |     maxLen = 3;  | 
718  |  |     | 
719  | 264M  |   GET_MATCHES_FOOTER(offset, maxLen)  | 
720  | 0  | }  | 
721  |  |  | 
722  |  | /*  | 
723  |  | static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
724  |  | { | 
725  |  |   UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos;  | 
726  |  |   UInt32 *hash;  | 
727  |  |   GET_MATCHES_HEADER(5)  | 
728  |  |  | 
729  |  |   HASH5_CALC;  | 
730  |  |  | 
731  |  |   hash = p->hash;  | 
732  |  |   pos = p->pos;  | 
733  |  |  | 
734  |  |   d2 = pos - hash                  [h2];  | 
735  |  |   d3 = pos - (hash + kFix3HashSize)[h3];  | 
736  |  |   d4 = pos - (hash + kFix4HashSize)[h4];  | 
737  |  |  | 
738  |  |   curMatch = (hash + kFix5HashSize)[hv];  | 
739  |  |  | 
740  |  |   hash                  [h2] = pos;  | 
741  |  |   (hash + kFix3HashSize)[h3] = pos;  | 
742  |  |   (hash + kFix4HashSize)[h4] = pos;  | 
743  |  |   (hash + kFix5HashSize)[hv] = pos;  | 
744  |  |  | 
745  |  |   maxLen = 0;  | 
746  |  |   offset = 0;  | 
747  |  |  | 
748  |  |   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)  | 
749  |  |   { | 
750  |  |     distances[0] = maxLen = 2;  | 
751  |  |     distances[1] = d2 - 1;  | 
752  |  |     offset = 2;  | 
753  |  |     if (*(cur - d2 + 2) == cur[2])  | 
754  |  |       distances[0] = maxLen = 3;  | 
755  |  |     else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
756  |  |     { | 
757  |  |       distances[2] = maxLen = 3;  | 
758  |  |       distances[3] = d3 - 1;  | 
759  |  |       offset = 4;  | 
760  |  |       d2 = d3;  | 
761  |  |     }  | 
762  |  |   }  | 
763  |  |   else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
764  |  |   { | 
765  |  |     distances[0] = maxLen = 3;  | 
766  |  |     distances[1] = d3 - 1;  | 
767  |  |     offset = 2;  | 
768  |  |     d2 = d3;  | 
769  |  |   }  | 
770  |  |     | 
771  |  |   if (d2 != d4 && d4 < p->cyclicBufferSize  | 
772  |  |       && *(cur - d4) == *cur  | 
773  |  |       && *(cur - d4 + 3) == *(cur + 3))  | 
774  |  |   { | 
775  |  |     maxLen = 4;  | 
776  |  |     distances[(size_t)offset + 1] = d4 - 1;  | 
777  |  |     offset += 2;  | 
778  |  |     d2 = d4;  | 
779  |  |   }  | 
780  |  |     | 
781  |  |   if (offset != 0)  | 
782  |  |   { | 
783  |  |     UPDATE_maxLen  | 
784  |  |     distances[(size_t)offset - 2] = maxLen;  | 
785  |  |     if (maxLen == lenLimit)  | 
786  |  |     { | 
787  |  |       SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));  | 
788  |  |       MOVE_POS_RET;  | 
789  |  |     }  | 
790  |  |   }  | 
791  |  |  | 
792  |  |   if (maxLen < 4)  | 
793  |  |     maxLen = 4;  | 
794  |  |     | 
795  |  |   GET_MATCHES_FOOTER(offset, maxLen)  | 
796  |  | }  | 
797  |  | */  | 
798  |  |  | 
799  |  | static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
800  | 0  | { | 
801  | 0  |   UInt32 h2, h3, d2, d3, pos;  | 
802  | 0  |   unsigned maxLen, offset;  | 
803  | 0  |   UInt32 *hash;  | 
804  | 0  |   GET_MATCHES_HEADER(4)  | 
805  |  | 
  | 
806  | 0  |   HASH4_CALC;  | 
807  |  | 
  | 
808  | 0  |   hash = p->hash;  | 
809  | 0  |   pos = p->pos;  | 
810  |  |     | 
811  | 0  |   d2 = pos - hash                  [h2];  | 
812  | 0  |   d3 = pos - (hash + kFix3HashSize)[h3];  | 
813  | 0  |   curMatch = (hash + kFix4HashSize)[hv];  | 
814  |  | 
  | 
815  | 0  |   hash                  [h2] = pos;  | 
816  | 0  |   (hash + kFix3HashSize)[h3] = pos;  | 
817  | 0  |   (hash + kFix4HashSize)[hv] = pos;  | 
818  |  | 
  | 
819  | 0  |   maxLen = 0;  | 
820  | 0  |   offset = 0;  | 
821  |  | 
  | 
822  | 0  |   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)  | 
823  | 0  |   { | 
824  | 0  |     maxLen = 2;  | 
825  | 0  |     distances[0] = 2;  | 
826  | 0  |     distances[1] = d2 - 1;  | 
827  | 0  |     offset = 2;  | 
828  | 0  |   }  | 
829  |  |     | 
830  | 0  |   if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
831  | 0  |   { | 
832  | 0  |     maxLen = 3;  | 
833  | 0  |     distances[(size_t)offset + 1] = d3 - 1;  | 
834  | 0  |     offset += 2;  | 
835  | 0  |     d2 = d3;  | 
836  | 0  |   }  | 
837  |  |     | 
838  | 0  |   if (offset != 0)  | 
839  | 0  |   { | 
840  | 0  |     UPDATE_maxLen  | 
841  | 0  |     distances[(size_t)offset - 2] = (UInt32)maxLen;  | 
842  | 0  |     if (maxLen == lenLimit)  | 
843  | 0  |     { | 
844  | 0  |       p->son[p->cyclicBufferPos] = curMatch;  | 
845  | 0  |       MOVE_POS_RET;  | 
846  | 0  |     }  | 
847  | 0  |   }  | 
848  |  |     | 
849  | 0  |   if (maxLen < 3)  | 
850  | 0  |     maxLen = 3;  | 
851  |  | 
  | 
852  | 0  |   offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),  | 
853  | 0  |       distances + offset, maxLen) - (distances));  | 
854  | 0  |   MOVE_POS_RET  | 
855  | 0  | }  | 
856  |  |  | 
857  |  | /*  | 
858  |  | static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
859  |  | { | 
860  |  |   UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos  | 
861  |  |   UInt32 *hash;  | 
862  |  |   GET_MATCHES_HEADER(5)  | 
863  |  |  | 
864  |  |   HASH5_CALC;  | 
865  |  |  | 
866  |  |   hash = p->hash;  | 
867  |  |   pos = p->pos;  | 
868  |  |     | 
869  |  |   d2 = pos - hash                  [h2];  | 
870  |  |   d3 = pos - (hash + kFix3HashSize)[h3];  | 
871  |  |   d4 = pos - (hash + kFix4HashSize)[h4];  | 
872  |  |  | 
873  |  |   curMatch = (hash + kFix5HashSize)[hv];  | 
874  |  |  | 
875  |  |   hash                  [h2] = pos;  | 
876  |  |   (hash + kFix3HashSize)[h3] = pos;  | 
877  |  |   (hash + kFix4HashSize)[h4] = pos;  | 
878  |  |   (hash + kFix5HashSize)[hv] = pos;  | 
879  |  |  | 
880  |  |   maxLen = 0;  | 
881  |  |   offset = 0;  | 
882  |  |  | 
883  |  |   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)  | 
884  |  |   { | 
885  |  |     distances[0] = maxLen = 2;  | 
886  |  |     distances[1] = d2 - 1;  | 
887  |  |     offset = 2;  | 
888  |  |     if (*(cur - d2 + 2) == cur[2])  | 
889  |  |       distances[0] = maxLen = 3;  | 
890  |  |     else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
891  |  |     { | 
892  |  |       distances[2] = maxLen = 3;  | 
893  |  |       distances[3] = d3 - 1;  | 
894  |  |       offset = 4;  | 
895  |  |       d2 = d3;  | 
896  |  |     }  | 
897  |  |   }  | 
898  |  |   else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)  | 
899  |  |   { | 
900  |  |     distances[0] = maxLen = 3;  | 
901  |  |     distances[1] = d3 - 1;  | 
902  |  |     offset = 2;  | 
903  |  |     d2 = d3;  | 
904  |  |   }  | 
905  |  |     | 
906  |  |   if (d2 != d4 && d4 < p->cyclicBufferSize  | 
907  |  |       && *(cur - d4) == *cur  | 
908  |  |       && *(cur - d4 + 3) == *(cur + 3))  | 
909  |  |   { | 
910  |  |     maxLen = 4;  | 
911  |  |     distances[(size_t)offset + 1] = d4 - 1;  | 
912  |  |     offset += 2;  | 
913  |  |     d2 = d4;  | 
914  |  |   }  | 
915  |  |     | 
916  |  |   if (offset != 0)  | 
917  |  |   { | 
918  |  |     UPDATE_maxLen  | 
919  |  |     distances[(size_t)offset - 2] = maxLen;  | 
920  |  |     if (maxLen == lenLimit)  | 
921  |  |     { | 
922  |  |       p->son[p->cyclicBufferPos] = curMatch;  | 
923  |  |       MOVE_POS_RET;  | 
924  |  |     }  | 
925  |  |   }  | 
926  |  |     | 
927  |  |   if (maxLen < 4)  | 
928  |  |     maxLen = 4;  | 
929  |  |  | 
930  |  |   offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),  | 
931  |  |       distances + offset, maxLen) - (distances));  | 
932  |  |   MOVE_POS_RET  | 
933  |  | }  | 
934  |  | */  | 
935  |  |  | 
936  |  | UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)  | 
937  | 0  | { | 
938  | 0  |   unsigned offset;  | 
939  | 0  |   GET_MATCHES_HEADER(3)  | 
940  | 0  |   HASH_ZIP_CALC;  | 
941  | 0  |   curMatch = p->hash[hv];  | 
942  | 0  |   p->hash[hv] = p->pos;  | 
943  | 0  |   offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),  | 
944  | 0  |       distances, 2) - (distances));  | 
945  | 0  |   MOVE_POS_RET  | 
946  | 0  | }  | 
947  |  |  | 
948  |  | static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
949  | 0  | { | 
950  | 0  |   do  | 
951  | 0  |   { | 
952  | 0  |     SKIP_HEADER(2)  | 
953  | 0  |     HASH2_CALC;  | 
954  | 0  |     curMatch = p->hash[hv];  | 
955  | 0  |     p->hash[hv] = p->pos;  | 
956  | 0  |     SKIP_FOOTER  | 
957  | 0  |   }  | 
958  | 0  |   while (--num != 0);  | 
959  | 0  | }  | 
960  |  |  | 
961  |  | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
962  | 0  | { | 
963  | 0  |   do  | 
964  | 0  |   { | 
965  | 0  |     SKIP_HEADER(3)  | 
966  | 0  |     HASH_ZIP_CALC;  | 
967  | 0  |     curMatch = p->hash[hv];  | 
968  | 0  |     p->hash[hv] = p->pos;  | 
969  | 0  |     SKIP_FOOTER  | 
970  | 0  |   }  | 
971  | 0  |   while (--num != 0);  | 
972  | 0  | }  | 
973  |  |  | 
974  |  | static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
975  | 0  | { | 
976  | 0  |   do  | 
977  | 0  |   { | 
978  | 0  |     UInt32 h2;  | 
979  | 0  |     UInt32 *hash;  | 
980  | 0  |     SKIP_HEADER(3)  | 
981  | 0  |     HASH3_CALC;  | 
982  | 0  |     hash = p->hash;  | 
983  | 0  |     curMatch = (hash + kFix3HashSize)[hv];  | 
984  | 0  |     hash[h2] =  | 
985  | 0  |     (hash + kFix3HashSize)[hv] = p->pos;  | 
986  | 0  |     SKIP_FOOTER  | 
987  | 0  |   }  | 
988  | 0  |   while (--num != 0);  | 
989  | 0  | }  | 
990  |  |  | 
991  |  | static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
992  | 934k  | { | 
993  | 934k  |   do  | 
994  | 118M  |   { | 
995  | 118M  |     UInt32 h2, h3;  | 
996  | 118M  |     UInt32 *hash;  | 
997  | 118M  |     SKIP_HEADER(4)  | 
998  | 118M  |     HASH4_CALC;  | 
999  | 118M  |     hash = p->hash;  | 
1000  | 118M  |     curMatch = (hash + kFix4HashSize)[hv];  | 
1001  | 118M  |     hash                  [h2] =  | 
1002  | 118M  |     (hash + kFix3HashSize)[h3] =  | 
1003  | 118M  |     (hash + kFix4HashSize)[hv] = p->pos;  | 
1004  | 118M  |     SKIP_FOOTER  | 
1005  | 118M  |   }  | 
1006  | 118M  |   while (--num != 0);  | 
1007  | 934k  | }  | 
1008  |  |  | 
1009  |  | /*  | 
1010  |  | static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
1011  |  | { | 
1012  |  |   do  | 
1013  |  |   { | 
1014  |  |     UInt32 h2, h3, h4;  | 
1015  |  |     UInt32 *hash;  | 
1016  |  |     SKIP_HEADER(5)  | 
1017  |  |     HASH5_CALC;  | 
1018  |  |     hash = p->hash;  | 
1019  |  |     curMatch = (hash + kFix5HashSize)[hv];  | 
1020  |  |     hash                  [h2] =  | 
1021  |  |     (hash + kFix3HashSize)[h3] =  | 
1022  |  |     (hash + kFix4HashSize)[h4] =  | 
1023  |  |     (hash + kFix5HashSize)[hv] = p->pos;  | 
1024  |  |     SKIP_FOOTER  | 
1025  |  |   }  | 
1026  |  |   while (--num != 0);  | 
1027  |  | }  | 
1028  |  | */  | 
1029  |  |  | 
1030  |  | static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
1031  | 0  | { | 
1032  | 0  |   do  | 
1033  | 0  |   { | 
1034  | 0  |     UInt32 h2, h3;  | 
1035  | 0  |     UInt32 *hash;  | 
1036  | 0  |     SKIP_HEADER(4)  | 
1037  | 0  |     HASH4_CALC;  | 
1038  | 0  |     hash = p->hash;  | 
1039  | 0  |     curMatch = (hash + kFix4HashSize)[hv];  | 
1040  | 0  |     hash                  [h2] =  | 
1041  | 0  |     (hash + kFix3HashSize)[h3] =  | 
1042  | 0  |     (hash + kFix4HashSize)[hv] = p->pos;  | 
1043  | 0  |     p->son[p->cyclicBufferPos] = curMatch;  | 
1044  | 0  |     MOVE_POS  | 
1045  | 0  |   }  | 
1046  | 0  |   while (--num != 0);  | 
1047  | 0  | }  | 
1048  |  |  | 
1049  |  | /*  | 
1050  |  | static void Hc5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
1051  |  | { | 
1052  |  |   do  | 
1053  |  |   { | 
1054  |  |     UInt32 h2, h3, h4;  | 
1055  |  |     UInt32 *hash;  | 
1056  |  |     SKIP_HEADER(5)  | 
1057  |  |     HASH5_CALC;  | 
1058  |  |     hash = p->hash;  | 
1059  |  |     curMatch = hash + kFix5HashSize)[hv];  | 
1060  |  |     hash                  [h2] =  | 
1061  |  |     (hash + kFix3HashSize)[h3] =  | 
1062  |  |     (hash + kFix4HashSize)[h4] =  | 
1063  |  |     (hash + kFix5HashSize)[hv] = p->pos;  | 
1064  |  |     p->son[p->cyclicBufferPos] = curMatch;  | 
1065  |  |     MOVE_POS  | 
1066  |  |   }  | 
1067  |  |   while (--num != 0);  | 
1068  |  | }  | 
1069  |  | */  | 
1070  |  |  | 
1071  |  | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)  | 
1072  | 0  | { | 
1073  | 0  |   do  | 
1074  | 0  |   { | 
1075  | 0  |     SKIP_HEADER(3)  | 
1076  | 0  |     HASH_ZIP_CALC;  | 
1077  | 0  |     curMatch = p->hash[hv];  | 
1078  | 0  |     p->hash[hv] = p->pos;  | 
1079  | 0  |     p->son[p->cyclicBufferPos] = curMatch;  | 
1080  | 0  |     MOVE_POS  | 
1081  | 0  |   }  | 
1082  | 0  |   while (--num != 0);  | 
1083  | 0  | }  | 
1084  |  |  | 
1085  |  | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)  | 
1086  | 6.34k  | { | 
1087  | 6.34k  |   vTable->Init = (Mf_Init_Func)MatchFinder_Init;  | 
1088  | 6.34k  |   vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;  | 
1089  | 6.34k  |   vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;  | 
1090  | 6.34k  |   if (!p->btMode)  | 
1091  | 0  |   { | 
1092  |  |     /* if (p->numHashBytes <= 4) */  | 
1093  | 0  |     { | 
1094  | 0  |       vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;  | 
1095  | 0  |       vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;  | 
1096  | 0  |     }  | 
1097  |  |     /*  | 
1098  |  |     else  | 
1099  |  |     { | 
1100  |  |       vTable->GetMatches = (Mf_GetMatches_Func)Hc5_MatchFinder_GetMatches;  | 
1101  |  |       vTable->Skip = (Mf_Skip_Func)Hc5_MatchFinder_Skip;  | 
1102  |  |     }  | 
1103  |  |     */  | 
1104  | 0  |   }  | 
1105  | 6.34k  |   else if (p->numHashBytes == 2)  | 
1106  | 0  |   { | 
1107  | 0  |     vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;  | 
1108  | 0  |     vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;  | 
1109  | 0  |   }  | 
1110  | 6.34k  |   else if (p->numHashBytes == 3)  | 
1111  | 0  |   { | 
1112  | 0  |     vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;  | 
1113  | 0  |     vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;  | 
1114  | 0  |   }  | 
1115  | 6.34k  |   else /* if (p->numHashBytes == 4) */  | 
1116  | 6.34k  |   { | 
1117  | 6.34k  |     vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;  | 
1118  | 6.34k  |     vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;  | 
1119  | 6.34k  |   }  | 
1120  |  |   /*  | 
1121  |  |   else  | 
1122  |  |   { | 
1123  |  |     vTable->GetMatches = (Mf_GetMatches_Func)Bt5_MatchFinder_GetMatches;  | 
1124  |  |     vTable->Skip = (Mf_Skip_Func)Bt5_MatchFinder_Skip;  | 
1125  |  |   }  | 
1126  |  |   */  | 
1127  | 6.34k  | }  |