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());
}
}
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;
}
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;
}
}
}
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;
}
}
}
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 };
}
Aggregations