Skip to content

Commit dc7ddfc

Browse files
committed
use actual array instead of C string
C strings in C++ have an extra member for NULL termination. replacing copy_n with copy like this will allow eventual conversion to std::ranges. Signed-off-by: Rosen Penev <[email protected]>
1 parent 2e6fb08 commit dc7ddfc

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/jpgimage.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ constexpr Internal::TagDetails jpegProcessMarkerTags[] = {
7373
{sof15_, N_("Lossless, differential arithmetic coding")},
7474
};
7575

76-
constexpr auto exifId_ = "Exif\0\0"; //!< Exif identifier
77-
// constexpr auto jfifId_ = "JFIF\0"; //!< JFIF identifier
78-
constexpr auto xmpId_ = "http://ns.adobe.com/xap/1.0/\0"; //!< XMP packet identifier
79-
constexpr auto iccId_ = "ICC_PROFILE\0"; //!< ICC profile identifier
76+
constexpr std::array<byte, 6> exifId_{
77+
'E', 'x', 'i', 'f', '\0', '\0',
78+
}; //!< Exif identifier
79+
constexpr std::array<byte, 29> xmpId_{
80+
'h', 't', 't', 'p', ':', '/', '/', 'n', 's', '.', 'a', 'd', 'o', 'b', 'e',
81+
'.', 'c', 'o', 'm', '/', 'x', 'a', 'p', '/', '1', '.', '0', '/', 0x0,
82+
}; //!< XMP packet identifier
83+
// constexpr auto jfifId_ = "JFIF"; //!< JFIF identifier
84+
constexpr auto iccId_ = "ICC_PROFILE"; //!< ICC profile identifier
8085

