Chris@16
|
1 #ifndef BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
|
Chris@16
|
2 #define BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // MS compatible compilers support #pragma once
|
Chris@101
|
5 #if defined(_MSC_VER)
|
Chris@16
|
6 # pragma once
|
Chris@16
|
7 #endif
|
Chris@16
|
8
|
Chris@16
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
10 // binary_from_base64.hpp
|
Chris@16
|
11
|
Chris@16
|
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
16
|
Chris@16
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/assert.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
22 #include <boost/serialization/pfto.hpp>
|
Chris@16
|
23 #include <boost/static_assert.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/iterator/transform_iterator.hpp>
|
Chris@16
|
26 #include <boost/archive/iterators/dataflow_exception.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29 namespace archive {
|
Chris@16
|
30 namespace iterators {
|
Chris@16
|
31
|
Chris@16
|
32 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
33 // convert base64 characters to binary data
|
Chris@16
|
34
|
Chris@16
|
35 namespace detail {
|
Chris@16
|
36
|
Chris@16
|
37 template<class CharType>
|
Chris@16
|
38 struct to_6_bit {
|
Chris@16
|
39 typedef CharType result_type;
|
Chris@16
|
40 CharType operator()(CharType t) const{
|
Chris@16
|
41 const signed char lookup_table[] = {
|
Chris@16
|
42 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
Chris@16
|
43 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
Chris@16
|
44 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
Chris@16
|
45 52,53,54,55,56,57,58,59,60,61,-1,-1,-1, 0,-1,-1, // render '=' as 0
|
Chris@16
|
46 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
|
Chris@16
|
47 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
Chris@16
|
48 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
|
Chris@16
|
49 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
|
Chris@16
|
50 };
|
Chris@16
|
51 // metrowerks trips this assertion - how come?
|
Chris@16
|
52 #if ! defined(__MWERKS__)
|
Chris@16
|
53 BOOST_STATIC_ASSERT(128 == sizeof(lookup_table));
|
Chris@16
|
54 #endif
|
Chris@16
|
55 signed char value = -1;
|
Chris@16
|
56 if((unsigned)t <= 127)
|
Chris@16
|
57 value = lookup_table[(unsigned)t];
|
Chris@16
|
58 if(-1 == value)
|
Chris@16
|
59 boost::serialization::throw_exception(
|
Chris@16
|
60 dataflow_exception(dataflow_exception::invalid_base64_character)
|
Chris@16
|
61 );
|
Chris@16
|
62 return value;
|
Chris@16
|
63 }
|
Chris@16
|
64 };
|
Chris@16
|
65
|
Chris@16
|
66 } // namespace detail
|
Chris@16
|
67
|
Chris@16
|
68 // note: what we would like to do is
|
Chris@101
|
69 // template<class Base, class CharType = typename Base::value_type>
|
Chris@16
|
70 // typedef transform_iterator<
|
Chris@16
|
71 // from_6_bit<CharType>,
|
Chris@16
|
72 // transform_width<Base, 6, sizeof(Base::value_type) * 8, CharType>
|
Chris@16
|
73 // > base64_from_binary;
|
Chris@16
|
74 // but C++ won't accept this. Rather than using a "type generator" and
|
Chris@16
|
75 // using a different syntax, make a derivation which should be equivalent.
|
Chris@16
|
76 //
|
Chris@16
|
77 // Another issue addressed here is that the transform_iterator doesn't have
|
Chris@16
|
78 // a templated constructor. This makes it incompatible with the dataflow
|
Chris@16
|
79 // ideal. This is also addressed here.
|
Chris@16
|
80
|
Chris@16
|
81 template<
|
Chris@16
|
82 class Base,
|
Chris@101
|
83 class CharType = typename boost::iterator_value<Base>::type
|
Chris@16
|
84 >
|
Chris@16
|
85 class binary_from_base64 : public
|
Chris@16
|
86 transform_iterator<
|
Chris@16
|
87 detail::to_6_bit<CharType>,
|
Chris@16
|
88 Base
|
Chris@16
|
89 >
|
Chris@16
|
90 {
|
Chris@16
|
91 friend class boost::iterator_core_access;
|
Chris@16
|
92 typedef transform_iterator<
|
Chris@16
|
93 detail::to_6_bit<CharType>,
|
Chris@16
|
94 Base
|
Chris@16
|
95 > super_t;
|
Chris@16
|
96 public:
|
Chris@16
|
97 // make composible buy using templated constructor
|
Chris@16
|
98 template<class T>
|
Chris@16
|
99 binary_from_base64(BOOST_PFTO_WRAPPER(T) start) :
|
Chris@16
|
100 super_t(
|
Chris@16
|
101 Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start))),
|
Chris@16
|
102 detail::to_6_bit<CharType>()
|
Chris@16
|
103 )
|
Chris@16
|
104 {}
|
Chris@16
|
105 // intel 7.1 doesn't like default copy constructor
|
Chris@16
|
106 binary_from_base64(const binary_from_base64 & rhs) :
|
Chris@16
|
107 super_t(
|
Chris@16
|
108 Base(rhs.base_reference()),
|
Chris@16
|
109 detail::to_6_bit<CharType>()
|
Chris@16
|
110 )
|
Chris@16
|
111 {}
|
Chris@16
|
112 // binary_from_base64(){};
|
Chris@16
|
113 };
|
Chris@16
|
114
|
Chris@16
|
115 } // namespace iterators
|
Chris@16
|
116 } // namespace archive
|
Chris@16
|
117 } // namespace boost
|
Chris@16
|
118
|
Chris@16
|
119 #endif // BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
|