crc.c revision 178826
1129202Scognet/*
2129202Scognet * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3129202Scognet * (Royal Institute of Technology, Stockholm, Sweden).
4129202Scognet * All rights reserved.
5129202Scognet *
6129202Scognet * Redistribution and use in source and binary forms, with or without
7129202Scognet * modification, are permitted provided that the following conditions
8129202Scognet * are met:
9129202Scognet *
10129202Scognet * 1. Redistributions of source code must retain the above copyright
11129202Scognet *    notice, this list of conditions and the following disclaimer.
12129202Scognet *
13129202Scognet * 2. Redistributions in binary form must reproduce the above copyright
14129202Scognet *    notice, this list of conditions and the following disclaimer in the
15129202Scognet *    documentation and/or other materials provided with the distribution.
16129202Scognet *
17129202Scognet * 3. Neither the name of the Institute nor the names of its contributors
18129202Scognet *    may be used to endorse or promote products derived from this software
19129202Scognet *    without specific prior written permission.
20129202Scognet *
21129202Scognet * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22129202Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23129202Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24129202Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25129202Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26129202Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27129202Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28129202Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29129202Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30129202Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31129202Scognet * SUCH DAMAGE.
32129202Scognet */
33129202Scognet
34129202Scognet#include "krb5_locl.h"
35129202Scognet
36129202ScognetRCSID("$Id: crc.c 17442 2006-05-05 09:31:15Z lha $");
37129202Scognet
38129202Scognetstatic u_long table[256];
39129202Scognet
40129202Scognet#define CRC_GEN 0xEDB88320L
41129202Scognet
42245650Sandrewvoid
43245650Sandrew_krb5_crc_init_table(void)
44245650Sandrew{
45245650Sandrew    static int flag = 0;
46245650Sandrew    unsigned long crc, poly;
47245650Sandrew    int     i, j;
48245650Sandrew
49129202Scognet    if(flag) return;
50245650Sandrew    poly = CRC_GEN;
51129202Scognet    for (i = 0; i < 256; i++) {
52129202Scognet	crc = i;
53129202Scognet	for (j = 8; j > 0; j--) {
54129202Scognet	    if (crc & 1) {
55129202Scognet		crc = (crc >> 1) ^ poly;
56129202Scognet	    } else {
57129202Scognet		crc >>= 1;
58129202Scognet	    }
59129202Scognet	}
60129202Scognet	table[i] = crc;
61129202Scognet    }
62129202Scognet    flag = 1;
63129202Scognet}
64129202Scognet
65129202Scognetuint32_t
66129202Scognet_krb5_crc_update (const char *p, size_t len, uint32_t res)
67129202Scognet{
68129202Scognet    while (len--)
69129202Scognet	res = table[(res ^ *p++) & 0xFF] ^ (res >> 8);
70171217Speter    return res & 0xFFFFFFFF;
71171217Speter}
72171217Speter