From e7676de4795d42d4a6c4aaa393ae3d3a1d26a60a Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Wed, 12 Jul 2023 08:56:50 -0700 Subject: [PATCH] remove endian handling There's std::endian now. Signed-off-by: Rosen Penev --- include/exiv2/config.h | 14 -------------- include/exiv2/image.hpp | 10 ---------- include/exiv2/pgfimage.hpp | 2 -- src/asfvideo.cpp | 9 ++++----- src/bmffimage.cpp | 3 ++- src/image.cpp | 35 ++--------------------------------- src/jp2image.cpp | 3 ++- src/pgfimage.cpp | 11 ++++++----- 8 files changed, 16 insertions(+), 71 deletions(-) diff --git a/include/exiv2/config.h b/include/exiv2/config.h index e55bd28bbb..d509df7743 100644 --- a/include/exiv2/config.h +++ b/include/exiv2/config.h @@ -31,20 +31,6 @@ #endif #endif -#ifndef __LITTLE_ENDIAN__ -#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -#define __LITTLE_ENDIAN__ 1 -#endif -#endif -#endif - -#ifndef __LITTLE_ENDIAN__ -#if defined(_WIN32) || defined(__CYGWIN__) -#define __LITTLE_ENDIAN__ 1 -#endif -#endif - /* If you're using Solaris and the Solaris Studio compiler you must -library=stdcxx4 along with these inclusions below diff --git a/include/exiv2/image.hpp b/include/exiv2/image.hpp index 339ea37e46..96b043a30b 100644 --- a/include/exiv2/image.hpp +++ b/include/exiv2/image.hpp @@ -301,16 +301,6 @@ class EXIV2API Image { void printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStructureOption option, size_t start, bool bSwap, char c, size_t depth); - /*! - @brief is the host platform bigEndian - */ - static bool isBigEndianPlatform(); - - /*! - @brief is the host platform littleEndian - */ - static bool isLittleEndianPlatform(); - static bool isStringType(uint16_t type); static bool isShortType(uint16_t type); static bool isLongType(uint16_t type); diff --git a/include/exiv2/pgfimage.hpp b/include/exiv2/pgfimage.hpp index ffd749175d..aeefc23d50 100644 --- a/include/exiv2/pgfimage.hpp +++ b/include/exiv2/pgfimage.hpp @@ -55,7 +55,6 @@ class EXIV2API PgfImage : public Image { //@} private: - bool bSwap_; // true for bigEndian hardware, else false /*! @brief Provides the main implementation of writeMetadata() by writing all buffered metadata to the provided BasicIo. @@ -71,7 +70,6 @@ class EXIV2API PgfImage : public Image { //! Read header structure. DataBuf readPgfHeaderStructure(BasicIo& iIo, uint32_t& width, uint32_t& height) const; //@} - }; // class PgfImage // ***************************************************************************** diff --git a/src/asfvideo.cpp b/src/asfvideo.cpp index 9fa5e089e1..db8d7cfba0 100644 --- a/src/asfvideo.cpp +++ b/src/asfvideo.cpp @@ -2,6 +2,7 @@ // included header files #include "asfvideo.hpp" +#include #include #include #include @@ -51,11 +52,9 @@ AsfVideo::GUIDTag::GUIDTag(const uint8_t* bytes) { std::memcpy(&data2_, bytes + DWORD, WORD); std::memcpy(&data3_, bytes + DWORD + WORD, WORD); std::copy(bytes + QWORD, bytes + 2 * QWORD, data4_.begin()); - if (isBigEndianPlatform()) { - data1_ = byteSwap(data1_, true); - data2_ = byteSwap(data2_, true); - data3_ = byteSwap(data3_, true); - } + data1_ = byteSwap(data1_, std::endian::native == std::endian::big); + data2_ = byteSwap(data2_, std::endian::native == std::endian::big); + data3_ = byteSwap(data3_, std::endian::native == std::endian::big); } std::string AsfVideo::GUIDTag::to_string() { diff --git a/src/bmffimage.cpp b/src/bmffimage.cpp index a945b12cce..03bbd6ed03 100644 --- a/src/bmffimage.cpp +++ b/src/bmffimage.cpp @@ -21,6 +21,7 @@ #endif // + standard includes +#include #include #include #include @@ -88,7 +89,7 @@ BmffImage::BmffImage(BasicIo::UniquePtr io, bool /* create */, size_t max_box_de std::string BmffImage::toAscii(uint32_t n) { const auto p = reinterpret_cast(&n); std::string result(p, p + 4); - if (!isBigEndianPlatform()) + if constexpr (std::endian::native == std::endian::little) std::reverse(result.begin(), result.end()); // show 0 as _ std::replace(result.begin(), result.end(), '\0', '_'); diff --git a/src/image.cpp b/src/image.cpp index e14dba586e..cd1947546f 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -173,38 +173,6 @@ bool Image::isPrintICC(uint16_t type, Exiv2::PrintStructureOption option) { return type == 0x8773 && option == kpsIccProfile; } -bool Image::isBigEndianPlatform() { -#ifdef __cpp_lib_endian - return std::endian::native == std::endian::big; -#elif defined(__LITTLE_ENDIAN__) - return false; -#elif defined(__BIG_ENDIAN__) - return true; -#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return true; -#else - return false; -#endif -#else - union { - uint32_t i; - char c[4]; - } e = {0x01000000}; - - return e.c[0] != 0; -#endif -} -bool Image::isLittleEndianPlatform() { -#ifdef __cpp_lib_endian - return std::endian::native == std::endian::little; -#elif defined(__LITTLE_ENDIAN__) - return true; -#else - return !isBigEndianPlatform(); -#endif -} - uint64_t Image::byteSwap(uint64_t value, bool bSwap) { #ifdef __cpp_lib_byteswap return bSwap ? std::byteswap(value) : value; @@ -548,7 +516,8 @@ void Image::printTiffStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruc // read header (we already know for certain that we have a Tiff file) io.readOrThrow(dir.data(), 8, ErrorCode::kerCorruptedMetadata); auto c = dir.read_uint8(0); - bool bSwap = (c == 'M' && isLittleEndianPlatform()) || (c == 'I' && isBigEndianPlatform()); + bool bSwap = (c == 'M' && std::endian::native == std::endian::little) || + (c == 'I' && std::endian::native == std::endian::big); size_t start = byteSwap4(dir, 4, bSwap); printIFDStructure(io, out, option, start + offset, bSwap, c, depth); } diff --git a/src/jp2image.cpp b/src/jp2image.cpp index 9485a28643..da0403e4f3 100644 --- a/src/jp2image.cpp +++ b/src/jp2image.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace Exiv2 { @@ -100,7 +101,7 @@ Jp2Image::Jp2Image(BasicIo::UniquePtr io, bool create) : Image(ImageType::jp2, m // Obtains the ascii version from the box.type std::string Jp2Image::toAscii(uint32_t n) { const auto p = reinterpret_cast(&n); - if (isBigEndianPlatform()) + if constexpr (std::endian::native == std::endian::big) return std::string(p, p + 4); std::string result(p, p + 4); std::reverse(result.begin(), result.end()); diff --git a/src/pgfimage.cpp b/src/pgfimage.cpp index 538b5bd9f4..c058583ebd 100644 --- a/src/pgfimage.cpp +++ b/src/pgfimage.cpp @@ -10,6 +10,7 @@ #include "futils.hpp" #include "image.hpp" +#include #include // Signature from front of PGF file @@ -54,7 +55,7 @@ static uint32_t byteSwap_(Exiv2::DataBuf& buf, size_t offset, bool bSwap) { } PgfImage::PgfImage(BasicIo::UniquePtr io, bool create) : - Image(ImageType::pgf, mdExif | mdIptc | mdXmp | mdComment, std::move(io)), bSwap_(isBigEndianPlatform()) { + Image(ImageType::pgf, mdExif | mdIptc | mdXmp | mdComment, std::move(io)) { if (create && io_->open() == 0) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Exiv2::PgfImage:: Creating PGF image to memory\n"; @@ -184,7 +185,7 @@ void PgfImage::doWriteMetadata(BasicIo& outIo) { auto newHeaderSize = static_cast(header.size() + imgSize); DataBuf buffer(4); std::copy_n(&newHeaderSize, sizeof(uint32_t), buffer.data()); - byteSwap_(buffer, 0, bSwap_); + byteSwap_(buffer, 0, std::endian::native == std::endian::big); if (outIo.write(buffer.c_data(), 4) != 4) throw Error(ErrorCode::kerImageWriteFailed); @@ -243,7 +244,7 @@ size_t PgfImage::readPgfHeaderSize(BasicIo& iIo) const { if (bufRead != buffer.size()) throw Error(ErrorCode::kerInputDataReadFailed); - auto headerSize = static_cast(byteSwap_(buffer, 0, bSwap_)); + auto headerSize = static_cast(byteSwap_(buffer, 0, std::endian::native == std::endian::big)); if (headerSize == 0) throw Error(ErrorCode::kerNoImageInInputData); @@ -264,8 +265,8 @@ DataBuf PgfImage::readPgfHeaderStructure(BasicIo& iIo, uint32_t& width, uint32_t DataBuf work(8); // don't disturb the binary data - doWriteMetadata reuses it std::copy_n(header.c_data(), 8, work.begin()); - width = byteSwap_(work, 0, bSwap_); - height = byteSwap_(work, 4, bSwap_); + width = byteSwap_(work, 0, std::endian::native == std::endian::big); + height = byteSwap_(work, 4, std::endian::native == std::endian::big); /* NOTE: properties not yet used byte nLevels = buffer.pData_[8];