pngerror.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Last changed in libpng 1.6.10 [March 6, 2014]
  4. * Copyright (c) 1998-2014 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. # ifndef PNG_ERROR_TEXT_SUPPORTED
  339. PNG_UNUSED(error_message)
  340. # endif
  341. }
  342. void /* PRIVATE */
  343. png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
  344. {
  345. if (png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN)
  346. png_warning(png_ptr, error_message);
  347. else
  348. png_error(png_ptr, error_message);
  349. # ifndef PNG_ERROR_TEXT_SUPPORTED
  350. PNG_UNUSED(error_message)
  351. # endif
  352. }
  353. void /* PRIVATE */
  354. png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
  355. {
  356. if (png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN)
  357. png_warning(png_ptr, error_message);
  358. else
  359. png_error(png_ptr, error_message);
  360. # ifndef PNG_ERROR_TEXT_SUPPORTED
  361. PNG_UNUSED(error_message)
  362. # endif
  363. }
  364. #endif /* BENIGN_ERRORS */
  365. /* These utilities are used internally to build an error message that relates
  366. * to the current chunk. The chunk name comes from png_ptr->chunk_name,
  367. * this is used to prefix the message. The message is limited in length
  368. * to 63 bytes, the name characters are output as hex digits wrapped in []
  369. * if the character is invalid.
  370. */
  371. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  372. static PNG_CONST char png_digit[16] = {
  373. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  374. 'A', 'B', 'C', 'D', 'E', 'F'
  375. };
  376. #define PNG_MAX_ERROR_TEXT 196 /* Currently limited be profile_error in png.c */
  377. #if defined(PNG_WARNINGS_SUPPORTED) || \
  378. (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
  379. static void /* PRIVATE */
  380. png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
  381. error_message)
  382. {
  383. png_uint_32 chunk_name = png_ptr->chunk_name;
  384. int iout = 0, ishift = 24;
  385. while (ishift >= 0)
  386. {
  387. int c = (int)(chunk_name >> ishift) & 0xff;
  388. ishift -= 8;
  389. if (isnonalpha(c))
  390. {
  391. buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
  392. buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  393. buffer[iout++] = png_digit[c & 0x0f];
  394. buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
  395. }
  396. else
  397. {
  398. buffer[iout++] = (char)c;
  399. }
  400. }
  401. if (error_message == NULL)
  402. buffer[iout] = '\0';
  403. else
  404. {
  405. int iin = 0;
  406. buffer[iout++] = ':';
  407. buffer[iout++] = ' ';
  408. while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
  409. buffer[iout++] = error_message[iin++];
  410. /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
  411. buffer[iout] = '\0';
  412. }
  413. }
  414. #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
  415. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
  416. PNG_FUNCTION(void,PNGAPI
  417. png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
  418. PNG_NORETURN)
  419. {
  420. char msg[18+PNG_MAX_ERROR_TEXT];
  421. if (png_ptr == NULL)
  422. png_error(png_ptr, error_message);
  423. else
  424. {
  425. png_format_buffer(png_ptr, msg, error_message);
  426. png_error(png_ptr, msg);
  427. }
  428. }
  429. #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
  430. #ifdef PNG_WARNINGS_SUPPORTED
  431. void PNGAPI
  432. png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
  433. {
  434. char msg[18+PNG_MAX_ERROR_TEXT];
  435. if (png_ptr == NULL)
  436. png_warning(png_ptr, warning_message);
  437. else
  438. {
  439. png_format_buffer(png_ptr, msg, warning_message);
  440. png_warning(png_ptr, msg);
  441. }
  442. }
  443. #endif /* PNG_WARNINGS_SUPPORTED */
  444. #ifdef PNG_READ_SUPPORTED
  445. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  446. void PNGAPI
  447. png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp
  448. error_message)
  449. {
  450. if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  451. png_chunk_warning(png_ptr, error_message);
  452. else
  453. png_chunk_error(png_ptr, error_message);
  454. # ifndef PNG_ERROR_TEXT_SUPPORTED
  455. PNG_UNUSED(error_message)
  456. # endif
  457. }
  458. #endif
  459. #endif /* PNG_READ_SUPPORTED */
  460. void /* PRIVATE */
  461. png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
  462. {
  463. # ifndef PNG_WARNINGS_SUPPORTED
  464. PNG_UNUSED(message)
  465. # endif
  466. /* This is always supported, but for just read or just write it
  467. * unconditionally does the right thing.
  468. */
  469. # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
  470. if (png_ptr->mode & PNG_IS_READ_STRUCT)
  471. # endif
  472. # ifdef PNG_READ_SUPPORTED
  473. {
  474. if (error < PNG_CHUNK_ERROR)
  475. png_chunk_warning(png_ptr, message);
  476. else
  477. png_chunk_benign_error(png_ptr, message);
  478. }
  479. # endif
  480. # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
  481. else if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
  482. # endif
  483. # ifdef PNG_WRITE_SUPPORTED
  484. {
  485. if (error < PNG_CHUNK_WRITE_ERROR)
  486. png_app_warning(png_ptr, message);
  487. else
  488. png_app_error(png_ptr, message);
  489. }
  490. # endif
  491. }
  492. #ifdef PNG_ERROR_TEXT_SUPPORTED
  493. #ifdef PNG_FLOATING_POINT_SUPPORTED
  494. PNG_FUNCTION(void,
  495. png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
  496. {
  497. # define fixed_message "fixed point overflow in "
  498. # define fixed_message_ln ((sizeof fixed_message)-1)
  499. int iin;
  500. char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
  501. memcpy(msg, fixed_message, fixed_message_ln);
  502. iin = 0;
  503. if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
  504. {
  505. msg[fixed_message_ln + iin] = name[iin];
  506. ++iin;
  507. }
  508. msg[fixed_message_ln + iin] = 0;
  509. png_error(png_ptr, msg);
  510. }
  511. #endif
  512. #endif
  513. #ifdef PNG_SETJMP_SUPPORTED
  514. /* This API only exists if ANSI-C style error handling is used,
  515. * otherwise it is necessary for png_default_error to be overridden.
  516. */
  517. jmp_buf* PNGAPI
  518. png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
  519. size_t jmp_buf_size)
  520. {
  521. /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
  522. * and it must not change after that. Libpng doesn't care how big the
  523. * buffer is, just that it doesn't change.
  524. *
  525. * If the buffer size is no *larger* than the size of jmp_buf when libpng is
  526. * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
  527. * semantics that this call will not fail. If the size is larger, however,
  528. * the buffer is allocated and this may fail, causing the function to return
  529. * NULL.
  530. */
  531. if (png_ptr == NULL)
  532. return NULL;
  533. if (png_ptr->jmp_buf_ptr == NULL)
  534. {
  535. png_ptr->jmp_buf_size = 0; /* not allocated */
  536. if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
  537. png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
  538. else
  539. {
  540. png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
  541. png_malloc_warn(png_ptr, jmp_buf_size));
  542. if (png_ptr->jmp_buf_ptr == NULL)
  543. return NULL; /* new NULL return on OOM */
  544. png_ptr->jmp_buf_size = jmp_buf_size;
  545. }
  546. }
  547. else /* Already allocated: check the size */
  548. {
  549. size_t size = png_ptr->jmp_buf_size;
  550. if (size == 0)
  551. {
  552. size = (sizeof png_ptr->jmp_buf_local);
  553. if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
  554. {
  555. /* This is an internal error in libpng: somehow we have been left
  556. * with a stack allocated jmp_buf when the application regained
  557. * control. It's always possible to fix this up, but for the moment
  558. * this is a png_error because that makes it easy to detect.
  559. */
  560. png_error(png_ptr, "Libpng jmp_buf still allocated");
  561. /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
  562. }
  563. }
  564. if (size != jmp_buf_size)
  565. {
  566. png_warning(png_ptr, "Application jmp_buf size changed");
  567. return NULL; /* caller will probably crash: no choice here */
  568. }
  569. }
  570. /* Finally fill in the function, now we have a satisfactory buffer. It is
  571. * valid to change the function on every call.
  572. */
  573. png_ptr->longjmp_fn = longjmp_fn;
  574. return png_ptr->jmp_buf_ptr;
  575. }
  576. void /* PRIVATE */
  577. png_free_jmpbuf(png_structrp png_ptr)
  578. {
  579. if (png_ptr != NULL)
  580. {
  581. jmp_buf *jb = png_ptr->jmp_buf_ptr;
  582. /* A size of 0 is used to indicate a local, stack, allocation of the
  583. * pointer; used here and in png.c
  584. */
  585. if (jb != NULL && png_ptr->jmp_buf_size > 0)
  586. {
  587. /* This stuff is so that a failure to free the error control structure
  588. * does not leave libpng in a state with no valid error handling: the
  589. * free always succeeds, if there is an error it gets ignored.
  590. */
  591. if (jb != &png_ptr->jmp_buf_local)
  592. {
  593. /* Make an internal, libpng, jmp_buf to return here */
  594. jmp_buf free_jmp_buf;
  595. if (!setjmp(free_jmp_buf))
  596. {
  597. png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
  598. png_ptr->jmp_buf_size = 0; /* stack allocation */
  599. png_ptr->longjmp_fn = longjmp;
  600. png_free(png_ptr, jb); /* Return to setjmp on error */
  601. }
  602. }
  603. }
  604. /* *Always* cancel everything out: */
  605. png_ptr->jmp_buf_size = 0;
  606. png_ptr->jmp_buf_ptr = NULL;
  607. png_ptr->longjmp_fn = 0;
  608. }
  609. }
  610. #endif
  611. /* This is the default error handling function. Note that replacements for
  612. * this function MUST NOT RETURN, or the program will likely crash. This
  613. * function is used by default, or if the program supplies NULL for the
  614. * error function pointer in png_set_error_fn().
  615. */
  616. static PNG_FUNCTION(void /* PRIVATE */,
  617. png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
  618. PNG_NORETURN)
  619. {
  620. #ifdef PNG_CONSOLE_IO_SUPPORTED
  621. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  622. /* Check on NULL only added in 1.5.4 */
  623. if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
  624. {
  625. /* Strip "#nnnn " from beginning of error message. */
  626. int offset;
  627. char error_number[16];
  628. for (offset = 0; offset<15; offset++)
  629. {
  630. error_number[offset] = error_message[offset + 1];
  631. if (error_message[offset] == ' ')
  632. break;
  633. }
  634. if ((offset > 1) && (offset < 15))
  635. {
  636. error_number[offset - 1] = '\0';
  637. fprintf(stderr, "libpng error no. %s: %s",
  638. error_number, error_message + offset + 1);
  639. fprintf(stderr, PNG_STRING_NEWLINE);
  640. }
  641. else
  642. {
  643. fprintf(stderr, "libpng error: %s, offset=%d",
  644. error_message, offset);
  645. fprintf(stderr, PNG_STRING_NEWLINE);
  646. }
  647. }
  648. else
  649. #endif
  650. {
  651. fprintf(stderr, "libpng error: %s", error_message ? error_message :
  652. "undefined");
  653. fprintf(stderr, PNG_STRING_NEWLINE);
  654. }
  655. #else
  656. PNG_UNUSED(error_message) /* Make compiler happy */
  657. #endif
  658. png_longjmp(png_ptr, 1);
  659. }
  660. PNG_FUNCTION(void,PNGAPI
  661. png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
  662. {
  663. #ifdef PNG_SETJMP_SUPPORTED
  664. if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr)
  665. png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
  666. #endif
  667. /* If control reaches this point, png_longjmp() must not return. The only
  668. * choice is to terminate the whole process (or maybe the thread); to do
  669. * this the ANSI-C abort() function is used unless a different method is
  670. * implemented by overriding the default configuration setting for
  671. * PNG_ABORT().
  672. */
  673. PNG_ABORT();
  674. }
  675. #ifdef PNG_WARNINGS_SUPPORTED
  676. /* This function is called when there is a warning, but the library thinks
  677. * it can continue anyway. Replacement functions don't have to do anything
  678. * here if you don't want them to. In the default configuration, png_ptr is
  679. * not used, but it is passed in case it may be useful.
  680. */
  681. static void /* PRIVATE */
  682. png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
  683. {
  684. #ifdef PNG_CONSOLE_IO_SUPPORTED
  685. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  686. if (*warning_message == PNG_LITERAL_SHARP)
  687. {
  688. int offset;
  689. char warning_number[16];
  690. for (offset = 0; offset < 15; offset++)
  691. {
  692. warning_number[offset] = warning_message[offset + 1];
  693. if (warning_message[offset] == ' ')
  694. break;
  695. }
  696. if ((offset > 1) && (offset < 15))
  697. {
  698. warning_number[offset + 1] = '\0';
  699. fprintf(stderr, "libpng warning no. %s: %s",
  700. warning_number, warning_message + offset);
  701. fprintf(stderr, PNG_STRING_NEWLINE);
  702. }
  703. else
  704. {
  705. fprintf(stderr, "libpng warning: %s",
  706. warning_message);
  707. fprintf(stderr, PNG_STRING_NEWLINE);
  708. }
  709. }
  710. else
  711. # endif
  712. {
  713. fprintf(stderr, "libpng warning: %s", warning_message);
  714. fprintf(stderr, PNG_STRING_NEWLINE);
  715. }
  716. #else
  717. PNG_UNUSED(warning_message) /* Make compiler happy */
  718. #endif
  719. PNG_UNUSED(png_ptr) /* Make compiler happy */
  720. }
  721. #endif /* PNG_WARNINGS_SUPPORTED */
  722. /* This function is called when the application wants to use another method
  723. * of handling errors and warnings. Note that the error function MUST NOT
  724. * return to the calling routine or serious problems will occur. The return
  725. * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
  726. */
  727. void PNGAPI
  728. png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
  729. png_error_ptr error_fn, png_error_ptr warning_fn)
  730. {
  731. if (png_ptr == NULL)
  732. return;
  733. png_ptr->error_ptr = error_ptr;
  734. png_ptr->error_fn = error_fn;
  735. #ifdef PNG_WARNINGS_SUPPORTED
  736. png_ptr->warning_fn = warning_fn;
  737. #else
  738. PNG_UNUSED(warning_fn)
  739. #endif
  740. }
  741. /* This function returns a pointer to the error_ptr associated with the user
  742. * functions. The application should free any memory associated with this
  743. * pointer before png_write_destroy and png_read_destroy are called.
  744. */
  745. png_voidp PNGAPI
  746. png_get_error_ptr(png_const_structrp png_ptr)
  747. {
  748. if (png_ptr == NULL)
  749. return NULL;
  750. return ((png_voidp)png_ptr->error_ptr);
  751. }
  752. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  753. void PNGAPI
  754. png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
  755. {
  756. if (png_ptr != NULL)
  757. {
  758. png_ptr->flags &=
  759. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
  760. PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  761. }
  762. }
  763. #endif
  764. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
  765. defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
  766. /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
  767. * possible to implement without setjmp support just so long as there is some
  768. * way to handle the error return here:
  769. */
  770. PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
  771. png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
  772. PNG_NORETURN)
  773. {
  774. const png_const_structrp png_ptr = png_nonconst_ptr;
  775. png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
  776. /* An error is always logged here, overwriting anything (typically a warning)
  777. * that is already there:
  778. */
  779. if (image != NULL)
  780. {
  781. png_safecat(image->message, (sizeof image->message), 0, error_message);
  782. image->warning_or_error |= PNG_IMAGE_ERROR;
  783. /* Retrieve the jmp_buf from within the png_control, making this work for
  784. * C++ compilation too is pretty tricky: C++ wants a pointer to the first
  785. * element of a jmp_buf, but C doesn't tell us the type of that.
  786. */
  787. if (image->opaque != NULL && image->opaque->error_buf != NULL)
  788. longjmp(png_control_jmp_buf(image->opaque), 1);
  789. /* Missing longjmp buffer, the following is to help debugging: */
  790. {
  791. size_t pos = png_safecat(image->message, (sizeof image->message), 0,
  792. "bad longjmp: ");
  793. png_safecat(image->message, (sizeof image->message), pos,
  794. error_message);
  795. }
  796. }
  797. /* Here on an internal programming error. */
  798. abort();
  799. }
  800. #ifdef PNG_WARNINGS_SUPPORTED
  801. void /* PRIVATE */ PNGCBAPI
  802. png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
  803. {
  804. const png_const_structrp png_ptr = png_nonconst_ptr;
  805. png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
  806. /* A warning is only logged if there is no prior warning or error. */
  807. if (image->warning_or_error == 0)
  808. {
  809. png_safecat(image->message, (sizeof image->message), 0, warning_message);
  810. image->warning_or_error |= PNG_IMAGE_WARNING;
  811. }
  812. }
  813. #endif
  814. int /* PRIVATE */
  815. png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
  816. {
  817. volatile png_imagep image = image_in;
  818. volatile int result;
  819. volatile png_voidp saved_error_buf;
  820. jmp_buf safe_jmpbuf;
  821. /* Safely execute function(arg) with png_error returning to this function. */
  822. saved_error_buf = image->opaque->error_buf;
  823. result = setjmp(safe_jmpbuf) == 0;
  824. if (result)
  825. {
  826. image->opaque->error_buf = safe_jmpbuf;
  827. result = function(arg);
  828. }
  829. image->opaque->error_buf = saved_error_buf;
  830. /* And do the cleanup prior to any failure return. */
  831. if (!result)
  832. png_image_free(image);
  833. return result;
  834. }
  835. #endif /* SIMPLIFIED READ/WRITE */
  836. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */