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 for updates, documentation, and revision history.
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 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
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.
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 typedef value_type table_type[ byte_combos ];
453 static void init_table();
455 static table_type table_;
457 }; // boost::detail::crc_table_t
459 // CRC table generator static data member definition
460 // (Some compilers [Borland C++] require the initializer to be present.)
461 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
462 typename crc_table_t<Bits, TruncPoly, Reflect>::table_type
463 crc_table_t<Bits, TruncPoly, Reflect>::table_
466 // Populate CRC lookup table
467 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
469 crc_table_t<Bits, TruncPoly, Reflect>::init_table
473 // compute table only on the first run
474 static bool did_init = false;
475 if ( did_init ) return;
477 // factor-out constants to avoid recalculation
478 value_type const fast_hi_bit = masking_type::high_bit_fast;
479 unsigned char const byte_hi_bit = 1u << (CHAR_BIT - 1u);
481 // loop over every possible dividend value
482 unsigned char dividend = 0;
485 value_type remainder = 0;
487 // go through all the dividend's bits
488 for ( unsigned char mask = byte_hi_bit ; mask ; mask >>= 1 )
490 // check if divisor fits
491 if ( dividend & mask )
493 remainder ^= fast_hi_bit;
496 // do polynominal division
497 if ( remainder & fast_hi_bit )
500 remainder ^= TruncPoly;
508 table_[ crc_helper<CHAR_BIT, Reflect>::reflect(dividend) ]
509 = crc_helper<Bits, Reflect>::reflect( remainder );
511 while ( ++dividend );
517 // CRC helper routines
518 template < std::size_t Bits, bool DoReflect >
523 typedef typename uint_t<Bits>::fast value_type;
525 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
526 // Possibly reflect a remainder
527 static value_type reflect( value_type x )
528 { return detail::reflector<Bits>::reflect( x ); }
530 // Compare a byte to the remainder's highest byte
531 static unsigned char index( value_type rem, unsigned char x )
534 // Shift out the remainder's highest byte
535 static value_type shift( value_type rem )
536 { return rem >> CHAR_BIT; }
538 // Possibly reflect a remainder
539 static value_type reflect( value_type x )
540 { return DoReflect ? detail::reflector<Bits>::reflect( x ) : x; }
542 // Compare a byte to the remainder's highest byte
543 static unsigned char index( value_type rem, unsigned char x )
544 { return x ^ ( rem >> (DoReflect ? 0u : Bits - CHAR_BIT) ); }
546 // Shift out the remainder's highest byte
547 static value_type shift( value_type rem )
548 { return DoReflect ? rem >> CHAR_BIT : rem << CHAR_BIT; }
551 }; // boost::detail::crc_helper
553 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
554 template < std::size_t Bits >
555 class crc_helper<Bits, false>
559 typedef typename uint_t<Bits>::fast value_type;
561 // Possibly reflect a remainder
562 static value_type reflect( value_type x )
565 // Compare a byte to the remainder's highest byte
566 static unsigned char index( value_type rem, unsigned char x )
567 { return x ^ ( rem >> (Bits - CHAR_BIT) ); }
569 // Shift out the remainder's highest byte
570 static value_type shift( value_type rem )
571 { return rem << CHAR_BIT; }
573 }; // boost::detail::crc_helper
577 } // namespace detail
580 // Simple CRC class function definitions -----------------------------------//
582 template < std::size_t Bits >
584 crc_basic<Bits>::crc_basic
586 typename crc_basic<Bits>::value_type truncated_polynominal,
587 typename crc_basic<Bits>::value_type initial_remainder, // = 0
588 typename crc_basic<Bits>::value_type final_xor_value, // = 0
589 bool reflect_input, // = false
590 bool reflect_remainder // = false
592 : rem_( initial_remainder ), poly_( truncated_polynominal )
593 , init_( initial_remainder ), final_( final_xor_value )
594 , rft_in_( reflect_input ), rft_out_( reflect_remainder )
598 template < std::size_t Bits >
600 typename crc_basic<Bits>::value_type
601 crc_basic<Bits>::get_truncated_polynominal
608 template < std::size_t Bits >
610 typename crc_basic<Bits>::value_type
611 crc_basic<Bits>::get_initial_remainder
618 template < std::size_t Bits >
620 typename crc_basic<Bits>::value_type
621 crc_basic<Bits>::get_final_xor_value
628 template < std::size_t Bits >
631 crc_basic<Bits>::get_reflect_input
638 template < std::size_t Bits >
641 crc_basic<Bits>::get_reflect_remainder
648 template < std::size_t Bits >
650 typename crc_basic<Bits>::value_type
651 crc_basic<Bits>::get_interim_remainder
655 return rem_ & masking_type::sig_bits;
658 template < std::size_t Bits >
661 crc_basic<Bits>::reset
663 typename crc_basic<Bits>::value_type new_rem
669 template < std::size_t Bits >
672 crc_basic<Bits>::reset
676 this->reset( this->get_initial_remainder() );
679 template < std::size_t Bits >
682 crc_basic<Bits>::process_bit
687 value_type const high_bit_mask = masking_type::high_bit;
689 // compare the new bit with the remainder's highest
690 rem_ ^= ( bit ? high_bit_mask : 0u );
692 // a full polynominal division step is done when the highest bit is one
693 bool const do_poly_div = static_cast<bool>( rem_ & high_bit_mask );
695 // shift out the highest bit
698 // carry out the division, if needed
705 template < std::size_t Bits >
707 crc_basic<Bits>::process_bits
710 std::size_t bit_count
713 // ignore the bits above the ones we want
714 bits <<= CHAR_BIT - bit_count;
716 // compute the CRC for each bit, starting with the upper ones
717 unsigned char const high_bit_mask = 1u << ( CHAR_BIT - 1u );
718 for ( std::size_t i = bit_count ; i > 0u ; --i, bits <<= 1u )
720 process_bit( static_cast<bool>(bits & high_bit_mask) );
724 template < std::size_t Bits >
727 crc_basic<Bits>::process_byte
732 process_bits( (rft_in_ ? detail::reflector<CHAR_BIT>::reflect(byte)
736 template < std::size_t Bits >
738 crc_basic<Bits>::process_block
740 void const * bytes_begin,
741 void const * bytes_end
744 for ( unsigned char const * p
745 = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
751 template < std::size_t Bits >
754 crc_basic<Bits>::process_bytes
757 std::size_t byte_count
760 unsigned char const * const b = static_cast<unsigned char const *>(
763 process_block( b, b + byte_count );
766 template < std::size_t Bits >
768 typename crc_basic<Bits>::value_type
769 crc_basic<Bits>::checksum
773 return ( (rft_out_ ? detail::reflector<Bits>::reflect( rem_ ) : rem_)
774 ^ final_ ) & masking_type::sig_bits;
778 // Optimized CRC class function definitions --------------------------------//
780 // Macro to compact code
781 #define BOOST_CRC_OPTIMAL_NAME crc_optimal<Bits, TruncPoly, InitRem, \
782 FinalXor, ReflectIn, ReflectRem>
784 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
785 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
786 bool ReflectIn, bool ReflectRem >
788 BOOST_CRC_OPTIMAL_NAME::crc_optimal
790 typename BOOST_CRC_OPTIMAL_NAME::value_type init_rem // = InitRem
792 : rem_( helper_type::reflect(init_rem) )
794 crc_table_type::init_table();
797 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
798 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
799 bool ReflectIn, bool ReflectRem >
801 typename BOOST_CRC_OPTIMAL_NAME::value_type
802 BOOST_CRC_OPTIMAL_NAME::get_truncated_polynominal
809 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
810 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
811 bool ReflectIn, bool ReflectRem >
813 typename BOOST_CRC_OPTIMAL_NAME::value_type
814 BOOST_CRC_OPTIMAL_NAME::get_initial_remainder
821 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
822 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
823 bool ReflectIn, bool ReflectRem >
825 typename BOOST_CRC_OPTIMAL_NAME::value_type
826 BOOST_CRC_OPTIMAL_NAME::get_final_xor_value
833 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
834 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
835 bool ReflectIn, bool ReflectRem >
838 BOOST_CRC_OPTIMAL_NAME::get_reflect_input
845 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
846 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
847 bool ReflectIn, bool ReflectRem >
850 BOOST_CRC_OPTIMAL_NAME::get_reflect_remainder
857 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
858 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
859 bool ReflectIn, bool ReflectRem >
861 typename BOOST_CRC_OPTIMAL_NAME::value_type
862 BOOST_CRC_OPTIMAL_NAME::get_interim_remainder
866 // Interim remainder should be _un_-reflected, so we have to undo it.
867 return helper_type::reflect( rem_ ) & masking_type::sig_bits_fast;
870 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
871 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
872 bool ReflectIn, bool ReflectRem >
875 BOOST_CRC_OPTIMAL_NAME::reset
877 typename BOOST_CRC_OPTIMAL_NAME::value_type new_rem // = InitRem
880 rem_ = helper_type::reflect( new_rem );
883 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
884 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
885 bool ReflectIn, bool ReflectRem >
888 BOOST_CRC_OPTIMAL_NAME::process_byte
893 process_bytes( &byte, sizeof(byte) );
896 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
897 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
898 bool ReflectIn, bool ReflectRem >
900 BOOST_CRC_OPTIMAL_NAME::process_block
902 void const * bytes_begin,
903 void const * bytes_end
906 // Recompute the CRC for each byte passed
907 for ( unsigned char const * p
908 = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
910 // Compare the new byte with the remainder's higher bits to
911 // get the new bits, shift out the remainder's current higher
912 // bits, and update the remainder with the polynominal division
914 unsigned char const byte_index = helper_type::index( rem_, *p );
915 rem_ = helper_type::shift( rem_ );
916 rem_ ^= crc_table_type::table_[ byte_index ];
920 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
921 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
922 bool ReflectIn, bool ReflectRem >
925 BOOST_CRC_OPTIMAL_NAME::process_bytes
928 std::size_t byte_count
931 unsigned char const * const b = static_cast<unsigned char const *>(
933 process_block( b, b + byte_count );
936 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
937 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
938 bool ReflectIn, bool ReflectRem >
940 typename BOOST_CRC_OPTIMAL_NAME::value_type
941 BOOST_CRC_OPTIMAL_NAME::checksum
945 return ( reflect_out_type::reflect(rem_) ^ get_final_xor_value() )
946 & masking_type::sig_bits_fast;
949 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
950 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
951 bool ReflectIn, bool ReflectRem >
954 BOOST_CRC_OPTIMAL_NAME::operator ()
959 process_byte( byte );
962 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
963 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
964 bool ReflectIn, bool ReflectRem >
966 typename BOOST_CRC_OPTIMAL_NAME::value_type
967 BOOST_CRC_OPTIMAL_NAME::operator ()
975 // CRC computation function definition -------------------------------------//
977 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
978 BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
979 bool ReflectIn, bool ReflectRem >
981 typename uint_t<Bits>::fast
985 std::size_t byte_count
989 BOOST_CRC_OPTIMAL_NAME computer;
990 computer.process_bytes( buffer, byte_count );
991 return computer.checksum();
995 // Augmented-message CRC computation function definitions ------------------//
997 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
998 typename uint_t<Bits>::fast
1001 void const * buffer,
1002 std::size_t byte_count,
1003 typename uint_t<Bits>::fast initial_remainder
1004 BOOST_ACRC_DUMMY_INIT
1007 typedef unsigned char byte_type;
1008 typedef detail::mask_uint_t<Bits> masking_type;
1009 typedef detail::crc_table_t<Bits, TruncPoly, false> crc_table_type;
1011 typename masking_type::fast rem = initial_remainder;
1012 byte_type const * const b = static_cast<byte_type const *>( buffer );
1013 byte_type const * const e = b + byte_count;
1015 crc_table_type::init_table();
1016 for ( byte_type const * p = b ; p < e ; ++p )
1018 // Use the current top byte as the table index to the next
1019 // "partial product." Shift out that top byte, shifting in
1020 // the next augmented-message byte. Complete the division.
1021 byte_type const byte_index = rem >> ( Bits - CHAR_BIT );
1024 rem ^= crc_table_type::table_[ byte_index ];
1027 return rem & masking_type::sig_bits_fast;
1030 template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
1032 typename uint_t<Bits>::fast
1035 void const * buffer,
1036 std::size_t byte_count
1037 BOOST_ACRC_DUMMY_INIT
1040 // The last function argument has its type specified so the other version of
1041 // augmented_crc will be called. If the cast wasn't in place, and the
1042 // BOOST_ACRC_DUMMY_INIT added a third argument (for a workaround), the "0"
1043 // would match as that third argument, leading to infinite recursion.
1044 return augmented_crc<Bits, TruncPoly>( buffer, byte_count,
1045 static_cast<typename uint_t<Bits>::fast>(0) );
1049 } // namespace boost
1052 // Undo header-private macros
1053 #undef BOOST_CRC_OPTIMAL_NAME
1054 #undef BOOST_ACRC_DUMMY_INIT
1055 #undef BOOST_ACRC_DUMMY_PARM_TYPE
1056 #undef BOOST_CRC_DUMMY_INIT
1057 #undef BOOST_CRC_DUMMY_PARM_TYPE
1058 #undef BOOST_CRC_PARM_TYPE
1061 #endif // BOOST_CRC_HPP