The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
9.9KB

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