Chris@16: #ifndef BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP Chris@16: #define BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // transform_width.hpp Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: // iterator which takes elements of x bits and returns elements of y bits. Chris@16: // used to change streams of 8 bit characters into streams of 6 bit characters. Chris@16: // and vice-versa for implementing base64 encodeing/decoding. Be very careful Chris@16: // when using and end iterator. end is only reliable detected when the input Chris@16: // stream length is some common multiple of x and y. E.G. Base64 6 bit Chris@16: // character and 8 bit bytes. Lowest common multiple is 24 => 4 6 bit characters Chris@16: // or 3 8 bit characters Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@101: #include // std::min Chris@101: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: namespace iterators { Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // class used by text archives to translate char strings to wchar_t Chris@16: // strings of the currently selected locale Chris@16: template< Chris@16: class Base, Chris@16: int BitsOut, Chris@16: int BitsIn, Chris@101: class CharType = typename boost::iterator_value::type // output character Chris@16: > Chris@16: class transform_width : Chris@16: public boost::iterator_adaptor< Chris@16: transform_width, Chris@16: Base, Chris@16: CharType, Chris@16: single_pass_traversal_tag, Chris@16: CharType Chris@16: > Chris@16: { Chris@16: friend class boost::iterator_core_access; Chris@101: typedef typename boost::iterator_adaptor< Chris@16: transform_width, Chris@16: Base, Chris@16: CharType, Chris@16: single_pass_traversal_tag, Chris@16: CharType Chris@16: > super_t; Chris@16: Chris@16: typedef transform_width this_t; Chris@101: typedef typename iterator_value::type base_value_type; Chris@16: Chris@16: void fill(); Chris@16: Chris@16: CharType dereference() const { Chris@16: if(!m_buffer_out_full) Chris@16: const_cast(this)->fill(); Chris@16: return m_buffer_out; Chris@16: } Chris@16: Chris@16: bool equal_impl(const this_t & rhs){ Chris@16: if(BitsIn < BitsOut) // discard any left over bits Chris@16: return this->base_reference() == rhs.base_reference(); Chris@16: else{ Chris@16: // BitsIn > BitsOut // zero fill Chris@16: if(this->base_reference() == rhs.base_reference()){ Chris@16: m_end_of_sequence = true; Chris@16: return 0 == m_remaining_bits; Chris@16: } Chris@16: return false; Chris@16: } Chris@16: } Chris@16: Chris@16: // standard iterator interface Chris@16: bool equal(const this_t & rhs) const { Chris@16: return const_cast(this)->equal_impl(rhs); Chris@16: } Chris@16: Chris@16: void increment(){ Chris@16: m_buffer_out_full = false; Chris@16: } Chris@16: Chris@16: bool m_buffer_out_full; Chris@16: CharType m_buffer_out; Chris@16: Chris@16: // last read element from input Chris@16: base_value_type m_buffer_in; Chris@16: Chris@16: // number of bits to left in the input buffer. Chris@16: unsigned int m_remaining_bits; Chris@16: Chris@16: // flag to indicate we've reached end of data. Chris@16: bool m_end_of_sequence; Chris@16: Chris@16: public: Chris@16: // make composible buy using templated constructor Chris@16: template Chris@16: transform_width(BOOST_PFTO_WRAPPER(T) start) : Chris@16: super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))), Chris@16: m_buffer_out_full(false), Chris@101: // To disable GCC warning, but not truly necessary Chris@101: //(m_buffer_in will be initialized later before being Chris@101: //used because m_remaining_bits == 0) Chris@101: m_buffer_in(0), Chris@16: m_remaining_bits(0), Chris@16: m_end_of_sequence(false) Chris@16: {} Chris@16: // intel 7.1 doesn't like default copy constructor Chris@16: transform_width(const transform_width & rhs) : Chris@16: super_t(rhs.base_reference()), Chris@16: m_buffer_out_full(rhs.m_buffer_out_full), Chris@101: m_buffer_out(rhs.m_buffer_out), Chris@101: m_buffer_in(rhs.m_buffer_in), Chris@16: m_remaining_bits(rhs.m_remaining_bits), Chris@16: m_end_of_sequence(false) Chris@16: {} Chris@16: }; Chris@16: Chris@16: template< Chris@16: class Base, Chris@16: int BitsOut, Chris@16: int BitsIn, Chris@16: class CharType Chris@16: > Chris@16: void transform_width::fill() { Chris@16: unsigned int missing_bits = BitsOut; Chris@16: m_buffer_out = 0; Chris@16: do{ Chris@16: if(0 == m_remaining_bits){ Chris@16: if(m_end_of_sequence){ Chris@16: m_buffer_in = 0; Chris@16: m_remaining_bits = missing_bits; Chris@16: } Chris@16: else{ Chris@16: m_buffer_in = * this->base_reference()++; Chris@16: m_remaining_bits = BitsIn; Chris@16: } Chris@16: } Chris@16: Chris@16: // append these bits to the next output Chris@16: // up to the size of the output Chris@16: unsigned int i = std::min(missing_bits, m_remaining_bits); Chris@16: // shift interesting bits to least significant position Chris@16: base_value_type j = m_buffer_in >> (m_remaining_bits - i); Chris@16: // and mask off the un interesting higher bits Chris@16: // note presumption of twos complement notation Chris@16: j &= (1 << i) - 1; Chris@16: // append then interesting bits to the output value Chris@16: m_buffer_out <<= i; Chris@16: m_buffer_out |= j; Chris@16: Chris@16: // and update counters Chris@16: missing_bits -= i; Chris@16: m_remaining_bits -= i; Chris@16: }while(0 < missing_bits); Chris@16: m_buffer_out_full = true; Chris@16: } Chris@16: Chris@16: } // namespace iterators Chris@16: } // namespace archive Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP