pngerror.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Last changed in libpng 1.2.13 November 13, 2006
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2006 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. *
  9. * This file provides a location for all error handling. Users who
  10. * need special error handling are expected to write replacement functions
  11. * and use png_set_error_fn() to use those functions. See the instructions
  12. * at each function.
  13. */
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  17. static void /* PRIVATE */
  18. png_default_error PNGARG((png_structp png_ptr,
  19. png_const_charp error_message));
  20. static void /* PRIVATE */
  21. png_default_warning PNGARG((png_structp png_ptr,
  22. png_const_charp warning_message));
  23. /* This function is called whenever there is a fatal error. This function
  24. * should not be changed. If there is a need to handle errors differently,
  25. * you should supply a replacement error function and use png_set_error_fn()
  26. * to replace the error function at run-time.
  27. */
  28. void PNGAPI
  29. png_error(png_structp png_ptr, png_const_charp error_message)
  30. {
  31. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  32. char msg[16];
  33. if (png_ptr != NULL)
  34. {
  35. if (png_ptr->flags&
  36. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  37. {
  38. if (*error_message == '#')
  39. {
  40. int offset;
  41. for (offset=1; offset<15; offset++)
  42. if (*(error_message+offset) == ' ')
  43. break;
  44. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  45. {
  46. int i;
  47. for (i=0; i<offset-1; i++)
  48. msg[i]=error_message[i+1];
  49. msg[i]='\0';
  50. error_message=msg;
  51. }
  52. else
  53. error_message+=offset;
  54. }
  55. else
  56. {
  57. if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  58. {
  59. msg[0]='0';
  60. msg[1]='\0';
  61. error_message=msg;
  62. }
  63. }
  64. }
  65. }
  66. #endif
  67. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  68. (*(png_ptr->error_fn))(png_ptr, error_message);
  69. /* If the custom handler doesn't exist, or if it returns,
  70. use the default handler, which will not return. */
  71. png_default_error(png_ptr, error_message);
  72. }
  73. /* This function is called whenever there is a non-fatal error. This function
  74. * should not be changed. If there is a need to handle warnings differently,
  75. * you should supply a replacement warning function and use
  76. * png_set_error_fn() to replace the warning function at run-time.
  77. */
  78. void PNGAPI
  79. png_warning(png_structp png_ptr, png_const_charp warning_message)
  80. {
  81. int offset = 0;
  82. if (png_ptr != NULL)
  83. {
  84. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  85. if (png_ptr->flags&
  86. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  87. #endif
  88. {
  89. if (*warning_message == '#')
  90. {
  91. for (offset=1; offset<15; offset++)
  92. if (*(warning_message+offset) == ' ')
  93. break;
  94. }
  95. }
  96. if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  97. (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
  98. }
  99. else
  100. png_default_warning(png_ptr, warning_message+offset);
  101. }
  102. /* These utilities are used internally to build an error message that relates
  103. * to the current chunk. The chunk name comes from png_ptr->chunk_name,
  104. * this is used to prefix the message. The message is limited in length
  105. * to 63 bytes, the name characters are output as hex digits wrapped in []
  106. * if the character is invalid.
  107. */
  108. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  109. const static PNG_CONST char png_digit[16] = {
  110. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  111. 'A', 'B', 'C', 'D', 'E', 'F'
  112. };
  113. static void /* PRIVATE */
  114. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  115. error_message)
  116. {
  117. int iout = 0, iin = 0;
  118. while (iin < 4)
  119. {
  120. int c = png_ptr->chunk_name[iin++];
  121. if (isnonalpha(c))
  122. {
  123. buffer[iout++] = '[';
  124. buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  125. buffer[iout++] = png_digit[c & 0x0f];
  126. buffer[iout++] = ']';
  127. }
  128. else
  129. {
  130. buffer[iout++] = (png_byte)c;
  131. }
  132. }
  133. if (error_message == NULL)
  134. buffer[iout] = 0;
  135. else
  136. {
  137. buffer[iout++] = ':';
  138. buffer[iout++] = ' ';
  139. png_strncpy(buffer+iout, error_message, 63);
  140. buffer[iout+63] = 0;
  141. }
  142. }
  143. void PNGAPI
  144. png_chunk_error(png_structp png_ptr, png_const_charp error_message)
  145. {
  146. char msg[18+64];
  147. if (png_ptr == NULL)
  148. png_error(png_ptr, error_message);
  149. else
  150. {
  151. png_format_buffer(png_ptr, msg, error_message);
  152. png_error(png_ptr, msg);
  153. }
  154. }
  155. void PNGAPI
  156. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  157. {
  158. char msg[18+64];
  159. if (png_ptr == NULL)
  160. png_warning(png_ptr, warning_message);
  161. else
  162. {
  163. png_format_buffer(png_ptr, msg, warning_message);
  164. png_warning(png_ptr, msg);
  165. }
  166. }
  167. /* This is the default error handling function. Note that replacements for
  168. * this function MUST NOT RETURN, or the program will likely crash. This
  169. * function is used by default, or if the program supplies NULL for the
  170. * error function pointer in png_set_error_fn().
  171. */
  172. static void /* PRIVATE */
  173. png_default_error(png_structp png_ptr, png_const_charp error_message)
  174. {
  175. #ifndef PNG_NO_CONSOLE_IO
  176. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  177. if (*error_message == '#')
  178. {
  179. int offset;
  180. char error_number[16];
  181. for (offset=0; offset<15; offset++)
  182. {
  183. error_number[offset] = *(error_message+offset+1);
  184. if (*(error_message+offset) == ' ')
  185. break;
  186. }
  187. if((offset > 1) && (offset < 15))
  188. {
  189. error_number[offset-1]='\0';
  190. fprintf(stderr, "libpng error no. %s: %s\n", error_number,
  191. error_message+offset);
  192. }
  193. else
  194. fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
  195. }
  196. else
  197. #endif
  198. fprintf(stderr, "libpng error: %s\n", error_message);
  199. #endif
  200. #ifdef PNG_SETJMP_SUPPORTED
  201. if (png_ptr)
  202. {
  203. # ifdef USE_FAR_KEYWORD
  204. {
  205. jmp_buf jmpbuf;
  206. png_memcpy(jmpbuf,png_ptr->jmpbuf,png_sizeof(jmp_buf));
  207. longjmp(jmpbuf, 1);
  208. }
  209. # else
  210. longjmp(png_ptr->jmpbuf, 1);
  211. # endif
  212. }
  213. #else
  214. PNG_ABORT();
  215. #endif
  216. #ifdef PNG_NO_CONSOLE_IO
  217. /* make compiler happy */ ;
  218. if (&error_message != NULL)
  219. return;
  220. #endif
  221. }
  222. /* This function is called when there is a warning, but the library thinks
  223. * it can continue anyway. Replacement functions don't have to do anything
  224. * here if you don't want them to. In the default configuration, png_ptr is
  225. * not used, but it is passed in case it may be useful.
  226. */
  227. static void /* PRIVATE */
  228. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  229. {
  230. #ifndef PNG_NO_CONSOLE_IO
  231. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  232. if (*warning_message == '#')
  233. {
  234. int offset;
  235. char warning_number[16];
  236. for (offset=0; offset<15; offset++)
  237. {
  238. warning_number[offset]=*(warning_message+offset+1);
  239. if (*(warning_message+offset) == ' ')
  240. break;
  241. }
  242. if((offset > 1) && (offset < 15))
  243. {
  244. warning_number[offset-1]='\0';
  245. fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
  246. warning_message+offset);
  247. }
  248. else
  249. fprintf(stderr, "libpng warning: %s\n", warning_message);
  250. }
  251. else
  252. # endif
  253. fprintf(stderr, "libpng warning: %s\n", warning_message);
  254. #else
  255. /* make compiler happy */ ;
  256. if (warning_message)
  257. return;
  258. #endif
  259. /* make compiler happy */ ;
  260. if (png_ptr)
  261. return;
  262. }
  263. /* This function is called when the application wants to use another method
  264. * of handling errors and warnings. Note that the error function MUST NOT
  265. * return to the calling routine or serious problems will occur. The return
  266. * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  267. */
  268. void PNGAPI
  269. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  270. png_error_ptr error_fn, png_error_ptr warning_fn)
  271. {
  272. if (png_ptr == NULL)
  273. return;
  274. png_ptr->error_ptr = error_ptr;
  275. png_ptr->error_fn = error_fn;
  276. png_ptr->warning_fn = warning_fn;
  277. }
  278. /* This function returns a pointer to the error_ptr associated with the user
  279. * functions. The application should free any memory associated with this
  280. * pointer before png_write_destroy and png_read_destroy are called.
  281. */
  282. png_voidp PNGAPI
  283. png_get_error_ptr(png_structp png_ptr)
  284. {
  285. if (png_ptr == NULL)
  286. return NULL;
  287. return ((png_voidp)png_ptr->error_ptr);
  288. }
  289. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  290. void PNGAPI
  291. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  292. {
  293. if(png_ptr != NULL)
  294. {
  295. png_ptr->flags &=
  296. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  297. }
  298. }
  299. #endif
  300. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */