Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add std::endian support if available #2708

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/asfvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,14 @@ void AsfVideo::decodeBlock() {
extendedStreamProperties();
else if (tag->second == "Degradable_JPEG_Media")
DegradableJPEGMedia();
else // tag found but not processed
{
else { // tag found but not processed
// Make sure that the remaining size is non-zero, so that we won't
// keep revisiting the same location in the file.
const uint64_t remaining_size = objectHeader.getRemainingSize();
Internal::enforce(remaining_size > 0, Exiv2::ErrorCode::kerCorruptedMetadata);
io_->seekOrThrow(io_->tell() + remaining_size, BasicIo::beg, ErrorCode::kerFailedToReadImageData);
}
} else // tag not found
{
} else { // tag not found
// Make sure that the remaining size is non-zero, so that we won't keep
// revisiting the same location in the file.
const uint64_t remaining_size = objectHeader.getRemainingSize();
Expand Down
14 changes: 7 additions & 7 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ enum {

// *****************************************************************************
// class member definitions
#ifdef EXV_ENABLE_BMFF
#ifndef EXV_ENABLE_BMFF
namespace Exiv2 {
bool enableBMFF(bool) {
return false;
}
} // namespace Exiv2
#else
namespace Exiv2 {
static bool enabled = false;
bool enableBMFF(bool enable) {
Expand Down Expand Up @@ -801,10 +807,4 @@ bool isBmffType(BasicIo& iIo, bool advance) {
return matched;
}
} // namespace Exiv2
#else // ifdef EXV_ENABLE_BMFF
namespace Exiv2 {
bool enableBMFF(bool) {
return false;
}
} // namespace Exiv2
#endif
24 changes: 22 additions & 2 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
#include <limits>
#include <set>

#ifdef __cpp_lib_endian
#include <bit>
#endif

// *****************************************************************************
namespace {
using namespace Exiv2;
Expand Down Expand Up @@ -168,7 +172,9 @@ bool Image::isPrintICC(uint16_t type, Exiv2::PrintStructureOption option) {
}

bool Image::isBigEndianPlatform() {
#ifdef __LITTLE_ENDIAN__
#ifdef __cpp_lib_endian
return std::endian::native == std::endian::big;
#elif defined(__LITTLE_ENDIAN__)
return false;
#elif defined(__BIG_ENDIAN__)
return true;
Expand All @@ -188,14 +194,19 @@ bool Image::isBigEndianPlatform() {
#endif
}
bool Image::isLittleEndianPlatform() {
#ifdef __LITTLE_ENDIAN__
#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;
#else
uint64_t result = 0;
auto source_value = reinterpret_cast<byte*>(&value);
auto destination_value = reinterpret_cast<byte*>(&result);
Expand All @@ -204,22 +215,31 @@ uint64_t Image::byteSwap(uint64_t value, bool bSwap) {
destination_value[i] = source_value[8 - i - 1];

return bSwap ? result : value;
#endif
}

uint32_t Image::byteSwap(uint32_t value, bool bSwap) {
#ifdef __cpp_lib_byteswap
return bSwap ? std::byteswap(value) : value;
#else
uint32_t result = 0;
result |= (value & 0x000000FFU) << 24;
result |= (value & 0x0000FF00U) << 8;
result |= (value & 0x00FF0000U) >> 8;
result |= (value & 0xFF000000U) >> 24;
return bSwap ? result : value;
#endif
}

uint16_t Image::byteSwap(uint16_t value, bool bSwap) {
#ifdef __cpp_lib_byteswap
return bSwap ? std::byteswap(value) : value;
#else
uint16_t result = 0;
result |= (value & 0x00FFU) << 8;
result |= (value & 0xFF00U) >> 8;
return bSwap ? result : value;
#endif
}

uint16_t Image::byteSwap2(const DataBuf& buf, size_t offset, bool bSwap) {
Expand Down
4 changes: 4 additions & 0 deletions src/pgfimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ const unsigned char pgfBlank[] = {

namespace Exiv2 {
static uint32_t byteSwap_(uint32_t value, bool bSwap) {
#ifdef __cpp_lib_byteswap
return bSwap ? std::byteswap(value) : value;
#else
uint32_t result = 0;
result |= (value & 0x000000FF) << 24;
result |= (value & 0x0000FF00) << 8;
result |= (value & 0x00FF0000) >> 8;
result |= (value & 0xFF000000) >> 24;
return bSwap ? result : value;
#endif
}

static uint32_t byteSwap_(Exiv2::DataBuf& buf, size_t offset, bool bSwap) {
Expand Down
22 changes: 12 additions & 10 deletions src/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ bool TagVocabulary::operator==(const std::string& key) const {
}

// Unknown Tag
static const TagInfo unknownTag{0xffff,
"Unknown tag",
N_("Unknown tag"),
N_("Unknown tag"),
IfdId::ifdIdNotSet,
SectionId::sectionIdNotSet,
asciiString,
-1,
printValue};
static constexpr TagInfo unknownTag{
0xffff,
"Unknown tag",
N_("Unknown tag"),
N_("Unknown tag"),
IfdId::ifdIdNotSet,
SectionId::sectionIdNotSet,
asciiString,
-1,
printValue,
};

} // namespace Exiv2::Internal

Expand Down Expand Up @@ -259,7 +261,7 @@ ExifKey::ExifKey(const std::string& key) : p_(std::make_unique<Impl>()) {
p_->decomposeKey(key);
}

ExifKey::ExifKey(const ExifKey& rhs) : p_(std::make_unique<Impl>(*rhs.p_)) {
ExifKey::ExifKey(const ExifKey& rhs) : Key(rhs), p_(std::make_unique<Impl>(*rhs.p_)) {
}

ExifKey::~ExifKey() = default;
Expand Down