Debian Codegraph @

src/org/tukaani/xz/lz/Hash234.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.lz;
7
8 import org.tukaani.xz.ArrayCache;
9
10 // 2-, 3-, and 4-byte hashing
11 final class Hash234 extends CRC32Hash {
12 private static final int HASH_2_SIZE = 1 << 10;
13 private static final int HASH_2_MASK = HASH_2_SIZE - 1;
14
15 private static final int HASH_3_SIZE = 1 << 16;
16 private static final int HASH_3_MASK = HASH_3_SIZE - 1;
17
18 private final int hash4Mask;
19
20 private final int[] hash2Table;
21 private final int[] hash3Table;
22 private final int[] hash4Table;
23 private final int hash4Size;
24
25 private int hash2Value = 0;
26 private int hash3Value = 0;
27 private int hash4Value = 0;
28
29 static int getHash4Size(int dictSize) {
30 int h = dictSize - 1;
31 h |= h >>> 1;
32 h |= h >>> 2;
33 h |= h >>> 4;
34 h |= h >>> 8;
35 h >>>= 1;
36 h |= 0xFFFF;
37 if (h > (1 << 24))
38 h >>>= 1;
39
40 return h + 1;
41 }
42
43 static int getMemoryUsage(int dictSize) {
44 // Sizes of the hash arrays + a little extra
45 return (HASH_2_SIZE + HASH_3_SIZE + getHash4Size(dictSize))
46 / (1024 / 4) + 4;
47 }
48
49 Hash234(int dictSize, ArrayCache arrayCache) {
50 hash2Table = arrayCache.getIntArray(HASH_2_SIZE, true);
51 hash3Table = arrayCache.getIntArray(HASH_3_SIZE, true);
52
53 hash4Size = getHash4Size(dictSize);
54 hash4Table = arrayCache.getIntArray(hash4Size, true);
55 hash4Mask = hash4Size - 1;
56 }
57
58 void putArraysToCache(ArrayCache arrayCache) {
59 arrayCache.putArray(hash4Table);
60 arrayCache.putArray(hash3Table);
61 arrayCache.putArray(hash2Table);
62 }
63
64 void calcHashes(byte[] buf, int off) {
65 int temp = crcTable[buf[off] & 0xFF] ^ (buf[off + 1] & 0xFF);
66 hash2Value = temp & HASH_2_MASK;
67
68 temp ^= (buf[off + 2] & 0xFF) << 8;
69 hash3Value = temp & HASH_3_MASK;
70
71 temp ^= crcTable[buf[off + 3] & 0xFF] << 5;
72 hash4Value = temp & hash4Mask;
73 }
74
75 int getHash2Pos() {
76 return hash2Table[hash2Value];
77 }
78
79 int getHash3Pos() {
80 return hash3Table[hash3Value];
81 }
82
83 int getHash4Pos() {
84 return hash4Table[hash4Value];
85 }
86
87 void updateTables(int pos) {
88 hash2Table[hash2Value] = pos;
89 hash3Table[hash3Value] = pos;
90 hash4Table[hash4Value] = pos;
91 }
92
93 void normalize(int normalizationOffset) {
94 LZEncoder.normalize(hash2Table, HASH_2_SIZE, normalizationOffset);
95 LZEncoder.normalize(hash3Table, HASH_3_SIZE, normalizationOffset);
96 LZEncoder.normalize(hash4Table, hash4Size, normalizationOffset);
97 }
98 }