Search in sources :

Example 1 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project hbase by apache.

the class TestChecksum method testAllChecksumTypes.

/**
   * Test all checksum types by writing and reading back blocks.
   */
@Test
public void testAllChecksumTypes() throws IOException {
    List<ChecksumType> cktypes = new ArrayList<>(Arrays.asList(ChecksumType.values()));
    for (Iterator<ChecksumType> itr = cktypes.iterator(); itr.hasNext(); ) {
        ChecksumType cktype = itr.next();
        Path path = new Path(TEST_UTIL.getDataTestDir(), "checksum" + cktype.getName());
        FSDataOutputStream os = fs.create(path);
        HFileContext meta = new HFileContextBuilder().withChecksumType(cktype).build();
        HFileBlock.Writer hbw = new HFileBlock.Writer(null, meta);
        DataOutputStream dos = hbw.startWriting(BlockType.DATA);
        for (int i = 0; i < 1000; ++i) {
            dos.writeInt(i);
        }
        hbw.writeHeaderAndData(os);
        int totalSize = hbw.getOnDiskSizeWithHeader();
        os.close();
        // Use hbase checksums.
        assertEquals(true, hfs.useHBaseChecksum());
        FSDataInputStreamWrapper is = new FSDataInputStreamWrapper(fs, path);
        meta = new HFileContextBuilder().withHBaseCheckSum(true).build();
        HFileBlock.FSReader hbr = new HFileBlock.FSReaderImpl(is, totalSize, (HFileSystem) fs, path, meta);
        HFileBlock b = hbr.readBlockData(0, -1, false);
        ByteBuff data = b.getBufferWithoutHeader();
        for (int i = 0; i < 1000; i++) {
            assertEquals(i, data.getInt());
        }
        boolean exception_thrown = false;
        try {
            data.getInt();
        } catch (BufferUnderflowException e) {
            exception_thrown = true;
        }
        assertTrue(exception_thrown);
        assertEquals(0, HFile.getChecksumFailuresCount());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) ChecksumType(org.apache.hadoop.hbase.util.ChecksumType) FSDataInputStreamWrapper(org.apache.hadoop.hbase.io.FSDataInputStreamWrapper) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) ByteBuff(org.apache.hadoop.hbase.nio.ByteBuff) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 2 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project jvm-serializers by eishay.

the class Image method unmarshal.

/**
	 * Deserializes the object.
	 * @param buf the data source.
	 * @param offset the initial index for {@code buf}, inclusive.
	 * @return the final index for {@code buf}, exclusive.
	 * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
	 * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
	 * @throws InputMismatchException when the data does not match this object's schema.
	 */
public int unmarshal(byte[] buf, int offset) {
    int i = offset;
    try {
        byte header = buf[i++];
        if (header == (byte) 0) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.image.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.uri = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 1) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.image.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.title = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 2) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = x;
            header = buf[i++];
        } else if (header == (byte) (2 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = -x;
            header = buf[i++];
        }
        if (header == (byte) 3) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = x;
            header = buf[i++];
        } else if (header == (byte) (3 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = -x;
            header = buf[i++];
        }
        if (header == (byte) 4) {
            this.small = true;
            header = buf[i++];
        }
        if (header == (byte) 5) {
            this.large = true;
            header = buf[i++];
        }
        if (header != (byte) 0x7f)
            throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
    } catch (IndexOutOfBoundsException e) {
        if (i - offset > colferSizeMax)
            throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
        if (i >= buf.length)
            throw new BufferUnderflowException();
        throw new RuntimeException("colfer: bug", e);
    }
    if (i - offset > colferSizeMax)
        throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
    return i;
}
Also used : InputMismatchException(java.util.InputMismatchException) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 3 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project walle by Meituan-Dianping.

the class V2SchemeVerifier method parseSigners.

/**
     * Parses each signer in the provided APK Signature Scheme v2 block and populates
     * {@code signerInfos} of the provided {@code result}.
     *
     * <p>This verifies signatures over {@code signed-data} block contained in each signer block.
     * However, this does not verify the integrity of the rest of the APK but rather simply reports
     * the expected digests of the rest of the APK (see {@code contentDigestsToVerify}).
     */
