crcmodel.c revision 6bbe05905a1c10a2703f95fb4912eb14b87f6670
346N/A/*
346N/A * CDDL HEADER START
346N/A *
346N/A * The contents of this file are subject to the terms of the
346N/A * Common Development and Distribution License (the "License").
346N/A * You may not use this file except in compliance with the License.
346N/A *
346N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
346N/A * or http://www.opensolaris.org/os/licensing.
346N/A * See the License for the specific language governing permissions
346N/A * and limitations under the License.
346N/A *
346N/A * When distributing Covered Code, include this CDDL HEADER in each
346N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
346N/A * If applicable, add the following below this CDDL HEADER, with the
346N/A * fields enclosed by brackets "[]" replaced with your own identifying
346N/A * information: Portions Copyright [yyyy] [name of copyright owner]
346N/A *
346N/A * CDDL HEADER END
346N/A */
346N/A
346N/A/*
783N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
346N/A * Use is subject to license terms.
346N/A */
346N/A
346N/A/*
783N/A *
783N/A * Start of crcmodel.c
1273N/A *
346N/A *
346N/A * Author : Ross Williams (ross@guest.adelaide.edu.au.).
346N/A * Date : 3 June 1993.
346N/A * Status : Public domain.
346N/A *
618N/A * Description : This is the implementation (.c) file for the reference
1273N/A * implementation of the Rocksoft^tm Model CRC Algorithm. For more
1273N/A * information on the Rocksoft^tm Model CRC Algorithm, see the document
346N/A * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
844N/A * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
844N/A * "ftp.adelaide.edu.au/pub/rocksoft".
618N/A *
1273N/A * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
346N/A *
346N/A *
346N/A *
346N/A * Implementation Notes
346N/A * --------------------
844N/A * To avoid inconsistencies, the specification of each function is not echoed
844N/A * here. See the header file for a description of these functions.
618N/A * This package is light on checking because I want to keep it short and
346N/A * simple and portable (i.e. it would be too messy to distribute my entire
346N/A * C culture (e.g. assertions package) with this package.
346N/A *
346N/A *
346N/A */
844N/A
844N/A#include "crcmodel.h"
618N/A
346N/A/* The following definitions make the code more readable. */
346N/A
346N/A#define BITMASK(X) (1L << (X))
346N/A#define MASK32 0xFFFFFFFFL
346N/A#define LOCAL static
346N/A
346N/ALOCAL uint32_t reflect P_((uint32_t v, int b));
346N/ALOCAL uint32_t
346N/Areflect(v, b)
346N/A/* Returns the value v with the bottom b [0,32] bits reflected. */
346N/A/* Example: reflect(0x3e23L,3) == 0x3e26 */
346N/Auint32_t v;
346N/Aint b;
346N/A{
346N/A int i;
346N/A uint32_t t = v;
346N/A for (i = 0; i < b; i++) {
346N/A if (t & 1L)
346N/A v |= BITMASK((b-1)-i);
346N/A else
346N/A v &= ~BITMASK((b-1)-i);
346N/A t >>= 1;
346N/A }
346N/A return (v);
346N/A}
351N/A
351N/ALOCAL uint32_t widmask P_((p_cm_t));
351N/ALOCAL uint32_t
351N/Awidmask(p_cm)
351N/A/* Returns a longword whose value is (2^p_cm->cm_width)-1. */
346N/A/* The trick is to do this portably (e.g. without doing <<32). */
351N/Ap_cm_t p_cm;
346N/A{
346N/A return ((((1L<<(p_cm->cm_width-1))-1L)<<1)|1L);
346N/A}
346N/A
346N/Avoid
346N/Acm_ini(p_cm)
346N/Ap_cm_t p_cm;
346N/A{
346N/A p_cm->cm_reg = p_cm->cm_init;
346N/A}
346N/A
346N/Avoid
346N/Acm_nxt(p_cm, ch)
346N/Ap_cm_t p_cm;
346N/Aint ch;
346N/A{
346N/A int i;
346N/A uint32_t uch = (uint32_t)ch;
346N/A uint32_t topbit = BITMASK(p_cm->cm_width-1);
346N/A
346N/A if (p_cm->cm_refin)
346N/A uch = reflect(uch, 8);
346N/A
346N/A p_cm->cm_reg ^= (uch << (p_cm->cm_width-8));
346N/A for (i = 0; i < 8; i++) {
346N/A if (p_cm->cm_reg & topbit)
346N/A p_cm->cm_reg = (p_cm->cm_reg << 1) ^ p_cm->cm_poly;
346N/A else
346N/A p_cm->cm_reg <<= 1;
346N/A
346N/A p_cm->cm_reg &= widmask(p_cm);
346N/A }
346N/A}
void
cm_blk(p_cm, blk_adr, blk_len)
p_cm_t p_cm;
p_ubyte_ blk_adr;
uint32_t blk_len;
{
while (blk_len--)
cm_nxt(p_cm, *blk_adr++);
}
uint32_t
cm_crc(p_cm)
p_cm_t p_cm;
{
if (p_cm->cm_refot)
return (p_cm->cm_xorot ^ reflect(p_cm->cm_reg, p_cm->cm_width));
else
return (p_cm->cm_xorot ^ p_cm->cm_reg);
}
uint32_t
cm_tab(p_cm, index)
p_cm_t p_cm;
int index;
{
int i;
uint32_t r;
uint32_t topbit = BITMASK(p_cm->cm_width-1);
uint32_t inbyte = (uint32_t)index;
if (p_cm->cm_refin)
inbyte = reflect(inbyte, 8);
r = inbyte << (p_cm->cm_width-8);
for (i = 0; i < 8; i++)
if (r & topbit)
r = (r << 1) ^ p_cm->cm_poly;
else
r <<= 1;
if (p_cm->cm_refin)
r = reflect(r, p_cm->cm_width);
return (r & widmask(p_cm));
}