base64_from_binary.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // base64_from_binary.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 <boost/assert.hpp>
  15. #include <cstddef> // size_t
  16. #if defined(BOOST_NO_STDC_NAMESPACE)
  17. namespace std{
  18. using ::size_t;
  19. } // namespace std
  20. #endif
  21. #include <boost/iterator/transform_iterator.hpp>
  22. #include <boost/archive/iterators/dataflow_exception.hpp>
  23. namespace boost {
  24. namespace archive {
  25. namespace iterators {
  26. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  27. // convert binary integers to base64 characters
  28. namespace detail {
  29. template<class CharType>
  30. struct from_6_bit {
  31. typedef CharType result_type;
  32. CharType operator()(CharType t) const{
  33. static const char * lookup_table =
  34. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  35. "abcdefghijklmnopqrstuvwxyz"
  36. "0123456789"
  37. "+/";
  38. BOOST_ASSERT(t < 64);
  39. return lookup_table[static_cast<size_t>(t)];
  40. }
  41. };
  42. } // namespace detail
  43. // note: what we would like to do is
  44. // template<class Base, class CharType = typename Base::value_type>
  45. // typedef transform_iterator<
  46. // from_6_bit<CharType>,
  47. // transform_width<Base, 6, sizeof(Base::value_type) * 8, CharType>
  48. // > base64_from_binary;
  49. // but C++ won't accept this. Rather than using a "type generator" and
  50. // using a different syntax, make a derivation which should be equivalent.
  51. //
  52. // Another issue addressed here is that the transform_iterator doesn't have
  53. // a templated constructor. This makes it incompatible with the dataflow
  54. // ideal. This is also addressed here.
  55. //template<class Base, class CharType = typename Base::value_type>
  56. template<
  57. class Base,
  58. class CharType = typename boost::iterator_value<Base>::type
  59. >
  60. class base64_from_binary :
  61. public transform_iterator<
  62. detail::from_6_bit<CharType>,
  63. Base
  64. >
  65. {
  66. friend class boost::iterator_core_access;
  67. typedef transform_iterator<
  68. typename detail::from_6_bit<CharType>,
  69. Base
  70. > super_t;
  71. public:
  72. // make composible buy using templated constructor
  73. template<class T>
  74. base64_from_binary(T start) :
  75. super_t(
  76. Base(static_cast< T >(start)),
  77. detail::from_6_bit<CharType>()
  78. )
  79. {}
  80. // intel 7.1 doesn't like default copy constructor
  81. base64_from_binary(const base64_from_binary & rhs) :
  82. super_t(
  83. Base(rhs.base_reference()),
  84. detail::from_6_bit<CharType>()
  85. )
  86. {}
  87. // base64_from_binary(){};
  88. };
  89. } // namespace iterators
  90. } // namespace archive
  91. } // namespace boost
  92. #endif // BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP