]> git.imager.perl.org - imager.git/commitdiff
- eliminate sign warning from image.c
authorTony Cook <tony@develop=help.com>
Fri, 17 Feb 2006 08:31:28 +0000 (08:31 +0000)
committerTony Cook <tony@develop=help.com>
Fri, 17 Feb 2006 08:31:28 +0000 (08:31 +0000)
- make TIFF detection stricter

Changes
image.c
t/t1000files.t

diff --git a/Changes b/Changes
index 1f156575e8162ce9d32941b6b90a7010144e165c..b0b7b3b783d2dca36c86085d7eaaa3dd8217d4bf 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1354,6 +1354,8 @@ Revision history for Perl extension Imager.
   - document return values
   - add examples
   - add AUTHOR, SEE ALSO, REVISION
+- eliminate sign warning from image.c
+- make TIFF detection stricter
 
 =================================================================
 
diff --git a/image.c b/image.c
index 097f8c5691d752e8fd82ee0270f7e24cf94f8a39..2d1b2ae6be488ffc274c056010f535e3833954b7 100644 (file)
--- a/image.c
+++ b/image.c
@@ -2119,30 +2119,33 @@ Check the beginning of the supplied file for a 'magic number'
 =cut
 */
 
+#define FORMAT_ENTRY(magic, type) \
+  { (unsigned char *)(magic ""), sizeof(magic)-1, type }
 
 char *
 i_test_format_probe(io_glue *data, int length) {
-
   static struct {
-    char *magic;
+    unsigned char *magic;
+    size_t magic_size;
     char *name;
   } formats[] = {
-    {"\xFF\xD8", "jpeg"},
-    {"GIF87a", "gif"},
-    {"GIF89a", "gif"},
-    {"MM\0*", "tiff"},
-    {"II*\0", "tiff"},
-    {"BM", "bmp"},
-    {"\x89PNG\x0d\x0a\x1a\x0a", "png"},
-    {"P1", "pnm"},
-    {"P2", "pnm"},
-    {"P3", "pnm"},
-    {"P4", "pnm"},
-    {"P5", "pnm"},
-    {"P6", "pnm"},
+    FORMAT_ENTRY("\xFF\xD8", "jpeg"),
+    FORMAT_ENTRY("GIF87a", "gif"),
+    FORMAT_ENTRY("GIF89a", "gif"),
+    FORMAT_ENTRY("MM\0*", "tiff"),
+    FORMAT_ENTRY("II*\0", "tiff"),
+    FORMAT_ENTRY("BM", "bmp"),
+    FORMAT_ENTRY("\x89PNG\x0d\x0a\x1a\x0a", "png"),
+    FORMAT_ENTRY("P1", "pnm"),
+    FORMAT_ENTRY("P2", "pnm"),
+    FORMAT_ENTRY("P3", "pnm"),
+    FORMAT_ENTRY("P4", "pnm"),
+    FORMAT_ENTRY("P5", "pnm"),
+    FORMAT_ENTRY("P6", "pnm"),
   };
+
   unsigned int i;
-  char head[18];
+  unsigned char head[18];
   char *match = NULL;
   ssize_t rc;
 
@@ -2153,9 +2156,9 @@ i_test_format_probe(io_glue *data, int length) {
 
   for(i=0; i<sizeof(formats)/sizeof(formats[0]); i++) { 
     int c;
-    ssize_t len = strlen(formats[i].magic);
-    if (rc<len) continue;
-    c = !strncmp(formats[i].magic, head, len);
+    if (rc < formats[i].magic_size)
+      continue;
+    c = !memcmp(formats[i].magic, head, formats[i].magic_size);
     if (c) {
       match = formats[i].name;
       break;
@@ -2176,7 +2179,9 @@ i_test_format_probe(io_glue *data, int length) {
 
   if (!match && 
       (rc == 18) &&
-      tga_header_verify(head)) return "tga";
+      tga_header_verify(head))
+    return "tga";
+
   return match;
 }
 
index 0497fdfaee8fa706757cde8ef926596c222b86bf..b306e2c0ae5c326c279082a49a27365bd8d5e243 100644 (file)
@@ -5,7 +5,7 @@
 
 use strict;
 use lib 't';
-use Test::More tests => 12;
+use Test::More tests => 18;
 use Imager;
 
 Imager::init_log("testout/t1000files.log", 1);
@@ -65,3 +65,23 @@ ok(Imager->set_file_limits(reset=>1),
    "just reset");
 is_deeply([ Imager->get_file_limits() ], [ 0, 0, 0 ],
          "check all are reset");
+
+# check file type probe
+probe_ok("49492A41", undef, "not quite tiff");
+probe_ok("4D4D0041", undef, "not quite tiff");
+probe_ok("49492A00", "tiff", "tiff intel");
+probe_ok("4D4D002A", "tiff", "tiff motorola");
+probe_ok("474946383961", "gif", "gif 89");
+probe_ok("474946383761", "gif", "gif 87");
+
+sub probe_ok {
+  my ($packed, $exp_type, $name) = @_;
+
+  my $builder = Test::Builder->new;
+  my $data = pack("H*", $packed);
+
+  my $io = Imager::io_new_buffer($data);
+  my $result = Imager::i_test_format_probe($io, -1);
+
+  return $builder->is_eq($result, $exp_type, $name)
+}