I am writing a adler32 code to compute the adler32 checksum of a string.
On the net I came across various sample codes:
I did not understand some code entirely as <b>it has additional code</b> marked in bold below apart from the basic implementation of the adler32 logic.
I did not understand the addional part as to what is is doing.
Can anybody experienced with adler32 give some hint on it?

The various sample codes are:

//---------------------------------------------------------------------
// WIKI
I understood this code as its the basic implementation of the adler32 logic.
So no doubts in this.

/* Loop over each byte of data, in order */
for (size_t index = 0; index < blen; ++index)
{
a = (a + buff[index]) % BASE;
b = (b + a) % BASE;
}

wweak_cs = (b << 16) | a;
//---------------------------------------------------------------------
// RSYNC
I did not understand some code marked in bold below.
{
#define CHAR_OFFSET 0
char *buf = buff;
int len = blen;
int index = 0;
UINT32 s1, s2;

s1 = s2 = 0;
[B]for (index = 0; index < (len-4); index+=4)
{
s2 += 4*(s1 + buf[index]) + 3*buf[index+1] + 2*buf[index+2] + buf[index+3] + 10*CHAR_OFFSET;
s1 += (buf[index+0] + buf[index+1] + buf[index+2] + buf[index+3] + 4*CHAR_OFFSET);
}[/B]
for (; index < len; index++)
{
s1 += (buf[index]+CHAR_OFFSET); s2 += s1;
}
rweak_cs = (s1 & 0xffff) + (s2 << 16);
weak_cs = rweak_cs;
}
//---------------------------------------------------------------------
// sharpdevelop
I did not understand some code marked in bold below.

{
char *buf = buff;
int len = blen;
sweak_cs = 1;
[B]int s1 = sweak_cs & 0xFFFF; 
int s2 = sweak_cs >> 16 & 0xFFFF; [/B]
int index =0;

[B]while (len > 0)
{ 
int n = 3800; 
if (n > len)
{ 
n = len; 
} 
len -= n; [/B]
while (--n >= 0)
{ 
s1 = s1 + (int)(buf[index++] & 0xFF); 
s2 = s2 + s1; 
} 
s1 %= BASE; 
s2 %= BASE; 
} 

sweak_cs = (s2 << 16) | s1;
weak_cs = sweak_cs;
}
//---------------------------------------------------------------------
//altivec
I did not understand some code marked in bold below.

{
[B]#define NMAX 5552 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
#define DO16(buf) DO8(buf,0); DO8(buf,8); [/B]
# define MOD(a) a %= BASE

char *buf = buff;
int len = blen;
aweak_cs = 1;
unsigned long s1 = aweak_cs & 0xffff;
unsigned long s2 = (aweak_cs >> 16) & 0xffff;
int k;
while (len > 0) 
{
[B]k = len < NMAX ? (int)len : NMAX;
len -= k;
while (k >= 16)
{
DO16(buf);
buf += 16;
k -= 16;
} [/B]
if (k != 0)
{
do
{
s1 += *buf++;
s2 += s1;
} while (--k);
}
MOD(s1);
MOD(s2);
}

aweak_cs = (s2 << 16) | s1; 
weak_cs = aweak_cs;
}

Can anybody clear the doubts?

Edited 6 Years Ago by Nick Evan: Added code-tags

This article has been dead for over six months. Start a new discussion instead.