📄 crc_v3.txt
字号:
to initialize the algorithm register to a non-zero value.14. Defining Algorithms Absolutely----------------------------------At this point we have covered all the different aspects oftable-driven CRC algorithms. As there are so many variations on thesealgorithms, it is worth trying to establish a nomenclature for them.This section attempts to do that.We have seen that CRC algorithms vary in: * Width of the poly (polynomial). * Value of the poly. * Initial value for the register. * Whether the bits of each byte are reflected before being processed. * Whether the algorithm feeds input bytes through the register or xors them with a byte from one end and then straight into the table. * Whether the final register value should be reversed (as in reflected versions). * Value to XOR with the final register value.In order to be able to talk about particular CRC algorithms, we needto able to define them more precisely than this. For this reason, thenext section attempts to provide a well-defined parameterized modelfor CRC algorithms. To refer to a particular algorithm, we need thensimply specify the algorithm in terms of parameters to the model.15. A Parameterized Model For CRC Algorithms--------------------------------------------In this section we define a precise parameterized model CRC algorithmwhich, for want of a better name, we will call the "Rocksoft^tm ModelCRC Algorithm" (and why not? Rocksoft^tm could do with some freeadvertising :-).The most important aspect of the model algorithm is that it focussesexclusively on functionality, ignoring all implementation details. Theaim of the exercise is to construct a way of referring precisely toparticular CRC algorithms, regardless of how confusingly they areimplemented. To this end, the model must be as simple and precise aspossible, with as little confusion as possible.The Rocksoft^tm Model CRC Algorithm is based essentially on the DIRECTTABLE ALGORITHM specified earlier. However, the algorithm has to befurther parameterized to enable it to behave in the same way as someof the messier algorithms out in the real world.To enable the algorithm to behave like reflected algorithms, weprovide a boolean option to reflect the input bytes, and a booleanoption to specify whether to reflect the output checksum value. Byframing reflection as an input/output transformation, we avoid theconfusion of having to mentally map the parameters of reflected andnon-reflected algorithms.An extra parameter allows the algorithm's register to be initializedto a particular value. A further parameter is XORed with the finalvalue before it is returned.By putting all these pieces together we end up with the parameters ofthe algorithm: NAME: This is a name given to the algorithm. A string value. WIDTH: This is the width of the algorithm expressed in bits. This is one less than the width of the Poly. POLY: This parameter is the poly. This is a binary value that should be specified as a hexadecimal number. The top bit of the poly should be omitted. For example, if the poly is 10110, you should specify 06. An important aspect of this parameter is that it represents the unreflected poly; the bottom bit of this parameter is always the LSB of the divisor during the division regardless of whether the algorithm being modelled is reflected. INIT: This parameter specifies the initial value of the register when the algorithm starts. This is the value that is to be assigned to the register in the direct table algorithm. In the table algorithm, we may think of the register always commencing with the value zero, and this value being XORed into the register after the N'th bit iteration. This parameter should be specified as a hexadecimal number. REFIN: This is a boolean parameter. If it is FALSE, input bytes are processed with bit 7 being treated as the most significant bit (MSB) and bit 0 being treated as the least significant bit. If this parameter is FALSE, each byte is reflected before being processed. REFOUT: This is a boolean parameter. If it is set to FALSE, the final value in the register is fed into the XOROUT stage directly, otherwise, if this parameter is TRUE, the final register value is reflected first. XOROUT: This is an W-bit value that should be specified as a hexadecimal number. It is XORed to the final register value (after the REFOUT) stage before the value is returned as the official checksum. CHECK: This field is not strictly part of the definition, and, in the event of an inconsistency between this field and the other field, the other fields take precedence. This field is a check value that can be used as a weak validator of implementations of the algorithm. The field contains the checksum obtained when the ASCII string "123456789" is fed through the specified algorithm (i.e. 313233... (hexadecimal)).With these parameters defined, the model can now be used to specify aparticular CRC algorithm exactly. Here is an example specification fora popular form of the CRC-16 algorithm. Name : "CRC-16" Width : 16 Poly : 8005 Init : 0000 RefIn : True RefOut : True XorOut : 0000 Check : BB3D16. A Catalog of Parameter Sets for Standards---------------------------------------------At this point, I would like to give a list of the specifications forcommonly used CRC algorithms. However, most of the algorithms that Ihave come into contact with so far are specified in such a vague waythat this has not been possible. What I can provide is a list of polysfor various CRC standards I have heard of: X25 standard : 1021 [CRC-CCITT, ADCCP, SDLC/HDLC] X25 reversed : 0811 CRC16 standard : 8005 CRC16 reversed : 4003 [LHA] CRC32 : 04C11DB7 [PKZIP, AUTODIN II, Ethernet, FDDI]I would be interested in hearing from anyone out there who can tiedown the complete set of model parameters for any of these standards.However, a program that was kicking around seemed to imply thefollowing specifications. Can anyone confirm or deny them (or providethe check values (which I couldn't be bothered coding up andcalculating)). Name : "CRC-16/CITT" Width : 16 Poly : 1021 Init : FFFF RefIn : False RefOut : False XorOut : 0000 Check : ? Name : "XMODEM" Width : 16 Poly : 8408 Init : 0000 RefIn : True RefOut : True XorOut : 0000 Check : ? Name : "ARC" Width : 16 Poly : 8005 Init : 0000 RefIn : True RefOut : True XorOut : 0000 Check : ?Here is the specification for the CRC-32 algorithm which is reportedlyused in PKZip, AUTODIN II, Ethernet, and FDDI. Name : "CRC-32" Width : 32 Poly : 04C11DB7 Init : FFFFFFFF RefIn : True RefOut : True XorOut : FFFFFFFF Check : CBF4392617. An Implementation of the Model Algorithm--------------------------------------------Here is an implementation of the model algorithm in the C programminglanguage. The implementation consists of a header file (.h) and animplementation file (.c). If you're reading this document in asequential scroller, you can skip this code by searching for thestring "Roll Your Own".To ensure that the following code is working, configure it for theCRC-16 and CRC-32 algorithms given above and ensure that they producethe specified "check" checksum when fed the test string "123456789"(see earlier)./******************************************************************************//* Start of crcmodel.h *//******************************************************************************//* *//* Author : Ross Williams (ross@guest.adelaide.edu.au.). *//* Date : 3 June 1993. *//* Status : Public domain. *//* *//* Description : This is the header (.h) file for the reference *//* implementation of the Rocksoft^tm Model CRC Algorithm. For more *//* information on the Rocksoft^tm Model CRC Algorithm, see the document *//* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross *//* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in *//* "ftp.adelaide.edu.au/pub/rocksoft". *//* *//* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. *//* *//******************************************************************************//* *//* How to Use This Package *//* ----------------------- *//* Step 1: Declare a variable of type cm_t. Declare another variable *//* (p_cm say) of type p_cm_t and initialize it to point to the first *//* variable (e.g. p_cm_t p_cm = &cm_t). *//* *//* Step 2: Assign values to the parameter fields of the structure. *//* If you don't know what to assign, see the document cited earlier. *//* For example: *//* p_cm->cm_width = 16; *//* p_cm->cm_poly = 0x8005L; *//* p_cm->cm_init = 0L; *//* p_cm->cm_refin = TRUE; *//* p_cm->cm_refot = TRUE; *//* p_cm->cm_xorot = 0L; *//* Note: Poly is specified without its top bit (18005 becomes 8005). *//* Note: Width is one bit less than the raw poly width. *//* *//* Step 3: Initialize the instance with a call cm_ini(p_cm); *//* *//* Step 4: Process zero or more message bytes by placing zero or more *//* successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); *//* *//* Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); *//* If the CRC is a 16-bit value, it will be in the bottom 16 bits. *//* *//******************************************************************************//* *//* Design Notes *//* ------------ *//* PORTABILITY: This package has been coded very conservatively so that *//* it will run on as many machines as possible. For example, all external *//* identifiers have been restricted to 6 characters and all internal ones to *//* 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid *//* namespace collisions. This package is endian independent. *//* *//* EFFICIENCY: This package (and its interface) is not designed for *//* speed. The purpose of this package is to act as a well-defined reference *//* model for the specification of CRC algorithms. If you want speed, cook up *//* a specific table-driven implementation as described in the document cited *//* above. This package is designed for validation only; if you have found or *//* implemented a CRC algorithm and wish to describe it as a set of parameters *//* to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation *//* should behave identically to this package under those parameters. *//* *//******************************************************************************//* The following #ifndef encloses this entire *//* header file, rendering it indempotent. */#ifndef CM_DONE#define CM_DONE/******************************************************************************//* The following definitions are extracted from my style header file which *//* would be cumbersome to distribute with this package. The DONE_STYLE is the *//* idempotence symbol used in my style header file. */#ifndef DONE_STYLEtypedef unsigned long ulong;typedef unsigned bool;typedef unsigned char * p_ubyte_;#ifndef TRUE#define FALSE 0#define TRUE 1#endif/* Change to the second definition if you don't have prototypes. */#define P_(A) A/* #define P_(A) () *//* Uncomment this definition if you don't have void. *//* typedef int void; */#endif/************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -