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