📄 libmng_read.c
字号:
/* ************************************************************************** *//* * For conditions of distribution and use, * *//* * see copyright notice in libmng.h * *//* ************************************************************************** *//* * * *//* * project : libmng * *//* * file : libmng_read.c copyright (c) 2000-2004 G.Juyn * *//* * version : 1.0.9 * *//* * * *//* * purpose : Read logic (implementation) * *//* * * *//* * author : G.Juyn * *//* * * *//* * comment : implementation of the high-level read logic * *//* * * *//* * changes : 0.5.1 - 05/08/2000 - G.Juyn * *//* * - changed strict-ANSI stuff * *//* * 0.5.1 - 05/11/2000 - G.Juyn * *//* * - added callback error-reporting support * *//* * 0.5.1 - 05/12/2000 - G.Juyn * *//* * - changed trace to macro for callback error-reporting * *//* * * *//* * 0.5.2 - 05/19/2000 - G.Juyn * *//* * - cleaned up some code regarding mixed support * *//* * 0.5.2 - 05/20/2000 - G.Juyn * *//* * - added support for JNG * *//* * 0.5.2 - 05/31/2000 - G.Juyn * *//* * - fixed up punctuation (contribution by Tim Rowley) * *//* * * *//* * 0.5.3 - 06/16/2000 - G.Juyn * *//* * - changed progressive-display processing * *//* * * *//* * 0.9.1 - 07/08/2000 - G.Juyn * *//* * - changed read-processing for improved I/O-suspension * *//* * 0.9.1 - 07/14/2000 - G.Juyn * *//* * - changed EOF processing behavior * *//* * 0.9.1 - 07/14/2000 - G.Juyn * *//* * - changed default readbuffer size from 1024 to 4200 * *//* * * *//* * 0.9.2 - 07/27/2000 - G.Juyn * *//* * - B110320 - fixed GCC warning about mix-sized pointer math * *//* * 0.9.2 - 07/31/2000 - G.Juyn * *//* * - B110546 - fixed for improperly returning UNEXPECTEDEOF * *//* * 0.9.2 - 08/04/2000 - G.Juyn * *//* * - B111096 - fixed large-buffer read-suspension * *//* * 0.9.2 - 08/05/2000 - G.Juyn * *//* * - changed file-prefixes * *//* * * *//* * 0.9.3 - 08/26/2000 - G.Juyn * *//* * - added MAGN chunk * *//* * 0.9.3 - 10/11/2000 - G.Juyn * *//* * - removed test-MaGN * *//* * 0.9.3 - 10/16/2000 - G.Juyn * *//* * - added support for JDAA * *//* * * *//* * 0.9.5 - 01/23/2001 - G.Juyn * *//* * - fixed timing-problem with switching framing_modes * *//* * * *//* * 1.0.4 - 06/22/2002 - G.Juyn * *//* * - B495443 - incorrect suspend check in read_databuffer * *//* * * *//* * 1.0.5 - 07/04/2002 - G.Juyn * *//* * - added errorcode for extreme chunk-sizes * *//* * 1.0.5 - 07/08/2002 - G.Juyn * *//* * - B578572 - removed eMNGma hack (thanks Dimitri!) * *//* * 1.0.5 - 07/16/2002 - G.Juyn * *//* * - B581625 - large chunks fail with suspension reads * *//* * 1.0.5 - 08/19/2002 - G.Juyn * *//* * - B597134 - libmng pollutes the linker namespace * *//* * - added HLAPI function to copy chunks * *//* * 1.0.5 - 09/16/2002 - G.Juyn * *//* * - added event handling for dynamic MNG * *//* * * *//* * 1.0.6 - 05/25/2003 - G.R-P * *//* * - added MNG_SKIPCHUNK_cHNK footprint optimizations * *//* * 1.0.6 - 07/07/2003 - G.R-P * *//* * - added MNG_NO_DELTA_PNG reduction * *//* * - skip additional code when MNG_INCLUDE_JNG is not enabled * *//* * 1.0.6 - 07/29/2003 - G.R-P * *//* * - added conditionals around PAST chunk support * *//* * 1.0.6 - 08/17/2003 - G.R-P * *//* * - added conditionals around non-VLC chunk support * *//* * * *//* * 1.0.7 - 03/10/2004 - G.R-P * *//* * - added conditionals around openstream/closestream * *//* * * *//* * 1.0.8 - 04/08/2004 - G.Juyn * *//* * - added CRC existence & checking flags * *//* * 1.0.8 - 04/11/2004 - G.Juyn * *//* * - added data-push mechanisms for specialized decoders * *//* * 1.0.8 - 07/06/2004 - G.R-P * *//* * - defend against using undefined closestream function * *//* * 1.0.8 - 07/28/2004 - G.R-P * *//* * - added check for extreme chunk-lengths * *//* * * *//* * 1.0.9 - 09/16/2004 - G.Juyn * *//* * - fixed chunk pushing mechanism * *//* * 1.0.9 - 12/05/2004 - G.Juyn * *//* * - added conditional MNG_OPTIMIZE_CHUNKINITFREE * *//* * 1.0.9 - 12/06/2004 - G.Juyn * *//* * - added conditional MNG_OPTIMIZE_CHUNKASSIGN * *//* * - added conditional MNG_OPTIMIZE_CHUNKREADER * *//* * 1.0.9 - 12/20/2004 - G.Juyn * *//* * - cleaned up macro-invocations (thanks to D. Airlie) * *//* * 1.0.9 - 12/31/2004 - G.R-P * *//* * - removed stray characters from #ifdef directive * *//* * * *//* ************************************************************************** */#include "libmng.h"#include "libmng_data.h"#include "libmng_error.h"#include "libmng_trace.h"#ifdef __BORLANDC__#pragma hdrstop#endif#include "libmng_memory.h"#include "libmng_objects.h"#include "libmng_object_prc.h"#include "libmng_chunks.h"#ifdef MNG_OPTIMIZE_CHUNKREADER#include "libmng_chunk_descr.h"#endif#include "libmng_chunk_prc.h"#include "libmng_chunk_io.h"#include "libmng_display.h"#include "libmng_read.h"#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)#pragma option -A /* force ANSI-C */#endif/* ************************************************************************** */#ifdef MNG_INCLUDE_READ_PROCS/* ************************************************************************** */mng_retcode mng_process_eof (mng_datap pData){ if (!pData->bEOF) /* haven't closed the stream yet ? */ { pData->bEOF = MNG_TRUE; /* now we do! */#ifndef MNG_NO_OPEN_CLOSE_STREAM if (pData->fClosestream && !pData->fClosestream ((mng_handle)pData)) { MNG_ERROR (pData, MNG_APPIOERROR); }#endif } return MNG_NOERROR;}/* ************************************************************************** */mng_retcode mng_release_pushdata (mng_datap pData){ mng_pushdatap pFirst = pData->pFirstpushdata; mng_pushdatap pNext = pFirst->pNext;#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_RELEASE_PUSHDATA, MNG_LC_START);#endif pData->pFirstpushdata = pNext; /* next becomes the first */ if (!pNext) /* no next? => no last! */ pData->pLastpushdata = MNG_NULL; /* buffer owned and release callback defined? */ if ((pFirst->bOwned) && (pData->fReleasedata)) pData->fReleasedata ((mng_handle)pData, pFirst->pData, pFirst->iLength); else /* otherwise use internal free mechanism */ MNG_FREEX (pData, pFirst->pData, pFirst->iLength); /* and free it */ MNG_FREEX (pData, pFirst, sizeof(mng_pushdata));#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_RELEASE_PUSHDATA, MNG_LC_END);#endif return MNG_NOERROR;}/* ************************************************************************** */mng_retcode mng_release_pushchunk (mng_datap pData){ mng_pushdatap pFirst = pData->pFirstpushchunk; mng_pushdatap pNext = pFirst->pNext;#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_RELEASE_PUSHCHUNK, MNG_LC_START);#endif pData->pFirstpushchunk = pNext; /* next becomes the first */ if (!pNext) /* no next? => no last! */ pData->pLastpushchunk = MNG_NULL; /* buffer owned and release callback defined? */ if ((pFirst->bOwned) && (pData->fReleasedata)) pData->fReleasedata ((mng_handle)pData, pFirst->pData, pFirst->iLength); else /* otherwise use internal free mechanism */ MNG_FREEX (pData, pFirst->pData, pFirst->iLength); /* and free it */ MNG_FREEX (pData, pFirst, sizeof(mng_pushdata));#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_RELEASE_PUSHCHUNK, MNG_LC_END);#endif return MNG_NOERROR;}/* ************************************************************************** */MNG_LOCAL mng_retcode read_data (mng_datap pData, mng_uint8p pBuf, mng_uint32 iSize, mng_uint32 * iRead){ mng_retcode iRetcode; mng_uint32 iTempsize = iSize; mng_uint8p pTempbuf = pBuf; mng_pushdatap pPush = pData->pFirstpushdata; mng_uint32 iPushsize = 0; *iRead = 0; /* nothing yet */#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_READ_DATA, MNG_LC_START);#endif while (pPush) /* calculate size of pushed data */ { iPushsize += pPush->iRemaining; pPush = pPush->pNext; } if (iTempsize <= iPushsize) /* got enough push data? */ { while (iTempsize) { pPush = pData->pFirstpushdata; /* enough data remaining in this buffer? */ if (pPush->iRemaining <= iTempsize) { /* no: then copy what we've got */ MNG_COPY (pTempbuf, pPush->pDatanext, pPush->iRemaining); /* move pointers & lengths */ pTempbuf += pPush->iRemaining; *iRead += pPush->iRemaining; iTempsize -= pPush->iRemaining; /* release the depleted buffer */ iRetcode = mng_release_pushdata (pData); if (iRetcode) return iRetcode; } else { /* copy the needed bytes */ MNG_COPY (pTempbuf, pPush->pDatanext, iTempsize); /* move pointers & lengths */ pPush->iRemaining -= iTempsize; pPush->pDatanext += iTempsize; pTempbuf += iTempsize; *iRead += iTempsize; iTempsize = 0; /* all done!!! */ } } } else { mng_uint32 iTempread = 0; /* get it from the app then */ if (!pData->fReaddata (((mng_handle)pData), pTempbuf, iTempsize, &iTempread)) MNG_ERROR (pData, MNG_APPIOERROR); *iRead += iTempread; }#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_READ_DATA, MNG_LC_END);#endif return MNG_NOERROR;}/* ************************************************************************** */MNG_LOCAL mng_retcode read_databuffer (mng_datap pData, mng_uint8p pBuf, mng_uint8p * pBufnext, mng_uint32 iSize, mng_uint32 * iRead){ mng_retcode iRetcode; #ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_READ_DATABUFFER, MNG_LC_START);#endif if (pData->bSuspensionmode) { mng_uint8p pTemp; mng_uint32 iTemp; *iRead = 0; /* let's be negative about the outcome */ if (!pData->pSuspendbuf) /* need to create a suspension buffer ? */ { pData->iSuspendbufsize = MNG_SUSPENDBUFFERSIZE; /* so, create it */ MNG_ALLOC (pData, pData->pSuspendbuf, pData->iSuspendbufsize); pData->iSuspendbufleft = 0; /* make sure to fill it first time */ pData->pSuspendbufnext = pData->pSuspendbuf; } /* more than our buffer can hold ? */ if (iSize > pData->iSuspendbufsize) { mng_uint32 iRemain; if (!*pBufnext) /* first time ? */ { if (pData->iSuspendbufleft) /* do we have some data left ? */ { /* then copy it */ MNG_COPY (pBuf, pData->pSuspendbufnext, pData->iSuspendbufleft); /* fixup variables */ *pBufnext = pBuf + pData->iSuspendbufleft; pData->pSuspendbufnext = pData->pSuspendbuf; pData->iSuspendbufleft = 0; } else { *pBufnext = pBuf; } } /* calculate how much to get */ iRemain = iSize - (mng_uint32)(*pBufnext - pBuf); /* let's go get it */ iRetcode = read_data (pData, *pBufnext, iRemain, &iTemp); if (iRetcode)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -