flate

deflate implementation
git clone git://git.suckless.org/flate
Log | Files | Refs | README

adler.c (1014B)


      1 #include "flate.h"
      2 
      3 enum {
      4 	AdlerBase = 65521, /* largest 16bit prime */
      5 	AdlerN    = 5552   /* max iters before 32bit overflow */
      6 };
      7 
      8 uint adler32(uchar *p, int n, uint adler) {
      9 	uint s1 = adler & 0xffff;
     10 	uint s2 = (adler >> 16) & 0xffff;
     11 	uchar *ep;
     12 	int k;
     13 
     14 	for (; n >= 16; n -= k) {
     15 		k = n < AdlerN ? n : AdlerN;
     16 		k &= ~0xf;
     17 		for (ep = p + k; p < ep; p += 16) {
     18 			s1 += p[0];
     19 			s2 += s1;
     20 			s1 += p[1];
     21 			s2 += s1;
     22 			s1 += p[2];
     23 			s2 += s1;
     24 			s1 += p[3];
     25 			s2 += s1;
     26 			s1 += p[4];
     27 			s2 += s1;
     28 			s1 += p[5];
     29 			s2 += s1;
     30 			s1 += p[6];
     31 			s2 += s1;
     32 			s1 += p[7];
     33 			s2 += s1;
     34 			s1 += p[8];
     35 			s2 += s1;
     36 			s1 += p[9];
     37 			s2 += s1;
     38 			s1 += p[10];
     39 			s2 += s1;
     40 			s1 += p[11];
     41 			s2 += s1;
     42 			s1 += p[12];
     43 			s2 += s1;
     44 			s1 += p[13];
     45 			s2 += s1;
     46 			s1 += p[14];
     47 			s2 += s1;
     48 			s1 += p[15];
     49 			s2 += s1;
     50 		}
     51 		s1 %= AdlerBase;
     52 		s2 %= AdlerBase;
     53 	}
     54 	if (n) {
     55 		for (ep = p + n; p < ep; p++) {
     56 			s1 += p[0];
     57 			s2 += s1;
     58 		}
     59 		s1 %= AdlerBase;
     60 		s2 %= AdlerBase;
     61 	}
     62 	return (s2 << 16) + s1;
     63 }