Debian Codegraph @

plugins/wcx/zip/src/lzma/compression/LZ/ULZBinTree.pas

raw
1 unit ULZBinTree;
2
3 {$IFDEF FPC}
4 {$MODE Delphi}
5 {$ENDIF}
6
7 interface
8
9 uses ULZInWindow,Math;
10
11 type TLZBinTree=class(TLZInWindow)
12 public
13 cyclicBufferPos:integer;
14 cyclicBufferSize:integer;
15 matchMaxLen:integer;
16
17 son: array of integer;
18 hash: array of integer;
19
20 cutValue:integer;
21 hashMask:integer;
22 hashSizeSum:integer;
23
24 HASH_ARRAY:boolean;
25
26
27 kNumHashDirectBytes:integer;
28 kMinMatchCheck:integer;
29 kFixHashSize:integer;
30 constructor Create;
31 procedure SetType(const numHashBytes:integer);
32 procedure Init;override;
33 procedure MovePos;override;
34 function _Create(const historySize,keepAddBufferBefore,matchMaxLen,keepAddBufferAfter:integer):boolean;reintroduce;
35 function GetMatches(var distances:array of integer):integer;
36 procedure Skip(num:integer);
37 procedure NormalizeLinks(var items:array of integer;const numItems,subValue:integer);
38 procedure Normalize;
39 procedure SetCutValue(const cutValue:integer);
40 end;
41
42 implementation
43
44 const kHash2Size = 1 shl 10;
45 kHash3Size = 1 shl 16;
46 kBT2HashSize = 1 shl 16;
47 kStartMaxLen = 1;
48 kHash3Offset = kHash2Size;
49 kEmptyHashValue = 0;
50 kMaxValForNormalize = (1 shl 30) - 1;
51
52 var CRCTable: array [0..255] of integer;
53
54 constructor TLZBinTree.Create;
55 begin
56 inherited Create;
57 cyclicBufferSize:=0;
58 cutValue:=$FF;
59 hashSizeSum:=0;
60 HASH_ARRAY:=true;
61 kNumHashDirectBytes:=0;
62 kMinMatchCheck:=4;
63 kFixHashsize:=kHash2Size + kHash3Size;
64 end;
65
66 procedure TLZBinTree.SetType(const numHashBytes:integer);
67 begin
68 HASH_ARRAY := (numHashBytes > 2);
69 if HASH_ARRAY then begin
70 kNumHashDirectBytes := 0;
71 kMinMatchCheck := 4;
72 kFixHashSize := kHash2Size + kHash3Size;
73 end
74 else begin
75 kNumHashDirectBytes := 2;
76 kMinMatchCheck := 2 + 1;
77 kFixHashSize := 0;
78 end;
79 end;
80
81 procedure TLZBinTree.Init;
82 var i:integer;
83 begin
84 inherited init;
85 for i := 0 to hashSizeSum - 1 do
86 hash[i] := kEmptyHashValue;
87 cyclicBufferPos := 0;
88 ReduceOffsets(-1);
89 end;
90
91 procedure TLZBinTree.MovePos;
92 begin
93 inc(cyclicBufferPos);
94 if cyclicBufferPos >= cyclicBufferSize then
95 cyclicBufferPos := 0;
96 inherited MovePos;
97 if pos = kMaxValForNormalize then
98 Normalize;
99 end;
100
101 function TLZBinTree._Create(const historySize,keepAddBufferBefore,matchMaxLen,keepAddBufferAfter:integer):boolean;
102 var windowReservSize:integer;
103 cyclicBufferSize:integer;
104 hs:integer;
105 begin
106 if (historySize > kMaxValForNormalize - 256) then begin
107 result:=false;
108 exit;
109 end;
110 cutValue := 16 + (matchMaxLen shr 1);
111
112 windowReservSize := (historySize + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) div 2 + 256;
113
114 inherited _Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
115
116 self.matchMaxLen := matchMaxLen;
117
118 cyclicBufferSize := historySize + 1;
119 if self.cyclicBufferSize <> cyclicBufferSize then begin
120 self.cyclicBufferSize:=cyclicBufferSize;
121 setlength(son,cyclicBufferSize * 2);
122 end;
123
124 hs := kBT2HashSize;
125
126 if HASH_ARRAY then begin
127 hs := historySize - 1;
128 hs := hs or (hs shr 1);
129 hs := hs or (hs shr 2);
130 hs := hs or (hs shr 4);
131 hs := hs or (hs shr 8);
132 hs := hs shr 1;
133 hs := hs or $FFFF;
134 if (hs > (1 shl 24)) then
135 hs := hs shr 1;
136 hashMask := hs;
137 inc(hs);
138 hs := hs + kFixHashSize;
139 end;
140 if (hs <> hashSizeSum) then begin
141 hashSizeSum := hs;
142 setlength(hash,hashSizeSum);
143 end;
144 result:=true;
145 end;
146
147 function TLZBinTree.GetMatches(var distances:array of integer):integer;
148 var lenLimit:integer;
149 offset,matchMinPos,cur,maxlen,hashvalue,hash2value,hash3value:integer;
150 temp,curmatch,curmatch2,curmatch3,ptr0,ptr1,len0,len1,count:integer;
151 delta,cyclicpos,pby1,len:integer;
152 begin
153 if pos + matchMaxLen <= streamPos then
154 lenLimit := matchMaxLen
155 else begin
156 lenLimit := streamPos - pos;
157 if lenLimit < kMinMatchCheck then begin
158 MovePos();
159 result:=0;
160 exit;
161 end;
162 end;
163
164 offset := 0;
165 if (pos > cyclicBufferSize) then
166 matchMinPos:=(pos - cyclicBufferSize)
167 else matchMinPos:=0;
168 cur := bufferOffset + pos;
169 maxLen := kStartMaxLen; // to avoid items for len < hashSize;
170 hash2Value := 0;
171 hash3Value := 0;
172
173 if HASH_ARRAY then begin
174 temp := CrcTable[bufferBase[cur] and $FF] xor (bufferBase[cur + 1] and $FF);
175 hash2Value := temp and (kHash2Size - 1);
176 temp := temp xor ((bufferBase[cur + 2] and $FF) shl 8);
177 hash3Value := temp and (kHash3Size - 1);
178 hashValue := (temp xor (CrcTable[bufferBase[cur + 3] and $FF] shl 5)) and hashMask;
179 end else
180 hashValue := ((bufferBase[cur] and $FF) xor ((bufferBase[cur + 1] and $FF) shl 8));
181
182 curMatch := hash[kFixHashSize + hashValue];
183 if HASH_ARRAY then begin
184 curMatch2 := hash[hash2Value];
185 curMatch3 := hash[kHash3Offset + hash3Value];
186 hash[hash2Value] := pos;
187 hash[kHash3Offset + hash3Value] := pos;
188 if curMatch2 > matchMinPos then
189 if bufferBase[bufferOffset + curMatch2] = bufferBase[cur] then begin
190 maxLen := 2;
191 distances[offset] := maxLen;
192 inc(offset);
193 distances[offset] := pos - curMatch2 - 1;
194 inc(offset);
195 end;
196 if curMatch3 > matchMinPos then
197 if bufferBase[bufferOffset + curMatch3] = bufferBase[cur] then begin
198 if curMatch3 = curMatch2 then
199 offset := offset - 2;
200 maxLen := 3;
201 distances[offset] := maxlen;
202 inc(offset);
203 distances[offset] := pos - curMatch3 - 1;
204 inc(offset);
205 curMatch2 := curMatch3;
206 end;
207 if (offset <> 0) and (curMatch2 = curMatch) then begin
208 offset := offset - 2;
209 maxLen := kStartMaxLen;
210 end;
211 end;
212
213 hash[kFixHashSize + hashValue] := pos;
214
215 ptr0 := (cyclicBufferPos shl 1) + 1;
216 ptr1 := (cyclicBufferPos shl 1);
217
218 len0 := kNumHashDirectBytes;
219 len1 := len0;
220
221 if kNumHashDirectBytes <> 0 then begin
222 if (curMatch > matchMinPos) then begin
223 if (bufferBase[bufferOffset + curMatch + kNumHashDirectBytes] <> bufferBase[cur + kNumHashDirectBytes]) then begin
224 maxLen := kNumHashDirectBytes;
225 distances[offset] := maxLen;
226 inc(offset);
227 distances[offset] := pos - curMatch - 1;
228 inc(offset);
229 end;
230 end;
231 end;
232
233 count := cutValue;
234
235 while (true) do begin
236 if (curMatch <= matchMinPos) or (count = 0) then begin
237 son[ptr1] := kEmptyHashValue;
238 son[ptr0] := son[ptr1];
239 break;
240 end;
241 dec(count);
242 delta := pos - curMatch;
243 if delta<=cyclicBufferPos then
244 cyclicpos:=(cyclicBufferPos - delta) shl 1
245 else cyclicpos:=(cyclicBufferPos - delta + cyclicBufferSize) shl 1;
246
247 pby1 := bufferOffset + curMatch;
248 len := min(len0, len1);
249 if bufferBase[pby1 + len] = bufferBase[cur + len] then begin
250 inc(len);
251 while (len <> lenLimit) do begin
252 if (bufferBase[pby1 + len] <> bufferBase[cur + len]) then
253 break;
254 inc(len);
255 end;
256 if maxLen < len then begin
257 maxLen := len;
258 distances[offset] := maxlen;
259 inc(offset);
260 distances[offset] := delta - 1;
261 inc(offset);
262 if (len = lenLimit) then begin
263 son[ptr1] := son[cyclicPos];
264 son[ptr0] := son[cyclicPos + 1];
265 break;
266 end;
267 end;
268 end;
269 if (bufferBase[pby1 + len] and $FF) < (bufferBase[cur + len] and $FF) then begin
270 son[ptr1] := curMatch;
271 ptr1 := cyclicPos + 1;
272 curMatch := son[ptr1];
273 len1 := len;
274 end else begin
275 son[ptr0] := curMatch;
276 ptr0 := cyclicPos;
277 curMatch := son[ptr0];
278 len0 := len;
279 end;
280 end;
281 MovePos;
282 result:=offset;
283 end;
284
285 procedure TLZBinTree.Skip(num:integer);
286 var lenLimit,matchminpos,cur,hashvalue,temp,hash2value,hash3value,curMatch:integer;
287 ptr0,ptr1,len,len0,len1,count,delta,cyclicpos,pby1:integer;
288 begin
289 repeat
290 if pos + matchMaxLen <= streamPos then
291 lenLimit := matchMaxLen
292 else begin
293 lenLimit := streamPos - pos;
294 if lenLimit < kMinMatchCheck then begin
295 MovePos();
296 dec(num);
297 continue;
298 end;
299 end;
300
301 if pos>cyclicBufferSize then
302 matchminpos:=(pos - cyclicBufferSize)
303 else matchminpos:=0;
304 cur := bufferOffset + pos;
305
306 if HASH_ARRAY then begin
307 temp := CrcTable[bufferBase[cur] and $FF] xor (bufferBase[cur + 1] and $FF);
308 hash2Value := temp and (kHash2Size - 1);
309 hash[hash2Value] := pos;
310 temp := temp xor ((bufferBase[cur + 2] and $FF) shl 8);
311 hash3Value := temp and (kHash3Size - 1);
312 hash[kHash3Offset + hash3Value] := pos;
313 hashValue := (temp xor (CrcTable[bufferBase[cur + 3] and $FF] shl 5)) and hashMask;
314 end else
315 hashValue := ((bufferBase[cur] and $FF) xor ((bufferBase[cur + 1] and $FF) shl 8));
316
317 curMatch := hash[kFixHashSize + hashValue];
318 hash[kFixHashSize + hashValue] := pos;
319
320 ptr0 := (cyclicBufferPos shl 1) + 1;
321 ptr1 := (cyclicBufferPos shl 1);
322
323 len0 := kNumHashDirectBytes;
324 len1 := kNumHashDirectBytes;
325
326 count := cutValue;
327 while true do begin
328 if (curMatch <= matchMinPos) or (count = 0) then begin
329 son[ptr1] := kEmptyHashValue;
330 son[ptr0] := son[ptr1];
331 break;
332 end else dec(count);
333
334 delta := pos - curMatch;
335 if (delta <= cyclicBufferPos) then
336 cyclicpos:=(cyclicBufferPos - delta) shl 1
337 else cyclicpos:=(cyclicBufferPos - delta + cyclicBufferSize) shl 1;
338
339 pby1 := bufferOffset + curMatch;
340 len := min(len0, len1);
341 if bufferBase[pby1 + len] = bufferBase[cur + len] then begin
342 inc(len);
343 while (len <> lenLimit) do begin
344 if bufferBase[pby1 + len] <> bufferBase[cur + len] then
345 break;
346 inc(len);
347 end;
348 if len = lenLimit then begin
349 son[ptr1] := son[cyclicPos];
350 son[ptr0] := son[cyclicPos + 1];
351 break;
352 end;
353 end;
354 if ((bufferBase[pby1 + len] and $FF) < (bufferBase[cur + len] and $FF)) then begin
355 son[ptr1] := curMatch;
356 ptr1 := cyclicPos + 1;
357 curMatch := son[ptr1];
358 len1 := len;
359 end else begin
360 son[ptr0] := curMatch;
361 ptr0 := cyclicPos;
362 curMatch := son[ptr0];
363 len0 := len;
364 end;
365 end;
366 MovePos;
367 dec(num);
368 until num=0;
369 end;
370
371 procedure TLZBinTree.NormalizeLinks(var items:array of integer;const numItems,subValue:integer);
372 var i,value:integer;
373 begin
374 for i:=0 to NumItems-1 do begin
375 value := items[i];
376 if value <= subValue then
377 value := kEmptyHashValue
378 else value := value - subValue;
379 items[i] := value;
380 end;
381 end;
382
383 procedure TLZBinTree.Normalize;
384 var subvalue:integer;
385 begin
386 subValue := pos - cyclicBufferSize;
387 NormalizeLinks(son, cyclicBufferSize * 2, subValue);
388 NormalizeLinks(hash, hashSizeSum, subValue);
389 ReduceOffsets(subValue);
390 end;
391
392 procedure TLZBinTree.SetCutValue(const cutvalue:integer);
393 begin
394 self.cutValue:=cutValue;
395 end;
396
397 procedure InitCRC;
398 var i,r,j:integer;
399 begin
400 for i := 0 to 255 do begin
401 r := i;
402 for j := 0 to 7 do
403 if ((r and 1) <> 0) then
404 r := (r shr 1) xor integer($EDB88320)
405 else
406 r := r shr 1;
407 CrcTable[i] := r;
408 end;
409 end;
410
411 initialization
412 InitCRC;
413 end.