Click here to Skip to main content
15,895,011 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::TBits

    @(#) $Id: TBits.cpp,v 1.2 2004/01/01 20:05:54 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 "TBits.h"

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

namespace AxPipe {
    namespace Stock {

        // Instantiate explicitly to generate code for supported bit-lengths.
        template TBits<128>;                ///< 128-bit instantiation of bit-block
        template TBits<160>;                ///< 160-bit instantiation of bit-block
        template TBits<256>;                ///< 256-bit instantiation of bit-block

        /// Zero-initialize
        /// the Bit buffer
        template<int iBits> TBits<iBits>::TBits() {
	        ZeroMemory(m_Bits, sizeof m_Bits);
        }

        /// If shorter, pad on the right, i.e. with the Least
        /// Signifcant Bytes (sic!)
        /// \param bpInit Pointer to a sequence of bytes to initalize with
        /// \param cb Number of bytes in the init sequence (to use)
        template<int iBits> TBits<iBits>::TBits(unsigned char *bpInit, int cb) {
	        ZeroMemory(m_Bits, sizeof m_Bits);
            CopyMemory(m_Bits, bpInit, cb < sizeof m_Bits ? cb : sizeof m_Bits);
        }

        /// Get the left-most n bits. This is actually sort of a dummy function,
        /// as we return a pointer, and the left-most always start at the beginning
        /// of the byte buffer where the bits are.
        /// \param n The number of bits to get.
        /// \return A pointer to the left-most n bits of the buffer.
        template<int iBits> void *
        TBits<iBits>::GetLeft(const int n) {
            return &m_Bits;
        }

        /// Get the right-most n bits. Assume that this is a multiple by 8 value,
        /// otherwise the function fails silently.
        /// \return A pointer to the right-most n bits of the buffer.
        template<int iBits> void *
        TBits<iBits>::GetRight(const int n) {
            return &m_Bits[sizeof m_Bits - n/8];
        }

        /// Arbitrary precision add, endian independent, inefficient...
        /// Note that the byte arrays is presumed to be stored big-endian!
        /// \param rhs A TBits
        /// \return A reference to the destination result, as it should.
        template<int iBits> TBits<iBits>&
        TBits<iBits>::operator += (const TBits<iBits>& rhs) {
            bool bCarry = false;
            for (int i = sizeof m_Bits; i >= 0; i--) {
                bCarry = (m_Bits[i] += rhs.m_Bits[i] + (unsigned char)bCarry) < rhs.m_Bits[i];
            }
            return *this;
        }

        /// Arbitrary precision XOR, inefficient. Don't use if you're in a hurry.
        /// \param rhs A TBits
        /// \return A reference to the destination result, as it should.
        template<int iBits> TBits<iBits>&
        TBits<iBits>::operator ^=(const TBits<iBits>& rhs) {
            size_t cb = sizeof m_Bits;
            unsigned char *dst = m_Bits;
            const unsigned char *src = rhs.m_Bits;
            while (cb--) {
                *dst++ ^= *src++;
            }
            return *this;
        }

        /// Copy-Assign
        /// \param rhs A TBits
        /// \return A reference to the destination result, as it should.
        template<int iBits> TBits<iBits>&
        TBits<iBits>::operator =(const TBits<iBits>& rhs) {
            CopyMemory(&m_Bits, &rhs.m_Bits, sizeof m_Bits);
            return *this;
        }
    }
}

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