8186
constexpr bool inRange(int lo, int value, int hi) {
8287
return lo <= value && value <= hi;
@@ -183,7 +188,7 @@ void JpegBase::readMetadata() {
183188
}
184189

185190
if (!foundExifData && marker == app1_ && size >= 8 // prevent out-of-bounds read in memcmp on next line
186-
&& buf.cmpBytes(2, exifId_, 6) == 0) {
191+
&& buf.cmpBytes(2, exifId_.data(), 6) == 0) {
187192
ByteOrder bo = ExifParser::decode(exifData_, buf.c_data(8), size - 8);
188193
setByteOrder(bo);
189194
if (size > 8 && byteOrder() == invalidByteOrder) {
@@ -195,7 +200,7 @@ void JpegBase::readMetadata() {
195200
--search;
196201
foundExifData = true;
197202
} else if (!foundXmpData && marker == app1_ && size >= 31 // prevent out-of-bounds read in memcmp on next line
198-
&& buf.cmpBytes(2, xmpId_, 29) == 0) {
203+
&& buf.cmpBytes(2, xmpId_.data(), 29) == 0) {
199204
xmpPacket_.assign(buf.c_str(31), size - 31);
200205
if (!xmpPacket_.empty() && XmpParser::decode(xmpData_, xmpPacket_)) {
201206
#ifndef SUPPRESS_WARNINGS
@@ -646,7 +651,7 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
646651
insertPos = count + 1;
647652
} else if (skipApp1Exif == notfound && marker == app1_ &&
648653
buf.size() >= 8 && // prevent out-of-bounds read in memcmp on next line
649-
buf.cmpBytes(2, exifId_, 6) == 0) {
654+
buf.cmpBytes(2, exifId_.data(), 6) == 0) {
650655
skipApp1Exif = count;
651656
++search;
652657
if (buf.size() > 8) {
@@ -655,7 +660,7 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
655660
}
656661
} else if (skipApp1Xmp == notfound && marker == app1_ &&
657662
buf.size() >= 31 && // prevent out-of-bounds read in memcmp on next line
658-
buf.cmpBytes(2, xmpId_, 29) == 0) {
663+
buf.cmpBytes(2, xmpId_.data(), 29) == 0) {
659664
skipApp1Xmp = count;
660665
++search;
661666
} else if (marker == app2_ && buf.size() >= 13 && // prevent out-of-bounds read in memcmp on next line
@@ -756,7 +761,7 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
756761
if (exifSize > 0xffff - 8)
757762
throw Error(ErrorCode::kerTooLargeJpegSegment, "Exif");
758763
us2Data(tmpBuf.data() + 2, static_cast<uint16_t>(exifSize + 8), bigEndian);
759-
std::copy_n(exifId_, 6, tmpBuf.begin() + 4);
764+
std::copy(exifId_.begin(), exifId_.end(), tmpBuf.begin() + 4);
760765
if (outIo.write(tmpBuf.data(), 10) != 10)
761766
throw Error(ErrorCode::kerImageWriteFailed);
762767

@@ -783,7 +788,7 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
783788
if (xmpPacket_.size() > 0xffff - 31)
784789
throw Error(ErrorCode::kerTooLargeJpegSegment, "XMP");
785790
us2Data(tmpBuf.data() + 2, static_cast<uint16_t>(xmpPacket_.size() + 31), bigEndian);
786-
std::copy_n(xmpId_, 29, tmpBuf.begin() + 4);
791+
std::copy(xmpId_.begin(), xmpId_.end(), tmpBuf.begin() + 4);
787792
if (outIo.write(tmpBuf.data(), 33) != 33)
788793
throw Error(ErrorCode::kerImageWriteFailed);
789794

src/pngimage.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,9 @@ void PngImage::doWriteMetadata(BasicIo& outIo) {
529529
if (bufRead != dataOffset + 4)
530530
throw Error(ErrorCode::kerInputDataReadFailed);
531531

532-
char szChunk[5];
533-
std::copy(cheaderBuf.begin() + 4, cheaderBuf.end(), szChunk);
534-
szChunk[4] = 0;
532+
const std::string szChunk(cheaderBuf.begin() + 4, cheaderBuf.end());
535533

536-
if (!strcmp(szChunk, "IEND")) {
534+
if (szChunk == "IEND") {
537535
// Last chunk found: we write it and done.
538536
#ifdef EXIV2_DEBUG_MESSAGES
539537
std::cout << "Exiv2::PngImage::doWriteMetadata: Write IEND chunk (length: " << dataOffset << ")\n";
@@ -542,14 +540,14 @@ void PngImage::doWriteMetadata(BasicIo& outIo) {
542540
throw Error(ErrorCode::kerImageWriteFailed);
543541
return;
544542
}
545-
if (!strcmp(szChunk, "eXIf") || !strcmp(szChunk, "iCCP")) {
543+
if (szChunk == "eXIf" || szChunk == "iCCP") {
546544
// do nothing (strip): Exif metadata is written following IHDR
547545
// together with the ICC profile as fresh eXIf and iCCP chunks
548546
#ifdef EXIV2_DEBUG_MESSAGES
549547
std::cout << "Exiv2::PngImage::doWriteMetadata: strip " << szChunk << " chunk (length: " << dataOffset << ")"
550548
<< '\n';
551549
#endif
552-
} else if (!strcmp(szChunk, "IHDR")) {
550+
} else if (szChunk == "IHDR") {
553551
#ifdef EXIV2_DEBUG_MESSAGES
554552
std::cout << "Exiv2::PngImage::doWriteMetadata: Write IHDR chunk (length: " << dataOffset << ")\n";
555553
#endif
@@ -646,7 +644,7 @@ void PngImage::doWriteMetadata(BasicIo& outIo) {
646644
throw Error(ErrorCode::kerImageWriteFailed);
647645
}
648646
}
649-
} else if (!strcmp(szChunk, "tEXt") || !strcmp(szChunk, "zTXt") || !strcmp(szChunk, "iTXt")) {
647+
} else if (szChunk == "tEXt" || szChunk == "zTXt" || szChunk == "iTXt") {
650648
DataBuf key = PngChunk::keyTXTChunk(chunkBuf, true);
651649
if (!key.empty() && (compare("Raw profile type exif", key) || compare("Raw profile type APP1", key) ||
652650
compare("Raw profile type iptc", key) || compare("Raw profile type xmp", key) ||

src/preview.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,9 @@ DataBuf makePnm(size_t width, size_t height, const DataBuf& rgb) {
943943
}
944944

945945
const std::string header = "P6\n" + std::to_string(width) + " " + std::to_string(height) + "\n255\n";
946-
const auto headerBytes = reinterpret_cast<const byte*>(header.data());
947946

948947
dest = DataBuf(header.size() + rgb.size());
949-
std::copy_n(headerBytes, header.size(), dest.begin());
948+
std::copy(header.begin(), header.end(), dest.begin());
950949
std::copy(rgb.cbegin(), rgb.cend(), dest.begin() + header.size());
951950
return dest;
952951
}

0 commit comments

Comments
 (0)