binary_from_base64.hpp 3.9 KB

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