Use custom error handler instead of fprintf(stderr, ...)

master
Ondřej Surý 2013-04-16 11:31:22 +02:00
parent 21efcc3092
commit 32b1307c62
33 changed files with 294 additions and 238 deletions

View File

@ -4,6 +4,7 @@
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
@ -13,6 +14,7 @@
#include "gd.h"
#include "gdhelpers.h"
#include "gd_color.h"
#include "gd_errors.h"
/* 2.0.12: this now checks the clipping rectangle */
#define gdImageBoundsSafeMacro(im, x, y) (!((((y) < (im)->cy1) || ((y) > (im)->cy2)) || (((x) < (im)->cx1) || ((x) > (im)->cx2))))
@ -67,6 +69,65 @@ static const unsigned char gd_toascii[256] = {
extern const int gdCosT[];
extern const int gdSinT[];
void gd_stderr_error(int priority, const char *format, ...)
{
va_list args;
va_start(args, format);
switch (priority) {
case E_ERROR:
fputs("GD Error: ", stderr);
break;
case E_WARNING:
fputs("GD Warning: ", stderr);
break;
case E_NOTICE:
fputs("GD Notice: ", stderr);
break;
case E_INFO:
fputs("GD Info: ", stderr);
break;
case E_DEBUG:
fputs("GD Debug: ", stderr);
break;
}
va_start(args, format);
fprintf(stderr, format, args);
va_end(args);
fflush(stderr);
}
static gdErrorMethod gd_error_method = gd_stderr_error;
void gd_error(const char *format, ...)
{
va_list args;
va_start(args, format);
gd_error_ex(E_WARNING, format, args);
va_end(args);
}
void gd_error_ex(int priority, const char *format, ...)
{
va_list args;
va_start(args, format);
if (gd_error_method) {
gd_error_method(priority, format, args);
}
va_end(args);
}
BGD_DECLARE(void) gdSetErrorMethod(gdErrorMethod error_method)
{
gd_error_method = error_method;
}
BGD_DECLARE(void) gdClearErrorMethod(void)
{
gd_error_method = gd_stderr_error;
}
static void gdImageBrushApply (gdImagePtr im, int x, int y);
static void gdImageTileApply (gdImagePtr im, int x, int y);
BGD_DECLARE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y);
@ -2840,7 +2901,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm (FILE * fd)
}
}
/* Shouldn't happen */
fprintf (stderr, "Error: bug in gdImageCreateFromXbm!\n");
gd_error("Error: bug in gdImageCreateFromXbm!\n");
fail:
gdImageDestroy (im);
return 0;

View File

@ -335,6 +335,11 @@ gdFont;
/* Text functions take these. */
typedef gdFont *gdFontPtr;
typedef void(*gdErrorMethod)(int, const char *, ...);
BGD_DECLARE(void) gdSetErrorMethod(gdErrorMethod);
BGD_DECLARE(void) gdClearErrorMethod(void);
/* For backwards compatibility only. Use gdImageSetStyle()
for MUCH more flexible line drawing. Also see
gdImageSetBrush(). */

26
src/gd_errors.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef GD_ERRORS_H
#define GD_ERRORS_H
#include <syslog.h>
/*
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message
*/
#define E_ERROR LOG_ERR
#define E_WARNING LOG_WARNING
#define E_NOTICE LOG_NOTICE
#define E_INFO LOG_INFO
#define E_DEBUG LOG_DEBUG
void gd_error(const char *format, ...);
void gd_error_ex(int priority, const char *format, ...);
#endif

View File

@ -602,7 +602,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2PartCtx (gdIOCtx * in, int srcx, int
/* gd 2.0.11: gdSeek returns TRUE on success, not 0.
Longstanding bug. 01/16/03 */
if (!gdSeek (in, dpos)) {
fprintf (stderr, "Seek error\n");
gd_error("Seek error\n");
goto fail2;
};
GD2_DBG (printf
@ -875,7 +875,7 @@ _gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt)
chunkIdx[chunkNum - 1].offset));
if (gdPutBuf (compData, compLen, out) <= 0) {
fprintf(stderr, "gd write error\n");
gd_error("gd write error\n");
};
};
};
@ -938,13 +938,13 @@ BGD_DECLARE(void *) gdImageGd2Ptr (gdImagePtr im, int cs, int fmt, int *size)
#else /* no HAVE_LIBZ */
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2 (FILE * inFile)
{
fprintf (stderr, "GD2 support is not available - no libz\n");
gd_error("GD2 support is not available - no libz\n");
return NULL;
}
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
{
fprintf (stderr, "GD2 support is not available - no libz\n");
gd_error("GD2 support is not available - no libz\n");
return NULL;
}
#endif /* HAVE_LIBZ */

