Counter Strike : Global Offensive Source Code
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.

447 lines
13 KiB

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