private static void parseSigners(ByteBuffer apkSignatureSchemeV2Block, Set<ContentDigestAlgorithm> contentDigestsToVerify, Result result) {
    ByteBuffer signers;
    try {
        signers = getLengthPrefixedSlice(apkSignatureSchemeV2Block);
    } catch (IOException e) {
        result.addError(Issue.V2_SIG_MALFORMED_SIGNERS);
        return;
    }
    if (!signers.hasRemaining()) {
        result.addError(Issue.V2_SIG_NO_SIGNERS);
        return;
    }
    CertificateFactory certFactory;
    try {
        certFactory = CertificateFactory.getInstance("X.509");
    } catch (CertificateException e) {
        throw new RuntimeException("Failed to obtain X.509 CertificateFactory", e);
    }
    int signerCount = 0;
    while (signers.hasRemaining()) {
        int signerIndex = signerCount;
        signerCount++;
        Result.SignerInfo signerInfo = new Result.SignerInfo();
        signerInfo.index = signerIndex;
        result.signers.add(signerInfo);
        try {
            ByteBuffer signer = getLengthPrefixedSlice(signers);
            parseSigner(signer, certFactory, signerInfo, contentDigestsToVerify);
        } catch (IOException | BufferUnderflowException e) {
            signerInfo.addError(Issue.V2_SIG_MALFORMED_SIGNER);
            return;
        }
    }
}
Also used : CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CertificateFactory(java.security.cert.CertificateFactory) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 4 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project walle by Meituan-Dianping.

the class V2SchemeVerifier method parseSigner.

/**
     * Parses the provided signer block and populates the {@code result}.
     *
     * <p>This verifies signatures over {@code signed-data} contained in this block but does not
     * verify the integrity of the rest of the APK. Rather, this method adds to the
     * {@code contentDigestsToVerify}.
     */
