]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/CheckSum.c
MdePkg/BaseLib: Add bit field population calculating methods
[mirror_edk2.git] / MdePkg / Library / BaseLib / CheckSum.c
1 /** @file
2 Utility functions to generate checksum based on 2's complement
3 algorithm.
4
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "BaseLibInternals.h"
17
18 /**
19 Returns the sum of all elements in a buffer in unit of UINT8.
20 During calculation, the carry bits are dropped.
21
22 This function calculates the sum of all elements in a buffer
23 in unit of UINT8. The carry bits in result of addition are dropped.
24 The result is returned as UINT8. If Length is Zero, then Zero is
25 returned.
26
27 If Buffer is NULL, then ASSERT().
28 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
29
30 @param Buffer The pointer to the buffer to carry out the sum operation.
31 @param Length The size, in bytes, of Buffer.
32
33 @return Sum The sum of Buffer with carry bits dropped during additions.
34
35 **/
36 UINT8
37 EFIAPI
38 CalculateSum8 (
39 IN CONST UINT8 *Buffer,
40 IN UINTN Length
41 )
42 {
43 UINT8 Sum;
44 UINTN Count;
45
46 ASSERT (Buffer != NULL);
47 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
48
49 for (Sum = 0, Count = 0; Count < Length; Count++) {
50 Sum = (UINT8) (Sum + *(Buffer + Count));
51 }
52
53 return Sum;
54 }
55
56
57 /**
58 Returns the two's complement checksum of all elements in a buffer
59 of 8-bit values.
60
61 This function first calculates the sum of the 8-bit values in the
62 buffer specified by Buffer and Length. The carry bits in the result
63 of addition are dropped. Then, the two's complement of the sum is
64 returned. If Length is 0, then 0 is returned.
65
66 If Buffer is NULL, then ASSERT().
67 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
68
69 @param Buffer The pointer to the buffer to carry out the checksum operation.
70 @param Length The size, in bytes, of Buffer.
71
72 @return Checksum The 2's complement checksum of Buffer.
73
74 **/
75 UINT8
76 EFIAPI
77 CalculateCheckSum8 (
78 IN CONST UINT8 *Buffer,
79 IN UINTN Length
80 )
81 {
82 UINT8 CheckSum;
83
84 CheckSum = CalculateSum8 (Buffer, Length);
85
86 //
87 // Return the checksum based on 2's complement.
88 //
89 return (UINT8) (0x100 - CheckSum);
90 }
91
92 /**
93 Returns the sum of all elements in a buffer of 16-bit values. During
94 calculation, the carry bits are dropped.
95
96 This function calculates the sum of the 16-bit values in the buffer
97 specified by Buffer and Length. The carry bits in result of addition are dropped.
98 The 16-bit result is returned. If Length is 0, then 0 is returned.
99
100 If Buffer is NULL, then ASSERT().
101 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
102 If Length is not aligned on a 16-bit boundary, then ASSERT().
103 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
104
105 @param Buffer The pointer to the buffer to carry out the sum operation.
106 @param Length The size, in bytes, of Buffer.
107
108 @return Sum The sum of Buffer with carry bits dropped during additions.
109
110 **/
111 UINT16
112 EFIAPI
113 CalculateSum16 (
114 IN CONST UINT16 *Buffer,
115 IN UINTN Length
116 )
117 {
118 UINT16 Sum;
119 UINTN Count;
120 UINTN Total;
121
122 ASSERT (Buffer != NULL);
123 ASSERT (((UINTN) Buffer & 0x1) == 0);
124 ASSERT ((Length & 0x1) == 0);
125 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
126
127 Total = Length / sizeof (*Buffer);
128 for (Sum = 0, Count = 0; Count < Total; Count++) {
129 Sum = (UINT16) (Sum + *(Buffer + Count));
130 }
131
132 return Sum;
133 }
134
135
136 /**
137 Returns the two's complement checksum of all elements in a buffer of
138 16-bit values.
139
140 This function first calculates the sum of the 16-bit values in the buffer
141 specified by Buffer and Length. The carry bits in the result of addition
142 are dropped. Then, the two's complement of the sum is returned. If Length
143 is 0, then 0 is returned.
144
145 If Buffer is NULL, then ASSERT().
146 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
147 If Length is not aligned on a 16-bit boundary, then ASSERT().
148 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
149
150 @param Buffer The pointer to the buffer to carry out the checksum operation.
151 @param Length The size, in bytes, of Buffer.
152
153 @return Checksum The 2's complement checksum of Buffer.
154
155 **/
156 UINT16
157 EFIAPI
158 CalculateCheckSum16 (
159 IN CONST UINT16 *Buffer,
160 IN UINTN Length
161 )
162 {
163 UINT16 CheckSum;
164
165 CheckSum = CalculateSum16 (Buffer, Length);
166
167 //
168 // Return the checksum based on 2's complement.
169 //
170 return (UINT16) (0x10000 - CheckSum);
171 }
172
173
174 /**
175 Returns the sum of all elements in a buffer of 32-bit values. During
176 calculation, the carry bits are dropped.
177
178 This function calculates the sum of the 32-bit values in the buffer
179 specified by Buffer and Length. The carry bits in result of addition are dropped.
180 The 32-bit result is returned. If Length is 0, then 0 is returned.
181
182 If Buffer is NULL, then ASSERT().
183 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
184 If Length is not aligned on a 32-bit boundary, then ASSERT().
185 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
186
187 @param Buffer The pointer to the buffer to carry out the sum operation.
188 @param Length The size, in bytes, of Buffer.
189
190 @return Sum The sum of Buffer with carry bits dropped during additions.
191
192 **/
193 UINT32
194 EFIAPI
195 CalculateSum32 (
196 IN CONST UINT32 *Buffer,
197 IN UINTN Length
198 )
199 {
200 UINT32 Sum;
201 UINTN Count;
202 UINTN Total;
203
204 ASSERT (Buffer != NULL);
205 ASSERT (((UINTN) Buffer & 0x3) == 0);
206 ASSERT ((Length & 0x3) == 0);
207 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
208
209 Total = Length / sizeof (*Buffer);
210 for (Sum = 0, Count = 0; Count < Total; Count++) {
211 Sum = Sum + *(Buffer + Count);
212 }
213
214 return Sum;
215 }
216
217
218 /**
219 Returns the two's complement checksum of all elements in a buffer of
220 32-bit values.
221
222 This function first calculates the sum of the 32-bit values in the buffer
223 specified by Buffer and Length. The carry bits in the result of addition
224 are dropped. Then, the two's complement of the sum is returned. If Length
225 is 0, then 0 is returned.
226
227 If Buffer is NULL, then ASSERT().
228 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
229 If Length is not aligned on a 32-bit boundary, then ASSERT().
230 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
231
232 @param Buffer The pointer to the buffer to carry out the checksum operation.
233 @param Length The size, in bytes, of Buffer.
234
235 @return Checksum The 2's complement checksum of Buffer.
236
237 **/
238 UINT32
239 EFIAPI
240 CalculateCheckSum32 (
241 IN CONST UINT32 *Buffer,
242 IN UINTN Length
243 )
244 {
245 UINT32 CheckSum;
246
247 CheckSum = CalculateSum32 (Buffer, Length);
248
249 //
250 // Return the checksum based on 2's complement.
251 //
252 return (UINT32) ((UINT32)(-1) - CheckSum + 1);
253 }
254
255
256 /**
257 Returns the sum of all elements in a buffer of 64-bit values. During
258 calculation, the carry bits are dropped.
259
260 This function calculates the sum of the 64-bit values in the buffer
261 specified by Buffer and Length. The carry bits in result of addition are dropped.
262 The 64-bit result is returned. If Length is 0, then 0 is returned.
263
264 If Buffer is NULL, then ASSERT().
265 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
266 If Length is not aligned on a 64-bit boundary, then ASSERT().
267 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
268
269 @param Buffer The pointer to the buffer to carry out the sum operation.
270 @param Length The size, in bytes, of Buffer.
271
272 @return Sum The sum of Buffer with carry bits dropped during additions.
273
274 **/
275 UINT64
276 EFIAPI
277 CalculateSum64 (
278 IN CONST UINT64 *Buffer,
279 IN UINTN Length
280 )
281 {
282 UINT64 Sum;
283 UINTN Count;
284 UINTN Total;
285
286 ASSERT (Buffer != NULL);
287 ASSERT (((UINTN) Buffer & 0x7) == 0);
288 ASSERT ((Length & 0x7) == 0);
289 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
290
291 Total = Length / sizeof (*Buffer);
292 for (Sum = 0, Count = 0; Count < Total; Count++) {
293 Sum = Sum + *(Buffer + Count);
294 }
295
296 return Sum;
297 }
298
299
300 /**
301 Returns the two's complement checksum of all elements in a buffer of
302 64-bit values.
303
304 This function first calculates the sum of the 64-bit values in the buffer
305 specified by Buffer and Length. The carry bits in the result of addition
306 are dropped. Then, the two's complement of the sum is returned. If Length
307 is 0, then 0 is returned.
308
309 If Buffer is NULL, then ASSERT().
310 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
311 If Length is not aligned on a 64-bit boundary, then ASSERT().
312 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
313
314 @param Buffer The pointer to the buffer to carry out the checksum operation.
315 @param Length The size, in bytes, of Buffer.
316
317 @return Checksum The 2's complement checksum of Buffer.
318
319 **/
320 UINT64
321 EFIAPI
322 CalculateCheckSum64 (
323 IN CONST UINT64 *Buffer,
324 IN UINTN Length
325 )
326 {
327 UINT64 CheckSum;
328
329 CheckSum = CalculateSum64 (Buffer, Length);
330
331 //
332 // Return the checksum based on 2's complement.
333 //
334 return (UINT64) ((UINT64)(-1) - CheckSum + 1);
335 }
336
337 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT32 mCrcTable[256] = {
338 0x00000000,
339 0x77073096,
340 0xEE0E612C,
341 0x990951BA,
342 0x076DC419,
343 0x706AF48F,
344 0xE963A535,
345 0x9E6495A3,
346 0x0EDB8832,
347 0x79DCB8A4,
348 0xE0D5E91E,
349 0x97D2D988,
350 0x09B64C2B,
351 0x7EB17CBD,
352 0xE7B82D07,
353 0x90BF1D91,
354 0x1DB71064,
355 0x6AB020F2,
356 0xF3B97148,
357 0x84BE41DE,
358 0x1ADAD47D,
359 0x6DDDE4EB,
360 0xF4D4B551,
361 0x83D385C7,
362 0x136C9856,
363 0x646BA8C0,
364 0xFD62F97A,
365 0x8A65C9EC,
366 0x14015C4F,
367 0x63066CD9,
368 0xFA0F3D63,
369 0x8D080DF5,
370 0x3B6E20C8,
371 0x4C69105E,
372 0xD56041E4,
373 0xA2677172,
374 0x3C03E4D1,
375 0x4B04D447,
376 0xD20D85FD,
377 0xA50AB56B,
378 0x35B5A8FA,
379 0x42B2986C,
380 0xDBBBC9D6,
381 0xACBCF940,
382 0x32D86CE3,
383 0x45DF5C75,
384 0xDCD60DCF,
385 0xABD13D59,
386 0x26D930AC,
387 0x51DE003A,
388 0xC8D75180,
389 0xBFD06116,
390 0x21B4F4B5,
391 0x56B3C423,
392 0xCFBA9599,
393 0xB8BDA50F,
394 0x2802B89E,
395 0x5F058808,
396 0xC60CD9B2,
397 0xB10BE924,
398 0x2F6F7C87,
399 0x58684C11,
400 0xC1611DAB,
401 0xB6662D3D,
402 0x76DC4190,
403 0x01DB7106,
404 0x98D220BC,
405 0xEFD5102A,
406 0x71B18589,
407 0x06B6B51F,
408 0x9FBFE4A5,
409 0xE8B8D433,
410 0x7807C9A2,
411 0x0F00F934,
412 0x9609A88E,
413 0xE10E9818,
414 0x7F6A0DBB,
415 0x086D3D2D,
416 0x91646C97,
417 0xE6635C01,
418 0x6B6B51F4,
419 0x1C6C6162,
420 0x856530D8,
421 0xF262004E,
422 0x6C0695ED,
423 0x1B01A57B,
424 0x8208F4C1,
425 0xF50FC457,
426 0x65B0D9C6,
427 0x12B7E950,
428 0x8BBEB8EA,
429 0xFCB9887C,
430 0x62DD1DDF,
431 0x15DA2D49,
432 0x8CD37CF3,
433 0xFBD44C65,
434 0x4DB26158,
435 0x3AB551CE,
436 0xA3BC0074,
437 0xD4BB30E2,
438 0x4ADFA541,
439 0x3DD895D7,
440 0xA4D1C46D,
441 0xD3D6F4FB,
442 0x4369E96A,
443 0x346ED9FC,
444 0xAD678846,
445 0xDA60B8D0,
446 0x44042D73,
447 0x33031DE5,
448 0xAA0A4C5F,
449 0xDD0D7CC9,
450 0x5005713C,
451 0x270241AA,
452 0xBE0B1010,
453 0xC90C2086,
454 0x5768B525,
455 0x206F85B3,
456 0xB966D409,
457 0xCE61E49F,
458 0x5EDEF90E,
459 0x29D9C998,
460 0xB0D09822,
461 0xC7D7A8B4,
462 0x59B33D17,
463 0x2EB40D81,
464 0xB7BD5C3B,
465 0xC0BA6CAD,
466 0xEDB88320,
467 0x9ABFB3B6,
468 0x03B6E20C,
469 0x74B1D29A,
470 0xEAD54739,
471 0x9DD277AF,
472 0x04DB2615,
473 0x73DC1683,
474 0xE3630B12,
475 0x94643B84,
476 0x0D6D6A3E,
477 0x7A6A5AA8,
478 0xE40ECF0B,
479 0x9309FF9D,
480 0x0A00AE27,
481 0x7D079EB1,
482 0xF00F9344,
483 0x8708A3D2,
484 0x1E01F268,
485 0x6906C2FE,
486 0xF762575D,
487 0x806567CB,
488 0x196C3671,
489 0x6E6B06E7,
490 0xFED41B76,
491 0x89D32BE0,
492 0x10DA7A5A,
493 0x67DD4ACC,
494 0xF9B9DF6F,
495 0x8EBEEFF9,
496 0x17B7BE43,
497 0x60B08ED5,
498 0xD6D6A3E8,
499 0xA1D1937E,
500 0x38D8C2C4,
501 0x4FDFF252,
502 0xD1BB67F1,
503 0xA6BC5767,
504 0x3FB506DD,
505 0x48B2364B,
506 0xD80D2BDA,
507 0xAF0A1B4C,
508 0x36034AF6,
509 0x41047A60,
510 0xDF60EFC3,
511 0xA867DF55,
512 0x316E8EEF,
513 0x4669BE79,
514 0xCB61B38C,
515 0xBC66831A,
516 0x256FD2A0,
517 0x5268E236,
518 0xCC0C7795,
519 0xBB0B4703,
520 0x220216B9,
521 0x5505262F,
522 0xC5BA3BBE,
523 0xB2BD0B28,
524 0x2BB45A92,
525 0x5CB36A04,
526 0xC2D7FFA7,
527 0xB5D0CF31,
528 0x2CD99E8B,
529 0x5BDEAE1D,
530 0x9B64C2B0,
531 0xEC63F226,
532 0x756AA39C,
533 0x026D930A,
534 0x9C0906A9,
535 0xEB0E363F,
536 0x72076785,
537 0x05005713,
538 0x95BF4A82,
539 0xE2B87A14,
540 0x7BB12BAE,
541 0x0CB61B38,
542 0x92D28E9B,
543 0xE5D5BE0D,
544 0x7CDCEFB7,
545 0x0BDBDF21,
546 0x86D3D2D4,
547 0xF1D4E242,
548 0x68DDB3F8,
549 0x1FDA836E,
550 0x81BE16CD,
551 0xF6B9265B,
552 0x6FB077E1,
553 0x18B74777,
554 0x88085AE6,
555 0xFF0F6A70,
556 0x66063BCA,
557 0x11010B5C,
558 0x8F659EFF,
559 0xF862AE69,
560 0x616BFFD3,
561 0x166CCF45,
562 0xA00AE278,
563 0xD70DD2EE,
564 0x4E048354,
565 0x3903B3C2,
566 0xA7672661,
567 0xD06016F7,
568 0x4969474D,
569 0x3E6E77DB,
570 0xAED16A4A,
571 0xD9D65ADC,
572 0x40DF0B66,
573 0x37D83BF0,
574 0xA9BCAE53,
575 0xDEBB9EC5,
576 0x47B2CF7F,
577 0x30B5FFE9,
578 0xBDBDF21C,
579 0xCABAC28A,
580 0x53B39330,
581 0x24B4A3A6,
582 0xBAD03605,
583 0xCDD70693,
584 0x54DE5729,
585 0x23D967BF,
586 0xB3667A2E,
587 0xC4614AB8,
588 0x5D681B02,
589 0x2A6F2B94,
590 0xB40BBE37,
591 0xC30C8EA1,
592 0x5A05DF1B,
593 0x2D02EF8D
594 };
595
596 /**
597 Computes and returns a 32-bit CRC for a data buffer.
598 CRC32 value bases on ITU-T V.42.
599
600 If Buffer is NULL, then ASSERT().
601 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
602
603 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed.
604 @param[in] Length The number of bytes in the buffer Data.
605
606 @retval Crc32 The 32-bit CRC was computed for the data buffer.
607
608 **/
609 UINT32
610 EFIAPI
611 CalculateCrc32(
612 IN VOID *Buffer,
613 IN UINTN Length
614 )
615 {
616 UINTN Index;
617 UINT32 Crc;
618 UINT8 *Ptr;
619
620 ASSERT (Buffer != NULL);
621 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
622
623 //
624 // Compute CRC
625 //
626 Crc = 0xffffffff;
627 for (Index = 0, Ptr = Buffer; Index < Length; Index++, Ptr++) {
628 Crc = (Crc >> 8) ^ mCrcTable[(UINT8) Crc ^ *Ptr];
629 }
630
631 return Crc ^ 0xffffffff;
632 }