xref: /illumos-gate/usr/src/lib/libfru/libfruraw/crcmodel.h (revision 438b5f69eb4f998015cc0835f19f2f80663f2f53)
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 #ifdef SOLARIS_UNIX
31 #include <sys/types.h>
32 #else
33 typedef long uint32_t;
34 #endif
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  *
42  *                             Start of crcmodel.h
43  *
44  *
45  * Author : Ross Williams (ross@guest.adelaide.edu.au.).
46  * Date   : 3 June 1993.
47  * Status : Public domain.
48  *
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".
55  *
56  * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
57  *
58  *
59  *
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).
65  *
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.
68  *         For example:
69  *            p_cm->cm_width = 16;
70  *            p_cm->cm_poly  = 0x8005L;
71  *            p_cm->cm_init  = 0L;
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.
77  *
78  * Step 3: Initialize the instance with a call cm_ini(p_cm);
79  *
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);
82  *
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.
85  *
86  *
87  *
88  * Design Notes
89  * ------------
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.
95  *
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.
104  *
105  */
106 
107 
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. */
111 
112 #ifndef DONE_STYLE
113 
114 typedef unsigned bool;
115 typedef unsigned char *p_ubyte_;
116 
117 #ifndef TRUE
118 #define	FALSE 0
119 #define	TRUE  1
120 #endif
121 
122 /* Change to the second definition if you don't have prototypes. */
123 #define	P_(A) A
124 /* #define P_(A) () */
125 
126 /* Uncomment this definition if you don't have void. */
127 /* typedef int void; */
128 
129 #endif
130 
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. */
136 typedef struct
137 {
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. */
144 
145 	uint32_t cm_reg; /* Context: Context during execution. */
146 } cm_t;
147 typedef cm_t *p_cm_t;
148 
149 /* Functions That Implement The Model */
150 /* ---------------------------------- */
151 /* The following functions animate the cm_t abstraction. */
152 
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. */
156 
157 void cm_nxt P_((p_cm_t p_cm, int ch));
158 /* Processes a single message byte [0,255]. */
159 
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. */
162 
163 uint32_t cm_crc P_((p_cm_t p_cm));
164 /* Returns the CRC value for the message bytes processed so far. */
165 
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. */
170 
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. */
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* _CRCMODEL_H */
182