1*6bbe0590SSundeep Panicker /*
2*6bbe0590SSundeep Panicker  * CDDL HEADER START
3*6bbe0590SSundeep Panicker  *
4*6bbe0590SSundeep Panicker  * The contents of this file are subject to the terms of the
5*6bbe0590SSundeep Panicker  * Common Development and Distribution License (the "License").
6*6bbe0590SSundeep Panicker  * You may not use this file except in compliance with the License.
7*6bbe0590SSundeep Panicker  *
8*6bbe0590SSundeep Panicker  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6bbe0590SSundeep Panicker  * or http://www.opensolaris.org/os/licensing.
10*6bbe0590SSundeep Panicker  * See the License for the specific language governing permissions
11*6bbe0590SSundeep Panicker  * and limitations under the License.
12*6bbe0590SSundeep Panicker  *
13*6bbe0590SSundeep Panicker  * When distributing Covered Code, include this CDDL HEADER in each
14*6bbe0590SSundeep Panicker  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6bbe0590SSundeep Panicker  * If applicable, add the following below this CDDL HEADER, with the
16*6bbe0590SSundeep Panicker  * fields enclosed by brackets "[]" replaced with your own identifying
17*6bbe0590SSundeep Panicker  * information: Portions Copyright [yyyy] [name of copyright owner]
18*6bbe0590SSundeep Panicker  *
19*6bbe0590SSundeep Panicker  * CDDL HEADER END
20*6bbe0590SSundeep Panicker  */
21*6bbe0590SSundeep Panicker 
22*6bbe0590SSundeep Panicker /*
23*6bbe0590SSundeep Panicker  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*6bbe0590SSundeep Panicker  * Use is subject to license terms.
25*6bbe0590SSundeep Panicker  */
26*6bbe0590SSundeep Panicker 
27*6bbe0590SSundeep Panicker #ifndef	_CRCMODEL_H
28*6bbe0590SSundeep Panicker #define	_CRCMODEL_H
29*6bbe0590SSundeep Panicker 
30*6bbe0590SSundeep Panicker #include <sys/types.h>
31*6bbe0590SSundeep Panicker 
32*6bbe0590SSundeep Panicker #ifdef __cplusplus
33*6bbe0590SSundeep Panicker extern "C" {
34*6bbe0590SSundeep Panicker #endif
35*6bbe0590SSundeep Panicker 
36*6bbe0590SSundeep Panicker /*
37*6bbe0590SSundeep Panicker  *
38*6bbe0590SSundeep Panicker  *                             Start of crcmodel.h
39*6bbe0590SSundeep Panicker  *
40*6bbe0590SSundeep Panicker  *
41*6bbe0590SSundeep Panicker  * Author : Ross Williams (ross@guest.adelaide.edu.au.).
42*6bbe0590SSundeep Panicker  * Date   : 3 June 1993.
43*6bbe0590SSundeep Panicker  * Status : Public domain.
44*6bbe0590SSundeep Panicker  *
45*6bbe0590SSundeep Panicker  * Description : This is the header (.h) file for the reference
46*6bbe0590SSundeep Panicker  * implementation of the Rocksoft^tm Model CRC Algorithm. For more
47*6bbe0590SSundeep Panicker  * information on the Rocksoft^tm Model CRC Algorithm, see the document
48*6bbe0590SSundeep Panicker  * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
49*6bbe0590SSundeep Panicker  * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
50*6bbe0590SSundeep Panicker  * "ftp.adelaide.edu.au/pub/rocksoft".
51*6bbe0590SSundeep Panicker  *
52*6bbe0590SSundeep Panicker  * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
53*6bbe0590SSundeep Panicker  *
54*6bbe0590SSundeep Panicker  *
55*6bbe0590SSundeep Panicker  *
56*6bbe0590SSundeep Panicker  * How to Use This Package
57*6bbe0590SSundeep Panicker  * -----------------------
58*6bbe0590SSundeep Panicker  * Step 1: Declare a variable of type cm_t. Declare another variable
59*6bbe0590SSundeep Panicker  *         (p_cm say) of type p_cm_t and initialize it to point to the first
60*6bbe0590SSundeep Panicker  *         variable (e.g. p_cm_t p_cm = &cm_t).
61*6bbe0590SSundeep Panicker  *
62*6bbe0590SSundeep Panicker  * Step 2: Assign values to the parameter fields of the structure.
63*6bbe0590SSundeep Panicker  *         If you don't know what to assign, see the document cited earlier.
64*6bbe0590SSundeep Panicker  *         For example:
65*6bbe0590SSundeep Panicker  *            p_cm->cm_width = 16;
66*6bbe0590SSundeep Panicker  *            p_cm->cm_poly  = 0x8005L;
67*6bbe0590SSundeep Panicker  *            p_cm->cm_init  = 0L;
68*6bbe0590SSundeep Panicker  *            p_cm->cm_refin = TRUE;
69*6bbe0590SSundeep Panicker  *            p_cm->cm_refot = TRUE;
70*6bbe0590SSundeep Panicker  *            p_cm->cm_xorot = 0L;
71*6bbe0590SSundeep Panicker  *         Note: Poly is specified without its top bit (18005 becomes 8005).
72*6bbe0590SSundeep Panicker  *         Note: Width is one bit less than the raw poly width.
73*6bbe0590SSundeep Panicker  *
74*6bbe0590SSundeep Panicker  * Step 3: Initialize the instance with a call cm_ini(p_cm);
75*6bbe0590SSundeep Panicker  *
76*6bbe0590SSundeep Panicker  * Step 4: Process zero or more message bytes by placing zero or more
77*6bbe0590SSundeep Panicker  *         successive calls to cm_nxt. Example: cm_nxt(p_cm,ch);
78*6bbe0590SSundeep Panicker  *
79*6bbe0590SSundeep Panicker  * Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm);
80*6bbe0590SSundeep Panicker  *         If the CRC is a 16-bit value, it will be in the bottom 16 bits.
81*6bbe0590SSundeep Panicker  *
82*6bbe0590SSundeep Panicker  *
83*6bbe0590SSundeep Panicker  *
84*6bbe0590SSundeep Panicker  * Design Notes
85*6bbe0590SSundeep Panicker  * ------------
86*6bbe0590SSundeep Panicker  * PORTABILITY: This package has been coded very conservatively so that
87*6bbe0590SSundeep Panicker  * it will run on as many machines as possible. For example, all external
88*6bbe0590SSundeep Panicker  * identifiers have been restricted to 6 characters and all internal ones to
89*6bbe0590SSundeep Panicker  * 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid
90*6bbe0590SSundeep Panicker  * namespace collisions. This package is endian independent.
91*6bbe0590SSundeep Panicker  *
92*6bbe0590SSundeep Panicker  * EFFICIENCY: This package (and its interface) is not designed for
93*6bbe0590SSundeep Panicker  * speed. The purpose of this package is to act as a well-defined reference
94*6bbe0590SSundeep Panicker  * model for the specification of CRC algorithms. If you want speed, cook up
95*6bbe0590SSundeep Panicker  * a specific table-driven implementation as described in the document cited
96*6bbe0590SSundeep Panicker  * above. This package is designed for validation only; if you have found or
97*6bbe0590SSundeep Panicker  * implemented a CRC algorithm and wish to describe it as a set of parameters
98*6bbe0590SSundeep Panicker  * to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation
99*6bbe0590SSundeep Panicker  * should behave identically to this package under those parameters.
100*6bbe0590SSundeep Panicker  *
101*6bbe0590SSundeep Panicker  */
102*6bbe0590SSundeep Panicker 
103*6bbe0590SSundeep Panicker 
104*6bbe0590SSundeep Panicker /* The following definitions are extracted from my style header file which */
105*6bbe0590SSundeep Panicker /* would be cumbersome to distribute with this package. The DONE_STYLE is the */
106*6bbe0590SSundeep Panicker /* idempotence symbol used in my style header file. */
107*6bbe0590SSundeep Panicker 
108*6bbe0590SSundeep Panicker #ifndef DONE_STYLE
109*6bbe0590SSundeep Panicker 
110*6bbe0590SSundeep Panicker typedef unsigned bool;
111*6bbe0590SSundeep Panicker typedef unsigned char *p_ubyte_;
112*6bbe0590SSundeep Panicker 
113*6bbe0590SSundeep Panicker #ifndef TRUE
114*6bbe0590SSundeep Panicker #define	FALSE 0
115*6bbe0590SSundeep Panicker #define	TRUE  1
116*6bbe0590SSundeep Panicker #endif
117*6bbe0590SSundeep Panicker 
118*6bbe0590SSundeep Panicker /* Change to the second definition if you don't have prototypes. */
119*6bbe0590SSundeep Panicker #define	P_(A) A
120*6bbe0590SSundeep Panicker /* #define P_(A) () */
121*6bbe0590SSundeep Panicker 
122*6bbe0590SSundeep Panicker /* Uncomment this definition if you don't have void. */
123*6bbe0590SSundeep Panicker /* typedef int void; */
124*6bbe0590SSundeep Panicker 
125*6bbe0590SSundeep Panicker #endif
126*6bbe0590SSundeep Panicker 
127*6bbe0590SSundeep Panicker /* CRC Model Abstract Type */
128*6bbe0590SSundeep Panicker /* ----------------------- */
129*6bbe0590SSundeep Panicker /* The following type stores the context of an executing instance of the */
130*6bbe0590SSundeep Panicker /* model algorithm. Most of the fields are model parameters which must be */
131*6bbe0590SSundeep Panicker /* set before the first initializing call to cm_ini. */
132*6bbe0590SSundeep Panicker typedef struct
133*6bbe0590SSundeep Panicker {
134*6bbe0590SSundeep Panicker 	int cm_width; /* Parameter: Width in bits [8,32]. */
135*6bbe0590SSundeep Panicker 	uint32_t cm_poly; /* Parameter: The algorithm's polynomial. */
136*6bbe0590SSundeep Panicker 	uint32_t cm_init; /* Parameter: Initial register value. */
137*6bbe0590SSundeep Panicker 	bool cm_refin; /* Parameter: Reflect input bytes? */
138*6bbe0590SSundeep Panicker 	bool cm_refot; /* Parameter: Reflect output CRC? */
139*6bbe0590SSundeep Panicker 	uint32_t cm_xorot; /* Parameter: XOR this to output CRC. */
140*6bbe0590SSundeep Panicker 
141*6bbe0590SSundeep Panicker 	uint32_t cm_reg; /* Context: Context during execution. */
142*6bbe0590SSundeep Panicker } cm_t;
143*6bbe0590SSundeep Panicker typedef cm_t *p_cm_t;
144*6bbe0590SSundeep Panicker 
145*6bbe0590SSundeep Panicker /* Functions That Implement The Model */
146*6bbe0590SSundeep Panicker /* ---------------------------------- */
147*6bbe0590SSundeep Panicker /* The following functions animate the cm_t abstraction. */
148*6bbe0590SSundeep Panicker 
149*6bbe0590SSundeep Panicker void cm_ini P_((p_cm_t p_cm));
150*6bbe0590SSundeep Panicker /* Initializes the argument CRC model instance. */
151*6bbe0590SSundeep Panicker /* All parameter fields must be set before calling this. */
152*6bbe0590SSundeep Panicker 
153*6bbe0590SSundeep Panicker void cm_nxt P_((p_cm_t p_cm, int ch));
154*6bbe0590SSundeep Panicker /* Processes a single message byte [0,255]. */
155*6bbe0590SSundeep Panicker 
156*6bbe0590SSundeep Panicker void cm_blk P_((p_cm_t p_cm, p_ubyte_ blk_adr, uint32_t blk_len));
157*6bbe0590SSundeep Panicker /* Processes a block of message bytes. */
158*6bbe0590SSundeep Panicker 
159*6bbe0590SSundeep Panicker uint32_t cm_crc P_((p_cm_t p_cm));
160*6bbe0590SSundeep Panicker /* Returns the CRC value for the message bytes processed so far. */
161*6bbe0590SSundeep Panicker 
162*6bbe0590SSundeep Panicker /* Functions For Table Calculation */
163*6bbe0590SSundeep Panicker /* ------------------------------- */
164*6bbe0590SSundeep Panicker /* The following function can be used to calculate a CRC lookup table. */
165*6bbe0590SSundeep Panicker /* It can also be used at run-time to create or check static tables. */
166*6bbe0590SSundeep Panicker 
167*6bbe0590SSundeep Panicker uint32_t cm_tab P_((p_cm_t p_cm, int index));
168*6bbe0590SSundeep Panicker /* Returns the i'th entry for the lookup table for the specified algorithm. */
169*6bbe0590SSundeep Panicker /* The function examines the fields cm_width, cm_poly, cm_refin, and the */
170*6bbe0590SSundeep Panicker /* argument table index in the range [0,255] and returns the table entry in */
171*6bbe0590SSundeep Panicker /* the bottom cm_width bytes of the return value. */
172*6bbe0590SSundeep Panicker 
173*6bbe0590SSundeep Panicker #ifdef __cplusplus
174*6bbe0590SSundeep Panicker }
175*6bbe0590SSundeep Panicker #endif
176*6bbe0590SSundeep Panicker 
177*6bbe0590SSundeep Panicker #endif /* _CRCMODEL_H */