From 8956ba536bfd3f7581038491d64ec8c86e0dd0dd Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 1 May 2013 18:46:15 +0100 Subject: [PATCH] fix some cppcheck warnings --- README.md | 4 + libvips/colour/rad2float.c | 5 +- libvips/deprecated/format.c | 3 +- libvips/deprecated/package.c | 13 +- libvips/deprecated/wrapvips7.c | 30 +- libvips/foreign/csv.c | 2 +- libvips/foreign/dzsave.c | 3 +- libvips/foreign/foreign.c | 3 +- libvips/foreign/openslide2vips.c | 7 +- libvips/foreign/ppm.c | 4 +- libvips/histograms_lut/im_buildlut.c | 5 +- libvips/histograms_lut/im_histplot.c | 12 +- libvips/iofuncs/base64.c | 570 +++++++++++++-------------- libvips/iofuncs/generate.c | 3 +- libvips/iofuncs/image.c | 3 +- libvips/iofuncs/init.c | 2 +- libvips/iofuncs/memory.c | 5 +- libvips/iofuncs/sinkscreen.c | 3 +- libvips/iofuncs/util.c | 10 +- libvips/morphology/im_profile.c | 4 +- libvips/morphology/im_rank_image.c | 5 +- libvips/mosaicing/global_balance.c | 3 +- 22 files changed, 360 insertions(+), 339 deletions(-) diff --git a/README.md b/README.md index 6bd0cf19..fe6af94d 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,10 @@ Then for a debug build: $ make $ make install +Static analysis with: + + $ cppcheck --force --enable=style . &> cppcheck.log + # Dependencies libvips has to have gettext, glib-2.x and libxml-2.0. The build system needs diff --git a/libvips/colour/rad2float.c b/libvips/colour/rad2float.c index 5e60ef7d..f4d74f1b 100644 --- a/libvips/colour/rad2float.c +++ b/libvips/colour/rad2float.c @@ -141,12 +141,11 @@ colr_color(col, clr) /* convert short to float color */ register COLOR col; register COLR clr; { - double f; - if (clr[EXP] == 0) col[RED] = col[GRN] = col[BLU] = 0.0; else { - f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); + double f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); + col[RED] = (clr[RED] + 0.5)*f; col[GRN] = (clr[GRN] + 0.5)*f; col[BLU] = (clr[BLU] + 0.5)*f; diff --git a/libvips/deprecated/format.c b/libvips/deprecated/format.c index 73319c26..be3849ec 100644 --- a/libvips/deprecated/format.c +++ b/libvips/deprecated/format.c @@ -284,13 +284,14 @@ static void vips_format_summary_class( VipsObjectClass *object_class, VipsBuf *buf ) { VipsFormatClass *class = VIPS_FORMAT_CLASS( object_class ); - const char **p; VIPS_OBJECT_CLASS( vips_format_parent_class )-> summary_class( object_class, buf ); vips_buf_appends( buf, ", " ); if( class->suffs ) { + const char **p; + vips_buf_appends( buf, "(" ); for( p = class->suffs; *p; p++ ) { vips_buf_appendf( buf, "%s", *p ); diff --git a/libvips/deprecated/package.c b/libvips/deprecated/package.c index 3fcbeb20..e00a38a5 100644 --- a/libvips/deprecated/package.c +++ b/libvips/deprecated/package.c @@ -774,14 +774,16 @@ im_map_packages( VSListMap2Fn fn, void *a ) { void *r = im_slist_map2( plugin_list, (VSListMap2Fn) apply_plugin, (void *) fn, a ); - int i; /* If not there, try main VIPS package list. */ - if( !r ) + if( !r ) { + int i; + for( i = 0; i < VIPS_NUMBER( built_in ); i++ ) if( (r = fn( built_in[i], a, NULL )) ) return( r ); + } return( r ); } @@ -1089,8 +1091,6 @@ add_hist( im_function *fn, im_object *vargv, int argc, char **argv ) static int dispatch_function( im_function *fn, im_object *vargv, int argc, char **argv ) { - int i; - /* Init memory from command line arguments. */ if( build_args( fn, vargv, argc, argv ) ) @@ -1107,7 +1107,9 @@ dispatch_function( im_function *fn, im_object *vargv, int argc, char **argv ) * - create a region on the input, closed by a * close callback on the output image */ - if( fn->flags & IM_FN_PIO ) + if( fn->flags & IM_FN_PIO ) { + int i; + for( i = 0; i < fn->argc; i++ ) { im_type_desc *type = fn->argv[i].desc; @@ -1116,6 +1118,7 @@ dispatch_function( im_function *fn, im_object *vargv, int argc, char **argv ) if( note_dependencies( fn, vargv, i ) ) return( -1 ); } + } /* Call function. */ diff --git a/libvips/deprecated/wrapvips7.c b/libvips/deprecated/wrapvips7.c index 445e9cf0..4b0165f6 100644 --- a/libvips/deprecated/wrapvips7.c +++ b/libvips/deprecated/wrapvips7.c @@ -317,13 +317,13 @@ vips_wrap7_object_set_property( GObject *gobject, vips__argument_table_lookup( oclass->argument_table, pspec ); VipsArgumentInstance *argument_instance = vips__argument_get_instance( argument_class, object ); - VipsWrap7 *wrap7 = VIPS_WRAP7( gobject ); VipsWrap7Class *class = VIPS_WRAP7_GET_CLASS( wrap7 ); - int i = argument_class->offset; - im_arg_desc *arg = &class->fn->argv[i]; - im_type_desc *type = arg->desc; - im_arg_type vt = type->type; + + int i; + im_arg_desc *arg; + im_type_desc *type; + im_arg_type vt; g_assert( argument_instance ); @@ -333,6 +333,11 @@ vips_wrap7_object_set_property( GObject *gobject, return; } + i = argument_class->offset; + arg = &class->fn->argv[i]; + type = arg->desc; + vt = type->type; + #ifdef DEBUG { char *str_value; @@ -418,13 +423,13 @@ vips_wrap7_object_get_property( GObject *gobject, vips__argument_table_lookup( class->argument_table, pspec ); VipsArgumentInstance *argument_instance = vips__argument_get_instance( argument_class, object ); - VipsWrap7 *wrap7 = VIPS_WRAP7( gobject ); VipsWrap7Class *wclass = VIPS_WRAP7_GET_CLASS( wrap7 ); - int i = argument_class->offset; - im_arg_desc *arg = &wclass->fn->argv[i]; - im_type_desc *type = arg->desc; - im_arg_type vt = type->type; + + int i; + im_arg_desc *arg; + im_type_desc *type; + im_arg_type vt; if( !argument_class ) { G_OBJECT_WARN_INVALID_PROPERTY_ID( gobject, @@ -432,6 +437,11 @@ vips_wrap7_object_get_property( GObject *gobject, return; } + i = argument_class->offset; + arg = &wclass->fn->argv[i]; + type = arg->desc; + vt = type->type; + g_assert( ((VipsArgument *) argument_class)->pspec == pspec ); if( !argument_instance->assigned ) { diff --git a/libvips/foreign/csv.c b/libvips/foreign/csv.c index a3a4a686..e8fe7a5b 100644 --- a/libvips/foreign/csv.c +++ b/libvips/foreign/csv.c @@ -172,7 +172,7 @@ read_double( FILE *fp, const char whitemap[256], const char sepmap[256], if( ch == '"' ) { (void) fgetc( fp ); - ch = skip_to_quote( fp ); + (void) skip_to_quote( fp ); ch = fgetc( fp ); } else if( !sepmap[ch] && diff --git a/libvips/foreign/dzsave.c b/libvips/foreign/dzsave.c index 78e2e6fc..709b4199 100644 --- a/libvips/foreign/dzsave.c +++ b/libvips/foreign/dzsave.c @@ -1161,8 +1161,7 @@ vips_foreign_save_dz_build( VipsObject *object ) vips_area_unref( background ); } - if( dz->overlap >= dz->tile_size || - dz->overlap >= dz->tile_size ) { + if( dz->overlap >= dz->tile_size ) { vips_error( "dzsave", "%s", _( "overlap must be less than tile " "width and height" ) ) ; diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 8acbd7ff..9b60bbb4 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -355,13 +355,14 @@ static void vips_foreign_summary_class( VipsObjectClass *object_class, VipsBuf *buf ) { VipsForeignClass *class = VIPS_FOREIGN_CLASS( object_class ); - const char **p; VIPS_OBJECT_CLASS( vips_foreign_parent_class )-> summary_class( object_class, buf ); vips_buf_appends( buf, " " ); if( class->suffs ) { + const char **p; + vips_buf_appends( buf, "(" ); for( p = class->suffs; *p; p++ ) { vips_buf_appendf( buf, "%s", *p ); diff --git a/libvips/foreign/openslide2vips.c b/libvips/foreign/openslide2vips.c index ec0cbaca..97afbb52 100644 --- a/libvips/foreign/openslide2vips.c +++ b/libvips/foreign/openslide2vips.c @@ -104,11 +104,12 @@ int vips__openslide_isslide( const char *filename ) { openslide_t *osr; - const char *vendor; int ok; ok = 0; if( (osr = openslide_open( filename )) ) { + const char *vendor; + /* Generic tiled tiff images can be opened by openslide as * well. Only offer to load this file if it's not a generic * tiff since we want vips_tiffload() to handle these. @@ -278,9 +279,7 @@ int vips__openslide_read_header( const char *filename, VipsImage *out, int level, char *associated ) { - ReadSlide *rslide; - - if( !(rslide = readslide_new( filename, out, level, associated )) ) + if( !readslide_new( filename, out, level, associated ) ) return( -1 ); return( 0 ); diff --git a/libvips/foreign/ppm.c b/libvips/foreign/ppm.c index 3c7c8e7c..8f49168a 100644 --- a/libvips/foreign/ppm.c +++ b/libvips/foreign/ppm.c @@ -81,9 +81,7 @@ static void skip_line( FILE *fp ) { - int ch; - - while( (ch = fgetc( fp )) != '\n' ) + while( fgetc( fp ) != '\n' ) ; } diff --git a/libvips/histograms_lut/im_buildlut.c b/libvips/histograms_lut/im_buildlut.c index b658bade..8d8e895b 100644 --- a/libvips/histograms_lut/im_buildlut.c +++ b/libvips/histograms_lut/im_buildlut.c @@ -89,11 +89,12 @@ compare( const void *a, const void *b ) static void free_state( State *state ) { - int i; + if( state->data ) { + int i; - if( state->data ) for( i = 0; i < state->input->ysize; i++ ) IM_FREE( state->data[i] ); + } IM_FREE( state->data ); IM_FREE( state->buf ); diff --git a/libvips/histograms_lut/im_histplot.c b/libvips/histograms_lut/im_histplot.c index 95043bda..e996746f 100644 --- a/libvips/histograms_lut/im_histplot.c +++ b/libvips/histograms_lut/im_histplot.c @@ -80,21 +80,18 @@ normalise( IMAGE *in, IMAGE *out ) return( -1 ); } else if( vips_bandfmt_isint( in->BandFmt ) ) { - IMAGE *t1; double min; /* Move min up to 0. */ - if( !(t1 = im_open_local( out, "im_histplot", "p" )) || - im_min( in, &min ) || - im_lintra( 1.0, in, -min, t1 ) ) + if( im_min( in, &min ) || + im_lintra( 1.0, in, -min, out ) ) return( -1 ); } else { /* Float image: scale min--max to 0--any. Output square * graph. */ - IMAGE *t1; DOUBLEMASK *stats; double min, max; int any; @@ -110,9 +107,8 @@ normalise( IMAGE *in, IMAGE *out ) max = VIPS_MASK( stats, 1, 0 ); im_free_dmask( stats ); - if( !(t1 = im_open_local( out, "im_histplot", "p" )) || - im_lintra( any / (max - min), in, - -min * any / (max - min), out ) ) + if( im_lintra( any / (max - min), in, + -min * any / (max - min), out ) ) return( -1 ); } diff --git a/libvips/iofuncs/base64.c b/libvips/iofuncs/base64.c index 6c2e1c48..d192bc5a 100644 --- a/libvips/iofuncs/base64.c +++ b/libvips/iofuncs/base64.c @@ -1,286 +1,286 @@ -/* base64.c -- Encode/decode integers in base64 format - * Created: Mon Sep 23 16:55:12 1996 by faith@dict.org - * Revised: Sat Mar 30 12:02:36 2002 by faith@dict.org - * Copyright 1996, 2002 Rickard E. Faith (faith@dict.org) - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Library General Public License as published - * by the Free Software Foundation; either version 2 of the License, or (at - * your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +/* base64.c -- Encode/decode integers in base64 format + * Created: Mon Sep 23 16:55:12 1996 by faith@dict.org + * Revised: Sat Mar 30 12:02:36 2002 by faith@dict.org + * Copyright 1996, 2002 Rickard E. Faith (faith@dict.org) + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Library General Public License as published + * by the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA - * - * \section{Base-64 Routines} - * - * \intro These routines use the 64-character subset of International - * Alphabet IA5 discussed in RFC 1421 (printeable encoding) and RFC 1522 - * (base64 MIME). - * - - Value Encoding Value Encoding Value Encoding Value Encoding - 0 A 17 R 34 i 51 z - 1 B 18 S 35 j 52 0 - 2 C 19 T 36 k 53 1 - 3 D 20 U 37 l 54 2 - 4 E 21 V 38 m 55 3 - 5 F 22 W 39 n 56 4 - 6 G 23 X 40 o 57 5 - 7 H 24 Y 41 p 58 6 - 8 I 25 Z 42 q 59 7 - 9 J 26 a 43 r 60 8 - 10 K 27 b 44 s 61 9 - 11 L 28 c 45 t 62 + - 12 M 29 d 46 u 63 / - 13 N 30 e 47 v - 14 O 31 f 48 w (pad) = - 15 P 32 g 49 x - 16 Q 33 h 50 y - * - */ - -/* - - Hacked for VIPS ... does any length object (not just ints), formats - base64 into 70 character lines, output to a malloc'd buffer. - - VIPS uses this to write BLOBs (like ICC profiles, for example) to the - XML that follows an image. - -Modified on: -23/7/07 JC - - oop, needed a slightly larger worst-case buffer in im__b64_encode() - -12/5/09 - - fix signed/unsigned warning - -25/3/11 - - move to vips_ namespace - - */ - -/* -#define DEBUG - */ - -#ifdef HAVE_CONFIG_H -#include -#endif /*HAVE_CONFIG_H*/ -#include - -#include -#include -#include -#include - -#include - -#include "base64.h" - -static unsigned char b64_list[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -#define XX 100 - -static unsigned char b64_index[256] = { - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63, - 52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX, - XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, - 15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX, - XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - 41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, -}; - -/* Read (up to) 3 bytes from in. Be careful about byte ordering :-/ we need to - * end up with in[2] in the bottom few bits. - */ -static int -read24( const unsigned char *in, size_t remaining ) -{ - int bits; - int i; - - bits = 0; - for( i = 0; i < 3; i++ ) { - bits <<= 8; - if( remaining > 0 ) { - bits |= in[i]; - remaining -= 1; - } - } - - return( bits ); -} - -/* Output (up to) 24 bits as four base64 chars. Pad with '=' characters. - */ -static void -encode24( char *p, int bits, size_t remaining ) -{ - int i; - - for( i = 0; i < 4; i++ ) { - if( remaining <= 0 ) - p[i] = '='; - else { - /* Take the top 6 bits of 24. - */ - p[i] = b64_list[(bits >> 18) & 63]; - bits <<= 6; - remaining -= 6; - } - } -} - -/* Output to a malloc'd buffer, NULL on error. Try to be simple and reliable, - * rather than quick. - */ -char * -vips__b64_encode( const unsigned char *data, size_t data_length ) -{ - /* Worst case: 1.333 chars per byte, plus 10% for extra carriage - * returns and stuff. And the \n\0 at the end. - */ - const size_t output_data_length = data_length * 44 / 30 + 2; - - char *buffer; - char *p; - size_t i; - int cursor; - - if( data_length <= 0 ) { - vips_error( "vips__b64_encode", "%s", _( "too little data" ) ); - return( NULL ); - } - if( output_data_length > 1024 * 1024 ) { - /* We shouldn't really be used for large amounts of data. - */ - vips_error( "vips__b64_encode", "%s", _( "too much data" ) ); - return( NULL ); - } - if( !(buffer = vips_malloc( NULL, output_data_length )) ) - return( NULL ); - - p = buffer; - *p++ = '\n'; - cursor = 0; - - for( i = 0; i < data_length; i += 3 ) { - size_t remaining = data_length - i; - int bits; - - bits = read24( data + i, remaining ); - encode24( p, bits, remaining * 8 ); - p += 4; - cursor += 4; - - if( cursor >= 76 ) { - *p++ = '\n'; - cursor = 0; - } - } - if( cursor > 0 ) - *p++ = '\n'; - *p++ = '\0'; - -#ifdef DEBUG -{ - unsigned int total; - - /* Calculate a very simple checksum for debugging. - */ - for( total = 0, i = 0; i < data_length; i++ ) - total += data[i]; - - printf( "vips__b64_encode: length = %d, checksum 0x%x\n", - data_length, total & 0xffff ); -} -#endif /*DEBUG*/ - - return( buffer ); -} - -/* Decode base64 back to binary in a malloc'd buffer. NULL on error. - */ -unsigned char * -vips__b64_decode( const char *buffer, size_t *data_length ) -{ - const size_t buffer_length = strlen( buffer ); - - /* Worst case. - */ - const size_t output_data_length = buffer_length * 3 / 4; - - unsigned char *data; - unsigned char *p; - unsigned int bits; - int nbits; - size_t i; - - if( output_data_length > 1024 * 1024 ) { - /* We shouldn't really be used for large amounts of data. - */ - vips_error( "vips__b64_decode", "%s", _( "too much data" ) ); - return( NULL ); - } - - if( !(data = vips_malloc( NULL, output_data_length )) ) - return( NULL ); - - p = data; - bits = 0; - nbits = 0; - - for( i = 0; i < buffer_length; i++ ) { - unsigned int val; - - if( (val = b64_index[(int) buffer[i]]) != XX ) { - bits <<= 6; - bits |= val; - nbits += 6; - - if( nbits >= 8 ) { - *p++ = (bits >> (nbits - 8)) & 0xff; - nbits -= 8; - } - } - } - - g_assert( (size_t) (p - data) < output_data_length ); - - if( data_length ) - *data_length = p - data; - -#ifdef DEBUG -{ - unsigned int total; - - /* Calculate a very simple checksum for debugging. - */ - for( total = 0, i = 0; i < p - data; i++ ) - total += data[i]; - - printf( "vips__b64_decode: length = %d, checksum 0x%x\n", - p - data, total & 0xffff ); -} -#endif /*DEBUG*/ - - return( data ); -} + * + * \section{Base-64 Routines} + * + * \intro These routines use the 64-character subset of International + * Alphabet IA5 discussed in RFC 1421 (printeable encoding) and RFC 1522 + * (base64 MIME). + * + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + * + */ + +/* + + Hacked for VIPS ... does any length object (not just ints), formats + base64 into 70 character lines, output to a malloc'd buffer. + + VIPS uses this to write BLOBs (like ICC profiles, for example) to the + XML that follows an image. + +Modified on: +23/7/07 JC + - oop, needed a slightly larger worst-case buffer in im__b64_encode() + +12/5/09 + - fix signed/unsigned warning + +25/3/11 + - move to vips_ namespace + + */ + +/* +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include +#include +#include + +#include + +#include "base64.h" + +static unsigned char b64_list[] = +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +#define XX 100 + +static unsigned char b64_index[256] = { + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63, + 52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX, + XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, + 15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX, + XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, + 41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, +}; + +/* Read (up to) 3 bytes from in. Be careful about byte ordering :-/ we need to + * end up with in[2] in the bottom few bits. + */ +static int +read24( const unsigned char *in, size_t remaining ) +{ + int bits; + int i; + + bits = 0; + for( i = 0; i < 3; i++ ) { + bits <<= 8; + if( remaining > 0 ) { + bits |= in[i]; + remaining -= 1; + } + } + + return( bits ); +} + +/* Output (up to) 24 bits as four base64 chars. Pad with '=' characters. + */ +static void +encode24( char *p, int bits, size_t remaining ) +{ + int i; + + for( i = 0; i < 4; i++ ) { + if( remaining == 0 ) + p[i] = '='; + else { + /* Take the top 6 bits of 24. + */ + p[i] = b64_list[(bits >> 18) & 63]; + bits <<= 6; + remaining -= 6; + } + } +} + +/* Output to a malloc'd buffer, NULL on error. Try to be simple and reliable, + * rather than quick. + */ +char * +vips__b64_encode( const unsigned char *data, size_t data_length ) +{ + /* Worst case: 1.333 chars per byte, plus 10% for extra carriage + * returns and stuff. And the \n\0 at the end. + */ + const size_t output_data_length = data_length * 44 / 30 + 2; + + char *buffer; + char *p; + size_t i; + int cursor; + + if( data_length == 0 ) { + vips_error( "vips__b64_encode", "%s", _( "too little data" ) ); + return( NULL ); + } + if( output_data_length > 1024 * 1024 ) { + /* We shouldn't really be used for large amounts of data. + */ + vips_error( "vips__b64_encode", "%s", _( "too much data" ) ); + return( NULL ); + } + if( !(buffer = vips_malloc( NULL, output_data_length )) ) + return( NULL ); + + p = buffer; + *p++ = '\n'; + cursor = 0; + + for( i = 0; i < data_length; i += 3 ) { + size_t remaining = data_length - i; + int bits; + + bits = read24( data + i, remaining ); + encode24( p, bits, remaining * 8 ); + p += 4; + cursor += 4; + + if( cursor >= 76 ) { + *p++ = '\n'; + cursor = 0; + } + } + if( cursor > 0 ) + *p++ = '\n'; + *p++ = '\0'; + +#ifdef DEBUG +{ + unsigned int total; + + /* Calculate a very simple checksum for debugging. + */ + for( total = 0, i = 0; i < data_length; i++ ) + total += data[i]; + + printf( "vips__b64_encode: length = %u, checksum 0x%x\n", + data_length, total & 0xffff ); +} +#endif /*DEBUG*/ + + return( buffer ); +} + +/* Decode base64 back to binary in a malloc'd buffer. NULL on error. + */ +unsigned char * +vips__b64_decode( const char *buffer, size_t *data_length ) +{ + const size_t buffer_length = strlen( buffer ); + + /* Worst case. + */ + const size_t output_data_length = buffer_length * 3 / 4; + + unsigned char *data; + unsigned char *p; + unsigned int bits; + int nbits; + size_t i; + + if( output_data_length > 1024 * 1024 ) { + /* We shouldn't really be used for large amounts of data. + */ + vips_error( "vips__b64_decode", "%s", _( "too much data" ) ); + return( NULL ); + } + + if( !(data = vips_malloc( NULL, output_data_length )) ) + return( NULL ); + + p = data; + bits = 0; + nbits = 0; + + for( i = 0; i < buffer_length; i++ ) { + unsigned int val; + + if( (val = b64_index[(int) buffer[i]]) != XX ) { + bits <<= 6; + bits |= val; + nbits += 6; + + if( nbits >= 8 ) { + *p++ = (bits >> (nbits - 8)) & 0xff; + nbits -= 8; + } + } + } + + g_assert( (size_t) (p - data) < output_data_length ); + + if( data_length ) + *data_length = p - data; + +#ifdef DEBUG +{ + unsigned int total; + + /* Calculate a very simple checksum for debugging. + */ + for( total = 0, i = 0; i < p - data; i++ ) + total += data[i]; + + printf( "vips__b64_decode: length = %d, checksum 0x%x\n", + p - data, total & 0xffff ); +} +#endif /*DEBUG*/ + + return( data ); +} diff --git a/libvips/iofuncs/generate.c b/libvips/iofuncs/generate.c index 05e28bd3..174671fe 100644 --- a/libvips/iofuncs/generate.c +++ b/libvips/iofuncs/generate.c @@ -509,13 +509,12 @@ vips_allocate_input_array( VipsImage *image, ... ) { va_list ap; VipsImage **ar; - VipsImage *im; int i, n; /* Count input images. */ va_start( ap, image ); - for( n = 0; (im = va_arg( ap, VipsImage * )); n++ ) + for( n = 0; va_arg( ap, VipsImage * ); n++ ) ; va_end( ap ); diff --git a/libvips/iofuncs/image.c b/libvips/iofuncs/image.c index 031fc653..f0b9d8ac 100644 --- a/libvips/iofuncs/image.c +++ b/libvips/iofuncs/image.c @@ -468,7 +468,8 @@ vips_image_sanity( VipsObject *object, VipsBuf *buf ) image->dtype > VIPS_IMAGE_PARTIAL || image->dhint > VIPS_DEMAND_STYLE_ANY ) vips_buf_appends( buf, "bad enum\n" ); - if( image->Xres < 0 || image->Xres < 0 ) + if( image->Xres < 0 || + image->Yres < 0 ) vips_buf_appends( buf, "bad resolution\n" ); } diff --git a/libvips/iofuncs/init.c b/libvips/iofuncs/init.c index a8d0fba7..4346c2ee 100644 --- a/libvips/iofuncs/init.c +++ b/libvips/iofuncs/init.c @@ -636,7 +636,7 @@ find_file( const char *name ) static const char * guess_prefix( const char *argv0, const char *name ) { - char *prefix; + char *prefix; /* Try to guess from argv0. */ diff --git a/libvips/iofuncs/memory.c b/libvips/iofuncs/memory.c index 6c850ffb..8f423be1 100644 --- a/libvips/iofuncs/memory.c +++ b/libvips/iofuncs/memory.c @@ -230,10 +230,11 @@ vips_tracked_free( void *s ) if( vips_tracked_allocs <= 0 ) vips_warn( "vips_tracked", "%s", _( "vips_free: too many frees" ) ); - vips_tracked_mem -= size; - if( vips_tracked_mem < 0 ) + if( vips_tracked_mem < size ) vips_warn( "vips_tracked", "%s", _( "vips_free: too much free" ) ); + + vips_tracked_mem -= size; vips_tracked_allocs -= 1; g_mutex_unlock( vips_tracked_mutex ); diff --git a/libvips/iofuncs/sinkscreen.c b/libvips/iofuncs/sinkscreen.c index ea387858..6ae08172 100644 --- a/libvips/iofuncs/sinkscreen.c +++ b/libvips/iofuncs/sinkscreen.c @@ -848,7 +848,6 @@ static void tile_copy( Tile *tile, VipsRegion *to ) { VipsRect ovlap; - int y; /* Find common pixels. */ @@ -861,6 +860,8 @@ tile_copy( Tile *tile, VipsRegion *to ) if( tile->painted && !tile->region->invalid ) { int len = VIPS_IMAGE_SIZEOF_PEL( to->im ) * ovlap.width; + int y; + VIPS_DEBUG_MSG( "tile_copy: " "copying calculated pixels for %p %dx%d\n", tile, tile->area.left, tile->area.top ); diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index d38b62c9..5b82145b 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -791,12 +791,16 @@ vips__file_read( FILE *fp, const char *filename, unsigned int *length_out ) len = 0; size = 0; do { + char *str2; + size += 1024; - if( !(str = realloc( str, size )) ) { + if( !(str2 = realloc( str, size )) ) { + free( str ); vips_error( "vips__file_read", "%s", _( "out of memory" ) ); return( NULL ); } + str = str2; /* -1 to allow space for an extra NULL we add later. */ @@ -1601,7 +1605,7 @@ vips__parse_size( const char *size_string ) guint64 size; int n; - int i, j; + int i; char *unit; /* An easy way to alloc a buffer large enough. @@ -1610,6 +1614,8 @@ vips__parse_size( const char *size_string ) n = sscanf( size_string, "%d %s", &i, unit ); size = i; if( n > 1 ) { + int j; + for( j = 0; j < VIPS_NUMBER( units ); j++ ) if( tolower( unit[0] ) == units[j].unit ) { size *= units[j].multiplier; diff --git a/libvips/morphology/im_profile.c b/libvips/morphology/im_profile.c index 9b3386ef..3f5913b4 100644 --- a/libvips/morphology/im_profile.c +++ b/libvips/morphology/im_profile.c @@ -80,7 +80,7 @@ im_profile( IMAGE *in, IMAGE *out, int dir ) { int sz; unsigned short *buf; - int x, y, b; + int x, y; /* If in is not uchar, do (!=0) to make a uchar image. */ @@ -149,6 +149,8 @@ im_profile( IMAGE *in, IMAGE *out, int dir ) for( y = 0; y < in->Ysize; y++ ) { VipsPel *p = IM_IMAGE_ADDR( in, 0, y ); + int b; + for( b = 0; b < in->Bands; b++ ) { VipsPel *p1; diff --git a/libvips/morphology/im_rank_image.c b/libvips/morphology/im_rank_image.c index c61bb6a5..47f7b412 100644 --- a/libvips/morphology/im_rank_image.c +++ b/libvips/morphology/im_rank_image.c @@ -104,11 +104,12 @@ rank_stop( void *vseq, void *a, void *b ) RankSequence *seq = (RankSequence *) vseq; Rank *rank = (Rank *) b; - int i; + if( seq->ir ) { + int i; - if( seq->ir ) for( i = 0; i < rank->n; i++ ) IM_FREEF( im_region_free, seq->ir[i] ); + } return( 0 ); } diff --git a/libvips/mosaicing/global_balance.c b/libvips/mosaicing/global_balance.c index 363d6a14..16adff04 100644 --- a/libvips/mosaicing/global_balance.c +++ b/libvips/mosaicing/global_balance.c @@ -712,7 +712,6 @@ static JoinNode * find_root( SymbolTable *st ) { JoinNode *root; - JoinNode *notroot; /* Clean the table, then scan it, setting all pointed-to nodes dirty. */ @@ -736,7 +735,7 @@ find_root( SymbolTable *st ) * more than one root. */ root->dirty = 1; - if( (notroot = im__map_table( st, is_root, NULL, NULL )) ) { + if( im__map_table( st, is_root, NULL, NULL ) ) { im_error( "im_global_balance", "%s", _( "more than one root" ) ); return( NULL );