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