Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libpng/pngerror.c
21551 views
1
/* pngerror.c - stub functions for i/o and memory allocation
2
*
3
* Copyright (c) 2018-2025 Cosmin Truta
4
* Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
5
* Copyright (c) 1996-1997 Andreas Dilger
6
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
*
8
* This code is released under the libpng license.
9
* For conditions of distribution and use, see the disclaimer
10
* and license in png.h
11
*
12
* This file provides a location for all error handling. Users who
13
* need special error handling are expected to write replacement functions
14
* and use png_set_error_fn() to use those functions. See the instructions
15
* at each function.
16
*/
17
18
#include "pngpriv.h"
19
20
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
21
22
static PNG_FUNCTION(void /* PRIVATE */,
23
png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
24
PNG_NORETURN);
25
26
#ifdef PNG_WARNINGS_SUPPORTED
27
static void /* PRIVATE */
28
png_default_warning(png_const_structrp png_ptr,
29
png_const_charp warning_message);
30
#endif /* WARNINGS */
31
32
/* This function is called whenever there is a fatal error. This function
33
* should not be changed. If there is a need to handle errors differently,
34
* you should supply a replacement error function and use png_set_error_fn()
35
* to replace the error function at run-time.
36
*/
37
#ifdef PNG_ERROR_TEXT_SUPPORTED
38
PNG_FUNCTION(void,PNGAPI
39
png_error,(png_const_structrp png_ptr, png_const_charp error_message),
40
PNG_NORETURN)
41
{
42
if (png_ptr != NULL && png_ptr->error_fn != NULL)
43
(*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr),
44
error_message);
45
46
/* If the custom handler doesn't exist, or if it returns,
47
use the default handler, which will not return. */
48
png_default_error(png_ptr, error_message);
49
}
50
#else
51
PNG_FUNCTION(void,PNGAPI
52
png_err,(png_const_structrp png_ptr),
53
PNG_NORETURN)
54
{
55
/* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
56
* erroneously as '\0', instead of the empty string "". This was
57
* apparently an error, introduced in libpng-1.2.20, and png_default_error
58
* will crash in this case.
59
*/
60
if (png_ptr != NULL && png_ptr->error_fn != NULL)
61
(*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), "");
62
63
/* If the custom handler doesn't exist, or if it returns,
64
use the default handler, which will not return. */
65
png_default_error(png_ptr, "");
66
}
67
#endif /* ERROR_TEXT */
68
69
/* Utility to safely appends strings to a buffer. This never errors out so
70
* error checking is not required in the caller.
71
*/
72
size_t
73
png_safecat(png_charp buffer, size_t bufsize, size_t pos,
74
png_const_charp string)
75
{
76
if (buffer != NULL && pos < bufsize)
77
{
78
if (string != NULL)
79
while (*string != '\0' && pos < bufsize-1)
80
buffer[pos++] = *string++;
81
82
buffer[pos] = '\0';
83
}
84
85
return pos;
86
}
87
88
#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
89
/* Utility to dump an unsigned value into a buffer, given a start pointer and
90
* and end pointer (which should point just *beyond* the end of the buffer!)
91
* Returns the pointer to the start of the formatted string.
92
*/
93
png_charp
94
png_format_number(png_const_charp start, png_charp end, int format,
95
png_alloc_size_t number)
96
{
97
int count = 0; /* number of digits output */
98
int mincount = 1; /* minimum number required */
99
int output = 0; /* digit output (for the fixed point format) */
100
101
*--end = '\0';
102
103
/* This is written so that the loop always runs at least once, even with
104
* number zero.
105
*/
106
while (end > start && (number != 0 || count < mincount))
107
{
108
109
static const char digits[] = "0123456789ABCDEF";
110
111
switch (format)
112
{
113
case PNG_NUMBER_FORMAT_fixed:
114
/* Needs five digits (the fraction) */
115
mincount = 5;
116
if (output != 0 || number % 10 != 0)
117
{
118
*--end = digits[number % 10];
119
output = 1;
120
}
121
number /= 10;
122
break;
123
124
case PNG_NUMBER_FORMAT_02u:
125
/* Expects at least 2 digits. */
126
mincount = 2;
127
/* FALLTHROUGH */
128
129
case PNG_NUMBER_FORMAT_u:
130
*--end = digits[number % 10];
131
number /= 10;
132
break;
133
134
case PNG_NUMBER_FORMAT_02x:
135
/* This format expects at least two digits */
136
mincount = 2;
137
/* FALLTHROUGH */
138
139
case PNG_NUMBER_FORMAT_x:
140
*--end = digits[number & 0xf];
141
number >>= 4;
142
break;
143
144
default: /* an error */
145
number = 0;
146
break;
147
}
148
149
/* Keep track of the number of digits added */
150
++count;
151
152
/* Float a fixed number here: */
153
if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
154
{
155
/* End of the fraction, but maybe nothing was output? In that case
156
* drop the decimal point. If the number is a true zero handle that
157
* here.
158
*/
159
if (output != 0)
160
*--end = '.';
161
else if (number == 0) /* and !output */
162
*--end = '0';
163
}
164
}
165
166
return end;
167
}
168
#endif
169
170
#ifdef PNG_WARNINGS_SUPPORTED
171
/* This function is called whenever there is a non-fatal error. This function
172
* should not be changed. If there is a need to handle warnings differently,
173
* you should supply a replacement warning function and use
174
* png_set_error_fn() to replace the warning function at run-time.
175
*/
176
void PNGAPI
177
png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
178
{
179
int offset = 0;
180
if (png_ptr != NULL && png_ptr->warning_fn != NULL)
181
(*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
182
warning_message + offset);
183
else
184
png_default_warning(png_ptr, warning_message + offset);
185
}
186
187
/* These functions support 'formatted' warning messages with up to
188
* PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter
189
* is introduced by @<number>, where 'number' starts at 1. This follows the
190
* standard established by X/Open for internationalizable error messages.
191
*/
192
void
193
png_warning_parameter(png_warning_parameters p, int number,
194
png_const_charp string)
195
{
196
if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
197
(void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
198
}
199
200
void
201
png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
202
png_alloc_size_t value)
203
{
204
char buffer[PNG_NUMBER_BUFFER_SIZE] = {0};
205
png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
206
}
207
208
void
209
png_warning_parameter_signed(png_warning_parameters p, int number, int format,
210
png_int_32 value)
211
{
212
png_alloc_size_t u;
213
png_charp str;
214
char buffer[PNG_NUMBER_BUFFER_SIZE] = {0};
215
216
/* Avoid overflow by doing the negate in a png_alloc_size_t: */
217
u = (png_alloc_size_t)value;
218
if (value < 0)
219
u = ~u + 1;
220
221
str = PNG_FORMAT_NUMBER(buffer, format, u);
222
223
if (value < 0 && str > buffer)
224
*--str = '-';
225
226
png_warning_parameter(p, number, str);
227
}
228
229
void
230
png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
231
png_const_charp message)
232
{
233
/* The internal buffer is just 192 bytes - enough for all our messages,
234
* overflow doesn't happen because this code checks! If someone figures
235
* out how to send us a message longer than 192 bytes, all that will
236
* happen is that the message will be truncated appropriately.
237
*/
238
size_t i = 0; /* Index in the msg[] buffer: */
239
char msg[192];
240
241
/* Each iteration through the following loop writes at most one character
242
* to msg[i++] then returns here to validate that there is still space for
243
* the trailing '\0'. It may (in the case of a parameter) read more than
244
* one character from message[]; it must check for '\0' and continue to the
245
* test if it finds the end of string.
246
*/
247
while (i<(sizeof msg)-1 && *message != '\0')
248
{
249
/* '@' at end of string is now just printed (previously it was skipped);
250
* it is an error in the calling code to terminate the string with @.
251
*/
252
if (p != NULL && *message == '@' && message[1] != '\0')
253
{
254
int parameter_char = *++message; /* Consume the '@' */
255
static const char valid_parameters[] = "123456789";
256
int parameter = 0;
257
258
/* Search for the parameter digit, the index in the string is the
259
* parameter to use.
260
*/
261
while (valid_parameters[parameter] != parameter_char &&
262
valid_parameters[parameter] != '\0')
263
++parameter;
264
265
/* If the parameter digit is out of range it will just get printed. */
266
if (parameter < PNG_WARNING_PARAMETER_COUNT)
267
{
268
/* Append this parameter */
269
png_const_charp parm = p[parameter];
270
png_const_charp pend = p[parameter] + (sizeof p[parameter]);
271
272
/* No need to copy the trailing '\0' here, but there is no guarantee
273
* that parm[] has been initialized, so there is no guarantee of a
274
* trailing '\0':
275
*/
276
while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
277
msg[i++] = *parm++;
278
279
/* Consume the parameter digit too: */
280
++message;
281
continue;
282
}
283
284
/* else not a parameter and there is a character after the @ sign; just
285
* copy that. This is known not to be '\0' because of the test above.
286
*/
287
}
288
289
/* At this point *message can't be '\0', even in the bad parameter case
290
* above where there is a lone '@' at the end of the message string.
291
*/
292
msg[i++] = *message++;
293
}
294
295
/* i is always less than (sizeof msg), so: */
296
msg[i] = '\0';
297
298
/* And this is the formatted message. It may be larger than
299
* PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
300
* are not (currently) formatted.
301
*/
302
png_warning(png_ptr, msg);
303
}
304
#endif /* WARNINGS */
305
306
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
307
void PNGAPI
308
png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
309
{
310
if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
311
{
312
# ifdef PNG_READ_SUPPORTED
313
if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
314
png_ptr->chunk_name != 0)
315
png_chunk_warning(png_ptr, error_message);
316
else
317
# endif
318
png_warning(png_ptr, error_message);
319
}
320
321
else
322
{
323
# ifdef PNG_READ_SUPPORTED
324
if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
325
png_ptr->chunk_name != 0)
326
png_chunk_error(png_ptr, error_message);
327
else
328
# endif
329
png_error(png_ptr, error_message);
330
}
331
332
# ifndef PNG_ERROR_TEXT_SUPPORTED
333
PNG_UNUSED(error_message)
334
# endif
335
}
336
337
void /* PRIVATE */
338
png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
339
{
340
if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
341
png_warning(png_ptr, error_message);
342
else
343
png_error(png_ptr, error_message);
344
345
# ifndef PNG_ERROR_TEXT_SUPPORTED
346
PNG_UNUSED(error_message)
347
# endif
348
}
349
350
void /* PRIVATE */
351
png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
352
{
353
if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
354
png_warning(png_ptr, error_message);
355
else
356
png_error(png_ptr, error_message);
357
358
# ifndef PNG_ERROR_TEXT_SUPPORTED
359
PNG_UNUSED(error_message)
360
# endif
361
}
362
#endif /* BENIGN_ERRORS */
363
364
#define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
365
#if defined(PNG_WARNINGS_SUPPORTED) || \
366
(defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
367
/* These utilities are used internally to build an error message that relates
368
* to the current chunk. The chunk name comes from png_ptr->chunk_name,
369
* which is used to prefix the message. The message is limited in length
370
* to 63 bytes. The name characters are output as hex digits wrapped in []
371
* if the character is invalid.
372
*/
373
#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
374
static const char png_digit[16] = {
375
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
376
'A', 'B', 'C', 'D', 'E', 'F'
377
};
378
379
static void /* PRIVATE */
380
png_format_buffer(png_const_structrp png_ptr, png_charp buffer,
381
png_const_charp error_message)
382
{
383
png_uint_32 chunk_name = png_ptr->chunk_name;
384
int iout = 0, ishift = 24;
385
386
while (ishift >= 0)
387
{
388
int c = (int)(chunk_name >> ishift) & 0xff;
389
390
ishift -= 8;
391
if (isnonalpha(c) != 0)
392
{
393
buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
394
buffer[iout++] = png_digit[(c & 0xf0) >> 4];
395
buffer[iout++] = png_digit[c & 0x0f];
396
buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
397
}
398
399
else
400
{
401
buffer[iout++] = (char)c;
402
}
403
}
404
405
if (error_message == NULL)
406
buffer[iout] = '\0';
407
408
else
409
{
410
int iin = 0;
411
412
buffer[iout++] = ':';
413
buffer[iout++] = ' ';
414
415
while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
416
buffer[iout++] = error_message[iin++];
417
418
/* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
419
buffer[iout] = '\0';
420
}
421
}
422
#endif /* WARNINGS || ERROR_TEXT */
423
424
#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
425
PNG_FUNCTION(void,PNGAPI
426
png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
427
PNG_NORETURN)
428
{
429
char msg[18+PNG_MAX_ERROR_TEXT];
430
if (png_ptr == NULL)
431
png_error(png_ptr, error_message);
432
433
else
434
{
435
png_format_buffer(png_ptr, msg, error_message);
436
png_error(png_ptr, msg);
437
}
438
}
439
#endif /* READ && ERROR_TEXT */
440
441
#ifdef PNG_WARNINGS_SUPPORTED
442
void PNGAPI
443
png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
444
{
445
char msg[18+PNG_MAX_ERROR_TEXT];
446
if (png_ptr == NULL)
447
png_warning(png_ptr, warning_message);
448
449
else
450
{
451
png_format_buffer(png_ptr, msg, warning_message);
452
png_warning(png_ptr, msg);
453
}
454
}
455
#endif /* WARNINGS */
456
457
#ifdef PNG_READ_SUPPORTED
458
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
459
void PNGAPI
460
png_chunk_benign_error(png_const_structrp png_ptr,
461
png_const_charp error_message)
462
{
463
if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
464
png_chunk_warning(png_ptr, error_message);
465
466
else
467
png_chunk_error(png_ptr, error_message);
468
469
# ifndef PNG_ERROR_TEXT_SUPPORTED
470
PNG_UNUSED(error_message)
471
# endif
472
}
473
#endif
474
#endif /* READ */
475
476
void /* PRIVATE */
477
png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
478
{
479
# ifndef PNG_WARNINGS_SUPPORTED
480
PNG_UNUSED(message)
481
# endif
482
483
/* This is always supported, but for just read or just write it
484
* unconditionally does the right thing.
485
*/
486
# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
487
if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
488
# endif
489
490
# ifdef PNG_READ_SUPPORTED
491
{
492
if (error < PNG_CHUNK_ERROR)
493
png_chunk_warning(png_ptr, message);
494
495
else
496
png_chunk_benign_error(png_ptr, message);
497
}
498
# endif
499
500
# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
501
else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
502
# endif
503
504
# ifdef PNG_WRITE_SUPPORTED
505
{
506
if (error < PNG_CHUNK_WRITE_ERROR)
507
png_app_warning(png_ptr, message);
508
509
else
510
png_app_error(png_ptr, message);
511
}
512
# endif
513
}
514
515
#ifdef PNG_ERROR_TEXT_SUPPORTED
516
#ifdef PNG_FLOATING_POINT_SUPPORTED
517
PNG_FUNCTION(void,
518
png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),
519
PNG_NORETURN)
520
{
521
# define fixed_message "fixed point overflow in "
522
# define fixed_message_ln ((sizeof fixed_message)-1)
523
unsigned int iin;
524
char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
525
memcpy(msg, fixed_message, fixed_message_ln);
526
iin = 0;
527
if (name != NULL)
528
while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
529
{
530
msg[fixed_message_ln + iin] = name[iin];
531
++iin;
532
}
533
msg[fixed_message_ln + iin] = 0;
534
png_error(png_ptr, msg);
535
}
536
#endif
537
#endif
538
539
#ifdef PNG_SETJMP_SUPPORTED
540
/* This API only exists if ANSI-C style error handling is used,
541
* otherwise it is necessary for png_default_error to be overridden.
542
*/
543
jmp_buf* PNGAPI
544
png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
545
size_t jmp_buf_size)
546
{
547
/* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
548
* and it must not change after that. Libpng doesn't care how big the
549
* buffer is, just that it doesn't change.
550
*
551
* If the buffer size is no *larger* than the size of jmp_buf when libpng is
552
* compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
553
* semantics that this call will not fail. If the size is larger, however,
554
* the buffer is allocated and this may fail, causing the function to return
555
* NULL.
556
*/
557
if (png_ptr == NULL)
558
return NULL;
559
560
if (png_ptr->jmp_buf_ptr == NULL)
561
{
562
png_ptr->jmp_buf_size = 0; /* not allocated */
563
564
if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
565
png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
566
567
else
568
{
569
png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
570
png_malloc_warn(png_ptr, jmp_buf_size));
571
572
if (png_ptr->jmp_buf_ptr == NULL)
573
return NULL; /* new NULL return on OOM */
574
575
png_ptr->jmp_buf_size = jmp_buf_size;
576
}
577
}
578
579
else /* Already allocated: check the size */
580
{
581
size_t size = png_ptr->jmp_buf_size;
582
583
if (size == 0)
584
{
585
size = (sizeof png_ptr->jmp_buf_local);
586
if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
587
{
588
/* This is an internal error in libpng: somehow we have been left
589
* with a stack allocated jmp_buf when the application regained
590
* control. It's always possible to fix this up, but for the moment
591
* this is a png_error because that makes it easy to detect.
592
*/
593
png_error(png_ptr, "Libpng jmp_buf still allocated");
594
/* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
595
}
596
}
597
598
if (size != jmp_buf_size)
599
{
600
png_warning(png_ptr, "Application jmp_buf size changed");
601
return NULL; /* caller will probably crash: no choice here */
602
}
603
}
604
605
/* Finally fill in the function, now we have a satisfactory buffer. It is
606
* valid to change the function on every call.
607
*/
608
png_ptr->longjmp_fn = longjmp_fn;
609
return png_ptr->jmp_buf_ptr;
610
}
611
612
void /* PRIVATE */
613
png_free_jmpbuf(png_structrp png_ptr)
614
{
615
if (png_ptr != NULL)
616
{
617
jmp_buf *jb = png_ptr->jmp_buf_ptr;
618
619
/* A size of 0 is used to indicate a local, stack, allocation of the
620
* pointer; used here and in png.c
621
*/
622
if (jb != NULL && png_ptr->jmp_buf_size > 0)
623
{
624
625
/* This stuff is so that a failure to free the error control structure
626
* does not leave libpng in a state with no valid error handling: the
627
* free always succeeds, if there is an error it gets ignored.
628
*/
629
if (jb != &png_ptr->jmp_buf_local)
630
{
631
/* Make an internal, libpng, jmp_buf to return here */
632
jmp_buf free_jmp_buf;
633
634
if (!setjmp(free_jmp_buf))
635
{
636
png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
637
png_ptr->jmp_buf_size = 0; /* stack allocation */
638
png_ptr->longjmp_fn = longjmp;
639
png_free(png_ptr, jb); /* Return to setjmp on error */
640
}
641
}
642
}
643
644
/* *Always* cancel everything out: */
645
png_ptr->jmp_buf_size = 0;
646
png_ptr->jmp_buf_ptr = NULL;
647
png_ptr->longjmp_fn = 0;
648
}
649
}
650
#endif
651
652
/* This is the default error handling function. Note that replacements for
653
* this function MUST NOT RETURN, or the program will likely crash. This
654
* function is used by default, or if the program supplies NULL for the
655
* error function pointer in png_set_error_fn().
656
*/
657
static PNG_FUNCTION(void /* PRIVATE */,
658
png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
659
PNG_NORETURN)
660
{
661
#ifdef PNG_CONSOLE_IO_SUPPORTED
662
fprintf(stderr, "libpng error: %s", error_message ? error_message :
663
"undefined");
664
fprintf(stderr, PNG_STRING_NEWLINE);
665
#else
666
PNG_UNUSED(error_message) /* Make compiler happy */
667
#endif
668
png_longjmp(png_ptr, 1);
669
}
670
671
PNG_FUNCTION(void,PNGAPI
672
png_longjmp,(png_const_structrp png_ptr, int val),
673
PNG_NORETURN)
674
{
675
#ifdef PNG_SETJMP_SUPPORTED
676
if (png_ptr != NULL && png_ptr->longjmp_fn != NULL &&
677
png_ptr->jmp_buf_ptr != NULL)
678
png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
679
#else
680
PNG_UNUSED(png_ptr)
681
PNG_UNUSED(val)
682
#endif
683
684
/* If control reaches this point, png_longjmp() must not return. The only
685
* choice is to terminate the whole process (or maybe the thread); to do
686
* this the ANSI-C abort() function is used unless a different method is
687
* implemented by overriding the default configuration setting for
688
* PNG_ABORT().
689
*/
690
PNG_ABORT();
691
}
692
693
#ifdef PNG_WARNINGS_SUPPORTED
694
/* This function is called when there is a warning, but the library thinks
695
* it can continue anyway. Replacement functions don't have to do anything
696
* here if you don't want them to. In the default configuration, png_ptr is
697
* not used, but it is passed in case it may be useful.
698
*/
699
static void /* PRIVATE */
700
png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
701
{
702
#ifdef PNG_CONSOLE_IO_SUPPORTED
703
fprintf(stderr, "libpng warning: %s", warning_message);
704
fprintf(stderr, PNG_STRING_NEWLINE);
705
#else
706
PNG_UNUSED(warning_message) /* Make compiler happy */
707
#endif
708
PNG_UNUSED(png_ptr) /* Make compiler happy */
709
}
710
#endif /* WARNINGS */
711
712
/* This function is called when the application wants to use another method
713
* of handling errors and warnings. Note that the error function MUST NOT
714
* return to the calling routine or serious problems will occur. The return
715
* method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
716
*/
717
void PNGAPI
718
png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
719
png_error_ptr error_fn, png_error_ptr warning_fn)
720
{
721
if (png_ptr == NULL)
722
return;
723
724
png_ptr->error_ptr = error_ptr;
725
png_ptr->error_fn = error_fn;
726
#ifdef PNG_WARNINGS_SUPPORTED
727
png_ptr->warning_fn = warning_fn;
728
#else
729
PNG_UNUSED(warning_fn)
730
#endif
731
}
732
733
734
/* This function returns a pointer to the error_ptr associated with the user
735
* functions. The application should free any memory associated with this
736
* pointer before png_write_destroy and png_read_destroy are called.
737
*/
738
png_voidp PNGAPI
739
png_get_error_ptr(png_const_structrp png_ptr)
740
{
741
if (png_ptr == NULL)
742
return NULL;
743
744
return (png_voidp)png_ptr->error_ptr;
745
}
746
747
748
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
749
void PNGAPI
750
png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
751
{
752
PNG_UNUSED(png_ptr)
753
PNG_UNUSED(strip_mode)
754
}
755
#endif
756
757
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
758
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
759
/* Currently the above both depend on SETJMP_SUPPORTED, however it would be
760
* possible to implement without setjmp support just so long as there is some
761
* way to handle the error return here:
762
*/
763
PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
764
png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
765
PNG_NORETURN)
766
{
767
png_const_structrp png_ptr = png_nonconst_ptr;
768
png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
769
770
/* An error is always logged here, overwriting anything (typically a warning)
771
* that is already there:
772
*/
773
if (image != NULL)
774
{
775
png_safecat(image->message, (sizeof image->message), 0, error_message);
776
image->warning_or_error |= PNG_IMAGE_ERROR;
777
778
/* Retrieve the jmp_buf from within the png_control, making this work for
779
* C++ compilation too is pretty tricky: C++ wants a pointer to the first
780
* element of a jmp_buf, but C doesn't tell us the type of that.
781
*/
782
if (image->opaque != NULL && image->opaque->error_buf != NULL)
783
longjmp(png_control_jmp_buf(image->opaque), 1);
784
785
/* Missing longjmp buffer, the following is to help debugging: */
786
{
787
size_t pos = png_safecat(image->message, (sizeof image->message), 0,
788
"bad longjmp: ");
789
png_safecat(image->message, (sizeof image->message), pos,
790
error_message);
791
}
792
}
793
794
/* Here on an internal programming error. */
795
abort();
796
}
797
798
#ifdef PNG_WARNINGS_SUPPORTED
799
void /* PRIVATE */ PNGCBAPI
800
png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
801
{
802
png_const_structrp png_ptr = png_nonconst_ptr;
803
png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
804
805
/* A warning is only logged if there is no prior warning or error. */
806
if (image->warning_or_error == 0)
807
{
808
png_safecat(image->message, (sizeof image->message), 0, warning_message);
809
image->warning_or_error |= PNG_IMAGE_WARNING;
810
}
811
}
812
#endif
813
814
int /* PRIVATE */
815
png_safe_execute(png_imagep image, int (*function)(png_voidp), png_voidp arg)
816
{
817
const png_voidp saved_error_buf = image->opaque->error_buf;
818
jmp_buf safe_jmpbuf;
819
820
/* Safely execute function(arg), with png_error returning back here. */
821
if (setjmp(safe_jmpbuf) == 0)
822
{
823
int result;
824
825
image->opaque->error_buf = safe_jmpbuf;
826
result = function(arg);
827
image->opaque->error_buf = saved_error_buf;
828
829
if (result)
830
return 1; /* success */
831
}
832
833
/* The function failed either because of a caught png_error and a regular
834
* return of false above or because of an uncaught png_error from the
835
* function itself. Ensure that the error_buf is always set back to the
836
* value saved above:
837
*/
838
image->opaque->error_buf = saved_error_buf;
839
840
/* On the final false return, when about to return control to the caller, the
841
* image is freed (png_image_free does this check but it is duplicated here
842
* for clarity:
843
*/
844
if (saved_error_buf == NULL)
845
png_image_free(image);
846
847
return 0; /* failure */
848
}
849
#endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */
850
#endif /* READ || WRITE */
851
852