1/*
2 * Copyright (c) 2012 Apple, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include "crc.h"
25
26static const int adler_mod_value = 65521;
27
28static uint64_t
29adler32_setup() { return 0; }
30
31static uint64_t
32adler32_implementation(size_t len, const void *in, uint64_t crc)
33{
34    uint32_t a = 1, b = 0;
35    uint8_t *bytes = (uint8_t *) in;
36
37    for (size_t i = 0; i < len; i++) {
38        a = (a + bytes[i]) % adler_mod_value;
39        b = (b + a) % adler_mod_value;
40    }
41    return (b << 16) | a;
42}
43
44static uint64_t
45adler32_final(size_t length, uint64_t crc) { return crc; }
46
47
48static uint64_t
49adler32_oneshot(size_t len, const void *in)
50{
51    return adler32_implementation(len, in, 0);
52}
53
54
55
56const crcDescriptor adler32 = {
57    .name = "adler-32",
58    .defType = functions,
59    .def.funcs.setup = adler32_setup,
60    .def.funcs.update = adler32_implementation,
61    .def.funcs.final = adler32_final,
62    .def.funcs.oneshot = adler32_oneshot
63};
64