1 // Auto-generated: DO NOT EDIT
3 package net.jpountz.lz4;
5 import static net.jpountz.lz4.LZ4Constants.*;
7 import java.nio.ByteBuffer;
9 import net.jpountz.util.ByteBufferUtils;
10 import net.jpountz.util.SafeUtils;
15 final class LZ4JavaSafeSafeDecompressor extends LZ4SafeDecompressor {
17 public static final LZ4SafeDecompressor INSTANCE = new LZ4JavaSafeSafeDecompressor();
20 public int decompress(byte[] src, final int srcOff, final int srcLen , byte[] dest, final int destOff, int destLen) {
23 SafeUtils.checkRange(src, srcOff, srcLen);
24 SafeUtils.checkRange(dest, destOff, destLen);
27 if (srcLen != 1 || SafeUtils.readByte(src, srcOff) != 0) {
28 throw new LZ4Exception("Output buffer too small");
33 final int srcEnd = srcOff + srcLen;
36 final int destEnd = destOff + destLen;
42 final int token = SafeUtils.readByte(src, sOff) & 0xFF;
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) {
52 literalLen += len & 0xFF;
55 final int literalCopyEnd = dOff + literalLen;
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);
64 LZ4SafeUtils.safeArraycopy(src, sOff, dest, dOff, literalLen);
66 dOff = literalCopyEnd;
71 LZ4SafeUtils.wildArraycopy(src, sOff, dest, dOff, literalLen);
73 dOff = literalCopyEnd;
76 final int matchDec = SafeUtils.readShortLE(src, sOff);
78 int matchOff = dOff - matchDec;
80 if (matchOff < destOff) {
81 throw new LZ4Exception("Malformed input at " + sOff);
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) {
90 matchLen += len & 0xFF;
92 matchLen += MIN_MATCH;
94 final int matchCopyEnd = dOff + matchLen;
96 if (matchCopyEnd > destEnd - COPY_LENGTH) {
97 if (matchCopyEnd > destEnd) {
98 throw new LZ4Exception("Malformed input at " + sOff);
100 LZ4SafeUtils.safeIncrementalCopy(dest, matchOff, dOff, matchLen);
102 LZ4SafeUtils.wildIncrementalCopy(dest, matchOff, dOff, matchCopyEnd);
108 return dOff - destOff;
113 public int decompress(ByteBuffer src, final int srcOff, final int srcLen , ByteBuffer dest, final int destOff, int destLen) {
115 if (src.hasArray() && dest.hasArray()) {
116 return decompress(src.array(), srcOff + src.arrayOffset(), srcLen, dest.array(), destOff + dest.arrayOffset(), destLen);
118 src = ByteBufferUtils.inNativeByteOrder(src);
119 dest = ByteBufferUtils.inNativeByteOrder(dest);
122 ByteBufferUtils.checkRange(src, srcOff, srcLen);
123 ByteBufferUtils.checkRange(dest, destOff, destLen);
126 if (srcLen != 1 || ByteBufferUtils.readByte(src, srcOff) != 0) {
127 throw new LZ4Exception("Output buffer too small");
132 final int srcEnd = srcOff + srcLen;
135 final int destEnd = destOff + destLen;
141 final int token = ByteBufferUtils.readByte(src, sOff) & 0xFF;
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) {
151 literalLen += len & 0xFF;
154 final int literalCopyEnd = dOff + literalLen;
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);
163 LZ4ByteBufferUtils.safeArraycopy(src, sOff, dest, dOff, literalLen);
165 dOff = literalCopyEnd;
170 LZ4ByteBufferUtils.wildArraycopy(src, sOff, dest, dOff, literalLen);
172 dOff = literalCopyEnd;
175 final int matchDec = ByteBufferUtils.readShortLE(src, sOff);
177 int matchOff = dOff - matchDec;
179 if (matchOff < destOff) {
180 throw new LZ4Exception("Malformed input at " + sOff);
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) {
189 matchLen += len & 0xFF;
191 matchLen += MIN_MATCH;
193 final int matchCopyEnd = dOff + matchLen;
195 if (matchCopyEnd > destEnd - COPY_LENGTH) {
196 if (matchCopyEnd > destEnd) {
197 throw new LZ4Exception("Malformed input at " + sOff);
199 LZ4ByteBufferUtils.safeIncrementalCopy(dest, matchOff, dOff, matchLen);
201 LZ4ByteBufferUtils.wildIncrementalCopy(dest, matchOff, dOff, matchCopyEnd);
207 return dOff - destOff;