Deflater.inl
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:5k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Copyright (C) 1999 DXGuide. All Rights Reserved.
- // File: Deflater.inl
- // Inlines for CDeflater
- // Index within the heap array of least frequent node in the Huffman tree
- #define SMALLEST 1
- // Remove the smallest element from the heap and recreate the
- // heap with one less element. Updates heap and m_nHeapLen.
- inline void CDeflater::pqremove(ct_data* pTree, int& top)
- {
- top = m_aHuffmanHeap[SMALLEST];
- m_aHuffmanHeap[SMALLEST] = m_aHuffmanHeap[m_nHeapLen --];
- pqdownheap(pTree, SMALLEST);
- }
- // Send a code of the given tree. c and tree must not have side effects
- inline void CDeflater::TreeSendCode(int c, const ct_data* pTree)
- {
- TRACE1("ncd %3d ", c);
- TreeSendBits(pTree[c].m_wCode, pTree[c].m_wLen);
- }
- // In order to simplify the code, particularly on 16 bit
- // machines, match distances are limited to MAX_DIST
- // instead of WSIZE.
- inline UINT CDeflater::MAX_DIST(void)
- {
- return m_uLZ77WindowSize - MIN_LOOKAHEAD;
- }
- // Update a hash value with the given input byte
- // IN assertion: all calls to to UpdateHash are made with
- // consecutive input characters, so that a running hash key
- // can be computed from the previous key instead of complete
- // recalculation each time.
- inline void CDeflater::UpdateHash(BYTE c)
- {
- m_uInsHashIndex = ((m_uInsHashIndex << m_uHashShift) ^ c) & (m_uHashSize - 1);
- }
- // Insert string str in the dictionary and set uMatchHead to
- // the previous head of the hash chain (the most recent string
- // with same hash key). Return the previous length of the hash
- // chain. If this file is compiled with -DFASTEST, the
- // compression level is forced to 1, and no hash chains are
- // maintained.
- // IN assertion: all calls to to InsertString are made with
- // consecutive input characters and the first MIN_MATCH bytes
- // of str are valid (except for the last MIN_MATCH - 1 bytes
- // of the input file).
- inline Pos CDeflater::InsertString(UINT uStrStart)
- {
- UpdateHash(m_pSlidingWindow[uStrStart + (MIN_MATCH - 1)]);
- Pos wMatchHeadPos =
- #ifndef FASTEST
- m_pPrevStringPos[uStrStart & m_uLZ77WindowMask] =
- #endif // !FASTEST
- m_pHashChainsHeadPos[m_uInsHashIndex];
- m_pHashChainsHeadPos[m_uInsHashIndex] = Pos(uStrStart);
- return wMatchHeadPos;
- }
- // Put a short in the pending buffer. The 16-bit value is put
- // in MSB order.
- // IN assertion: the stream state is correct and there is
- // enough room in m_pPendingBuf.
- inline void CDeflater::PutShortMSB(WORD w)
- {
- m_pPendingBuf[m_uPendingBytes ++] = (BYTE)(w >> 8);
- m_pPendingBuf[m_uPendingBytes ++] = (BYTE)(w & 0xFF);
- }
- // Output a short LSB first on the stream.
- // IN assertion: there is enough room in pendingBuf.
- inline void CDeflater::PutShortLSB(WORD w)
- {
- m_pPendingBuf[m_uPendingBytes ++] = (BYTE)(w & 0xFF);
- m_pPendingBuf[m_uPendingBytes ++] = (BYTE)(w >> 8);
- }
- // Mapping from a distance to a distance code. dist is the
- // distance - 1 and must not have side effects.
- // _dist_code[256] and _dist_code[257] are never used.
- inline BYTE CDeflater::GetDistCode(UINT uDist)
- {
- return (uDist < 256) ? _dist_code[uDist] : _dist_code[256 + (uDist >> 7)];
- }
- // Flush the current block, with given end-of-file flag.
- // IN assertion: m_uStrStart is set to the end of the current match.
- inline void CDeflater::FlushBlock(bool bEof)
- {
- TreeFlushBlock(
- (m_lBlockStartPos >= 0L ?
- &m_pSlidingWindow[m_lBlockStartPos] : NULL),
- (DWORD)((long)m_uStrStart - m_lBlockStartPos),
- bEof);
- m_lBlockStartPos = m_uStrStart;
- FlushPending();
- TRACE0("[FLUSH]n");
- }
- inline bool CDeflater::TreeTallyLit(BYTE c)
- {
- TRACE1("%c", c);
- #ifndef _DEBUG
- m_pwDistBuf[m_uLastLitLenBufIndex] = 0;
- m_pLitLenBuf[m_uLastLitLenBufIndex ++] = c;
- m_aDynLiterTree[c].m_wFreq ++;
- return (m_uLastLitLenBufIndex == m_uLitLenBufSize - 1);
- #else // _DEBUG
- return TreeTally(0, c);
- #endif // !_DEBUG
- }
- inline bool CDeflater::TreeTallyDist(WORD wDist, BYTE byteLen)
- {
- #ifndef _DEBUG
- m_pwDistBuf[m_uLastLitLenBufIndex] = wDist;
- m_pLitLenBuf[m_uLastLitLenBufIndex++] = byteLen;
- wDist --;
- m_aDynLiterTree[_length_code[byteLen] + LITERALS + 1].m_wFreq++;
- m_aDynDistTree[GetDistCode(wDist)].m_wFreq++;
- return (m_uLastLitLenBufIndex == m_uLitLenBufSize - 1);
- #else // _DEBUG
- return TreeTally(wDist, byteLen);
- #endif // !_DEBUG
- }
- inline DWORD CDeflater::GetTotalIn(void) const
- {
- return m_dwTotalInputBytes;
- }
- inline DWORD CDeflater::GetTotalOut(void) const
- {
- return m_dwTotalOutputBytes;
- }
- // Returns the CRC-32 checksum of the uncompressed entry data, or -1 if not known.
- inline DWORD CDeflater::GetAdler32(void) const
- {
- return m_checksum.GetValue();
- }
- // Sets input data for compression. This should be called
- // whenever NeedsInput() returns true indicating that more
- // input data is required.
- inline void CDeflater::SetInput(const void* pBuf, UINT uLen)
- {
- ASSERT(pBuf != NULL && uLen > 0);
- m_pSourceBuf = (const BYTE*)pBuf;
- m_uSourceBufLen = uLen;
- }
- // Returns true if the input data buffer is empty and
- // setInput() should be called in order to provide more input.
- inline bool CDeflater::NeedsInput(void) const
- {
- return (m_uSourceBufLen == 0);
- }