]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/crc64.c
1 /* SPDX-License-Identifier: MIT */
7 * This file is licensed under the MIT License as stated below
9 * Copyright (c) 2016 Lammert Bies
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * The source file contains the routines which are needed to
32 * calculate a 64 bit CRC value of a sequence of bytes.
40 #define CRC_START_64_ECMA 0x0000000000000000ull
41 #define CRC_START_64_WE 0xFFFFFFFFFFFFFFFFull
43 const uint64_t crc_tab64
[256] = {
44 0x0000000000000000ull
,
45 0x42F0E1EBA9EA3693ull
,
46 0x85E1C3D753D46D26ull
,
47 0xC711223CFA3E5BB5ull
,
48 0x493366450E42ECDFull
,
49 0x0BC387AEA7A8DA4Cull
,
50 0xCCD2A5925D9681F9ull
,
51 0x8E224479F47CB76Aull
,
52 0x9266CC8A1C85D9BEull
,
53 0xD0962D61B56FEF2Dull
,
54 0x17870F5D4F51B498ull
,
55 0x5577EEB6E6BB820Bull
,
56 0xDB55AACF12C73561ull
,
57 0x99A54B24BB2D03F2ull
,
58 0x5EB4691841135847ull
,
59 0x1C4488F3E8F96ED4ull
,
60 0x663D78FF90E185EFull
,
61 0x24CD9914390BB37Cull
,
62 0xE3DCBB28C335E8C9ull
,
63 0xA12C5AC36ADFDE5Aull
,
64 0x2F0E1EBA9EA36930ull
,
65 0x6DFEFF5137495FA3ull
,
66 0xAAEFDD6DCD770416ull
,
67 0xE81F3C86649D3285ull
,
68 0xF45BB4758C645C51ull
,
69 0xB6AB559E258E6AC2ull
,
70 0x71BA77A2DFB03177ull
,
71 0x334A9649765A07E4ull
,
72 0xBD68D2308226B08Eull
,
73 0xFF9833DB2BCC861Dull
,
74 0x388911E7D1F2DDA8ull
,
75 0x7A79F00C7818EB3Bull
,
76 0xCC7AF1FF21C30BDEull
,
77 0x8E8A101488293D4Dull
,
78 0x499B3228721766F8ull
,
79 0x0B6BD3C3DBFD506Bull
,
80 0x854997BA2F81E701ull
,
81 0xC7B97651866BD192ull
,
82 0x00A8546D7C558A27ull
,
83 0x4258B586D5BFBCB4ull
,
84 0x5E1C3D753D46D260ull
,
85 0x1CECDC9E94ACE4F3ull
,
86 0xDBFDFEA26E92BF46ull
,
87 0x990D1F49C77889D5ull
,
88 0x172F5B3033043EBFull
,
89 0x55DFBADB9AEE082Cull
,
90 0x92CE98E760D05399ull
,
91 0xD03E790CC93A650Aull
,
92 0xAA478900B1228E31ull
,
93 0xE8B768EB18C8B8A2ull
,
94 0x2FA64AD7E2F6E317ull
,
95 0x6D56AB3C4B1CD584ull
,
96 0xE374EF45BF6062EEull
,
97 0xA1840EAE168A547Dull
,
98 0x66952C92ECB40FC8ull
,
99 0x2465CD79455E395Bull
,
100 0x3821458AADA7578Full
,
101 0x7AD1A461044D611Cull
,
102 0xBDC0865DFE733AA9ull
,
103 0xFF3067B657990C3Aull
,
104 0x711223CFA3E5BB50ull
,
105 0x33E2C2240A0F8DC3ull
,
106 0xF4F3E018F031D676ull
,
107 0xB60301F359DBE0E5ull
,
108 0xDA050215EA6C212Full
,
109 0x98F5E3FE438617BCull
,
110 0x5FE4C1C2B9B84C09ull
,
111 0x1D14202910527A9Aull
,
112 0x93366450E42ECDF0ull
,
113 0xD1C685BB4DC4FB63ull
,
114 0x16D7A787B7FAA0D6ull
,
115 0x5427466C1E109645ull
,
116 0x4863CE9FF6E9F891ull
,
117 0x0A932F745F03CE02ull
,
118 0xCD820D48A53D95B7ull
,
119 0x8F72ECA30CD7A324ull
,
120 0x0150A8DAF8AB144Eull
,
121 0x43A04931514122DDull
,
122 0x84B16B0DAB7F7968ull
,
123 0xC6418AE602954FFBull
,
124 0xBC387AEA7A8DA4C0ull
,
125 0xFEC89B01D3679253ull
,
126 0x39D9B93D2959C9E6ull
,
127 0x7B2958D680B3FF75ull
,
128 0xF50B1CAF74CF481Full
,
129 0xB7FBFD44DD257E8Cull
,
130 0x70EADF78271B2539ull
,
131 0x321A3E938EF113AAull
,
132 0x2E5EB66066087D7Eull
,
133 0x6CAE578BCFE24BEDull
,
134 0xABBF75B735DC1058ull
,
135 0xE94F945C9C3626CBull
,
136 0x676DD025684A91A1ull
,
137 0x259D31CEC1A0A732ull
,
138 0xE28C13F23B9EFC87ull
,
139 0xA07CF2199274CA14ull
,
140 0x167FF3EACBAF2AF1ull
,
141 0x548F120162451C62ull
,
142 0x939E303D987B47D7ull
,
143 0xD16ED1D631917144ull
,
144 0x5F4C95AFC5EDC62Eull
,
145 0x1DBC74446C07F0BDull
,
146 0xDAAD56789639AB08ull
,
147 0x985DB7933FD39D9Bull
,
148 0x84193F60D72AF34Full
,
149 0xC6E9DE8B7EC0C5DCull
,
150 0x01F8FCB784FE9E69ull
,
151 0x43081D5C2D14A8FAull
,
152 0xCD2A5925D9681F90ull
,
153 0x8FDAB8CE70822903ull
,
154 0x48CB9AF28ABC72B6ull
,
155 0x0A3B7B1923564425ull
,
156 0x70428B155B4EAF1Eull
,
157 0x32B26AFEF2A4998Dull
,
158 0xF5A348C2089AC238ull
,
159 0xB753A929A170F4ABull
,
160 0x3971ED50550C43C1ull
,
161 0x7B810CBBFCE67552ull
,
162 0xBC902E8706D82EE7ull
,
163 0xFE60CF6CAF321874ull
,
164 0xE224479F47CB76A0ull
,
165 0xA0D4A674EE214033ull
,
166 0x67C58448141F1B86ull
,
167 0x253565A3BDF52D15ull
,
168 0xAB1721DA49899A7Full
,
169 0xE9E7C031E063ACECull
,
170 0x2EF6E20D1A5DF759ull
,
171 0x6C0603E6B3B7C1CAull
,
172 0xF6FAE5C07D3274CDull
,
173 0xB40A042BD4D8425Eull
,
174 0x731B26172EE619EBull
,
175 0x31EBC7FC870C2F78ull
,
176 0xBFC9838573709812ull
,
177 0xFD39626EDA9AAE81ull
,
178 0x3A28405220A4F534ull
,
179 0x78D8A1B9894EC3A7ull
,
180 0x649C294A61B7AD73ull
,
181 0x266CC8A1C85D9BE0ull
,
182 0xE17DEA9D3263C055ull
,
183 0xA38D0B769B89F6C6ull
,
184 0x2DAF4F0F6FF541ACull
,
185 0x6F5FAEE4C61F773Full
,
186 0xA84E8CD83C212C8Aull
,
187 0xEABE6D3395CB1A19ull
,
188 0x90C79D3FEDD3F122ull
,
189 0xD2377CD44439C7B1ull
,
190 0x15265EE8BE079C04ull
,
191 0x57D6BF0317EDAA97ull
,
192 0xD9F4FB7AE3911DFDull
,
193 0x9B041A914A7B2B6Eull
,
194 0x5C1538ADB04570DBull
,
195 0x1EE5D94619AF4648ull
,
196 0x02A151B5F156289Cull
,
197 0x4051B05E58BC1E0Full
,
198 0x87409262A28245BAull
,
199 0xC5B073890B687329ull
,
200 0x4B9237F0FF14C443ull
,
201 0x0962D61B56FEF2D0ull
,
202 0xCE73F427ACC0A965ull
,
203 0x8C8315CC052A9FF6ull
,
204 0x3A80143F5CF17F13ull
,
205 0x7870F5D4F51B4980ull
,
206 0xBF61D7E80F251235ull
,
207 0xFD913603A6CF24A6ull
,
208 0x73B3727A52B393CCull
,
209 0x31439391FB59A55Full
,
210 0xF652B1AD0167FEEAull
,
211 0xB4A25046A88DC879ull
,
212 0xA8E6D8B54074A6ADull
,
213 0xEA16395EE99E903Eull
,
214 0x2D071B6213A0CB8Bull
,
215 0x6FF7FA89BA4AFD18ull
,
216 0xE1D5BEF04E364A72ull
,
217 0xA3255F1BE7DC7CE1ull
,
218 0x64347D271DE22754ull
,
219 0x26C49CCCB40811C7ull
,
220 0x5CBD6CC0CC10FAFCull
,
221 0x1E4D8D2B65FACC6Full
,
222 0xD95CAF179FC497DAull
,
223 0x9BAC4EFC362EA149ull
,
224 0x158E0A85C2521623ull
,
225 0x577EEB6E6BB820B0ull
,
226 0x906FC95291867B05ull
,
227 0xD29F28B9386C4D96ull
,
228 0xCEDBA04AD0952342ull
,
229 0x8C2B41A1797F15D1ull
,
230 0x4B3A639D83414E64ull
,
231 0x09CA82762AAB78F7ull
,
232 0x87E8C60FDED7CF9Dull
,
233 0xC51827E4773DF90Eull
,
234 0x020905D88D03A2BBull
,
235 0x40F9E43324E99428ull
,
236 0x2CFFE7D5975E55E2ull
,
237 0x6E0F063E3EB46371ull
,
238 0xA91E2402C48A38C4ull
,
239 0xEBEEC5E96D600E57ull
,
240 0x65CC8190991CB93Dull
,
241 0x273C607B30F68FAEull
,
242 0xE02D4247CAC8D41Bull
,
243 0xA2DDA3AC6322E288ull
,
244 0xBE992B5F8BDB8C5Cull
,
245 0xFC69CAB42231BACFull
,
246 0x3B78E888D80FE17Aull
,
247 0x7988096371E5D7E9ull
,
248 0xF7AA4D1A85996083ull
,
249 0xB55AACF12C735610ull
,
250 0x724B8ECDD64D0DA5ull
,
251 0x30BB6F267FA73B36ull
,
252 0x4AC29F2A07BFD00Dull
,
253 0x08327EC1AE55E69Eull
,
254 0xCF235CFD546BBD2Bull
,
255 0x8DD3BD16FD818BB8ull
,
256 0x03F1F96F09FD3CD2ull
,
257 0x41011884A0170A41ull
,
258 0x86103AB85A2951F4ull
,
259 0xC4E0DB53F3C36767ull
,
260 0xD8A453A01B3A09B3ull
,
261 0x9A54B24BB2D03F20ull
,
262 0x5D45907748EE6495ull
,
263 0x1FB5719CE1045206ull
,
264 0x919735E51578E56Cull
,
265 0xD367D40EBC92D3FFull
,
266 0x1476F63246AC884Aull
,
267 0x568617D9EF46BED9ull
,
268 0xE085162AB69D5E3Cull
,
269 0xA275F7C11F7768AFull
,
270 0x6564D5FDE549331Aull
,
271 0x279434164CA30589ull
,
272 0xA9B6706FB8DFB2E3ull
,
273 0xEB46918411358470ull
,
274 0x2C57B3B8EB0BDFC5ull
,
275 0x6EA7525342E1E956ull
,
276 0x72E3DAA0AA188782ull
,
277 0x30133B4B03F2B111ull
,
278 0xF7021977F9CCEAA4ull
,
279 0xB5F2F89C5026DC37ull
,
280 0x3BD0BCE5A45A6B5Dull
,
281 0x79205D0E0DB05DCEull
,
282 0xBE317F32F78E067Bull
,
283 0xFCC19ED95E6430E8ull
,
284 0x86B86ED5267CDBD3ull
,
285 0xC4488F3E8F96ED40ull
,
286 0x0359AD0275A8B6F5ull
,
287 0x41A94CE9DC428066ull
,
288 0xCF8B0890283E370Cull
,
289 0x8D7BE97B81D4019Full
,
290 0x4A6ACB477BEA5A2Aull
,
291 0x089A2AACD2006CB9ull
,
292 0x14DEA25F3AF9026Dull
,
293 0x562E43B4931334FEull
,
294 0x913F6188692D6F4Bull
,
295 0xD3CF8063C0C759D8ull
,
296 0x5DEDC41A34BBEEB2ull
,
297 0x1F1D25F19D51D821ull
,
298 0xD80C07CD676F8394ull
,
299 0x9AFCE626CE85B507ull
304 * uint64_t crc_64_ecma( const unsigned char *input_str, size_t num_bytes );
306 * The function crc_64_ecma() calculates in one pass the ECMA 64 bit CRC value
307 * for a byte string that is passed to the function together with a parameter
308 * indicating the length.
311 uint64_t ul_crc64_ecma( const unsigned char *input_str
, size_t num_bytes
) {
314 const unsigned char *ptr
;
317 crc
= CRC_START_64_ECMA
;
320 if ( ptr
!= NULL
) for (a
=0; a
<num_bytes
; a
++) {
322 crc
= (crc
<< 8) ^ crc_tab64
[ ((crc
>> 56) ^ (uint64_t) *ptr
++) & 0x00000000000000FFull
];
330 * uint64_t ul_crc64_we( const unsigned char *input_str, size_t num_bytes );
332 * The function crc_64_we() calculates in one pass the CRC64-WE 64 bit CRC
333 * value for a byte string that is passed to the function together with a
334 * parameter indicating the length.
337 uint64_t ul_crc64_we( const unsigned char *input_str
, size_t num_bytes
) {
340 const unsigned char *ptr
;
343 crc
= CRC_START_64_WE
;
346 if ( ptr
!= NULL
) for (a
=0; a
<num_bytes
; a
++) {
348 crc
= (crc
<< 8) ^ crc_tab64
[ ((crc
>> 56) ^ (uint64_t) *ptr
++) & 0x00000000000000FFull
];
351 return crc
^ 0xFFFFFFFFFFFFFFFFull
;
356 * uint64_t ul_update_crc64( uint64_t crc, unsigned char c );
358 * The function update_crc_64() calculates a new CRC-64 value based on the
359 * previous value of the CRC and the next byte of the data to be checked.
362 uint64_t ul_update_crc64( uint64_t crc
, unsigned char c
) {
364 return (crc
<< 8) ^ crc_tab64
[ ((crc
>> 56) ^ (uint64_t) c
) & 0x00000000000000FFull
];
366 } /* update_crc_64 */