crcmodel.h revision 11015:0a0751599d31
187866Ssheldonh/* 287866Ssheldonh * CDDL HEADER START 387866Ssheldonh * 487866Ssheldonh * The contents of this file are subject to the terms of the 587866Ssheldonh * Common Development and Distribution License (the "License"). 687866Ssheldonh * You may not use this file except in compliance with the License. 787866Ssheldonh * 887866Ssheldonh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 987866Ssheldonh * or http://www.opensolaris.org/os/licensing. 1087866Ssheldonh * See the License for the specific language governing permissions 1187866Ssheldonh * and limitations under the License. 1287866Ssheldonh * 1387866Ssheldonh * When distributing Covered Code, include this CDDL HEADER in each 1487866Ssheldonh * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1587866Ssheldonh * If applicable, add the following below this CDDL HEADER, with the 1687866Ssheldonh * fields enclosed by brackets "[]" replaced with your own identifying 1787866Ssheldonh * information: Portions Copyright [yyyy] [name of copyright owner] 1887866Ssheldonh * 1987866Ssheldonh * CDDL HEADER END 2087866Ssheldonh */ 2187866Ssheldonh 2287866Ssheldonh/* 2387866Ssheldonh * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2487866Ssheldonh * Use is subject to license terms. 2587866Ssheldonh */ 2687866Ssheldonh 2787866Ssheldonh#ifndef _CRCMODEL_H 2887866Ssheldonh#define _CRCMODEL_H 2987866Ssheldonh 3087866Ssheldonh#ifdef SOLARIS_UNIX 3187866Ssheldonh#include <sys/types.h> 3287866Ssheldonh#else 3387866Ssheldonhtypedef long uint32_t; 3487866Ssheldonh#endif 3587866Ssheldonh 3687866Ssheldonh#ifdef __cplusplus 3787866Ssheldonhextern "C" { 3887866Ssheldonh#endif 3987866Ssheldonh 4087866Ssheldonh/* 4187866Ssheldonh * 4287866Ssheldonh * Start of crcmodel.h 4387866Ssheldonh * 4487866Ssheldonh * 4587866Ssheldonh * Author : Ross Williams (ross@guest.adelaide.edu.au.). 4687866Ssheldonh * Date : 3 June 1993. 4787866Ssheldonh * Status : Public domain. 4887866Ssheldonh * 4987866Ssheldonh * Description : This is the header (.h) file for the reference 5087866Ssheldonh * implementation of the Rocksoft^tm Model CRC Algorithm. For more 5187866Ssheldonh * information on the Rocksoft^tm Model CRC Algorithm, see the document 5287866Ssheldonh * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross 5387866Ssheldonh * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in 5487866Ssheldonh * "ftp.adelaide.edu.au/pub/rocksoft". 5587866Ssheldonh * 5687866Ssheldonh * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. 5787866Ssheldonh * 5887866Ssheldonh * 5987866Ssheldonh * 6087866Ssheldonh * How to Use This Package 6187866Ssheldonh * ----------------------- 6287866Ssheldonh * Step 1: Declare a variable of type cm_t. Declare another variable 6387866Ssheldonh * (p_cm say) of type p_cm_t and initialize it to point to the first 6487866Ssheldonh * variable (e.g. p_cm_t p_cm = &cm_t). 6587866Ssheldonh * 6687866Ssheldonh * Step 2: Assign values to the parameter fields of the structure. 6787866Ssheldonh * If you don't know what to assign, see the document cited earlier. 6887866Ssheldonh * For example: 6987866Ssheldonh * p_cm->cm_width = 16; 7087866Ssheldonh * p_cm->cm_poly = 0x8005L; 7187866Ssheldonh * p_cm->cm_init = 0L; 7287866Ssheldonh * p_cm->cm_refin = TRUE; 7387866Ssheldonh * p_cm->cm_refot = TRUE; 7487866Ssheldonh * p_cm->cm_xorot = 0L; 7587866Ssheldonh * Note: Poly is specified without its top bit (18005 becomes 8005). 7687866Ssheldonh * Note: Width is one bit less than the raw poly width. 7787866Ssheldonh * 7887866Ssheldonh * Step 3: Initialize the instance with a call cm_ini(p_cm); 7987866Ssheldonh * 8087866Ssheldonh * Step 4: Process zero or more message bytes by placing zero or more 8187866Ssheldonh * successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); 8287866Ssheldonh * 8387866Ssheldonh * Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); 8487866Ssheldonh * If the CRC is a 16-bit value, it will be in the bottom 16 bits. 8587866Ssheldonh * 8687866Ssheldonh * 8787866Ssheldonh * 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 114typedef unsigned bool; 115typedef 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. */ 136typedef 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; 147typedef cm_t *p_cm_t; 148 149/* Functions That Implement The Model */ 150/* ---------------------------------- */ 151/* The following functions animate the cm_t abstraction. */ 152 153void 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 157void cm_nxt P_((p_cm_t p_cm, int ch)); 158/* Processes a single message byte [0,255]. */ 159 160void cm_blk P_((p_cm_t p_cm, p_ubyte_ blk_adr, uint32_t blk_len)); 161/* Processes a block of message bytes. */ 162 163uint32_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 171uint32_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