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.
31 #include <sys/types.h>
33 typedef long uint32_t;
45 * Author : Ross Williams (ross@guest.adelaide.edu.au.).
47 * Status : Public domain.
49 * Description : This is the header (.h) file for the reference
50 * implementation of the Rocksoft^tm Model CRC Algorithm. For more
51 * information on the Rocksoft^tm Model CRC Algorithm, see the document
52 * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
53 * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
54 * "ftp.adelaide.edu.au/pub/rocksoft".
56 * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
60 * How to Use This Package
61 * -----------------------
62 * Step 1: Declare a variable of type cm_t. Declare another variable
63 * (p_cm say) of type p_cm_t and initialize it to point to the first
64 * variable (e.g. p_cm_t p_cm = &cm_t).
66 * Step 2: Assign values to the parameter fields of the structure.
67 * If you don't know what to assign, see the document cited earlier.
69 * p_cm->cm_width = 16;
70 * p_cm->cm_poly = 0x8005L;
72 * p_cm->cm_refin = TRUE;
73 * p_cm->cm_refot = TRUE;
74 * p_cm->cm_xorot = 0L;
75 * Note: Poly is specified without its top bit (18005 becomes 8005).
76 * Note: Width is one bit less than the raw poly width.
78 * Step 3: Initialize the instance with a call cm_ini(p_cm);
80 * Step 4: Process zero or more message bytes by placing zero or more
81 * successive calls to cm_nxt. Example: cm_nxt(p_cm,ch);
83 * Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm);
84 * If the CRC is a 16-bit value, it will be in the bottom 16 bits.
90 * PORTABILITY: This package has been coded very conservatively so that
91 * it will run on as many machines as possible. For example, all external
92 * identifiers have been restricted to 6 characters and all internal ones to
93 * 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid
94 * namespace collisions. This package is endian independent.
96 * EFFICIENCY: This package (and its interface) is not designed for
97 * speed. The purpose of this package is to act as a well-defined reference
98 * model for the specification of CRC algorithms. If you want speed, cook up
99 * a specific table-driven implementation as described in the document cited
100 * above. This package is designed for validation only; if you have found or
101 * implemented a CRC algorithm and wish to describe it as a set of parameters
102 * to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation
103 * should behave identically to this package under those parameters.
108 /* The following definitions are extracted from my style header file which */
109 /* would be cumbersome to distribute with this package. The DONE_STYLE is the */
110 /* idempotence symbol used in my style header file. */
114 typedef unsigned bool;
115 typedef unsigned char *p_ubyte_
;
122 /* Change to the second definition if you don't have prototypes. */
124 /* #define P_(A) () */
126 /* Uncomment this definition if you don't have void. */
127 /* typedef int void; */
131 /* CRC Model Abstract Type */
132 /* ----------------------- */
133 /* The following type stores the context of an executing instance of the */
134 /* model algorithm. Most of the fields are model parameters which must be */
135 /* set before the first initializing call to cm_ini. */
138 int cm_width
; /* Parameter: Width in bits [8,32]. */
139 uint32_t cm_poly
; /* Parameter: The algorithm's polynomial. */
140 uint32_t cm_init
; /* Parameter: Initial register value. */
141 bool cm_refin
; /* Parameter: Reflect input bytes? */
142 bool cm_refot
; /* Parameter: Reflect output CRC? */
143 uint32_t cm_xorot
; /* Parameter: XOR this to output CRC. */
145 uint32_t cm_reg
; /* Context: Context during execution. */
147 typedef cm_t
*p_cm_t
;
149 /* Functions That Implement The Model */
150 /* ---------------------------------- */
151 /* The following functions animate the cm_t abstraction. */
153 void cm_ini
P_((p_cm_t p_cm
));
154 /* Initializes the argument CRC model instance. */
155 /* All parameter fields must be set before calling this. */
157 void cm_nxt
P_((p_cm_t p_cm
, int ch
));
158 /* Processes a single message byte [0,255]. */
160 void cm_blk
P_((p_cm_t p_cm
, p_ubyte_ blk_adr
, uint32_t blk_len
));
161 /* Processes a block of message bytes. */
163 uint32_t cm_crc
P_((p_cm_t p_cm
));
164 /* Returns the CRC value for the message bytes processed so far. */
166 /* Functions For Table Calculation */
167 /* ------------------------------- */
168 /* The following function can be used to calculate a CRC lookup table. */
169 /* It can also be used at run-time to create or check static tables. */
171 uint32_t cm_tab
P_((p_cm_t p_cm
, int index
));
172 /* Returns the i'th entry for the lookup table for the specified algorithm. */
173 /* The function examines the fields cm_width, cm_poly, cm_refin, and the */
174 /* argument table index in the range [0,255] and returns the table entry in */
175 /* the bottom cm_width bytes of the return value. */
181 #endif /* _CRCMODEL_H */