Chris@16: #ifndef BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP Chris@16: #define BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_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: // binary_from_base64.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: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: 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: // convert base64 characters to binary data Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct to_6_bit { Chris@16: typedef CharType result_type; Chris@16: CharType operator()(CharType t) const{ Chris@16: const signed char lookup_table[] = { Chris@16: -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, Chris@16: -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, Chris@16: -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, Chris@16: 52,53,54,55,56,57,58,59,60,61,-1,-1,-1, 0,-1,-1, // render '=' as 0 Chris@16: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, Chris@16: 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, Chris@16: -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, Chris@16: 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1 Chris@16: }; Chris@16: // metrowerks trips this assertion - how come? Chris@16: #if ! defined(__MWERKS__) Chris@16: BOOST_STATIC_ASSERT(128 == sizeof(lookup_table)); Chris@16: #endif Chris@16: signed char value = -1; Chris@16: if((unsigned)t <= 127) Chris@16: value = lookup_table[(unsigned)t]; Chris@16: if(-1 == value) Chris@16: boost::serialization::throw_exception( Chris@16: dataflow_exception(dataflow_exception::invalid_base64_character) Chris@16: ); Chris@16: return value; Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: // note: what we would like to do is Chris@101: // template Chris@16: // typedef transform_iterator< Chris@16: // from_6_bit, Chris@16: // transform_width Chris@16: // > base64_from_binary; Chris@16: // but C++ won't accept this. Rather than using a "type generator" and Chris@16: // using a different syntax, make a derivation which should be equivalent. Chris@16: // Chris@16: // Another issue addressed here is that the transform_iterator doesn't have Chris@16: // a templated constructor. This makes it incompatible with the dataflow Chris@16: // ideal. This is also addressed here. Chris@16: Chris@16: template< Chris@16: class Base, Chris@101: class CharType = typename boost::iterator_value::type Chris@16: > Chris@16: class binary_from_base64 : public Chris@16: transform_iterator< Chris@16: detail::to_6_bit, Chris@16: Base Chris@16: > Chris@16: { Chris@16: friend class boost::iterator_core_access; Chris@16: typedef transform_iterator< Chris@16: detail::to_6_bit, Chris@16: Base Chris@16: > super_t; Chris@16: public: Chris@16: // make composible buy using templated constructor Chris@16: template Chris@16: binary_from_base64(BOOST_PFTO_WRAPPER(T) start) : Chris@16: super_t( Chris@16: Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start))), Chris@16: detail::to_6_bit() Chris@16: ) Chris@16: {} Chris@16: // intel 7.1 doesn't like default copy constructor Chris@16: binary_from_base64(const binary_from_base64 & rhs) : Chris@16: super_t( Chris@16: Base(rhs.base_reference()), Chris@16: detail::to_6_bit() Chris@16: ) Chris@16: {} Chris@16: // binary_from_base64(){}; Chris@16: }; Chris@16: Chris@16: } // namespace iterators Chris@16: } // namespace archive Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP