| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.lz; |
| 7 |
|
| 8 |
import org.tukaani.xz.ArrayCache; |
| 9 |
|
| 10 |
|
| 11 |
final class HC4 extends LZEncoder { |
| 12 |
private final Hash234 hash; |
| 13 |
private final int[] chain; |
| 14 |
private final Matches matches; |
| 15 |
private final int depthLimit; |
| 16 |
|
| 17 |
private final int cyclicSize; |
| 18 |
private int cyclicPos = -1; |
| 19 |
private int lzPos; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
static int getMemoryUsage(int dictSize) { |
| 25 |
return Hash234.getMemoryUsage(dictSize) + dictSize / (1024 / 4) + 10; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
HC4(int dictSize, int beforeSizeMin, int readAheadMax, |
| 33 |
int niceLen, int matchLenMax, int depthLimit, |
| 34 |
ArrayCache arrayCache) { |
| 35 |
super(dictSize, beforeSizeMin, readAheadMax, niceLen, matchLenMax, |
| 36 |
arrayCache); |
| 37 |
|
| 38 |
hash = new Hash234(dictSize, arrayCache); |
| 39 |
|
| 40 |
|
| 41 |
cyclicSize = dictSize + 1; |
| 42 |
chain = arrayCache.getIntArray(cyclicSize, false); |
| 43 |
lzPos = cyclicSize; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
matches = new Matches(niceLen - 1); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
this.depthLimit = (depthLimit > 0) ? depthLimit : 4 + niceLen / 4; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public void putArraysToCache(ArrayCache arrayCache) { |
| 58 |
arrayCache.putArray(chain); |
| 59 |
hash.putArraysToCache(arrayCache); |
| 60 |
super.putArraysToCache(arrayCache); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private int movePos() { |
| 70 |
int avail = movePos(4, 4); |
| 71 |
|
| 72 |
if (avail != 0) { |
| 73 |
if (++lzPos == Integer.MAX_VALUE) { |
| 74 |
int normalizationOffset = Integer.MAX_VALUE - cyclicSize; |
| 75 |
hash.normalize(normalizationOffset); |
| 76 |
normalize(chain, cyclicSize, normalizationOffset); |
| 77 |
lzPos -= normalizationOffset; |
| 78 |
} |
| 79 |
|
| 80 |
if (++cyclicPos == cyclicSize) |
| 81 |
cyclicPos = 0; |
| 82 |
} |
| 83 |
|
| 84 |
return avail; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Matches getMatches() { |
| 89 |
matches.count = 0; |
| 90 |
int matchLenLimit = matchLenMax; |
| 91 |
int niceLenLimit = niceLen; |
| 92 |
int avail = movePos(); |
| 93 |
|
| 94 |
if (avail < matchLenLimit) { |
| 95 |
if (avail == 0) |
| 96 |
return matches; |
| 97 |
|
| 98 |
matchLenLimit = avail; |
| 99 |
if (niceLenLimit > avail) |
| 100 |
niceLenLimit = avail; |
| 101 |
} |
| 102 |
|
| 103 |
hash.calcHashes(buf, readPos); |
| 104 |
int delta2 = lzPos - hash.getHash2Pos(); |
| 105 |
int delta3 = lzPos - hash.getHash3Pos(); |
| 106 |
int currentMatch = hash.getHash4Pos(); |
| 107 |
hash.updateTables(lzPos); |
| 108 |
|
| 109 |
chain[cyclicPos] = currentMatch; |
| 110 |
|
| 111 |
int lenBest = 0; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (delta2 < cyclicSize && buf[readPos - delta2] == buf[readPos]) { |
| 118 |
lenBest = 2; |
| 119 |
matches.len[0] = 2; |
| 120 |
matches.dist[0] = delta2 - 1; |
| 121 |
matches.count = 1; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if (delta2 != delta3 && delta3 < cyclicSize |
| 129 |
&& buf[readPos - delta3] == buf[readPos]) { |
| 130 |
lenBest = 3; |
| 131 |
matches.dist[matches.count++] = delta3 - 1; |
| 132 |
delta2 = delta3; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (matches.count > 0) { |
| 137 |
lenBest = MatchLength.getLen(buf, readPos, delta2, |
| 138 |
lenBest, matchLenLimit); |
| 139 |
matches.len[matches.count - 1] = lenBest; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (lenBest >= niceLenLimit) |
| 144 |
return matches; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (lenBest < 3) |
| 150 |
lenBest = 3; |
| 151 |
|
| 152 |
int depth = depthLimit; |
| 153 |
|
| 154 |
while (true) { |
| 155 |
int delta = lzPos - currentMatch; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if (depth-- == 0 || delta >= cyclicSize) |
| 161 |
return matches; |
| 162 |
|
| 163 |
currentMatch = chain[cyclicPos - delta |
| 164 |
+ (delta > cyclicPos ? cyclicSize : 0)]; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if (buf[readPos + lenBest - delta] == buf[readPos + lenBest] |
| 170 |
&& buf[readPos - delta] == buf[readPos]) { |
| 171 |
|
| 172 |
int len = MatchLength.getLen(buf, readPos, delta, |
| 173 |
1, matchLenLimit); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (len > lenBest) { |
| 178 |
lenBest = len; |
| 179 |
matches.len[matches.count] = len; |
| 180 |
matches.dist[matches.count] = delta - 1; |
| 181 |
++matches.count; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if (len >= niceLenLimit) |
| 186 |
return matches; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void skip(int len) { |
| 194 |
assert len >= 0; |
| 195 |
|
| 196 |
while (len-- > 0) { |
| 197 |
if (movePos() != 0) { |
| 198 |
|
| 199 |
hash.calcHashes(buf, readPos); |
| 200 |
chain[cyclicPos] = hash.getHash4Pos(); |
| 201 |
hash.updateTables(lzPos); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |