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

more fixes #2508

Merged
merged 9 commits into from
Feb 13, 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
2 changes: 1 addition & 1 deletion app/exiv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ int Params::option(int opt, const std::string& optArg, int optOpt) {

int Params::setLogLevel(const std::string& optArg) {
int rc = 0;
const char logLevel = tolower(optArg[0]);
const auto logLevel = static_cast<char>(tolower(optArg[0]));
switch (logLevel) {
case 'd':
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::debug);
Expand Down
7 changes: 2 additions & 5 deletions src/asfvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "error.hpp"
#include "futils.hpp"
#include "helper_functions.hpp"
#include "utils.hpp"
// *****************************************************************************
// class member definitions
namespace Exiv2 {
Expand Down Expand Up @@ -71,13 +72,9 @@ std::string AsfVideo::GUIDTag::to_string() {
}

// Concatenate all strings into a single string
std::string strGuid = ss.str();
// Convert the string to uppercase
for (auto& c : strGuid) {
c = toupper(c);
}
// Example of output 399595EC-8667-4E2D-8FDB-98814CE76C1E
return strGuid;
return Internal::upper(ss.str());
}

bool AsfVideo::GUIDTag::operator<(const GUIDTag& other) const {
Expand Down
7 changes: 6 additions & 1 deletion src/crwimage_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,12 @@ void CrwMap::decode0x180e(const CiffComponent& ciffComponent, const CrwMapping*
ULongValue v;
v.read(ciffComponent.pData(), 8, byteOrder);
time_t t = v.value_.at(0);
auto tm = std::localtime(&t);
struct tm r;
#ifdef _WIN32
auto tm = localtime_s(&r, &t) ? nullptr : &r;
#else
auto tm = localtime_r(&t, &r);
#endif
if (tm) {
const size_t m = 20;
char s[m];
Expand Down
2 changes: 1 addition & 1 deletion src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ char to_hex(char code) {

/// @brief Convert a hex character to its integer value.
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
return isdigit(ch) ? ch - '0' : static_cast<char>(tolower(ch)) - 'a' + 10;
}

std::string urlencode(const std::string& str) {
Expand Down
4 changes: 3 additions & 1 deletion src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st
// http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzab6/rzab6uafinet.htm
if (serv_addr.sin_addr.s_addr == static_cast<unsigned long>(INADDR_NONE)) {
auto host = gethostbyname(servername_p);
if (!host)
if (!host) {
closesocket(sockfd);
return error(errors, "no such host", servername_p);
}
memcpy(&serv_addr.sin_addr, host->h_addr, sizeof(serv_addr.sin_addr));
}

Expand Down
6 changes: 3 additions & 3 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
if (bNikon) {
// tag is an embedded tiff
const long byteslen = count - jump;
DataBuf bytes(byteslen); // allocate a buffer
io.readOrThrow(bytes.data(), byteslen, ErrorCode::kerCorruptedMetadata); // read
MemIo memIo(bytes.c_data(), byteslen); // create a file
auto b = DataBuf(byteslen); // allocate a buffer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of doing it this way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC bytes it a shadowed declaration. because of copy elision, it's the same.

io.readOrThrow(b.data(), byteslen, ErrorCode::kerCorruptedMetadata); // read
MemIo memIo(b.c_data(), byteslen); // create a file
printTiffStructure(memIo, out, option, depth + 1);
} else {
// tag is an IFD
Expand Down
2 changes: 1 addition & 1 deletion src/iptc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void IptcData::printStructure(std::ostream& out, const Slice<byte*>& bytes, size
uint16_t dataset = bytes.at(i + 2);
Internal::enforce(bytes.size() - i >= 5, ErrorCode::kerCorruptedMetadata);
uint16_t len = getUShort(bytes.subSlice(i + 3, bytes.size()), bigEndian);
snprintf(buff, sizeof(buff), " %6d | %7d | %-24s | %6d | ", record, dataset,
snprintf(buff, sizeof(buff), " %6hu | %7hu | %-24s | %6hu | ", record, dataset,
Exiv2::IptcDataSets::dataSetName(dataset, record).c_str(), len);

Internal::enforce(bytes.size() - i >= 5 + static_cast<size_t>(len), ErrorCode::kerCorruptedMetadata);
Expand Down
10 changes: 2 additions & 8 deletions src/pentaxmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,20 +892,14 @@ constexpr TagDetails pentaxHighISONoiseReduction[] = {

std::ostream& PentaxMakerNote::printVersion(std::ostream& os, const Value& value, const ExifData*) {
std::string val = value.toString();
size_t i = 0;
while ((i = val.find(' ', i)) != std::string::npos && i != val.length() - 1) {
val.replace(i, 1, ".");
}
std::replace(val.begin(), val.end(), ' ', '.');
os << val;
return os;
}

std::ostream& PentaxMakerNote::printResolution(std::ostream& os, const Value& value, const ExifData*) {
std::string val = value.toString();
size_t i = 0;
while ((i = val.find(' ', i)) != std::string::npos && i != val.length() - 1) {
val.replace(i, 1, "x");
}
std::replace(val.begin(), val.end(), ' ', 'x');
os << val;
return os;
}
Expand Down
17 changes: 5 additions & 12 deletions src/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,7 @@ bool LoaderXmpJpeg::readDimensions() {
DataBuf decodeHex(const byte* src, size_t srcSize) {
// create decoding table
byte invalid = 16;
std::array<byte, 256> decodeHexTable;
decodeHexTable.fill(invalid);
auto decodeHexTable = std::vector<byte>(256, invalid);
for (byte i = 0; i < 10; i++)
decodeHexTable[static_cast<byte>('0') + i] = i;
for (byte i = 0; i < 6; i++)
Expand Down Expand Up @@ -861,21 +860,15 @@ DataBuf decodeHex(const byte* src, size_t srcSize) {
const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

DataBuf decodeBase64(const std::string& src) {
const size_t srcSize = src.size();

// create decoding table
unsigned long invalid = 64;
std::array<unsigned long, 256> decodeBase64Table;
decodeBase64Table.fill(invalid);
auto decodeBase64Table = std::vector<unsigned long>(256, invalid);
for (unsigned long i = 0; i < 64; i++)
decodeBase64Table[static_cast<unsigned char>(encodeBase64Table[i])] = i;

// calculate dest size
unsigned long validSrcSize = 0;
for (unsigned long srcPos = 0; srcPos < srcSize; srcPos++) {
if (decodeBase64Table[static_cast<unsigned char>(src[srcPos])] != invalid)
validSrcSize++;
}
auto validSrcSize = static_cast<unsigned long>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use size_t here? I'd like to eliminate long from the codebase.

std::count_if(src.begin(), src.end(), [=](unsigned char c) { return decodeBase64Table.at(c) != invalid; }));
if (validSrcSize > ULONG_MAX / 3)
return {}; // avoid integer overflow
const unsigned long destSize = (validSrcSize * 3) / 4;
Expand All @@ -888,7 +881,7 @@ DataBuf decodeBase64(const std::string& src) {
// decode
for (unsigned long srcPos = 0, destPos = 0; destPos < destSize;) {
unsigned long buffer = 0;
for (int bufferPos = 3; bufferPos >= 0 && srcPos < srcSize; srcPos++) {
for (int bufferPos = 3; bufferPos >= 0 && srcPos < src.size(); srcPos++) {
unsigned long srcValue = decodeBase64Table[static_cast<unsigned char>(src[srcPos])];
if (srcValue == invalid)
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/sonymn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2289,13 +2289,13 @@ static DataBuf sonyTagCipher(uint16_t /* tag */, const byte* bytes, size_t size,
byte code[256];
for (uint32_t i = 0; i < 249; i++) {
if (bDecipher) {
code[(i * i * i) % 249] = i;
code[(i * i * i) % 249] = static_cast<byte>(i);
} else {
code[i] = (i * i * i) % 249;
}
}
for (uint32_t i = 249; i < 256; i++) {
code[i] = i;
code[i] = static_cast<byte>(i);
}

// code byte-by-byte
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Exiv2::Internal {

std::string upper(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::toupper);
std::transform(str.begin(), str.end(), result.begin(), [](int c) { return static_cast<char>(toupper(c)); });
return result;
}

std::string lower(const std::string& a) {
std::string b = a;
std::transform(a.begin(), a.end(), b.begin(), ::tolower);
std::transform(a.begin(), a.end(), b.begin(), [](int c) { return static_cast<char>(tolower(c)); });
return b;
}

Expand Down
3 changes: 1 addition & 2 deletions src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ static std::vector<std::string> getLoadedLibraries() {
char szFilename[_MAX_PATH];
for (DWORD h = 0; h < cbNeeded / sizeof(handles[0]); h++) {
GetModuleFileNameA(handles[h], szFilename, static_cast<DWORD>(std::size(szFilename)));
std::string path(szFilename);
pushPath(path, libs, paths);
pushPath(szFilename, libs, paths);
}
}
#elif defined(__APPLE__)
Expand Down