Replace g_memdup with safer g_memdup2
[geeqie.git] / src / exiv2.cc
index 455c8d3..a2c16c4 100644 (file)
@@ -1,28 +1,46 @@
 /*
- * Geeqie
- * Copyright (C) 2008 - 2012 The Geeqie Team
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: Vladimir Nadvornik
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 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 "filefilter.h"
-#include "ui_fileops.h"
+#include "ui-fileops.h"
 
 #include "misc.h"
-}
 
 typedef struct _AltKey AltKey;
 
@@ -124,8 +121,8 @@ struct _ExifData
        virtual ~_ExifData()
        {
        }
-       
-       virtual void writeMetadata(gchar *path = NULL)
+
+       virtual void writeMetadata(gchar *UNUSED(path) = NULL)
        {
                g_critical("Unsupported method of writing metadata");
        }
@@ -136,7 +133,7 @@ struct _ExifData
        }
 
        virtual Exiv2::Image *image() = 0;
-       
+
        virtual Exiv2::ExifData &exifData() = 0;
 
        virtual Exiv2::IptcData &iptcData() = 0;
@@ -148,6 +145,10 @@ struct _ExifData
        virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
 
        virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
+
+       virtual std::string image_comment() const = 0;
+
+       virtual void set_image_comment(const std::string& comment) = 0;
 };
 
 // This allows read-only access to the original metadata
@@ -160,6 +161,7 @@ protected:
        unsigned char *cp_data_;
        guint cp_length_;
        gboolean valid_;
+       gchar *pathl_;
 
        Exiv2::ExifData emptyExifData_;
        Exiv2::IptcData emptyIptcData_;
@@ -181,11 +183,11 @@ public:
                cp_data_ = NULL;
                cp_length_ = 0;
                valid_ = TRUE;
-               
-               gchar *pathl = path_from_utf8(path);
-               try 
+
+               pathl_ = path_from_utf8(path);
+               try
                        {
-                       image_ = Exiv2::ImageFactory::open(pathl);
+                       image_ = Exiv2::ImageFactory::open(pathl_);
 //                     g_assert (image.get() != 0);
                        image_->readMetadata();
 
@@ -215,24 +217,24 @@ public:
                                }
 #endif
                        }
-               catch (Exiv2::AnyError& e) 
+               catch (Exiv2::AnyError& e)
                        {
                        valid_ = FALSE;
                        }
-               g_free(pathl);
        }
-       
+
        virtual ~_ExifDataOriginal()
        {
                if (cp_data_) g_free(cp_data_);
+               if (pathl_) g_free(pathl_);
        }
-       
+
        virtual Exiv2::Image *image()
        {
                if (!valid_) return NULL;
                return image_.get();
        }
-       
+
        virtual Exiv2::ExifData &exifData ()
        {
                if (!valid_) return emptyExifData_;
@@ -265,15 +267,28 @@ public:
                if (cp_data_)
                {
                        if (data_len) *data_len = cp_length_;
+#if GLIB_CHECK_VERSION(2,68,0)
+                       return (unsigned char *) g_memdup2(cp_data_, cp_length_);
+#else
                        return (unsigned char *) g_memdup(cp_data_, cp_length_);
+#endif
                }
                return NULL;
        }
+
+       virtual std::string image_comment() const
+       {
+               return image_.get() ? image_->comment() : "";
+       }
+
+       virtual void set_image_comment(const std::string& comment)
+       {
+               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
@@ -339,7 +354,7 @@ public:
                if (!path)
                        {
 #if EXIV2_TEST_VERSION(0,17,0)
-                       if (options->metadata.save_legacy_IPTC) 
+                       if (options->metadata.save_legacy_IPTC)
                                copyXmpToIptc(xmpData_, iptcData_);
                        else
                                iptcData_.clear();
@@ -347,37 +362,45 @@ public:
                        copyXmpToExif(xmpData_, exifData_);
 #endif
                        Exiv2::Image *image = imageData_->image();
-                       
-                       if (!image) Exiv2::Error(21);
+
+#ifdef HAVE_EXIV2_ERROR_CODE
+                       if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
+#else
+                       if (!image) throw Exiv2::Error(21);
+#endif
                        image->setExifData(exifData_);
                        image->setIptcData(iptcData_);
 #if EXIV2_TEST_VERSION(0,16,0)
                        image->setXmpData(xmpData_);
 #endif
                        image->writeMetadata();
-                       } 
+                       }
                else
                        {
 #if EXIV2_TEST_VERSION(0,17,0)
                        gchar *pathl = path_from_utf8(path);;
 
                        Exiv2::Image::AutoPtr 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()
        {
                return imageData_->image();
        }
-       
+
        virtual Exiv2::ExifData &exifData ()
        {
                return exifData_;
@@ -404,12 +427,21 @@ public:
        {
                return imageData_->get_jpeg_color_profile(data_len);
        }
+
+       virtual std::string image_comment() const
+       {
+               return imageData_->image_comment();
+       }
+
+       virtual void set_image_comment(const std::string& comment)
+       {
+               imageData_->set_image_comment(comment);
+       }
 };
 
 
 
 
-extern "C" {
 
 
 void exif_init(void)
@@ -417,6 +449,10 @@ void exif_init(void)
 #ifdef EXV_ENABLE_NLS
        bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
 #endif
+
+#ifdef EXV_ENABLE_BMFF
+       Exiv2::enableBMFF(TRUE);
+#endif
 }
 
 
@@ -436,7 +472,7 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
                debug_exception(e);
                return NULL;
        }
-       
+
 }
 
 gboolean exif_write(ExifData *exif)
@@ -461,7 +497,7 @@ gboolean exif_write_sidecar(ExifData *exif, gchar *path)
                debug_exception(e);
                return FALSE;
        }
-       
+
 }
 
 
@@ -579,7 +615,7 @@ ExifItem *exif_get_first_item(ExifData *exif)
                        }
 #endif
                return NULL;
-                       
+
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
@@ -713,8 +749,8 @@ static guint format_id_trans_tbl [] = {
        EXIF_FORMAT_STRING,
        EXIF_FORMAT_STRING
        };
-       
-       
+
+
 
 guint exif_item_get_format_id(ExifItem *item)
 {
@@ -730,7 +766,7 @@ 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 UNUSED(brief))
 {
        try {
                if (!item) return NULL;
@@ -782,7 +818,7 @@ gchar *exif_item_get_string(ExifItem *item, int idx)
 #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 (str.length() > 5 && str.substr(0, 5) == "lang=")
@@ -860,7 +896,7 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
 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];
@@ -876,7 +912,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);
                        while (pos != exif->exifData().end())
                                {
@@ -892,7 +928,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                }
                catch (Exiv2::AnyError& e) {
 #if EXIV2_TEST_VERSION(0,16,0)
-                       try 
+                       try
 #endif
                        {
                                Exiv2::IptcKey ekey(key);
@@ -938,13 +974,13 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
 {
        gint ret = exif_update_metadata_simple(exif, key, values);
-       
+
        if (
 #if !EXIV2_TEST_VERSION(0,17,0)
            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 */
@@ -975,17 +1011,17 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
            id == Exiv2::comment
            )
                {
-#endif 
+#endif
                /* read as a single entry */
                std::string str;
-               
+
                if (format == METADATA_FORMATTED)
                        {
 #if EXIV2_TEST_VERSION(0,17,0)
                        str = item.print(
 #if EXIV2_TEST_VERSION(0,18,0)
                                        metadata
-#endif 
+#endif
                                        );
 #else
                        std::stringstream stream;
@@ -1020,7 +1056,7 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
                        std::string::size_type pos = str.find_first_of(' ');
                        if (pos != std::string::npos) str = str.substr(pos+1);
                        }
-               list = g_list_append(list, utf8_validate_or_convert(str.c_str())); 
+               list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
 #if EXIV2_TEST_VERSION(0,16,0)
                }
        else
@@ -1028,9 +1064,9 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
                /* read as a list */
                gint i;
                for (i = 0; i < item.count(); i++)
-                       list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str())); 
+                       list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
                }
-#endif 
+#endif
        return list;
 }
 
@@ -1086,9 +1122,9 @@ GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format
                text = exif_get_formatted_by_key(exif, key, &key_valid);
                if (key_valid) return g_list_append(NULL, text);
                }
-               
+
        list = exif_get_metadata_simple(exif, key, format);
-       
+
        /* the following code can be ifdefed out as soon as Exiv2 supports it */
        if (!list)
                {
@@ -1096,7 +1132,7 @@ GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format
                if (alt_key && alt_key->iptc_key)
                        list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
 
-#if !EXIV2_TEST_VERSION(0,17,0)        
+#if !EXIV2_TEST_VERSION(0,17,0)
                /* with older Exiv2 versions exif is not synced */
                if (!list && alt_key && alt_key->exif_key)
                        list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
@@ -1122,6 +1158,23 @@ guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
        return ret;
 }
 
