diff --git a/libvips/deprecated/format.c b/libvips/deprecated/format.c index 03ea8fd1..fd94a2f0 100644 --- a/libvips/deprecated/format.c +++ b/libvips/deprecated/format.c @@ -176,7 +176,7 @@ im_isvips( const char *filename ) { unsigned char buf[4]; - if( im__get_bytes( filename, buf, 4 ) ) { + if( im__get_bytes( filename, buf, 4 ) == 4 ) { if( buf[0] == 0x08 && buf[1] == 0xf2 && buf[2] == 0xa6 && buf[3] == 0xb6 ) /* SPARC-order VIPS image. @@ -212,7 +212,7 @@ vips_flags( const char *filename ) flags = VIPS_FORMAT_PARTIAL; - if( im__get_bytes( filename, buf, 4 ) && + if( im__get_bytes( filename, buf, 4 ) == 4 && buf[0] == 0x08 && buf[1] == 0xf2 && buf[2] == 0xa6 && diff --git a/libvips/foreign/magick7load.c b/libvips/foreign/magick7load.c index d729db9b..a81a56af 100644 --- a/libvips/foreign/magick7load.c +++ b/libvips/foreign/magick7load.c @@ -751,15 +751,13 @@ G_DEFINE_TYPE( VipsForeignLoadMagick7File, vips_foreign_load_magick7_file, static gboolean ismagick7( const char *filename ) { - /* Fetch the first 100 bytes. Hopefully that'll be enough. + /* Fetch up to the first 100 bytes. Hopefully that'll be enough. */ unsigned char buf[100]; + int len; - /* Files shorter than 100 bytes will leave nonsense at the end of buf, - * but it shouldn't matter. - */ - return( vips__get_bytes( filename, buf, 100 ) && - magick_ismagick( buf, 100 ) ); + return( (len = vips__get_bytes( filename, buf, 100 )) > 10 && + magick_ismagick( buf, len ) ); } static int diff --git a/libvips/foreign/magickload.c b/libvips/foreign/magickload.c index a1215a30..bfb26566 100644 --- a/libvips/foreign/magickload.c +++ b/libvips/foreign/magickload.c @@ -173,15 +173,13 @@ G_DEFINE_TYPE( VipsForeignLoadMagickFile, vips_foreign_load_magick_file, static gboolean ismagick( const char *filename ) { - /* Fetch the first 100 bytes. Hopefully that'll be enough. + /* Fetch up to the first 100 bytes. Hopefully that'll be enough. */ unsigned char buf[100]; + int len; - /* Files shorter than 100 bytes will leave nonsense at the end of buf, - * but it shouldn't matter. - */ - return( vips__get_bytes( filename, buf, 100 ) && - magick_ismagick( buf, 100 ) ); + return( (len = vips__get_bytes( filename, buf, 100 )) > 10 && + magick_ismagick( buf, len ) ); } /* Unfortunately, libMagick does not support header-only reads very well. See diff --git a/libvips/include/vips/util.h b/libvips/include/vips/util.h index fe0d4b7e..d9cdf93e 100644 --- a/libvips/include/vips/util.h +++ b/libvips/include/vips/util.h @@ -266,8 +266,8 @@ char *vips__file_read( FILE *fp, const char *name, size_t *length_out ); char *vips__file_read_name( const char *name, const char *fallback_dir, size_t *length_out ); int vips__file_write( void *data, size_t size, size_t nmemb, FILE *stream ); -guint64 vips__get_bytes( const char *filename, - unsigned char buf[], guint64 len ); +gint64 vips__get_bytes( const char *filename, + unsigned char buf[], gint64 len ); int vips__fgetc( FILE *fp ); GValue *vips__gvalue_ref_string_new( const char *text ); diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index 2399485d..99c67641 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -874,13 +874,13 @@ vips__file_write( void *data, size_t size, size_t nmemb, FILE *stream ) * types, so we must read binary. * * Return the number of bytes actually read (the file might be shorter than - * len), or 0 for error. + * len), or -1 for error. */ -guint64 -vips__get_bytes( const char *filename, unsigned char buf[], guint64 len ) +gint64 +vips__get_bytes( const char *filename, unsigned char buf[], gint64 len ) { int fd; - guint64 bytes_read; + gint64 bytes_read; /* File may not even exist (for tmp images for example!) * so no hasty messages. And the file might be truncated, so no error