Function detectFormatFromSig

Synopsis

#include <src/bitinputarchive.cpp>

const BitInFormat & detectFormatFromSig(IInStream *stream)

Description

No description yet.

Source

Lines 434-488 in src/bitformat.cpp. Line 41 in src/bitinputarchive.cpp.

const BitInFormat& detectFormatFromSig( IInStream* stream ) {
    CONSTEXPR auto SIGNATURE_SIZE = 8u;
    uint64_t file_signature = readSignature( stream, SIGNATURE_SIZE );
    uint64_t signature_mask = 0xFFFFFFFFFFFFFFFFull;
    for ( auto i = 0u; i < SIGNATURE_SIZE - 1; ++i ) {
        auto it = common_signatures.find( file_signature );
        if ( it != common_signatures.end() ) {
            stream->Seek( 0, 0, nullptr );
            return it->second;
        }
        signature_mask <<= 8ull;          // left shifting the mask of 1 byte, so that
        file_signature &= signature_mask; // the least significant i bytes are masked (set to 0)
    }
    for ( auto& sig : common_signatures_with_offset ) {
        stream->Seek( sig.offset, 0, nullptr );
        file_signature = readSignature( stream, sig.size );
        if ( file_signature == sig.signature ) {
            stream->Seek( 0, 0, nullptr );
            return sig.format;
        }
    }
    // Detecting ISO/UDF
    CONSTEXPR auto ISO_SIGNATURE              = 0x4344303031000000; //CD001
    CONSTEXPR auto ISO_SIGNATURE_SIZE         = 5ull;
    CONSTEXPR auto ISO_SIGNATURE_OFFSET       = 0x8001;
    // Checking for ISO signature
    stream->Seek( ISO_SIGNATURE_OFFSET, 0, nullptr );
    file_signature = readSignature( stream, ISO_SIGNATURE_SIZE );
    if ( file_signature == ISO_SIGNATURE ) {
        CONSTEXPR auto MAX_VOLUME_DESCRIPTORS     = 16;
        CONSTEXPR auto ISO_VOLUME_DESCRIPTOR_SIZE = 0x800; //2048
        CONSTEXPR auto UDF_SIGNATURE          = 0x4E53523000000000; //NSR0
        CONSTEXPR auto UDF_SIGNATURE_SIZE     = 4u;
        // The file is ISO, checking if it is also UDF!
        for ( auto descriptor_index = 1ull; descriptor_index < MAX_VOLUME_DESCRIPTORS; ++descriptor_index ) {
            stream->Seek( ISO_SIGNATURE_OFFSET + descriptor_index * ISO_VOLUME_DESCRIPTOR_SIZE, 0, nullptr );
            file_signature = readSignature( stream, UDF_SIGNATURE_SIZE );
            if ( file_signature == UDF_SIGNATURE ) {
                stream->Seek( 0, 0, nullptr );
                return Udf;
            }
        }
        stream->Seek( 0, 0, nullptr );
        return Iso; //No UDF volume signature found, i.e. simple ISO!
    }
    stream->Seek( 0, 0, nullptr );
    throw BitException( "Cannot detect the format of the file" );
}





Add Discussion as Guest

Log in