]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4JavaSafeFastDecompressor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4JavaSafeFastDecompressor.java
1 // Auto-generated: DO NOT EDIT
2
3 package net.jpountz.lz4;
4
5 import static net.jpountz.lz4.LZ4Constants.*;
6
7 import java.nio.ByteBuffer;
8
9 import net.jpountz.util.ByteBufferUtils;
10 import net.jpountz.util.SafeUtils;
11
12 /**
13  * Decompressor.
14  */
15 final class LZ4JavaSafeFastDecompressor extends LZ4FastDecompressor {
16
17   public static final LZ4FastDecompressor INSTANCE = new LZ4JavaSafeFastDecompressor();
18
19   @Override
20   public int decompress(byte[] src, final int srcOff, byte[] dest, final int destOff, int destLen) {
21
22
23     SafeUtils.checkRange(src, srcOff);
24     SafeUtils.checkRange(dest, destOff, destLen);
25
26     if (destLen == 0) {
27       if (SafeUtils.readByte(src, srcOff) != 0) {
28         throw new LZ4Exception("Malformed input at " + srcOff);
29       }
30       return 1;
31     }
32
33
34     final int destEnd = destOff + destLen;
35
36     int sOff = srcOff;
37     int dOff = destOff;
38
39     while (true) {
40       final int token = SafeUtils.readByte(src, sOff) & 0xFF;
41       ++sOff;
42
43       // literals
44       int literalLen = token >>> ML_BITS;
45       if (literalLen == RUN_MASK) {
46         byte len = (byte) 0xFF;
47         while ((len = SafeUtils.readByte(src, sOff++)) == (byte) 0xFF) {
48           literalLen += 0xFF;
49         }
50         literalLen += len & 0xFF;
51       }
52
53       final int literalCopyEnd = dOff + literalLen;
54
55       if (literalCopyEnd > destEnd - COPY_LENGTH) {
56         if (literalCopyEnd != destEnd) {
57           throw new LZ4Exception("Malformed input at " + sOff);
58
59         } else {
60           LZ4SafeUtils.safeArraycopy(src, sOff, dest, dOff, literalLen);
61           sOff += literalLen;
62           dOff = literalCopyEnd;
63           break; // EOF
64         }
65       }
66
67       LZ4SafeUtils.wildArraycopy(src, sOff, dest, dOff, literalLen);
68       sOff += literalLen;
69       dOff = literalCopyEnd;
70
71       // matchs
72       final int matchDec = SafeUtils.readShortLE(src, sOff);
73       sOff += 2;
74       int matchOff = dOff - matchDec;
75
76       if (matchOff < destOff) {
77         throw new LZ4Exception("Malformed input at " + sOff);
78       }
79
80       int matchLen = token & ML_MASK;
81       if (matchLen == ML_MASK) {
82         byte len = (byte) 0xFF;
83         while ((len = SafeUtils.readByte(src, sOff++)) == (byte) 0xFF) {
84           matchLen += 0xFF;
85         }
86         matchLen += len & 0xFF;
87       }
88       matchLen += MIN_MATCH;
89
90       final int matchCopyEnd = dOff + matchLen;
91
92       if (matchCopyEnd > destEnd - COPY_LENGTH) {
93         if (matchCopyEnd > destEnd) {
94           throw new LZ4Exception("Malformed input at " + sOff);
95         }
96         LZ4SafeUtils.safeIncrementalCopy(dest, matchOff, dOff, matchLen);
97       } else {
98         LZ4SafeUtils.wildIncrementalCopy(dest, matchOff, dOff, matchCopyEnd);
99       }
100       dOff = matchCopyEnd;
101     }
102
103
104     return sOff - srcOff;
105
106   }
107
108   @Override
109   public int decompress(ByteBuffer src, final int srcOff, ByteBuffer dest, final int destOff, int destLen) {
110
111     if (src.hasArray() && dest.hasArray()) {
112       return decompress(src.array(), srcOff + src.arrayOffset(), dest.array(), destOff + dest.arrayOffset(), destLen);
113     }
114     src = ByteBufferUtils.inNativeByteOrder(src);
115     dest = ByteBufferUtils.inNativeByteOrder(dest);
116
117
118     ByteBufferUtils.checkRange(src, srcOff);
119     ByteBufferUtils.checkRange(dest, destOff, destLen);
120
121     if (destLen == 0) {
122       if (ByteBufferUtils.readByte(src, srcOff) != 0) {
123         throw new LZ4Exception("Malformed input at " + srcOff);
124       }
125       return 1;
126     }
127
128
129     final int destEnd = destOff + destLen;
130
131     int sOff = srcOff;
132     int dOff = destOff;
133
134     while (true) {
135       final int token = ByteBufferUtils.readByte(src, sOff) & 0xFF;
136       ++sOff;
137
138       // literals
139       int literalLen = token >>> ML_BITS;
140       if (literalLen == RUN_MASK) {
141         byte len = (byte) 0xFF;
142         while ((len = ByteBufferUtils.readByte(src, sOff++)) == (byte) 0xFF) {
143           literalLen += 0xFF;
144         }
145         literalLen += len & 0xFF;
146       }
147
148       final int literalCopyEnd = dOff + literalLen;
149
150       if (literalCopyEnd > destEnd - COPY_LENGTH) {
151         if (literalCopyEnd != destEnd) {
152           throw new LZ4Exception("Malformed input at " + sOff);
153
154         } else {
155           LZ4ByteBufferUtils.safeArraycopy(src, sOff, dest, dOff, literalLen);
156           sOff += literalLen;
157           dOff = literalCopyEnd;
158           break; // EOF
159         }
160       }
161
162       LZ4ByteBufferUtils.wildArraycopy(src, sOff, dest, dOff, literalLen);
163       sOff += literalLen;
164       dOff = literalCopyEnd;
165
166       // matchs
167       final int matchDec = ByteBufferUtils.readShortLE(src, sOff);
168       sOff += 2;
169       int matchOff = dOff - matchDec;
170
171       if (matchOff < destOff) {
172         throw new LZ4Exception("Malformed input at " + sOff);
173       }
174
175       int matchLen = token & ML_MASK;
176       if (matchLen == ML_MASK) {
177         byte len = (byte) 0xFF;
178         while ((len = ByteBufferUtils.readByte(src, sOff++)) == (byte) 0xFF) {
179           matchLen += 0xFF;
180         }
181         matchLen += len & 0xFF;
182       }
183       matchLen += MIN_MATCH;
184
185       final int matchCopyEnd = dOff + matchLen;
186
187       if (matchCopyEnd > destEnd - COPY_LENGTH) {
188         if (matchCopyEnd > destEnd) {
189           throw new LZ4Exception("Malformed input at " + sOff);
190         }
191         LZ4ByteBufferUtils.safeIncrementalCopy(dest, matchOff, dOff, matchLen);
192       } else {
193         LZ4ByteBufferUtils.wildIncrementalCopy(dest, matchOff, dOff, matchCopyEnd);
194       }
195       dOff = matchCopyEnd;
196     }
197
198
199     return sOff - srcOff;
200
201   }
202
203
204 }
205