| 1 | #ifndef BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP
|
|---|
| 2 | #define BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP
|
|---|
| 3 |
|
|---|
| 4 | // MS compatible compilers support #pragma once
|
|---|
| 5 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
|---|
| 6 | # pragma once
|
|---|
| 7 | #endif
|
|---|
| 8 | #include <xutility>
|
|---|
| 9 | #if defined(_MSC_VER) && (_MSC_VER >= 1800)
|
|---|
| 10 | #include <algorithm>
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| 13 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|---|
| 14 | // transform_width.hpp
|
|---|
| 15 |
|
|---|
| 16 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
|---|
| 17 | // Use, modification and distribution is subject to the Boost Software
|
|---|
| 18 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|---|
| 19 | // http://www.boost.org/LICENSE_1_0.txt)
|
|---|
| 20 |
|
|---|
| 21 | // See http://www.boost.org for updates, documentation, and revision history.
|
|---|
| 22 |
|
|---|
| 23 | // iterator which takes elements of x bits and returns elements of y bits.
|
|---|
| 24 | // used to change streams of 8 bit characters into streams of 6 bit characters.
|
|---|
| 25 | // and vice-versa for implementing base64 encodeing/decoding. Be very careful
|
|---|
| 26 | // when using and end iterator. end is only reliable detected when the input
|
|---|
| 27 | // stream length is some common multiple of x and y. E.G. Base64 6 bit
|
|---|
| 28 | // character and 8 bit bytes. Lowest common multiple is 24 => 4 6 bit characters
|
|---|
| 29 | // or 3 8 bit characters
|
|---|
| 30 |
|
|---|
| 31 | #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME & PTFO
|
|---|
| 32 | #include <boost/serialization/pfto.hpp>
|
|---|
| 33 |
|
|---|
| 34 | #include <boost/iterator/iterator_adaptor.hpp>
|
|---|
| 35 | #include <boost/iterator/iterator_traits.hpp>
|
|---|
| 36 |
|
|---|
| 37 | namespace boost {
|
|---|
| 38 | namespace archive {
|
|---|
| 39 | namespace iterators {
|
|---|
| 40 |
|
|---|
| 41 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|---|
| 42 | // class used by text archives to translate char strings to wchar_t
|
|---|
| 43 | // strings of the currently selected locale
|
|---|
| 44 | template<
|
|---|
| 45 | class Base,
|
|---|
| 46 | int BitsOut,
|
|---|
| 47 | int BitsIn,
|
|---|
| 48 | class CharType = BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type // output character
|
|---|
| 49 | >
|
|---|
| 50 | class transform_width :
|
|---|
| 51 | public boost::iterator_adaptor<
|
|---|
| 52 | transform_width<Base, BitsOut, BitsIn, CharType>,
|
|---|
| 53 | Base,
|
|---|
| 54 | CharType,
|
|---|
| 55 | single_pass_traversal_tag,
|
|---|
| 56 | CharType
|
|---|
| 57 | >
|
|---|
| 58 | {
|
|---|
| 59 | friend class boost::iterator_core_access;
|
|---|
| 60 | typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
|
|---|
| 61 | transform_width<Base, BitsOut, BitsIn, CharType>,
|
|---|
| 62 | Base,
|
|---|
| 63 | CharType,
|
|---|
| 64 | single_pass_traversal_tag,
|
|---|
| 65 | CharType
|
|---|
| 66 | > super_t;
|
|---|
| 67 |
|
|---|
| 68 | typedef transform_width<Base, BitsOut, BitsIn, CharType> this_t;
|
|---|
| 69 | typedef BOOST_DEDUCED_TYPENAME iterator_value<Base>::type base_value_type;
|
|---|
| 70 |
|
|---|
| 71 | void fill();
|
|---|
| 72 |
|
|---|
| 73 | CharType dereference() const {
|
|---|
| 74 | if(!m_buffer_out_full)
|
|---|
| 75 | const_cast<this_t *>(this)->fill();
|
|---|
| 76 | return m_buffer_out;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | bool equal_impl(const this_t & rhs){
|
|---|
| 80 | if(BitsIn < BitsOut) // discard any left over bits
|
|---|
| 81 | return this->base_reference() == rhs.base_reference();
|
|---|
| 82 | else{
|
|---|
| 83 | // BitsIn > BitsOut // zero fill
|
|---|
| 84 | if(this->base_reference() == rhs.base_reference()){
|
|---|
| 85 | m_end_of_sequence = true;
|
|---|
| 86 | return 0 == m_remaining_bits;
|
|---|
| 87 | }
|
|---|
| 88 | return false;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // standard iterator interface
|
|---|
| 93 | bool equal(const this_t & rhs) const {
|
|---|
| 94 | return const_cast<this_t *>(this)->equal_impl(rhs);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void increment(){
|
|---|
| 98 | m_buffer_out_full = false;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | bool m_buffer_out_full;
|
|---|
| 102 | CharType m_buffer_out;
|
|---|
| 103 |
|
|---|
| 104 | // last read element from input
|
|---|
| 105 | base_value_type m_buffer_in;
|
|---|
| 106 |
|
|---|
| 107 | // number of bits to left in the input buffer.
|
|---|
| 108 | unsigned int m_remaining_bits;
|
|---|
| 109 |
|
|---|
| 110 | // flag to indicate we've reached end of data.
|
|---|
| 111 | bool m_end_of_sequence;
|
|---|
| 112 |
|
|---|
| 113 | public:
|
|---|
| 114 | // make composible buy using templated constructor
|
|---|
| 115 | template<class T>
|
|---|
| 116 | transform_width(BOOST_PFTO_WRAPPER(T) start) :
|
|---|
| 117 | super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))),
|
|---|
| 118 | m_buffer_out_full(false),
|
|---|
| 119 | m_remaining_bits(0),
|
|---|
| 120 | m_end_of_sequence(false)
|
|---|
| 121 | {}
|
|---|
| 122 | // intel 7.1 doesn't like default copy constructor
|
|---|
| 123 | transform_width(const transform_width & rhs) :
|
|---|
| 124 | super_t(rhs.base_reference()),
|
|---|
| 125 | m_buffer_out_full(rhs.m_buffer_out_full),
|
|---|
| 126 | m_remaining_bits(rhs.m_remaining_bits),
|
|---|
| 127 | m_buffer_in(rhs.m_buffer_in),
|
|---|
| 128 | m_end_of_sequence(false)
|
|---|
| 129 | {}
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | template<
|
|---|
| 133 | class Base,
|
|---|
| 134 | int BitsOut,
|
|---|
| 135 | int BitsIn,
|
|---|
| 136 | class CharType
|
|---|
| 137 | >
|
|---|
| 138 | void transform_width<Base, BitsOut, BitsIn, CharType>::fill() {
|
|---|
| 139 | unsigned int missing_bits = BitsOut;
|
|---|
| 140 | m_buffer_out = 0;
|
|---|
| 141 | do{
|
|---|
| 142 | if(0 == m_remaining_bits){
|
|---|
| 143 | if(m_end_of_sequence){
|
|---|
| 144 | m_buffer_in = 0;
|
|---|
| 145 | m_remaining_bits = missing_bits;
|
|---|
| 146 | }
|
|---|
| 147 | else{
|
|---|
| 148 | m_buffer_in = * this->base_reference()++;
|
|---|
| 149 | m_remaining_bits = BitsIn;
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | // append these bits to the next output
|
|---|
| 154 | // up to the size of the output
|
|---|
| 155 | unsigned int i = std::min(missing_bits, m_remaining_bits);
|
|---|
| 156 | // shift interesting bits to least significant position
|
|---|
| 157 | base_value_type j = m_buffer_in >> (m_remaining_bits - i);
|
|---|
| 158 | // and mask off the un interesting higher bits
|
|---|
| 159 | // note presumption of twos complement notation
|
|---|
| 160 | j &= (1 << i) - 1;
|
|---|
| 161 | // append then interesting bits to the output value
|
|---|
| 162 | m_buffer_out <<= i;
|
|---|
| 163 | m_buffer_out |= j;
|
|---|
| 164 |
|
|---|
| 165 | // and update counters
|
|---|
| 166 | missing_bits -= i;
|
|---|
| 167 | m_remaining_bits -= i;
|
|---|
| 168 | }while(0 < missing_bits);
|
|---|
| 169 | m_buffer_out_full = true;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | } // namespace iterators
|
|---|
| 173 | } // namespace archive
|
|---|
| 174 | } // namespace boost
|
|---|
| 175 |
|
|---|
| 176 | #endif // BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP
|
|---|