pngerror.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Last changed in libpng 1.5.8 [February 1, 2011]
  4. * Copyright (c) 1998-2012 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 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. #include "pngpriv.h"
  18. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  19. static PNG_FUNCTION(void, png_default_error,PNGARG((png_structp png_ptr,
  20. png_const_charp error_message)),PNG_NORETURN);
  21. #ifdef PNG_WARNINGS_SUPPORTED
  22. static void /* PRIVATE */
  23. png_default_warning PNGARG((png_structp png_ptr,
  24. png_const_charp warning_message));
  25. #endif /* PNG_WARNINGS_SUPPORTED */
  26. /* This function is called whenever there is a fatal error. This function
  27. * should not be changed. If there is a need to handle errors differently,
  28. * you should supply a replacement error function and use png_set_error_fn()
  29. * to replace the error function at run-time.
  30. */
  31. #ifdef PNG_ERROR_TEXT_SUPPORTED
  32. PNG_FUNCTION(void,PNGAPI
  33. png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN)
  34. {
  35. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  36. char msg[16];
  37. if (png_ptr != NULL)
  38. {
  39. if (png_ptr->flags&
  40. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  41. {
  42. if (*error_message == PNG_LITERAL_SHARP)
  43. {
  44. /* Strip "#nnnn " from beginning of error message. */
  45. int offset;
  46. for (offset = 1; offset<15; offset++)
  47. if (error_message[offset] == ' ')
  48. break;
  49. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  50. {
  51. int i;
  52. for (i = 0; i < offset - 1; i++)
  53. msg[i] = error_message[i + 1];
  54. msg[i - 1] = '\0';
  55. error_message = msg;
  56. }
  57. else
  58. error_message += offset;
  59. }
  60. else
  61. {
  62. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  63. {
  64. msg[0] = '0';
  65. msg[1] = '\0';
  66. error_message = msg;
  67. }
  68. }
  69. }
  70. }
  71. #endif
  72. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  73. (*(png_ptr->error_fn))(png_ptr, error_message);
  74. /* If the custom handler doesn't exist, or if it returns,
  75. use the default handler, which will not return. */
  76. png_default_error(png_ptr, error_message);
  77. }
  78. #else
  79. PNG_FUNCTION(void,PNGAPI
  80. png_err,(png_structp png_ptr),PNG_NORETURN)
  81. {
  82. /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
  83. * erroneously as '\0', instead of the empty string "". This was
  84. * apparently an error, introduced in libpng-1.2.20, and png_default_error
  85. * will crash in this case.
  86. */
  87. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  88. (*(png_ptr->error_fn))(png_ptr, "");
  89. /* If the custom handler doesn't exist, or if it returns,
  90. use the default handler, which will not return. */
  91. png_default_error(png_ptr, "");
  92. }
  93. #endif /* PNG_ERROR_TEXT_SUPPORTED */
  94. /* Utility to safely appends strings to a buffer. This never errors out so
  95. * error checking is not required in the caller.
  96. */
  97. size_t
  98. png_safecat(png_charp buffer, size_t bufsize, size_t pos,
  99. png_const_charp string)
  100. {
  101. if (buffer != NULL && pos < bufsize)
  102. {
  103. if (string != NULL)
  104. while (*string != '\0' && pos < bufsize-1)
  105. buffer[pos++] = *string++;
  106. buffer[pos] = '\0';
  107. }
  108. return pos;
  109. }
  110. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
  111. /* Utility to dump an unsigned value into a buffer, given a start pointer and
  112. * and end pointer (which should point just *beyond* the end of the buffer!)
  113. * Returns the pointer to the start of the formatted string.
  114. */
  115. png_charp
  116. png_format_number(png_const_charp start, png_charp end, int format,
  117. png_alloc_size_t number)
  118. {
  119. int count = 0; /* number of digits output */
  120. int mincount = 1; /* minimum number required */
  121. int output = 0; /* digit output (for the fixed point format) */
  122. *--end = '\0';
  123. /* This is written so that the loop always runs at least once, even with
  124. * number zero.
  125. */
  126. while (end > start && (number != 0 || count < mincount))
  127. {
  128. static const char digits[] = "0123456789ABCDEF";
  129. switch (format)
  130. {
  131. case PNG_NUMBER_FORMAT_fixed:
  132. /* Needs five digits (the fraction) */
  133. mincount = 5;
  134. if (output || number % 10 != 0)
  135. {
  136. *--end = digits[number % 10];
  137. output = 1;
  138. }
  139. number /= 10;
  140. break;
  141. case PNG_NUMBER_FORMAT_02u:
  142. /* Expects at least 2 digits. */
  143. mincount = 2;
  144. /* fall through */
  145. case PNG_NUMBER_FORMAT_u:
  146. *--end = digits[number % 10];
  147. number /= 10;
  148. break;
  149. case PNG_NUMBER_FORMAT_02x:
  150. /* This format expects at least two digits */
  151. mincount = 2;
  152. /* fall through */
  153. case PNG_NUMBER_FORMAT_x:
  154. *--end = digits[number & 0xf];
  155. number >>= 4;
  156. break;
  157. default: /* an error */
  158. number = 0;
  159. break;
  160. }
  161. /* Keep track of the number of digits added */
  162. ++count;
  163. /* Float a fixed number here: */
  164. if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start)
  165. {
  166. /* End of the fraction, but maybe nothing was output? In that case
  167. * drop the decimal point. If the number is a true zero handle that
  168. * here.
  169. */
  170. if (output)
  171. *--end = '.';
  172. else if (number == 0) /* and !output */
  173. *--end = '0';
  174. }
  175. }
  176. return end;
  177. }
  178. #endif
  179. #ifdef PNG_WARNINGS_SUPPORTED
  180. /* This function is called whenever there is a non-fatal error. This function
  181. * should not be changed. If there is a need to handle warnings differently,
  182. * you should supply a replacement warning function and use
  183. * png_set_error_fn() to replace the warning function at run-time.
  184. */
  185. void PNGAPI
  186. png_warning(png_structp png_ptr, png_const_charp warning_message)
  187. {
  188. int offset = 0;
  189. if (png_ptr != NULL)
  190. {
  191. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  192. if (png_ptr->flags&
  193. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  194. #endif
  195. {
  196. if (*warning_message == PNG_LITERAL_SHARP)
  197. {
  198. for (offset = 1; offset < 15; offset++)
  199. if (warning_message[offset] == ' ')
  200. break;
  201. }
  202. }
  203. }
  204. if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  205. (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
  206. else
  207. png_default_warning(png_ptr, warning_message + offset);
  208. }
  209. /* These functions support 'formatted' warning messages with up to
  210. * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter
  211. * is introduced by @<number>, where 'number' starts at 1. This follows the
  212. * standard established by X/Open for internationalizable error messages.
  213. */
  214. void
  215. png_warning_parameter(png_warning_parameters p, int number,
  216. png_const_charp string)
  217. {
  218. if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
  219. (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
  220. }
  221. void
  222. png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
  223. png_alloc_size_t value)
  224. {
  225. char buffer[PNG_NUMBER_BUFFER_SIZE];
  226. png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
  227. }
  228. void
  229. png_warning_parameter_signed(png_warning_parameters p, int number, int format,
  230. png_int_32 value)
  231. {
  232. png_alloc_size_t u;
  233. png_charp str;
  234. char buffer[PNG_NUMBER_BUFFER_SIZE];
  235. /* Avoid overflow by doing the negate in a png_alloc_size_t: */
  236. u = (png_alloc_size_t)value;
  237. if (value < 0)
  238. u = ~u + 1;
  239. str = PNG_FORMAT_NUMBER(buffer, format, u);
  240. if (value < 0 && str > buffer)
  241. *--str = '-';
  242. png_warning_parameter(p, number, str);
  243. }
  244. void
  245. png_formatted_warning(png_structp png_ptr, png_warning_parameters p,
  246. png_const_charp message)
  247. {
  248. /* The internal buffer is just 192 bytes - enough for all our messages,
  249. * overflow doesn't happen because this code checks! If someone figures
  250. * out how to send us a message longer than 192 bytes, all that will
  251. * happen is that the message will be truncated appropriately.
  252. */
  253. size_t i = 0; /* Index in the msg[] buffer: */
  254. char msg[192];
  255. /* Each iteration through the following loop writes at most one character
  256. * to msg[i++] then returns here to validate that there is still space for
  257. * the trailing '\0'. It may (in the case of a parameter) read more than
  258. * one character from message[]; it must check for '\0' and continue to the
  259. * test if it finds the end of string.
  260. */
  261. while (i<(sizeof msg)-1 && *message != '\0')
  262. {
  263. /* '@' at end of string is now just printed (previously it was skipped);
  264. * it is an error in the calling code to terminate the string with @.
  265. */
  266. if (p != NULL && *message == '@' && message[1] != '\0')
  267. {
  268. int parameter_char = *++message; /* Consume the '@' */
  269. static const char valid_parameters[] = "123456789";
  270. int parameter = 0;
  271. /* Search for the parameter digit, the index in the string is the
  272. * parameter to use.
  273. */
  274. while (valid_parameters[parameter] != parameter_char &&
  275. valid_parameters[parameter] != '\0')
  276. ++parameter;
  277. /* If the parameter digit is out of range it will just get printed. */
  278. if (parameter < PNG_WARNING_PARAMETER_COUNT)
  279. {
  280. /* Append this parameter */
  281. png_const_charp parm = p[parameter];
  282. png_const_charp pend = p[parameter] + (sizeof p[parameter]);
  283. /* No need to copy the trailing '\0' here, but there is no guarantee
  284. * that parm[] has been initialized, so there is no guarantee of a
  285. * trailing '\0':
  286. */
  287. while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
  288. msg[i++] = *parm++;
  289. /* Consume the parameter digit too: */
  290. ++message;
  291. continue;
  292. }
  293. /* else not a parameter and there is a character after the @ sign; just
  294. * copy that. This is known not to be '\0' because of the test above.
  295. */
  296. }
  297. /* At this point *message can't be '\0', even in the bad parameter case
  298. * above where there is a lone '@' at the end of the message string.
  299. */
  300. msg[i++] = *message++;
  301. }
  302. /* i is always less than (sizeof msg), so: */
  303. msg[i] = '\0';
  304. /* And this is the formatted message, it may be larger than
  305. * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these are
  306. * not (currently) formatted.
  307. */
  308. png_warning(png_ptr, msg);
  309. }
  310. #endif /* PNG_WARNINGS_SUPPORTED */
  311. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  312. void PNGAPI
  313. png_benign_error(png_structp png_ptr, png_const_charp error_message)
  314. {
  315. if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  316. png_warning(png_ptr, error_message);
  317. else
  318. png_error(png_ptr, error_message);
  319. }
  320. #endif
  321. /* These utilities are used internally to build an error message that relates
  322. * to the current chunk. The chunk name comes from png_ptr->chunk_name,
  323. * this is used to prefix the message. The message is limited in length
  324. * to 63 bytes, the name characters are output as hex digits wrapped in []
  325. * if the character is invalid.
  326. */
  327. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  328. static PNG_CONST char png_digit[16] = {
  329. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  330. 'A', 'B', 'C', 'D', 'E', 'F'
  331. };
  332. #define PNG_MAX_ERROR_TEXT 64
  333. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
  334. static void /* PRIVATE */
  335. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  336. error_message)
  337. {
  338. png_uint_32 chunk_name = png_ptr->chunk_name;
  339. int iout = 0, ishift = 24;
  340. while (ishift >= 0)
  341. {
  342. int c = (int)(chunk_name >> ishift) & 0xff;
  343. ishift -= 8;
  344. if (isnonalpha(c))
  345. {
  346. buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
  347. buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  348. buffer[iout++] = png_digit[c & 0x0f];
  349. buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
  350. }
  351. else
  352. {
  353. buffer[iout++] = (char)c;
  354. }
  355. }
  356. if (error_message == NULL)
  357. buffer[iout] = '\0';
  358. else
  359. {
  360. int iin = 0;
  361. buffer[iout++] = ':';
  362. buffer[iout++] = ' ';
  363. while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
  364. buffer[iout++] = error_message[iin++];
  365. /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
  366. buffer[iout] = '\0';
  367. }
  368. }
  369. #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
  370. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
  371. PNG_FUNCTION(void,PNGAPI
  372. png_chunk_error,(png_structp png_ptr, png_const_charp error_message),
  373. PNG_NORETURN)
  374. {
  375. char msg[18+PNG_MAX_ERROR_TEXT];
  376. if (png_ptr == NULL)
  377. png_error(png_ptr, error_message);
  378. else
  379. {
  380. png_format_buffer(png_ptr, msg, error_message);
  381. png_error(png_ptr, msg);
  382. }
  383. }
  384. #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
  385. #ifdef PNG_WARNINGS_SUPPORTED
  386. void PNGAPI
  387. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  388. {
  389. char msg[18+PNG_MAX_ERROR_TEXT];
  390. if (png_ptr == NULL)
  391. png_warning(png_ptr, warning_message);
  392. else
  393. {
  394. png_format_buffer(png_ptr, msg, warning_message);
  395. png_warning(png_ptr, msg);
  396. }
  397. }
  398. #endif /* PNG_WARNINGS_SUPPORTED */
  399. #ifdef PNG_READ_SUPPORTED
  400. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  401. void PNGAPI
  402. png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
  403. {
  404. if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  405. png_chunk_warning(png_ptr, error_message);
  406. else
  407. png_chunk_error(png_ptr, error_message);
  408. }
  409. #endif
  410. #endif /* PNG_READ_SUPPORTED */
  411. #ifdef PNG_ERROR_TEXT_SUPPORTED
  412. #ifdef PNG_FLOATING_POINT_SUPPORTED
  413. PNG_FUNCTION(void,
  414. png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN)
  415. {
  416. # define fixed_message "fixed point overflow in "
  417. # define fixed_message_ln ((sizeof fixed_message)-1)
  418. int iin;
  419. char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
  420. png_memcpy(msg, fixed_message, fixed_message_ln);
  421. iin = 0;
  422. if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
  423. {
  424. msg[fixed_message_ln + iin] = name[iin];
  425. ++iin;
  426. }
  427. msg[fixed_message_ln + iin] = 0;
  428. png_error(png_ptr, msg);
  429. }
  430. #endif
  431. #endif
  432. #ifdef PNG_SETJMP_SUPPORTED
  433. /* This API only exists if ANSI-C style error handling is used,
  434. * otherwise it is necessary for png_default_error to be overridden.
  435. */
  436. jmp_buf* PNGAPI
  437. png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
  438. size_t jmp_buf_size)
  439. {
  440. if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
  441. return NULL;
  442. png_ptr->longjmp_fn = longjmp_fn;
  443. return &png_ptr->longjmp_buffer;
  444. }
  445. #endif
  446. /* This is the default error handling function. Note that replacements for
  447. * this function MUST NOT RETURN, or the program will likely crash. This
  448. * function is used by default, or if the program supplies NULL for the
  449. * error function pointer in png_set_error_fn().
  450. */
  451. static PNG_FUNCTION(void /* PRIVATE */,
  452. png_default_error,(png_structp png_ptr, png_const_charp error_message),
  453. PNG_NORETURN)
  454. {
  455. #ifdef PNG_CONSOLE_IO_SUPPORTED
  456. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  457. /* Check on NULL only added in 1.5.4 */
  458. if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
  459. {
  460. /* Strip "#nnnn " from beginning of error message. */
  461. int offset;
  462. char error_number[16];
  463. for (offset = 0; offset<15; offset++)
  464. {
  465. error_number[offset] = error_message[offset + 1];
  466. if (error_message[offset] == ' ')
  467. break;
  468. }
  469. if ((offset > 1) && (offset < 15))
  470. {
  471. error_number[offset - 1] = '\0';
  472. fprintf(stderr, "libpng error no. %s: %s",
  473. error_number, error_message + offset + 1);
  474. fprintf(stderr, PNG_STRING_NEWLINE);
  475. }
  476. else
  477. {
  478. fprintf(stderr, "libpng error: %s, offset=%d",
  479. error_message, offset);
  480. fprintf(stderr, PNG_STRING_NEWLINE);
  481. }
  482. }
  483. else
  484. #endif
  485. {
  486. fprintf(stderr, "libpng error: %s", error_message ? error_message :
  487. "undefined");
  488. fprintf(stderr, PNG_STRING_NEWLINE);
  489. }
  490. #else
  491. PNG_UNUSED(error_message) /* Make compiler happy */
  492. #endif
  493. png_longjmp(png_ptr, 1);
  494. }
  495. PNG_FUNCTION(void,PNGAPI
  496. png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN)
  497. {
  498. #ifdef PNG_SETJMP_SUPPORTED
  499. if (png_ptr && png_ptr->longjmp_fn)
  500. {
  501. # ifdef USE_FAR_KEYWORD
  502. {
  503. jmp_buf tmp_jmpbuf;
  504. png_memcpy(tmp_jmpbuf, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
  505. png_ptr->longjmp_fn(tmp_jmpbuf, val);
  506. }
  507. # else
  508. png_ptr->longjmp_fn(png_ptr->longjmp_buffer, val);
  509. # endif
  510. }
  511. #endif
  512. /* Here if not setjmp support or if png_ptr is null. */
  513. PNG_ABORT();
  514. }
  515. #ifdef PNG_WARNINGS_SUPPORTED
  516. /* This function is called when there is a warning, but the library thinks
  517. * it can continue anyway. Replacement functions don't have to do anything
  518. * here if you don't want them to. In the default configuration, png_ptr is
  519. * not used, but it is passed in case it may be useful.
  520. */
  521. static void /* PRIVATE */
  522. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  523. {
  524. #ifdef PNG_CONSOLE_IO_SUPPORTED
  525. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  526. if (*warning_message == PNG_LITERAL_SHARP)
  527. {
  528. int offset;
  529. char warning_number[16];
  530. for (offset = 0; offset < 15; offset++)
  531. {
  532. warning_number[offset] = warning_message[offset + 1];
  533. if (warning_message[offset] == ' ')
  534. break;
  535. }
  536. if ((offset > 1) && (offset < 15))
  537. {
  538. warning_number[offset + 1] = '\0';
  539. fprintf(stderr, "libpng warning no. %s: %s",
  540. warning_number, warning_message + offset);
  541. fprintf(stderr, PNG_STRING_NEWLINE);
  542. }
  543. else
  544. {
  545. fprintf(stderr, "libpng warning: %s",
  546. warning_message);
  547. fprintf(stderr, PNG_STRING_NEWLINE);
  548. }
  549. }
  550. else
  551. # endif
  552. {
  553. fprintf(stderr, "libpng warning: %s", warning_message);
  554. fprintf(stderr, PNG_STRING_NEWLINE);
  555. }
  556. #else
  557. PNG_UNUSED(warning_message) /* Make compiler happy */
  558. #endif
  559. PNG_UNUSED(png_ptr) /* Make compiler happy */
  560. }
  561. #endif /* PNG_WARNINGS_SUPPORTED */
  562. /* This function is called when the application wants to use another method
  563. * of handling errors and warnings. Note that the error function MUST NOT
  564. * return to the calling routine or serious problems will occur. The return
  565. * method used in the default routine calls longjmp(png_ptr->longjmp_buffer, 1)
  566. */
  567. void PNGAPI
  568. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  569. png_error_ptr error_fn, png_error_ptr warning_fn)
  570. {
  571. if (png_ptr == NULL)
  572. return;
  573. png_ptr->error_ptr = error_ptr;
  574. png_ptr->error_fn = error_fn;
  575. #ifdef PNG_WARNINGS_SUPPORTED
  576. png_ptr->warning_fn = warning_fn;
  577. #else
  578. PNG_UNUSED(warning_fn)
  579. #endif
  580. }
  581. /* This function returns a pointer to the error_ptr associated with the user
  582. * functions. The application should free any memory associated with this
  583. * pointer before png_write_destroy and png_read_destroy are called.
  584. */
  585. png_voidp PNGAPI
  586. png_get_error_ptr(png_const_structp png_ptr)
  587. {
  588. if (png_ptr == NULL)
  589. return NULL;
  590. return ((png_voidp)png_ptr->error_ptr);
  591. }
  592. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  593. void PNGAPI
  594. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  595. {
  596. if (png_ptr != NULL)
  597. {
  598. png_ptr->flags &=
  599. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
  600. PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  601. }
  602. }
  603. #endif
  604. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */