blob: d95c6e38a45591bd8b3dff70aff4ce993c40e8e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * crc.c
3 *
4 * PURPOSE
5 * Routines to generate, calculate, and test a 16-bit CRC.
6 *
7 * DESCRIPTION
8 * The CRC code was devised by Don P. Mitchell of AT&T Bell Laboratories
9 * and Ned W. Rhodes of Software Systems Group. It has been published in
10 * "Design and Validation of Computer Protocols", Prentice Hall,
11 * Englewood Cliffs, NJ, 1991, Chapter 3, ISBN 0-13-539925-4.
12 *
13 * Copyright is held by AT&T.
14 *
15 * AT&T gives permission for the free use of the CRC source code.
16 *
17 * CONTACTS
18 * E-mail regarding any portion of the Linux UDF file system should be
19 * directed to the development team mailing list (run by majordomo):
20 * linux_udf@hpesjro.fc.hp.com
21 *
22 * COPYRIGHT
23 * This file is distributed under the terms of the GNU General Public
24 * License (GPL). Copies of the GPL can be obtained from:
25 * ftp://prep.ai.mit.edu/pub/gnu/GPL
26 * Each contributing author retains all rights to their own work.
27 */
28
29#include "udfdecl.h"
30
31static uint16_t crc_table[256] = {
32 0x0000U, 0x1021U, 0x2042U, 0x3063U, 0x4084U, 0x50a5U, 0x60c6U, 0x70e7U,
33 0x8108U, 0x9129U, 0xa14aU, 0xb16bU, 0xc18cU, 0xd1adU, 0xe1ceU, 0xf1efU,
34 0x1231U, 0x0210U, 0x3273U, 0x2252U, 0x52b5U, 0x4294U, 0x72f7U, 0x62d6U,
35 0x9339U, 0x8318U, 0xb37bU, 0xa35aU, 0xd3bdU, 0xc39cU, 0xf3ffU, 0xe3deU,
36 0x2462U, 0x3443U, 0x0420U, 0x1401U, 0x64e6U, 0x74c7U, 0x44a4U, 0x5485U,
37 0xa56aU, 0xb54bU, 0x8528U, 0x9509U, 0xe5eeU, 0xf5cfU, 0xc5acU, 0xd58dU,
38 0x3653U, 0x2672U, 0x1611U, 0x0630U, 0x76d7U, 0x66f6U, 0x5695U, 0x46b4U,
39 0xb75bU, 0xa77aU, 0x9719U, 0x8738U, 0xf7dfU, 0xe7feU, 0xd79dU, 0xc7bcU,
40 0x48c4U, 0x58e5U, 0x6886U, 0x78a7U, 0x0840U, 0x1861U, 0x2802U, 0x3823U,
41 0xc9ccU, 0xd9edU, 0xe98eU, 0xf9afU, 0x8948U, 0x9969U, 0xa90aU, 0xb92bU,
42 0x5af5U, 0x4ad4U, 0x7ab7U, 0x6a96U, 0x1a71U, 0x0a50U, 0x3a33U, 0x2a12U,
43 0xdbfdU, 0xcbdcU, 0xfbbfU, 0xeb9eU, 0x9b79U, 0x8b58U, 0xbb3bU, 0xab1aU,
44 0x6ca6U, 0x7c87U, 0x4ce4U, 0x5cc5U, 0x2c22U, 0x3c03U, 0x0c60U, 0x1c41U,
45 0xedaeU, 0xfd8fU, 0xcdecU, 0xddcdU, 0xad2aU, 0xbd0bU, 0x8d68U, 0x9d49U,
46 0x7e97U, 0x6eb6U, 0x5ed5U, 0x4ef4U, 0x3e13U, 0x2e32U, 0x1e51U, 0x0e70U,
47 0xff9fU, 0xefbeU, 0xdfddU, 0xcffcU, 0xbf1bU, 0xaf3aU, 0x9f59U, 0x8f78U,
48 0x9188U, 0x81a9U, 0xb1caU, 0xa1ebU, 0xd10cU, 0xc12dU, 0xf14eU, 0xe16fU,
49 0x1080U, 0x00a1U, 0x30c2U, 0x20e3U, 0x5004U, 0x4025U, 0x7046U, 0x6067U,
50 0x83b9U, 0x9398U, 0xa3fbU, 0xb3daU, 0xc33dU, 0xd31cU, 0xe37fU, 0xf35eU,
51 0x02b1U, 0x1290U, 0x22f3U, 0x32d2U, 0x4235U, 0x5214U, 0x6277U, 0x7256U,
52 0xb5eaU, 0xa5cbU, 0x95a8U, 0x8589U, 0xf56eU, 0xe54fU, 0xd52cU, 0xc50dU,
53 0x34e2U, 0x24c3U, 0x14a0U, 0x0481U, 0x7466U, 0x6447U, 0x5424U, 0x4405U,
54 0xa7dbU, 0xb7faU, 0x8799U, 0x97b8U, 0xe75fU, 0xf77eU, 0xc71dU, 0xd73cU,
55 0x26d3U, 0x36f2U, 0x0691U, 0x16b0U, 0x6657U, 0x7676U, 0x4615U, 0x5634U,
56 0xd94cU, 0xc96dU, 0xf90eU, 0xe92fU, 0x99c8U, 0x89e9U, 0xb98aU, 0xa9abU,
57 0x5844U, 0x4865U, 0x7806U, 0x6827U, 0x18c0U, 0x08e1U, 0x3882U, 0x28a3U,
58 0xcb7dU, 0xdb5cU, 0xeb3fU, 0xfb1eU, 0x8bf9U, 0x9bd8U, 0xabbbU, 0xbb9aU,
59 0x4a75U, 0x5a54U, 0x6a37U, 0x7a16U, 0x0af1U, 0x1ad0U, 0x2ab3U, 0x3a92U,
60 0xfd2eU, 0xed0fU, 0xdd6cU, 0xcd4dU, 0xbdaaU, 0xad8bU, 0x9de8U, 0x8dc9U,
61 0x7c26U, 0x6c07U, 0x5c64U, 0x4c45U, 0x3ca2U, 0x2c83U, 0x1ce0U, 0x0cc1U,
62 0xef1fU, 0xff3eU, 0xcf5dU, 0xdf7cU, 0xaf9bU, 0xbfbaU, 0x8fd9U, 0x9ff8U,
63 0x6e17U, 0x7e36U, 0x4e55U, 0x5e74U, 0x2e93U, 0x3eb2U, 0x0ed1U, 0x1ef0U
64};
65
66/*
67 * udf_crc
68 *
69 * PURPOSE
70 * Calculate a 16-bit CRC checksum using ITU-T V.41 polynomial.
71 *
72 * DESCRIPTION
73 * The OSTA-UDF(tm) 1.50 standard states that using CRCs is mandatory.
74 * The polynomial used is: x^16 + x^12 + x^15 + 1
75 *
76 * PRE-CONDITIONS
77 * data Pointer to the data block.
78 * size Size of the data block.
79 *
80 * POST-CONDITIONS
81 * <return> CRC of the data block.
82 *
83 * HISTORY
84 * July 21, 1997 - Andrew E. Mileski
85 * Adapted from OSTA-UDF(tm) 1.50 standard.
86 */
87uint16_t
88udf_crc(uint8_t *data, uint32_t size, uint16_t crc)
89{
90 while (size--)
91 crc = crc_table[(crc >> 8 ^ *(data++)) & 0xffU] ^ (crc << 8);
92
93 return crc;
94}
95
96/****************************************************************************/
97#if defined(TEST)
98
99/*
100 * PURPOSE
101 * Test udf_crc()
102 *
103 * HISTORY
104 * July 21, 1997 - Andrew E. Mileski
105 * Adapted from OSTA-UDF(tm) 1.50 standard.
106 */
107
108unsigned char bytes[] = { 0x70U, 0x6AU, 0x77U };
109
110int main(void)
111{
112 unsigned short x;
113
114 x = udf_crc16(bytes, sizeof bytes);
115 printf("udf_crc16: calculated = %4.4x, correct = %4.4x\n", x, 0x3299U);
116
117 return 0;
118}
119
120#endif /* defined(TEST) */
121
122/****************************************************************************/
123#if defined(GENERATE)
124
125/*
126 * PURPOSE
127 * Generate a table for fast 16-bit CRC calculations (any polynomial).
128 *
129 * DESCRIPTION
130 * The ITU-T V.41 polynomial is 010041.
131 *
132 * HISTORY
133 * July 21, 1997 - Andrew E. Mileski
134 * Adapted from OSTA-UDF(tm) 1.50 standard.
135 */
136
137#include <stdio.h>
138
139int main(int argc, char **argv)
140{
141 unsigned long crc, poly;
142 int n, i;
143
144 /* Get the polynomial */
145 sscanf(argv[1], "%lo", &poly);
146 if (poly & 0xffff0000U){
147 fprintf(stderr, "polynomial is too large\en");
148 exit(1);
149 }
150
151 printf("/* CRC 0%o */\n", poly);
152
153 /* Create a table */
154 printf("static unsigned short crc_table[256] = {\n");
155 for (n = 0; n < 256; n++){
156 if (n % 8 == 0)
157 printf("\t");
158 crc = n << 8;
159 for (i = 0; i < 8; i++){
160 if(crc & 0x8000U)
161 crc = (crc << 1) ^ poly;
162 else
163 crc <<= 1;
164 crc &= 0xFFFFU;
165 }
166 if (n == 255)
167 printf("0x%04xU ", crc);
168 else
169 printf("0x%04xU, ", crc);
170 if(n % 8 == 7)
171 printf("\n");
172 }
173 printf("};\n");
174
175 return 0;
176}
177
178#endif /* defined(GENERATE) */