Replace g_memdup with safer g_memdup2
[geeqie.git] / src / exiv2.cc
index 0ad5c61..a2c16c4 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 "filefilter.h"
-#include "ui_fileops.h"
+#include "ui-fileops.h"
 
 #include "misc.h"
-}
 
 typedef struct _AltKey AltKey;
 
@@ -135,7 +122,7 @@ struct _ExifData
        {
        }
 
-       virtual void writeMetadata(gchar *path = NULL)
+       virtual void writeMetadata(gchar *UNUSED(path) = NULL)
        {
                g_critical("Unsupported method of writing metadata");
        }
@@ -280,7 +267,11 @@ 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;
        }
@@ -297,9 +288,7 @@ public:
        }
 };
 
-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
@@ -374,7 +363,11 @@ public:
 #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)
@@ -393,8 +386,12 @@ public:
 
                        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
                        }
        }
@@ -445,7 +442,6 @@ public:
 
 
 
-extern "C" {
 
 
 void exif_init(void)
@@ -453,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
 }
 
 
@@ -766,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;
@@ -818,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=")
@@ -980,7 +980,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 */
@@ -1243,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
@@ -1278,7 +1277,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;