crc_examples.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Boost CRC library documentation examples file ---------------------------//
  2. // Copyright 2012 Daryle Walker.
  3. // Distributed under the Boost Software License, Version 1.0. (See the
  4. // accompanying file LICENSE_1_0.txt or a copy at
  5. // <http://www.boost.org/LICENSE_1_0.txt>.)
  6. // See <http://www.boost.org/libs/crc/> for the library's home page.
  7. #include "boost/crc.hpp"
  8. #include <cstdarg>
  9. #include <cstddef>
  10. #include <utility>
  11. //[ crc_basic_reuse
  12. //` Here's an example of reuse:
  13. std::pair<unsigned, unsigned> crc_16_and_xmodem( void const *b, std::size_t l )
  14. {
  15. std::pair<unsigned, unsigned> result;
  16. /*<< The parameters are based on `boost::crc_16_type`. >>*/
  17. boost::crc_basic<16> crc1( 0x8005u, 0u, 0u, true, true );
  18. crc1.process_bytes( b, l );
  19. result.first = crc1.checksum();
  20. /*<< Change the parameters to match `boost::crc_xmodem_type`. >>*/
  21. crc1 = boost::crc_basic<16>( 0x8408u, crc1.get_initial_remainder(),
  22. crc1.get_final_xor_value(), crc1.get_reflect_input(),
  23. crc1.get_reflect_remainder() );
  24. crc1.process_bytes( b, l );
  25. result.second = crc1.checksum();
  26. return result;
  27. }
  28. /*`
  29. For now, most __RMCA__ parameters can only be changed through assignment to the
  30. entire object.
  31. */
  32. //]
  33. //[ crc_piecemeal_run
  34. //` Persistent objects mean that the data doesn't have to be in one block:
  35. unsigned combined_crc_16( unsigned block_count, ... )
  36. {
  37. /*<< C-style variable-argument routines are or may be macros. >>*/
  38. using namespace std;
  39. /*<< The parameters are based on `boost::crc_16_type`. >>*/
  40. boost::crc_basic<16> crc1( 0x8005u, 0u, 0u, true, true );
  41. va_list ap;
  42. va_start( ap, block_count );
  43. while ( block_count-- )
  44. {
  45. void const * const bs = va_arg( ap, void const * );
  46. size_t const bl = va_arg( ap, size_t );
  47. /*<< The `va_arg` calls were within the `process_bytes` call, but I
  48. remembered that calling order is not guaranteed among function
  49. arguments, so I need explicit object declarations to enforce the
  50. extraction order. >>*/
  51. crc1.process_bytes( bs, bl );
  52. }
  53. va_end( ap );
  54. return crc1.checksum();
  55. }
  56. /*` No CRC operation throws, so there is no need for extra protection between
  57. the varargs macro calls.
  58. */
  59. //]
  60. //[ acrc_piecemeal_run
  61. //` The `augmented_crc` function can compute CRCs from distributed data, too:
  62. unsigned combined_acrc_16( int block_count, ... )
  63. {
  64. /*<< C-style variable-argument routines are or may be macros. >>*/
  65. using namespace std;
  66. va_list ap;
  67. unsigned result = 0u;
  68. va_start( ap, block_count );
  69. if ( block_count <= 0 )
  70. goto finish;
  71. void const * bs = va_arg( ap, void const * );
  72. size_t bl = va_arg( ap, size_t );
  73. /*<< The parameters are based on `boost::crc_xmodem_t`. >>*/
  74. result = boost::augmented_crc<16, 0x1021u>( bs, bl );
  75. while ( --block_count )
  76. {
  77. bs = va_arg( ap, void const * );
  78. bl = va_arg( ap, size_t );
  79. result = boost::augmented_crc<16, 0x1021u>( bs, bl, result );
  80. }
  81. finish:
  82. va_end( ap );
  83. return result;
  84. }
  85. /*` No CRC operation throws, so there is no need for extra protection between
  86. the varargs macro calls. Feeding the result from the previous run as the
  87. initial remainder for the next run works easily because there's no output
  88. reflection or XOR mask.
  89. */
  90. //]
  91. /*<-*/ int main() {} /*->*/