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