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