Statistics
| Branch: | Revision:

ffmpeg / libavutil / adler32.c @ 38603ff6

History | View | Annotate | Download (1.07 KB)

1
/* adler32.c -- compute the Adler-32 checksum of a data stream
2
 * Copyright (C) 1995 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5

    
6
#include "common.h"
7
#include "adler32.h"
8

    
9
#define BASE 65521L /* largest prime smaller than 65536 */
10
#define NMAX 5552
11
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
12

    
13
#define DO1(buf)  {s1 += *buf++; s2 += s1;}
14
#define DO2(buf)  DO1(buf); DO1(buf);
15
#define DO4(buf)  DO2(buf); DO2(buf);
16
#define DO8(buf)  DO4(buf); DO4(buf);
17
#define DO16(buf) DO8(buf); DO8(buf);
18

    
19
unsigned long av_adler32_update(unsigned long adler, const uint8_t *buf, unsigned int len)
20
{
21
    unsigned long s1 = adler & 0xffff;
22
    unsigned long s2 = (adler >> 16) & 0xffff;
23
    int k;
24

    
25
    if (buf == NULL) return 1L;
26

    
27
    while (len > 0) {
28
        k = FFMIN(len, NMAX);
29
        len -= k;
30
#ifndef CONFIG_SMALL
31
        while (k >= 16) {
32
            DO16(buf);
33
            k -= 16;
34
        }
35
#endif
36
        while(k--) {
37
            DO1(buf);
38
        }
39
        s1 %= BASE;
40
        s2 %= BASE;
41
    }
42
    return (s2 << 16) | s1;
43
}