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