4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
33 * Author : Ross Williams (ross@guest.adelaide.edu.au.).
35 * Status : Public domain.
37 * Description : This is the implementation (.c) file for the reference
38 * implementation of the Rocksoft^tm Model CRC Algorithm. For more
39 * information on the Rocksoft^tm Model CRC Algorithm, see the document
40 * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
41 * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
42 * "ftp.adelaide.edu.au/pub/rocksoft".
44 * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
48 * Implementation Notes
49 * --------------------
50 * To avoid inconsistencies, the specification of each function is not echoed
51 * here. See the header file for a description of these functions.
52 * This package is light on checking because I want to keep it short and
53 * simple and portable (i.e. it would be too messy to distribute my entire
54 * C culture (e.g. assertions package) with this package.
61 /* The following definitions make the code more readable. */
63 #define BITMASK(X) (1L << (X))
64 #define MASK32 0xFFFFFFFFL
67 LOCAL
uint32_t reflect
P_((uint32_t v
, int b
));
70 /* Returns the value v with the bottom b [0,32] bits reflected. */
71 /* Example: reflect(0x3e23L,3) == 0x3e26 */
77 for (i
= 0; i
< b
; i
++) {
79 v
|= BITMASK((b
-1)-i
);
81 v
&= ~BITMASK((b
-1)-i
);
87 LOCAL
uint32_t widmask
P_((p_cm_t
));
90 /* Returns a longword whose value is (2^p_cm->cm_width)-1. */
91 /* The trick is to do this portably (e.g. without doing <<32). */
94 return ((((1L<<(p_cm
->cm_width
-1))-1L)<<1)|1L);
101 p_cm
->cm_reg
= p_cm
->cm_init
;
110 uint32_t uch
= (uint32_t)ch
;
111 uint32_t topbit
= BITMASK(p_cm
->cm_width
-1);
114 uch
= reflect(uch
, 8);
116 p_cm
->cm_reg
^= (uch
<< (p_cm
->cm_width
-8));
117 for (i
= 0; i
< 8; i
++) {
118 if (p_cm
->cm_reg
& topbit
)
119 p_cm
->cm_reg
= (p_cm
->cm_reg
<< 1) ^ p_cm
->cm_poly
;
123 p_cm
->cm_reg
&= widmask(p_cm
);
128 cm_blk(p_cm
, blk_adr
, blk_len
)
134 cm_nxt(p_cm
, *blk_adr
++);
142 return (p_cm
->cm_xorot
^ reflect(p_cm
->cm_reg
, p_cm
->cm_width
));
144 return (p_cm
->cm_xorot
^ p_cm
->cm_reg
);
154 uint32_t topbit
= BITMASK(p_cm
->cm_width
-1);
155 uint32_t inbyte
= (uint32_t)index
;
158 inbyte
= reflect(inbyte
, 8);
160 r
= inbyte
<< (p_cm
->cm_width
-8);
161 for (i
= 0; i
< 8; i
++)
163 r
= (r
<< 1) ^ p_cm
->cm_poly
;
168 r
= reflect(r
, p_cm
->cm_width
);
170 return (r
& widmask(p_cm
));