Category : Files from Magazines
Archive   : CGAZV5N1.ZIP
Filename : MKCCITT.C

 
Output of file : MKCCITT.C contained in archive : CGAZV5N1.ZIP

/*---MKCCITT.C ------------------------ Listing 2 ---
* Makes a CCITT Table as a header file
* Outputs to screen. Redirect to disk as needed.
* In this article, we expect table creation with
* MKCCITT > CCITT.TBL
*
* Author: Nigel Cort
* (c) 1990 C Gazette. Use freely but acknowledge author
--------------------------------------------------*/

#include

unsigned short calculate_ccitt ( unsigned int );

unsigned short table [256];

void main ( void )
{
unsigned int i;

for ( i = 0; i < 256; i++ )
table [i] = calculate_ccitt ( i );

puts
("/* CCITT Lookup Table */ \nunsigned short ccitt_table[256] =\n{");

for ( i = 0; i < 256; i++ )
{
if ( (i % 8) == 0)
printf ("\n \/* %3u -- */ ", i );

printf ("0x%04X", table [i]); /* print the CRC checkvalue */
if (i != 255)
printf (", ");
}
puts ("\n};"); /* closing brace for table */
}

unsigned short calculate_ccitt ( unsigned int index )
{
unsigned short a, i;
a = 0; /* serves as an accumulator */

index <<= 8;

for ( i = 8; i > 0; i-- ) /* The heart of the CCITT CRC */
{
if ( (index ^ a ) & 0x8000 )
a = ( a << 1 ) ^ 0x1021;
else
a <<= 1;
index <<= 1;
}
return a;
}