Boost logo

Boost Users :

Subject: [Boost-users] [crc] bug in boost::crc
From: U.Mutlu (for-gmane_at_[hidden])
Date: 2015-07-09 13:27:02


/*
   boost-crc-bug-demo.cpp

   Written by U.Mutlu on 2015-07-09-Th

   Compile:
     g++ -O2 -Wall -std=gnu++11 boost-crc-bug-demo.cpp

   Run:
     ./a.out

   Output:
     crc("The quick brown fox jumps over the lazy dog")=414fa339
     crc("The quick brown fox ")=88b075e2
     crc("The quick brown fox jumps over the lazy dog")=4ac56d0f
     ERROR

   See also:
     http://www.boost.org/doc/libs/1_58_0/libs/crc/crc.html#usage

   Conclusion:
     It doesn't do what it should and also advertises; ie. there's a bug

*/

#include <boost/crc.hpp>
#include <boost/cstdint.hpp>
#include <iostream>

int main()
   {
     uint32_t expected;

     {
     const std::string sTxt = "The quick brown fox jumps over the lazy dog";
     boost::crc_optimal<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
     crc.process_bytes(sTxt.data(), sTxt.size());
     std::cout << "crc(\"" << sTxt << "\")=" << std::hex << crc.checksum() <<
std::endl;

     // save the result for the following test
     expected = crc.checksum();
     }

     // test: doing the same as above, but now in 2 steps, ie. chunk- or
blockwise:
     std::string sTxt;
     uint32_t result = 0xFFFFFFFF;
     for (size_t i = 0; i <= 1; ++i)
     {
     sTxt += !i ? "The quick brown fox "
                : "jumps over the lazy dog";

     // using the special ctor for initial remainder stored in "result"
     boost::crc_optimal<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true>
crc(result);
     crc.process_bytes(sTxt.data(), sTxt.size());
     std::cout << "crc(\"" << sTxt << "\")=" << std::hex << crc.checksum() <<
std::endl;

     result = crc.checksum();
     }

     // eval:
     const std::string sRes = result == expected ? "SUCCESS" : "ERROR";
     std::cout << sRes << std::endl;

     return 0;
   }


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net