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