source: trunk/src/3rdparty/libmng/libmng_read.c

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 30.2 KB
Line 
1/* ************************************************************************** */
2/* * For conditions of distribution and use, * */
3/* * see copyright notice in libmng.h * */
4/* ************************************************************************** */
5/* * * */
6/* * project : libmng * */
7/* * file : libmng_read.c copyright (c) 2000 G.Juyn * */
8/* * version : 1.0.4 * */
9/* * * */
10/* * purpose : Read logic (implementation) * */
11/* * * */
12/* * author : G.Juyn * */
13/* * web : http://www.3-t.com * */
14/* * email : mailto:info@3-t.com * */
15/* * * */
16/* * comment : implementation of the high-level read logic * */
17/* * * */
18/* * changes : 0.5.1 - 05/08/2000 - G.Juyn * */
19/* * - changed strict-ANSI stuff * */
20/* * 0.5.1 - 05/11/2000 - G.Juyn * */
21/* * - added callback error-reporting support * */
22/* * 0.5.1 - 05/12/2000 - G.Juyn * */
23/* * - changed trace to macro for callback error-reporting * */
24/* * * */
25/* * 0.5.2 - 05/19/2000 - G.Juyn * */
26/* * - cleaned up some code regarding mixed support * */
27/* * 0.5.2 - 05/20/2000 - G.Juyn * */
28/* * - added support for JNG * */
29/* * 0.5.2 - 05/31/2000 - G.Juyn * */
30/* * - fixed up punctuation (contribution by Tim Rowley) * */
31/* * * */
32/* * 0.5.3 - 06/16/2000 - G.Juyn * */
33/* * - changed progressive-display processing * */
34/* * * */
35/* * 0.9.1 - 07/08/2000 - G.Juyn * */
36/* * - changed read-processing for improved I/O-suspension * */
37/* * 0.9.1 - 07/14/2000 - G.Juyn * */
38/* * - changed EOF processing behavior * */
39/* * 0.9.1 - 07/14/2000 - G.Juyn * */
40/* * - changed default readbuffer size from 1024 to 4200 * */
41/* * * */
42/* * 0.9.2 - 07/27/2000 - G.Juyn * */
43/* * - B110320 - fixed GCC warning about mix-sized pointer math * */
44/* * 0.9.2 - 07/31/2000 - G.Juyn * */
45/* * - B110546 - fixed for improperly returning UNEXPECTEDEOF * */
46/* * 0.9.2 - 08/04/2000 - G.Juyn * */
47/* * - B111096 - fixed large-buffer read-suspension * */
48/* * 0.9.2 - 08/05/2000 - G.Juyn * */
49/* * - changed file-prefixes * */
50/* * * */
51/* * 0.9.3 - 08/26/2000 - G.Juyn * */
52/* * - added MAGN chunk * */
53/* * 0.9.3 - 10/11/2000 - G.Juyn * */
54/* * - removed test-MaGN * */
55/* * 0.9.3 - 10/16/2000 - G.Juyn * */
56/* * - added support for JDAA * */
57/* * * */
58/* * 0.9.5 - 01/23/2001 - G.Juyn * */
59/* * - fixed timing-problem with switching framing_modes * */
60/* * * */
61/* * 1.0.4 - 06/22/2002 - G.Juyn * */
62/* * - B495443 - incorrect suspend check in read_databuffer * */
63/* * * */
64/* ************************************************************************** */
65
66#include "libmng.h"
67#include "libmng_data.h"
68#include "libmng_error.h"
69#include "libmng_trace.h"
70#ifdef __BORLANDC__
71#pragma hdrstop
72#endif
73#include "libmng_memory.h"
74#include "libmng_objects.h"
75#include "libmng_object_prc.h"
76#include "libmng_chunks.h"
77#include "libmng_chunk_prc.h"
78#include "libmng_chunk_io.h"
79#include "libmng_display.h"
80#include "libmng_read.h"
81
82#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
83#pragma option -A /* force ANSI-C */
84#endif
85
86/* ************************************************************************** */
87
88#ifdef MNG_INCLUDE_READ_PROCS
89
90/* ************************************************************************** */
91
92mng_retcode process_eof (mng_datap pData)
93{
94 if (!pData->bEOF) /* haven't closed the stream yet ? */
95 {
96 pData->bEOF = MNG_TRUE; /* now we do! */
97
98 if (!pData->fClosestream ((mng_handle)pData))
99 MNG_ERROR (pData, MNG_APPIOERROR)
100 }
101
102 return MNG_NOERROR;
103}
104
105/* ************************************************************************** */
106
107mng_retcode read_databuffer (mng_datap pData,
108 mng_uint8p pBuf,
109 mng_uint32 iSize,
110 mng_uint32 * iRead)
111{
112#ifdef MNG_SUPPORT_TRACE
113 MNG_TRACE (pData, MNG_FN_READ_DATABUFFER, MNG_LC_START)
114#endif
115
116 if (pData->bSuspensionmode)
117 {
118 mng_uint8p pTemp;
119 mng_uint32 iTemp;
120
121 *iRead = 0; /* let's be negative about the outcome */
122
123 if (!pData->pSuspendbuf) /* need to create a suspension buffer ? */
124 {
125 pData->iSuspendbufsize = MNG_SUSPENDBUFFERSIZE;
126 /* so, create it */
127 MNG_ALLOC (pData, pData->pSuspendbuf, pData->iSuspendbufsize)
128
129 pData->iSuspendbufleft = 0; /* make sure to fill it first time */
130 pData->pSuspendbufnext = pData->pSuspendbuf;
131 }
132 /* more than our buffer can hold ? */
133 if (iSize > pData->iSuspendbufsize)
134 {
135 mng_uint32 iRemain;
136
137 if (!pData->pReadbufnext) /* first time ? */
138 {
139 if (pData->iSuspendbufleft) /* do we have some data left ? */
140 { /* then copy it */
141 MNG_COPY (pBuf, pData->pSuspendbufnext, pData->iSuspendbufleft)
142 /* fixup variables */
143 pData->pReadbufnext = pBuf + pData->iSuspendbufleft;
144 pData->pSuspendbufnext = pData->pSuspendbuf;
145 pData->iSuspendbufleft = 0;
146 }
147 else
148 {
149 pData->pReadbufnext = pBuf;
150 }
151 }
152 /* calculate how much to get */
153 iRemain = iSize - (mng_uint32)(pData->pReadbufnext - pBuf);
154 /* let's go get it */
155 if (!pData->fReaddata (((mng_handle)pData), pData->pReadbufnext, iRemain, &iTemp))
156 MNG_ERROR (pData, MNG_APPIOERROR);
157 /* first read after suspension return 0 means EOF */
158 if ((pData->iSuspendpoint) && (iTemp == 0))
159 { /* that makes it final */
160 mng_retcode iRetcode = process_eof (pData);
161
162 if (iRetcode) /* on error bail out */
163 return iRetcode;
164 /* indicate the source is depleted */
165 *iRead = iSize - iRemain + iTemp;
166 }
167 else
168 {
169 if (iTemp < iRemain) /* suspension required ? */
170 {
171 pData->pReadbufnext = pData->pReadbufnext + iTemp;
172 pData->bSuspended = MNG_TRUE;
173 }
174 else
175 {
176 *iRead = iSize; /* got it all now ! */
177 }
178 }
179 }
180 else
181 { /* need to read some more ? */
182 while ((!pData->bSuspended) && (!pData->bEOF) && (iSize > pData->iSuspendbufleft))
183 { /* not enough space left in buffer ? */
184 if (pData->iSuspendbufsize - pData->iSuspendbufleft -
185 (mng_uint32)(pData->pSuspendbufnext - pData->pSuspendbuf) <
186 MNG_SUSPENDREQUESTSIZE)
187 {
188 if (pData->iSuspendbufleft) /* then lets shift (if there's anything left) */
189 MNG_COPY (pData->pSuspendbuf, pData->pSuspendbufnext, pData->iSuspendbufleft)
190 /* adjust running pointer */
191 pData->pSuspendbufnext = pData->pSuspendbuf;
192 }
193 /* still not enough room ? */
194 if (pData->iSuspendbufsize - pData->iSuspendbufleft < MNG_SUSPENDREQUESTSIZE)
195 MNG_ERROR (pData, MNG_INTERNALERROR)
196 /* now read some more data */
197 pTemp = pData->pSuspendbufnext + pData->iSuspendbufleft;
198
199 if (!pData->fReaddata (((mng_handle)pData), pTemp, MNG_SUSPENDREQUESTSIZE, &iTemp))
200 MNG_ERROR (pData, MNG_APPIOERROR);
201 /* adjust fill-counter */
202 pData->iSuspendbufleft += iTemp;
203 /* first read after suspension returning 0 means EOF */
204 if ((pData->iSuspendpoint) && (iTemp == 0))
205 { /* that makes it final */
206 mng_retcode iRetcode = process_eof (pData);
207
208 if (iRetcode) /* on error bail out */
209 return iRetcode;
210
211 if (pData->iSuspendbufleft) /* return the leftover scraps */
212 MNG_COPY (pBuf, pData->pSuspendbufnext, pData->iSuspendbufleft)
213 /* and indicate so */
214 *iRead = pData->iSuspendbufleft;
215 pData->pSuspendbufnext = pData->pSuspendbuf;
216 pData->iSuspendbufleft = 0;
217 }
218 else
219 { /* suspension required ? */
220 if ((iSize > pData->iSuspendbufleft) && (iTemp < MNG_SUSPENDREQUESTSIZE))
221 pData->bSuspended = MNG_TRUE;
222
223 }
224
225 pData->iSuspendpoint = 0; /* reset it here in case we loop back */
226 }
227
228 if ((!pData->bSuspended) && (!pData->bEOF))
229 { /* return the data ! */
230 MNG_COPY (pBuf, pData->pSuspendbufnext, iSize)
231
232 *iRead = iSize; /* returned it all */
233 /* adjust suspension-buffer variables */
234 pData->pSuspendbufnext += iSize;
235 pData->iSuspendbufleft -= iSize;
236 }
237 }
238 }
239 else
240 {
241 if (!pData->fReaddata (((mng_handle)pData), (mng_ptr)pBuf, iSize, iRead))
242 {
243 if (*iRead == 0) /* suspension required ? */
244 pData->bSuspended = MNG_TRUE;
245 else
246 MNG_ERROR (pData, MNG_APPIOERROR);
247 }
248 }
249
250 pData->iSuspendpoint = 0; /* safely reset it here ! */
251
252#ifdef MNG_SUPPORT_TRACE
253 MNG_TRACE (pData, MNG_FN_READ_DATABUFFER, MNG_LC_END)
254#endif
255
256 return MNG_NOERROR;
257}
258
259/* ************************************************************************** */
260
261mng_retcode process_raw_chunk (mng_datap pData,
262 mng_uint8p pBuf,
263 mng_uint32 iBuflen)
264{
265 /* the table-idea & binary search code was adapted from
266 libpng 1.1.0 (pngread.c) */
267 /* NOTE1: the table must remain sorted by chunkname, otherwise the binary
268 search will break !!! */
269 /* NOTE2: the layout must remain equal to the header part of all the
270 chunk-structures (yes, that means even the pNext and pPrev fields;
271 it's wasting a bit of space, but hey, the code is a lot easier) */
272
273 mng_chunk_header chunk_unknown = {MNG_UINT_HUH, init_unknown, free_unknown,
274 read_unknown, write_unknown, 0, 0};
275
276 mng_chunk_header chunk_table [] =
277 {
278 {MNG_UINT_BACK, init_back, free_back, read_back, write_back, 0, 0},
279 {MNG_UINT_BASI, init_basi, free_basi, read_basi, write_basi, 0, 0},
280 {MNG_UINT_CLIP, init_clip, free_clip, read_clip, write_clip, 0, 0},
281 {MNG_UINT_CLON, init_clon, free_clon, read_clon, write_clon, 0, 0},
282 {MNG_UINT_DBYK, init_dbyk, free_dbyk, read_dbyk, write_dbyk, 0, 0},
283 {MNG_UINT_DEFI, init_defi, free_defi, read_defi, write_defi, 0, 0},
284 {MNG_UINT_DHDR, init_dhdr, free_dhdr, read_dhdr, write_dhdr, 0, 0},
285 {MNG_UINT_DISC, init_disc, free_disc, read_disc, write_disc, 0, 0},
286 {MNG_UINT_DROP, init_drop, free_drop, read_drop, write_drop, 0, 0},
287 {MNG_UINT_ENDL, init_endl, free_endl, read_endl, write_endl, 0, 0},
288 {MNG_UINT_FRAM, init_fram, free_fram, read_fram, write_fram, 0, 0},
289 {MNG_UINT_IDAT, init_idat, free_idat, read_idat, write_idat, 0, 0}, /* 12-th element! */
290 {MNG_UINT_IEND, init_iend, free_iend, read_iend, write_iend, 0, 0},
291 {MNG_UINT_IHDR, init_ihdr, free_ihdr, read_ihdr, write_ihdr, 0, 0},
292 {MNG_UINT_IJNG, init_ijng, free_ijng, read_ijng, write_ijng, 0, 0},
293 {MNG_UINT_IPNG, init_ipng, free_ipng, read_ipng, write_ipng, 0, 0},
294#ifdef MNG_INCLUDE_JNG
295 {MNG_UINT_JDAA, init_jdaa, free_jdaa, read_jdaa, write_jdaa, 0, 0},
296 {MNG_UINT_JDAT, init_jdat, free_jdat, read_jdat, write_jdat, 0, 0},
297 {MNG_UINT_JHDR, init_jhdr, free_jhdr, read_jhdr, write_jhdr, 0, 0},
298 {MNG_UINT_JSEP, init_jsep, free_jsep, read_jsep, write_jsep, 0, 0},
299 {MNG_UINT_JdAA, init_jdaa, free_jdaa, read_jdaa, write_jdaa, 0, 0},
300#endif
301 {MNG_UINT_LOOP, init_loop, free_loop, read_loop, write_loop, 0, 0},
302 {MNG_UINT_MAGN, init_magn, free_magn, read_magn, write_magn, 0, 0},
303 {MNG_UINT_MEND, init_mend, free_mend, read_mend, write_mend, 0, 0},
304 {MNG_UINT_MHDR, init_mhdr, free_mhdr, read_mhdr, write_mhdr, 0, 0},
305 {MNG_UINT_MOVE, init_move, free_move, read_move, write_move, 0, 0},
306 {MNG_UINT_ORDR, init_ordr, free_ordr, read_ordr, write_ordr, 0, 0},
307 {MNG_UINT_PAST, init_past, free_past, read_past, write_past, 0, 0},
308 {MNG_UINT_PLTE, init_plte, free_plte, read_plte, write_plte, 0, 0},
309 {MNG_UINT_PPLT, init_pplt, free_pplt, read_pplt, write_pplt, 0, 0},
310 {MNG_UINT_PROM, init_prom, free_prom, read_prom, write_prom, 0, 0},
311 {MNG_UINT_SAVE, init_save, free_save, read_save, write_save, 0, 0},
312 {MNG_UINT_SEEK, init_seek, free_seek, read_seek, write_seek, 0, 0},
313 {MNG_UINT_SHOW, init_show, free_show, read_show, write_show, 0, 0},
314 {MNG_UINT_TERM, init_term, free_term, read_term, write_term, 0, 0},
315 {MNG_UINT_bKGD, init_bkgd, free_bkgd, read_bkgd, write_bkgd, 0, 0},
316 {MNG_UINT_cHRM, init_chrm, free_chrm, read_chrm, write_chrm, 0, 0},
317 {MNG_UINT_eXPI, init_expi, free_expi, read_expi, write_expi, 0, 0},
318 {MNG_UINT_fPRI, init_fpri, free_fpri, read_fpri, write_fpri, 0, 0},
319 {MNG_UINT_gAMA, init_gama, free_gama, read_gama, write_gama, 0, 0},
320 {MNG_UINT_hIST, init_hist, free_hist, read_hist, write_hist, 0, 0},
321 {MNG_UINT_iCCP, init_iccp, free_iccp, read_iccp, write_iccp, 0, 0},
322 {MNG_UINT_iTXt, init_itxt, free_itxt, read_itxt, write_itxt, 0, 0},
323 {MNG_UINT_nEED, init_need, free_need, read_need, write_need, 0, 0},
324/* TODO: {MNG_UINT_oFFs, 0, 0, 0, 0, 0, 0}, */
325/* TODO: {MNG_UINT_pCAL, 0, 0, 0, 0, 0, 0}, */
326 {MNG_UINT_pHYg, init_phyg, free_phyg, read_phyg, write_phyg, 0, 0},
327 {MNG_UINT_pHYs, init_phys, free_phys, read_phys, write_phys, 0, 0},
328 {MNG_UINT_sBIT, init_sbit, free_sbit, read_sbit, write_sbit, 0, 0},
329/* TODO: {MNG_UINT_sCAL, 0, 0, 0, 0, 0, 0}, */
330 {MNG_UINT_sPLT, init_splt, free_splt, read_splt, write_splt, 0, 0},
331 {MNG_UINT_sRGB, init_srgb, free_srgb, read_srgb, write_srgb, 0, 0},
332 {MNG_UINT_tEXt, init_text, free_text, read_text, write_text, 0, 0},
333 {MNG_UINT_tIME, init_time, free_time, read_time, write_time, 0, 0},
334 {MNG_UINT_tRNS, init_trns, free_trns, read_trns, write_trns, 0, 0},
335 {MNG_UINT_zTXt, init_ztxt, free_ztxt, read_ztxt, write_ztxt, 0, 0},
336 };
337 /* binary search variables */
338 mng_int32 iTop, iLower, iUpper, iMiddle;
339 mng_chunk_headerp pEntry; /* pointer to found entry */
340 mng_chunkid iChunkname; /* the chunk's tag */
341 mng_chunkp pChunk; /* chunk structure (if #define MNG_STORE_CHUNKS) */
342 mng_retcode iRetcode; /* temporary error-code */
343
344#ifdef MNG_SUPPORT_TRACE
345 MNG_TRACE (pData, MNG_FN_PROCESS_RAW_CHUNK, MNG_LC_START)
346#endif
347 /* get the chunkname */
348 iChunkname = (mng_chunkid)(mng_get_uint32 (pBuf));
349
350 pBuf += sizeof (mng_chunkid); /* adjust the buffer */
351 iBuflen -= sizeof (mng_chunkid);
352 /* determine max index of table */
353 iTop = (sizeof (chunk_table) / sizeof (chunk_table [0])) - 1;
354
355 /* binary search; with 52 chunks, worst-case is 7 comparisons */
356 iLower = 0;
357 iMiddle = 11; /* start with the IDAT entry */
358 iUpper = iTop;
359 pEntry = 0; /* no goods yet! */
360 pChunk = 0;
361
362 do /* the binary search itself */
363 {
364 if (chunk_table [iMiddle].iChunkname < iChunkname)
365 iLower = iMiddle + 1;
366 else if (chunk_table [iMiddle].iChunkname > iChunkname)
367 iUpper = iMiddle - 1;
368 else
369 {
370 pEntry = &chunk_table [iMiddle];
371 break;
372 }
373
374 iMiddle = (iLower + iUpper) >> 1;
375 }
376 while (iLower <= iUpper);
377
378 if (!pEntry) /* unknown chunk ? */
379 pEntry = &chunk_unknown; /* make it so! */
380
381 pData->iChunkname = iChunkname; /* keep track of where we are */
382 pData->iChunkseq++;
383
384 if (pEntry->fRead) /* read-callback available ? */
385 {
386 iRetcode = pEntry->fRead (pData, pEntry, iBuflen, (mng_ptr)pBuf, &pChunk);
387
388 if (!iRetcode) /* everything oke ? */
389 { /* remember unknown chunk's id */
390 if ((pChunk) && (pEntry == &chunk_unknown))
391 ((mng_chunk_headerp)pChunk)->iChunkname = iChunkname;
392 }
393 }
394 else
395 iRetcode = MNG_NOERROR;
396
397 if (pChunk) /* store this chunk ? */
398 add_chunk (pData, pChunk); /* do it */
399
400#ifdef MNG_INCLUDE_JNG /* implicit EOF ? */
401 if ((!pData->bHasMHDR) && (!pData->bHasIHDR) && (!pData->bHasJHDR))
402#else
403 if ((!pData->bHasMHDR) && (!pData->bHasIHDR))
404#endif
405 iRetcode = process_eof (pData); /* then do some EOF processing */
406
407 if (iRetcode) /* on error bail out */
408 return iRetcode;
409
410#ifdef MNG_SUPPORT_TRACE
411 MNG_TRACE (pData, MNG_FN_PROCESS_RAW_CHUNK, MNG_LC_END)
412#endif
413
414 return MNG_NOERROR;
415}
416
417/* ************************************************************************** */
418
419mng_retcode read_chunk (mng_datap pData)
420{
421 mng_uint32 iBufmax = pData->iReadbufsize;
422 mng_uint8p pBuf = pData->pReadbuf;
423 mng_uint32 iBuflen = 0; /* number of bytes requested */
424 mng_uint32 iRead = 0; /* number of bytes read */
425 mng_uint32 iCrc; /* calculated CRC */
426 mng_retcode iRetcode = MNG_NOERROR;
427
428#ifdef MNG_SUPPORT_TRACE
429 MNG_TRACE (pData, MNG_FN_READ_CHUNK, MNG_LC_START)
430#endif
431
432#ifdef MNG_SUPPORT_DISPLAY
433 if (pData->pCurraniobj) /* processing an animation object ? */
434 {
435 do /* process it then */
436 {
437 iRetcode = ((mng_object_headerp)pData->pCurraniobj)->fProcess (pData, pData->pCurraniobj);
438 /* refresh needed ? */
439/* if ((!iRetcode) && (!pData->bTimerset) && (pData->bNeedrefresh))
440 iRetcode = display_progressive_refresh (pData, 1); */
441 /* can we advance to next object ? */
442 if ((!iRetcode) && (pData->pCurraniobj) &&
443 (!pData->bTimerset) && (!pData->bSectionwait))
444 {
445 pData->pCurraniobj = ((mng_object_headerp)pData->pCurraniobj)->pNext;
446 /* TERM processing to be done ? */
447 if ((!pData->pCurraniobj) && (pData->bHasTERM) && (!pData->bHasMHDR))
448 iRetcode = process_display_mend (pData);
449 }
450 } /* until error or a break or no more objects */
451 while ((!iRetcode) && (pData->pCurraniobj) &&
452 (!pData->bTimerset) && (!pData->bSectionwait) && (!pData->bFreezing));
453 }
454 else
455 {
456 if (pData->iBreakpoint) /* do we need to finish something first ? */
457 {
458 switch (pData->iBreakpoint) /* return to broken display routine */
459 {
460 case 1 : { iRetcode = process_display_fram2 (pData); break; }
461 case 2 : { iRetcode = process_display_ihdr (pData); break; }
462 case 3 : ; /* same as 4 !!! */
463 case 4 : { iRetcode = process_display_show (pData); break; }
464 case 5 : { iRetcode = process_display_clon2 (pData); break; }
465#ifdef MNG_INCLUDE_JNG
466 case 7 : { iRetcode = process_display_jhdr (pData); break; }
467#endif
468 case 6 : ; /* same as 8 !!! */
469 case 8 : { iRetcode = process_display_iend (pData); break; }
470 case 9 : { iRetcode = process_display_magn2 (pData); break; }
471 }
472 }
473 }
474
475 if (iRetcode) /* on error bail out */
476 return iRetcode;
477
478#endif /* MNG_SUPPORT_DISPLAY */
479 /* can we continue processing now, or do we */
480 /* need to wait for the timer to finish (again) ? */
481#ifdef MNG_SUPPORT_DISPLAY
482 if ((!pData->bTimerset) && (!pData->bSectionwait) && (!pData->bEOF))
483#else
484 if (!pData->bEOF)
485#endif
486 { /* freezing in progress ? */
487 if ((pData->bFreezing) && (pData->iSuspendpoint == 0))
488 pData->bRunning = MNG_FALSE; /* then this is the right moment to do it */
489
490 if (pData->iSuspendpoint <= 2)
491 {
492 iBuflen = sizeof (mng_uint32); /* read length */
493 iRetcode = read_databuffer (pData, pBuf, iBuflen, &iRead);
494
495 if (iRetcode) /* bail on errors */
496 return iRetcode;
497
498 if (pData->bSuspended) /* suspended ? */
499 pData->iSuspendpoint = 2;
500 else /* save the length */
501 pData->iChunklen = mng_get_uint32 (pBuf);
502
503 }
504
505 if (!pData->bSuspended) /* still going ? */
506 { /* previously suspended or not eof ? */
507 if ((pData->iSuspendpoint > 2) || (iRead == iBuflen))
508 { /* determine length chunkname + data + crc */
509 iBuflen = pData->iChunklen + (mng_uint32)(sizeof (mng_chunkid) + sizeof (iCrc));
510
511 if (iBuflen < iBufmax) /* does it fit in default buffer ? */
512 { /* note that we don't use the full size
513 so there's always a zero-byte at the
514 very end !!! */
515 iRetcode = read_databuffer (pData, pBuf, iBuflen, &iRead);
516
517 if (iRetcode) /* bail on errors */
518 return iRetcode;
519
520 if (pData->bSuspended) /* suspended ? */
521 pData->iSuspendpoint = 3;
522 else
523 {
524 if (iRead != iBuflen) /* did we get all the data ? */
525 iRetcode = MNG_UNEXPECTEDEOF;
526 else
527 {
528 mng_uint32 iL = iBuflen - (mng_uint32)(sizeof (iCrc));
529 /* calculate the crc */
530 iCrc = crc (pData, pBuf, iL);
531 /* and check it */
532 if (!(iCrc == mng_get_uint32 (pBuf + iL)))
533 iRetcode = MNG_INVALIDCRC;
534 else
535 iRetcode = process_raw_chunk (pData, pBuf, iL);
536 }
537 }
538 }
539 else
540 {
541 if (!pData->iSuspendpoint) /* create additional large buffer ? */
542 { /* again reserve space for the last zero-byte */
543 pData->iLargebufsize = iBuflen + 1;
544 MNG_ALLOC (pData, pData->pLargebuf, pData->iLargebufsize)
545 }
546
547 iRetcode = read_databuffer (pData, pData->pLargebuf, iBuflen, &iRead);
548
549 if (iRetcode)
550 return iRetcode;
551
552 if (pData->bSuspended) /* suspended ? */
553 pData->iSuspendpoint = 4;
554 else
555 {
556 if (iRead != iBuflen) /* did we get all the data ? */
557 iRetcode = MNG_UNEXPECTEDEOF;
558 else
559 {
560 mng_uint32 iL = iBuflen - (mng_uint32)(sizeof (iCrc));
561 /* calculate the crc */
562 iCrc = crc (pData, pData->pLargebuf, iL);
563 /* and check it */
564 if (!(iCrc == mng_get_uint32 (pData->pLargebuf + iL)))
565 iRetcode = MNG_INVALIDCRC;
566 else
567 iRetcode = process_raw_chunk (pData, pData->pLargebuf, iL);
568 }
569 /* cleanup additional large buffer */
570 MNG_FREE (pData, pData->pLargebuf, pData->iLargebufsize)
571 }
572 }
573
574 if (iRetcode) /* on error bail out */
575 return iRetcode;
576
577 }
578 else
579 { /* that's final */
580 iRetcode = process_eof (pData);
581
582 if (iRetcode) /* on error bail out */
583 return iRetcode;
584
585 if ((iRead != 0) || /* did we get an unexpected eof ? */
586#ifdef MNG_INCLUDE_JNG
587 (pData->bHasIHDR || pData->bHasMHDR || pData->bHasJHDR))
588#else
589 (pData->bHasIHDR || pData->bHasMHDR))
590#endif
591 MNG_ERROR (pData, MNG_UNEXPECTEDEOF);
592 }
593 }
594 }
595
596#ifdef MNG_SUPPORT_DISPLAY /* refresh needed ? */
597 if ((!pData->bTimerset) && (!pData->bSuspended) && (pData->bNeedrefresh))
598 {
599 iRetcode = display_progressive_refresh (pData, 1);
600
601 if (iRetcode) /* on error bail out */
602 return iRetcode;
603 }
604#endif
605
606#ifdef MNG_SUPPORT_TRACE
607 MNG_TRACE (pData, MNG_FN_READ_CHUNK, MNG_LC_END)
608#endif
609
610 return MNG_NOERROR;
611}
612
613/* ************************************************************************** */
614
615mng_retcode read_graphic (mng_datap pData)
616{
617 mng_uint32 iBuflen; /* number of bytes requested */
618 mng_uint32 iRead; /* number of bytes read */
619 mng_retcode iRetcode; /* temporary error-code */
620
621#ifdef MNG_SUPPORT_TRACE
622 MNG_TRACE (pData, MNG_FN_READ_GRAPHIC, MNG_LC_START)
623#endif
624
625 if (!pData->pReadbuf) /* buffer allocated ? */
626 {
627 pData->iReadbufsize = 4200; /* allocate a default read buffer */
628 MNG_ALLOC (pData, pData->pReadbuf, pData->iReadbufsize)
629 }
630 /* haven't processed the signature ? */
631 if ((!pData->bHavesig) || (pData->iSuspendpoint == 1))
632 {
633 iBuflen = 2 * sizeof (mng_uint32); /* read signature */
634
635 iRetcode = read_databuffer (pData, pData->pReadbuf, iBuflen, &iRead);
636
637 if (iRetcode)
638 return iRetcode;
639
640 if (pData->bSuspended) /* input suspension ? */
641 pData->iSuspendpoint = 1;
642 else
643 {
644 if (iRead != iBuflen) /* full signature received ? */
645 MNG_ERROR (pData, MNG_UNEXPECTEDEOF);
646 /* is it a valid signature ? */
647 if (mng_get_uint32 (pData->pReadbuf) == PNG_SIG)
648 pData->eSigtype = mng_it_png;
649 else
650 if (mng_get_uint32 (pData->pReadbuf) == JNG_SIG)
651 pData->eSigtype = mng_it_jng;
652 else
653 if (mng_get_uint32 (pData->pReadbuf) == MNG_SIG)
654 pData->eSigtype = mng_it_mng;
655 else
656 MNG_ERROR (pData, MNG_INVALIDSIG);
657 /* all of it ? */
658 if (mng_get_uint32 (pData->pReadbuf+4) != POST_SIG)
659 MNG_ERROR (pData, MNG_INVALIDSIG);
660
661 pData->bHavesig = MNG_TRUE;
662 }
663 }
664
665 if (!pData->bSuspended) /* still going ? */
666 {
667 do
668 {
669 iRetcode = read_chunk (pData); /* process a chunk */
670
671 if (iRetcode) /* on error bail out */
672 return iRetcode;
673 }
674#ifdef MNG_SUPPORT_DISPLAY /* until EOF or a break-request */
675 while (((!pData->bEOF) || (pData->pCurraniobj)) && (!pData->bSuspended) &&
676 (!pData->bTimerset) && (!pData->bSectionwait));
677#else
678 while ((!pData->bEOF) && (!pData->bSuspended));
679#endif
680 }
681
682#ifdef MNG_SUPPORT_TRACE
683 MNG_TRACE (pData, MNG_FN_READ_GRAPHIC, MNG_LC_END)
684#endif
685
686 return MNG_NOERROR;
687}
688
689/* ************************************************************************** */
690
691#endif /* MNG_INCLUDE_READ_PROCS */
692
693/* ************************************************************************** */
694/* * end of file * */
695/* ************************************************************************** */
696
Note: See TracBrowser for help on using the repository browser.