View File

@ -33,6 +33,7 @@
#include <string.h>
#include "gd.h"
#include "gd_errors.h"
/* TBB: move this up so include files are not brought in */
/* JCE: arrange HAVE_LIBJPEG so that it can be set in gd.h */
#ifdef HAVE_LIBJPEG
@ -64,7 +65,7 @@ static void fatal_jpeg_error(j_common_ptr cinfo)
{
jmpbuf_wrapper *jmpbufw;
fprintf(stderr, "gd-jpeg: JPEG library reports unrecoverable error: ");
gd_error("gd-jpeg: JPEG library reports unrecoverable error: ");
(*cinfo->err->output_message)(cinfo);
fflush(stderr);
@ -73,9 +74,9 @@ static void fatal_jpeg_error(j_common_ptr cinfo)
if(jmpbufw != 0) {
longjmp(jmpbufw->jmpbuf, 1);
fprintf(stderr, "gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating\n");
gd_error("gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating\n");
} else {
fprintf(stderr, "gd-jpeg: EXTREMELY fatal error: jmpbuf unrecoverable; terminating\n");
gd_error("gd-jpeg: EXTREMELY fatal error: jmpbuf unrecoverable; terminating\n");
}
fflush(stderr);
@ -181,7 +182,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components * sizeof(JSAMPLE));
if(row == 0) {
fprintf (stderr, "gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
jpeg_destroy_compress(&cinfo);
return;
}
@ -202,7 +203,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
if(im->trueColor) {
#if BITS_IN_JSAMPLE == 12
fprintf(stderr,
gd_error(
"gd-jpeg: error: jpeg library was compiled for 12-bit\n"
"precision. This is mostly useless, because JPEGs on the web are\n"
"8-bit and such versions of the jpeg library won't read or write\n"
@ -223,7 +224,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
if(nlines != 1) {
fprintf (stderr, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
gd_error("gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
}
}
} else {
@ -251,7 +252,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
if(nlines != 1) {
fprintf (stderr, "gd_jpeg: warning: jpeg_write_scanlines"
gd_error("gd_jpeg: warning: jpeg_write_scanlines"
" returns %u -- expected 1\n", nlines);
}
}
@ -343,25 +344,25 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
retval = jpeg_read_header(&cinfo, TRUE);
if(retval != JPEG_HEADER_OK) {
fprintf (stderr, "gd-jpeg: warning: jpeg_read_header returns"
gd_error("gd-jpeg: warning: jpeg_read_header returns"
" %d, expected %d\n", retval, JPEG_HEADER_OK);
}
if(cinfo.image_height > INT_MAX) {
fprintf (stderr, "gd-jpeg: warning: JPEG image height (%u) is"
gd_error("gd-jpeg: warning: JPEG image height (%u) is"
" greater than INT_MAX (%d) (and thus greater than"
" gd can handle)", cinfo.image_height, INT_MAX);
}
if(cinfo.image_width > INT_MAX) {
fprintf (stderr, "gd-jpeg: warning: JPEG image width (%u) is"
gd_error("gd-jpeg: warning: JPEG image width (%u) is"
" greater than INT_MAX (%d) (and thus greater than"
" gd can handle)\n", cinfo.image_width, INT_MAX);
}
im = gdImageCreateTrueColor((int)cinfo.image_width, (int)cinfo.image_height);
if(im == 0) {
fprintf (stderr, "gd-jpeg error: cannot allocate gdImage struct\n");
gd_error("gd-jpeg error: cannot allocate gdImage struct\n");
goto error;
}
@ -388,7 +389,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
}
if(jpeg_start_decompress(&cinfo) != TRUE) {
fprintf(stderr, "gd-jpeg: warning: jpeg_start_decompress"
gd_error("gd-jpeg: warning: jpeg_start_decompress"
" reports suspended data source\n");
}
@ -452,7 +453,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
#endif
if(cinfo.out_color_space == JCS_RGB) {
if(cinfo.output_components != 3) {
fprintf (stderr, "gd-jpeg: error: JPEG color quantization"
gd_error("gd-jpeg: error: JPEG color quantization"
" request resulted in output_components == %d"
" (expected 3 for RGB)\n", cinfo.output_components);
goto error;
@ -461,7 +462,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
} else if(cinfo.out_color_space == JCS_CMYK) {
jpeg_saved_marker_ptr marker;
if(cinfo.output_components != 4) {
fprintf (stderr, "gd-jpeg: error: JPEG color quantization"
gd_error("gd-jpeg: error: JPEG color quantization"
" request resulted in output_components == %d"
" (expected 4 for CMYK)\n", cinfo.output_components);
goto error;
@ -479,11 +480,12 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
marker = marker->next;
}
} else {
fprintf(stderr, "gd-jpeg: error: unexpected colorspace\n");
gd_error("gd-jpeg: error: unexpected colorspace\n");
goto error;
}
#if BITS_IN_JSAMPLE == 12
fprintf(stderr, "gd-jpeg: error: jpeg library was compiled for 12-bit\n"
gd_error_ex(E_ERROR,
"gd-jpeg: error: jpeg library was compiled for 12-bit\n"
"precision. This is mostly useless, because JPEGs on the web are\n"
"8-bit and such versions of the jpeg library won't read or write\n"
"them. GD doesn't support these unusual images. Edit your\n"
@ -494,7 +496,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
row = gdCalloc(cinfo.output_width *channels, sizeof(JSAMPLE));
if(row == 0) {
fprintf (stderr, "gd-jpeg: error: unable to allocate row for"
gd_error("gd-jpeg: error: unable to allocate row for"
" JPEG scanline: gdCalloc returns NULL\n");
goto error;
}
@ -505,7 +507,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
register int *tpix = im->tpixels[i];
nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
if(nrows != 1) {
fprintf (stderr, "gd-jpeg: error: jpeg_read_scanlines"
gd_error("gd-jpeg: error: jpeg_read_scanlines"
" returns %u, expected 1\n", nrows);
goto error;
}
@ -519,7 +521,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
register int *tpix = im->tpixels[i];
nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
if(nrows != 1) {
fprintf (stderr, "gd-jpeg: error: jpeg_read_scanlines"
gd_error("gd-jpeg: error: jpeg_read_scanlines"
" returns %u, expected 1\n", nrows);
goto error;
}
@ -530,7 +532,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
}
if(jpeg_finish_decompress (&cinfo) != TRUE) {
fprintf(stderr, "gd-jpeg: warning: jpeg_finish_decompress"
gd_error("gd-jpeg: warning: jpeg_finish_decompress"
" reports suspended data source\n");
}
/* TBB 2.0.29: we should do our best to read whatever we can read, and a

View File

@ -35,6 +35,7 @@
#include <string.h>
#include "gd.h"
#include "gdhelpers.h"
#include "gd_errors.h"
#include "gd_nnquant.h"
@ -466,7 +467,7 @@ int verbose;
for (i=0; i<rad; i++)
nnq->radpower[i] = alpha*(((rad*rad - i*i)*radbias)/(rad*rad));
if(verbose) fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad);
if (verbose) gd_error_ex(E_NOTICE, "beginning 1D learning: initial radius=%d\n", rad);
if ((nnq->lengthcount%prime1) != 0) step = 4*prime1;
else {
@ -501,7 +502,7 @@ int verbose;
nnq->radpower[j] = alpha*(((rad*rad - j*j)*radbias)/(rad*rad));
}
}
if(verbose) fprintf(stderr,"finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
if (verbose) gd_error_ex(E_NOTICE, "finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
}
BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor)
@ -578,10 +579,8 @@ BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int
}
}
if (bot_idx != top_idx + 1) {
fprintf(stderr,
" internal logic error: remapped bot_idx = %d, top_idx = %d\n",
gd_error(" internal logic error: remapped bot_idx = %d, top_idx = %d\n",
bot_idx, top_idx);
fflush(stderr);
goto done;
}

View File

@ -61,12 +61,12 @@ gdPngErrorHandler (png_structp png_ptr, png_const_charp msg)
* regardless of whether _BSD_SOURCE or anything else has (or has not)
* been defined. */
fprintf (stderr, "gd-png: fatal libpng error: %s\n", msg);
gd_error("gd-png: fatal libpng error: %s\n", msg);
fflush (stderr);
jmpbuf_ptr = png_get_error_ptr (png_ptr);
if (jmpbuf_ptr == NULL) { /* we are completely hosed now */
fprintf (stderr, "gd-png: EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
gd_error("gd-png: EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
fflush (stderr);
exit (99);
}
@ -163,13 +163,13 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
#endif
if (png_ptr == NULL) {
fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
gd_error("gd-png error: cannot allocate libpng main struct\n");
return NULL;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
gd_error("gd-png error: cannot allocate libpng info struct\n");
png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL;
@ -184,7 +184,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
* PNG-reading libpng function */
#ifdef PNG_SETJMP_SUPPORTED
if (setjmp(jbw.jmpbuf)) {
fprintf (stderr, "gd-png error: setjmp returns error condition 1\n");
gd_error("gd-png error: setjmp returns error condition 1\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
@ -204,7 +204,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
im = gdImageCreate ((int) width, (int) height);
}
if (im == NULL) {
fprintf (stderr, "gd-png error: cannot allocate gdImage struct\n");
gd_error("gd-png error: cannot allocate gdImage struct\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
@ -221,7 +221,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
*/
#ifdef PNG_SETJMP_SUPPORTED
if (setjmp(jbw.jmpbuf)) {
fprintf(stderr, "gd-png error: setjmp returns error condition 2\n");
gd_error("gd-png error: setjmp returns error condition 2\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
gdFree(image_data);
gdFree(row_pointers);
@ -250,7 +250,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
case PNG_COLOR_TYPE_PALETTE:
png_get_PLTE (png_ptr, info_ptr, &palette, &num_palette);
#ifdef DEBUG
fprintf (stderr, "gd-png color_type is palette, colors: %d\n", num_palette);
gd_error("gd-png color_type is palette, colors: %d\n", num_palette);
#endif /* DEBUG */
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
/* gd 2.0: we support this rather thoroughly now. Grab the
@ -275,7 +275,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
case PNG_COLOR_TYPE_GRAY:
/* create a fake palette and check for single-shade transparency */
if ((palette = (png_colorp) gdMalloc (256 * sizeof (png_color))) == NULL) {
fprintf (stderr, "gd-png error: cannot allocate gray palette\n");
gd_error("gd-png error: cannot allocate gray palette\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
@ -345,7 +345,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
}
image_data = (png_bytep) gdMalloc (rowbytes * height);
if (!image_data) {
fprintf (stderr, "gd-png error: cannot allocate image data\n");
gd_error("gd-png error: cannot allocate image data\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
if (im) {
gdImageDestroy(im);
@ -363,7 +363,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
row_pointers = (png_bytepp) gdMalloc (height * sizeof (png_bytep));
if (!row_pointers) {
fprintf (stderr, "gd-png error: cannot allocate row pointers\n");
gd_error("gd-png error: cannot allocate row pointers\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
if (im) {
gdImageDestroy(im);
@ -544,20 +544,20 @@ BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level)
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
#endif
if (png_ptr == NULL) {
fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
gd_error("gd-png error: cannot allocate libpng main struct\n");
return;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
gd_error("gd-png error: cannot allocate libpng info struct\n");
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
return;
}
#ifdef PNG_SETJMP_SUPPORTED
if (setjmp(jbw.jmpbuf)) {
fprintf (stderr, "gd-png error: setjmp returns error condition\n");
gd_error("gd-png error: setjmp returns error condition\n");
png_destroy_write_struct (&png_ptr, &info_ptr);
return;
}
@ -610,7 +610,7 @@ BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level)
}
}
if (colors == 0) {
fprintf(stderr, "gd-png error: no colors in palette\n");
gd_error("gd-png error: no colors in palette\n");
goto bail;
}
if (colors < im->colorsTotal) {
@ -746,14 +746,14 @@ BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level)
}
row_pointers = gdMalloc (sizeof (png_bytep) * height);
if (row_pointers == NULL) {
fprintf (stderr, "gd-png error: unable to allocate row_pointers\n");
gd_error("gd-png error: unable to allocate row_pointers\n");
goto bail;
}
prow_pointers = row_pointers;
for (j = 0; j < height; ++j) {
if (overflow2(width, channels) || ((*prow_pointers =
(png_bytep) gdMalloc (width * channels)) == NULL)) {
fprintf (stderr, "gd-png error: unable to allocate rows\n");
gd_error("gd-png error: unable to allocate rows\n");
for (i = 0; i < j; ++i)
gdFree (row_pointers[i]);
/* 2.0.29: memory leak TBB */
@ -795,12 +795,12 @@ BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level)
}
row_pointers = gdMalloc (sizeof (png_bytep) * height);
if (row_pointers == NULL) {
fprintf (stderr, "gd-png error: unable to allocate row_pointers\n");
gd_error("gd-png error: unable to allocate row_pointers\n");
goto bail;
}
for (j = 0; j < height; ++j) {
if ((row_pointers[j] = (png_bytep) gdMalloc (width)) == NULL) {
fprintf (stderr, "gd-png error: unable to allocate rows\n");
gd_error("gd-png error: unable to allocate rows\n");
for (i = 0; i < j; ++i)
gdFree (row_pointers[i]);
/* TBB: memory leak */

View File

@ -16,15 +16,16 @@
#include <stdlib.h>
#include <limits.h>
#include "gd.h"
#include "gd_errors.h"
int overflow2(int a, int b)
{
if(a <= 0 || b <= 0) {
fprintf(stderr, "gd warning: one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully\n");
gd_error(E_WARNING, "one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully\n");
return 1;
}
if(a > INT_MAX / b) {
fprintf(stderr, "gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
gd_error_ex(E_WARNING, "product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
return 1;
}
return 0;

View File

@ -41,13 +41,13 @@ BGD_DECLARE(void) gdImagePngToSink (gdImagePtr im, gdSinkPtr outSink)
{
(void)im;
(void)outSink;
fprintf (stderr, "PNG support is not available\n");
gd_error("PNG support is not available\n");
}
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngSource (gdSourcePtr inSource)
{
(void)inSource;
fprintf (stderr, "PNG support is not available\n");
gd_error("PNG support is not available\n");
return NULL;
}
#endif /* HAVE_LIBPNG */

View File

@ -132,7 +132,7 @@ int read_header_tga(gdIOCtx *ctx, oTga *tga)
unsigned char header[18];
if (gdGetBuf(header, sizeof(header), ctx) < 18) {
fprintf(stderr, "fail to read header");
gd_error("fail to read header");
return -1;
}
@ -165,7 +165,7 @@ int read_header_tga(gdIOCtx *ctx, oTga *tga)
case 32:
break;
default:
fprintf(stderr, "bps %i not supported", tga->bits);
gd_error("bps %i not supported", tga->bits);
return -1;
break;
}

View File

@ -91,13 +91,13 @@ tiff_handle * new_tiff_handle(gdIOCtx *g)
tiff_handle * t;
if (!g) {
fprintf(stderr, "Cannot create a new tiff handle, missing Ctx argument");
gd_error("Cannot create a new tiff handle, missing Ctx argument");
return NULL;
}
t = (tiff_handle *) gdMalloc(sizeof(tiff_handle));
if (!t) {
fprintf(stderr, "Failed to allocate a new tiff handle");
gd_error("Failed to allocate a new tiff handle");
return NULL;
}
@ -369,7 +369,7 @@ void tiffWriter(gdImagePtr image, gdIOCtx *out, int bitDepth)
/* Write the scan line to the tiff */
if(TIFFWriteEncodedStrip(tiff, y, scan, width * samplesPerPixel) == -1) {
/* error handler here */
fprintf(stderr, "Could not create TIFF\n");
gd_error("Could not create TIFF\n");
return;
}
}
@ -463,7 +463,7 @@ static int readTiffColorMap(gdImagePtr im, TIFF *tif, char is_bw, int photometri
return GD_SUCCESS;
} else if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &redcmap, &greencmap, &bluecmap)) {
fprintf(stderr, "Cannot read the color map");
gd_error("Cannot read the color map");
return GD_FAILURE;
}
@ -670,12 +670,12 @@ static int createFromTiffLines(TIFF *tif, gdImagePtr im, uint16 bps, uint16 phot
}
if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &im_height)) {
fprintf(stderr, "Can't fetch TIFF height\n");
gd_error("Can't fetch TIFF height\n");
return FALSE;
}
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &im_width)) {
fprintf(stderr, "Can't fetch TIFF width \n");
gd_error("Can't fetch TIFF width \n");
return FALSE;
}
@ -694,7 +694,7 @@ static int createFromTiffLines(TIFF *tif, gdImagePtr im, uint16 bps, uint16 phot
case 8:
for (y = 0; y < im_height; y++ ) {
if (!TIFFReadScanline (tif, buffer, y, 0)) {
fprintf(stderr, "Error while reading scanline %i", y);
gd_error("Error while reading scanline %i", y);
break;
}
/* reading one line at a time */
@ -706,7 +706,7 @@ static int createFromTiffLines(TIFF *tif, gdImagePtr im, uint16 bps, uint16 phot
if (is_bw) {
for (y = 0; y < im_height; y++ ) {
if (!TIFFReadScanline (tif, buffer, y, 0)) {
fprintf(stderr, "Error while reading scanline %i", y);
gd_error("Error while reading scanline %i", y);
break;
}
/* reading one line at a time */
@ -805,17 +805,17 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile)
tiff_unmapproc);
if (!tif) {
fprintf(stderr, "Cannot open TIFF image");
gd_error("Cannot open TIFF image");
return NULL;
}
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width)) {
fprintf(stderr, "TIFF error, Cannot read image width");
gd_error("TIFF error, Cannot read image width");
goto error;
}
if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height)) {
fprintf(stderr, "TIFF error, Cannot read image width");
gd_error("TIFF error, Cannot read image width");
goto error;
}
@ -839,11 +839,11 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile)
compression == COMPRESSION_CCITTFAX4 ||
compression == COMPRESSION_CCITTRLE ||
compression == COMPRESSION_CCITTRLEW)) {
fprintf(stderr, "Could not get photometric. "
gd_error("Could not get photometric. "
"Image is CCITT compressed, assuming min-is-white");
photometric = PHOTOMETRIC_MINISWHITE;
} else {
fprintf(stderr, "Could not get photometric. "
gd_error("Could not get photometric. "
"Assuming min-is-black");
photometric = PHOTOMETRIC_MINISBLACK;
@ -862,7 +862,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile)
--extra;
} else if (extra > 0 && (extra_types[0] == EXTRASAMPLE_UNSPECIFIED)) {
/* assuming unassociated alpha if unspecified */
fprintf(stderr, "alpha channel type not defined, assuming alpha is not premultiplied");
gd_error("alpha channel type not defined, assuming alpha is not premultiplied");
has_alpha = TRUE;
save_transparent = TRUE;
--extra;
@ -959,7 +959,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile)
break;
default:
fprintf (stderr, "Orientation %d not handled yet!", orientation);
gd_error("Orientation %d not handled yet!", orientation);
break;
}
}

View File

@ -97,7 +97,7 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
/* create the WBMP */
if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
fprintf(stderr, "Could not create WBMP\n");
gd_error("Could not create WBMP\n");
return;
}
@ -114,7 +114,7 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
/* write the WBMP to a gd file descriptor */
if(writewbmp(wbmp, &gd_putout, out)) {
fprintf(stderr, "Could not save WBMP\n");
gd_error("Could not save WBMP\n");
}
/* des submitted this bugfix: gdFree the memory. */

View File

@ -50,7 +50,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpPtr (int size, void *data)
if (Y) free(Y);
if (U) free(U);
if (V) free(V);
fprintf(stderr, "WebP decode: fail to decode input data");
gd_error("WebP decode: fail to decode input data");
return NULL;
}
im = gdImageCreateTrueColor(width, height);
@ -79,7 +79,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
filedata = gdMalloc(size);
if (!filedata) {
fprintf(stderr, "WebP decode: alloc failed");
gd_error("WebP decode: alloc failed");
return NULL;
}
gdGetBuf(filedata, size, infile);
@ -89,7 +89,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
if (Y) free(Y);
if (U) free(U);
if (V) free(V);
fprintf(stderr, "WebP decode: fail to decode input data");
gd_error("WebP decode: fail to decode input data");
return NULL;
}
im = gdImageCreateTrueColor(width, height);
@ -144,7 +144,7 @@ int mapQualityToVP8QP(int quality) {
const float vp8qp =
scale * (MAX_QUALITY - quality) / (MAX_QUALITY - MIN_QUALITY) + MIN_VP8QP;
if (quality < MIN_QUALITY || quality > MAX_QUALITY) {
fprintf(stderr, "Wrong quality value %d.", quality);
gd_error("Wrong quality value %d.", quality);
return -1;
}
@ -175,7 +175,7 @@ BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantiza
yuv_nbytes = width * height + 2 * yuv_width * yuv_height;
if ((Y = (unsigned char *)gdCalloc(yuv_nbytes, sizeof(unsigned char))) == NULL) {
fprintf(stderr, "gd-webp error: cannot allocate Y buffer");
gd_error("gd-webp error: cannot allocate Y buffer");
return;
}
vp8_quality = mapQualityToVP8QP(quantization);
@ -193,7 +193,7 @@ BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantiza
if (filedata) {
free(filedata);
}
fprintf(stderr, "gd-webp error: WebP Encoder failed");
gd_error("gd-webp error: WebP Encoder failed");
return;
}

View File

@ -495,10 +495,6 @@ fontFetch (char **error, void *key)
return NULL;
}
#if 0
fprintf(stderr,"fontpathname=%s\n",fullname);
#endif
err = FT_New_Face(*b->library, a->fontpath, 0, &a->face);
/* Read kerning metrics for Postscript fonts. */
@ -1035,10 +1031,6 @@ BGD_DECLARE(char *) gdImageStringFTEx (gdImage * im, int *brect, int fg, char *f
}
#endif
#if 0
fprintf(stderr,"dpi=%d,%d metric_res=%d ptsize=%g\n",hdpi,vdpi,METRIC_RES,ptsize);
#endif
oldpenf.x = oldpenf.y = 0; /* for postscript xshow operator */
penf.x = penf.y = 0; /* running position of non-rotated glyphs */
previous = 0; /* index of previous glyph for kerning calculations */

View File

@ -231,7 +231,7 @@ main (int argc, char *argv[])
"bottom text", gdTrueColorAlpha (240, 240, 255, 32));
out = fopen ("gdfx.png", "wb");
if (!out) {
fprintf (stderr, "Can't create gdfx.png\n");
gd_error("Can't create gdfx.png\n");
return 1;
}
gdImagePng (im, out);

View File

@ -10,6 +10,7 @@
#include <string.h>
#include "gd.h"
#include "gdhelpers.h"
#include "gd_errors.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
@ -94,34 +95,6 @@ iconv_close (iconv_t cd)
#define ESC 27
#define SS2 142
static void
debug (const char *format, ...)
{
#ifdef DEBUG
va_list args;
va_start (args, format);
fprintf (stdout, "%s: ", LIBNAME);
vfprintf (stdout, format, args);
fprintf (stdout, "\n");
va_end (args);
#else
(void)format;
#endif
}
static void
error (const char *format, ...)
{
va_list args;
va_start (args, format);
fprintf (stderr, "%s: ", LIBNAME);
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
va_end (args);
}
/* DetectKanjiCode() derived from DetectCodeType() by Ken Lunde. */
static int
@ -200,11 +173,11 @@ DetectKanjiCode (unsigned char *str)
#ifdef DEBUG
if (whatcode == ASCII)
debug ("Kanji code not included.");
gd_error_ex(E_DEBUG, "Kanji code not included.");
else if (whatcode == EUCORSJIS)
debug ("Kanji code not detected.");
gd_error_ex(E_DEBUG, "Kanji code not detected.");
else
debug ("Kanji code detected at %d byte.", i);
gd_error_ex(E_DEBUG, "Kanji code detected at %d byte.", i);
#endif
if (whatcode == EUCORSJIS && oldcode != ASCII)
@ -367,7 +340,7 @@ do_convert (unsigned char **to_p, unsigned char **from_p, const char *code)
size_t from_len, to_len;
if ((cd = iconv_open (EUCSTR, code)) == (iconv_t) - 1) {
error ("iconv_open() error");
gd_error ("iconv_open() error");
#ifdef HAVE_ERRNO_H
if (errno == EINVAL)
error ("invalid code specification: \"%s\" or \"%s\"", EUCSTR, code);
@ -383,20 +356,20 @@ do_convert (unsigned char **to_p, unsigned char **from_p, const char *code)
== -1) {
#ifdef HAVE_ERRNO_H
if (errno == EINVAL)
error ("invalid end of input string");
gd_error ("invalid end of input string");
else if (errno == EILSEQ)
error ("invalid code in input string");
gd_error ("invalid code in input string");
else if (errno == E2BIG)
error ("output buffer overflow at do_convert()");
gd_error ("output buffer overflow at do_convert()");
else
#endif
error ("something happen");
gd_error ("something happen");
ustrcpy (to, from);
return;
}
if (iconv_close (cd) != 0) {
error ("iconv_close() error");
gd_error ("iconv_close() error");
}
#else
int p1, p2, i, j;
@ -447,12 +420,12 @@ do_convert (unsigned char **to_p, unsigned char **from_p, const char *code)
}
}
} else {
error ("invalid code specification: \"%s\"", code);
gd_error ("invalid code specification: \"%s\"", code);
return;
}
if (j >= BUFSIZ) {
error ("output buffer overflow at do_convert()");
gd_error ("output buffer overflow at do_convert()");
ustrcpy (to, from);
} else
to[j] = '\0';
@ -469,44 +442,43 @@ do_check_and_conv (unsigned char *to, unsigned char *from)
switch (DetectKanjiCode (from)) {
case NEW:
debug ("Kanji code is New JIS.");
gd_error_ex(E_DEBUG, "Kanji code is New JIS.");
do_convert (&tmp_p, &from, NEWJISSTR);
break;
case OLD:
debug ("Kanji code is Old JIS.");
gd_error_ex(E_DEBUG, "Kanji code is Old JIS.");
do_convert (&tmp_p, &from, OLDJISSTR);
break;
case ESCI:
debug
("This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
gd_error_ex(E_DEBUG, "This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
do_convert (&tmp_p, &from, NEWJISSTR);
break;
case NEC:
debug ("Kanji code is NEC Kanji.");
error ("cannot convert NEC Kanji.");
gd_error_ex(E_DEBUG, "Kanji code is NEC Kanji.");
gd_error("cannot convert NEC Kanji.");
ustrcpy (tmp, from);
kanji = FALSE;
break;
case EUC:
debug ("Kanji code is EUC.");
gd_error_ex(E_DEBUG, "Kanji code is EUC.");
ustrcpy (tmp, from);
break;
case SJIS:
debug ("Kanji code is SJIS.");
gd_error_ex(E_DEBUG, "Kanji code is SJIS.");
do_convert (&tmp_p, &from, SJISSTR);
break;
case EUCORSJIS:
debug ("Kanji code is EUC or SJIS.");
gd_error_ex(E_DEBUG, "Kanji code is EUC or SJIS.");
ustrcpy (tmp, from);
kanji = FALSE;
break;
case ASCII:
debug ("This is ASCII string.");
gd_error_ex(E_DEBUG, "This is ASCII string.");
ustrcpy (tmp, from);
kanji = FALSE;
break;
default:
debug ("This string includes unknown code.");
gd_error_ex(E_DEBUG, "This string includes unknown code.");
ustrcpy (tmp, from);
kanji = FALSE;
break;
@ -535,7 +507,7 @@ do_check_and_conv (unsigned char *to, unsigned char *from)
}
if (j >= BUFSIZ) {
error ("output buffer overflow at Hankaku --> Zenkaku");
gd_error("output buffer overflow at Hankaku --> Zenkaku");
ustrcpy (to, tmp);
} else
to[j] = '\0';
@ -552,7 +524,7 @@ any2eucjp (unsigned char *dest, unsigned char *src, unsigned int dest_max)
int ret;
if (strlen ((const char *) src) >= BUFSIZ) {
error ("input string too large");
gd_error("input string too large");
return -1;
}
if (dest_max > BUFSIZ) {
@ -563,7 +535,7 @@ any2eucjp (unsigned char *dest, unsigned char *src, unsigned int dest_max)
}
ret = do_check_and_conv (tmp_dest, src);
if (strlen ((const char *) tmp_dest) >= dest_max) {
error ("output buffer overflow");
gd_error("output buffer overflow");
ustrcpy (dest, src);
return -1;
}

View File

@ -1,4 +1,3 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

View File

@ -60,8 +60,7 @@ main (int argc, char *argv[])
} else {
/* 2.02: usage message. Defaulting to Times wasn't working well for the
many people with no /usr/share/fonts/truetype. */
fprintf (stderr, "Usage: gdtestft fontfilename\n");
fprintf (stderr,
fprintf(stderr, "Usage: gdtestft fontfilename\n"
"If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
"it. If GDFONTPATH is not set, /usr/share/fonts/truetype is searched.\n");
exit (1);

View File

@ -15,12 +15,13 @@
#include "gd.h"
#include "gdhelpers.h"
#include "gd_color_map.h"
#include "gd_errors.h"
#ifndef HAVE_LIBXPM
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm(char *filename)
{
(void)filename;
fprintf(stderr, "libgd was not built with xpm support\n");
gd_error_ex(E_ERROR, "libgd was not built with xpm support\n");
return NULL;
}
#else

View File

@ -1,4 +1,3 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

View File

@ -41,13 +41,13 @@ main (int argc, char *argv[])
gdImagePng (im, out);
fclose (out);
#else
fprintf(stderr, "Compiled without libpng support\n");
gd_error("Compiled without libpng support\n");
#endif /* HAVE_LIBPNG */
gdImageDestroy (im);
return 0;
#else
fprintf(stderr, "Compiled without freetype support\n");
gd_error("Compiled without freetype support\n");
return 0;
#endif /* HAVE_LIBFREETYPE */
}

View File

@ -555,7 +555,7 @@ void gd_RGBAToYUV420(gdImagePtr im2,
palette image. */
im = gdImageCreateTrueColor(im2->sx, im2->sy);
if (!im) {
fprintf(stderr, "gd-webp error: cannot convert palette input to truecolor");
gd_error("gd-webp error: cannot convert palette input to truecolor");
return;
}
gdImageCopy(im, im2, 0, 0, 0, 0, im->sx, im->sy);

View File

@ -53,7 +53,7 @@ main (int argc, char **argv)
in = fopen (argv[argc - 1], "rb");
}
if (!in) {
fprintf (stderr, "Error: can't open file %s.\n", argv[argc - 1]);
fprintf(stderr, "can't open file %s.\n", argv[argc - 1]);
exit (1);
}
/* Now load the image. */
@ -78,7 +78,7 @@ main (int argc, char **argv)
goto usage;
} else if (!strcmp (argv[i], "-i")) {
if (i == (argc - 2)) {
fprintf (stderr, "Error: -i specified without y or n.\n");
fprintf(stderr, "-i specified without y or n.\n");
no = 1;
goto usage;
}