pngerror.c 29 KB

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