blob: a800884df1293bcb428720fa6bf9f04bfa262b5a [file] [log] [blame]
Guy Schalnat6d764711995-12-19 03:22:19 -06001
2/* pngerror.c - stub functions for i/o and memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
4 * libpng 1.00.97
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * May 28, 1997
9 *
10 * This file provides a location for all error handling. Users which
11 * need special error handling are expected to write replacement functions
12 * and use png_set_error_fn() to use those functions. See the instructions
13 * at each function.
14 */
Guy Schalnat6d764711995-12-19 03:22:19 -060015
16#define PNG_INTERNAL
17#include "png.h"
18
Guy Schalnate5a37791996-06-05 15:50:50 -050019static void png_default_error PNGARG((png_structp png_ptr,
20 png_const_charp message));
21static void png_default_warning PNGARG((png_structp png_ptr,
22 png_const_charp message));
23
Guy Schalnat69b14481996-01-10 02:56:49 -060024/* This function is called whenever there is a fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060025 * should not be changed. If there is a need to handle errors differently,
26 * you should supply a replacement error function and use png_set_error_fn()
27 * to replace the error function at run-time.
28 */
Guy Schalnat6d764711995-12-19 03:22:19 -060029void
30png_error(png_structp png_ptr, png_const_charp message)
31{
Andreas Dilger47a0c421997-05-16 02:46:07 -050032 if (png_ptr->error_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060033 (*(png_ptr->error_fn))(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060034
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060035 /* if the following returns or doesn't exist, use the default function,
36 which will not return */
37 png_default_error(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060038}
39
Guy Schalnat69b14481996-01-10 02:56:49 -060040/* This function is called whenever there is a non-fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060041 * should not be changed. If there is a need to handle warnings differently,
42 * you should supply a replacement warning function and use
43 * png_set_error_fn() to replace the warning function at run-time.
44 */
Guy Schalnat6d764711995-12-19 03:22:19 -060045void
46png_warning(png_structp png_ptr, png_const_charp message)
47{
Andreas Dilger47a0c421997-05-16 02:46:07 -050048 if (png_ptr->warning_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060049 (*(png_ptr->warning_fn))(png_ptr, message);
50 else
51 png_default_warning(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060052}
53
Guy Schalnat4ee97b01996-01-16 01:51:56 -060054/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * this function MUST NOT RETURN, or the program will likely crash. This
56 * function is used by default, or if the program supplies NULL for the
57 * error function pointer in png_set_error_fn().
58 */
Guy Schalnate5a37791996-06-05 15:50:50 -050059static void
Guy Schalnat6d764711995-12-19 03:22:19 -060060png_default_error(png_structp png_ptr, png_const_charp message)
61{
62#ifndef PNG_NO_STDIO
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060063 fprintf(stderr, "libpng error: %s\n", message);
Guy Schalnat6d764711995-12-19 03:22:19 -060064#endif
65
66#ifdef USE_FAR_KEYWORD
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060067 {
68 jmp_buf jmpbuf;
69 png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
70 longjmp(jmpbuf, 1);
71 }
Guy Schalnat6d764711995-12-19 03:22:19 -060072#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060073 longjmp(png_ptr->jmpbuf, 1);
Guy Schalnat6d764711995-12-19 03:22:19 -060074#endif
75}
76
Guy Schalnat69b14481996-01-10 02:56:49 -060077/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 * it can continue anyway. Replacement functions don't have to do anything
79 * here if you don't want to. In the default configuration, png_ptr is
80 * not used, but it is passed in case it may be useful.
81 */
Guy Schalnate5a37791996-06-05 15:50:50 -050082static void
Guy Schalnat6d764711995-12-19 03:22:19 -060083png_default_warning(png_structp png_ptr, png_const_charp message)
84{
Andreas Dilger47a0c421997-05-16 02:46:07 -050085 if (png_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060086 return;
Guy Schalnat6d764711995-12-19 03:22:19 -060087
88#ifndef PNG_NO_STDIO
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060089 fprintf(stderr, "libpng warning: %s\n", message);
Guy Schalnat6d764711995-12-19 03:22:19 -060090#endif
91}
92
Guy Schalnat69b14481996-01-10 02:56:49 -060093/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * of handling errors and warnings. Note that the error function MUST NOT
95 * return to the calling routine or serious problems will occur. The return
96 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
97 */
Guy Schalnat6d764711995-12-19 03:22:19 -060098void
Guy Schalnate5a37791996-06-05 15:50:50 -050099png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
100 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600101{
Guy Schalnate5a37791996-06-05 15:50:50 -0500102 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600103 png_ptr->error_fn = error_fn;
104 png_ptr->warning_fn = warning_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -0600105}
106
107
Guy Schalnate5a37791996-06-05 15:50:50 -0500108/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600109 * functions. The application should free any memory associated with this
110 * pointer before png_write_destroy and png_read_destroy are called.
111 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_voidp
Guy Schalnate5a37791996-06-05 15:50:50 -0500113png_get_error_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600114{
Guy Schalnate5a37791996-06-05 15:50:50 -0500115 return png_ptr->error_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600116}
117
118
119