binary_from_base32hex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE32HEX_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE32HEX_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_base32hex.h (derived from boost 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. #include <exceptions/exceptions.h>
  22. namespace boost {
  23. namespace archive {
  24. namespace iterators {
  25. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  26. // convert base32hex characters to binary data
  27. namespace detail {
  28. template<class CharType>
  29. struct to_5_bit {
  30. typedef CharType result_type;
  31. CharType operator()(CharType t) const{
  32. const char lookup_table[] = {
  33. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 00-0f
  34. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 10-1f
  35. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 20-2f
  36. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, // 30-3f
  37. -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, // 40-4f
  38. 25,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 50-5f
  39. -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, // 60-6f
  40. 25,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,-1 // 70-7f
  41. };
  42. // metrowerks trips this assertion - how come?
  43. #if ! defined(__MWERKS__)
  44. BOOST_STATIC_ASSERT(0x80 == sizeof(lookup_table));
  45. #endif
  46. signed char value = -1;
  47. if((unsigned)t < sizeof(lookup_table))
  48. value = lookup_table[(unsigned)t];
  49. if(-1 == value) {
  50. isc_throw(isc::BadValue,
  51. "attempt to decode a value not in base32hex char set");
  52. }
  53. return value;
  54. }
  55. };
  56. } // namespace detail
  57. // note: what we would like to do is
  58. // template<class Base, class CharType = BOOST_DEDUCED_TYPENAME Base::value_type>
  59. // typedef transform_iterator<
  60. // from_5_bit<CharType>,
  61. // transform_width<Base, 5, sizeof(Base::value_type) * 8, CharType>
  62. // > base32hex_from_binary;
  63. // but C++ won't accept this. Rather than using a "type generator" and
  64. // using a different syntax, make a derivation which should be equivalent.
  65. //
  66. // Another issue addressed here is that the transform_iterator doesn't have
  67. // a templated constructor. This makes it incompatible with the dataflow
  68. // ideal. This is also addressed here.
  69. template<
  70. class Base,
  71. class CharType = BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type
  72. >
  73. class binary_from_base32hex : public
  74. transform_iterator<
  75. detail::to_5_bit<CharType>,
  76. Base
  77. >
  78. {
  79. friend class boost::iterator_core_access;
  80. typedef transform_iterator<
  81. detail::to_5_bit<CharType>,
  82. Base
  83. > super_t;
  84. public:
  85. // make composible buy using templated constructor
  86. template<class T>
  87. binary_from_base32hex(BOOST_PFTO_WRAPPER(T) start) :
  88. super_t(
  89. Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))),
  90. detail::to_5_bit<CharType>()
  91. )
  92. {}
  93. // intel 7.1 doesn't like default copy constructor
  94. binary_from_base32hex(const binary_from_base32hex & rhs) :
  95. super_t(
  96. Base(rhs.base_reference()),
  97. detail::to_5_bit<CharType>()
  98. )
  99. {}
  100. // binary_from_base32hex(){};
  101. };
  102. } // namespace iterators
  103. } // namespace archive
  104. } // namespace boost
  105. #endif // BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE32HEX_HPP
  106. // Local Variables:
  107. // mode: c++
  108. // End: