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 
88 #include "libmng.h"
89 #include "libmng_data.h"
90 #include "libmng_error.h"
91 #include "libmng_trace.h"
92 #ifdef __BORLANDC__
93 #pragma hdrstop
94 #endif
95 
96 #if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
97 #pragma option -A                      /* force ANSI-C */
98 #endif
99 
100 /* ************************************************************************** */
101 
102 #ifdef MNG_INCLUDE_ERROR_STRINGS
103 MNG_LOCAL mng_error_entry const error_table [] =
104   {
105     {MNG_NOERROR,          "No error"},
106     {MNG_OUTOFMEMORY,      "Out of memory"},
107     {MNG_INVALIDHANDLE,    "The handle is invalid"},
108     {MNG_NOCALLBACK,       "A required callback is not defined"},
109     {MNG_UNEXPECTEDEOF,    "Encountered unexpected end-of-file"},
110     {MNG_ZLIBERROR,        "zlib encountered an error"},
111 #ifdef MNG_INCLUDE_JNG
112     {MNG_JPEGERROR,        "ijgsrc6b encountered an error"},
113 #endif
114     {MNG_LCMSERROR,        "lcms encountered an error"},
115     {MNG_NOOUTPUTPROFILE,  "No output-profile defined for CMS"},
116     {MNG_NOSRGBPROFILE,    "No sRGB-profile defined for CMS"},
117     {MNG_BUFOVERFLOW,      "Internal buffer-overflow"},
118     {MNG_FUNCTIONINVALID,  "Function is invalid at this point"},
119     {MNG_OUTPUTERROR,      "Writing was unsuccessful; disk full?"},
120     {MNG_JPEGBUFTOOSMALL,  "Internal buffer for JPEG processing too small"},
121     {MNG_NEEDMOREDATA,     "Reading suspended; waiting for I/O to catch up"},
122     {MNG_NEEDTIMERWAIT,    "Timer suspension; normal animation delay"},
123     {MNG_NEEDSECTIONWAIT,  "SEEK suspension; application decides"},
124     {MNG_LOOPWITHCACHEOFF, "LOOP encountered when playback cache is turned off"},
125 
126     {MNG_APPIOERROR,       "Application signalled I/O error"},
127     {MNG_APPTIMERERROR,    "Application signalled timing error"},
128     {MNG_APPCMSERROR,      "Application signalled CMS error"},
129     {MNG_APPMISCERROR,     "Application signalled an error"},
130     {MNG_APPTRACEABORT,    "Application signalled error during trace-callback"},
131 
132     {MNG_INTERNALERROR,    "Internal error in libmng"},
133 
134     {MNG_INVALIDSIG,       "The signature is invalid"},
135     {MNG_INVALIDCRC,       "The CRC for this chunk is invalid"},
136     {MNG_INVALIDLENGTH,    "Chunk-length is invalid"},
137     {MNG_SEQUENCEERROR,    "Chunk out of sequence"},
138     {MNG_CHUNKNOTALLOWED,  "Chunk not allowed at this point"},
139     {MNG_MULTIPLEERROR,    "Chunk cannot occur multiple times"},
140     {MNG_PLTEMISSING,      "Missing PLTE chunk"},
141     {MNG_IDATMISSING,      "Missing IDAT chunk(s)"},
142     {MNG_CANNOTBEEMPTY,    "Chunk cannot be empty"},
143     {MNG_GLOBALLENGTHERR,  "Global data length invalid"},
144     {MNG_INVALIDBITDEPTH,  "The bit_depth is invalid"},
145     {MNG_INVALIDCOLORTYPE, "The color_type is invalid"},
146     {MNG_INVALIDCOMPRESS,  "The compression_method is invalid"},
147     {MNG_INVALIDFILTER,    "The filter_method or filter_type is invalid"},
148     {MNG_INVALIDINTERLACE, "The interlace_method is invalid"},
149     {MNG_NOTENOUGHIDAT,    "There is not enough data in the IDAT chunk(s)"},
150     {MNG_PLTEINDEXERROR,   "Palette-index out of bounds"},
151     {MNG_NULLNOTFOUND,     "NULL separator not found"},
152     {MNG_KEYWORDNULL,      "Keyword cannot be zero-length"},
153     {MNG_OBJECTUNKNOWN,    "Object does not exist"},
154     {MNG_OBJECTEXISTS,     "Object already exists"},
155     {MNG_TOOMUCHIDAT,      "Too much data in IDAT chunk(s)"},
156     {MNG_INVSAMPLEDEPTH,   "The sample_depth is invalid"},
157     {MNG_INVOFFSETSIZE,    "The offset_type is invalid"},
158     {MNG_INVENTRYTYPE,     "The entry_type is invalid"},
159     {MNG_ENDWITHNULL,      "Chunk must not end with NULL byte"},
160     {MNG_INVIMAGETYPE,     "The image_type is invalid"},
161 #ifndef MNG_NO_DELTA_PNG
162     {MNG_INVDELTATYPE,     "The delta_type is invalid"},
163 #endif
164     {MNG_INVALIDINDEX,     "Index-value out of bounds"},
165 #ifdef MNG_INCLUDE_JNG
166     {MNG_TOOMUCHJDAT,      "Too much data in JDAT chunk(s)"},
167     {MNG_JPEGPARMSERR,     "JHDR parameters & JFIF-data do not match"},
168 #endif
169     {MNG_INVFILLMETHOD,    "The fill_method is invalid"},
170 #ifndef MNG_NO_DELTA_PNG
171     {MNG_OBJNOTCONCRETE,   "Target object for DHDR must be concrete"},
172 #endif
173     {MNG_TARGETNOALPHA,    "Target object must have alpha-channel"},
174     {MNG_MNGTOOCOMPLEX,    "MHDR simplicity indicates unsupported feature(s)"},
175     {MNG_UNKNOWNCRITICAL,  "Unknown critical chunk encountered"},
176 #ifndef MNG_SKIPCHUNK_nEED
177     {MNG_UNSUPPORTEDNEED,  "Requested nEED resources are not supported"},
178 #endif
179     {MNG_INVALIDDELTA,     "The delta operation is invalid (mismatched color_types?)"},
180     {MNG_INVALIDMETHOD,    "Method is invalid"},
181     {MNG_IMPROBABLELENGTH, "Chunklength is incredibly large"},
182     {MNG_INVALIDBLOCK,     "Delta block width and or height invalid"},
183     {MNG_INVALIDEVENT,     "Event type is invalid"},
184     {MNG_INVALIDMASK,      "Mask type is invalid"},
185     {MNG_NOMATCHINGLOOP,   "ENDL without matching LOOP"},
186 #ifndef MNG_SKIPCHUNK_evNT
187     {MNG_SEEKNOTFOUND,     "evNT points to unknown SEEK"},
188 #endif
189 #ifndef MNG_SKIPCHUNK_PAST
190     {MNG_OBJNOTABSTRACT,   "Destination object for PAST must be abstract"},
191 #endif
192     {MNG_TERMSEQERROR,     "TERM misplaced during creation of MNG stream"},
193     {MNG_INVALIDFIELDVAL,  "invalid fieldvalue (generic)"},
194     {MNG_INVALIDWIDTH,     "invalid frame/image width"},
195     {MNG_INVALIDHEIGHT,    "invalid frame/image height"},
196 
197     {MNG_INVALIDCNVSTYLE,  "Canvas_style is invalid"},
198     {MNG_WRONGCHUNK,       "Attempt to access the wrong chunk"},
199     {MNG_INVALIDENTRYIX,   "Attempt to access an non-existing entry"},
200     {MNG_NOHEADER,         "No valid header-chunk"},
201     {MNG_NOCORRCHUNK,      "Parent chunk not found"},
202     {MNG_NOMHDR,           "No MNG header (MHDR) found"},
203 
204     {MNG_IMAGETOOLARGE,    "Image is larger than defined maximum"},
205     {MNG_NOTANANIMATION,   "Image is not an animation"},
206     {MNG_FRAMENRTOOHIGH,   "Framenr out of bounds"},
207     {MNG_LAYERNRTOOHIGH,   "Layernr out of bounds"},
208     {MNG_PLAYTIMETOOHIGH,  "Playtime out of bounds"},
209     {MNG_FNNOTIMPLEMENTED, "Function not yet implemented"},
210     {MNG_IMAGEFROZEN,      "Image is frozen"},
211 
212     {MNG_LCMS_NOHANDLE,    "Handle could not be initialized"},
213     {MNG_LCMS_NOMEM,       "No memory for gamma-table(s)"},
214     {MNG_LCMS_NOTRANS,     "Transformation could not be initialized"}
215   };
216 #endif /* MNG_INCLUDE_ERROR_STRINGS */
217 
218 /* ************************************************************************** */
219 
mng_store_error(mng_datap pData,mng_retcode iError,mng_retcode iExtra1,mng_retcode iExtra2)220 mng_bool mng_store_error (mng_datap   pData,
221                           mng_retcode iError,
222                           mng_retcode iExtra1,
223                           mng_retcode iExtra2)
224 {
225 #ifdef MNG_SUPPORT_TRACE
226   MNG_TRACEB (pData, MNG_FN_STORE_ERROR, MNG_LC_START);
227 #endif
228 
229   if (pData != 0)
230   {
231     pData->iErrorcode = iError;        /* save also for getlasterror */
232     pData->iErrorx1   = iExtra1;
233     pData->iErrorx2   = iExtra2;
234 
235 #ifdef MNG_INCLUDE_ERROR_STRINGS
236     {                                  /* binary search variables */
237       mng_int32        iTop, iLower, iUpper, iMiddle;
238       mng_error_entryp pEntry;         /* pointer to found entry */
239                                        /* determine max index of table */
240       iTop = (sizeof (error_table) / sizeof (error_table [0])) - 1;
241 
242       iLower  = 0;                     /* initialize binary search */
243       iMiddle = iTop >> 1;             /* start in the middle */
244       iUpper  = iTop;
245       pEntry  = 0;                     /* no goods yet! */
246 
247       do                               /* the binary search itself */
248         {
249           if (error_table [iMiddle].iError < iError)
250             iLower = iMiddle + 1;
251           else if (error_table [iMiddle].iError > iError)
252             iUpper = iMiddle - 1;
253           else
254           {
255             pEntry = &error_table [iMiddle];
256             break;
257           }
258 
259           iMiddle = (iLower + iUpper) >> 1;
260         }
261       while (iLower <= iUpper);
262 
263       if (pEntry)                      /* found it ? */
264         pData->zErrortext = pEntry->zErrortext;
265       else
266         pData->zErrortext = "Unknown error";
267       }
268 #else /* MNG_INCLUDE_ERROR_STRINGS */
269     pData->zErrortext = 0;
270 #endif /* MNG_INCLUDE_ERROR_STRINGS */
271 
272     if (iError == 0)                   /* no error is not severe ! */
273     {
274       pData->iSeverity = 0;
275     }
276     else
277     {
278       switch (iError&0x3C00)           /* determine the severity */
279       {
280         case 0x0800 : { pData->iSeverity = 5; break; }
281         case 0x1000 : { pData->iSeverity = 2; break; }
282         case 0x2000 : { pData->iSeverity = 1; break; }
283         default     : { pData->iSeverity = 9; }
284       }
285     }
286   }
287 
288 #ifdef MNG_SUPPORT_TRACE
289   MNG_TRACEB (pData, MNG_FN_STORE_ERROR, MNG_LC_END);
290 #endif
291 
292   return MNG_TRUE;
293 }
294 
295 /* ************************************************************************** */
296 
mng_process_error(mng_datap pData,mng_retcode iError,mng_retcode iExtra1,mng_retcode iExtra2)297 mng_bool mng_process_error (mng_datap   pData,
298                             mng_retcode iError,
299                             mng_retcode iExtra1,
300                             mng_retcode iExtra2)
301 {
302 #ifdef MNG_SUPPORT_TRACE
303   MNG_TRACEB (pData, MNG_FN_PROCESS_ERROR, MNG_LC_START);
304 #endif
305 
306   mng_store_error (pData, iError, iExtra1, iExtra2);
307 
308   if ((pData != MNG_NULL) && (pData->iMagic == MNG_MAGIC))
309   {
310     if (pData->fErrorproc)             /* callback defined ? */
311       return pData->fErrorproc (((mng_handle)pData), iError, pData->iSeverity,
312                                 pData->iChunkname, pData->iChunkseq,
313                                 pData->iErrorx1, pData->iErrorx2, pData->zErrortext);
314   }
315 
316 #ifdef MNG_SUPPORT_TRACE
317   MNG_TRACEB (pData, MNG_FN_PROCESS_ERROR, MNG_LC_END);
318 #endif
319 
320   return MNG_TRUE;                     /* warnings are ignored by default ! */
321 }
322 
323 /* ************************************************************************** */
324 /* * end of file                                                            * */
325 /* ************************************************************************** */
326 
327