/******************************************************************************* ; ; MODULE AttoZ.c ; ; DESCRIPTION Wrapper module for code decompression with zlib. ; ; THIS PROGRAM AND THE INFORMATION CONTAINED HEREIN IS THE PROPERTY OF ; ATTO TECHNOLOGY, INC. AND SHALL NOT BE REPRODUCED, COPIED, OR USED IN ; WHOLE OR IN PART OTHER THAN AS PROVIDED FOR IN THE LICENSE AGREEMENT ; PURSUANT TO WHICH IT WAS FURNISHED. ; ; COPYRIGHT (c) ATTO TECHNOLOGY, INC. 2005-2010 ; ALL RIGHTS RESERVED. ; ; *******************************************************************************/ /* #defines which affect #include */ /* #include section */ #include "zlib.h" #include "memory.h" /* externs section */ /* globals section */ /* typedef section */ typedef PBYTE *PPBYTE; /* local structs & unions section */ /* local (static) function prototypes section */ static PVOID AttoZAlloc(PVOID pOpaque, DWORD dwItems, DWORD dwSize); static void AttoZFree(PVOID pOpaque, PVOID pAddress); /* local data */ static DWORD adwBuf[32768]; /* decompression buffer to prevent 'the bends' */ /* enums & #defines */ /******************************************************************************* ; ; FUNCTION AttoZInflateCode ; ; RESPONSIBILITY Decompress code or data section with zlib. ; ; PARAMETERS Name I/O Description ; pDest I Ptr to destination. ; pSrc I Ptr to source. ; dwSize I Decompressed size of code or data section. ; dwCompSize I Compressed size of code or data section. ; ; RETURNS void ; ; NOTES ; *******************************************************************************/ void AttoZInflateCode(PBYTE pDest, PBYTE pSrc, DWORD dwSize, DWORD dwCompSize) { PVOID pBuf = adwBuf; z_stream strm; int ret; /* Allocate inflate state */ strm.zalloc = (PVOID (*)(PVOID, unsigned int, unsigned int))AttoZAlloc; strm.zfree = AttoZFree; strm.opaque = &pBuf; strm.avail_in = dwCompSize; strm.next_in = pSrc; strm.avail_out = dwSize; strm.next_out = pDest; ret = inflateInit(&strm); if (ret != Z_OK) { for (;;) ; } /* Decompress code or data */ ret = inflate(&strm, Z_FINISH); if (ret != Z_STREAM_END) { for (;;) ; } /* Cleanup inflate state */ ret = inflateEnd(&strm); if (ret != Z_OK) { for (;;) ; } } /******************************************************************************* ; ; FUNCTION AttoZAlloc ; ; RESPONSIBILITY Allocate memory for zlib functions. ; ; PARAMETERS Name I/O Description ; pOpaque I Ptr to a byte pointer to available memory. ; dwItems I Number of items to allocate. ; dwSize I Size of item to allocate. ; ; RETURNS Pointer to a chunk of free memory of the requested size. ; ; NOTES ; *******************************************************************************/ static PVOID AttoZAlloc(PVOID pOpaque, DWORD dwItems, DWORD dwSize) { PPBYTE ppBuf = (PPBYTE) pOpaque; PBYTE pAvail = *ppBuf; /* Advance the pointer to the next chunk of available */ /* memory for the next time this function is called. */ *ppBuf += ((dwItems * dwSize) + 0x03) & ~0x03; return pAvail; } /******************************************************************************* ; ; FUNCTION AttoZFree ; ; RESPONSIBILITY Free memory allocated by AttoZAlloc. ; ; PARAMETERS Name I/O Description ; pOpaque I Ptr to a byte pointer to available memory. ; pAddress I Ptr to memory chunk to free. ; ; RETURNS void ; ; NOTES ; *******************************************************************************/ static void AttoZFree(PVOID pOpaque, PVOID pAddress) { /* Do nothing. Uh, yup. */ } /******************************************************************************* ; ; FUNCTION AttoZInflate ; ; RESPONSIBILITY Expand the compressed data in pbySrc into the buffer ; specified by pbyDest. ; ; PARAMETERS Name I/O Description ; pDst I Ptr to buffer to hold inflated data ; pdwDstLen I/O I-pbyDst size, O-Actual inflated data sz ; pSrc I Ptr to compressed data ; dwSrcLen I Size in bytes of the compressed data ; ; RETURNS SUCCESS if data successfully inflated, FAILURE otherwise ; ; NOTES ; *******************************************************************************/ ASTAT AttoZInflate(PVOID pDst, DWORD *pdwDstLen, PVOID pSrc, DWORD dwSrcLen) { int iRet; iRet = uncompress(pDst, pdwDstLen, pSrc, dwSrcLen); if (iRet == Z_OK) { return SUCCESS; } else { #if DEBUG_LEVEL >= 1 DebugLevel1(("iRet:[%i]", iRet)); srlHexDump("Decompress", pSrc, dwSrcLen, srlPrintf); #endif return FAILURE; } } /* AttoZInflate */ /******************************************************************************* ; ; FUNCTION AttoZCompress ; ; RESPONSIBILITY Compress the data in pbySrc into the buffer specified by ; pbyDest. ; ; PARAMETERS Name I/O Description ; pDst I Ptr to buffer to hold compressed data ; pdwDstLen I/O I-pbyDst size, O-Actual compressed data sz ; pSrc I Ptr to data to compress ; dwSrcLen I Size in bytes of the data to compress ; ; RETURNS SUCCESS if data successfully compressed, FAILURE otherwise ; ; NOTES ; *******************************************************************************/ ASTAT AttoZCompress(PVOID pDst, DWORD *pdwDstLen, PVOID pSrc, DWORD dwSrcLen) { int iRet; iRet = compress(pDst, pdwDstLen, pSrc, dwSrcLen); if (iRet == Z_OK) { return SUCCESS; } else { DebugLevel1(("iRet:[%i]", iRet)); return FAILURE; } } /* AttoZCompress */ /******************************************************************************* ; ; FUNCTION zcalloc ; ; RESPONSIBILITY ATTO-specific memory allocation function for zlib. ; ; PARAMETERS Name I/O Description ; opaque I Unused ; items I number of items to allocate ; size I size of each item ; ; RETURNS Ptr to memory region or NULL if unable to allocate ; ; NOTES ; *******************************************************************************/ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) { return (voidpf)ATTOcalloc(items, size); } /* zcalloc */ /******************************************************************************* ; ; FUNCTION zcfree ; ; RESPONSIBILITY ATTO-specific memory free function for zlib. ; ; PARAMETERS Name I/O Description ; opaque I Unused ; ptr I Pointer to ; ; RETURNS nothing ; ; NOTES ; *******************************************************************************/ void zcfree (voidpf opaque, voidpf ptr) { ATTOfree(ptr); } /* zcfree */