]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4Utils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4Utils.java
1 package net.jpountz.lz4;
2
3 /*
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import static net.jpountz.lz4.LZ4Constants.HASH_LOG;
18 import static net.jpountz.lz4.LZ4Constants.HASH_LOG_64K;
19 import static net.jpountz.lz4.LZ4Constants.HASH_LOG_HC;
20 import static net.jpountz.lz4.LZ4Constants.MIN_MATCH;
21
22 enum LZ4Utils {
23   ;
24
25   private static final int MAX_INPUT_SIZE = 0x7E000000;
26
27   static int maxCompressedLength(int length) {
28     if (length < 0) {
29       throw new IllegalArgumentException("length must be >= 0, got " + length);
30     } else if (length >= MAX_INPUT_SIZE) {
31         throw new IllegalArgumentException("length must be < " + MAX_INPUT_SIZE);
32     }
33     return length + length / 255 + 16;
34   }
35
36   static int hash(int i) {
37     return (i * -1640531535) >>> ((MIN_MATCH * 8) - HASH_LOG);
38   }
39
40   static int hash64k(int i) {
41     return (i * -1640531535) >>> ((MIN_MATCH * 8) - HASH_LOG_64K);
42   }
43
44   static int hashHC(int i) {
45     return (i * -1640531535) >>> ((MIN_MATCH * 8) - HASH_LOG_HC);
46   }
47
48   static class Match {
49     int start, ref, len;
50
51     void fix(int correction) {
52       start += correction;
53       ref += correction;
54       len -= correction;
55     }
56
57     int end() {
58       return start + len;
59     }
60   }
61
62   static void copyTo(Match m1, Match m2) {
63     m2.len = m1.len;
64     m2.start = m1.start;
65     m2.ref = m1.ref;
66   }
67
68 }