From 92345027fb7b0607530d661d8e81986e92d9c7d3 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 18 May 2023 00:50:52 +0200 Subject: [PATCH] Use generic `toInt()` function which relies less on compiler optimizations --- CMakeLists.txt | 2 +- flac/flactooggmappingheader.cpp | 6 +++--- id3/id3v2frame.cpp | 6 +++--- id3/id3v2frame.h | 2 +- matroska/ebmlelement.cpp | 6 +++--- mediafileinfo.cpp | 9 +++++---- mp4/mp4tagfield.h | 2 +- opus/opusidentificationheader.cpp | 6 +++--- signature.cpp | 10 +++++----- tagvalue.cpp | 4 ++-- tests/overallflac.cpp | 2 +- tests/overallmp4.cpp | 2 +- vorbis/vorbiscomment.cpp | 2 +- vorbis/vorbisidentificationheader.cpp | 2 +- 14 files changed, 31 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba23b8f..0fc9ad9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,7 +185,7 @@ set(RES_FILES "${LANGUAGE_HEADER_ISO_639_2}") set(CONFIGURATION_PACKAGE_SUFFIX "" CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities") -find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.19.0 REQUIRED) +find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.21.0 REQUIRED) use_cpp_utilities(VISIBILITY PUBLIC) # link against a possibly required extra library for std::filesystem diff --git a/flac/flactooggmappingheader.cpp b/flac/flactooggmappingheader.cpp index 8355ce3..4571933 100644 --- a/flac/flactooggmappingheader.cpp +++ b/flac/flactooggmappingheader.cpp @@ -27,7 +27,7 @@ void FlacToOggMappingHeader::parseHeader(OggIterator &iterator) constexpr auto idSize = 0x05, mappingHeaderSize = 0x0D, blockHeaderSize = 0x04, streamInfoSize = 0x22; char buff[mappingHeaderSize + blockHeaderSize + streamInfoSize - idSize]; iterator.read(buff, idSize); - if (*buff != 0x7Fu || BE::toUInt32(buff + 1) != 0x464C4143u) { + if (*buff != 0x7Fu || BE::toInt(buff + 1) != 0x464C4143u) { throw InvalidDataException(); // not FLAC-to-Ogg mapping header } iterator.read(buff, sizeof(buff)); @@ -35,8 +35,8 @@ void FlacToOggMappingHeader::parseHeader(OggIterator &iterator) // parse FLAC-to-Ogg mapping header m_majorVersion = static_cast(*(buff + 0x00)); m_minorVersion = static_cast(*(buff + 0x01)); - m_headerCount = BE::toUInt16(buff + 0x02); - if (BE::toUInt32(buff + 0x04) != 0x664C6143u) { + m_headerCount = BE::toInt(buff + 0x02); + if (BE::toInt(buff + 0x04) != 0x664C6143u) { throw InvalidDataException(); // native FLAC signature not present } diff --git a/id3/id3v2frame.cpp b/id3/id3v2frame.cpp index 346bf06..84b7050 100644 --- a/id3/id3v2frame.cpp +++ b/id3/id3v2frame.cpp @@ -934,7 +934,7 @@ tuple Id3v2Frame::parseSubstring( case TagTextEncoding::Utf16BigEndian: case TagTextEncoding::Utf16LittleEndian: { if (bufferSize >= 2) { - switch (LE::toUInt16(buffer)) { + switch (LE::toInt(buffer)) { case 0xFEFF: if (encoding == TagTextEncoding::Utf16BigEndian) { diag.emplace_back(DiagLevel::Critical, @@ -1002,9 +1002,9 @@ void Id3v2Frame::parseBom(const char *buffer, std::size_t maxSize, TagTextEncodi switch (encoding) { case TagTextEncoding::Utf16BigEndian: case TagTextEncoding::Utf16LittleEndian: - if ((maxSize >= 2) && (BE::toUInt16(buffer) == 0xFFFE)) { + if ((maxSize >= 2) && (BE::toInt(buffer) == 0xFFFE)) { encoding = TagTextEncoding::Utf16LittleEndian; - } else if ((maxSize >= 2) && (BE::toUInt16(buffer) == 0xFEFF)) { + } else if ((maxSize >= 2) && (BE::toInt(buffer) == 0xFEFF)) { encoding = TagTextEncoding::Utf16BigEndian; } break; diff --git a/id3/id3v2frame.h b/id3/id3v2frame.h index 6f04347..27799aa 100644 --- a/id3/id3v2frame.h +++ b/id3/id3v2frame.h @@ -338,7 +338,7 @@ inline Id3v2Frame::IdentifierType Id3v2Frame::fieldIdFromString(std::string_view case 3: return CppUtilities::BE::toUInt24(idString.data()); case 4: - return CppUtilities::BE::toUInt32(idString.data()); + return CppUtilities::BE::toInt(idString.data()); default: throw CppUtilities::ConversionException("ID3v2 ID must be 3 or 4 chars"); } diff --git a/matroska/ebmlelement.cpp b/matroska/ebmlelement.cpp index b6b59d0..79c275c 100644 --- a/matroska/ebmlelement.cpp +++ b/matroska/ebmlelement.cpp @@ -100,7 +100,7 @@ void EbmlElement::internalParse(Diagnostics &diag) continue; // try again } reader().read(buf + (maximumIdLengthSupported() - m_idLength), m_idLength); - m_id = BE::toUInt32(buf); + m_id = BE::toInt(buf); // check whether this element is actually a sibling of one of its parents rather then a child // (might be the case if the parent's size is unknown and hence assumed to be the max file size) @@ -172,7 +172,7 @@ void EbmlElement::internalParse(Diagnostics &diag) reader().read(buf + (maximumSizeLengthSupported() - m_sizeLength), m_sizeLength); // xor the first byte in buffer which has been read from the file with mask *(buf + (maximumSizeLengthSupported() - m_sizeLength)) ^= static_cast(mask); - m_dataSize = BE::toUInt64(buf); + m_dataSize = BE::toInt(buf); // check if element is truncated if (totalSize() > maxTotalSize()) { if (m_idLength + m_sizeLength > maxTotalSize()) { // header truncated @@ -242,7 +242,7 @@ std::uint64_t EbmlElement::readUInteger() const auto bytesToSkip = maxBytesToRead - min(dataSize(), maxBytesToRead); stream().seekg(static_cast(dataOffset()), ios_base::beg); stream().read(buff + bytesToSkip, static_cast(sizeof(buff) - bytesToSkip)); - return BE::toUInt64(buff); + return BE::toInt(buff); } /*! diff --git a/mediafileinfo.cpp b/mediafileinfo.cpp index 3885189..d9f4815 100644 --- a/mediafileinfo.cpp +++ b/mediafileinfo.cpp @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -206,7 +207,7 @@ startParsingSignature: stream().read(buff, 5); // set the container offset to skip ID3v2 header - m_containerOffset += toNormalInt(BE::toUInt32(buff + 1)) + 10; + m_containerOffset += toNormalInt(BE::toInt(buff + 1)) + 10; if ((*buff) & 0x10) { // footer present m_containerOffset += 10; @@ -432,10 +433,10 @@ void MediaFileInfo::parseTags(Diagnostics &diag, AbortableProgressFeedback &prog char buffer[apeHeaderSize]; stream().seekg(footerOffset, std::ios_base::beg); stream().read(buffer, sizeof(buffer)); - if (BE::toUInt64(buffer) == 0x4150455441474558ul /* APETAGEX */) { + if (BE::toInt(buffer) == 0x4150455441474558ul /* APETAGEX */) { // take record of APE tag - const auto tagSize = static_cast(LE::toUInt32(buffer + 12)); - const auto flags = LE::toUInt32(buffer + 20); + const auto tagSize = static_cast(LE::toInt(buffer + 12)); + const auto flags = LE::toInt(buffer + 20); // subtract tag size (footer size and contents) from effective size if (tagSize <= effectiveSize) { effectiveSize -= tagSize; diff --git a/mp4/mp4tagfield.h b/mp4/mp4tagfield.h index 2b6e0a0..a491763 100644 --- a/mp4/mp4tagfield.h +++ b/mp4/mp4tagfield.h @@ -257,7 +257,7 @@ inline Mp4TagField::IdentifierType Mp4TagField::fieldIdFromString(std::string_vi const auto latin1 = CppUtilities::convertUtf8ToLatin1(idString.data(), idString.size()); switch (latin1.second) { case 4: - return CppUtilities::BE::toUInt32(latin1.first.get()); + return CppUtilities::BE::toInt(latin1.first.get()); default: throw CppUtilities::ConversionException("MP4 ID must be exactly 4 chars"); } diff --git a/opus/opusidentificationheader.cpp b/opus/opusidentificationheader.cpp index c9ea402..737bc60 100644 --- a/opus/opusidentificationheader.cpp +++ b/opus/opusidentificationheader.cpp @@ -25,15 +25,15 @@ void OpusIdentificationHeader::parseHeader(OggIterator &iterator) { char buff[19 - 8]; iterator.read(buff, 8); - if (BE::toUInt64(buff) != 0x4F70757348656164u) { + if (BE::toInt(buff) != 0x4F70757348656164u) { throw InvalidDataException(); // not Opus identification header } iterator.read(buff, sizeof(buff)); m_version = static_cast(*(buff)); m_channels = static_cast(*(buff + 1)); - m_preSkip = LE::toUInt16(buff + 2); + m_preSkip = LE::toInt(buff + 2); m_sampleRate = LE::toUInt32(buff + 4); - m_outputGain = LE::toUInt16(buff + 8); + m_outputGain = LE::toInt(buff + 8); m_channelMap = static_cast(*(buff + 10)); } diff --git a/signature.cpp b/signature.cpp index eb335ef..45e6229 100644 --- a/signature.cpp +++ b/signature.cpp @@ -115,12 +115,12 @@ ContainerFormat parseSignature(std::string_view buffer) // read signature std::uint64_t sig = 0; if (buffer.size() >= 8) { - sig = BE::toUInt64(buffer.data()); + sig = BE::toInt(buffer.data()); } else if (buffer.size() >= 4) { - sig = BE::toUInt32(buffer.data()); + sig = BE::toInt(buffer.data()); sig <<= 4; } else if (buffer.size() >= 2) { - sig = BE::toUInt16(buffer.data()); + sig = BE::toInt(buffer.data()); sig <<= 6; } else { return ContainerFormat::Unknown; @@ -194,9 +194,9 @@ ContainerFormat parseSignature(std::string_view buffer) case PhotoshopDocument: return ContainerFormat::PhotoshopDocument; case Riff: - if (buffer.size() >= 16 && BE::toUInt64(buffer.data() + 8) == Sig64::RiffAvi) { + if (buffer.size() >= 16 && BE::toInt(buffer.data() + 8) == Sig64::RiffAvi) { return ContainerFormat::RiffAvi; - } else if (buffer.size() >= 12 && BE::toUInt32(buffer.data() + 8) == RiffWave) { + } else if (buffer.size() >= 12 && BE::toInt(buffer.data() + 8) == RiffWave) { return ContainerFormat::RiffWave; } else { return ContainerFormat::Riff; diff --git a/tagvalue.cpp b/tagvalue.cpp index eab61ff..258ba10 100644 --- a/tagvalue.cpp +++ b/tagvalue.cpp @@ -1360,13 +1360,13 @@ void TagValue::stripBom(const char *&text, size_t &length, TagTextEncoding encod } break; case TagTextEncoding::Utf16LittleEndian: - if ((length >= 2) && (LE::toUInt16(text) == 0xFEFF)) { + if ((length >= 2) && (LE::toInt(text) == 0xFEFF)) { text += 2; length -= 2; } break; case TagTextEncoding::Utf16BigEndian: - if ((length >= 2) && (BE::toUInt16(text) == 0xFEFF)) { + if ((length >= 2) && (BE::toInt(text) == 0xFEFF)) { text += 2; length -= 2; } diff --git a/tests/overallflac.cpp b/tests/overallflac.cpp index 23cf6bc..8e6b764 100644 --- a/tests/overallflac.cpp +++ b/tests/overallflac.cpp @@ -35,7 +35,7 @@ void OverallTests::checkFlacTestfile1() CPPUNIT_ASSERT_EQUAL("1998"s, tags.front()->value(KnownField::RecordDate).toString()); CPPUNIT_ASSERT(tags.front()->value(KnownField::Comment).isEmpty()); //CPPUNIT_ASSERT(tags.front()->value(KnownField::Cover).dataSize() == 0x58f3); - //CPPUNIT_ASSERT(BE::toUInt64(tags.front()->value(KnownField::Cover).dataPointer()) == 0xFFD8FFE000104A46); + //CPPUNIT_ASSERT(BE::toInt(tags.front()->value(KnownField::Cover).dataPointer()) == 0xFFD8FFE000104A46); CPPUNIT_ASSERT_EQUAL(PositionInSet(3, 4), tags.front()->value(KnownField::TrackPosition).toPositionInSet()); CPPUNIT_ASSERT_EQUAL(PositionInSet(1, 1), tags.front()->value(KnownField::DiskPosition).toPositionInSet()); break; diff --git a/tests/overallmp4.cpp b/tests/overallmp4.cpp index bfc8aca..7f0b428 100644 --- a/tests/overallmp4.cpp +++ b/tests/overallmp4.cpp @@ -209,7 +209,7 @@ void OverallTests::checkMp4Testfile4() CPPUNIT_ASSERT_EQUAL("1998"s, tags.front()->value(KnownField::RecordDate).toString()); CPPUNIT_ASSERT(tags.front()->value(KnownField::Comment).isEmpty()); CPPUNIT_ASSERT_EQUAL(0x58f3_st, tags.front()->value(KnownField::Cover).dataSize()); - CPPUNIT_ASSERT_EQUAL(0xFFD8FFE000104A46ul, BE::toUInt64(tags.front()->value(KnownField::Cover).dataPointer())); + CPPUNIT_ASSERT_EQUAL(0xFFD8FFE000104A46ul, BE::toInt(tags.front()->value(KnownField::Cover).dataPointer())); CPPUNIT_ASSERT_EQUAL(PositionInSet(3, 4), tags.front()->value(KnownField::TrackPosition).toPositionInSet()); CPPUNIT_ASSERT_EQUAL(PositionInSet(1, 1), tags.front()->value(KnownField::DiskPosition).toPositionInSet()); break; diff --git a/vorbis/vorbiscomment.cpp b/vorbis/vorbiscomment.cpp index 3459cc7..ff97e50 100644 --- a/vorbis/vorbiscomment.cpp +++ b/vorbis/vorbiscomment.cpp @@ -167,7 +167,7 @@ template void VorbisComment::internalParse(StreamType &stream if (!skipSignature) { CHECK_MAX_SIZE(7) stream.read(sig, 7); - skipSignature = (BE::toUInt64(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u; + skipSignature = (BE::toInt(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u; } if (skipSignature) { // read vendor (length prefixed string) diff --git a/vorbis/vorbisidentificationheader.cpp b/vorbis/vorbisidentificationheader.cpp index be1bec8..756e1f0 100644 --- a/vorbis/vorbisidentificationheader.cpp +++ b/vorbis/vorbisidentificationheader.cpp @@ -25,7 +25,7 @@ void VorbisIdentificationHeader::parseHeader(OggIterator &iterator) { char buff[30 - 7]; iterator.read(buff, 7); - if ((BE::toUInt64(buff) & 0xffffffffffffff00u) != 0x01766F7262697300u) { + if ((BE::toInt(buff) & 0xffffffffffffff00u) != 0x01766F7262697300u) { throw InvalidDataException(); // not Vorbis identification header } iterator.read(buff, sizeof(buff));