Skip to content

Commit c9bdd6e

Browse files
authored
Merge pull request #2209 from Exiv2/mainTODOs
Improvements around TODO comments
2 parents d59e143 + 2b74cc8 commit c9bdd6e

File tree

19 files changed

+312
-124
lines changed

19 files changed

+312
-124
lines changed

app/actions.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ int setModeAndPrintStructure(Exiv2::PrintStructureOption option, const std::stri
170170
if (binary && option == Exiv2::kpsIccProfile) {
171171
std::stringstream output(std::stringstream::out | std::stringstream::binary);
172172
result = printStructure(output, option, path);
173-
if (result == 0) {
174-
std::string str = output.str();
173+
std::string str = output.str();
174+
if (result == 0 && !str.empty()) {
175175
Exiv2::DataBuf iccProfile(str.size());
176176
Exiv2::DataBuf ascii(str.size() * 3 + 1);
177177
ascii.write_uint8(str.size() * 3, 0);
@@ -549,9 +549,11 @@ bool Print::printMetadatum(const Exiv2::Metadatum& md, const Exiv2::Image* pImag
549549
if (Params::instance().printItems_ & Params::prHex) {
550550
if (!first)
551551
std::cout << std::endl;
552-
Exiv2::DataBuf buf(md.size());
553-
md.copy(buf.data(), pImage->byteOrder());
554-
Exiv2::hexdump(std::cout, buf.c_data(), buf.size());
552+
if (md.size() > 0) {
553+
Exiv2::DataBuf buf(md.size());
554+
md.copy(buf.data(), pImage->byteOrder());
555+
Exiv2::hexdump(std::cout, buf.c_data(), buf.size());
556+
}
555557
}
556558
std::cout << std::endl;
557559
return true;

include/exiv2/types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ struct EXIV2API DataBuf {
201201
int cmpBytes(size_t offset, const void* buf, size_t bufsize) const;
202202

203203
//! Returns a data pointer.
204-
byte* data(size_t offset = 0);
204+
[[nodiscard]] byte* data(size_t offset = 0);
205205

206206
//! Returns a (read-only) data pointer.
207207
[[nodiscard]] const byte* c_data(size_t offset = 0) const;

samples/addmoddel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
2+
23
// Sample program showing how to add, modify and delete Exif metadata.
34

45
#include <exiv2/exiv2.hpp>

src/crwimage_int.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ void CrwMap::encodeBasic(const Image& image, const CrwMapping* pCrwMapping, Ciff
853853
auto ed = image.exifData().findKey(ek);
854854

855855
// Set the new value or remove the entry
856-
if (ed != image.exifData().end()) {
856+
if (ed != image.exifData().end() && ed->size() > 0) {
857857
DataBuf buf(ed->size());
858858
ed->copy(buf.data(), pHead->byteOrder());
859859
pHead->add(pCrwMapping->crwTagId_, pCrwMapping->crwDir_, std::move(buf));

src/epsimage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// *****************************************************************************
3030
namespace {
3131
using namespace Exiv2;
32-
using Exiv2::byte;
32+
using namespace Exiv2::Internal;
3333

3434
// signature of DOS EPS
3535
constexpr auto dosEpsSignature = std::string_view("\xC5\xD0\xD3\xC6");

src/jpgimage.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ void JpegBase::readMetadata() {
184184
#ifdef EXIV2_DEBUG_MESSAGES
185185
std::cerr << "Found app13 segment, size = " << size << "\n";
186186
#endif
187-
// Append to psBlob
188-
append(psBlob, buf.c_data(16), size - 16);
187+
if (buf.size() > 16) { // Append to psBlob
188+
append(psBlob, buf.c_data(16), size - 16);
189+
}
189190
// Check whether psBlob is complete
190191
if (!psBlob.empty() && Photoshop::valid(&psBlob[0], psBlob.size())) {
191192
--search;
@@ -745,9 +746,9 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
745746
bo = littleEndian;
746747
setByteOrder(bo);
747748
}
748-
WriteMethod wm = ExifParser::encode(blob, rawExif.c_data(), rawExif.size(), bo, exifData_);
749749
const byte* pExifData = rawExif.c_data();
750750
size_t exifSize = rawExif.size();
751+
WriteMethod wm = ExifParser::encode(blob, pExifData, exifSize, bo, exifData_);
751752
if (wm == wmIntrusive) {
752753
pExifData = !blob.empty() ? &blob[0] : nullptr;
753754
exifSize = blob.size();
@@ -842,9 +843,9 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
842843
if (foundCompletePsData || iptcData_.count() > 0) {
843844
// Set the new IPTC IRB, keeps existing IRBs but removes the
844845
// IPTC block if there is no new IPTC data to write
845-
DataBuf newPsData = Photoshop::setIptcIrb(!psBlob.empty() ? psBlob.data() : nullptr, psBlob.size(), iptcData_);
846+
DataBuf newPsData = Photoshop::setIptcIrb(psBlob.data(), psBlob.size(), iptcData_);
846847
const long maxChunkSize = 0xffff - 16;
847-
const byte* chunkStart = newPsData.c_data();
848+
const byte* chunkStart = newPsData.empty() ? nullptr : newPsData.c_data();
848849
const byte* chunkEnd = newPsData.empty() ? nullptr : newPsData.c_data(newPsData.size() - 1);
849850
while (chunkStart < chunkEnd) {
850851
// Determine size of next chunk

src/pngchunk_int.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void PngChunk::decodeTXTChunk(Image* pImage, const DataBuf& data, TxtChunkType t
5656
#ifdef EXIV2_DEBUG_MESSAGES
5757
std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk data: " << std::string(arr.c_str(), arr.size()) << std::endl;
5858
#endif
59-
parseChunkContent(pImage, key.c_data(), key.size(), arr);
59+
if (!key.empty())
60+
parseChunkContent(pImage, key.c_data(), key.size(), arr);
6061
}
6162

6263
DataBuf PngChunk::decodeTXTChunk(const DataBuf& data, TxtChunkType type) {
@@ -558,8 +559,10 @@ DataBuf PngChunk::readRawProfile(const DataBuf& text, bool iTXt) {
558559
return {};
559560
}
560561

561-
// Copy profile, skipping white space and column 1 "=" signs
562+
if (info.empty()) // Early return
563+
return info;
562564

565+
// Copy profile, skipping white space and column 1 "=" signs
563566
unsigned char* dp = info.data(); // decode pointer
564567
size_t nibbles = length * 2;
565568

src/pngimage.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "pngimage.hpp"
1919
#include "tiffimage.hpp"
2020
#include "types.hpp"
21+
#include "utils.hpp"
2122

2223
#include <array>
2324
#include <iostream>
@@ -171,14 +172,8 @@ static bool tEXtToDataBuf(const byte* bytes, long length, DataBuf& result) {
171172
return true;
172173
}
173174

174-
std::string upper(const std::string& str) {
175-
std::string result;
176-
transform(str.begin(), str.end(), std::back_inserter(result), toupper);
177-
return result;
178-
}
179-
180175
std::string::size_type findi(const std::string& str, const std::string& substr) {
181-
return upper(str).find(upper(substr));
176+
return str.find(substr);
182177
}
183178

184179
void PngImage::printStructure(std::ostream& out, PrintStructureOption option, int depth) {
@@ -194,14 +189,14 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
194189
chType[4] = 0;
195190

196191
if (option == kpsBasic || option == kpsXMP || option == kpsIccProfile || option == kpsRecursive) {
197-
constexpr auto xmpKey = "XML:com.adobe.xmp";
198-
constexpr auto exifKey = "Raw profile type exif";
199-
constexpr auto app1Key = "Raw profile type APP1";
200-
constexpr auto iptcKey = "Raw profile type iptc";
201-
constexpr auto iccKey = "icc";
202-
constexpr auto softKey = "Software";
203-
constexpr auto commKey = "Comment";
204-
constexpr auto descKey = "Description";
192+
const auto xmpKey = upper("XML:com.adobe.xmp");
193+
const auto exifKey = upper("Raw profile type exif");
194+
const auto app1Key = upper("Raw profile type APP1");
195+
const auto iptcKey = upper("Raw profile type iptc");
196+
const auto iccKey = upper("icc");
197+
const auto softKey = upper("Software");
198+
const auto commKey = upper("Comment");
199+
const auto descKey = upper("Description");
205200

206201
bool bPrint = option == kpsBasic || option == kpsRecursive;
207202
if (bPrint) {
@@ -234,8 +229,10 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
234229
}
235230

236231
DataBuf buff(dataOffset);
237-
bufRead = io_->read(buff.data(), dataOffset);
238-
enforce(bufRead == dataOffset, ErrorCode::kerFailedToReadImageData);
232+
if (dataOffset > 0) {
233+
bufRead = io_->read(buff.data(), dataOffset);
234+
enforce(bufRead == dataOffset, ErrorCode::kerFailedToReadImageData);
235+
}
239236
io_->seek(restore, BasicIo::beg);
240237

241238
// format output
@@ -273,15 +270,14 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
273270
bool eXIf = std::strcmp(chType, "eXIf") == 0;
274271

275272
// for XMP, ICC etc: read and format data
276-
/// \todo inside findi we are transforming the dataString to uppercase. Therefore we are transforming it
277-
/// several times when we could do it just once and reuse it.
278-
bool bXMP = option == kpsXMP && findi(dataString, xmpKey) == 0;
279-
bool bICC = option == kpsIccProfile && findi(dataString, iccKey) == 0;
280-
bool bExif = option == kpsRecursive && (findi(dataString, exifKey) == 0 || findi(dataString, app1Key) == 0);
281-
bool bIptc = option == kpsRecursive && findi(dataString, iptcKey) == 0;
282-
bool bSoft = option == kpsRecursive && findi(dataString, softKey) == 0;
283-
bool bComm = option == kpsRecursive && findi(dataString, commKey) == 0;
284-
bool bDesc = option == kpsRecursive && findi(dataString, descKey) == 0;
273+
const auto dataStringU = upper(dataString);
274+
bool bXMP = option == kpsXMP && findi(dataStringU, xmpKey) == 0;
275+
bool bICC = option == kpsIccProfile && findi(dataStringU, iccKey) == 0;
276+
bool bExif = option == kpsRecursive && (findi(dataStringU, exifKey) == 0 || findi(dataStringU, app1Key) == 0);
277+
bool bIptc = option == kpsRecursive && findi(dataStringU, iptcKey) == 0;
278+
bool bSoft = option == kpsRecursive && findi(dataStringU, softKey) == 0;
279+
bool bComm = option == kpsRecursive && findi(dataStringU, commKey) == 0;
280+
bool bDesc = option == kpsRecursive && findi(dataStringU, descKey) == 0;
285281
bool bDump = bXMP || bICC || bExif || bIptc || bSoft || bComm || bDesc || eXIf;
286282

287283
if (bDump) {
@@ -426,7 +422,9 @@ void PngImage::readMetadata() {
426422
if (chunkType == "IEND" || chunkType == "IHDR" || chunkType == "tEXt" || chunkType == "zTXt" ||
427423
chunkType == "eXIf" || chunkType == "iTXt" || chunkType == "iCCP") {
428424
DataBuf chunkData(chunkLength);
429-
readChunk(chunkData, *io_); // Extract chunk data.
425+
if (chunkLength > 0) {
426+
readChunk(chunkData, *io_); // Extract chunk data.
427+
}
430428

431429
if (chunkType == "IEND") {
432430
return; // Last chunk found: we stop parsing.

src/rafimage.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ void RafImage::readMetadata() {
259259
if (io_->seek(jpg_img_off + 12, BasicIo::beg) != 0)
260260
throw Error(ErrorCode::kerFailedToReadImageData);
261261

262-
io_->read(buf.data(), buf.size());
263-
if (io_->error() || io_->eof())
264-
throw Error(ErrorCode::kerFailedToReadImageData);
265-
266-
io_->seek(0, BasicIo::beg); // rewind
262+
if (!buf.empty()) {
263+
io_->read(buf.data(), buf.size());
264+
if (io_->error() || io_->eof())
265+
throw Error(ErrorCode::kerFailedToReadImageData);
266+
}
267267

268268
ByteOrder bo = TiffParser::decode(exifData_, iptcData_, xmpData_, buf.c_data(), buf.size());
269269

src/tiffcomposite_int.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ uint32_t TiffDirectory::writeDirEntry(IoWrapper& ioWrapper, ByteOrder byteOrder,
976976

977977
uint32_t TiffEntryBase::doWrite(IoWrapper& ioWrapper, ByteOrder byteOrder, int64_t /*offset*/, uint32_t /*valueIdx*/,
978978
uint32_t /*dataIdx*/, uint32_t& /*imageIdx*/) {
979-
if (!pValue_)
979+
if (!pValue_ || pValue_->size() == 0)
980980
return 0;
981981

982982
DataBuf buf(pValue_->size());
@@ -1182,7 +1182,8 @@ uint32_t TiffDataEntry::doWriteData(IoWrapper& ioWrapper, ByteOrder /*byteOrder*
11821182
return 0;
11831183

11841184
DataBuf buf = pValue()->dataArea();
1185-
ioWrapper.write(buf.c_data(), buf.size());
1185+
if (!buf.empty())
1186+
ioWrapper.write(buf.c_data(), buf.size());
11861187
// Align data to word boundary
11871188
uint32_t align = (buf.size() & 1);
11881189
if (align)

0 commit comments

Comments
 (0)