base64_from_binary.hpp 3.3 KB

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