Fix memory leak.
[gem5.git] / src / base / crc.cc
1 /*
2 * Copyright (c) 1988, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <string>
35
36 #include "sim/host.hh"
37 #include "base/crc.hh"
38
39 #define ETHER_CRC_POLY_LE 0xedb88320
40 #define ETHER_CRC_POLY_BE 0x04c11db6
41
42 #if 0
43 /*
44 * This is for reference. We have a table-driven version
45 * of the little-endian crc32 generator, which is faster
46 * than the double-loop.
47 */
48 uint32_t
49 crc32le(const uint8_t *buf, size_t len)
50 {
51 uint32_t c, crc, carry;
52 size_t i, j;
53
54 crc = 0xffffffffU; /* initial value */
55
56 for (i = 0; i < len; i++) {
57 c = buf[i];
58 for (j = 0; j < 8; j++) {
59 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
60 crc >>= 1;
61 c >>= 1;
62 if (carry)
63 crc = (crc ^ ETHER_CRC_POLY_LE);
64 }
65 }
66
67 return (crc);
68 }
69 #else
70 uint32_t
71 crc32le(const uint8_t *buf, size_t len)
72 {
73 static const uint32_t crctab[] = {
74 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
75 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
76 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
77 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
78 };
79 uint32_t crc;
80 int i;
81
82 crc = 0xffffffffU; /* initial value */
83
84 for (i = 0; i < len; i++) {
85 crc ^= buf[i];
86 crc = (crc >> 4) ^ crctab[crc & 0xf];
87 crc = (crc >> 4) ^ crctab[crc & 0xf];
88 }
89
90 return (crc);
91 }
92 #endif
93
94 uint32_t
95 crc32be(const uint8_t *buf, size_t len)
96 {
97 uint32_t c, crc, carry;
98 size_t i, j;
99
100 crc = 0xffffffffU; /* initial value */
101
102 for (i = 0; i < len; i++) {
103 c = buf[i];
104 for (j = 0; j < 8; j++) {
105 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
106 crc <<= 1;
107 c >>= 1;
108 if (carry)
109 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
110 }
111 }
112
113 return (crc);
114 }