CHROMA
crc48.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief 48-bit CRC
3  */
4 
5 #include "crc48.h"
6 
7 namespace CRC48
8 {
9 
10 /************************************************************************
11  48 bit CRC routines.
12 
13  These routines can be used to calculate a 48 bit CRC over an array of
14  characters.
15 
16  Author: Gary Mussar mussar@bnr.ca
17  Dec 1, 1991 These routines are public domain and may be freely use
18  in both academic projects and commercial products.
19 
20  The author would appreciate receiving any improvements
21  (or bug fixes)
22 **************************************************************************/
23 #include "crc48.h"
24 
25 /*
26  Define a number required items.
27 
28  initializer - use initialize the CRC accumulator at the start of frame
29  generator - 48 bit CRC generating polynomial
30  complement - use to modify CRC residual before transmission
31  CRCHIGHBIT - bit position in base integer used when carrying a
32  bit from on array element to the next
33  CRCBYTE0 - expression to extract byte 0 of CRC from structure
34  CRCBYTE1 - expression to extract byte 1 of CRC from structure
35  . .
36  . .
37 */
38 #ifdef CRCBASE8
39 # define CRCHIGHBIT 0x80
40  static CRC48_t initializer = {{0x33, 0xa5, 0x57, 0xdf, 0xf1, 0xec}};
41  static CRC48_t generator = {{0x28, 0x35, 0x09, 0x71, 0xdb, 0xea}};
42  static CRC48_t complement = {{0xcc, 0x5a, 0x57, 0xdf, 0x0e, 0x13}};
43 # define CRCBYTE0 crc[0]
44 # define CRCBYTE1 crc[1]
45 # define CRCBYTE2 crc[2]
46 # define CRCBYTE3 crc[3]
47 # define CRCBYTE4 crc[4]
48 # define CRCBYTE5 crc[5]
49 #endif
50 #ifdef CRCBASE16
51 # define CRCHIGHBIT 0x8000
52  static CRC48_t initializer = {{0xa533, 0xdf57, 0xecf1}};
53  static CRC48_t generator = {{0x3528, 0x7109, 0xeadb}};
54  static CRC48_t complement = {{0x5acc, 0xdf57, 0x130e}};
55 # define CRCBYTE0 crc[0]
56 # define CRCBYTE1 crc[0] >> 8
57 # define CRCBYTE2 crc[1]
58 # define CRCBYTE3 crc[1] >> 8
59 # define CRCBYTE4 crc[2]
60 # define CRCBYTE5 crc[2] >> 8
61 #endif
62 #ifdef CRCBASE32
63 # define CRCHIGHBIT 0x80000000L
64  static CRC48_t initializer = {{0xdf57a533L, 0xecf1L}};
65  static CRC48_t generator = {{0x71093528L, 0xeadbL}};
66  static CRC48_t complement = {{0xdf575accL, 0x130eL}};
67 # define CRCBYTE0 crc[0]
68 # define CRCBYTE1 crc[0] >> 8
69 # define CRCBYTE2 crc[0] >> 16
70 # define CRCBYTE3 crc[0] >> 24
71 # define CRCBYTE4 crc[1]
72 # define CRCBYTE5 crc[1] >> 8
73 #endif
74 #ifdef CRCBASE64
75 # define CRCHIGHBIT 0x800000000000L
76  static CRC48_t initializer = {{0xecf1df57a533L}};
77  static CRC48_t generator = {{0xeadb71093528L}};
78  static CRC48_t complement = {{0x130edf575accL}};
79 # define CRCBYTE0 crc[0]
80 # define CRCBYTE1 crc[0] >> 8
81 # define CRCBYTE2 crc[0] >> 16
82 # define CRCBYTE3 crc[0] >> 24
83 # define CRCBYTE4 crc[0] >> 32
84 # define CRCBYTE5 crc[0] >> 40
85 #endif
86 
87 void initCRC48(CRC48_t& acc)
88 {
89  /* Initialize accumulator */
90  acc = initializer;
91 }
92 
93 
94 void calcCRC48(CRC48_t& acc, const void *dataPtr, int count)
95 {
96  /* Perform CRC over every byte in array */
97  for (unsigned char *p = (unsigned char *)dataPtr; count; count--)
98  {
99  /* Mathematically, it makes no difference whether all
100  the data bits of a byte are XORed at once and then
101  check CRC over the next 8 bits or whether the CRC is
102  done one bit at a time. */
103 
104  /* XOR whole byte into CRC accumulator */
105  acc.crc[0] ^= *(p++);
106 
107  /* Perform CRC check over next 8 bits */
108  for(int bits=0; bits<8; bits++)
109  {
110  /* Remember value of least significant byte of CRC */
111  int bit = acc.crc[0] & 0x01;
112 
113  /* Shift whole CRC one bit to right */
114  for (int i=0; i<CRCARRAYSIZE-1; i++)
115  {
116  acc.crc[i] >>= 1;
117  if (acc.crc[i+1] & 0x01) acc.crc[i] ^= CRCHIGHBIT;
118  }
119  acc.crc[CRCARRAYSIZE-1] >>= 1;
120 
121  /* If least significant bit was set, we should subtract
122  the generator polymonial */
123  if (bit)
124  {
125  for(int i = 0; i<CRCARRAYSIZE; i++)
126  acc.crc[i] ^= generator.crc[i];
127  }
128  }
129  }
130 }
131 
132 void getCRC48(const CRC48_t& acc, void *dataPtr, int count)
133 {
134  CRC48_t xmit;
135 
136  /* Sanity check. Who wants 0 or less bytes? */
137  if (count < 1) return;
138 
139  /* Copy CRC to temporary location and prepare for transmission by
140  XORing with complement */
141  xmit = acc;
142  for(int i = 0; i < CRCARRAYSIZE; i++)
143  xmit.crc[i] ^= complement.crc[i];
144 
145  unsigned char *p = (unsigned char*)dataPtr;
146 
147  /* Painfully extract each byte. This is uglier and less efficient than
148  it should be so that we can accomdate big endian and little endian
149  architectures as well as situations where the real size of the
150  integer used in the CRC array is larger than we have been told */
151  p[0] = xmit.CRCBYTE0;
152  if (count > 1)
153  p[1] = xmit.CRCBYTE1;
154  if (count > 2)
155  p[2] = xmit.CRCBYTE2;
156  if (count > 3)
157  p[3] = xmit.CRCBYTE3;
158  if (count > 4)
159  p[4] = xmit.CRCBYTE4;
160  if (count > 5)
161  p[5] = xmit.CRCBYTE5;
162 
163 }
164 
165 } // namespace CRC
#define CRCHIGHBIT
Definition: crc48.cc:39
48-bit CRC
#define CRCARRAYSIZE
Definition: crc48.h:67
unsigned i
Definition: ldumul_w.cc:34
Definition: crc48.cc:8
void getCRC48(const CRC48_t &acc, void *dataPtr, int count)
Definition: crc48.cc:132
void initCRC48(CRC48_t &acc)
Definition: crc48.cc:87
static CRC48_t complement
Definition: crc48.cc:42
static CRC48_t initializer
Definition: crc48.cc:40
static CRC48_t generator
Definition: crc48.cc:41
void calcCRC48(CRC48_t &acc, const void *dataPtr, int count)
Definition: crc48.cc:94
int count
Definition: octave.h:14
CRCBASETYPE crc[CRCARRAYSIZE]
Definition: crc48.h:87