|
51 | 51 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
52 | 52 | */ |
53 | 53 | public class Lz4FrameDecoder extends ByteToMessageDecoder { |
| 54 | + private final int maxDecompressedLength; |
54 | 55 | /** |
55 | 56 | * Current state of stream. |
56 | 57 | */ |
@@ -117,6 +118,21 @@ public Lz4FrameDecoder(boolean validateChecksums) { |
117 | 118 | this(LZ4Factory.fastestInstance(), validateChecksums); |
118 | 119 | } |
119 | 120 |
|
| 121 | + /** |
| 122 | + * Creates a LZ4 decoder with fastest decoder instance available on your machine. |
| 123 | + * |
| 124 | + * @param validateChecksums if {@code true}, the checksum field will be validated against the actual |
| 125 | + * uncompressed data, and if the checksums do not match, a suitable |
| 126 | + * {@link DecompressionException} will be thrown |
| 127 | + * @param maxDecompressedLength |
| 128 | + * maximum length of the decompressed block. If {@code 0} is given it uses {@code 32MB} |
| 129 | + * by default. |
| 130 | + */ |
| 131 | + public Lz4FrameDecoder(boolean validateChecksums, int maxDecompressedLength) { |
| 132 | + this(LZ4Factory.fastestInstance(), validateChecksums ? new Lz4XXHash32(DEFAULT_SEED) : null, |
| 133 | + maxDecompressedLength); |
| 134 | + } |
| 135 | + |
120 | 136 | /** |
121 | 137 | * Creates a new LZ4 decoder with customizable implementation. |
122 | 138 | * |
@@ -143,8 +159,25 @@ public Lz4FrameDecoder(LZ4Factory factory, boolean validateChecksums) { |
143 | 159 | * You may set {@code null} if you do not want to validate checksum of each block |
144 | 160 | */ |
145 | 161 | public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) { |
| 162 | + this(factory, checksum, MAX_BLOCK_SIZE); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Creates a new customizable LZ4 decoder. |
| 167 | + * |
| 168 | + * @param factory user customizable {@link LZ4Factory} instance |
| 169 | + * which may be JNI bindings to the original C implementation, a pure Java implementation |
| 170 | + * or a Java implementation that uses the {@link sun.misc.Unsafe} |
| 171 | + * @param checksum the {@link Checksum} instance to use to check data for integrity. |
| 172 | + * You may set {@code null} if you do not want to validate checksum of each block |
| 173 | + * @param maxDecompressedLength |
| 174 | + * maximum length of the decompressed block. If {@code 0} is given it uses {@code 32MB} by default. |
| 175 | + */ |
| 176 | + public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum, int maxDecompressedLength) { |
146 | 177 | decompressor = ObjectUtil.checkNotNull(factory, "factory").fastDecompressor(); |
147 | 178 | this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum); |
| 179 | + this.maxDecompressedLength = maxDecompressedLength == 0 ? MAX_BLOCK_SIZE : |
| 180 | + ObjectUtil.checkInRange(maxDecompressedLength, 0, MAX_BLOCK_SIZE, "maxDecompressedLength"); |
148 | 181 | } |
149 | 182 |
|
150 | 183 | @Override |
@@ -172,12 +205,18 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t |
172 | 205 | } |
173 | 206 |
|
174 | 207 | int decompressedLength = Integer.reverseBytes(in.readInt()); |
175 | | - final int maxDecompressedLength = 1 << compressionLevel; |
176 | | - if (decompressedLength < 0 || decompressedLength > maxDecompressedLength) { |
| 208 | + if (decompressedLength > maxDecompressedLength) { |
177 | 209 | throw new DecompressionException(String.format( |
178 | | - "invalid decompressedLength: %d (expected: 0-%d)", |
| 210 | + "decompressedLength too large: %d (expected: 0-%d)", |
179 | 211 | decompressedLength, maxDecompressedLength)); |
180 | 212 | } |
| 213 | + |
| 214 | + final int maxLocalDecompressedLength = 1 << compressionLevel; |
| 215 | + if (decompressedLength < 0 || decompressedLength > maxLocalDecompressedLength) { |
| 216 | + throw new DecompressionException(String.format( |
| 217 | + "invalid decompressedLength: %d (expected: 0-%d)", |
| 218 | + decompressedLength, maxLocalDecompressedLength)); |
| 219 | + } |
181 | 220 | if (decompressedLength == 0 && compressedLength != 0 |
182 | 221 | || decompressedLength != 0 && compressedLength == 0 |
183 | 222 | || blockType == BLOCK_TYPE_NON_COMPRESSED && decompressedLength != compressedLength) { |
|