From: Tony Cook <tony@develop=help.com> Date: Fri, 17 Feb 2006 08:31:28 +0000 (+0000) Subject: - eliminate sign warning from image.c X-Git-Tag: Imager-0.48^2~12 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/db7a875466c4129ef265a4fa597efd34decdcb3e - eliminate sign warning from image.c - make TIFF detection stricter --- diff --git a/Changes b/Changes index 1f156575..b0b7b3b7 100644 --- 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 097f8c56..2d1b2ae6 100644 --- 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; } diff --git a/t/t1000files.t b/t/t1000files.t index 0497fdfa..b306e2c0 100644 --- a/t/t1000files.t +++ b/t/t1000files.t @@ -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) +}