pngerror.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Last changed in libpng 1.2.45 [July 7, 2011]
  4. * Copyright (c) 1998-2011 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. #define PNG_INTERNAL
  18. #define PNG_NO_PEDANTIC_WARNINGS
  19. #include "png.h"
  20. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  21. static void /* PRIVATE */
  22. png_default_error PNGARG((png_structp png_ptr,
  23. png_const_charp error_message)) PNG_NORETURN;
  24. #ifdef PNG_WARNINGS_SUPPORTED
  25. static void /* PRIVATE */
  26. png_default_warning PNGARG((png_structp png_ptr,
  27. png_const_charp warning_message));
  28. #endif /* PNG_WARNINGS_SUPPORTED */
  29. /* This function is called whenever there is a fatal error. This function
  30. * should not be changed. If there is a need to handle errors differently,
  31. * you should supply a replacement error function and use png_set_error_fn()
  32. * to replace the error function at run-time.
  33. */
  34. #ifdef PNG_ERROR_TEXT_SUPPORTED
  35. void PNGAPI
  36. png_error(png_structp png_ptr, png_const_charp error_message)
  37. {
  38. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  39. char msg[16];
  40. if (png_ptr != NULL)
  41. {
  42. if (png_ptr->flags&
  43. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  44. {
  45. if (*error_message == PNG_LITERAL_SHARP)
  46. {
  47. /* Strip "#nnnn " from beginning of error message. */
  48. int offset;
  49. for (offset = 1; offset<15; offset++)
  50. if (error_message[offset] == ' ')
  51. break;
  52. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  53. {
  54. int i;
  55. for (i = 0; i < offset - 1; i++)
  56. msg[i] = error_message[i + 1];
  57. msg[i - 1] = '\0';
  58. error_message = msg;
  59. }
  60. else
  61. error_message += offset;
  62. }
  63. else
  64. {
  65. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  66. {
  67. msg[0] = '0';
  68. msg[1] = '\0';
  69. error_message = msg;
  70. }
  71. }
  72. }
  73. }
  74. #endif
  75. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  76. (*(png_ptr->error_fn))(png_ptr, error_message);
  77. /* If the custom handler doesn't exist, or if it returns,
  78. use the default handler, which will not return. */
  79. png_default_error(png_ptr, error_message);
  80. }
  81. #else
  82. void PNGAPI
  83. png_err(png_structp png_ptr)
  84. {
  85. /* Prior to 1.2.45 the error_fn received a NULL pointer, expressed
  86. * erroneously as '\0', instead of the empty string "". This was
  87. * apparently an error, introduced in libpng-1.2.20, and png_default_error
  88. * will crash in this case.
  89. */
  90. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  91. (*(png_ptr->error_fn))(png_ptr, "");
  92. /* If the custom handler doesn't exist, or if it returns,
  93. use the default handler, which will not return. */
  94. png_default_error(png_ptr, "");
  95. }
  96. #endif /* PNG_ERROR_TEXT_SUPPORTED */
  97. #ifdef PNG_WARNINGS_SUPPORTED
  98. /* This function is called whenever there is a non-fatal error. This function
  99. * should not be changed. If there is a need to handle warnings differently,
  100. * you should supply a replacement warning function and use
  101. * png_set_error_fn() to replace the warning function at run-time.
  102. */
  103. void PNGAPI
  104. png_warning(png_structp png_ptr, png_const_charp warning_message)
  105. {
  106. int offset = 0;
  107. if (png_ptr != NULL)
  108. {
  109. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  110. if (png_ptr->flags&
  111. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  112. #endif
  113. {
  114. if (*warning_message == PNG_LITERAL_SHARP)
  115. {
  116. for (offset = 1; offset < 15; offset++)
  117. if (warning_message[offset] == ' ')
  118. break;
  119. }
  120. }
  121. }
  122. if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  123. (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
  124. else
  125. png_default_warning(png_ptr, warning_message + offset);
  126. }
  127. #endif /* PNG_WARNINGS_SUPPORTED */
  128. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  129. void PNGAPI
  130. png_benign_error(png_structp png_ptr, png_const_charp error_message)
  131. {
  132. if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  133. png_warning(png_ptr, error_message);
  134. else
  135. png_error(png_ptr, error_message);
  136. }
  137. #endif
  138. /* These utilities are used internally to build an error message that relates
  139. * to the current chunk. The chunk name comes from png_ptr->chunk_name,
  140. * this is used to prefix the message. The message is limited in length
  141. * to 63 bytes, the name characters are output as hex digits wrapped in []
  142. * if the character is invalid.
  143. */
  144. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  145. static PNG_CONST char png_digit[16] = {
  146. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  147. 'A', 'B', 'C', 'D', 'E', 'F'
  148. };
  149. #define PNG_MAX_ERROR_TEXT 64
  150. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
  151. static void /* PRIVATE */
  152. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  153. error_message)
  154. {
  155. int iout = 0, iin = 0;
  156. while (iin < 4)
  157. {
  158. int c = png_ptr->chunk_name[iin++];
  159. if (isnonalpha(c))
  160. {
  161. buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
  162. buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  163. buffer[iout++] = png_digit[c & 0x0f];
  164. buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
  165. }
  166. else
  167. {
  168. buffer[iout++] = (png_byte)c;
  169. }
  170. }
  171. if (error_message == NULL)
  172. buffer[iout] = '\0';
  173. else
  174. {
  175. buffer[iout++] = ':';
  176. buffer[iout++] = ' ';
  177. iin = 0;
  178. while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
  179. buffer[iout++] = error_message[iin++];
  180. /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
  181. buffer[iout] = '\0';
  182. }
  183. }
  184. #ifdef PNG_READ_SUPPORTED
  185. void PNGAPI
  186. png_chunk_error(png_structp png_ptr, png_const_charp error_message)
  187. {
  188. char msg[18+PNG_MAX_ERROR_TEXT];
  189. if (png_ptr == NULL)
  190. png_error(png_ptr, error_message);
  191. else
  192. {
  193. png_format_buffer(png_ptr, msg, error_message);
  194. png_error(png_ptr, msg);
  195. }
  196. }
  197. #endif /* PNG_READ_SUPPORTED */
  198. #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
  199. #ifdef PNG_WARNINGS_SUPPORTED
  200. void PNGAPI
  201. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  202. {
  203. char msg[18+PNG_MAX_ERROR_TEXT];
  204. if (png_ptr == NULL)
  205. png_warning(png_ptr, warning_message);
  206. else
  207. {
  208. png_format_buffer(png_ptr, msg, warning_message);
  209. png_warning(png_ptr, msg);
  210. }
  211. }
  212. #endif /* PNG_WARNINGS_SUPPORTED */
  213. #ifdef PNG_READ_SUPPORTED
  214. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  215. void PNGAPI
  216. png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
  217. {
  218. if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  219. png_chunk_warning(png_ptr, error_message);
  220. else
  221. png_chunk_error(png_ptr, error_message);
  222. }
  223. #endif
  224. #endif /* PNG_READ_SUPPORTED */
  225. /* This is the default error handling function. Note that replacements for
  226. * this function MUST NOT RETURN, or the program will likely crash. This
  227. * function is used by default, or if the program supplies NULL for the
  228. * error function pointer in png_set_error_fn().
  229. */
  230. static void /* PRIVATE */
  231. png_default_error(png_structp png_ptr, png_const_charp error_message)
  232. {
  233. #ifdef PNG_CONSOLE_IO_SUPPORTED
  234. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  235. if (*error_message == PNG_LITERAL_SHARP)
  236. {
  237. /* Strip "#nnnn " from beginning of error message. */
  238. int offset;
  239. char error_number[16];
  240. for (offset = 0; offset<15; offset++)
  241. {
  242. error_number[offset] = error_message[offset + 1];
  243. if (error_message[offset] == ' ')
  244. break;
  245. }
  246. if ((offset > 1) && (offset < 15))
  247. {
  248. error_number[offset - 1] = '\0';
  249. fprintf(stderr, "libpng error no. %s: %s",
  250. error_number, error_message + offset + 1);
  251. fprintf(stderr, PNG_STRING_NEWLINE);
  252. }
  253. else
  254. {
  255. fprintf(stderr, "libpng error: %s, offset=%d",
  256. error_message, offset);
  257. fprintf(stderr, PNG_STRING_NEWLINE);
  258. }
  259. }
  260. else
  261. #endif
  262. {
  263. fprintf(stderr, "libpng error: %s", error_message);
  264. fprintf(stderr, PNG_STRING_NEWLINE);
  265. }
  266. #endif
  267. #ifdef PNG_SETJMP_SUPPORTED
  268. if (png_ptr)
  269. {
  270. # ifdef USE_FAR_KEYWORD
  271. {
  272. jmp_buf jmpbuf;
  273. png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  274. longjmp(jmpbuf,1);
  275. }
  276. # else
  277. longjmp(png_ptr->jmpbuf, 1);
  278. # endif
  279. }
  280. #endif
  281. /* Here if not setjmp support or if png_ptr is null. */
  282. PNG_ABORT();
  283. #ifndef PNG_CONSOLE_IO_SUPPORTED
  284. error_message = error_message; /* Make compiler happy */
  285. #endif
  286. }
  287. #ifdef PNG_WARNINGS_SUPPORTED
  288. /* This function is called when there is a warning, but the library thinks
  289. * it can continue anyway. Replacement functions don't have to do anything
  290. * here if you don't want them to. In the default configuration, png_ptr is
  291. * not used, but it is passed in case it may be useful.
  292. */
  293. static void /* PRIVATE */
  294. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  295. {
  296. #ifdef PNG_CONSOLE_IO_SUPPORTED
  297. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  298. if (*warning_message == PNG_LITERAL_SHARP)
  299. {
  300. int offset;
  301. char warning_number[16];
  302. for (offset = 0; offset < 15; offset++)
  303. {
  304. warning_number[offset] = warning_message[offset + 1];
  305. if (warning_message[offset] == ' ')
  306. break;
  307. }
  308. if ((offset > 1) && (offset < 15))
  309. {
  310. warning_number[offset + 1] = '\0';
  311. fprintf(stderr, "libpng warning no. %s: %s",
  312. warning_number, warning_message + offset);
  313. fprintf(stderr, PNG_STRING_NEWLINE);
  314. }
  315. else
  316. {
  317. fprintf(stderr, "libpng warning: %s",
  318. warning_message);
  319. fprintf(stderr, PNG_STRING_NEWLINE);
  320. }
  321. }
  322. else
  323. # endif
  324. {
  325. fprintf(stderr, "libpng warning: %s", warning_message);
  326. fprintf(stderr, PNG_STRING_NEWLINE);
  327. }
  328. #else
  329. warning_message = warning_message; /* Make compiler happy */
  330. #endif
  331. png_ptr = png_ptr; /* Make compiler happy */
  332. }
  333. #endif /* PNG_WARNINGS_SUPPORTED */
  334. /* This function is called when the application wants to use another method
  335. * of handling errors and warnings. Note that the error function MUST NOT
  336. * return to the calling routine or serious problems will occur. The return
  337. * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  338. */
  339. void PNGAPI
  340. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  341. png_error_ptr error_fn, png_error_ptr warning_fn)
  342. {
  343. if (png_ptr == NULL)
  344. return;
  345. png_ptr->error_ptr = error_ptr;
  346. png_ptr->error_fn = error_fn;
  347. png_ptr->warning_fn = warning_fn;
  348. }
  349. /* This function returns a pointer to the error_ptr associated with the user
  350. * functions. The application should free any memory associated with this
  351. * pointer before png_write_destroy and png_read_destroy are called.
  352. */
  353. png_voidp PNGAPI
  354. png_get_error_ptr(png_structp png_ptr)
  355. {
  356. if (png_ptr == NULL)
  357. return NULL;
  358. return ((png_voidp)png_ptr->error_ptr);
  359. }
  360. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  361. void PNGAPI
  362. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  363. {
  364. if (png_ptr != NULL)
  365. {
  366. png_ptr->flags &=
  367. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  368. }
  369. }
  370. #endif
  371. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */