/src/compiler/android/jni/ftk/pngerror.c

http://ftk.googlecode.com/ · C · 386 lines · 305 code · 23 blank · 58 comment · 67 complexity · a19f892426a95598a206e5a20b68034f MD5 · raw file

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