+gchar* exif_get_image_comment(FileData* fd)
+{
+       if (!fd || !fd->exif)
+               return g_strdup("");
+
+       return g_strdup(fd->exif->image_comment().c_str());
+}
+
+void exif_set_image_comment(FileData* fd, const gchar* comment)
+{
+       if (!fd || !fd->exif)
+               return;
+
+       fd->exif->set_image_comment(comment ? comment : "");
+}
+
+
 #if EXIV2_TEST_VERSION(0,17,90)
 
 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
@@ -1129,11 +1182,11 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
        if (!exif) return NULL;
 
        if (!exif->image()) return NULL;
-       
-       const char* path = exif->image()->io().path().c_str();
+
+       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, FORMAT_CLASS_RAWIMAGE);
-       
+       gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
+
        if (!is_raw && requested_width == 0) return NULL;
 
        try {
@@ -1146,7 +1199,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                        {
                        Exiv2::PreviewPropertiesList::iterator pos;
                        Exiv2::PreviewPropertiesList::iterator last = --list.end();
-                       
+
                        if (requested_width == 0)
                                {
                                pos = last; // the largest
@@ -1160,11 +1213,11 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                                            pos->height_ >= (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 (!is_raw)
                                        {
-                                       if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL; 
+                                       if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
                                        }
                                }
 
@@ -1190,7 +1243,6 @@ void exif_free_preview(guchar *buf)
 }
 #endif
 
-}
 #if !EXIV2_TEST_VERSION(0,17,90)
 
 /* This is a dirty hack to support raw file preview, bassed on
@@ -1225,17 +1277,17 @@ 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;
 
        if (!exif) return NULL;
        if (!exif->image()) return NULL;
 
-       const char* path = exif->image()->io().path().c_str();
+       std::string const path = exif->image()->io().path();
 
        /* given image pathname, first do simple (and fast) file extension test */
-       if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
+       if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
 
        try {
                struct stat st;
@@ -1243,12 +1295,12 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
                size_t map_len;
                UnmapData *ud;
                int fd;
-               
+
                RawFile rf(exif->image()->io());
                offset = rf.preview_offset();
-               DEBUG_1("%s: offset %lu", path, offset);
-               
-               fd = open(path, O_RDONLY);
+               DEBUG_1("%s: offset %lu", path.c_str(), offset);
+
+               fd = open(path.c_str(), O_RDONLY);
                if (fd == -1)
                        {
                        return NULL;
@@ -1271,10 +1323,10 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
                ud->ptr = map_data + offset;
                ud->map_data = map_data;
                ud->map_len = map_len;
-               
+
                exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
                return ud->ptr;
-               
+
        }
        catch (Exiv2::AnyError& e) {
                debug_exception(e);
@@ -1286,7 +1338,7 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
 void exif_free_preview(guchar *buf)
 {
        GList *work = exif_unmap_list;
-       
+
        while (work)
                {
                UnmapData *ud = (UnmapData *)work->data;
@@ -1322,7 +1374,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
         if (io.open() != 0) {
             throw Error(9, io.path(), strError());
         }
-        
+
         map_data = io.mmap();
         map_len = io.size();
 
@@ -1376,7 +1428,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
        if (0 == rootDir.get()) {
                throw Error(1, "No root element defined in TIFF structure");
        }
-       
+
        if (tiffHeader)
                {
                if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
@@ -1386,12 +1438,12 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                rootDir->setStart(map_data + tiffHeader->ifdOffset());
 #endif
                }
-               
+
        if (cr2Header)
                {
                rootDir->setStart(map_data + cr2Header->offset());
                }
-       
+
        TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
 
        TiffReader reader(map_data,
@@ -1400,7 +1452,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                          state);
 
        rootDir->accept(reader);
-       
+
        if (tiffHeader)
                delete tiffHeader;
        if (cr2Header)
@@ -1431,7 +1483,7 @@ unsigned long RawFile::preview_offset(void)
 {
        const Value *val;
        if (offset) return offset;
-       
+
        if (type == Exiv2::ImageType::cr2)
                {
                val = find(0x111, Group::ifd0);
@@ -1439,7 +1491,7 @@ unsigned long RawFile::preview_offset(void)
 
                return 0;
                }
-       
+
        val = find(0x201, Group::sub0_0);
        if (val) return val->toLong();