[easytag] Fix Picture_Format_From_Data()



commit 1fdb2ef8382e0bbce97b60f4259a01dc961e1ff6
Author: David King <amigadave amigadave com>
Date:   Wed Dec 17 22:18:16 2014 +0000

    Fix Picture_Format_From_Data()
    
    Check the result of memcmp() against 0. Extend the JPEG magic number.
    Add a simple test.

 src/picture.c        |   10 +++++-----
 tests/test-picture.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 5 deletions(-)
---
diff --git a/src/picture.c b/src/picture.c
index ef022f1..cc3a233 100644
--- a/src/picture.c
+++ b/src/picture.c
@@ -120,26 +120,26 @@ Picture_Format_From_Data (const EtPicture *pic)
 
     data = g_bytes_get_data (pic->bytes, &size);
 
-    /* JPEG : "\xff\xd8". */
-    if (size > 2 && memcmp (data, "\xff\xd8", 2))
+    /* JPEG : "\xff\xd8\xff". */
+    if (size > 3 && (memcmp (data, "\xff\xd8\xff", 3) == 0))
     {
         return PICTURE_FORMAT_JPEG;
     }
 
     /* PNG : "\x89PNG\x0d\x0a\x1a\x0a". */
-    if (size > 8 && memcmp (data, "\x89PNG\x0d\x0a\x1a\x0a", 8))
+    if (size > 8 && (memcmp (data, "\x89PNG\x0d\x0a\x1a\x0a", 8) == 0))
     {
         return PICTURE_FORMAT_PNG;
     }
     
     /* GIF: "GIF87a" */
-    if (size > 6 && memcmp (data, "GIF87a", 6))
+    if (size > 6 && (memcmp (data, "GIF87a", 6) == 0))
     {
         return PICTURE_FORMAT_GIF;
     }
 
     /* GIF: "GIF89a" */
-    if (size > 6 && memcmp (data, "GIF89a", 6))
+    if (size > 6 && (memcmp (data, "GIF89a", 6) == 0))
     {
         return PICTURE_FORMAT_GIF;
     }
diff --git a/tests/test-picture.c b/tests/test-picture.c
index ea25733..1e8e316 100644
--- a/tests/test-picture.c
+++ b/tests/test-picture.c
@@ -18,6 +18,8 @@
 
 #include "picture.h"
 
+#include <string.h>
+
 static void
 picture_copy (void)
 {
@@ -131,12 +133,46 @@ picture_type_from_filename (void)
     }
 }
 
+static void
+picture_format_from_data (void)
+{
+    gsize i;
+
+    static const struct
+    {
+        const gchar *data;
+        Picture_Format format;
+    } pictures[] =
+    {
+        { "\xff\xd8", PICTURE_FORMAT_UNKNOWN },
+        { "\xff\xd8\xff", PICTURE_FORMAT_JPEG },
+        { "\x89PNG\x0d\x0a\x1a\x0a", PICTURE_FORMAT_PNG },
+        { "GIF87a", PICTURE_FORMAT_GIF },
+        { "GIF89a", PICTURE_FORMAT_GIF },
+        { "GIF900", PICTURE_FORMAT_UNKNOWN }
+    };
+
+    for (i = 0; i < G_N_ELEMENTS (pictures); i++)
+    {
+        EtPicture *pic;
+
+        pic = et_picture_new ();
+        pic->bytes = g_bytes_new_static (pictures[i].data,
+                                         strlen (pictures[i].data) + 1);
+        g_assert_cmpint (pictures[i].format, ==,
+                         Picture_Format_From_Data (pic));
+
+        et_picture_free (pic);
+    }
+}
+
 int
 main (int argc, char** argv)
 {
     g_test_init (&argc, &argv, NULL);
 
     g_test_add_func ("/picture/copy", picture_copy);
+    g_test_add_func ("/picture/format-from-data", picture_format_from_data);
     g_test_add_func ("/picture/type-from-filename",
                      picture_type_from_filename);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]