Skip to content

Commit

Permalink
remove endian handling
Browse files Browse the repository at this point in the history
There's std::endian now.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Sep 27, 2024
1 parent 1f2bf37 commit e7676de
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 71 deletions.
14 changes: 0 additions & 14 deletions include/exiv2/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions include/exiv2/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions include/exiv2/pgfimage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

// *****************************************************************************
Expand Down
9 changes: 4 additions & 5 deletions src/asfvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// included header files
#include "asfvideo.hpp"

#include <bit>
#include <cstring>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif

// + standard includes
#include <bit>
#include <cinttypes>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -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<const char*>(&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', '_');
Expand Down
35 changes: 2 additions & 33 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <algorithm>
#include <array>
#include <bit>
#include <iostream>

namespace Exiv2 {
Expand Down Expand Up @@ -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<const char*>(&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());
Expand Down
11 changes: 6 additions & 5 deletions src/pgfimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "futils.hpp"
#include "image.hpp"

#include <bit>
#include <iostream>

// Signature from front of PGF file
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -184,7 +185,7 @@ void PgfImage::doWriteMetadata(BasicIo& outIo) {
auto newHeaderSize = static_cast<uint32_t>(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);

Expand Down Expand Up @@ -243,7 +244,7 @@ size_t PgfImage::readPgfHeaderSize(BasicIo& iIo) const {
if (bufRead != buffer.size())
throw Error(ErrorCode::kerInputDataReadFailed);

auto headerSize = static_cast<size_t>(byteSwap_(buffer, 0, bSwap_));
auto headerSize = static_cast<size_t>(byteSwap_(buffer, 0, std::endian::native == std::endian::big));
if (headerSize == 0)
throw Error(ErrorCode::kerNoImageInInputData);

Expand All @@ -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];
Expand Down

0 comments on commit e7676de

Please sign in to comment.