123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #ifndef BOOST_ALGORITHM_HEXHPP
- #define BOOST_ALGORITHM_HEXHPP
- #include <iterator> // for std::iterator_traits
- #include <stdexcept>
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- #include <boost/exception/all.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_integral.hpp>
- namespace boost { namespace algorithm {
-
-
- struct hex_decode_error : virtual boost::exception, virtual std::exception {};
- struct not_enough_input : virtual hex_decode_error {};
- struct non_hex_input : virtual hex_decode_error {};
- typedef boost::error_info<struct bad_char_,char> bad_char;
- namespace detail {
- template <typename T, typename OutputIterator>
- OutputIterator encode_one ( T val, OutputIterator out ) {
- const std::size_t num_hex_digits = 2 * sizeof ( T );
- char res [ num_hex_digits ];
- char *p = res + num_hex_digits;
- for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 )
- *--p = "0123456789ABCDEF" [ val & 0x0F ];
- return std::copy ( res, res + num_hex_digits, out );
- }
- template <typename T>
- unsigned char hex_char_to_int ( T val ) {
- char c = static_cast<char> ( val );
- unsigned retval = 0;
- if ( c >= '0' && c <= '9' ) retval = c - '0';
- else if ( c >= 'A' && c <= 'F' ) retval = c - 'A' + 10;
- else if ( c >= 'a' && c <= 'f' ) retval = c - 'a' + 10;
- else BOOST_THROW_EXCEPTION (non_hex_input() << bad_char (c));
- return retval;
- }
- template <typename Iterator>
- struct hex_iterator_traits {
- typedef typename std::iterator_traits<Iterator>::value_type value_type;
- };
- template<typename Container>
- struct hex_iterator_traits< std::back_insert_iterator<Container> > {
- typedef typename Container::value_type value_type;
- };
- template<typename Container>
- struct hex_iterator_traits< std::front_insert_iterator<Container> > {
- typedef typename Container::value_type value_type;
- };
- template<typename Container>
- struct hex_iterator_traits< std::insert_iterator<Container> > {
- typedef typename Container::value_type value_type;
- };
- template<typename T, typename charType, typename traits>
- struct hex_iterator_traits< std::ostream_iterator<T, charType, traits> > {
- typedef T value_type;
- };
- template <typename Iterator>
- bool iter_end ( Iterator current, Iterator last ) { return current == last; }
-
- template <typename T>
- bool ptr_end ( const T* ptr, const T* ) { return *ptr == '\0'; }
-
- template <typename InputIterator, typename OutputIterator, typename EndPred>
- typename boost::enable_if<boost::is_integral<typename hex_iterator_traits<OutputIterator>::value_type>, OutputIterator>::type
- decode_one ( InputIterator &first, InputIterator last, OutputIterator out, EndPred pred ) {
- typedef typename hex_iterator_traits<OutputIterator>::value_type T;
- T res (0);
-
- for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) {
- if ( pred ( first, last ))
- BOOST_THROW_EXCEPTION (not_enough_input ());
- res = ( 16 * res ) + hex_char_to_int (*first);
- }
-
- *out = res;
- return ++out;
- }
- }
- template <typename InputIterator, typename OutputIterator>
- typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
- hex ( InputIterator first, InputIterator last, OutputIterator out ) {
- for ( ; first != last; ++first )
- out = detail::encode_one ( *first, out );
- return out;
- }
-
- template <typename T, typename OutputIterator>
- typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
- hex ( const T *ptr, OutputIterator out ) {
- while ( *ptr )
- out = detail::encode_one ( *ptr++, out );
- return out;
- }
- template <typename Range, typename OutputIterator>
- typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<typename Range::iterator>::value_type>, OutputIterator>::type
- hex ( const Range &r, OutputIterator out ) {
- return hex (boost::begin(r), boost::end(r), out);
- }
- template <typename InputIterator, typename OutputIterator>
- OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator out ) {
- while ( first != last )
- out = detail::decode_one ( first, last, out, detail::iter_end<InputIterator> );
- return out;
- }
- template <typename T, typename OutputIterator>
- OutputIterator unhex ( const T *ptr, OutputIterator out ) {
- while ( *ptr )
- out = detail::decode_one ( ptr, (const T *) NULL, out, detail::ptr_end<T> );
- return out;
- }
- template <typename Range, typename OutputIterator>
- OutputIterator unhex ( const Range &r, OutputIterator out ) {
- return unhex (boost::begin(r), boost::end(r), out);
- }
- template<typename String>
- String hex ( const String &input ) {
- String output;
- output.reserve (input.size () * (2 * sizeof (typename String::value_type)));
- (void) hex (input, std::back_inserter (output));
- return output;
- }
- template<typename String>
- String unhex ( const String &input ) {
- String output;
- output.reserve (input.size () / (2 * sizeof (typename String::value_type)));
- (void) unhex (input, std::back_inserter (output));
- return output;
- }
- }}
- #endif
|