当前位置: 首页>>代码示例>>Java>>正文


Java DataSource.getByteBuffer方法代码示例

本文整理汇总了Java中com.android.apksig.util.DataSource.getByteBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java DataSource.getByteBuffer方法的具体用法?Java DataSource.getByteBuffer怎么用?Java DataSource.getByteBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.android.apksig.util.DataSource的用法示例。


在下文中一共展示了DataSource.getByteBuffer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findSignature

import com.android.apksig.util.DataSource; //导入方法依赖的package包/类
/**
 * Returns the APK Signature Scheme v2 block contained in the provided APK file and the
 * additional information relevant for verifying the block against the file.
 *
 * @throws SignatureNotFoundException if the APK is not signed using APK Signature Scheme v2
 * @throws IOException if an I/O error occurs while reading the APK
 */
private static SignatureInfo findSignature(
        DataSource apk, ApkUtils.ZipSections zipSections, Result result)
                throws IOException, SignatureNotFoundException {
    // Find the APK Signing Block. The block immediately precedes the Central Directory.
    ByteBuffer eocd = zipSections.getZipEndOfCentralDirectory();
    Pair<DataSource, Long> apkSigningBlockAndOffset = findApkSigningBlock(apk, zipSections);
    DataSource apkSigningBlock = apkSigningBlockAndOffset.getFirst();
    long apkSigningBlockOffset = apkSigningBlockAndOffset.getSecond();
    ByteBuffer apkSigningBlockBuf =
            apkSigningBlock.getByteBuffer(0, (int) apkSigningBlock.size());
    apkSigningBlockBuf.order(ByteOrder.LITTLE_ENDIAN);

    // Find the APK Signature Scheme v2 Block inside the APK Signing Block.
    ByteBuffer apkSignatureSchemeV2Block =
            findApkSignatureSchemeV2Block(apkSigningBlockBuf, result);

    return new SignatureInfo(
            apkSignatureSchemeV2Block,
            apkSigningBlockOffset,
            zipSections.getZipCentralDirectoryOffset(),
            zipSections.getZipEndOfCentralDirectoryOffset(),
            eocd);
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:31,代码来源:V2SchemeVerifier.java

示例2: getZipCentralDirectory

import com.android.apksig.util.DataSource; //导入方法依赖的package包/类
private static ByteBuffer getZipCentralDirectory(
        DataSource apk,
        ApkUtils.ZipSections apkSections) throws IOException, ApkFormatException {
    long cdSizeBytes = apkSections.getZipCentralDirectorySizeBytes();
    if (cdSizeBytes > Integer.MAX_VALUE) {
        throw new ApkFormatException("ZIP Central Directory too large: " + cdSizeBytes);
    }
    long cdOffset = apkSections.getZipCentralDirectoryOffset();
    ByteBuffer cd = apk.getByteBuffer(cdOffset, (int) cdSizeBytes);
    cd.order(ByteOrder.LITTLE_ENDIAN);
    return cd;
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:13,代码来源:ApkSigner.java

示例3: findZipEndOfCentralDirectoryRecord

import com.android.apksig.util.DataSource; //导入方法依赖的package包/类
/**
 * Returns the ZIP End of Central Directory record of the provided ZIP file.
 *
 * @param maxCommentSize maximum accepted size (in bytes) of EoCD comment field. The permitted
 *        value is from 0 to 65535 inclusive. The smaller the value, the faster this method
 *        locates the record, provided its comment field is no longer than this value.
 *
 * @return contents of the ZIP End of Central Directory record and the record's offset in the
 *         file or {@code null} if the file does not contain the record.
 *
 * @throws IOException if an I/O error occurs while reading the file.
 */
private static Pair<ByteBuffer, Long> findZipEndOfCentralDirectoryRecord(
        DataSource zip, int maxCommentSize) throws IOException {
    // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive.
    // The record can be identified by its 4-byte signature/magic which is located at the very
    // beginning of the record. A complication is that the record is variable-length because of
    // the comment field.
    // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from
    // end of the buffer for the EOCD record signature. Whenever we find a signature, we check
    // the candidate record's comment length is such that the remainder of the record takes up
    // exactly the remaining bytes in the buffer. The search is bounded because the maximum
    // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number.

    if ((maxCommentSize < 0) || (maxCommentSize > UINT16_MAX_VALUE)) {
        throw new IllegalArgumentException("maxCommentSize: " + maxCommentSize);
    }

    long fileSize = zip.size();
    if (fileSize < ZIP_EOCD_REC_MIN_SIZE) {
        // No space for EoCD record in the file.
        return null;
    }
    // Lower maxCommentSize if the file is too small.
    maxCommentSize = (int) Math.min(maxCommentSize, fileSize - ZIP_EOCD_REC_MIN_SIZE);

    int maxEocdSize = ZIP_EOCD_REC_MIN_SIZE + maxCommentSize;
    long bufOffsetInFile = fileSize - maxEocdSize;
    ByteBuffer buf = zip.getByteBuffer(bufOffsetInFile, maxEocdSize);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    int eocdOffsetInBuf = findZipEndOfCentralDirectoryRecord(buf);
    if (eocdOffsetInBuf == -1) {
        // No EoCD record found in the buffer
        return null;
    }
    // EoCD found
    buf.position(eocdOffsetInBuf);
    ByteBuffer eocd = buf.slice();
    eocd.order(ByteOrder.LITTLE_ENDIAN);
    return Pair.of(eocd, bufOffsetInFile + eocdOffsetInBuf);
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:52,代码来源:ZipUtils.java

示例4: parseZipCentralDirectory

import com.android.apksig.util.DataSource; //导入方法依赖的package包/类
public static List<CentralDirectoryRecord> parseZipCentralDirectory(
        DataSource apk,
        ApkUtils.ZipSections apkSections)
                throws IOException, ApkFormatException {
    // Read the ZIP Central Directory
    long cdSizeBytes = apkSections.getZipCentralDirectorySizeBytes();
    if (cdSizeBytes > Integer.MAX_VALUE) {
        throw new ApkFormatException("ZIP Central Directory too large: " + cdSizeBytes);
    }
    long cdOffset = apkSections.getZipCentralDirectoryOffset();
    ByteBuffer cd = apk.getByteBuffer(cdOffset, (int) cdSizeBytes);
    cd.order(ByteOrder.LITTLE_ENDIAN);

    // Parse the ZIP Central Directory
    int expectedCdRecordCount = apkSections.getZipCentralDirectoryRecordCount();
    List<CentralDirectoryRecord> cdRecords = new ArrayList<>(expectedCdRecordCount);
    for (int i = 0; i < expectedCdRecordCount; i++) {
        CentralDirectoryRecord cdRecord;
        int offsetInsideCd = cd.position();
        try {
            cdRecord = CentralDirectoryRecord.getRecord(cd);
        } catch (ZipFormatException e) {
            throw new ApkFormatException(
                    "Malformed ZIP Central Directory record #" + (i + 1)
                            + " at file offset " + (cdOffset + offsetInsideCd),
                    e);
        }
        String entryName = cdRecord.getName();
        if (entryName.endsWith("/")) {
            // Ignore directory entries
            continue;
        }
        cdRecords.add(cdRecord);
    }
    // There may be more data in Central Directory, but we don't warn or throw because Android
    // ignores unused CD data.

    return cdRecords;
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:40,代码来源:V1SchemeVerifier.java

示例5: findApkSigningBlock

import com.android.apksig.util.DataSource; //导入方法依赖的package包/类
/**
 * Returns the APK Signing Block and its offset in the provided APK.
 *
 * @throws SignatureNotFoundException if the APK does not contain an APK Signing Block
 */
public static Pair<DataSource, Long> findApkSigningBlock(
        DataSource apk, ApkUtils.ZipSections zipSections)
                throws IOException, SignatureNotFoundException {
    // FORMAT:
    // OFFSET       DATA TYPE  DESCRIPTION
    // * @+0  bytes uint64:    size in bytes (excluding this field)
    // * @+8  bytes payload
    // * @-24 bytes uint64:    size in bytes (same as the one above)
    // * @-16 bytes uint128:   magic

    long centralDirStartOffset = zipSections.getZipCentralDirectoryOffset();
    long centralDirEndOffset =
            centralDirStartOffset + zipSections.getZipCentralDirectorySizeBytes();
    long eocdStartOffset = zipSections.getZipEndOfCentralDirectoryOffset();
    if (centralDirEndOffset != eocdStartOffset) {
        throw new SignatureNotFoundException(
                "ZIP Central Directory is not immediately followed by End of Central Directory"
                        + ". CD end: " + centralDirEndOffset
                        + ", EoCD start: " + eocdStartOffset);
    }

    if (centralDirStartOffset < APK_SIG_BLOCK_MIN_SIZE) {
        throw new SignatureNotFoundException(
                "APK too small for APK Signing Block. ZIP Central Directory offset: "
                        + centralDirStartOffset);
    }
    // Read the magic and offset in file from the footer section of the block:
    // * uint64:   size of block
    // * 16 bytes: magic
    ByteBuffer footer = apk.getByteBuffer(centralDirStartOffset - 24, 24);
    footer.order(ByteOrder.LITTLE_ENDIAN);
    if ((footer.getLong(8) != APK_SIG_BLOCK_MAGIC_LO)
            || (footer.getLong(16) != APK_SIG_BLOCK_MAGIC_HI)) {
        throw new SignatureNotFoundException(
                "No APK Signing Block before ZIP Central Directory");
    }
    // Read and compare size fields
    long apkSigBlockSizeInFooter = footer.getLong(0);
    if ((apkSigBlockSizeInFooter < footer.capacity())
            || (apkSigBlockSizeInFooter > Integer.MAX_VALUE - 8)) {
        throw new SignatureNotFoundException(
                "APK Signing Block size out of range: " + apkSigBlockSizeInFooter);
    }
    int totalSize = (int) (apkSigBlockSizeInFooter + 8);
    long apkSigBlockOffset = centralDirStartOffset - totalSize;
    if (apkSigBlockOffset < 0) {
        throw new SignatureNotFoundException(
                "APK Signing Block offset out of range: " + apkSigBlockOffset);
    }
    ByteBuffer apkSigBlock = apk.getByteBuffer(apkSigBlockOffset, 8);
    apkSigBlock.order(ByteOrder.LITTLE_ENDIAN);
    long apkSigBlockSizeInHeader = apkSigBlock.getLong(0);
    if (apkSigBlockSizeInHeader != apkSigBlockSizeInFooter) {
        throw new SignatureNotFoundException(
                "APK Signing Block sizes in header and footer do not match: "
                        + apkSigBlockSizeInHeader + " vs " + apkSigBlockSizeInFooter);
    }
    return Pair.of(apk.slice(apkSigBlockOffset, totalSize), apkSigBlockOffset);
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:65,代码来源:V2SchemeVerifier.java


注:本文中的com.android.apksig.util.DataSource.getByteBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。