Exemple #1
0
  private void validate() throws IOException {
    long compressedSize = inCounted.getSize();

    // Validate Compressed Size and Uncompressed Size if they were
    // present in Block Header.
    if ((compressedSizeInHeader != -1 && compressedSizeInHeader != compressedSize)
        || (uncompressedSizeInHeader != -1 && uncompressedSizeInHeader != uncompressedSize))
      throw new CorruptedInputException();

    // Block Padding bytes must be zeros.
    while ((compressedSize++ & 3) != 0)
      if (inData.readUnsignedByte() != 0x00) throw new CorruptedInputException();

    // Validate the integrity check.
    byte[] storedCheck = new byte[check.getSize()];
    inData.readFully(storedCheck);
    if (!Arrays.equals(check.finish(), storedCheck))
      throw new CorruptedInputException("Integrity check (" + check.getName() + ") does not match");
  }
Exemple #2
0
  @Override
  public int read(byte[] buf, int off, int len) throws IOException {
    if (endReached) return -1;

    int ret = filterChain.read(buf, off, len);

    if (ret > 0) {
      check.update(buf, off, ret);
      uncompressedSize += ret;

      // Catch invalid values.
      long compressedSize = inCounted.getSize();
      if (compressedSize < 0
          || compressedSize > compressedSizeLimit
          || uncompressedSize < 0
          || (uncompressedSizeInHeader != -1 && uncompressedSize > uncompressedSizeInHeader))
        throw new CorruptedInputException();

      // Check the Block integrity as soon as possible:
      //   - The filter chain shouldn't return less than requested
      //     unless it hit the end of the input.
      //   - If the uncompressed size is known, we know when there
      //     shouldn't be more data coming. We still need to read
      //     one byte to let the filter chain catch errors and to
      //     let it read end of payload marker(s).
      if (ret < len || uncompressedSize == uncompressedSizeInHeader) {
        if (filterChain.read() != -1) throw new CorruptedInputException();

        validate();
        endReached = true;
      }
    } else if (ret == -1) {
      validate();
      endReached = true;
    }

    return ret;
  }
Exemple #3
0
 public long getUnpaddedSize() {
   return headerSize + inCounted.getSize() + check.getSize();
 }