clang-tidy: readability-non-const-parameter
[geeqie.git] / src / exiv2.cc
index 0ad5c61..b41db97 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "config.h"
+#include <config.h>
 
 #ifdef HAVE_EXIV2
 
-#include <exiv2/image.hpp>
-#include <exiv2/exif.hpp>
+// Don't include the <exiv2/version.hpp> file directly
+// Early Exiv2 versions didn't have version.hpp and the macros.
+#include <exiv2/exiv2.hpp>
 #include <iostream>
 #include <string>
 
 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
+#ifdef EXIV2_VERSION
 #ifndef EXIV2_TEST_VERSION
-# define EXIV2_TEST_VERSION(major,minor,patch) \
+#define EXIV2_TEST_VERSION(major,minor,patch) \
        ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
 #endif
+#else
+#define EXIV2_TEST_VERSION(major,minor,patch) (false)
+#endif
 
+#if EXIV2_TEST_VERSION(0,27,0)
+#define HAVE_EXIV2_ERROR_CODE
+#endif
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
 
-#if !EXIV2_TEST_VERSION(0,17,90)
-#include <exiv2/tiffparser.hpp>
-#include <exiv2/tiffcomposite.hpp>
-#include <exiv2/tiffvisitor.hpp>
-#include <exiv2/tiffimage.hpp>
-#include <exiv2/cr2image.hpp>
-#include <exiv2/crwimage.hpp>
-#if EXIV2_TEST_VERSION(0,16,0)
-#include <exiv2/orfimage.hpp>
-#endif
-#if EXIV2_TEST_VERSION(0,13,0)
-#include <exiv2/rafimage.hpp>
-#endif
-#include <exiv2/futils.hpp>
-#else
-#include <exiv2/preview.hpp>
-#endif
-
-#if EXIV2_TEST_VERSION(0,17,0)
-#include <exiv2/convert.hpp>
-#include <exiv2/xmpsidecar.hpp>
+#if EXIV2_TEST_VERSION(0,27,0)
+#define EXV_PACKAGE "exiv2"
 #endif
 
-extern "C" {
 #include <glib.h>
 
 #include "main.h"
 #include "exif.h"
 
+#include "filedata.h"
 #include "filefilter.h"
-#include "ui_fileops.h"
-
 #include "misc.h"
-}
+#include "ui-fileops.h"
 
-typedef struct _AltKey AltKey;
+#if EXIV2_TEST_VERSION(0,28,0)
+#define AnyError Error
+#define AutoPtr UniquePtr
+#endif
 
-struct _AltKey
+struct AltKey
 {
        const gchar *xmp_key;
        const gchar *exif_key;
@@ -85,30 +75,29 @@ struct _AltKey
 };
 
 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
-static const AltKey alt_keys[] = {
-       {"Xmp.tiff.Orientation",                "Exif.Image.Orientation",       NULL},
-       {"Xmp.dc.title",                        NULL,                           "Iptc.Application2.ObjectName"          },
-       {"Xmp.photoshop.Urgency",               NULL,                           "Iptc.Application2.Urgency"             },
-       {"Xmp.photoshop.Category",              NULL,                           "Iptc.Application2.Category"            },
-       {"Xmp.photoshop.SupplementalCategory",  NULL,                           "Iptc.Application2.SuppCategory"        },
-       {"Xmp.dc.subject",                      NULL,                           "Iptc.Application2.Keywords"            },
-       {"Xmp.iptc.Location",                   NULL,                           "Iptc.Application2.LocationName"        },
-       {"Xmp.photoshop.Instruction",           NULL,                           "Iptc.Application2.SpecialInstructions" },
-       {"Xmp.photoshop.DateCreated",           NULL,                           "Iptc.Application2.DateCreated"         },
-       {"Xmp.dc.creator",                      NULL,                           "Iptc.Application2.Byline"              },
-       {"Xmp.photoshop.AuthorsPosition",       NULL,                           "Iptc.Application2.BylineTitle"         },
-       {"Xmp.photoshop.City",                  NULL,                           "Iptc.Application2.City"                },
-       {"Xmp.photoshop.State",                 NULL,                           "Iptc.Application2.ProvinceState"       },
-       {"Xmp.iptc.CountryCode",                NULL,                           "Iptc.Application2.CountryCode"         },
-       {"Xmp.photoshop.Country",               NULL,                           "Iptc.Application2.CountryName"         },
-       {"Xmp.photoshop.TransmissionReference", NULL,                           "Iptc.Application2.TransmissionReference"},
-       {"Xmp.photoshop.Headline",              NULL,                           "Iptc.Application2.Headline"            },
-       {"Xmp.photoshop.Credit",                NULL,                           "Iptc.Application2.Credit"              },
-       {"Xmp.photoshop.Source",                NULL,                           "Iptc.Application2.Source"              },
-       {"Xmp.dc.rights",                       NULL,                           "Iptc.Application2.Copyright"           },
-       {"Xmp.dc.description",                  NULL,                           "Iptc.Application2.Caption"             },
-       {"Xmp.photoshop.CaptionWriter",         NULL,                           "Iptc.Application2.Writer"              },
-       {NULL, NULL, NULL}
+static constexpr AltKey alt_keys[] = {
+       {"Xmp.tiff.Orientation",                "Exif.Image.Orientation",       nullptr},
+       {"Xmp.dc.title",                        nullptr,                                "Iptc.Application2.ObjectName"          },
+       {"Xmp.photoshop.Urgency",               nullptr,                                "Iptc.Application2.Urgency"             },
+       {"Xmp.photoshop.Category",              nullptr,                                "Iptc.Application2.Category"            },
+       {"Xmp.photoshop.SupplementalCategory",  nullptr,                                "Iptc.Application2.SuppCategory"        },
+       {"Xmp.dc.subject",                      nullptr,                                "Iptc.Application2.Keywords"            },
+       {"Xmp.iptc.Location",                   nullptr,                                "Iptc.Application2.LocationName"        },
+       {"Xmp.photoshop.Instruction",           nullptr,                                "Iptc.Application2.SpecialInstructions" },
+       {"Xmp.photoshop.DateCreated",           nullptr,                                "Iptc.Application2.DateCreated"         },
+       {"Xmp.dc.creator",                      nullptr,                                "Iptc.Application2.Byline"              },
+       {"Xmp.photoshop.AuthorsPosition",       nullptr,                                "Iptc.Application2.BylineTitle"         },
+       {"Xmp.photoshop.City",                  nullptr,                                "Iptc.Application2.City"                },
+       {"Xmp.photoshop.State",                 nullptr,                                "Iptc.Application2.ProvinceState"       },
+       {"Xmp.iptc.CountryCode",                nullptr,                                "Iptc.Application2.CountryCode"         },
+       {"Xmp.photoshop.Country",               nullptr,                                "Iptc.Application2.CountryName"         },
+       {"Xmp.photoshop.TransmissionReference", nullptr,                                "Iptc.Application2.TransmissionReference"},
+       {"Xmp.photoshop.Headline",              nullptr,                                "Iptc.Application2.Headline"            },
+       {"Xmp.photoshop.Credit",                nullptr,                                "Iptc.Application2.Credit"              },
+       {"Xmp.photoshop.Source",                nullptr,                                "Iptc.Application2.Source"              },
+       {"Xmp.dc.rights",                       nullptr,                                "Iptc.Application2.Copyright"           },
+       {"Xmp.dc.description",                  nullptr,                                "Iptc.Application2.Caption"             },
+       {"Xmp.photoshop.CaptionWriter",         nullptr,                                "Iptc.Application2.Writer"              },
        };
 
 static void _debug_exception(const char* file,
@@ -116,14 +105,14 @@ static void _debug_exception(const char* file,
                              const char* func,
                              Exiv2::AnyError& e)
 {
-       gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
+       gchar *str = g_locale_from_utf8(e.what(), -1, nullptr, nullptr, nullptr);
        DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
        g_free(str);
 }
 
 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
 
-struct _ExifData
+struct ExifData
 {
        Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
        Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
@@ -131,18 +120,16 @@ struct _ExifData
        Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
 #endif
 
-       virtual ~_ExifData()
-       {
-       }
+       virtual ~ExifData() = default;
 
-       virtual void writeMetadata(gchar *path = NULL)
+       virtual void writeMetadata(gchar * = nullptr)
        {
                g_critical("Unsupported method of writing metadata");
        }
 
        virtual ExifData *original()
        {
-               return NULL;
+               return nullptr;
        }
 
        virtual Exiv2::Image *image() = 0;
@@ -165,7 +152,7 @@ struct _ExifData
 };
 
 // This allows read-only access to the original metadata
-struct _ExifDataOriginal : public _ExifData
+struct ExifDataOriginal : public ExifData
 {
 protected:
        Exiv2::Image::AutoPtr image_;
@@ -183,17 +170,17 @@ protected:
 #endif
 
 public:
-       _ExifDataOriginal(Exiv2::Image::AutoPtr image)
+       ExifDataOriginal(Exiv2::Image::AutoPtr image)
        {
-               cp_data_ = NULL;
+               cp_data_ = nullptr;
                cp_length_ = 0;
-               image_ = image;
+               image_ = std::move(image);
                valid_ = TRUE;
        }
 
-       _ExifDataOriginal(gchar *path)
+       ExifDataOriginal(gchar *path)
        {
-               cp_data_ = NULL;
+               cp_data_ = nullptr;
                cp_length_ = 0;
                valid_ = TRUE;
 
@@ -201,7 +188,6 @@ public:
                try
                        {
                        image_ = Exiv2::ImageFactory::open(pathl_);
-//                     g_assert (image.get() != 0);
                        image_->readMetadata();
 
 #if EXIV2_TEST_VERSION(0,16,0)
@@ -222,7 +208,7 @@ public:
                                if (!open) io.open();
                                if (io.isopen())
                                        {
-                                       unsigned char *mapped = (unsigned char*)io.mmap();
+                                       auto mapped = static_cast<unsigned char*>(io.mmap());
                                        if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
                                        io.munmap();
                                        }
@@ -236,77 +222,79 @@ public:
                        }
        }
 
-       virtual ~_ExifDataOriginal()
+       ~ExifDataOriginal() override
        {
                if (cp_data_) g_free(cp_data_);
                if (pathl_) g_free(pathl_);
        }
 
-       virtual Exiv2::Image *image()
+       Exiv2::Image *image() override
        {
-               if (!valid_) return NULL;
+               if (!valid_) return nullptr;
                return image_.get();
        }
 
-       virtual Exiv2::ExifData &exifData ()
+       Exiv2::ExifData &exifData () override
        {
                if (!valid_) return emptyExifData_;
                return image_->exifData();
        }
 
-       virtual Exiv2::IptcData &iptcData ()
+       Exiv2::IptcData &iptcData () override
        {
                if (!valid_) return emptyIptcData_;
                return image_->iptcData();
        }
 
 #if EXIV2_TEST_VERSION(0,16,0)
-       virtual Exiv2::XmpData &xmpData ()
+       Exiv2::XmpData &xmpData () override
        {
                if (!valid_) return emptyXmpData_;
                return image_->xmpData();
        }
 #endif
 
-       virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
+       void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
        {
                if (cp_data_) g_free(cp_data_);
                cp_data_ = cp_data;
                cp_length_ = cp_length;
        }
 
-       virtual guchar *get_jpeg_color_profile(guint *data_len)
+       guchar *get_jpeg_color_profile(guint *data_len) override
        {
                if (cp_data_)
                {
                        if (data_len) *data_len = cp_length_;
-                       return (unsigned char *) g_memdup(cp_data_, cp_length_);
+#if GLIB_CHECK_VERSION(2,68,0)
+                       return static_cast<unsigned char *>(g_memdup2(cp_data_, cp_length_));
+#else
+                       return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
+#endif
                }
-               return NULL;
+               return nullptr;
        }
 
-       virtual std::string image_comment() const
+       std::string image_comment() const override
        {
                return image_.get() ? image_->comment() : "";
        }
 
-       virtual void set_image_comment(const std::string& comment)
+       void set_image_comment(const std::string& comment) override
        {
                if (image_.get())
                        image_->setComment(comment);
        }
 };
 
-extern "C" {
 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
-}
 
 // This allows read-write access to the metadata
-struct _ExifDataProcessed : public _ExifData
+struct ExifDataProcessed : public ExifData
 {
 protected:
-       _ExifDataOriginal *imageData_;
-       _ExifDataOriginal *sidecarData_;
+       std::unique_ptr<ExifDataOriginal> imageData_;
+       std::unique_ptr<ExifDataOriginal> sidecarData_;
 
        Exiv2::ExifData exifData_;
        Exiv2::IptcData iptcData_;
@@ -315,14 +303,14 @@ protected:
 #endif
 
 public:
-       _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
+       ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
        {
-               imageData_ = new _ExifDataOriginal(path);
-               sidecarData_ = NULL;
+               imageData_ = std::make_unique<ExifDataOriginal>(path);
+               sidecarData_ = nullptr;
 #if EXIV2_TEST_VERSION(0,16,0)
                if (sidecar_path)
                        {
-                       sidecarData_ = new _ExifDataOriginal(sidecar_path);
+                       sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
                        xmpData_ = sidecarData_->xmpData();
                        }
                else
@@ -349,18 +337,12 @@ public:
                        }
        }
 
-       virtual ~_ExifDataProcessed()
-       {
-               if (imageData_) delete imageData_;
-               if (sidecarData_) delete sidecarData_;
-       }
-
-       virtual ExifData *original()
+       ExifData *original() override
        {
-               return imageData_;
+               return imageData_.get();
        }
 
-       virtual void writeMetadata(gchar *path = NULL)
+       void writeMetadata(gchar *path = nullptr) override
        {
                if (!path)
                        {
@@ -374,7 +356,15 @@ public:
 #endif
                        Exiv2::Image *image = imageData_->image();
 
-                       if (!image) Exiv2::Error(21);
+#ifdef HAVE_EXIV2_ERROR_CODE
+#if EXIV2_TEST_VERSION(0,28,0)
+            if (!image) throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
+#else
+                       if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
+#endif
+#else
+                       if (!image) throw Exiv2::Error(21);
+#endif
                        image->setExifData(exifData_);
                        image->setIptcData(iptcData_);
 #if EXIV2_TEST_VERSION(0,16,0)
@@ -387,56 +377,60 @@ public:
 #if EXIV2_TEST_VERSION(0,17,0)
                        gchar *pathl = path_from_utf8(path);;
 
-                       Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
+                       auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
 
                        g_free(pathl);
 
                        sidecar->setXmpData(xmpData_);
                        sidecar->writeMetadata();
+#else
+#ifdef HAVE_EXIV2_ERROR_CODE
+                       throw Exiv2::Error(Exiv2::kerNotAnImage, "xmp");
 #else
                        throw Exiv2::Error(3, "xmp");
+#endif
 #endif
                        }
        }
 
-       virtual Exiv2::Image *image()
+       Exiv2::Image *image() override
        {
                return imageData_->image();
        }
 
-       virtual Exiv2::ExifData &exifData ()
+       Exiv2::ExifData &exifData () override
        {
                return exifData_;
        }
 
-       virtual Exiv2::IptcData &iptcData ()
+       Exiv2::IptcData &iptcData () override
        {
                return iptcData_;
        }
 
 #if EXIV2_TEST_VERSION(0,16,0)
-       virtual Exiv2::XmpData &xmpData ()
+       Exiv2::XmpData &xmpData () override
        {
                return xmpData_;
        }
 #endif
 
-       virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
+       void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
        {
                imageData_->add_jpeg_color_profile(cp_data, cp_length);
        }
 
-       virtual guchar *get_jpeg_color_profile(guint *data_len)
+       guchar *get_jpeg_color_profile(guint *data_len) override
        {
                return imageData_->get_jpeg_color_profile(data_len);
        }
 
-       virtual std::string image_comment() const
+       std::string image_comment() const override
        {
                return imageData_->image_comment();
        }
 
-       virtual void set_image_comment(const std::string& comment)
+       void set_image_comment(const std::string& comment) override
        {
                imageData_->set_image_comment(comment);
        }
@@ -445,32 +439,35 @@ public:
 
 
 
-extern "C" {
 
 
-void exif_init(void)
+void exif_init()
 {
 #ifdef EXV_ENABLE_NLS
        bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
 #endif
+
+#ifdef EXV_ENABLE_BMFF
+       Exiv2::enableBMFF(TRUE);
+#endif
 }
 
 
 
 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
 {
-       exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
+       exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
 }
 
 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
 {
        DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
        try {
-               return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
+               return new ExifDataProcessed(path, sidecar_path, modified_xmp);
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 
 }
@@ -504,7 +501,7 @@ gboolean exif_write_sidecar(ExifData *exif, gchar *path)
 void exif_free(ExifData *exif)
 {
        if (!exif) return;
-       g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
+       g_assert(dynamic_cast<ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
        delete exif;
 }
 
@@ -517,71 +514,71 @@ ExifData *exif_get_original(ExifData *exif)
 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
 {
        try {
-               Exiv2::Metadatum *item = NULL;
+               Exiv2::Metadatum *item = nullptr;
                try {
                        Exiv2::ExifKey ekey(key);
-                       Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
-                       if (pos == exif->exifData().end()) return NULL;
+                       auto pos = exif->exifData().findKey(ekey);
+                       if (pos == exif->exifData().end()) return nullptr;
                        item = &*pos;
                }
                catch (Exiv2::AnyError& e) {
                        try {
                                Exiv2::IptcKey ekey(key);
-                               Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
-                               if (pos == exif->iptcData().end()) return NULL;
+                               auto pos = exif->iptcData().findKey(ekey);
+                               if (pos == exif->iptcData().end()) return nullptr;
                                item = &*pos;
                        }
                        catch (Exiv2::AnyError& e) {
 #if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey ekey(key);
-                               Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
-                               if (pos == exif->xmpData().end()) return NULL;
+                               auto pos = exif->xmpData().findKey(ekey);
+                               if (pos == exif->xmpData().end()) return nullptr;
                                item = &*pos;
 #endif
                        }
                }
-               return (ExifItem *)item;
+               return reinterpret_cast<ExifItem *>(item);
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
 {
        try {
-               Exiv2::Metadatum *item = NULL;
+               Exiv2::Metadatum *item = nullptr;
                try {
                        Exiv2::ExifKey ekey(key);
-                       exif->exifData().add(ekey, NULL);
-                       Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
+                       exif->exifData().add(ekey, nullptr);
+                       auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
                        pos--;
                        item = &*pos;
                }
                catch (Exiv2::AnyError& e) {
                        try {
                                Exiv2::IptcKey ekey(key);
-                               exif->iptcData().add(ekey, NULL);
-                               Exiv2::IptcData::iterator pos = exif->iptcData().end();
+                               exif->iptcData().add(ekey, nullptr);
+                               auto pos = exif->iptcData().end();
                                pos--;
                                item = &*pos;
                        }
                        catch (Exiv2::AnyError& e) {
 #if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey ekey(key);
-                               exif->xmpData().add(ekey, NULL);
-                               Exiv2::XmpData::iterator pos = exif->xmpData().end();
+                               exif->xmpData().add(ekey, nullptr);
+                               auto pos = exif->xmpData().end();
                                pos--;
                                item = &*pos;
 #endif
                        }
                }
-               return (ExifItem *)item;
+               return reinterpret_cast<ExifItem *>(item);
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -614,12 +611,12 @@ ExifItem *exif_get_first_item(ExifData *exif)
                        return (ExifItem *)item;
                        }
 #endif
-               return NULL;
+               return nullptr;
 
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -646,23 +643,23 @@ ExifItem *exif_get_next_item(ExifData *exif)
                        return (ExifItem *)item;
                }
 #endif
-               return NULL;
+               return nullptr;
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
 char *exif_item_get_tag_name(ExifItem *item)
 {
        try {
-               if (!item) return NULL;
-               return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
+               if (!item) return nullptr;
+               return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -670,7 +667,7 @@ guint exif_item_get_tag_id(ExifItem *item)
 {
        try {
                if (!item) return 0;
-               return ((Exiv2::Metadatum *)item)->tag();
+               return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
@@ -682,7 +679,7 @@ guint exif_item_get_elements(ExifItem *item)
 {
        try {
                if (!item) return 0;
-               return ((Exiv2::Metadatum *)item)->count();
+               return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
@@ -693,29 +690,28 @@ guint exif_item_get_elements(ExifItem *item)
 char *exif_item_get_data(ExifItem *item, guint *data_len)
 {
        try {
-               if (!item) return 0;
-               Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
+               if (!item) return nullptr;
+               auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
                if (data_len) *data_len = md->size();
-               char *data = (char *)g_malloc(md->size());
-               long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
+               auto data = static_cast<char *>(g_malloc(md->size()));
+               long res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
                g_assert(res == md->size());
                return data;
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
 char *exif_item_get_description(ExifItem *item)
 {
        try {
-               if (!item) return NULL;
-               return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
+               if (!item) return nullptr;
+               return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
        }
        catch (std::exception& e) {
-//             debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -756,7 +752,7 @@ guint exif_item_get_format_id(ExifItem *item)
 {
        try {
                if (!item) return EXIF_FORMAT_UNKNOWN;
-               guint id = ((Exiv2::Metadatum *)item)->typeId();
+               guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
                if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
                return format_id_trans_tbl[id];
        }
@@ -766,26 +762,26 @@ guint exif_item_get_format_id(ExifItem *item)
        }
 }
 
-const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
+const char *exif_item_get_format_name(ExifItem *item, gboolean)
 {
        try {
-               if (!item) return NULL;
-               return ((Exiv2::Metadatum *)item)->typeName();
+               if (!item) return nullptr;
+               return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
 
-gchar *exif_item_get_data_as_text(ExifItem *item)
+gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
 {
        try {
-               if (!item) return NULL;
-               Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
+               if (!item) return nullptr;
+               auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
 #if EXIV2_TEST_VERSION(0,17,0)
-               return utf8_validate_or_convert(metadatum->print().c_str());
+               return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
 #else
                std::stringstream str;
                Exiv2::Exifdatum *exifdatum;
@@ -806,21 +802,21 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
 #endif
        }
        catch (Exiv2::AnyError& e) {
-               return NULL;
+               return nullptr;
        }
 }
 
 gchar *exif_item_get_string(ExifItem *item, int idx)
 {
        try {
-               if (!item) return NULL;
-               Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
+               if (!item) return nullptr;
+               auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
 #if EXIV2_TEST_VERSION(0,16,0)
                std::string str = em->toString(idx);
 #else
-               std::string str = em->toString(); // FIXME
+               std::string str = em->toString(); /**< @FIXME ?? */
 #endif
-               if (idx == 0 && str == "") str = em->toString();
+               if (idx == 0 && str.empty()) str = em->toString();
                if (str.length() > 5 && str.substr(0, 5) == "lang=")
                        {
                        std::string::size_type pos = str.find_first_of(' ');
@@ -830,7 +826,7 @@ gchar *exif_item_get_string(ExifItem *item, int idx)
                return utf8_validate_or_convert(str.c_str());
        }
        catch (Exiv2::AnyError& e) {
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -839,7 +835,12 @@ gint exif_item_get_integer(ExifItem *item, gint *value)
 {
        try {
                if (!item || exif_item_get_elements(item) == 0) return 0;
-               *value = ((Exiv2::Metadatum *)item)->toLong();
+
+#if EXIV2_TEST_VERSION(0,28,0)
+        *value = ((Exiv2::Metadatum *)item)->toInt64();
+#else
+               *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
+#endif
                return 1;
        }
        catch (Exiv2::AnyError& e) {
@@ -851,18 +852,18 @@ gint exif_item_get_integer(ExifItem *item, gint *value)
 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
 {
        try {
-               if (!item) return NULL;
-               if (n >= exif_item_get_elements(item)) return NULL;
-               Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
+               if (!item) return nullptr;
+               if (n >= exif_item_get_elements(item)) return nullptr;
+               Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
                static ExifRational ret;
                ret.num = v.first;
                ret.den = v.second;
-               if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
+               if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
                return &ret;
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -886,23 +887,18 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
                        }
                        catch (Exiv2::AnyError& e) {
                                debug_exception(e);
-                               return NULL;
+                               return nullptr;
                        }
                }
        }
-       return NULL;
+       return nullptr;
 }
 
 static const AltKey *find_alt_key(const gchar *xmp_key)
 {
-       gint i = 0;
-
-       while (alt_keys[i].xmp_key)
-               {
-               if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
-               i++;
-               }
-       return NULL;
+       for (const auto& k : alt_keys)
+               if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
+       return nullptr;
 }
 
 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
@@ -913,7 +909,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                try {
                        Exiv2::ExifKey ekey(key);
 
-                       Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
+                       auto pos = exif->exifData().findKey(ekey);
                        while (pos != exif->exifData().end())
                                {
                                exif->exifData().erase(pos);
@@ -922,7 +918,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 
                        while (work)
                                {
-                               exif->exifData()[key] = (gchar *)work->data;
+                               exif->exifData()[key] = static_cast<gchar *>(work->data);
                                work = work->next;
                                }
                }
@@ -932,7 +928,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 #endif
                        {
                                Exiv2::IptcKey ekey(key);
-                               Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
+                               auto pos = exif->iptcData().findKey(ekey);
                                while (pos != exif->iptcData().end())
                                        {
                                        exif->iptcData().erase(pos);
@@ -941,14 +937,14 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 
                                while (work)
                                        {
-                                       exif->iptcData()[key] = (gchar *)work->data;
+                                       exif->iptcData()[key] = static_cast<gchar *>(work->data);
                                        work = work->next;
                                        }
                        }
 #if EXIV2_TEST_VERSION(0,16,0)
                        catch (Exiv2::AnyError& e) {
                                Exiv2::XmpKey ekey(key);
-                               Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
+                               auto pos = exif->xmpData().findKey(ekey);
                                while (pos != exif->xmpData().end())
                                        {
                                        exif->xmpData().erase(pos);
@@ -957,7 +953,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 
                                while (work)
                                        {
-                                       exif->xmpData()[key] = (gchar *)work->data;
+                                       exif->xmpData()[key] = static_cast<gchar *>(work->data);
                                        work = work->next;
                                        }
                        }
@@ -980,7 +976,7 @@ gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
            TRUE || /* no conversion support */
 #endif
            !values || /* deleting item */
-           !ret  /* writing to the explicitely given xmp tag failed */
+           !ret  /* writing to the explicitly given xmp tag failed */
            )
                {
                /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
@@ -1072,11 +1068,11 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
 
 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
 {
-       GList *list = NULL;
+       GList *list = nullptr;
        try {
                try {
                        Exiv2::ExifKey ekey(key);
-                       Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
+                       auto pos = exif->exifData().findKey(ekey);
                        if (pos != exif->exifData().end())
                                list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
 
@@ -1084,11 +1080,11 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, Metadat
                catch (Exiv2::AnyError& e) {
                        try {
                                Exiv2::IptcKey ekey(key);
-                               Exiv2::IptcData::iterator pos = exif->iptcData().begin();
+                               auto pos = exif->iptcData().begin();
                                while (pos != exif->iptcData().end())
                                        {
                                        if (pos->key() == key)
-                                               list = exif_add_value_to_glist(list, *pos, format, NULL);
+                                               list = exif_add_value_to_glist(list, *pos, format, nullptr);
                                        ++pos;
                                        }
 
@@ -1096,9 +1092,9 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, Metadat
                        catch (Exiv2::AnyError& e) {
 #if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey ekey(key);
-                               Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
+                               auto pos = exif->xmpData().findKey(ekey);
                                if (pos != exif->xmpData().end())
-                                       list = exif_add_value_to_glist(list, *pos, format, NULL);
+                                       list = exif_add_value_to_glist(list, *pos, format, nullptr);
 #endif
                        }
                }
@@ -1111,16 +1107,16 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, Metadat
 
 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
 {
-       GList *list = NULL;
+       GList *list = nullptr;
 
-       if (!key) return NULL;
+       if (!key) return nullptr;
 
        if (format == METADATA_FORMATTED)
                {
                gchar *text;
                gint key_valid;
                text = exif_get_formatted_by_key(exif, key, &key_valid);
-               if (key_valid) return g_list_append(NULL, text);
+               if (key_valid) return g_list_append(nullptr, text);
                }
 
        list = exif_get_metadata_simple(exif, key, format);
@@ -1154,7 +1150,7 @@ guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
 
        ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
        if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
-               ret = (guchar *)exif_item_get_data(prof_item, data_len);
+               ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
        return ret;
 }
 
@@ -1179,15 +1175,15 @@ void exif_set_image_comment(FileData* fd, const gchar* comment)
 
 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
 {
-       if (!exif) return NULL;
+       if (!exif) return nullptr;
 
-       if (!exif->image()) return NULL;
+       if (!exif->image()) return nullptr;
 
        std::string const path = exif->image()->io().path();
        /* given image pathname, first do simple (and fast) file extension test */
        gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
 
-       if (!is_raw && requested_width == 0) return NULL;
+       if (!is_raw && requested_width == 0) return nullptr;
 
        try {
 
@@ -1198,7 +1194,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                if (!list.empty())
                        {
                        Exiv2::PreviewPropertiesList::iterator pos;
-                       Exiv2::PreviewPropertiesList::iterator last = --list.end();
+                       auto last = --list.end();
 
                        if (requested_width == 0)
                                {
@@ -1209,41 +1205,42 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                                pos = list.begin();
                                while (pos != last)
                                        {
-                                       if (pos->width_ >= (uint32_t)requested_width &&
-                                           pos->height_ >= (uint32_t)requested_height) break;
+                                       if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
+                                           pos->height_ >= static_cast<uint32_t>(requested_height)) break;
                                        ++pos;
                                        }
 
                                // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
                                if (!is_raw)
                                        {
-                                       if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
+                                       if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return nullptr;
                                        }
                                }
 
                        Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
 
-                       Exiv2::DataBuf buf = image.copy();
-                       std::pair<Exiv2::byte*, long> p = buf.release();
-
-                       *data_len = p.second;
-                       return p.first;
+                       // Let's not touch data_len until we finish copy.
+                       // Just in case we run into OOM.
+                       size_t img_sz = image.size();
+                       auto* b = new Exiv2::byte[img_sz];
+                       std::copy_n(image.pData(), img_sz, b);
+                       *data_len = img_sz;
+                       return b;
                        }
-               return NULL;
+               return nullptr;
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
-               return NULL;
+               return nullptr;
        }
 }
 
-void exif_free_preview(guchar *buf)
+void exif_free_preview(const guchar *buf)
 {
-       delete[] (Exiv2::byte*)buf;
+       delete[] static_cast<Exiv2::byte*>(buf);
 }
 #endif
 
-}
 #if !EXIV2_TEST_VERSION(0,17,90)
 
 /* This is a dirty hack to support raw file preview, bassed on
@@ -1268,8 +1265,7 @@ class RawFile {
        unsigned long offset;
 };
 
-typedef struct _UnmapData UnmapData;
-struct _UnmapData
+struct UnmapData
 {
        guchar *ptr;
        guchar *map_data;
@@ -1278,7 +1274,7 @@ struct _UnmapData
 
 static GList *exif_unmap_list = 0;
 
-extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
+guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
 {
        unsigned long offset;
 
@@ -1383,22 +1379,22 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
        type = Exiv2::ImageFactory::getType(map_data, map_len);
 
 #if EXIV2_TEST_VERSION(0,16,0)
-       TiffHeaderBase *tiffHeader = NULL;
+       std::unique_ptr<TiffHeaderBase> tiffHeader;
 #else
-       TiffHeade2 *tiffHeader = NULL;
+       std::unique_ptr<TiffHeade2> tiffHeader;
 #endif
-       Cr2Header *cr2Header = NULL;
+       std::unique_ptr<Cr2Header> cr2Header;
 
        switch (type) {
                case Exiv2::ImageType::tiff:
-                       tiffHeader = new TiffHeade2();
+                       tiffHeader = std::make_unique<TiffHeade2>();
                        break;
                case Exiv2::ImageType::cr2:
-                       cr2Header = new Cr2Header();
+                       cr2Header = std::make_unique<Cr2Header>();
                        break;
 #if EXIV2_TEST_VERSION(0,16,0)
                case Exiv2::ImageType::orf:
-                       tiffHeader = new OrfHeader();
+                       tiffHeader = std::make_unique<OrfHeader>();
                        break;
 #endif
 #if EXIV2_TEST_VERSION(0,13,0)
@@ -1410,7 +1406,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                case Exiv2::ImageType::crw:
                        {
                        // Parse the image, starting with a CIFF header component
-                       Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
+                       auto parseTree = std::make_unique<Exiv2::CiffHeader>();
                        parseTree->read(map_data, map_len);
                        CiffComponent *entry = parseTree->findComponent(0x2007, 0);
                        if (entry) offset =  entry->pData() - map_data;
@@ -1453,11 +1449,6 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                          state);
 
        rootDir->accept(reader);
-
-       if (tiffHeader)
-               delete tiffHeader;
-       if (cr2Header)
-               delete cr2Header;
 }
 
 RawFile::~RawFile(void)
@@ -1488,22 +1479,41 @@ unsigned long RawFile::preview_offset(void)
        if (type == Exiv2::ImageType::cr2)
                {
                val = find(0x111, Group::ifd0);
-               if (val) return val->toLong();
-
+#if EXIV2_TEST_VERSION(0,28,0)
+               if (val) return val->toInt64();
+#else
+               if (val) return val->tolong();
+#endif
                return 0;
                }
 
        val = find(0x201, Group::sub0_0);
-       if (val) return val->toLong();
+#if EXIV2_TEST_VERSION(0,28,0)
+       if (val) return val->toInt64();
+#else
+       if (val) return val->tolong();
+#endif
 
        val = find(0x201, Group::ifd0);
-       if (val) return val->toLong();
+#if EXIV2_TEST_VERSION(0,28,0)
+       if (val) return val->toInt64();
+#else
+       if (val) return val->tolong();
+#endif
 
        val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
-       if (val) return val->toLong();
+#if EXIV2_TEST_VERSION(0,28,0)
+       if (val) return val->toInt64();
+#else
+       if (val) return val->tolong();
+#endif
 
        val = find(0x111, Group::sub0_1); // dng
-       if (val) return val->toLong();
+#if EXIV2_TEST_VERSION(0,28,0)
+       if (val) return val->toInt64();
+#else
+       if (val) return val->tolong();
+#endif
 
        return 0;
 }