private static void parseSigner(ByteBuffer signerBlock, CertificateFactory certFactory, Result.SignerInfo result, Set<ContentDigestAlgorithm> contentDigestsToVerify) throws IOException {
    ByteBuffer signedData = getLengthPrefixedSlice(signerBlock);
    byte[] signedDataBytes = new byte[signedData.remaining()];
    signedData.get(signedDataBytes);
    signedData.flip();
    result.signedData = signedDataBytes;
    ByteBuffer signatures = getLengthPrefixedSlice(signerBlock);
    byte[] publicKeyBytes = readLengthPrefixedByteArray(signerBlock);
    // Parse the signatures block and identify supported signatures
    int signatureCount = 0;
    List<SupportedSignature> supportedSignatures = new ArrayList<>(1);
    while (signatures.hasRemaining()) {
        signatureCount++;
        try {
            ByteBuffer signature = getLengthPrefixedSlice(signatures);
            int sigAlgorithmId = signature.getInt();
            byte[] sigBytes = readLengthPrefixedByteArray(signature);
            result.signatures.add(new Result.SignerInfo.Signature(sigAlgorithmId, sigBytes));
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.findById(sigAlgorithmId);
            if (signatureAlgorithm == null) {
                result.addWarning(Issue.V2_SIG_UNKNOWN_SIG_ALGORITHM, sigAlgorithmId);
                continue;
            }
            supportedSignatures.add(new SupportedSignature(signatureAlgorithm, sigBytes));
        } catch (IOException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_SIGNATURE, signatureCount);
            return;
        }
    }
    if (result.signatures.isEmpty()) {
        result.addError(Issue.V2_SIG_NO_SIGNATURES);
        return;
    }
    // Verify signatures over signed-data block using the public key
    List<SupportedSignature> signaturesToVerify = getSignaturesToVerify(supportedSignatures);
    if (signaturesToVerify.isEmpty()) {
        result.addError(Issue.V2_SIG_NO_SUPPORTED_SIGNATURES);
        return;
    }
    for (SupportedSignature signature : signaturesToVerify) {
        SignatureAlgorithm signatureAlgorithm = signature.algorithm;
        String jcaSignatureAlgorithm = signatureAlgorithm.getJcaSignatureAlgorithmAndParams().getFirst();
        AlgorithmParameterSpec jcaSignatureAlgorithmParams = signatureAlgorithm.getJcaSignatureAlgorithmAndParams().getSecond();
        String keyAlgorithm = signatureAlgorithm.getJcaKeyAlgorithm();
        PublicKey publicKey;
        try {
            publicKey = KeyFactory.getInstance(keyAlgorithm).generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        } catch (Exception e) {
            result.addError(Issue.V2_SIG_MALFORMED_PUBLIC_KEY, e);
            return;
        }
        try {
            Signature sig = Signature.getInstance(jcaSignatureAlgorithm);
            sig.initVerify(publicKey);
            if (jcaSignatureAlgorithmParams != null) {
                sig.setParameter(jcaSignatureAlgorithmParams);
            }
            signedData.position(0);
            sig.update(signedData);
            byte[] sigBytes = signature.signature;
            if (!sig.verify(sigBytes)) {
                result.addError(Issue.V2_SIG_DID_NOT_VERIFY, signatureAlgorithm);
                return;
            }
            result.verifiedSignatures.put(signatureAlgorithm, sigBytes);
            contentDigestsToVerify.add(signatureAlgorithm.getContentDigestAlgorithm());
        } catch (Exception e) {
            result.addError(Issue.V2_SIG_VERIFY_EXCEPTION, signatureAlgorithm, e);
            return;
        }
    }
    // At least one signature over signedData has verified. We can now parse signed-data.
    signedData.position(0);
    ByteBuffer digests = getLengthPrefixedSlice(signedData);
    ByteBuffer certificates = getLengthPrefixedSlice(signedData);
    ByteBuffer additionalAttributes = getLengthPrefixedSlice(signedData);
    // Parse the certificates block
    int certificateIndex = -1;
    while (certificates.hasRemaining()) {
        certificateIndex++;
        byte[] encodedCert = readLengthPrefixedByteArray(certificates);
        X509Certificate certificate;
        try {
            certificate = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(encodedCert));
        } catch (CertificateException e) {
            result.addError(Issue.V2_SIG_MALFORMED_CERTIFICATE, certificateIndex, certificateIndex + 1, e);
            return;
        }
        // Wrap the cert so that the result's getEncoded returns exactly the original encoded
        // form. Without this, getEncoded may return a different form from what was stored in
        // the signature. This is becase some X509Certificate(Factory) implementations re-encode
        // certificates.
        certificate = new GuaranteedEncodedFormX509Certificate(certificate, encodedCert);
        result.certs.add(certificate);
    }
    if (result.certs.isEmpty()) {
        result.addError(Issue.V2_SIG_NO_CERTIFICATES);
        return;
    }
    X509Certificate mainCertificate = result.certs.get(0);
    byte[] certificatePublicKeyBytes = mainCertificate.getPublicKey().getEncoded();
    if (!Arrays.equals(publicKeyBytes, certificatePublicKeyBytes)) {
        result.addError(Issue.V2_SIG_PUBLIC_KEY_MISMATCH_BETWEEN_CERTIFICATE_AND_SIGNATURES_RECORD, toHex(certificatePublicKeyBytes), toHex(publicKeyBytes));
        return;
    }
    // Parse the digests block
    int digestCount = 0;
    while (digests.hasRemaining()) {
        digestCount++;
        try {
            ByteBuffer digest = getLengthPrefixedSlice(digests);
            int sigAlgorithmId = digest.getInt();
            byte[] digestBytes = readLengthPrefixedByteArray(digest);
            result.contentDigests.add(new Result.SignerInfo.ContentDigest(sigAlgorithmId, digestBytes));
        } catch (IOException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_DIGEST, digestCount);
            return;
        }
    }
    List<Integer> sigAlgsFromSignaturesRecord = new ArrayList<>(result.signatures.size());
    for (Result.SignerInfo.Signature signature : result.signatures) {
        sigAlgsFromSignaturesRecord.add(signature.getAlgorithmId());
    }
    List<Integer> sigAlgsFromDigestsRecord = new ArrayList<>(result.contentDigests.size());
    for (Result.SignerInfo.ContentDigest digest : result.contentDigests) {
        sigAlgsFromDigestsRecord.add(digest.getSignatureAlgorithmId());
    }
    if (!sigAlgsFromSignaturesRecord.equals(sigAlgsFromDigestsRecord)) {
        result.addError(Issue.V2_SIG_SIG_ALG_MISMATCH_BETWEEN_SIGNATURES_AND_DIGESTS_RECORDS, sigAlgsFromSignaturesRecord, sigAlgsFromDigestsRecord);
        return;
    }
    // Parse the additional attributes block.
    int additionalAttributeCount = 0;
    while (additionalAttributes.hasRemaining()) {
        additionalAttributeCount++;
        try {
            ByteBuffer attribute = getLengthPrefixedSlice(additionalAttributes);
            int id = attribute.getInt();
            byte[] value = readLengthPrefixedByteArray(attribute);
            result.additionalAttributes.add(new Result.SignerInfo.AdditionalAttribute(id, value));
            result.addWarning(Issue.V2_SIG_UNKNOWN_ADDITIONAL_ATTRIBUTE, id);
        } catch (IOException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_ADDITIONAL_ATTRIBUTE, additionalAttributeCount);
            return;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) BufferUnderflowException(java.nio.BufferUnderflowException) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DigestException(java.security.DigestException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BufferUnderflowException(java.nio.BufferUnderflowException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate) DelegatingX509Certificate(com.android.apksigner.core.internal.util.DelegatingX509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(java.security.Signature) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 5 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method writeTangentArray.

public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
    if (!isLoaded()) {
        throw new NullPointerException();
    }
    if (tangentStore != null) {
        if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    tangentStore.rewind();
    if (binormalStore != null) {
        if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    binormalStore.rewind();
    Vector3f normal = new Vector3f();
    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    for (int r = 0; r < getHeight(); r++) {
        for (int c = 0; c < getWidth(); c++) {
            int idx = (r * getWidth() + c) * 3;
            normal.set(normalBuffer.get(idx), normalBuffer.get(idx + 1), normalBuffer.get(idx + 2));
            tangent.set(normal.cross(new Vector3f(0, 0, 1)));
            binormal.set(new Vector3f(1, 0, 0).cross(normal));
            // save the tangent
            BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c));
            // save the binormal
            BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c));
        }
    }
    /*        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {

                int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
                int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
                int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end

                v1.set(c, getValue(c, r), r);
                t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));

                // below
                if (r == getHeight()-1) { // last row
                    v3.set(c, getValue(c, r), r + 1);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
                    v += textureBuffer.get(texIdx + 1);
                    t3.set(u, v);
                } else {
                    v3.set(c, getValue(c, r + 1), r + 1);
                    t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
                }
                
                //right
                if (c == getWidth()-1) { // last column
                    v2.set(c + 1, getValue(c, r), r);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
                    v += textureBuffer.get(texIdx - 1);
                    t2.set(u, v);
                } else {
                    v2.set(c + 1, getValue(c + 1, r), r); // one to the right
                    t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
                }

                calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
                BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }
        */
    return new FloatBuffer[] { tangentStore, binormalStore };
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

BufferUnderflowException (java.nio.BufferUnderflowException)121 ByteBuffer (java.nio.ByteBuffer)69 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)22 DirectByteBuffer (java.nio.DirectByteBuffer)15 Test (org.junit.Test)14 CertificateException (java.security.cert.CertificateException)12 X509Certificate (java.security.cert.X509Certificate)11 BigInteger (java.math.BigInteger)10 ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)9 UnitTest (org.apache.geode.test.junit.categories.UnitTest)9 CharBuffer (java.nio.CharBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FloatBuffer (java.nio.FloatBuffer)6 CertificateFactory (java.security.cert.CertificateFactory)6 HashMap (java.util.HashMap)6 ArrayMap (android.util.ArrayMap)5 HSIconFileElement (com.android.anqp.HSIconFileElement)5