2
0

pngerror.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Last changed in libpng 1.2.9 April 14, 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. 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. png_format_buffer(png_ptr, msg, error_message);
  150. png_error(png_ptr, msg);
  151. }
  152. void PNGAPI
  153. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  154. {
  155. char msg[18+64];
  156. if (png_ptr == NULL)
  157. png_warning(png_ptr, warning_message);
  158. png_format_buffer(png_ptr, msg, warning_message);
  159. png_warning(png_ptr, msg);
  160. }
  161. /* This is the default error handling function. Note that replacements for
  162. * this function MUST NOT RETURN, or the program will likely crash. This
  163. * function is used by default, or if the program supplies NULL for the
  164. * error function pointer in png_set_error_fn().
  165. */
  166. static void /* PRIVATE */
  167. png_default_error(png_structp png_ptr, png_const_charp error_message)
  168. {
  169. #ifndef PNG_NO_CONSOLE_IO
  170. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  171. if (*error_message == '#')
  172. {
  173. int offset;
  174. char error_number[16];
  175. for (offset=0; offset<15; offset++)
  176. {
  177. error_number[offset] = *(error_message+offset+1);
  178. if (*(error_message+offset) == ' ')
  179. break;
  180. }
  181. if((offset > 1) && (offset < 15))
  182. {
  183. error_number[offset-1]='\0';
  184. fprintf(stderr, "libpng error no. %s: %s\n", error_number,
  185. error_message+offset);
  186. }
  187. else
  188. fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
  189. }
  190. else
  191. #endif
  192. fprintf(stderr, "libpng error: %s\n", error_message);
  193. #endif
  194. #ifdef PNG_SETJMP_SUPPORTED
  195. # ifdef USE_FAR_KEYWORD
  196. {
  197. jmp_buf jmpbuf;
  198. png_memcpy(jmpbuf,png_ptr->jmpbuf,png_sizeof(jmp_buf));
  199. longjmp(jmpbuf, 1);
  200. }
  201. # else
  202. longjmp(png_ptr->jmpbuf, 1);
  203. # endif
  204. #else
  205. /* make compiler happy */ ;
  206. if (png_ptr)
  207. PNG_ABORT();
  208. #endif
  209. #ifdef PNG_NO_CONSOLE_IO
  210. /* make compiler happy */ ;
  211. if (&error_message != NULL)
  212. return;
  213. #endif
  214. }
  215. /* This function is called when there is a warning, but the library thinks
  216. * it can continue anyway. Replacement functions don't have to do anything
  217. * here if you don't want them to. In the default configuration, png_ptr is
  218. * not used, but it is passed in case it may be useful.
  219. */
  220. static void /* PRIVATE */
  221. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  222. {
  223. #ifndef PNG_NO_CONSOLE_IO
  224. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  225. if (*warning_message == '#')
  226. {
  227. int offset;
  228. char warning_number[16];
  229. for (offset=0; offset<15; offset++)
  230. {
  231. warning_number[offset]=*(warning_message+offset+1);
  232. if (*(warning_message+offset) == ' ')
  233. break;
  234. }
  235. if((offset > 1) && (offset < 15))
  236. {
  237. warning_number[offset-1]='\0';
  238. fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
  239. warning_message+offset);
  240. }
  241. else
  242. fprintf(stderr, "libpng warning: %s\n", warning_message);
  243. }
  244. else
  245. # endif
  246. fprintf(stderr, "libpng warning: %s\n", warning_message);
  247. #else
  248. /* make compiler happy */ ;
  249. if (warning_message)
  250. return;
  251. #endif
  252. /* make compiler happy */ ;
  253. if (png_ptr)
  254. return;
  255. }
  256. /* This function is called when the application wants to use another method
  257. * of handling errors and warnings. Note that the error function MUST NOT
  258. * return to the calling routine or serious problems will occur. The return
  259. * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  260. */
  261. void PNGAPI
  262. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  263. png_error_ptr error_fn, png_error_ptr warning_fn)
  264. {
  265. if (png_ptr == NULL)
  266. return;
  267. png_ptr->error_ptr = error_ptr;
  268. png_ptr->error_fn = error_fn;
  269. png_ptr->warning_fn = warning_fn;
  270. }
  271. /* This function returns a pointer to the error_ptr associated with the user
  272. * functions. The application should free any memory associated with this
  273. * pointer before png_write_destroy and png_read_destroy are called.
  274. */
  275. png_voidp PNGAPI
  276. png_get_error_ptr(png_structp png_ptr)
  277. {
  278. if (png_ptr == NULL)
  279. return NULL;
  280. return ((png_voidp)png_ptr->error_ptr);
  281. }
  282. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  283. void PNGAPI
  284. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  285. {
  286. if(png_ptr != NULL)
  287. {
  288. png_ptr->flags &=
  289. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  290. }
  291. }
  292. #endif
  293. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */