libmng_error.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* ************************************************************************** */
  2. /* * For conditions of distribution and use, * */
  3. /* * see copyright notice in libmng.h * */
  4. /* ************************************************************************** */
  5. /* * * */
  6. /* * project : libmng * */
  7. /* * file : libmng_error.c copyright (c) 2000-2007 G.Juyn * */
  8. /* * version : 1.0.10 * */
  9. /* * * */
  10. /* * purpose : Error routines (implementation) * */
  11. /* * * */
  12. /* * author : G.Juyn * */
  13. /* * * */
  14. /* * comment : implementation of the general error handling routines * */
  15. /* * * */
  16. /* * changes : 0.5.1 - 05/08/2000 - G.Juyn * */
  17. /* * - changed strict-ANSI stuff * */
  18. /* * * */
  19. /* * 0.5.2 - 05/23/2000 - G.Juyn * */
  20. /* * - added error telltaling * */
  21. /* * 0.5.2 - 05/30/2000 - G.Juyn * */
  22. /* * - added errorstrings for delta-image processing * */
  23. /* * 0.5.2 - 05/31/2000 - G.Juyn * */
  24. /* * - fixed up punctuation (contributed by Tim Rowley) * */
  25. /* * 0.5.2 - 06/06/2000 - G.Juyn * */
  26. /* * - added errorstring for delayed buffer-processing * */
  27. /* * * */
  28. /* * 0.9.1 - 07/06/2000 - G.Juyn * */
  29. /* * - added MNG_NEEDTIMERWAIT errorstring * */
  30. /* * 0.9.1 - 07/15/2000 - G.Juyn * */
  31. /* * - added NEEDSECTIONWAIT errorstring * */
  32. /* * - added macro + routine to set returncode without * */
  33. /* * calling error callback * */
  34. /* * 0.9.1 - 07/19/2000 - G.Juyn * */
  35. /* * - added errorstring for updatemngheader if not a MNG * */
  36. /* * * */
  37. /* * 0.9.2 - 08/05/2000 - G.Juyn * */
  38. /* * - changed file-prefixes * */
  39. /* * * */
  40. /* * 0.9.3 - 08/09/2000 - G.Juyn * */
  41. /* * - added check for simplicity-bits in MHDR * */
  42. /* * 0.9.3 - 10/11/2000 - G.Juyn * */
  43. /* * - fixed processing of unknown critical chunks * */
  44. /* * - added support for nEED * */
  45. /* * 0.9.3 - 10/20/2000 - G.Juyn * */
  46. /* * - added errorcode for delayed delta-processing * */
  47. /* * * */
  48. /* * 0.9.4 - 01/18/2001 - G.Juyn * */
  49. /* * - added errorcode for MAGN methods * */
  50. /* * * */
  51. /* * 1.0.2 - 06/23/2001 - G.Juyn * */
  52. /* * - added optimization option for MNG-video playback * */
  53. /* * * */
  54. /* * 1.0.5 - 07/04/2002 - G.Juyn * */
  55. /* * - added errorcode for extreme chunk-sizes * */
  56. /* * 1.0.5 - 08/15/2002 - G.Juyn * */
  57. /* * - completed delta-image support * */
  58. /* * 1.0.5 - 08/19/2002 - G.Juyn * */
  59. /* * - B597134 - libmng pollutes the linker namespace * */
  60. /* * 1.0.5 - 09/14/2002 - G.Juyn * */
  61. /* * - added event handling for dynamic MNG * */
  62. /* * 1.0.5 - 09/15/2002 - G.Juyn * */
  63. /* * - fixed LOOP iteration=0 special case * */
  64. /* * 1.0.5 - 09/19/2002 - G.Juyn * */
  65. /* * - warnings are ignored by default now! * */
  66. /* * 1.0.5 - 09/20/2002 - G.Juyn * */
  67. /* * - added support for PAST * */
  68. /* * 1.0.5 - 10/07/2002 - G.Juyn * */
  69. /* * - added check for TERM placement during create/write * */
  70. /* * * */
  71. /* * 1.0.6 - 07/07/2003 - G. R-P * */
  72. /* * - added MNG_SKIPCHUNK_CHNK, MNG_NO_DELTA_PNG reductions. * */
  73. /* * - skipped more code when MNG_INCLUDE_JNG is not enabled. * */
  74. /* * 1.0.6 - 07/29/2003 - G.R-P * */
  75. /* * - added conditional around evNT chunk support * */
  76. /* * * */
  77. /* * 1.0.7 - 03/24/2004 - G.R-P * */
  78. /* * - fixed typo on SKIPCHUNK_evNT (->PAST) * */
  79. /* * * */
  80. /* * 1.0.9 - 12/20/2004 - G.Juyn * */
  81. /* * - cleaned up macro-invocations (thanks to D. Airlie) * */
  82. /* * * */
  83. /* * 1.0.10 - 04/08/2007 - G.Juyn * */
  84. /* * - added support for mPNG proposal * */
  85. /* * * */
  86. /* ************************************************************************** */
  87. #include "libmng.h"
  88. #include "libmng_data.h"
  89. #include "libmng_error.h"
  90. #include "libmng_trace.h"
  91. #ifdef __BORLANDC__
  92. #pragma hdrstop
  93. #endif
  94. #if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
  95. #pragma option -A /* force ANSI-C */
  96. #endif
  97. /* ************************************************************************** */
  98. #ifdef MNG_INCLUDE_ERROR_STRINGS
  99. MNG_LOCAL mng_error_entry const error_table [] =
  100. {
  101. {MNG_NOERROR, "No error"},
  102. {MNG_OUTOFMEMORY, "Out of memory"},
  103. {MNG_INVALIDHANDLE, "The handle is invalid"},
  104. {MNG_NOCALLBACK, "A required callback is not defined"},
  105. {MNG_UNEXPECTEDEOF, "Encountered unexpected end-of-file"},
  106. {MNG_ZLIBERROR, "zlib encountered an error"},
  107. #ifdef MNG_INCLUDE_JNG
  108. {MNG_JPEGERROR, "ijgsrc6b encountered an error"},
  109. #endif
  110. {MNG_LCMSERROR, "lcms encountered an error"},
  111. {MNG_NOOUTPUTPROFILE, "No output-profile defined for CMS"},
  112. {MNG_NOSRGBPROFILE, "No sRGB-profile defined for CMS"},
  113. {MNG_BUFOVERFLOW, "Internal buffer-overflow"},
  114. {MNG_FUNCTIONINVALID, "Function is invalid at this point"},
  115. {MNG_OUTPUTERROR, "Writing was unsuccessful; disk full?"},
  116. {MNG_JPEGBUFTOOSMALL, "Internal buffer for JPEG processing too small"},
  117. {MNG_NEEDMOREDATA, "Reading suspended; waiting for I/O to catch up"},
  118. {MNG_NEEDTIMERWAIT, "Timer suspension; normal animation delay"},
  119. {MNG_NEEDSECTIONWAIT, "SEEK suspension; application decides"},
  120. {MNG_LOOPWITHCACHEOFF, "LOOP encountered when playback cache is turned off"},
  121. {MNG_APPIOERROR, "Application signalled I/O error"},
  122. {MNG_APPTIMERERROR, "Application signalled timing error"},
  123. {MNG_APPCMSERROR, "Application signalled CMS error"},
  124. {MNG_APPMISCERROR, "Application signalled an error"},
  125. {MNG_APPTRACEABORT, "Application signalled error during trace-callback"},
  126. {MNG_INTERNALERROR, "Internal error in libmng"},
  127. {MNG_INVALIDSIG, "The signature is invalid"},
  128. {MNG_INVALIDCRC, "The CRC for this chunk is invalid"},
  129. {MNG_INVALIDLENGTH, "Chunk-length is invalid"},
  130. {MNG_SEQUENCEERROR, "Chunk out of sequence"},
  131. {MNG_CHUNKNOTALLOWED, "Chunk not allowed at this point"},
  132. {MNG_MULTIPLEERROR, "Chunk cannot occur multiple times"},
  133. {MNG_PLTEMISSING, "Missing PLTE chunk"},
  134. {MNG_IDATMISSING, "Missing IDAT chunk(s)"},
  135. {MNG_CANNOTBEEMPTY, "Chunk cannot be empty"},
  136. {MNG_GLOBALLENGTHERR, "Global data length invalid"},
  137. {MNG_INVALIDBITDEPTH, "The bit_depth is invalid"},
  138. {MNG_INVALIDCOLORTYPE, "The color_type is invalid"},
  139. {MNG_INVALIDCOMPRESS, "The compression_method is invalid"},
  140. {MNG_INVALIDFILTER, "The filter_method or filter_type is invalid"},
  141. {MNG_INVALIDINTERLACE, "The interlace_method is invalid"},
  142. {MNG_NOTENOUGHIDAT, "There is not enough data in the IDAT chunk(s)"},
  143. {MNG_PLTEINDEXERROR, "Palette-index out of bounds"},
  144. {MNG_NULLNOTFOUND, "NULL separator not found"},
  145. {MNG_KEYWORDNULL, "Keyword cannot be zero-length"},
  146. {MNG_OBJECTUNKNOWN, "Object does not exist"},
  147. {MNG_OBJECTEXISTS, "Object already exists"},
  148. {MNG_TOOMUCHIDAT, "Too much data in IDAT chunk(s)"},
  149. {MNG_INVSAMPLEDEPTH, "The sample_depth is invalid"},
  150. {MNG_INVOFFSETSIZE, "The offset_type is invalid"},
  151. {MNG_INVENTRYTYPE, "The entry_type is invalid"},
  152. {MNG_ENDWITHNULL, "Chunk must not end with NULL byte"},
  153. {MNG_INVIMAGETYPE, "The image_type is invalid"},
  154. #ifndef MNG_NO_DELTA_PNG
  155. {MNG_INVDELTATYPE, "The delta_type is invalid"},
  156. #endif
  157. {MNG_INVALIDINDEX, "Index-value out of bounds"},
  158. #ifdef MNG_INCLUDE_JNG
  159. {MNG_TOOMUCHJDAT, "Too much data in JDAT chunk(s)"},
  160. {MNG_JPEGPARMSERR, "JHDR parameters & JFIF-data do not match"},
  161. #endif
  162. {MNG_INVFILLMETHOD, "The fill_method is invalid"},
  163. #ifndef MNG_NO_DELTA_PNG
  164. {MNG_OBJNOTCONCRETE, "Target object for DHDR must be concrete"},
  165. #endif
  166. {MNG_TARGETNOALPHA, "Target object must have alpha-channel"},
  167. {MNG_MNGTOOCOMPLEX, "MHDR simplicity indicates unsupported feature(s)"},
  168. {MNG_UNKNOWNCRITICAL, "Unknown critical chunk encountered"},
  169. #ifndef MNG_SKIPCHUNK_nEED
  170. {MNG_UNSUPPORTEDNEED, "Requested nEED resources are not supported"},
  171. #endif
  172. {MNG_INVALIDDELTA, "The delta operation is invalid (mismatched color_types?)"},
  173. {MNG_INVALIDMETHOD, "Method is invalid"},
  174. {MNG_IMPROBABLELENGTH, "Chunklength is incredibly large"},
  175. {MNG_INVALIDBLOCK, "Delta block width and or height invalid"},
  176. {MNG_INVALIDEVENT, "Event type is invalid"},
  177. {MNG_INVALIDMASK, "Mask type is invalid"},
  178. {MNG_NOMATCHINGLOOP, "ENDL without matching LOOP"},
  179. #ifndef MNG_SKIPCHUNK_evNT
  180. {MNG_SEEKNOTFOUND, "evNT points to unknown SEEK"},
  181. #endif
  182. #ifndef MNG_SKIPCHUNK_PAST
  183. {MNG_OBJNOTABSTRACT, "Destination object for PAST must be abstract"},
  184. #endif
  185. {MNG_TERMSEQERROR, "TERM misplaced during creation of MNG stream"},
  186. {MNG_INVALIDFIELDVAL, "invalid fieldvalue (generic)"},
  187. {MNG_INVALIDWIDTH, "invalid frame/image width"},
  188. {MNG_INVALIDHEIGHT, "invalid frame/image height"},
  189. {MNG_INVALIDCNVSTYLE, "Canvas_style is invalid"},
  190. {MNG_WRONGCHUNK, "Attempt to access the wrong chunk"},
  191. {MNG_INVALIDENTRYIX, "Attempt to access an non-existing entry"},
  192. {MNG_NOHEADER, "No valid header-chunk"},
  193. {MNG_NOCORRCHUNK, "Parent chunk not found"},
  194. {MNG_NOMHDR, "No MNG header (MHDR) found"},
  195. {MNG_IMAGETOOLARGE, "Image is larger than defined maximum"},
  196. {MNG_NOTANANIMATION, "Image is not an animation"},
  197. {MNG_FRAMENRTOOHIGH, "Framenr out of bounds"},
  198. {MNG_LAYERNRTOOHIGH, "Layernr out of bounds"},
  199. {MNG_PLAYTIMETOOHIGH, "Playtime out of bounds"},
  200. {MNG_FNNOTIMPLEMENTED, "Function not yet implemented"},
  201. {MNG_IMAGEFROZEN, "Image is frozen"},
  202. {MNG_LCMS_NOHANDLE, "Handle could not be initialized"},
  203. {MNG_LCMS_NOMEM, "No memory for gamma-table(s)"},
  204. {MNG_LCMS_NOTRANS, "Transformation could not be initialized"}
  205. };
  206. #endif /* MNG_INCLUDE_ERROR_STRINGS */
  207. /* ************************************************************************** */
  208. mng_bool mng_store_error (mng_datap pData,
  209. mng_retcode iError,
  210. mng_retcode iExtra1,
  211. mng_retcode iExtra2)
  212. {
  213. #ifdef MNG_SUPPORT_TRACE
  214. MNG_TRACEB (pData, MNG_FN_STORE_ERROR, MNG_LC_START);
  215. #endif
  216. if (pData != 0)
  217. {
  218. pData->iErrorcode = iError; /* save also for getlasterror */
  219. pData->iErrorx1 = iExtra1;
  220. pData->iErrorx2 = iExtra2;
  221. #ifdef MNG_INCLUDE_ERROR_STRINGS
  222. { /* binary search variables */
  223. mng_int32 iTop, iLower, iUpper, iMiddle;
  224. mng_error_entryp pEntry; /* pointer to found entry */
  225. /* determine max index of table */
  226. iTop = (sizeof (error_table) / sizeof (error_table [0])) - 1;
  227. iLower = 0; /* initialize binary search */
  228. iMiddle = iTop >> 1; /* start in the middle */
  229. iUpper = iTop;
  230. pEntry = 0; /* no goods yet! */
  231. do /* the binary search itself */
  232. {
  233. if (error_table [iMiddle].iError < iError)
  234. iLower = iMiddle + 1;
  235. else if (error_table [iMiddle].iError > iError)
  236. iUpper = iMiddle - 1;
  237. else
  238. {
  239. pEntry = &error_table [iMiddle];
  240. break;
  241. }
  242. iMiddle = (iLower + iUpper) >> 1;
  243. }
  244. while (iLower <= iUpper);
  245. if (pEntry) /* found it ? */
  246. pData->zErrortext = pEntry->zErrortext;
  247. else
  248. pData->zErrortext = "Unknown error";
  249. }
  250. #else /* MNG_INCLUDE_ERROR_STRINGS */
  251. pData->zErrortext = 0;
  252. #endif /* MNG_INCLUDE_ERROR_STRINGS */
  253. if (iError == 0) /* no error is not severe ! */
  254. {
  255. pData->iSeverity = 0;
  256. }
  257. else
  258. {
  259. switch (iError&0x3C00) /* determine the severity */
  260. {
  261. case 0x0800 : { pData->iSeverity = 5; break; }
  262. case 0x1000 : { pData->iSeverity = 2; break; }
  263. case 0x2000 : { pData->iSeverity = 1; break; }
  264. default : { pData->iSeverity = 9; }
  265. }
  266. }
  267. }
  268. #ifdef MNG_SUPPORT_TRACE
  269. MNG_TRACEB (pData, MNG_FN_STORE_ERROR, MNG_LC_END);
  270. #endif
  271. return MNG_TRUE;
  272. }
  273. /* ************************************************************************** */
  274. mng_bool mng_process_error (mng_datap pData,
  275. mng_retcode iError,
  276. mng_retcode iExtra1,
  277. mng_retcode iExtra2)
  278. {
  279. #ifdef MNG_SUPPORT_TRACE
  280. MNG_TRACEB (pData, MNG_FN_PROCESS_ERROR, MNG_LC_START);
  281. #endif
  282. mng_store_error (pData, iError, iExtra1, iExtra2);
  283. if ((pData != MNG_NULL) && (pData->iMagic == MNG_MAGIC))
  284. {
  285. if (pData->fErrorproc) /* callback defined ? */
  286. return pData->fErrorproc (((mng_handle)pData), iError, pData->iSeverity,
  287. pData->iChunkname, pData->iChunkseq,
  288. pData->iErrorx1, pData->iErrorx2, pData->zErrortext);
  289. }
  290. #ifdef MNG_SUPPORT_TRACE
  291. MNG_TRACEB (pData, MNG_FN_PROCESS_ERROR, MNG_LC_END);
  292. #endif
  293. return MNG_TRUE; /* warnings are ignored by default ! */
  294. }
  295. /* ************************************************************************** */
  296. /* * end of file * */
  297. /* ************************************************************************** */