root/lang/c/swifty/adler32.c @ 32222

Revision 32222, 1.6 kB (checked in by kazuho, 5 years ago)

moving to coderepos

Line 
1/*
2 * This is a modified version based on adler32.c from gst-ffmpeg based on
3 * adler32.c from the zlib library.
4 *
5 * Copyright (C) 1995 Mark Adler
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty.  In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 *    claim that you wrote the original software. If you use this software
17 *    in a product, an acknowledgment in the product documentation would be
18 *    appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 *    misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 *
23 */
24
25#include <stdint.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include "swifty.h"
29
30#define BASE 65521L /* largest prime smaller than 65536 */
31
32#define DO1(buf)  {s1 += *buf++; s2 += s1;}
33#define DO4(buf)  DO1(buf); DO1(buf); DO1(buf); DO1(buf);
34#define DO16(buf) DO4(buf); DO4(buf); DO4(buf); DO4(buf);
35
36uint32_t swifty_adler32(const void* ptr, uint32_t len)
37{
38  const uint8_t* buf = ptr;
39  uint32_t s1 = 1;
40  uint32_t s2 = 0;
41
42  while (len > 0) {
43    while(len > 16 && s2 < (1U<<31)) {
44      DO16(buf); len-=16;
45    }
46    DO1(buf); len--;
47    s1 %= BASE;
48    s2 %= BASE;
49  }
50  return (s2 << 16) | s1;
51}
Note: See TracBrowser for help on using the browser.