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