private static void checkByte(byte actual, byte expect) { if (actual != expect) { throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy " + "protocol version?"); } }
static DecompressionException inflaterException(Inflater z, String message, int resultCode) { return new DecompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : "")); }
private static void checkByte(byte actual, byte expect) { if (actual != expect) { throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy " + "protocol version?"); } }
/** * Validates that the offset extracted from a compressed reference is within * the permissible bounds of an offset (0 < offset < Integer.MAX_VALUE), and does not * exceed the length of the chunk currently read so far. * * @param offset The offset extracted from the compressed reference * @param chunkSizeSoFar The number of bytes read so far from this chunk * @throws DecompressionException if the offset is invalid */ private static void validateOffset(int offset, int chunkSizeSoFar) { if (offset == 0) { throw new DecompressionException("Offset is less than minimum permissible value"); } if (offset < 0) { // Due to arithmetic overflow throw new DecompressionException("Offset is greater than maximum value supported by this implementation"); } if (offset > chunkSizeSoFar) { throw new DecompressionException("Offset exceeds size of chunk"); } }
static DecompressionException inflaterException(Inflater z, String message, int resultCode) { return new DecompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : "")); }
/** * Set up the Inverse Burrows-Wheeler Transform merged pointer array. */ private void initialiseInverseBWT() { final int bwtStartPointer = this.bwtStartPointer; final byte[] bwtBlock = this.bwtBlock; final int[] bwtMergedPointers = new int[bwtBlockLength]; final int[] characterBase = new int[256]; if (bwtStartPointer < 0 || bwtStartPointer >= bwtBlockLength) { throw new DecompressionException("start pointer invalid"); } // Cumulative character counts System.arraycopy(bwtByteCounts, 0, characterBase, 1, 255); for (int i = 2; i <= 255; i++) { characterBase[i] += characterBase[i - 1]; } // Merged-Array Inverse Burrows-Wheeler Transform // Combining the output characters and forward pointers into a single array here, where we // have already read both of the corresponding values, cuts down on memory accesses in the // final walk through the array for (int i = 0; i < bwtBlockLength; i++) { int value = bwtBlock[i] & 0xff; bwtMergedPointers[characterBase[value]++] = (i << 8) + value; } this.bwtMergedPointers = bwtMergedPointers; bwtCurrentMergedPointer = bwtMergedPointers[bwtStartPointer]; }
/** * Verify and return the block CRC. This method may only be called * after all of the block's bytes have been read. * @return The block CRC */ int checkCRC() { final int computedBlockCRC = crc.getCRC(); if (blockCRC != computedBlockCRC) { throw new DecompressionException("block CRC error"); } return computedBlockCRC; } }
/** * Computes the CRC32C checksum of the supplied data, performs the "mask" operation * on the computed checksum, and then compares the resulting masked checksum to the * supplied checksum. * * @param expectedChecksum The checksum decoded from the stream to compare against * @param data The input data to calculate the CRC32C checksum of * @throws DecompressionException If the calculated and supplied checksums do not match */ static void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int length) { final int actualChecksum = calculateChecksum(data, offset, length); if (actualChecksum != expectedChecksum) { throw new DecompressionException( "mismatching checksum: " + Integer.toHexString(actualChecksum) + " (expected: " + Integer.toHexString(expectedChecksum) + ')'); } }
/** * Verify and return the block CRC. This method may only be called * after all of the block's bytes have been read. * @return The block CRC */ int checkCRC() { final int computedBlockCRC = crc.getCRC(); if (blockCRC != computedBlockCRC) { throw new DecompressionException("block CRC error"); } return computedBlockCRC; } }
/** * Reads the length varint (a series of bytes, where the lower 7 bits * are data and the upper bit is a flag to indicate more bytes to be * read). * * @param in The input buffer to read the preamble from * @return The calculated length based on the input buffer, or 0 if * no preamble is able to be calculated */ private static int readPreamble(ByteBuf in) { int length = 0; int byteIndex = 0; while (in.isReadable()) { int current = in.readUnsignedByte(); length |= (current & 0x7f) << byteIndex++ * 7; if ((current & 0x80) == 0) { return length; } if (byteIndex >= 4) { throw new DecompressionException("Preamble is greater than 4 bytes"); } } return 0; }
static void checkChecksum(ByteBufChecksum checksum, ByteBuf uncompressed, int currentChecksum) { checksum.reset(); checksum.update(uncompressed, uncompressed.readerIndex(), uncompressed.readableBytes()); final int checksumResult = (int) checksum.getValue(); if (checksumResult != currentChecksum) { throw new DecompressionException(String.format( "stream corrupted: mismatching checksum: %d (expected: %d)", checksumResult, currentChecksum)); } }
/** * Computes the CRC32C checksum of the supplied data, performs the "mask" operation * on the computed checksum, and then compares the resulting masked checksum to the * supplied checksum. * * @param expectedChecksum The checksum decoded from the stream to compare against * @param data The input data to calculate the CRC32C checksum of * @throws DecompressionException If the calculated and supplied checksums do not match */ static void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int length) { final int actualChecksum = calculateChecksum(data, offset, length); if (actualChecksum != expectedChecksum) { throw new DecompressionException( "mismatching checksum: " + Integer.toHexString(actualChecksum) + " (expected: " + Integer.toHexString(expectedChecksum) + ')'); } }
groupIndex++; if (groupIndex == selectors.length) { throw new DecompressionException("error decoding block"); throw new DecompressionException("a valid code was not recognised");
private void verifyCrc(ByteBuf in) { long crcValue = 0; for (int i = 0; i < 4; ++i) { crcValue |= (long) in.readUnsignedByte() << i * 8; } long readCrc = crc.getValue(); if (crcValue != readCrc) { throw new DecompressionException( "CRC value mismatch. Expected: " + crcValue + ", Got: " + readCrc); } }
static void checkChecksum(ByteBufChecksum checksum, ByteBuf uncompressed, int currentChecksum) { checksum.reset(); checksum.update(uncompressed, uncompressed.readerIndex(), uncompressed.readableBytes()); final int checksumResult = (int) checksum.getValue(); if (checksumResult != currentChecksum) { throw new DecompressionException(String.format( "stream corrupted: mismatching checksum: %d (expected: %d)", checksumResult, currentChecksum)); } }
private void verifyCrc(ByteBuf in) { long crcValue = 0; for (int i = 0; i < 4; ++i) { crcValue |= (long) in.readUnsignedByte() << i * 8; } long readCrc = crc.getValue(); if (crcValue != readCrc) { throw new DecompressionException( "CRC value mismatch. Expected: " + crcValue + ", Got: " + readCrc); } }
private boolean readGZIPFooter(ByteBuf buf) { if (buf.readableBytes() < 8) { return false; } verifyCrc(buf); // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) { dataLength |= buf.readUnsignedByte() << i * 8; } int readLength = inflater.getTotalOut(); if (dataLength != readLength) { throw new DecompressionException( "Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); } return true; }
static void checkChecksum(ByteBufChecksum checksum, ByteBuf uncompressed, int currentChecksum) { checksum.reset(); checksum.update(uncompressed, uncompressed.readerIndex(), uncompressed.readableBytes()); final int checksumResult = (int) checksum.getValue(); if (checksumResult != currentChecksum) { throw new DecompressionException(String.format( "stream corrupted: mismatching checksum: %d (expected: %d)", checksumResult, currentChecksum)); } }
private void verifyCrc(ByteBuf in) { long crcValue = 0; for (int i = 0; i < 4; ++i) { crcValue |= (long) in.readUnsignedByte() << i * 8; } long readCrc = crc.getValue(); if (crcValue != readCrc) { throw new DecompressionException( "CRC value mismatch. Expected: " + crcValue + ", Got: " + readCrc); } }
private boolean readGZIPFooter(ByteBuf buf) { if (buf.readableBytes() < 8) { return false; } verifyCrc(buf); // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) { dataLength |= buf.readUnsignedByte() << i * 8; } int readLength = inflater.getTotalOut(); if (dataLength != readLength) { throw new DecompressionException( "Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); } return true; }