Click here to Skip to main content
15,888,521 members
Articles / Desktop Programming / MFC

AxPipe - Multi-Threaded Binary Stream C++ Class Library

Rate me:
Please Sign up or sign in to vote.
3.71/5 (7 votes)
14 Jan 20042 min read 61.4K   1.1K   17  
A small and efficient collection of classes and templates to create multi-stage multi-threaded data processing pipe lines
/*! \file
    \brief Implementation of AxPipe::Stock::CPipeInflate

    @(#) $Id: CPipeInflate.cpp,v 1.1.1.1 2003/12/17 21:36:58 svante Exp $

    AxPipe - Binary Stream Framework

    Copyright (C) 2003 Svante Seleborg/Axon Data, All rights reserved.

    This program is free software; you can redistribute it and/or modify it under the terms
    of the GNU General Public License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with this program;
    if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA 02111-1307 USA

    The author may be reached at mailto:axpipe@axondata.se and http://axpipe.sourceforge.net

    Why is this framework released as GPL and not LGPL? See http://www.gnu.org/philosophy/why-not-lgpl.html

----
\verbatim
    E-mail                          YYYY-MM-DD              Reason
    axpipe@axondata.se              2003-12-15              Initial
\endverbatim
*/
#include "stdafx.h"
#include "CPipeInflate.h"

// Can't use this for convenient notation below:
// using AxPipe::Stock::CPipeInflate;
// because the Doxygen get's confused.

namespace AxPipe {
    namespace Stock {
        /// Clean up if necessary, should only need
        /// work to be done on error.
        CPipeInflate::~CPipeInflate() {
            if (m_pOutSeg) m_pOutSeg->Release();
        }

        /// Initialize zlib for this inflation.
        /// \return true to continue cascading of Open()
        bool
        CPipeInflate::OutOpen() {
            bool fReturn = CPipe::OutOpen();        // Open base first, like constructor
            m_cb = 0;                               // Total output bytes counter
            ZeroMemory(&m_Zstream, sizeof m_Zstream);
            m_Zstream.next_in = Z_NULL;	            // Defer check to first call to inflate
            m_Zstream.zalloc = Z_NULL;              // Use default alloc()
            m_Zstream.zfree = Z_NULL;               // Use default free()

            if (inflateInit(&m_Zstream) != Z_OK) {
                SetError(ERROR_CODE_STOCK, _T("ZLIB initialization error"));
            }
            m_pOutSeg = NULL;

            return fReturn;                         // Return the saved return code.
        }

        /// Clean up and call base class CPipe::OutClose()
        /// \return true to continue cascading the Open()
        bool
        CPipeInflate::OutClose() {
            // This is a safety first measure, should not really be needed.
            if (m_pOutSeg) {
                m_pOutSeg->Release();
                m_pOutSeg = NULL;
            }
            return CPipe::OutClose();               // End by closing base, like destructor
        }

        /// Accept each segment as it is passed. It's important that you do not
        /// send more data than needed. The compression format is self-terminating,
        /// and there must be no data sent after the last byte of the compressed
        /// stream. If so, an error is set and that data is discarded. The output
        /// is decompressed, but may be sent in multiple segments.
        /// \param pSeg A segment with compressed data, and no trailing if last
        void
        CPipeInflate::Out(AxPipe::CSeg *pSeg) {
            m_Zstream.next_in = (unsigned char *)pSeg->PtrRd();
            m_Zstream.avail_in = (UINT)pSeg->Len();
            while (true) {
                if (!m_pOutSeg) {
                    // Allocate the output segment, and point the Zstream structure to it
                    m_pOutSeg = GetSeg(m_Zstream.avail_in + m_Zstream.avail_in);
                    ASSCHK(m_pOutSeg != NULL, _T(""));
                    m_Zstream.avail_out = (UINT)m_pOutSeg->Size();
                    m_Zstream.next_out = m_pOutSeg->PtrWr();
                }
                int iZerror = inflate(&m_Zstream, 0);
                m_cb += m_Zstream.total_out;        // Update total output bytes ctr
                m_Zstream.total_out = 0;            // can't use total_out since it's 32-bit
                switch (iZerror) {
                case Z_OK:
                    // ZLib guarantees to either use all input or all output buffer.
                    if (m_Zstream.avail_in && m_Zstream.avail_out) {
                        SetError(ERROR_CODE_DERIVED, _T("ZLIB sequence error"));
                    }
                    if (!m_Zstream.avail_out) {
                        Pump(m_pOutSeg);
                        m_pOutSeg = NULL;
                    }
                    if (m_Zstream.avail_in) {
                        continue;                   // More data to inflate!
                    }
                    // If we have no more input, we need to return and wait for more
                    break;
                case Z_STREAM_END:
                    m_pOutSeg->Len(m_pOutSeg->Size() - m_Zstream.avail_out);
                    if (m_pOutSeg->Len()) {
                        Pump(m_pOutSeg);
                    }
                    m_pOutSeg = NULL;
                    if (m_Zstream.avail_in) {
                        SetError(ERROR_CODE_STOCK, _T("Trailing data"));
                    }
                    break;
                default:
                    SetError(ERROR_CODE_STOCK, _T("ZLIB inflate error"));
                    break;
                }
                break;
            }
            pSeg->Release();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer Axantum Software AB
Sweden Sweden
I've been working with all aspects of software development since 1979 - from compiler construction to management. Currently I'm an independent consultant mostly specializing in computer security. Please see my homepage for contact details.

I speak C like a native, and have a pretty good grasp of C++. The most recent five years C# has been the main development language. Traditionally Unix has been the dominating environment, but currently the scales have tipped over to Windows, due to market demands but I'm equally at home developing in both environments.

When I'm not coding I'm usually sitting on one of my 4 bikes, indoors or outdoors, on the road or in the woods.

Comments and Discussions