1 /*****************************************************************************
2 * FullFAT - High Performance, Thread-Safe Embedded FAT File-System *
3 * Copyright (C) 2009 James Walmsley (james@worm.me.uk) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 * Alternative Licensing is available directly from the Copyright holder, *
21 * (James Walmsley). For more information consult LICENSING.TXT to obtain *
22 * a Commercial license. *
24 * See RESTRICTIONS.TXT for extra restrictions on the use of FullFAT. *
26 * Removing the above notice is illegal and will invalidate this license. *
27 *****************************************************************************
28 * See http://worm.me.uk/fullfat for more information. *
29 * Or http://fullfat.googlecode.com/ for latest releases and the wiki. *
30 *****************************************************************************/
34 * @author James Walmsley
37 * @defgroup CRC CRC Checksums for Strings
38 * @brief Provides fast hashing functions.
44 static const FF_T_UINT8 CRC16_Low
[256] =
46 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
47 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
48 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
49 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
50 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
51 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
52 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
53 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
54 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
55 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
56 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
57 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
58 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
59 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
60 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
61 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
62 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
63 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
64 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
65 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
66 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
67 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
68 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
69 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
70 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
71 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
72 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
73 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
74 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
75 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
76 0x000, 0x0c1, 0x081, 0x040, 0x001, 0x0c0, 0x080, 0x041,
77 0x001, 0x0c0, 0x080, 0x041, 0x000, 0x0c1, 0x081, 0x040,
80 static const FF_T_UINT8 CRC16_High
[256] =
82 0x000, 0x0c0, 0x0c1, 0x001, 0x0c3, 0x003, 0x002, 0x0c2,
83 0x0c6, 0x006, 0x007, 0x0c7, 0x005, 0x0c5, 0x0c4, 0x004,
84 0x0cc, 0x00c, 0x00d, 0x0cd, 0x00f, 0x0cf, 0x0ce, 0x00e,
85 0x00a, 0x0ca, 0x0cb, 0x00b, 0x0c9, 0x009, 0x008, 0x0c8,
86 0x0d8, 0x018, 0x019, 0x0d9, 0x01b, 0x0db, 0x0da, 0x01a,
87 0x01e, 0x0de, 0x0df, 0x01f, 0x0dd, 0x01d, 0x01c, 0x0dc,
88 0x014, 0x0d4, 0x0d5, 0x015, 0x0d7, 0x017, 0x016, 0x0d6,
89 0x0d2, 0x012, 0x013, 0x0d3, 0x011, 0x0d1, 0x0d0, 0x010,
90 0x0f0, 0x030, 0x031, 0x0f1, 0x033, 0x0f3, 0x0f2, 0x032,
91 0x036, 0x0f6, 0x0f7, 0x037, 0x0f5, 0x035, 0x034, 0x0f4,
92 0x03c, 0x0fc, 0x0fd, 0x03d, 0x0ff, 0x03f, 0x03e, 0x0fe,
93 0x0fa, 0x03a, 0x03b, 0x0fb, 0x039, 0x0f9, 0x0f8, 0x038,
94 0x028, 0x0e8, 0x0e9, 0x029, 0x0eb, 0x02b, 0x02a, 0x0ea,
95 0x0ee, 0x02e, 0x02f, 0x0ef, 0x02d, 0x0ed, 0x0ec, 0x02c,
96 0x0e4, 0x024, 0x025, 0x0e5, 0x027, 0x0e7, 0x0e6, 0x026,
97 0x022, 0x0e2, 0x0e3, 0x023, 0x0e1, 0x021, 0x020, 0x0e0,
98 0x0a0, 0x060, 0x061, 0x0a1, 0x063, 0x0a3, 0x0a2, 0x062,
99 0x066, 0x0a6, 0x0a7, 0x067, 0x0a5, 0x065, 0x064, 0x0a4,
100 0x06c, 0x0ac, 0x0ad, 0x06d, 0x0af, 0x06f, 0x06e, 0x0ae,
101 0x0aa, 0x06a, 0x06b, 0x0ab, 0x069, 0x0a9, 0x0a8, 0x068,
102 0x078, 0x0b8, 0x0b9, 0x079, 0x0bb, 0x07b, 0x07a, 0x0ba,
103 0x0be, 0x07e, 0x07f, 0x0bf, 0x07d, 0x0bd, 0x0bc, 0x07c,
104 0x0b4, 0x074, 0x075, 0x0b5, 0x077, 0x0b7, 0x0b6, 0x076,
105 0x072, 0x0b2, 0x0b3, 0x073, 0x0b1, 0x071, 0x070, 0x0b0,
106 0x050, 0x090, 0x091, 0x051, 0x093, 0x053, 0x052, 0x092,
107 0x096, 0x056, 0x057, 0x097, 0x055, 0x095, 0x094, 0x054,
108 0x09c, 0x05c, 0x05d, 0x09d, 0x05f, 0x09f, 0x09e, 0x05e,
109 0x05a, 0x09a, 0x09b, 0x05b, 0x099, 0x059, 0x058, 0x098,
110 0x088, 0x048, 0x049, 0x089, 0x04b, 0x08b, 0x08a, 0x04a,
111 0x04e, 0x08e, 0x08f, 0x04f, 0x08d, 0x04d, 0x04c, 0x08c,
112 0x044, 0x084, 0x085, 0x045, 0x087, 0x047, 0x046, 0x086,
113 0x082, 0x042, 0x043, 0x083, 0x041, 0x081, 0x080, 0x040,
116 /*****************************************************************************
117 * Description: Function to 16 bit CRC check a block of memory
120 * Parameters: pbyData - Pointer to the source data
121 * stLength - The length to CRC
123 * Return value: The 16 bit CRC value
125 *****************************************************************************/
127 FF_T_UINT16
FF_GetCRC16(FF_T_UINT8
*pbyData
, FF_T_UINT32 stLength
) {
128 FF_T_UINT8 bTableValue
;
129 FF_T_UINT16 wCRC
= 0;
132 bTableValue
= (FF_T_UINT8
)((wCRC
& 0x00FF) ^ *pbyData
++);
133 wCRC
= (FF_T_UINT16
)(((CRC16_High
[bTableValue
]) << 8)
134 + (CRC16_Low
[bTableValue
] ^ ((wCRC
>> 8) & 0x00FF)));
141 static const FF_T_UINT8 byCRCLookUpTable
[256] =
143 0, 94, 188, 226, 97, 63, 221, 131,
144 194, 156, 126, 32, 163, 253, 31, 65,
145 157, 195, 33, 127, 252, 162, 64, 30,
146 95, 1, 227, 189, 62, 96, 130, 220,
147 35, 125, 159, 193, 66, 28, 254, 160,
148 225, 191, 93, 3, 128, 222, 60, 98,
149 190, 224, 2, 92, 223, 129, 99, 61,
150 124, 34, 192, 158, 29, 67, 161, 255,
151 70, 24, 250, 164, 39, 121, 155, 197,
152 132, 218, 56, 102, 229, 187, 89, 7,
153 219, 133, 103, 57, 186, 228, 6, 88,
154 25, 71, 165, 251, 120, 38, 196, 154,
155 101, 59, 217, 135, 4, 90, 184, 230,
156 167, 249, 27, 69, 198, 152, 122, 36,
157 248, 166, 68, 26, 153, 199, 37, 123,
158 58, 100, 134, 216, 91, 5, 231, 185,
159 140, 210, 48, 110, 237, 179, 81, 15,
160 78, 16, 242, 172, 47, 113, 147, 205,
161 17, 79, 173, 243, 112, 46, 204, 146,
162 211, 141, 111, 49, 178, 236, 14, 80,
163 175, 241, 19, 77, 206, 144, 114, 44,
164 109, 51, 209, 143, 12, 82, 176, 238,
165 50, 108, 142, 208, 83, 13, 239, 177,
166 240, 174, 76, 18, 145, 207, 45, 115,
167 202, 148, 118, 40, 171, 245, 23, 73,
168 8, 86, 180, 234, 105, 55, 213, 139,
169 87, 9, 235, 181, 54, 104, 138, 212,
170 149, 203, 41, 119, 244, 170, 72, 22,
171 233, 183, 85, 11, 136, 214, 52, 106,
172 43, 117, 151, 201, 74, 20, 246, 168,
173 116, 42, 200, 150, 21, 75, 169, 247,
174 182, 232, 10, 84, 215, 137, 107, 53
177 /*****************************************************************************
178 * Description: Function to CRC check a block of memory
180 * Parameters: pbyData - Pointer to the source data
181 * stLength - The length to CRC
183 * Return value: The 8 bit CRC value
185 *****************************************************************************/
187 FF_T_UINT8
FF_GetCRC8(FF_T_UINT8
*pbyData
, FF_T_UINT32 stLength
) {
188 FF_T_UINT8 byCRC
= 0, byData
;
191 byCRC
= byCRCLookUpTable
[(byCRC
^ byData
)];