1 // Boost CRC library crc.hpp header file -----------------------------------//
3 // (C) Copyright Daryle Walker 2001. Permission to copy, use, modify, sell and
4 // distribute this software is granted provided this copyright notice appears
5 // in all copies. This software is provided "as is" without express or
6 // implied warranty, and with no claim as to its suitability for any purpose.
8 // See http://www.boost.org/libs/crc for documentation.
13 #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
14 #include <boost/integer.hpp> // for boost::uint_t
16 #include <climits> // for CHAR_BIT, etc.
17 #include <cstddef> // for std::size_t
19 #include <boost/limits.hpp> // for std::numeric_limits
22 // The type of CRC parameters that can go in a template should be related
23 // on the CRC's bit count. This macro expresses that type in a compact
24 // form, but also allows an alternate type for compilers that don't support
25 // dependent types (in template value-parameters).
26 #if !(defined(BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS) || (defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)))
27 #define BOOST_CRC_PARM_TYPE typename ::boost::uint_t<Bits>::fast
29 #define BOOST_CRC_PARM_TYPE unsigned long
32 // Some compilers [MS VC++ 6] cannot correctly set up several versions of a
33 // function template unless every template argument can be unambiguously
34 // deduced from the function arguments. (The bug is hidden if only one version
35 // is needed.) Since all of the CRC function templates have this problem, the
36 // workaround is to make up a dummy function argument that encodes the template
37 // arguments. Calls to such template functions need all their template
38 // arguments explicitly specified. At least one compiler that needs this
39 // workaround also needs the default value for the dummy argument to be
40 // specified in the definition.
41 #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
42 #define BOOST_CRC_DUMMY_PARM_TYPE
43 #define BOOST_CRC_DUMMY_INIT
44 #define BOOST_ACRC_DUMMY_PARM_TYPE
45 #define BOOST_ACRC_DUMMY_INIT
47 namespace boost { namespace detail {
48 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
49 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
50 bool ReflectIn, bool ReflectRem >
51 struct dummy_crc_argument { };
53 #define BOOST_CRC_DUMMY_PARM_TYPE , detail::dummy_crc_argument<Bits, \
54 TruncPoly, InitRem, FinalXor, ReflectIn, ReflectRem> *p_
55 #define BOOST_CRC_DUMMY_INIT BOOST_CRC_DUMMY_PARM_TYPE = 0
56 #define BOOST_ACRC_DUMMY_PARM_TYPE , detail::dummy_crc_argument<Bits, \
57 TruncPoly, 0, 0, false, false> *p_
58 #define BOOST_ACRC_DUMMY_INIT BOOST_ACRC_DUMMY_PARM_TYPE = 0
66 // Forward declarations ----------------------------------------------------//
68 template < std::size_t Bits >
71 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly = 0u,
72 BOOST_CRC_PARM_TYPE InitRem = 0u,
73 BOOST_CRC_PARM_TYPE FinalXor = 0u, bool ReflectIn = false,
74 bool ReflectRem = false >
77 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
78 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
79 bool ReflectIn, bool ReflectRem >
80 typename uint_t<Bits>::fast crc( void const *buffer,
81 std::size_t byte_count
82 BOOST_CRC_DUMMY_PARM_TYPE );
84 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
85 typename uint_t<Bits>::fast augmented_crc( void const *buffer,
86 std::size_t byte_count, typename uint_t<Bits>::fast initial_remainder
87 BOOST_ACRC_DUMMY_PARM_TYPE );
89 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
90 typename uint_t<Bits>::fast augmented_crc( void const *buffer,
91 std::size_t byte_count
92 BOOST_ACRC_DUMMY_PARM_TYPE );
94 typedef crc_optimal<16, 0x8005, 0, 0, true, true> crc_16_type;
95 typedef crc_optimal<16, 0x1021, 0xFFFF, 0, false, false> crc_ccitt_type;
96 typedef crc_optimal<16, 0x8408, 0, 0, true, true> crc_xmodem_type;
98 typedef crc_optimal<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true>
102 // Forward declarations for implementation detail stuff --------------------//
103 // (Just for the stuff that will be needed for the next two sections)
107 template < std::size_t Bits >
111 struct mask_uint_t< std::numeric_limits<unsigned char>::digits >;
113 #if USHRT_MAX > UCHAR_MAX
115 struct mask_uint_t< std::numeric_limits<unsigned short>::digits >;
118 #if UINT_MAX > USHRT_MAX
120 struct mask_uint_t< std::numeric_limits<unsigned int>::digits >;
123 #if ULONG_MAX > UINT_MAX
125 struct mask_uint_t< std::numeric_limits<unsigned long>::digits >;
128 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
131 template < std::size_t Bits, bool DoReflect >
134 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
135 template < std::size_t Bits >
136 class crc_helper< Bits, false >;
139 } // namespace detail
142 // Simple cyclic redundancy code (CRC) class declaration -------------------//
144 template < std::size_t Bits >
147 // Implementation type
148 typedef detail::mask_uint_t<Bits> masking_type;
152 typedef typename masking_type::least value_type;
154 // Constant for the template parameter
155 BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
158 explicit crc_basic( value_type truncated_polynominal,
159 value_type initial_remainder = 0, value_type final_xor_value = 0,
160 bool reflect_input = false, bool reflect_remainder = false );
162 // Internal Operations
163 value_type get_truncated_polynominal() const;
164 value_type get_initial_remainder() const;
165 value_type get_final_xor_value() const;
166 bool get_reflect_input() const;
167 bool get_reflect_remainder() const;
169 value_type get_interim_remainder() const;
170 void reset( value_type new_rem );
173 // External Operations
174 void process_bit( bool bit );
175 void process_bits( unsigned char bits, std::size_t bit_count );
176 void process_byte( unsigned char byte );
177 void process_block( void const *bytes_begin, void const *bytes_end );
178 void process_bytes( void const *buffer, std::size_t byte_count );
180 value_type checksum() const;
185 value_type poly_, init_, final_; // non-const to allow assignability
186 bool rft_in_, rft_out_; // non-const to allow assignability
188 }; // boost::crc_basic
191 // Optimized cyclic redundancy code (CRC) class declaration ----------------//
193 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
194 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
195 bool ReflectIn, bool ReflectRem >
198 // Implementation type
199 typedef detail::mask_uint_t<Bits> masking_type;
203 typedef typename masking_type::fast value_type;
205 // Constants for the template parameters
206 BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
207 BOOST_STATIC_CONSTANT( value_type, truncated_polynominal = TruncPoly );
208 BOOST_STATIC_CONSTANT( value_type, initial_remainder = InitRem );
209 BOOST_STATIC_CONSTANT( value_type, final_xor_value = FinalXor );
210 BOOST_STATIC_CONSTANT( bool, reflect_input = ReflectIn );
211 BOOST_STATIC_CONSTANT( bool, reflect_remainder = ReflectRem );
214 explicit crc_optimal( value_type init_rem = InitRem );
216 // Internal Operations
217 value_type get_truncated_polynominal() const;
218 value_type get_initial_remainder() const;
219 value_type get_final_xor_value() const;
220 bool get_reflect_input() const;
221 bool get_reflect_remainder() const;
223 value_type get_interim_remainder() const;
224 void reset( value_type new_rem = InitRem );
226 // External Operations
227 void process_byte( unsigned char byte );
228 void process_block( void const *bytes_begin, void const *bytes_end );
229 void process_bytes( void const *buffer, std::size_t byte_count );
231 value_type checksum() const;
234 void operator ()( unsigned char byte );
235 value_type operator ()() const;
238 // The implementation of output reflection depends on both reflect states.
239 BOOST_STATIC_CONSTANT( bool, reflect_output = (ReflectRem != ReflectIn) );
242 #define BOOST_CRC_REF_OUT_VAL reflect_output
244 typedef crc_optimal self_type;
245 #define BOOST_CRC_REF_OUT_VAL (self_type::reflect_output)
248 // More implementation types
249 typedef detail::crc_table_t<Bits, TruncPoly, ReflectIn> crc_table_type;
250 typedef detail::crc_helper<Bits, ReflectIn> helper_type;
251 typedef detail::crc_helper<Bits, BOOST_CRC_REF_OUT_VAL> reflect_out_type;
253 #undef BOOST_CRC_REF_OUT_VAL
258 }; // boost::crc_optimal
261 // Implementation detail stuff ---------------------------------------------//
265 // Forward declarations for more implementation details
266 template < std::size_t Bits >
269 template < std::size_t Bits >
273 // Traits class for mask; given the bit number
274 // (1-based), get the mask for that bit by itself.
275 template < std::size_t Bits >
277 : boost::uint_t< Bits >
279 typedef boost::uint_t<Bits> base_type;
280 typedef typename base_type::least least;
281 typedef typename base_type::fast fast;
283 BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << ( Bits
285 BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << ( Bits
288 }; // boost::detail::high_uint_t
291 // Reflection routine class wrapper
292 // (since MS VC++ 6 couldn't handle the unwrapped version)
293 template < std::size_t Bits >
296 typedef typename boost::uint_t<Bits>::fast value_type;
298 static value_type reflect( value_type x );
300 }; // boost::detail::reflector
302 // Function that reflects its argument
303 template < std::size_t Bits >
304 typename reflector<Bits>::value_type
305 reflector<Bits>::reflect
307 typename reflector<Bits>::value_type x
310 value_type reflection = 0;
311 value_type const one = 1;
313 for ( std::size_t i = 0 ; i < Bits ; ++i, x >>= 1 )
317 reflection |= ( one << (Bits - 1u - i) );
325 // Traits class for masks; given the bit number (1-based),
326 // get the mask for that bit and its lower bits.
327 template < std::size_t Bits >
329 : high_uint_t< Bits >
331 typedef high_uint_t<Bits> base_type;
332 typedef typename base_type::least least;
333 typedef typename base_type::fast fast;
336 using base_type::high_bit;
337 using base_type::high_bit_fast;
339 BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
340 BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
343 BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
344 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
346 }; // boost::detail::mask_uint_t
349 struct mask_uint_t< std::numeric_limits<unsigned char>::digits >
350 : high_uint_t< std::numeric_limits<unsigned char>::digits >
352 typedef high_uint_t<std::numeric_limits<unsigned char>::digits>
354 typedef base_type::least least;
355 typedef base_type::fast fast;
358 using base_type::high_bit;
359 using base_type::high_bit_fast;
361 BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
362 BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
365 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
366 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
368 }; // boost::detail::mask_uint_t
370 #if USHRT_MAX > UCHAR_MAX
372 struct mask_uint_t< std::numeric_limits<unsigned short>::digits >
373 : high_uint_t< std::numeric_limits<unsigned short>::digits >
375 typedef high_uint_t<std::numeric_limits<unsigned short>::digits>
377 typedef base_type::least least;
378 typedef base_type::fast fast;
381 using base_type::high_bit;
382 using base_type::high_bit_fast;
384 BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
385 BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
388 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
389 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
391 }; // boost::detail::mask_uint_t
394 #if UINT_MAX > USHRT_MAX
396 struct mask_uint_t< std::numeric_limits<unsigned int>::digits >
397 : high_uint_t< std::numeric_limits<unsigned int>::digits >
399 typedef high_uint_t<std::numeric_limits<unsigned int>::digits>
401 typedef base_type::least least;
402 typedef base_type::fast fast;
405 using base_type::high_bit;
406 using base_type::high_bit_fast;
408 BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
409 BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
412 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
413 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
415 }; // boost::detail::mask_uint_t
418 #if ULONG_MAX > UINT_MAX
420 struct mask_uint_t< std::numeric_limits<unsigned long>::digits >
421 : high_uint_t< std::numeric_limits<unsigned long>::digits >
423 typedef high_uint_t<std::numeric_limits<unsigned long>::digits>
425 typedef base_type::least least;
426 typedef base_type::fast fast;
429 using base_type::high_bit;
430 using base_type::high_bit_fast;
432 BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
433 BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
436 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
437 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
439 }; // boost::detail::mask_uint_t
443 // CRC table generator
444 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
447 BOOST_STATIC_CONSTANT( std::size_t, byte_combos = (1ul << CHAR_BIT) );
449 typedef mask_uint_t<Bits> masking_type;
450 typedef typename masking_type::fast value_type;
451 #if defined(__BORLANDC__) && defined(_M_IX86) && (__BORLANDC__ == 0x560)
452 // for some reason Borland's command line compiler (version 0x560)
453 // chokes over this unless we do the calculation for it:
454 typedef value_type table_type[ 0x100 ];
456 typedef value_type table_type[ byte_combos ];
459 static void init_table();
461 static table_type table_;
463 }; // boost::detail::crc_table_t
465 // CRC table generator static data member definition
466 // (Some compilers [Borland C++] require the initializer to be present.)
467 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
468 typename crc_table_t<Bits, TruncPoly, Reflect>::table_type
469 crc_table_t<Bits, TruncPoly, Reflect>::table_
472 // Populate CRC lookup table
473 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
475 crc_table_t<Bits, TruncPoly, Reflect>::init_table
479 // compute table only on the first run
480 static bool did_init = false;
481 if ( did_init ) return;
483 // factor-out constants to avoid recalculation
484 value_type const fast_hi_bit = masking_type::high_bit_fast;
485 unsigned char const byte_hi_bit = 1u << (CHAR_BIT - 1u);
487 // loop over every possible dividend value
488 unsigned char dividend = 0;
491 value_type remainder = 0;
493 // go through all the dividend's bits
494 for ( unsigned char mask = byte_hi_bit ; mask ; mask >>= 1 )
496 // check if divisor fits
497 if ( dividend & mask )
499 remainder ^= fast_hi_bit;
502 // do polynominal division
503 if ( remainder & fast_hi_bit )
506 remainder ^= TruncPoly;
514 table_[ crc_helper<CHAR_BIT, Reflect>::reflect(dividend) ]
515 = crc_helper<Bits, Reflect>::reflect( remainder );
517 while ( ++dividend );
523 // CRC helper routines
524 template < std::size_t Bits, bool DoReflect >
529 typedef typename uint_t<Bits>::fast value_type;
531 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
532 // Possibly reflect a remainder
533 static value_type reflect( value_type x )
534 { return detail::reflector<Bits>::reflect( x ); }
536 // Compare a byte to the remainder's highest byte
537 static unsigned char index( value_type rem, unsigned char x )
540 // Shift out the remainder's highest byte
541 static value_type shift( value_type rem )
542 { return rem >> CHAR_BIT; }
544 // Possibly reflect a remainder
545 static value_type reflect( value_type x )
546 { return DoReflect ? detail::reflector<Bits>::reflect( x ) : x; }
548 // Compare a byte to the remainder's highest byte
549 static unsigned char index( value_type rem, unsigned char x )
550 { return x ^ ( rem >> (DoReflect ? 0u : Bits - CHAR_BIT) ); }
552 // Shift out the remainder's highest byte
553 static value_type shift( value_type rem )
554 { return DoReflect ? rem >> CHAR_BIT : rem << CHAR_BIT; }
557 }; // boost::detail::crc_helper
559 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
560 template < std::size_t Bits >
561 class crc_helper<Bits, false>
565 typedef typename uint_t<Bits>::fast value_type;
567 // Possibly reflect a remainder
568 static value_type reflect( value_type x )
571 // Compare a byte to the remainder's highest byte
572 static unsigned char index( value_type rem, unsigned char x )
573 { return x ^ ( rem >> (Bits - CHAR_BIT) ); }
575 // Shift out the remainder's highest byte
576 static value_type shift( value_type rem )
577 { return rem << CHAR_BIT; }
579 }; // boost::detail::crc_helper
583 } // namespace detail
586 // Simple CRC class function definitions -----------------------------------//
588 template < std::size_t Bits >
590 crc_basic<Bits>::crc_basic
592 typename crc_basic<Bits>::value_type truncated_polynominal,
593 typename crc_basic<Bits>::value_type initial_remainder, // = 0
594 typename crc_basic<Bits>::value_type final_xor_value, // = 0
595 bool reflect_input, // = false
596 bool reflect_remainder // = false
598 : rem_( initial_remainder ), poly_( truncated_polynominal )
599 , init_( initial_remainder ), final_( final_xor_value )
600 , rft_in_( reflect_input ), rft_out_( reflect_remainder )
604 template < std::size_t Bits >
606 typename crc_basic<Bits>::value_type
607 crc_basic<Bits>::get_truncated_polynominal
614 template < std::size_t Bits >
616 typename crc_basic<Bits>::value_type
617 crc_basic<Bits>::get_initial_remainder
624 template < std::size_t Bits >
626 typename crc_basic<Bits>::value_type
627 crc_basic<Bits>::get_final_xor_value
634 template < std::size_t Bits >
637 crc_basic<Bits>::get_reflect_input
644 template < std::size_t Bits >
647 crc_basic<Bits>::get_reflect_remainder
654 template < std::size_t Bits >
656 typename crc_basic<Bits>::value_type
657 crc_basic<Bits>::get_interim_remainder
661 return rem_ & masking_type::sig_bits;
664 template < std::size_t Bits >
667 crc_basic<Bits>::reset
669 typename crc_basic<Bits>::value_type new_rem
675 template < std::size_t Bits >
678 crc_basic<Bits>::reset
682 this->reset( this->get_initial_remainder() );
685 template < std::size_t Bits >
688 crc_basic<Bits>::process_bit
693 value_type const high_bit_mask = masking_type::high_bit;
695 // compare the new bit with the remainder's highest
696 rem_ ^= ( bit ? high_bit_mask : 0u );
698 // a full polynominal division step is done when the highest bit is one
699 bool const do_poly_div = static_cast<bool>( rem_ & high_bit_mask );
701 // shift out the highest bit
704 // carry out the division, if needed
711 template < std::size_t Bits >
713 crc_basic<Bits>::process_bits
716 std::size_t bit_count
719 // ignore the bits above the ones we want
720 bits <<= CHAR_BIT - bit_count;
722 // compute the CRC for each bit, starting with the upper ones
723 unsigned char const high_bit_mask = 1u << ( CHAR_BIT - 1u );
724 for ( std::size_t i = bit_count ; i > 0u ; --i, bits <<= 1u )
726 process_bit( static_cast<bool>(bits & high_bit_mask) );
730 template < std::size_t Bits >
733 crc_basic<Bits>::process_byte
738 process_bits( (rft_in_ ? detail::reflector<CHAR_BIT>::reflect(byte)
742 template < std::size_t Bits >
744 crc_basic<Bits>::process_block
746 void const * bytes_begin,
747 void const * bytes_end
750 for ( unsigned char const * p
751 = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
757 template < std::size_t Bits >
760 crc_basic<Bits>::process_bytes
763 std::size_t byte_count
766 unsigned char const * const b = static_cast<unsigned char const *>(
769 process_block( b, b + byte_count );
772 template < std::size_t Bits >
774 typename crc_basic<Bits>::value_type
775 crc_basic<Bits>::checksum
779 return ( (rft_out_ ? detail::reflector<Bits>::reflect( rem_ ) : rem_)
780 ^ final_ ) & masking_type::sig_bits;
784 // Optimized CRC class function definitions --------------------------------//
786 // Macro to compact code
787 #define BOOST_CRC_OPTIMAL_NAME crc_optimal<Bits, TruncPoly, InitRem, \
788 FinalXor, ReflectIn, ReflectRem>
790 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
791 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
792 bool ReflectIn, bool ReflectRem >
794 BOOST_CRC_OPTIMAL_NAME::crc_optimal
796 typename BOOST_CRC_OPTIMAL_NAME::value_type init_rem // = InitRem
798 : rem_( helper_type::reflect(init_rem) )
800 crc_table_type::init_table();
803 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
804 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
805 bool ReflectIn, bool ReflectRem >
807 typename BOOST_CRC_OPTIMAL_NAME::value_type
808 BOOST_CRC_OPTIMAL_NAME::get_truncated_polynominal
815 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
816 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
817 bool ReflectIn, bool ReflectRem >
819 typename BOOST_CRC_OPTIMAL_NAME::value_type
820 BOOST_CRC_OPTIMAL_NAME::get_initial_remainder
827 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
828 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
829 bool ReflectIn, bool ReflectRem >
831 typename BOOST_CRC_OPTIMAL_NAME::value_type
832 BOOST_CRC_OPTIMAL_NAME::get_final_xor_value
839 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
840 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
841 bool ReflectIn, bool ReflectRem >
844 BOOST_CRC_OPTIMAL_NAME::get_reflect_input
851 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
852 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
853 bool ReflectIn, bool ReflectRem >
856 BOOST_CRC_OPTIMAL_NAME::get_reflect_remainder
863 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
864 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
865 bool ReflectIn, bool ReflectRem >
867 typename BOOST_CRC_OPTIMAL_NAME::value_type
868 BOOST_CRC_OPTIMAL_NAME::get_interim_remainder
872 // Interim remainder should be _un_-reflected, so we have to undo it.
873 return helper_type::reflect( rem_ ) & masking_type::sig_bits_fast;
876 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
877 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
878 bool ReflectIn, bool ReflectRem >
881 BOOST_CRC_OPTIMAL_NAME::reset
883 typename BOOST_CRC_OPTIMAL_NAME::value_type new_rem // = InitRem
886 rem_ = helper_type::reflect( new_rem );
889 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
890 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
891 bool ReflectIn, bool ReflectRem >
894 BOOST_CRC_OPTIMAL_NAME::process_byte
899 process_bytes( &byte, sizeof(byte) );
902 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
903 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
904 bool ReflectIn, bool ReflectRem >
906 BOOST_CRC_OPTIMAL_NAME::process_block
908 void const * bytes_begin,
909 void const * bytes_end
912 // Recompute the CRC for each byte passed
913 for ( unsigned char const * p
914 = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
916 // Compare the new byte with the remainder's higher bits to
917 // get the new bits, shift out the remainder's current higher
918 // bits, and update the remainder with the polynominal division
920 unsigned char const byte_index = helper_type::index( rem_, *p );
921 rem_ = helper_type::shift( rem_ );
922 rem_ ^= crc_table_type::table_[ byte_index ];
926 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
927 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
928 bool ReflectIn, bool ReflectRem >
931 BOOST_CRC_OPTIMAL_NAME::process_bytes
934 std::size_t byte_count
937 unsigned char const * const b = static_cast<unsigned char const *>(
939 process_block( b, b + byte_count );
942 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
943 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
944 bool ReflectIn, bool ReflectRem >
946 typename BOOST_CRC_OPTIMAL_NAME::value_type
947 BOOST_CRC_OPTIMAL_NAME::checksum
951 return ( reflect_out_type::reflect(rem_) ^ get_final_xor_value() )
952 & masking_type::sig_bits_fast;
955 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
956 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
957 bool ReflectIn, bool ReflectRem >
960 BOOST_CRC_OPTIMAL_NAME::operator ()
965 process_byte( byte );
968 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
969 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
970 bool ReflectIn, bool ReflectRem >
972 typename BOOST_CRC_OPTIMAL_NAME::value_type
973 BOOST_CRC_OPTIMAL_NAME::operator ()
981 // CRC computation function definition -------------------------------------//
983 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
984 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
985 bool ReflectIn, bool ReflectRem >
987 typename uint_t<Bits>::fast
991 std::size_t byte_count
995 BOOST_CRC_OPTIMAL_NAME computer;
996 computer.process_bytes( buffer, byte_count );
997 return computer.checksum();
1001 // Augmented-message CRC computation function definitions ------------------//
1003 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
1004 typename uint_t<Bits>::fast
1007 void const * buffer,
1008 std::size_t byte_count,
1009 typename uint_t<Bits>::fast initial_remainder
1010 BOOST_ACRC_DUMMY_INIT
1013 typedef unsigned char byte_type;
1014 typedef detail::mask_uint_t<Bits> masking_type;
1015 typedef detail::crc_table_t<Bits, TruncPoly, false> crc_table_type;
1017 typename masking_type::fast rem = initial_remainder;
1018 byte_type const * const b = static_cast<byte_type const *>( buffer );
1019 byte_type const * const e = b + byte_count;
1021 crc_table_type::init_table();
1022 for ( byte_type const * p = b ; p < e ; ++p )
1024 // Use the current top byte as the table index to the next
1025 // "partial product." Shift out that top byte, shifting in
1026 // the next augmented-message byte. Complete the division.
1027 byte_type const byte_index = rem >> ( Bits - CHAR_BIT );
1030 rem ^= crc_table_type::table_[ byte_index ];
1033 return rem & masking_type::sig_bits_fast;
1036 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
1038 typename uint_t<Bits>::fast
1041 void const * buffer,
1042 std::size_t byte_count
1043 BOOST_ACRC_DUMMY_INIT
1046 // The last function argument has its type specified so the other version of
1047 // augmented_crc will be called. If the cast wasn't in place, and the
1048 // BOOST_ACRC_DUMMY_INIT added a third argument (for a workaround), the "0"
1049 // would match as that third argument, leading to infinite recursion.
1050 return augmented_crc<Bits, TruncPoly>( buffer, byte_count,
1051 static_cast<typename uint_t<Bits>::fast>(0) );
1055 } // namespace boost
1058 // Undo header-private macros
1059 #undef BOOST_CRC_OPTIMAL_NAME
1060 #undef BOOST_ACRC_DUMMY_INIT
1061 #undef BOOST_ACRC_DUMMY_PARM_TYPE
1062 #undef BOOST_CRC_DUMMY_INIT
1063 #undef BOOST_CRC_DUMMY_PARM_TYPE
1064 #undef BOOST_CRC_PARM_TYPE
1067 #endif // BOOST_CRC_HPP