📄 libmng_chunk_io.c
字号:
mng_int32 iLen){#ifdef MNG_USE_ZLIB_CRC return crc32 (iCrc, pBuf, iLen);#else mng_uint32 iC = iCrc; mng_int32 iN; if (!pData->bCRCcomputed) make_crc_table (pData); for (iN = 0; iN < iLen; iN++) iC = pData->aCRCtable [(iC ^ pBuf [iN]) & 0xff] ^ (iC >> 8); return iC;#endif}/* Return the CRC of the bytes buf[0..len-1]. */mng_uint32 mng_crc (mng_datap pData, mng_uint8p pBuf, mng_int32 iLen){#ifdef MNG_USE_ZLIB_CRC return update_crc (pData, 0, pBuf, iLen);#else return update_crc (pData, 0xffffffffU, pBuf, iLen) ^ 0xffffffffU;#endif}/* ************************************************************************** *//* * * *//* * Routines for swapping byte-order from and to graphic files * *//* * (This code is adapted from the libpng package) * *//* * * *//* ************************************************************************** */#ifndef MNG_BIGENDIAN_SUPPORTED/* ************************************************************************** */mng_uint32 mng_get_uint32 (mng_uint8p pBuf){ mng_uint32 i = ((mng_uint32)(*pBuf) << 24) + ((mng_uint32)(*(pBuf + 1)) << 16) + ((mng_uint32)(*(pBuf + 2)) << 8) + (mng_uint32)(*(pBuf + 3)); return (i);}/* ************************************************************************** */mng_int32 mng_get_int32 (mng_uint8p pBuf){ mng_int32 i = ((mng_int32)(*pBuf) << 24) + ((mng_int32)(*(pBuf + 1)) << 16) + ((mng_int32)(*(pBuf + 2)) << 8) + (mng_int32)(*(pBuf + 3)); return (i);}/* ************************************************************************** */mng_uint16 mng_get_uint16 (mng_uint8p pBuf){ mng_uint16 i = (mng_uint16)(((mng_uint16)(*pBuf) << 8) + (mng_uint16)(*(pBuf + 1))); return (i);}/* ************************************************************************** */void mng_put_uint32 (mng_uint8p pBuf, mng_uint32 i){ *pBuf = (mng_uint8)((i >> 24) & 0xff); *(pBuf+1) = (mng_uint8)((i >> 16) & 0xff); *(pBuf+2) = (mng_uint8)((i >> 8) & 0xff); *(pBuf+3) = (mng_uint8)(i & 0xff);}/* ************************************************************************** */void mng_put_int32 (mng_uint8p pBuf, mng_int32 i){ *pBuf = (mng_uint8)((i >> 24) & 0xff); *(pBuf+1) = (mng_uint8)((i >> 16) & 0xff); *(pBuf+2) = (mng_uint8)((i >> 8) & 0xff); *(pBuf+3) = (mng_uint8)(i & 0xff);}/* ************************************************************************** */void mng_put_uint16 (mng_uint8p pBuf, mng_uint16 i){ *pBuf = (mng_uint8)((i >> 8) & 0xff); *(pBuf+1) = (mng_uint8)(i & 0xff);}/* ************************************************************************** */#endif /* !MNG_BIGENDIAN_SUPPORTED *//* ************************************************************************** *//* * * *//* * Helper routines to simplify chunk-data extraction * *//* * * *//* ************************************************************************** */#ifdef MNG_INCLUDE_READ_PROCS/* ************************************************************************** */#ifndef MNG_OPTIMIZE_CHUNKREADERMNG_LOCAL mng_uint8p find_null (mng_uint8p pIn){ mng_uint8p pOut = pIn; while (*pOut) /* the read_graphic routine has made sure there's */ pOut++; /* always at least 1 zero-byte in the buffer */ return pOut;}#endif/* ************************************************************************** */#if !defined(MNG_SKIPCHUNK_iCCP) || !defined(MNG_SKIPCHUNK_zTXt) || !defined(MNG_SKIPCHUNK_iTXt)mng_retcode mng_inflate_buffer (mng_datap pData, mng_uint8p pInbuf, mng_uint32 iInsize, mng_uint8p *pOutbuf, mng_uint32 *iOutsize, mng_uint32 *iRealsize){ mng_retcode iRetcode = MNG_NOERROR;#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_INFLATE_BUFFER, MNG_LC_START);#endif if (iInsize) /* anything to do ? */ { *iOutsize = iInsize * 3; /* estimate uncompressed size */ /* and allocate a temporary buffer */ MNG_ALLOC (pData, *pOutbuf, *iOutsize); do { mngzlib_inflateinit (pData); /* initialize zlib */ /* let zlib know where to store the output */ pData->sZlib.next_out = *pOutbuf; /* "size - 1" so we've got space for the zero-termination of a possible string */ pData->sZlib.avail_out = *iOutsize - 1; /* ok; let's inflate... */ iRetcode = mngzlib_inflatedata (pData, iInsize, pInbuf); /* determine actual output size */ *iRealsize = (mng_uint32)pData->sZlib.total_out; mngzlib_inflatefree (pData); /* zlib's done */ if (iRetcode == MNG_BUFOVERFLOW) /* not enough space ? */ { /* then get some more */ MNG_FREEX (pData, *pOutbuf, *iOutsize); *iOutsize = *iOutsize + iInsize; MNG_ALLOC (pData, *pOutbuf, *iOutsize); } } /* repeat if we didn't have enough space */ while ((iRetcode == MNG_BUFOVERFLOW) && (*iOutsize < 20 * iInsize)); if (!iRetcode) /* if oke ? */ *((*pOutbuf) + *iRealsize) = 0; /* then put terminator zero */ } else { *pOutbuf = 0; /* nothing to do; then there's no output */ *iOutsize = 0; *iRealsize = 0; }#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_INFLATE_BUFFER, MNG_LC_END);#endif return iRetcode;}#endif/* ************************************************************************** */#endif /* MNG_INCLUDE_READ_PROCS *//* ************************************************************************** *//* * * *//* * Helper routines to simplify chunk writing * *//* * * *//* ************************************************************************** */#ifdef MNG_INCLUDE_WRITE_PROCS/* ************************************************************************** */#if !defined(MNG_SKIPCHUNK_iCCP) || !defined(MNG_SKIPCHUNK_zTXt) || !defined(MNG_SKIPCHUNK_iTXt)MNG_LOCAL mng_retcode deflate_buffer (mng_datap pData, mng_uint8p pInbuf, mng_uint32 iInsize, mng_uint8p *pOutbuf, mng_uint32 *iOutsize, mng_uint32 *iRealsize){ mng_retcode iRetcode = MNG_NOERROR;#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_DEFLATE_BUFFER, MNG_LC_START);#endif if (iInsize) /* anything to do ? */ { *iOutsize = (iInsize * 5) >> 2; /* estimate compressed size */ /* and allocate a temporary buffer */ MNG_ALLOC (pData, *pOutbuf, *iOutsize); do { mngzlib_deflateinit (pData); /* initialize zlib */ /* let zlib know where to store the output */ pData->sZlib.next_out = *pOutbuf; pData->sZlib.avail_out = *iOutsize; /* ok; let's deflate... */ iRetcode = mngzlib_deflatedata (pData, iInsize, pInbuf); /* determine actual output size */ *iRealsize = pData->sZlib.total_out; mngzlib_deflatefree (pData); /* zlib's done */ if (iRetcode == MNG_BUFOVERFLOW) /* not enough space ? */ { /* then get some more */ MNG_FREEX (pData, *pOutbuf, *iOutsize); *iOutsize = *iOutsize + (iInsize >> 1); MNG_ALLOC (pData, *pOutbuf, *iOutsize); } } /* repeat if we didn't have enough space */ while (iRetcode == MNG_BUFOVERFLOW); } else { *pOutbuf = 0; /* nothing to do; then there's no output */ *iOutsize = 0; *iRealsize = 0; }#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_DEFLATE_BUFFER, MNG_LC_END);#endif return iRetcode;}#endif/* ************************************************************************** */MNG_LOCAL mng_retcode write_raw_chunk (mng_datap pData, mng_chunkid iChunkname, mng_uint32 iRawlen, mng_uint8p pRawdata){ mng_uint32 iCrc; mng_uint32 iWritten;#ifdef MNG_SUPPORT_TRACE MNG_TRACE (pData, MNG_FN_WRITE_RAW_CHUNK, MNG_LC_START);#endif /* temporary buffer ? */ if ((pRawdata != 0) && (pRawdata != pData->pWritebuf+8)) { /* store length & chunktype in default buffer */ mng_put_uint32 (pData->pWritebuf, iRawlen); mng_put_uint32 (pData->pWritebuf+4, (mng_uint32)iChunkname); if (pData->iCrcmode & MNG_CRC_OUTPUT) { if ((pData->iCrcmode & MNG_CRC_OUTPUT) == MNG_CRC_OUTPUT_GENERATE) { /* calculate the crc */ iCrc = update_crc (pData, 0xffffffffL, pData->pWritebuf+4, 4); iCrc = update_crc (pData, iCrc, pRawdata, iRawlen) ^ 0xffffffffL; } else { iCrc = 0; /* dummy crc */ } /* store in default buffer */ mng_put_uint32 (pData->pWritebuf+8, iCrc); } /* write the length & chunktype */ if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf, 8, &iWritten)) MNG_ERROR (pData, MNG_APPIOERROR); if (iWritten != 8) /* disk full ? */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -