calculate checksum for spilted file by boost crc

1.1k Views Asked by At

I wonder that if I can calculate 2 checksums by read first half of the file to get one checksum A and then read the rest of the file to get another checksum B, and these two checksums A,B will combined to a uniq check sum (with longer length)

I use the boost::CRC library try to implement it, but I don't know if I use it right? (1)The second parameter of process_bytes, is that means the total buffer length? (2) Does the result will calculated by the function recursively that I don't have to worry about the array? Or when I call the process_byte, it just calculate the new checksum of the new single byte of the buffer array?

Frankly

    std::ifstream  ifs( argv[i], std::ios_base::binary );

    if ( ifs )
    {
        do
        {
            char  buffer[BUFFER_SIZE];

            ifs.read( buffer, BUFFER_SIZE);
            result.process_bytes( buffer, HALF_FILE_SIZE );
        } while (HALF or END of FILE );
     }
    std::cout << result.checksum() << std::endl;

plz refer to this page to see the boost::CRC example code: http://www.boost.org/doc/libs/1_37_0/libs/crc/crc_example.cpp

1

There are 1 best solutions below

0
On

I can't figure out what you're asking.

First off, a CRC is not a checksum. The "sum" in checksum means that the data is added. A CRC computes a polynomial remainder over a finite field, which is not a sum. This is important, since you seem to be asking about combining CRCs. The CRCs of two pieces cannot be added to get the CRC of the whole thing.

Second, the way to get the CRC of multiple pieces is to compute a single CRC over those pieces. That is what the example code does. result contains a single CRC that is updated with each piece that is run through it with process_bytes.

Third, it is possible to combine two CRCs, given the two CRCs and the length of the first piece, to get what would have been a single CRC of the two pieces concatenated. The operation is not trivial, but you can find it in zlib's crc32_combine() routine.