hex.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright (c) Marshall Clow 2011-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Thanks to Nevin for his comments/help.
  6. */
  7. /*
  8. General problem - turn a sequence of integral types into a sequence of hexadecimal characters.
  9. - and back.
  10. */
  11. /// \file hex.hpp
  12. /// \brief Convert sequence of integral types into a sequence of hexadecimal
  13. /// characters and back. Based on the MySQL functions HEX and UNHEX
  14. /// \author Marshall Clow
  15. #ifndef BOOST_ALGORITHM_HEXHPP
  16. #define BOOST_ALGORITHM_HEXHPP
  17. #include <iterator> // for std::iterator_traits
  18. #include <stdexcept>
  19. #include <boost/config.hpp>
  20. #include <boost/range/begin.hpp>
  21. #include <boost/range/end.hpp>
  22. #include <boost/exception/exception.hpp>
  23. #include <boost/exception/info.hpp>
  24. #include <boost/throw_exception.hpp>
  25. #include <boost/utility/enable_if.hpp>
  26. #include <boost/type_traits/is_integral.hpp>
  27. namespace boost { namespace algorithm {
  28. /*!
  29. \struct hex_decode_error
  30. \brief Base exception class for all hex decoding errors
  31. */ /*!
  32. \struct non_hex_input
  33. \brief Thrown when a non-hex value (0-9, A-F) encountered when decoding.
  34. Contains the offending character
  35. */ /*!
  36. \struct not_enough_input
  37. \brief Thrown when the input sequence unexpectedly ends
  38. */
  39. struct hex_decode_error : virtual boost::exception, virtual std::exception {};
  40. struct not_enough_input : virtual hex_decode_error {};
  41. struct non_hex_input : virtual hex_decode_error {};
  42. typedef boost::error_info<struct bad_char_,char> bad_char;
  43. namespace detail {
  44. /// \cond DOXYGEN_HIDE
  45. template <typename T, typename OutputIterator>
  46. OutputIterator encode_one ( T val, OutputIterator out, const char * hexDigits ) {
  47. const std::size_t num_hex_digits = 2 * sizeof ( T );
  48. char res [ num_hex_digits ];
  49. char *p = res + num_hex_digits;
  50. for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 )
  51. *--p = hexDigits [ val & 0x0F ];
  52. return std::copy ( res, res + num_hex_digits, out );
  53. }
  54. template <typename T>
  55. unsigned char hex_char_to_int ( T val ) {
  56. char c = static_cast<char> ( val );
  57. unsigned retval = 0;
  58. if ( c >= '0' && c <= '9' ) retval = c - '0';
  59. else if ( c >= 'A' && c <= 'F' ) retval = c - 'A' + 10;
  60. else if ( c >= 'a' && c <= 'f' ) retval = c - 'a' + 10;
  61. else BOOST_THROW_EXCEPTION (non_hex_input() << bad_char (c));
  62. return static_cast<char>(retval);
  63. }
  64. // My own iterator_traits class.
  65. // It is here so that I can "reach inside" some kinds of output iterators
  66. // and get the type to write.
  67. template <typename Iterator>
  68. struct hex_iterator_traits {
  69. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  70. };
  71. template<typename Container>
  72. struct hex_iterator_traits< std::back_insert_iterator<Container> > {
  73. typedef typename Container::value_type value_type;
  74. };
  75. template<typename Container>
  76. struct hex_iterator_traits< std::front_insert_iterator<Container> > {
  77. typedef typename Container::value_type value_type;
  78. };
  79. template<typename Container>
  80. struct hex_iterator_traits< std::insert_iterator<Container> > {
  81. typedef typename Container::value_type value_type;
  82. };
  83. // ostream_iterators have three template parameters.
  84. // The first one is the output type, the second one is the character type of
  85. // the underlying stream, the third is the character traits.
  86. // We only care about the first one.
  87. template<typename T, typename charType, typename traits>
  88. struct hex_iterator_traits< std::ostream_iterator<T, charType, traits> > {
  89. typedef T value_type;
  90. };
  91. template <typename Iterator>
  92. bool iter_end ( Iterator current, Iterator last ) { return current == last; }
  93. template <typename T>
  94. bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; }
  95. // What can we assume here about the inputs?
  96. // is std::iterator_traits<InputIterator>::value_type always 'char' ?
  97. // Could it be wchar_t, say? Does it matter?
  98. // We are assuming ASCII for the values - but what about the storage?
  99. template <typename InputIterator, typename OutputIterator, typename EndPred>
  100. typename boost::enable_if<boost::is_integral<typename hex_iterator_traits<OutputIterator>::value_type>, OutputIterator>::type
  101. decode_one ( InputIterator &first, InputIterator last, OutputIterator out, EndPred pred ) {
  102. typedef typename hex_iterator_traits<OutputIterator>::value_type T;
  103. T res (0);
  104. // Need to make sure that we get can read that many chars here.
  105. for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) {
  106. if ( pred ( first, last ))
  107. BOOST_THROW_EXCEPTION (not_enough_input ());
  108. res = ( 16 * res ) + hex_char_to_int (*first);
  109. }
  110. *out = res;
  111. return ++out;
  112. }
  113. /// \endcond
  114. }
  115. /// \fn hex ( InputIterator first, InputIterator last, OutputIterator out )
  116. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
  117. ///
  118. /// \param first The start of the input sequence
  119. /// \param last One past the end of the input sequence
  120. /// \param out An output iterator to the results into
  121. /// \return The updated output iterator
  122. /// \note Based on the MySQL function of the same name
  123. template <typename InputIterator, typename OutputIterator>
  124. typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
  125. hex ( InputIterator first, InputIterator last, OutputIterator out ) {
  126. for ( ; first != last; ++first )
  127. out = detail::encode_one ( *first, out, "0123456789ABCDEF" );
  128. return out;
  129. }
  130. /// \fn hex_lower ( InputIterator first, InputIterator last, OutputIterator out )
  131. /// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
  132. ///
  133. /// \param first The start of the input sequence
  134. /// \param last One past the end of the input sequence
  135. /// \param out An output iterator to the results into
  136. /// \return The updated output iterator
  137. /// \note Based on the MySQL function of the same name
  138. template <typename InputIterator, typename OutputIterator>
  139. typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
  140. hex_lower ( InputIterator first, InputIterator last, OutputIterator out ) {
  141. for ( ; first != last; ++first )
  142. out = detail::encode_one ( *first, out, "0123456789abcdef" );
  143. return out;
  144. }
  145. /// \fn hex ( const T *ptr, OutputIterator out )
  146. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
  147. ///
  148. /// \param ptr A pointer to a 0-terminated sequence of data.
  149. /// \param out An output iterator to the results into
  150. /// \return The updated output iterator
  151. /// \note Based on the MySQL function of the same name
  152. template <typename T, typename OutputIterator>
  153. typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
  154. hex ( const T *ptr, OutputIterator out ) {
  155. while ( *ptr )
  156. out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" );
  157. return out;
  158. }
  159. /// \fn hex_lower ( const T *ptr, OutputIterator out )
  160. /// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
  161. ///
  162. /// \param ptr A pointer to a 0-terminated sequence of data.
  163. /// \param out An output iterator to the results into
  164. /// \return The updated output iterator
  165. /// \note Based on the MySQL function of the same name
  166. template <typename T, typename OutputIterator>
  167. typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
  168. hex_lower ( const T *ptr, OutputIterator out ) {
  169. while ( *ptr )
  170. out = detail::encode_one ( *ptr++, out, "0123456789abcdef" );
  171. return out;
  172. }
  173. /// \fn hex ( const Range &r, OutputIterator out )
  174. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
  175. ///
  176. /// \param r The input range
  177. /// \param out An output iterator to the results into
  178. /// \return The updated output iterator
  179. /// \note Based on the MySQL function of the same name
  180. template <typename Range, typename OutputIterator>
  181. typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<typename Range::iterator>::value_type>, OutputIterator>::type
  182. hex ( const Range &r, OutputIterator out ) {
  183. return hex (boost::begin(r), boost::end(r), out);
  184. }
  185. /// \fn hex_lower ( const Range &r, OutputIterator out )
  186. /// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
  187. ///
  188. /// \param r The input range
  189. /// \param out An output iterator to the results into
  190. /// \return The updated output iterator
  191. /// \note Based on the MySQL function of the same name
  192. template <typename Range, typename OutputIterator>
  193. typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<typename Range::iterator>::value_type>, OutputIterator>::type
  194. hex_lower ( const Range &r, OutputIterator out ) {
  195. return hex_lower (boost::begin(r), boost::end(r), out);
  196. }
  197. /// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out )
  198. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
  199. ///
  200. /// \param first The start of the input sequence
  201. /// \param last One past the end of the input sequence
  202. /// \param out An output iterator to the results into
  203. /// \return The updated output iterator
  204. /// \note Based on the MySQL function of the same name
  205. template <typename InputIterator, typename OutputIterator>
  206. OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator out ) {
  207. while ( first != last )
  208. out = detail::decode_one ( first, last, out, detail::iter_end<InputIterator> );
  209. return out;
  210. }
  211. /// \fn unhex ( const T *ptr, OutputIterator out )
  212. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
  213. ///
  214. /// \param ptr A pointer to a null-terminated input sequence.
  215. /// \param out An output iterator to the results into
  216. /// \return The updated output iterator
  217. /// \note Based on the MySQL function of the same name
  218. template <typename T, typename OutputIterator>
  219. OutputIterator unhex ( const T *ptr, OutputIterator out ) {
  220. // If we run into the terminator while decoding, we will throw a
  221. // malformed input exception. It would be nicer to throw a 'Not enough input'
  222. // exception - but how much extra work would that require?
  223. while ( *ptr )
  224. out = detail::decode_one ( ptr, (const T *) NULL, out, detail::ptr_end<T> );
  225. return out;
  226. }
  227. /// \fn OutputIterator unhex ( const Range &r, OutputIterator out )
  228. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
  229. ///
  230. /// \param r The input range
  231. /// \param out An output iterator to the results into
  232. /// \return The updated output iterator
  233. /// \note Based on the MySQL function of the same name
  234. template <typename Range, typename OutputIterator>
  235. OutputIterator unhex ( const Range &r, OutputIterator out ) {
  236. return unhex (boost::begin(r), boost::end(r), out);
  237. }
  238. /// \fn String hex ( const String &input )
  239. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
  240. ///
  241. /// \param input A container to be converted
  242. /// \return A container with the encoded text
  243. template<typename String>
  244. String hex ( const String &input ) {
  245. String output;
  246. output.reserve (input.size () * (2 * sizeof (typename String::value_type)));
  247. (void) hex (input, std::back_inserter (output));
  248. return output;
  249. }
  250. /// \fn String hex_lower ( const String &input )
  251. /// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
  252. ///
  253. /// \param input A container to be converted
  254. /// \return A container with the encoded text
  255. template<typename String>
  256. String hex_lower ( const String &input ) {
  257. String output;
  258. output.reserve (input.size () * (2 * sizeof (typename String::value_type)));
  259. (void) hex_lower (input, std::back_inserter (output));
  260. return output;
  261. }
  262. /// \fn String unhex ( const String &input )
  263. /// \brief Converts a sequence of hexadecimal characters into a sequence of characters.
  264. ///
  265. /// \param input A container to be converted
  266. /// \return A container with the decoded text
  267. template<typename String>
  268. String unhex ( const String &input ) {
  269. String output;
  270. output.reserve (input.size () / (2 * sizeof (typename String::value_type)));
  271. (void) unhex (input, std::back_inserter (output));
  272. return output;
  273. }
  274. }}
  275. #endif // BOOST_ALGORITHM_HEXHPP