Ok, rolling crc. I should really start reading the full questions...
Main Topics
Browse All TopicsHi Folks:
I am trying to find sample code (C++ is OK , VB might be better) for an example of calculating a rolling checksum crc along the lines of rsync. My understanding is that you can take a weak crc calculation of a block of data and by somehow "dropping" the first byte and "adding" the next byte after the block quickly generate a new "weak" check sum of the next block of data. e.g.: a) calculate a weak crc on a 512 byte block of data; b) "drop" the first byte of the block and "add" byte 513 (next byte) using AND / XOR whatever? - and obtain the weak CRC for the block running from byte 2 (1 based) to byte 513. What I am trying to locate is i) code for the weak crc check sum and ii) what are the instructions to create the "rolling checksum?
Any assitance would be greatly appreciated... I have found many items regarding "rsync" but the low level math eludes me ... a more specific example would be extremely helpful for my project.
Best regards to all ... thanks in advance for any help / suggestions etc. ... Dave Melnyk
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Couple of things about CRC.
Treating 'file' as a bigint, "CalculateFileCRC" calculates CRC as CRCmod(0xFFFFFFFF * (256 ** filesize) ^ file) ^ 0xFFFFFFFF. This is good for pkzip and other archivers because all-zero files with different lengths will return different checksums.
However, for a rolling CRC, we don't care about the absolute length of the file. We just want to know the CRC of this particular buffer. In other words, we want CRC(file) = CRC(file with arbitrary number of zeros prepended). The easy way to do that is to get rid of the 0xffffffff terms in the above CRC method. So we'll need to use 0 as the initial CRC and refrain from doing the ^0xffffffff at the end of the CRC calculation.
We already know how to calculate CRC(file[0:512]), just extending CRC(file[0:511]) in the normal way. But we actually want to know CRC(file[1:512]).
file[0:512] = (file[0] ++ (512 zero bytes)) ^ ((zero byte) ++ file[1:512]), so
CRC(file[0:512]) = CRC(file[0] ++ (512 zero bytes)) ^ CRC((zero byte) ++ file[1:512])
Since prepended zero bytes have no effect in our CRC calculation,
CRC(file[0:512]) = CRC(file[0] ++ (512 zero bytes)) ^ CRC(file[1:512])
shuffling the terms around
CRC(file[1:512]) = CRC(file[0] ++ (512 zero bytes)) ^ CRC(file[0:512])
So create a rolloff table and initialize it like this:
for i = 0 to 255
rolloff[i] = CRC(i ++ (512 zero bytes))
Then use the rolloff table like this:
first calculate CRC(file[0:511]) the normal way
calculate CRC(file[0:512]) based on CRC(file[0:511])
CRC(file[1:512]) = rolloff(file[0]) ^ CRC(file[0:512])
CRC(file[2:513]) = rolloff(file[1]) ^ CRC(file[1:513])
and so on
NovaDenizen:
Thanks for the reply ... sorry I took a while to respond. I am having trouble understanding your answer (I am really mostly a VB programmer, minimal C) I showed your answer to my partner who knows C++ very well and he wasn't sure either! He said he was having trouble following your notation. Don't know if it is possible to break it down to more of a "rolling CRC for Dummies" level?!
Thanks again for the assistance ...
Dave Melnyk
(feeling completely inept!)
#include <stdio.h>
#include <stdlib.h>
unsigned long CRCTable[256];
#define CRC32_POLYNOMIAL 0xEDB88320L
void BuildCRCTable()
{
// same as Dr Dobbs article
}
unsigned long CalculateBufferCRC( count, crc, buffer )
unsigned int count;
unsigned long crc;
void *buffer;
{
// same as Dr Dobbs article
}
// This is my new code
unsigned long RolloffTable[256];
void SetupRolloffTable(int windowsize) {
int i;
char *buf = malloc(windowsize+1);
bzero(buf, windowsize+1);
for(i =0; i < 256; i++) {
buf[0] = i;
RolloffTable[i]=CalculateB
}
free(buf);
}
unsigned long RolloffCRC(unsigned long oldcrc, unsigned char oldchar,
unsigned char newchar)
{
unsigned long temp1, temp2, tempcrc;
temp1 = ( oldcrc >> 8 ) & 0x00FFFFFFL;
temp2 = CRCTable[ ( (int) oldcrc ^ newchar) & 0xff ];
tempcrc = temp1 ^ temp2;
unsigned long newcrc = tempcrc ^ RolloffTable[oldchar];
return newcrc;
}
int main(int argc, char *argv[]) {
BuildCRCTable();
FILE *f = fopen("testdata", "r");
if (f == NULL) { printf("f is NULL\n"); exit(1); }
unsigned char buf[8193];
int bufsize=530;
int blocksize=512;
fread(buf, 1, 8192, f);
fclose(f);
int i;
printf("First, doing it the hard way, CRC over all 512 bytes in window:\n");
for(i = 0; i < bufsize-blocksize; i++) {
unsigned long crc=CalculateBufferCRC(blo
printf("%3d-%3d: %08x\n", i, i+blocksize-1, crc);
}
printf("Now, doing it the easy way, 1 512 byte crc, then incremental changes:\n");
SetupRolloffTable(blocksiz
unsigned long crc = CalculateBufferCRC(blocksi
for(i = 512; i < bufsize; i++) {
printf("%3d-%3d: %08x\n", i-blocksize, i, crc);
crc = RolloffCRC(crc, buf[i-blocksize], buf[i]);
}
}
You can find the rsync implementation here:
http://librsync.cvs.source
http://librsync.cvs.source
(NOTE that this is LGPL code. Make sure the license is compatible with your project.)
Here is some documentation that you might also find useful:
http://en.wikipedia.org/wi
http://samba.anu.edu.au/rs
Business Accounts
Answer for Membership
by: NovaDenizenPosted on 2007-05-10 at 07:14:50ID: 19065376
Here's a good article on CRC: http://www.dogma.net/markn /articles/ crcman/crc man.htm
nt, crc, buffer);
The important functions are "BuildCRCTable" which initializes the CRC table, and "CalculateBufferCRC" which extends a CRC calculation. "CalculateFileCRC" shows how you would use multiple calls to CalculateBufferCRC to compute a full file's CRC.
The way CRC works, you start out with a 0xFFFFFFFF CRC. Then you read in a buffer and extend your crc calculation. Like so:
crc = 0xffffffff;
while (more bytes available) {
read 'bytecount' bytes into buffer;
crc = calculateBufferCRC(bytecou
}
Hope this helps.