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