Debian Codegraph @

ptl_ips/ips_crc32.c

raw
1 /* The code in this file was derived from crc32.c in zlib 1.2.3, and
2 modified from its original form to suit our requirements. The zlib
3 license and crc32.c copyright and credits are preserved below. */
4
5 /* zlib.h -- interface of the 'zlib' general purpose compression library
6 version 1.2.3, July 18th, 2005
7
8 Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
9
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any damages
12 arising from the use of this software.
13
14 Permission is granted to anyone to use this software for any purpose,
15 including commercial applications, and to alter it and redistribute it
16 freely, subject to the following restrictions:
17
18 1. The origin of this software must not be misrepresented; you must not
19 claim that you wrote the original software. If you use this software
20 in a product, an acknowledgment in the product documentation would be
21 appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25
26 Jean-loup Gailly Mark Adler
27 [email protected] [email protected]
28
29 The data format used by the zlib library is described by RFCs (Request for
30 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
31 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
32 */
33
34 /* crc32.c -- compute the CRC-32 of a data stream
35 * Copyright (C) 1995-2005 Mark Adler
36 * For conditions of distribution and use, see copyright notice in zlib.h
37 *
38 * Thanks to Rodney Brown <[email protected]> for his contribution of faster
39 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
40 * tables for updating the shift register in one step with three exclusive-ors
41 * instead of four steps with four exclusive-ors. This results in about a
42 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
43 */
44
45 #include "ips_proto.h"
46 #include "ips_proto_internal.h"
47
48 /* Table of CRCs of all 8-bit messages. */
49 static uint32_t crc_table[256];
50
51 /* Flag: has the table been computed? Initially false. */
52 static int crc_table_computed = 0;
53
54 /* Make the table for a fast CRC. */
55 static void make_crc_table(void)
56 {
57 uint32_t c;
58 int n, k;
59
60 for (n = 0; n < 256; n++) {
61 c = (uint32_t) n;
62 for (k = 0; k < 8; k++) {
63 if (c & 1)
64 c = 0xedb88320 ^ (c >> 1);
65 else
66 c = c >> 1;
67 }
68 crc_table[n] = c;
69 }
70 crc_table_computed = 1;
71 }
72
73 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
74 * should be initialized to all 1's, and the transmitted value
75 * is the 1's complement of the final running CRC (see the
76 * crc() routine below)).
77 */
78
79 uint32_t ips_crc_calculate(uint32_t len, uint8_t *data, uint32_t crc)
80 {
81 uint32_t c = crc;
82 uint32_t n;
83
84 if (!crc_table_computed) {
85 make_crc_table();
86 }
87 for (n = 0; n < len; n++) {
88 c = crc_table[(c ^ data[n]) & 0xff] ^ (c >> 8);
89 }
90 return c;
91 }