File: crc.c

package info (click to toggle)
evms 1.0.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,168 kB
  • ctags: 5,853
  • sloc: ansic: 87,317; makefile: 691; sh: 238
file content (158 lines) | stat: -rw-r--r-- 6,676 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 * Module: CRC.c
 *
 * Functions:
 *
 *              Build_CRC_table()
 *              CalculateCRC()
 *
 *
 * Description: The functions in this module provide a means of calculating
 *              the 32 bit CRC for a block of data.  Build_CRC_table must
 *              be called to initialize this module.  CalculateCRC must
 *              NOT be used until after Build_CRC_table has been called.
 *              Once Build_CRC_table has been called, CalculateCRC can
 *              be used to calculate the CRC of the data residing in a
 *              user specified buffer.
 *
 */

#include <stdlib.h>
#include <common.h>
#include <fullengine.h>
#include "engine.h"
#include "crc.h"


/*--------------------------------------------------
 * Private Constants
 --------------------------------------------------*/
#define CRC_POLYNOMIAL     0xEDB88320L


/*--------------------------------------------------
 * Private Global Variables.
 --------------------------------------------------*/
static u_int32_t  CRC_table[ 256 ];

static BOOLEAN CRC_table_Built=FALSE;  /* TRUE=table is built, FALSE=table not built yet */

/*********************************************************************/
/*                                                                   */
/*   Function Name: build_CRC_table                                  */
/*                                                                   */
/*   Descriptive Name: This module implements the CRC function using */
/*                     a table driven method.  The required table    */
/*                     must be setup before the CalculateCRC function*/
/*                     can be used.  This table only needs to be set */
/*                     up once.  This function sets up the CRC table */
/*                     needed by CalculateCRC.                       */
/*                                                                   */
/*   Input: None                                                     */
/*                                                                   */
/*   Output: None                                                    */
/*                                                                   */
/*   Error Handling: N/A                                             */
/*                                                                   */
/*   Side Effects:  The internal CRC Table is initialized.           */
/*                                                                   */
/*   Notes:  None.                                                   */
/*                                                                   */
/*********************************************************************/
static void  build_CRC_table( void ) {
    u_int32_t   i, j, CRC;

    LOG_PROC_ENTRY();

    for ( i = 0; i <= 255 ; i++ ) {

        CRC = i;

        for ( j = 8 ; j > 0; j-- ) {

            if ( CRC & 1 )
                CRC = ( CRC >> 1 ) ^ CRC_POLYNOMIAL;
            else
                CRC >>= 1;
        }

        CRC_table[ i ] = CRC;

    }

    CRC_table_Built = TRUE;

    LOG_PROC_EXIT_VOID();
}


/*********************************************************************/
/*                                                                   */
/*   Function Name: calculate_CRC                                    */
/*                                                                   */
/*   Descriptive Name: This function calculates the CRC value for    */
/*                     the data in the buffer specified by Buffer.   */
/*                                                                   */
/*   Input: CARDINAL32   CRC : This is the starting CRC.  If you are */
/*                             starting a new CRC calculation, then  */
/*                             this should be set to 0xFFFFFFFFL.  If*/
/*                             you are continuing a CRC calculation  */
/*                             (i.e. all of the data did not fit in  */
/*                             the buffer so you could not calculate */
/*                             the CRC in a single operation), then  */
/*                             this is the CRC output by the last    */
/*                             CalculateCRC call.                    */
/*                                                                   */
/*   Output: The CRC for the data in the buffer, based upon the value*/
/*           of the input parameter CRC.                             */
/*                                                                   */
/*   Error Handling: None.                                           */
/*                                                                   */
/*   Side Effects:  None.                                            */
/*                                                                   */
/*   Notes:  None.                                                   */
/*                                                                   */
/*********************************************************************/
u_int32_t   calculate_CRC( u_int32_t CRC, void * Buffer, u_int32_t buffer_size) {
    u_char  * current_byte;
    u_int32_t temp1, temp2, I;

    LOG_PROC_ENTRY();

    current_byte = ( u_char *) Buffer;

    /* Make sure the CRC table is available */
    if (CRC_table_Built==FALSE)  build_CRC_table();

    /* Process each byte in the buffer. */
    for ( I = 0; I < buffer_size; I++ ) {

        temp1 = (CRC >> 8) & 0x00FFFFFFL;
        temp2 = CRC_table[ ( CRC ^ (u_int32_t) *current_byte ) & (u_int32_t) 0xff ];
        current_byte++;
        CRC = temp1 ^ temp2;
    }

    LOG_PROC_EXIT_UINT(CRC);
    return( CRC );
}