b326000
diff --git a/include/exiv2/error.hpp b/include/exiv2/error.hpp
b326000
index 24a70bf6..cc67725b 100644
b326000
--- a/include/exiv2/error.hpp
b326000
+++ b/include/exiv2/error.hpp
b326000
@@ -192,6 +192,74 @@ namespace Exiv2 {
b326000
         return os << error.what();
b326000
     }
b326000
b326000
+    //! Complete list of all Exiv2 error codes
b326000
+    enum ErrorCode {
b326000
+        kerGeneralError = -1,
b326000
+        kerSuccess = 0,
b326000
+        kerErrorMessage,
b326000
+        kerCallFailed,
b326000
+        kerNotAnImage,
b326000
+        kerInvalidDataset,
b326000
+        kerInvalidRecord,
b326000
+        kerInvalidKey,
b326000
+        kerInvalidTag,
b326000
+        kerValueNotSet,
b326000
+        kerDataSourceOpenFailed,
b326000
+        kerFileOpenFailed,
b326000
+        kerFileContainsUnknownImageType,
b326000
+        kerMemoryContainsUnknownImageType,
b326000
+        kerUnsupportedImageType,
b326000
+        kerFailedToReadImageData,
b326000
+        kerNotAJpeg,
b326000
+        kerFailedToMapFileForReadWrite,
b326000
+        kerFileRenameFailed,
b326000
+        kerTransferFailed,
b326000
+        kerMemoryTransferFailed,
b326000
+        kerInputDataReadFailed,
b326000
+        kerImageWriteFailed,
b326000
+        kerNoImageInInputData,
b326000
+        kerInvalidIfdId,
b326000
+        //! Entry::setValue: Value too large
b326000
+        kerValueTooLarge,
b326000
+        //! Entry::setDataArea: Value too large
b326000
+        kerDataAreaValueTooLarge,
b326000
+        kerOffsetOutOfRange,
b326000
+        kerUnsupportedDataAreaOffsetType,
b326000
+        kerInvalidCharset,
b326000
+        kerUnsupportedDateFormat,
b326000
+        kerUnsupportedTimeFormat,
b326000
+        kerWritingImageFormatUnsupported,
b326000
+        kerInvalidSettingForImage,
b326000
+        kerNotACrwImage,
b326000
+        kerFunctionNotSupported,
b326000
+        kerNoNamespaceInfoForXmpPrefix,
b326000
+        kerNoPrefixForNamespace,
b326000
+        kerTooLargeJpegSegment,
b326000
+        kerUnhandledXmpdatum,
b326000
+        kerUnhandledXmpNode,
b326000
+        kerXMPToolkitError,
b326000
+        kerDecodeLangAltPropertyFailed,
b326000
+        kerDecodeLangAltQualifierFailed,
b326000
+        kerEncodeLangAltPropertyFailed,
b326000
+        kerPropertyNameIdentificationFailed,
b326000
+        kerSchemaNamespaceNotRegistered,
b326000
+        kerNoNamespaceForPrefix,
b326000
+        kerAliasesNotSupported,
b326000
+        kerInvalidXmpText,
b326000
+        kerTooManyTiffDirectoryEntries,
b326000
+        kerMultipleTiffArrayElementTagsInDirectory,
b326000
+        kerWrongTiffArrayElementTagType,
b326000
+        kerInvalidKeyXmpValue,
b326000
+        kerInvalidIccProfile,
b326000
+        kerInvalidXMP,
b326000
+        kerTiffDirectoryTooLarge,
b326000
+        kerInvalidTypeValue,
b326000
+        kerInvalidMalloc,
b326000
+        kerCorruptedMetadata,
b326000
+        kerArithmeticOverflow,
b326000
+        kerMallocFailed,
b326000
+    };
b326000
+
b326000
     /*!
b326000
       @brief Simple error class used for exceptions. An output operator is
b326000
              provided to print errors to a stream.
b326000
b326000
diff --git a/src/enforce.hpp b/src/enforce.hpp
b326000
new file mode 100644
b326000
index 00000000..b2d77eea
b326000
--- /dev/null
b326000
+++ b/src/enforce.hpp
b326000
@@ -0,0 +1,96 @@
b326000
+// ********************************************************* -*- C++ -*-
b326000
+/*
b326000
+ * Copyright (C) 2004-2018 Exiv2 maintainers
b326000
+ *
b326000
+ * This program is part of the Exiv2 distribution.
b326000
+ *
b326000
+ * This program is free software; you can redistribute it and/or
b326000
+ * modify it under the terms of the GNU General Public License
b326000
+ * as published by the Free Software Foundation; either version 2
b326000
+ * of the License, or (at your option) any later version.
b326000
+ *
b326000
+ * This program is distributed in the hope that it will be useful,
b326000
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b326000
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b326000
+ * GNU General Public License for more details.
b326000
+ *
b326000
+ * You should have received a copy of the GNU General Public License
b326000
+ * along with this program; if not, write to the Free Software
b326000
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
b326000
+ */
b326000
+/*!
b326000
+  @file    enforce.hpp
b326000
+  @brief   Port of D's enforce() to C++ & Exiv2
b326000
+  @author  Dan Čermák (D4N)
b326000
+           dan.cermak@cgc-instruments.com
b326000
+  @date    11-March-18, D4N: created
b326000
+ */
b326000
+
b326000
+#include <string>
b326000
+
b326000
+#include "error.hpp"
b326000
+
b326000
+/*!
b326000
+ * @brief Ensure that condition is true, otherwise throw an exception of the
b326000
+ * type exception_t
b326000
+ *
b326000
+ * @tparam exception_t  Exception type that is thrown, must provide a
b326000
+ * constructor that accepts a single argument to which arg1 is forwarded.
b326000
+ *
b326000
+ * @todo once we have C++>=11 use variadic templates and std::forward to remove
b326000
+ * all overloads of enforce
b326000
+ */
b326000
+template <typename exception_t, typename T>
b326000
+inline void enforce(bool condition, const T& arg1)
b326000
+{
b326000
+    if (!condition) {
b326000
+        throw exception_t(arg1);
b326000
+    }
b326000
+}
b326000
+
b326000
+/*!
b326000
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
b326000
+ * the given error_code.
b326000
+ */
b326000
+inline void enforce(bool condition, Exiv2::ErrorCode err_code)
b326000
+{
b326000
+    if (!condition) {
b326000
+        throw Exiv2::Error(err_code);
b326000
+    }
b326000
+}
b326000
+
b326000
+/*!
b326000
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
b326000
+ * the given error_code & arg1.
b326000
+ */
b326000
+template <typename T>
b326000
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1)
b326000
+{
b326000
+    if (!condition) {
b326000
+        throw Exiv2::Error(err_code, arg1);
b326000
+    }
b326000
+}
b326000
+
b326000
+/*!
b326000
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
b326000
+ * the given error_code, arg1 & arg2.
b326000
+ */
b326000
+template <typename T, typename U>
b326000
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2)
b326000
+{
b326000
+    if (!condition) {
b326000
+        throw Exiv2::Error(err_code, arg1, arg2);
b326000
+    }
b326000
+}
b326000
+
b326000
+/*!
b326000
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
b326000
+ * the given error_code, arg1, arg2 & arg3.
b326000
+ */
b326000
+template <typename T, typename U, typename V>
b326000
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2, const V& arg3)
b326000
+{
b326000
+    if (!condition) {
b326000
+        throw Exiv2::Error(err_code, arg1, arg2, arg3);
b326000
+    }
b326000
+}
b326000
b326000
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
b326000
index 4dcca4d..aae0f5f 100644
b326000
--- a/src/pngchunk.cpp
b326000
+++ b/src/pngchunk.cpp
b326000
@@ -37,6 +37,7 @@ EXIV2_RCSID("@(#) $Id$")
b326000
 #include "iptc.hpp"
b326000
 #include "image.hpp"
b326000
 #include "error.hpp"
b326000
+#include "enforce.hpp"
b326000
b326000
 // + standard includes
b326000
 #include <sstream>
b326000
@@ -46,6 +47,7 @@ EXIV2_RCSID("@(#) $Id$")
b326000
 #include <iostream>
b326000
 #include <cassert>
b326000
 #include <cstdio>
b326000
+#include <algorithm>
b326000
b326000
 #include <zlib.h>     // To uncompress or compress text chunk
b326000
b326000
@@ -86,7 +88,7 @@ namespace Exiv2 {
b326000
b326000
 #ifdef DEBUG
b326000
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk data: "
b326000
-                  << std::string((const char*)arr.pData_, arr.size_) << "\n";
b326000
+                  << std::string((const char*)arr.pData_, arr.size_) << std::endl;
b326000
 #endif
b326000
         parseChunkContent(pImage, key.pData_, key.size_, arr);
b326000
b326000
@@ -99,7 +101,7 @@ namespace Exiv2 {
b326000
b326000
 #ifdef DEBUG
b326000
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk key: "
b326000
-                  << std::string((const char*)key.pData_, key.size_) << "\n";
b326000
+                  << std::string((const char*)key.pData_, key.size_) << std::endl;
b326000
 #endif
b326000
         return parseTXTChunk(data, key.size_, type);
b326000
b326000
@@ -164,12 +166,18 @@ namespace Exiv2 {
b326000
         }
b326000
         else if(type == iTXt_Chunk)
b326000
         {
b326000
+            const int nullSeparators = std::count(&data.pData_[keysize+3], &data.pData_[data.size_], '\0');
b326000
+
b326000
+            enforce(nullSeparators >= 2, Exiv2::kerCorruptedMetadata);
b326000
+
b326000
             // Extract a deflate compressed or uncompressed UTF-8 text chunk
b326000
b326000
             // we get the compression flag after the key
b326000
-            const byte* compressionFlag   = data.pData_ + keysize + 1;
b326000
+            const byte compressionFlag   = data.pData_[keysize + 1];
b326000
             // we get the compression method after the compression flag
b326000
-            const byte* compressionMethod = data.pData_ + keysize + 2;
b326000
+            const byte compressionMethod = data.pData_[keysize + 2];
b326000
+            enforce(compressionFlag == 0x00 || compressionFlag == 0x01, Exiv2::kerCorruptedMetadata);
b326000
+            enforce(compressionMethod == 0x00, Exiv2::kerCorruptedMetadata);
b326000
             // language description string after the compression technique spec
b326000
             std::string languageText((const char*)(data.pData_ + keysize + 3));
b326000
             unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
b326000
@@ -177,7 +185,7 @@ namespace Exiv2 {
b326000
             std::string translatedKeyText((const char*)(data.pData_ + keysize + 3 + languageTextSize +1));
b326000
             unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
b326000
b326000
-            if ( compressionFlag[0] == 0x00 )
b326000
+            if ( compressionFlag == 0x00 )
b326000
             {
b326000
                 // then it's an uncompressed iTXt chunk
b326000
 #ifdef DEBUG
b326000
@@ -191,7 +199,7 @@ namespace Exiv2 {
b326000
                 arr.alloc(textsize);
b326000
                 arr = DataBuf(text, textsize);
b326000
             }
b326000
-            else if ( compressionFlag[0] == 0x01 && compressionMethod[0] == 0x00 )
b326000
+            else if ( compressionFlag == 0x01 && compressionMethod == 0x00 )
b326000
             {
b326000
                 // then it's a zlib compressed iTXt chunk
b326000
 #ifdef DEBUG
b326000
diff --git a/src/pngimage.cpp b/src/pngimage.cpp
b326000
index ed7399a..991da6c 100644
b326000
--- a/src/pngimage.cpp
b326000
+++ b/src/pngimage.cpp
b326000
@@ -375,7 +375,7 @@ namespace Exiv2 {
b326000
     void PngImage::readMetadata()
b326000
     {
b326000
 #ifdef DEBUG
b326000
-        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << "\n";
b326000
+        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << std::endl;
b326000
 #endif
b326000
         if (io_->open() != 0)
b326000
         {
b326000
@@ -398,7 +398,7 @@ namespace Exiv2 {
b326000
             // Read chunk header.
b326000
b326000
 #ifdef DEBUG
b326000
-            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << "\n";
b326000
+            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << std::endl;
b326000
 #endif
b326000
             std::memset(cheaderBuf.pData_, 0x0, cheaderBuf.size_);
b326000
             long bufRead = io_->read(cheaderBuf.pData_, cheaderBuf.size_);
b326000
@@ -432,14 +432,14 @@ namespace Exiv2 {
b326000
                 {
b326000
                     // Last chunk found: we stop parsing.
b326000
 #ifdef DEBUG
b326000
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk (length: " << dataOffset << ")\n";
b326000
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk with length: " << dataOffset << std::endl;
b326000
 #endif
b326000
                     return;
b326000
                 }
b326000
                 else if (!memcmp(cheaderBuf.pData_ + 4, "IHDR", 4))
b326000
                 {
b326000
 #ifdef DEBUG
b326000
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk (length: " << dataOffset << ")\n";
b326000
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk with length: " << dataOffset << std::endl;
b326000
 #endif
b326000
                     if (cdataBuf.size_ >= 8) {
b326000
                         PngChunk::decodeIHDRChunk(cdataBuf, &pixelWidth_, &pixelHeight_);
b326000
@@ -448,21 +448,21 @@ namespace Exiv2 {
b326000
                 else if (!memcmp(cheaderBuf.pData_ + 4, "tEXt", 4))
b326000
                 {
b326000
 #ifdef DEBUG
b326000
-                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk (length: " << dataOffset << ")\n";
b326000
+                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk with length: " << dataOffset << std::endl;
b326000
 #endif
b326000
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::tEXt_Chunk);
b326000
                 }
b326000
                 else if (!memcmp(cheaderBuf.pData_ + 4, "zTXt", 4))
b326000
                 {
b326000
 #ifdef DEBUG
b326000
-                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk (length: " << dataOffset << ")\n";
b326000
+                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk with length: " << dataOffset << std::endl;
b326000
 #endif
b326000
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::zTXt_Chunk);
b326000
                 }
b326000
                 else if (!memcmp(cheaderBuf.pData_ + 4, "iTXt", 4))
b326000
                 {
b326000
 #ifdef DEBUG
b326000
-                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk (length: " << dataOffset << ")\n";
b326000
+                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk with length: " << dataOffset << std::endl;
b326000
 #endif
b326000
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::iTXt_Chunk);
b326000
                 }
b326000
@@ -481,7 +481,7 @@ namespace Exiv2 {
b326000
b326000
             // Move to the next chunk: chunk data size + 4 CRC bytes.
b326000
 #ifdef DEBUG
b326000
-            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << "\n";
b326000
+            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << std::endl;
b326000
 #endif
b326000
             io_->seek(dataOffset + 4 , BasicIo::cur);
b326000
             if (io_->error() || io_->eof()) throw Error(14);
b326000
@@ -511,8 +511,8 @@ namespace Exiv2 {
b326000
         if (!outIo.isopen()) throw Error(21);
b326000
b326000
 #ifdef DEBUG
b326000
-        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << "\n";
b326000
-        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << "\n";
b326000
+        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << std::endl;
b326000
+        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << std::endl;
b326000
 #endif
b326000
b326000
         // Ensure that this is the correct image type