fun findInZip()

in lib/src/main/kotlin/org/jetbrains/zip/signer/metadata/ZipMetadata.kt [23:63]


        fun findInZip(zipArchive: DataSource, zipSectionsInformation: ZipSectionsInformation): ZipMetadata? {
            val centralDirStartOffset = zipSectionsInformation.centralDirectoryOffset
            if (centralDirStartOffset < signatureBlockMetadataSize) return null

            val footer = zipArchive
                .getByteBuffer(centralDirStartOffset - signatureBlockFooterSize, signatureBlockFooterSize)
                .apply {
                    order(ByteOrder.LITTLE_ENDIAN)
                }
            if (footer.getLong(8) != SIGNATURE_BLOCK_MAGIC_LO || footer.getLong(16) != SIGNATURE_BLOCK_MAGIC_HI) {
                return null
            }

            val signatureBlockSizeInFooter = footer.getLong(0)
            if (signatureBlockSizeInFooter < footer.capacity() || signatureBlockSizeInFooter > Int.MAX_VALUE - 8) {
                return null
            }
            val totalSize = (signatureBlockSizeInFooter + 8).toInt()
            val signingBlockOffset = centralDirStartOffset - totalSize
            if (signingBlockOffset < 0) return null
            val signatureBlockHeader = zipArchive.getByteBuffer(signingBlockOffset, signatureBlockHeaderSize).apply {
                order(ByteOrder.LITTLE_ENDIAN)
            }
            val signatureBlockSizeInHeader = signatureBlockHeader.getLong(0)
            if (signatureBlockSizeInHeader != signatureBlockSizeInFooter) return null


            val protobufContent = ZipMetadataProto.parseFrom(
                zipArchive.getByteBuffer(
                    signingBlockOffset + signatureBlockHeaderSize,
                    totalSize - signatureBlockMetadataSize
                )
            )
            require(protobufContent.signatureSchemeVersion == 1)

            return ZipMetadata(
                protobufContent.content.digestsList.map { Digest(it) },
                protobufContent.content.signersList.map { SignerBlock(it) },
                protobufContent
            )
        }