crc.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // Boost CRC library crc.hpp header file -----------------------------------//
  2. // Copyright 2001, 2004 Daryle Walker. Use, modification, and distribution are
  3. // subject to the Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. // See <http://www.boost.org/libs/crc/> for the library's home page.
  6. #ifndef BOOST_CRC_HPP
  7. #define BOOST_CRC_HPP
  8. #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
  9. #include <boost/integer.hpp> // for boost::uint_t
  10. #include <climits> // for CHAR_BIT, etc.
  11. #include <cstddef> // for std::size_t
  12. #include <boost/limits.hpp> // for std::numeric_limits
  13. // The type of CRC parameters that can go in a template should be related
  14. // on the CRC's bit count. This macro expresses that type in a compact
  15. // form, but also allows an alternate type for compilers that don't support
  16. // dependent types (in template value-parameters).
  17. #if !(defined(BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS) || (defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)))
  18. #define BOOST_CRC_PARM_TYPE typename ::boost::uint_t<Bits>::fast
  19. #else
  20. #define BOOST_CRC_PARM_TYPE unsigned long
  21. #endif
  22. // Some compilers [MS VC++ 6] cannot correctly set up several versions of a
  23. // function template unless every template argument can be unambiguously
  24. // deduced from the function arguments. (The bug is hidden if only one version
  25. // is needed.) Since all of the CRC function templates have this problem, the
  26. // workaround is to make up a dummy function argument that encodes the template
  27. // arguments. Calls to such template functions need all their template
  28. // arguments explicitly specified. At least one compiler that needs this
  29. // workaround also needs the default value for the dummy argument to be
  30. // specified in the definition.
  31. #if defined(__GNUC__) || !defined(BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
  32. #define BOOST_CRC_DUMMY_PARM_TYPE
  33. #define BOOST_CRC_DUMMY_INIT
  34. #define BOOST_ACRC_DUMMY_PARM_TYPE
  35. #define BOOST_ACRC_DUMMY_INIT
  36. #else
  37. namespace boost { namespace detail {
  38. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  39. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  40. bool ReflectIn, bool ReflectRem >
  41. struct dummy_crc_argument { };
  42. } }
  43. #define BOOST_CRC_DUMMY_PARM_TYPE , detail::dummy_crc_argument<Bits, \
  44. TruncPoly, InitRem, FinalXor, ReflectIn, ReflectRem> *p_
  45. #define BOOST_CRC_DUMMY_INIT BOOST_CRC_DUMMY_PARM_TYPE = 0
  46. #define BOOST_ACRC_DUMMY_PARM_TYPE , detail::dummy_crc_argument<Bits, \
  47. TruncPoly, 0, 0, false, false> *p_
  48. #define BOOST_ACRC_DUMMY_INIT BOOST_ACRC_DUMMY_PARM_TYPE = 0
  49. #endif
  50. namespace boost
  51. {
  52. // Forward declarations ----------------------------------------------------//
  53. template < std::size_t Bits >
  54. class crc_basic;
  55. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly = 0u,
  56. BOOST_CRC_PARM_TYPE InitRem = 0u,
  57. BOOST_CRC_PARM_TYPE FinalXor = 0u, bool ReflectIn = false,
  58. bool ReflectRem = false >
  59. class crc_optimal;
  60. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  61. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  62. bool ReflectIn, bool ReflectRem >
  63. typename uint_t<Bits>::fast crc( void const *buffer,
  64. std::size_t byte_count
  65. BOOST_CRC_DUMMY_PARM_TYPE );
  66. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
  67. typename uint_t<Bits>::fast augmented_crc( void const *buffer,
  68. std::size_t byte_count, typename uint_t<Bits>::fast initial_remainder
  69. BOOST_ACRC_DUMMY_PARM_TYPE );
  70. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
  71. typename uint_t<Bits>::fast augmented_crc( void const *buffer,
  72. std::size_t byte_count
  73. BOOST_ACRC_DUMMY_PARM_TYPE );
  74. typedef crc_optimal<16, 0x8005, 0, 0, true, true> crc_16_type;
  75. typedef crc_optimal<16, 0x1021, 0xFFFF, 0, false, false> crc_ccitt_type;
  76. typedef crc_optimal<16, 0x8408, 0, 0, true, true> crc_xmodem_type;
  77. typedef crc_optimal<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true>
  78. crc_32_type;
  79. // Forward declarations for implementation detail stuff --------------------//
  80. // (Just for the stuff that will be needed for the next two sections)
  81. namespace detail
  82. {
  83. template < std::size_t Bits >
  84. struct mask_uint_t;
  85. template < >
  86. struct mask_uint_t< std::numeric_limits<unsigned char>::digits >;
  87. #if USHRT_MAX > UCHAR_MAX
  88. template < >
  89. struct mask_uint_t< std::numeric_limits<unsigned short>::digits >;
  90. #endif
  91. #if UINT_MAX > USHRT_MAX
  92. template < >
  93. struct mask_uint_t< std::numeric_limits<unsigned int>::digits >;
  94. #endif
  95. #if ULONG_MAX > UINT_MAX
  96. template < >
  97. struct mask_uint_t< std::numeric_limits<unsigned long>::digits >;
  98. #endif
  99. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
  100. struct crc_table_t;
  101. template < std::size_t Bits, bool DoReflect >
  102. class crc_helper;
  103. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  104. template < std::size_t Bits >
  105. class crc_helper< Bits, false >;
  106. #endif
  107. } // namespace detail
  108. // Simple cyclic redundancy code (CRC) class declaration -------------------//
  109. template < std::size_t Bits >
  110. class crc_basic
  111. {
  112. // Implementation type
  113. typedef detail::mask_uint_t<Bits> masking_type;
  114. public:
  115. // Type
  116. typedef typename masking_type::least value_type;
  117. // Constant for the template parameter
  118. BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
  119. // Constructor
  120. explicit crc_basic( value_type truncated_polynominal,
  121. value_type initial_remainder = 0, value_type final_xor_value = 0,
  122. bool reflect_input = false, bool reflect_remainder = false );
  123. // Internal Operations
  124. value_type get_truncated_polynominal() const;
  125. value_type get_initial_remainder() const;
  126. value_type get_final_xor_value() const;
  127. bool get_reflect_input() const;
  128. bool get_reflect_remainder() const;
  129. value_type get_interim_remainder() const;
  130. void reset( value_type new_rem );
  131. void reset();
  132. // External Operations
  133. void process_bit( bool bit );
  134. void process_bits( unsigned char bits, std::size_t bit_count );
  135. void process_byte( unsigned char byte );
  136. void process_block( void const *bytes_begin, void const *bytes_end );
  137. void process_bytes( void const *buffer, std::size_t byte_count );
  138. value_type checksum() const;
  139. private:
  140. // Member data
  141. value_type rem_;
  142. value_type poly_, init_, final_; // non-const to allow assignability
  143. bool rft_in_, rft_out_; // non-const to allow assignability
  144. }; // boost::crc_basic
  145. // Optimized cyclic redundancy code (CRC) class declaration ----------------//
  146. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  147. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  148. bool ReflectIn, bool ReflectRem >
  149. class crc_optimal
  150. {
  151. // Implementation type
  152. typedef detail::mask_uint_t<Bits> masking_type;
  153. public:
  154. // Type
  155. typedef typename masking_type::fast value_type;
  156. // Constants for the template parameters
  157. BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
  158. BOOST_STATIC_CONSTANT( value_type, truncated_polynominal = TruncPoly );
  159. BOOST_STATIC_CONSTANT( value_type, initial_remainder = InitRem );
  160. BOOST_STATIC_CONSTANT( value_type, final_xor_value = FinalXor );
  161. BOOST_STATIC_CONSTANT( bool, reflect_input = ReflectIn );
  162. BOOST_STATIC_CONSTANT( bool, reflect_remainder = ReflectRem );
  163. // Constructor
  164. explicit crc_optimal( value_type init_rem = InitRem );
  165. // Internal Operations
  166. value_type get_truncated_polynominal() const;
  167. value_type get_initial_remainder() const;
  168. value_type get_final_xor_value() const;
  169. bool get_reflect_input() const;
  170. bool get_reflect_remainder() const;
  171. value_type get_interim_remainder() const;
  172. void reset( value_type new_rem = InitRem );
  173. // External Operations
  174. void process_byte( unsigned char byte );
  175. void process_block( void const *bytes_begin, void const *bytes_end );
  176. void process_bytes( void const *buffer, std::size_t byte_count );
  177. value_type checksum() const;
  178. // Operators
  179. void operator ()( unsigned char byte );
  180. value_type operator ()() const;
  181. private:
  182. // The implementation of output reflection depends on both reflect states.
  183. BOOST_STATIC_CONSTANT( bool, reflect_output = (ReflectRem != ReflectIn) );
  184. #ifndef __BORLANDC__
  185. #define BOOST_CRC_REF_OUT_VAL reflect_output
  186. #else
  187. typedef crc_optimal self_type;
  188. #define BOOST_CRC_REF_OUT_VAL (self_type::reflect_output)
  189. #endif
  190. // More implementation types
  191. typedef detail::crc_table_t<Bits, TruncPoly, ReflectIn> crc_table_type;
  192. typedef detail::crc_helper<Bits, ReflectIn> helper_type;
  193. typedef detail::crc_helper<Bits, BOOST_CRC_REF_OUT_VAL> reflect_out_type;
  194. #undef BOOST_CRC_REF_OUT_VAL
  195. // Member data
  196. value_type rem_;
  197. }; // boost::crc_optimal
  198. // Implementation detail stuff ---------------------------------------------//
  199. namespace detail
  200. {
  201. // Forward declarations for more implementation details
  202. template < std::size_t Bits >
  203. struct high_uint_t;
  204. template < std::size_t Bits >
  205. struct reflector;
  206. // Traits class for mask; given the bit number
  207. // (1-based), get the mask for that bit by itself.
  208. template < std::size_t Bits >
  209. struct high_uint_t
  210. : boost::uint_t< Bits >
  211. {
  212. typedef boost::uint_t<Bits> base_type;
  213. typedef typename base_type::least least;
  214. typedef typename base_type::fast fast;
  215. #if defined(__EDG_VERSION__) && __EDG_VERSION__ <= 243
  216. static const least high_bit = 1ul << ( Bits - 1u );
  217. static const fast high_bit_fast = 1ul << ( Bits - 1u );
  218. #else
  219. BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << ( Bits
  220. - 1u )) );
  221. BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << ( Bits
  222. - 1u )) );
  223. #endif
  224. }; // boost::detail::high_uint_t
  225. // Reflection routine class wrapper
  226. // (since MS VC++ 6 couldn't handle the unwrapped version)
  227. template < std::size_t Bits >
  228. struct reflector
  229. {
  230. typedef typename boost::uint_t<Bits>::fast value_type;
  231. static value_type reflect( value_type x );
  232. }; // boost::detail::reflector
  233. // Function that reflects its argument
  234. template < std::size_t Bits >
  235. typename reflector<Bits>::value_type
  236. reflector<Bits>::reflect
  237. (
  238. typename reflector<Bits>::value_type x
  239. )
  240. {
  241. value_type reflection = 0;
  242. value_type const one = 1;
  243. for ( std::size_t i = 0 ; i < Bits ; ++i, x >>= 1 )
  244. {
  245. if ( x & one )
  246. {
  247. reflection |= ( one << (Bits - 1u - i) );
  248. }
  249. }
  250. return reflection;
  251. }
  252. // Traits class for masks; given the bit number (1-based),
  253. // get the mask for that bit and its lower bits.
  254. template < std::size_t Bits >
  255. struct mask_uint_t
  256. : high_uint_t< Bits >
  257. {
  258. typedef high_uint_t<Bits> base_type;
  259. typedef typename base_type::least least;
  260. typedef typename base_type::fast fast;
  261. #ifndef __BORLANDC__
  262. using base_type::high_bit;
  263. using base_type::high_bit_fast;
  264. #else
  265. BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
  266. BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
  267. #endif
  268. #if defined(__EDG_VERSION__) && __EDG_VERSION__ <= 243
  269. static const least sig_bits = (~( ~( 0ul ) << Bits )) ;
  270. #else
  271. BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
  272. #endif
  273. #if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 0 && __GNUC_PATCHLEVEL__ == 2
  274. // Work around a weird bug that ICEs the compiler in build_c_cast
  275. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = static_cast<fast>(sig_bits) );
  276. #else
  277. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  278. #endif
  279. }; // boost::detail::mask_uint_t
  280. template < >
  281. struct mask_uint_t< std::numeric_limits<unsigned char>::digits >
  282. : high_uint_t< std::numeric_limits<unsigned char>::digits >
  283. {
  284. typedef high_uint_t<std::numeric_limits<unsigned char>::digits>
  285. base_type;
  286. typedef base_type::least least;
  287. typedef base_type::fast fast;
  288. #ifndef __BORLANDC__
  289. using base_type::high_bit;
  290. using base_type::high_bit_fast;
  291. #else
  292. BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
  293. BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
  294. #endif
  295. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
  296. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  297. }; // boost::detail::mask_uint_t
  298. #if USHRT_MAX > UCHAR_MAX
  299. template < >
  300. struct mask_uint_t< std::numeric_limits<unsigned short>::digits >
  301. : high_uint_t< std::numeric_limits<unsigned short>::digits >
  302. {
  303. typedef high_uint_t<std::numeric_limits<unsigned short>::digits>
  304. base_type;
  305. typedef base_type::least least;
  306. typedef base_type::fast fast;
  307. #ifndef __BORLANDC__
  308. using base_type::high_bit;
  309. using base_type::high_bit_fast;
  310. #else
  311. BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
  312. BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
  313. #endif
  314. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
  315. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  316. }; // boost::detail::mask_uint_t
  317. #endif
  318. #if UINT_MAX > USHRT_MAX
  319. template < >
  320. struct mask_uint_t< std::numeric_limits<unsigned int>::digits >
  321. : high_uint_t< std::numeric_limits<unsigned int>::digits >
  322. {
  323. typedef high_uint_t<std::numeric_limits<unsigned int>::digits>
  324. base_type;
  325. typedef base_type::least least;
  326. typedef base_type::fast fast;
  327. #ifndef __BORLANDC__
  328. using base_type::high_bit;
  329. using base_type::high_bit_fast;
  330. #else
  331. BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
  332. BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
  333. #endif
  334. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
  335. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  336. }; // boost::detail::mask_uint_t
  337. #endif
  338. #if ULONG_MAX > UINT_MAX
  339. template < >
  340. struct mask_uint_t< std::numeric_limits<unsigned long>::digits >
  341. : high_uint_t< std::numeric_limits<unsigned long>::digits >
  342. {
  343. typedef high_uint_t<std::numeric_limits<unsigned long>::digits>
  344. base_type;
  345. typedef base_type::least least;
  346. typedef base_type::fast fast;
  347. #ifndef __BORLANDC__
  348. using base_type::high_bit;
  349. using base_type::high_bit_fast;
  350. #else
  351. BOOST_STATIC_CONSTANT( least, high_bit = base_type::high_bit );
  352. BOOST_STATIC_CONSTANT( fast, high_bit_fast = base_type::high_bit_fast );
  353. #endif
  354. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) );
  355. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  356. }; // boost::detail::mask_uint_t
  357. #endif
  358. // CRC table generator
  359. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
  360. struct crc_table_t
  361. {
  362. BOOST_STATIC_CONSTANT( std::size_t, byte_combos = (1ul << CHAR_BIT) );
  363. typedef mask_uint_t<Bits> masking_type;
  364. typedef typename masking_type::fast value_type;
  365. #if defined(__BORLANDC__) && defined(_M_IX86) && (__BORLANDC__ == 0x560)
  366. // for some reason Borland's command line compiler (version 0x560)
  367. // chokes over this unless we do the calculation for it:
  368. typedef value_type table_type[ 0x100 ];
  369. #elif defined(__GNUC__)
  370. // old versions of GCC (before 4.0.2) choke on using byte_combos
  371. // as a constant expression when compiling with -pedantic.
  372. typedef value_type table_type[1ul << CHAR_BIT];
  373. #else
  374. typedef value_type table_type[ byte_combos ];
  375. #endif
  376. static void init_table();
  377. static table_type table_;
  378. }; // boost::detail::crc_table_t
  379. // CRC table generator static data member definition
  380. // (Some compilers [Borland C++] require the initializer to be present.)
  381. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
  382. typename crc_table_t<Bits, TruncPoly, Reflect>::table_type
  383. crc_table_t<Bits, TruncPoly, Reflect>::table_
  384. = { 0 };
  385. // Populate CRC lookup table
  386. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly, bool Reflect >
  387. void
  388. crc_table_t<Bits, TruncPoly, Reflect>::init_table
  389. (
  390. )
  391. {
  392. // compute table only on the first run
  393. static bool did_init = false;
  394. if ( did_init ) return;
  395. // factor-out constants to avoid recalculation
  396. value_type const fast_hi_bit = masking_type::high_bit_fast;
  397. unsigned char const byte_hi_bit = 1u << (CHAR_BIT - 1u);
  398. // loop over every possible dividend value
  399. unsigned char dividend = 0;
  400. do
  401. {
  402. value_type remainder = 0;
  403. // go through all the dividend's bits
  404. for ( unsigned char mask = byte_hi_bit ; mask ; mask >>= 1 )
  405. {
  406. // check if divisor fits
  407. if ( dividend & mask )
  408. {
  409. remainder ^= fast_hi_bit;
  410. }
  411. // do polynominal division
  412. if ( remainder & fast_hi_bit )
  413. {
  414. remainder <<= 1;
  415. remainder ^= TruncPoly;
  416. }
  417. else
  418. {
  419. remainder <<= 1;
  420. }
  421. }
  422. table_[ crc_helper<CHAR_BIT, Reflect>::reflect(dividend) ]
  423. = crc_helper<Bits, Reflect>::reflect( remainder );
  424. }
  425. while ( ++dividend );
  426. did_init = true;
  427. }
  428. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  429. // Align the msb of the remainder to a byte
  430. template < std::size_t Bits, bool RightShift >
  431. class remainder
  432. {
  433. public:
  434. typedef typename uint_t<Bits>::fast value_type;
  435. static unsigned char align_msb( value_type rem )
  436. { return rem >> (Bits - CHAR_BIT); }
  437. };
  438. // Specialization for the case that the remainder has less
  439. // bits than a byte: align the remainder msb to the byte msb
  440. template < std::size_t Bits >
  441. class remainder< Bits, false >
  442. {
  443. public:
  444. typedef typename uint_t<Bits>::fast value_type;
  445. static unsigned char align_msb( value_type rem )
  446. { return rem << (CHAR_BIT - Bits); }
  447. };
  448. #endif
  449. // CRC helper routines
  450. template < std::size_t Bits, bool DoReflect >
  451. class crc_helper
  452. {
  453. public:
  454. // Type
  455. typedef typename uint_t<Bits>::fast value_type;
  456. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  457. // Possibly reflect a remainder
  458. static value_type reflect( value_type x )
  459. { return detail::reflector<Bits>::reflect( x ); }
  460. // Compare a byte to the remainder's highest byte
  461. static unsigned char index( value_type rem, unsigned char x )
  462. { return x ^ rem; }
  463. // Shift out the remainder's highest byte
  464. static value_type shift( value_type rem )
  465. { return rem >> CHAR_BIT; }
  466. #else
  467. // Possibly reflect a remainder
  468. static value_type reflect( value_type x )
  469. { return DoReflect ? detail::reflector<Bits>::reflect( x ) : x; }
  470. // Compare a byte to the remainder's highest byte
  471. static unsigned char index( value_type rem, unsigned char x )
  472. { return x ^ ( DoReflect ? rem :
  473. ((Bits>CHAR_BIT)?( rem >> (Bits - CHAR_BIT) ) :
  474. ( rem << (CHAR_BIT - Bits) ))); }
  475. // Shift out the remainder's highest byte
  476. static value_type shift( value_type rem )
  477. { return DoReflect ? rem >> CHAR_BIT : rem << CHAR_BIT; }
  478. #endif
  479. }; // boost::detail::crc_helper
  480. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  481. template < std::size_t Bits >
  482. class crc_helper<Bits, false>
  483. {
  484. public:
  485. // Type
  486. typedef typename uint_t<Bits>::fast value_type;
  487. // Possibly reflect a remainder
  488. static value_type reflect( value_type x )
  489. { return x; }
  490. // Compare a byte to the remainder's highest byte
  491. static unsigned char index( value_type rem, unsigned char x )
  492. { return x ^ remainder<Bits,(Bits>CHAR_BIT)>::align_msb( rem ); }
  493. // Shift out the remainder's highest byte
  494. static value_type shift( value_type rem )
  495. { return rem << CHAR_BIT; }
  496. }; // boost::detail::crc_helper
  497. #endif
  498. } // namespace detail
  499. // Simple CRC class function definitions -----------------------------------//
  500. template < std::size_t Bits >
  501. inline
  502. crc_basic<Bits>::crc_basic
  503. (
  504. typename crc_basic<Bits>::value_type truncated_polynominal,
  505. typename crc_basic<Bits>::value_type initial_remainder, // = 0
  506. typename crc_basic<Bits>::value_type final_xor_value, // = 0
  507. bool reflect_input, // = false
  508. bool reflect_remainder // = false
  509. )
  510. : rem_( initial_remainder ), poly_( truncated_polynominal )
  511. , init_( initial_remainder ), final_( final_xor_value )
  512. , rft_in_( reflect_input ), rft_out_( reflect_remainder )
  513. {
  514. }
  515. template < std::size_t Bits >
  516. inline
  517. typename crc_basic<Bits>::value_type
  518. crc_basic<Bits>::get_truncated_polynominal
  519. (
  520. ) const
  521. {
  522. return poly_;
  523. }
  524. template < std::size_t Bits >
  525. inline
  526. typename crc_basic<Bits>::value_type
  527. crc_basic<Bits>::get_initial_remainder
  528. (
  529. ) const
  530. {
  531. return init_;
  532. }
  533. template < std::size_t Bits >
  534. inline
  535. typename crc_basic<Bits>::value_type
  536. crc_basic<Bits>::get_final_xor_value
  537. (
  538. ) const
  539. {
  540. return final_;
  541. }
  542. template < std::size_t Bits >
  543. inline
  544. bool
  545. crc_basic<Bits>::get_reflect_input
  546. (
  547. ) const
  548. {
  549. return rft_in_;
  550. }
  551. template < std::size_t Bits >
  552. inline
  553. bool
  554. crc_basic<Bits>::get_reflect_remainder
  555. (
  556. ) const
  557. {
  558. return rft_out_;
  559. }
  560. template < std::size_t Bits >
  561. inline
  562. typename crc_basic<Bits>::value_type
  563. crc_basic<Bits>::get_interim_remainder
  564. (
  565. ) const
  566. {
  567. return rem_ & masking_type::sig_bits;
  568. }
  569. template < std::size_t Bits >
  570. inline
  571. void
  572. crc_basic<Bits>::reset
  573. (
  574. typename crc_basic<Bits>::value_type new_rem
  575. )
  576. {
  577. rem_ = new_rem;
  578. }
  579. template < std::size_t Bits >
  580. inline
  581. void
  582. crc_basic<Bits>::reset
  583. (
  584. )
  585. {
  586. this->reset( this->get_initial_remainder() );
  587. }
  588. template < std::size_t Bits >
  589. inline
  590. void
  591. crc_basic<Bits>::process_bit
  592. (
  593. bool bit
  594. )
  595. {
  596. value_type const high_bit_mask = masking_type::high_bit;
  597. // compare the new bit with the remainder's highest
  598. rem_ ^= ( bit ? high_bit_mask : 0u );
  599. // a full polynominal division step is done when the highest bit is one
  600. bool const do_poly_div = static_cast<bool>( rem_ & high_bit_mask );
  601. // shift out the highest bit
  602. rem_ <<= 1;
  603. // carry out the division, if needed
  604. if ( do_poly_div )
  605. {
  606. rem_ ^= poly_;
  607. }
  608. }
  609. template < std::size_t Bits >
  610. void
  611. crc_basic<Bits>::process_bits
  612. (
  613. unsigned char bits,
  614. std::size_t bit_count
  615. )
  616. {
  617. // ignore the bits above the ones we want
  618. bits <<= CHAR_BIT - bit_count;
  619. // compute the CRC for each bit, starting with the upper ones
  620. unsigned char const high_bit_mask = 1u << ( CHAR_BIT - 1u );
  621. for ( std::size_t i = bit_count ; i > 0u ; --i, bits <<= 1u )
  622. {
  623. process_bit( static_cast<bool>(bits & high_bit_mask) );
  624. }
  625. }
  626. template < std::size_t Bits >
  627. inline
  628. void
  629. crc_basic<Bits>::process_byte
  630. (
  631. unsigned char byte
  632. )
  633. {
  634. process_bits( (rft_in_ ? detail::reflector<CHAR_BIT>::reflect(byte)
  635. : byte), CHAR_BIT );
  636. }
  637. template < std::size_t Bits >
  638. void
  639. crc_basic<Bits>::process_block
  640. (
  641. void const * bytes_begin,
  642. void const * bytes_end
  643. )
  644. {
  645. for ( unsigned char const * p
  646. = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
  647. {
  648. process_byte( *p );
  649. }
  650. }
  651. template < std::size_t Bits >
  652. inline
  653. void
  654. crc_basic<Bits>::process_bytes
  655. (
  656. void const * buffer,
  657. std::size_t byte_count
  658. )
  659. {
  660. unsigned char const * const b = static_cast<unsigned char const *>(
  661. buffer );
  662. process_block( b, b + byte_count );
  663. }
  664. template < std::size_t Bits >
  665. inline
  666. typename crc_basic<Bits>::value_type
  667. crc_basic<Bits>::checksum
  668. (
  669. ) const
  670. {
  671. return ( (rft_out_ ? detail::reflector<Bits>::reflect( rem_ ) : rem_)
  672. ^ final_ ) & masking_type::sig_bits;
  673. }
  674. // Optimized CRC class function definitions --------------------------------//
  675. // Macro to compact code
  676. #define BOOST_CRC_OPTIMAL_NAME crc_optimal<Bits, TruncPoly, InitRem, \
  677. FinalXor, ReflectIn, ReflectRem>
  678. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  679. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  680. bool ReflectIn, bool ReflectRem >
  681. inline
  682. BOOST_CRC_OPTIMAL_NAME::crc_optimal
  683. (
  684. typename BOOST_CRC_OPTIMAL_NAME::value_type init_rem // = InitRem
  685. )
  686. : rem_( helper_type::reflect(init_rem) )
  687. {
  688. crc_table_type::init_table();
  689. }
  690. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  691. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  692. bool ReflectIn, bool ReflectRem >
  693. inline
  694. typename BOOST_CRC_OPTIMAL_NAME::value_type
  695. BOOST_CRC_OPTIMAL_NAME::get_truncated_polynominal
  696. (
  697. ) const
  698. {
  699. return TruncPoly;
  700. }
  701. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  702. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  703. bool ReflectIn, bool ReflectRem >
  704. inline
  705. typename BOOST_CRC_OPTIMAL_NAME::value_type
  706. BOOST_CRC_OPTIMAL_NAME::get_initial_remainder
  707. (
  708. ) const
  709. {
  710. return InitRem;
  711. }
  712. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  713. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  714. bool ReflectIn, bool ReflectRem >
  715. inline
  716. typename BOOST_CRC_OPTIMAL_NAME::value_type
  717. BOOST_CRC_OPTIMAL_NAME::get_final_xor_value
  718. (
  719. ) const
  720. {
  721. return FinalXor;
  722. }
  723. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  724. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  725. bool ReflectIn, bool ReflectRem >
  726. inline
  727. bool
  728. BOOST_CRC_OPTIMAL_NAME::get_reflect_input
  729. (
  730. ) const
  731. {
  732. return ReflectIn;
  733. }
  734. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  735. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  736. bool ReflectIn, bool ReflectRem >
  737. inline
  738. bool
  739. BOOST_CRC_OPTIMAL_NAME::get_reflect_remainder
  740. (
  741. ) const
  742. {
  743. return ReflectRem;
  744. }
  745. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  746. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  747. bool ReflectIn, bool ReflectRem >
  748. inline
  749. typename BOOST_CRC_OPTIMAL_NAME::value_type
  750. BOOST_CRC_OPTIMAL_NAME::get_interim_remainder
  751. (
  752. ) const
  753. {
  754. // Interim remainder should be _un_-reflected, so we have to undo it.
  755. return helper_type::reflect( rem_ ) & masking_type::sig_bits_fast;
  756. }
  757. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  758. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  759. bool ReflectIn, bool ReflectRem >
  760. inline
  761. void
  762. BOOST_CRC_OPTIMAL_NAME::reset
  763. (
  764. typename BOOST_CRC_OPTIMAL_NAME::value_type new_rem // = InitRem
  765. )
  766. {
  767. rem_ = helper_type::reflect( new_rem );
  768. }
  769. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  770. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  771. bool ReflectIn, bool ReflectRem >
  772. inline
  773. void
  774. BOOST_CRC_OPTIMAL_NAME::process_byte
  775. (
  776. unsigned char byte
  777. )
  778. {
  779. process_bytes( &byte, sizeof(byte) );
  780. }
  781. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  782. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  783. bool ReflectIn, bool ReflectRem >
  784. void
  785. BOOST_CRC_OPTIMAL_NAME::process_block
  786. (
  787. void const * bytes_begin,
  788. void const * bytes_end
  789. )
  790. {
  791. // Recompute the CRC for each byte passed
  792. for ( unsigned char const * p
  793. = static_cast<unsigned char const *>(bytes_begin) ; p < bytes_end ; ++p )
  794. {
  795. // Compare the new byte with the remainder's higher bits to
  796. // get the new bits, shift out the remainder's current higher
  797. // bits, and update the remainder with the polynominal division
  798. // of the new bits.
  799. unsigned char const byte_index = helper_type::index( rem_, *p );
  800. rem_ = helper_type::shift( rem_ );
  801. rem_ ^= crc_table_type::table_[ byte_index ];
  802. }
  803. }
  804. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  805. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  806. bool ReflectIn, bool ReflectRem >
  807. inline
  808. void
  809. BOOST_CRC_OPTIMAL_NAME::process_bytes
  810. (
  811. void const * buffer,
  812. std::size_t byte_count
  813. )
  814. {
  815. unsigned char const * const b = static_cast<unsigned char const *>(
  816. buffer );
  817. process_block( b, b + byte_count );
  818. }
  819. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  820. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  821. bool ReflectIn, bool ReflectRem >
  822. inline
  823. typename BOOST_CRC_OPTIMAL_NAME::value_type
  824. BOOST_CRC_OPTIMAL_NAME::checksum
  825. (
  826. ) const
  827. {
  828. return ( reflect_out_type::reflect(rem_) ^ get_final_xor_value() )
  829. & masking_type::sig_bits_fast;
  830. }
  831. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  832. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  833. bool ReflectIn, bool ReflectRem >
  834. inline
  835. void
  836. BOOST_CRC_OPTIMAL_NAME::operator ()
  837. (
  838. unsigned char byte
  839. )
  840. {
  841. process_byte( byte );
  842. }
  843. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  844. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  845. bool ReflectIn, bool ReflectRem >
  846. inline
  847. typename BOOST_CRC_OPTIMAL_NAME::value_type
  848. BOOST_CRC_OPTIMAL_NAME::operator ()
  849. (
  850. ) const
  851. {
  852. return checksum();
  853. }
  854. // CRC computation function definition -------------------------------------//
  855. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
  856. BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
  857. bool ReflectIn, bool ReflectRem >
  858. inline
  859. typename uint_t<Bits>::fast
  860. crc
  861. (
  862. void const * buffer,
  863. std::size_t byte_count
  864. BOOST_CRC_DUMMY_INIT
  865. )
  866. {
  867. BOOST_CRC_OPTIMAL_NAME computer;
  868. computer.process_bytes( buffer, byte_count );
  869. return computer.checksum();
  870. }
  871. // Augmented-message CRC computation function definitions ------------------//
  872. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
  873. typename uint_t<Bits>::fast
  874. augmented_crc
  875. (
  876. void const * buffer,
  877. std::size_t byte_count,
  878. typename uint_t<Bits>::fast initial_remainder
  879. BOOST_ACRC_DUMMY_INIT
  880. )
  881. {
  882. typedef unsigned char byte_type;
  883. typedef detail::mask_uint_t<Bits> masking_type;
  884. typedef detail::crc_table_t<Bits, TruncPoly, false> crc_table_type;
  885. typename masking_type::fast rem = initial_remainder;
  886. byte_type const * const b = static_cast<byte_type const *>( buffer );
  887. byte_type const * const e = b + byte_count;
  888. crc_table_type::init_table();
  889. for ( byte_type const * p = b ; p < e ; ++p )
  890. {
  891. // Use the current top byte as the table index to the next
  892. // "partial product." Shift out that top byte, shifting in
  893. // the next augmented-message byte. Complete the division.
  894. byte_type const byte_index = rem >> ( Bits - CHAR_BIT );
  895. rem <<= CHAR_BIT;
  896. rem |= *p;
  897. rem ^= crc_table_type::table_[ byte_index ];
  898. }
  899. return rem & masking_type::sig_bits_fast;
  900. }
  901. template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly >
  902. inline
  903. typename uint_t<Bits>::fast
  904. augmented_crc
  905. (
  906. void const * buffer,
  907. std::size_t byte_count
  908. BOOST_ACRC_DUMMY_INIT
  909. )
  910. {
  911. // The last function argument has its type specified so the other version of
  912. // augmented_crc will be called. If the cast wasn't in place, and the
  913. // BOOST_ACRC_DUMMY_INIT added a third argument (for a workaround), the "0"
  914. // would match as that third argument, leading to infinite recursion.
  915. return augmented_crc<Bits, TruncPoly>( buffer, byte_count,
  916. static_cast<typename uint_t<Bits>::fast>(0) );
  917. }
  918. } // namespace boost
  919. // Undo header-private macros
  920. #undef BOOST_CRC_OPTIMAL_NAME
  921. #undef BOOST_ACRC_DUMMY_INIT
  922. #undef BOOST_ACRC_DUMMY_PARM_TYPE
  923. #undef BOOST_CRC_DUMMY_INIT
  924. #undef BOOST_CRC_DUMMY_PARM_TYPE
  925. #undef BOOST_CRC_PARM_TYPE
  926. #endif // BOOST_CRC_HPP