adler32_combine()函数是将两个adler32值组合的函数。
:::数学原理:::
A1 = P[1] + P[2] + P[3] + ... + P[m]
B1 = mP[1] + (m-1)P[2] + (m-2)P[3] + ... + P[m]
A2 = Q[1] + Q[2] + Q[3] + ... + Q[n]
B2 = nQ[1] + (n-1)Q[2] + (n-2)Q[3] + ... + Q[n]
A = P[1] + P[2] + ... + P[m] + Q[1] + Q[2] + Q[3] + ... + Q[n] = A1 + A2
B = (m+n)P[1] + (m-1+n)P[2] + (m-2+n)P[3] + ... + (1+n)P[m] + nQ[1] + (n-1)Q[2] + (n-2)Q[3] + ... + Q[n]
= B1 + B2 + nA1
Adler32
A = (A1+A2) mod Modulo
B = (B1+B2+nA1) mod Modulo
但Adler32的发明者Mark Adler为什么在实现ader32_combine有如此古怪的代码
---------------------------------------------
rem = (unsigned)(len2 % BASE);
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
---------------------------------------------
???
I.
rem
II.
rem*sum1
III.
BASE-1
IV.
BASE-rem
希望各位大牛指点一二!!!
在此谢过!!!
我不是指语法或语义层面,指算法的实现。
uLong ZEXPORT adler32_combine(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off_t len2;
{
unsigned long sum1;
unsigned long sum2;
unsigned rem;
/* the derivation of this formula is left as an exercise for the reader */
rem = (unsigned)(len2 % BASE);
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 > BASE) sum1 -= BASE;
if (sum1 > BASE) sum1 -= BASE;
if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 > BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!