4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
32 #if defined(_LITTLE_ENDIAN)
34 /* Little-endian architectures need byte-swapping. */
36 #define sws(x) (((x >> 8) & 0x00ff) | ((x << 8) & 0xff00))
37 #define swl(x) (sws(x >> 16) | (sws(x) << 16))
39 #define swap_short(x) (x = sws(x))
40 #define swap_long(x) (x = swl(x))
42 #else /* if !_LITTLE_ENDIAN */
44 /* Big-endian anchictectures don't need byte-swapping. */
49 #define swap_short(x) (x = sws(x))
50 #define swap_long(x) (x = swl(x))
52 #endif /* _LITTLE_ENDIAN */
55 compute_crc8(unsigned char *bytes
, int length
)
65 p_crc
->cm_poly
= 0x107; /* = X^8 + x^2 + x + 1 */
67 p_crc
->cm_refin
= TRUE
;
68 p_crc
->cm_refot
= TRUE
;
73 for (i
= 0; i
< length
; i
++) {
74 cm_nxt(p_crc
, bytes
[i
]);
77 aCRC
= (unsigned char)cm_crc(p_crc
);
83 compute_crc32(unsigned char *bytes
, int length
)
93 p_crc
->cm_poly
= 0x04c11db7;
94 p_crc
->cm_init
= 0xffffffff;
95 p_crc
->cm_refin
= TRUE
;
96 p_crc
->cm_refot
= TRUE
;
97 p_crc
->cm_xorot
= 0xffffffff;
101 for (i
= 0; i
< length
; i
++) {
102 cm_nxt(p_crc
, bytes
[i
]);
105 aCRC
= (uint32_t)cm_crc(p_crc
);
111 * This is the max value an uint32_t value can hold...
112 * Define this for Windows compilers which don't have "limits.h" or equivalant
114 #define UINT32_T_MAX 0xFFFFFFFF
117 compute_checksum32(unsigned char *bytes
, int length
)
122 unsigned char tailbytes
[4] = { 0x00, 0x00, 0x00, 0x00 };
124 /* Grab bytes in 4-byte chunks */
125 for (i
= 0; i
< length
-4; i
+= 4) {
126 /* Grab chunk as an int */
127 (void) memcpy(&next4bytes
, &(bytes
[i
]), 4);
128 swap_long(next4bytes
);
130 if (next4bytes
> UINT32_T_MAX
- regval
) {
131 next4bytes
-= UINT32_T_MAX
- regval
;
135 /* Add intval to regval */
136 regval
+= next4bytes
;
139 /* Grab any remaining bytes at the end */
140 for (j
= length
-1, k
= 3; j
>= i
; j
--, k
--) {
141 tailbytes
[k
] = bytes
[j
];
145 * Treat any remaining bytes put into tailbytes as if they were
146 * a left-zero-padded unsigned int (uint32_t == 4 bytes!)
148 (void) memcpy(&next4bytes
, tailbytes
, 4);
149 swap_long(next4bytes
);
150 if (next4bytes
> UINT32_T_MAX
- regval
) {
151 next4bytes
-= UINT32_T_MAX
- regval
;
154 regval
+= next4bytes
;
156 return ((uint32_t)regval
);