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