2 Utility functions to generate checksum based on 2's complement
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "BaseLibInternals.h"
13 Returns the sum of all elements in a buffer in unit of UINT8.
14 During calculation, the carry bits are dropped.
16 This function calculates the sum of all elements in a buffer
17 in unit of UINT8. The carry bits in result of addition are dropped.
18 The result is returned as UINT8. If Length is Zero, then Zero is
21 If Buffer is NULL, then ASSERT().
22 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
24 @param Buffer The pointer to the buffer to carry out the sum operation.
25 @param Length The size, in bytes, of Buffer.
27 @return Sum The sum of Buffer with carry bits dropped during additions.
33 IN CONST UINT8
*Buffer
,
40 ASSERT (Buffer
!= NULL
);
41 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
43 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
44 Sum
= (UINT8
) (Sum
+ *(Buffer
+ Count
));
52 Returns the two's complement checksum of all elements in a buffer
55 This function first calculates the sum of the 8-bit values in the
56 buffer specified by Buffer and Length. The carry bits in the result
57 of addition are dropped. Then, the two's complement of the sum is
58 returned. If Length is 0, then 0 is returned.
60 If Buffer is NULL, then ASSERT().
61 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
63 @param Buffer The pointer to the buffer to carry out the checksum operation.
64 @param Length The size, in bytes, of Buffer.
66 @return Checksum The 2's complement checksum of Buffer.
72 IN CONST UINT8
*Buffer
,
78 CheckSum
= CalculateSum8 (Buffer
, Length
);
81 // Return the checksum based on 2's complement.
83 return (UINT8
) (0x100 - CheckSum
);
87 Returns the sum of all elements in a buffer of 16-bit values. During
88 calculation, the carry bits are dropped.
90 This function calculates the sum of the 16-bit values in the buffer
91 specified by Buffer and Length. The carry bits in result of addition are dropped.
92 The 16-bit result is returned. If Length is 0, then 0 is returned.
94 If Buffer is NULL, then ASSERT().
95 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
96 If Length is not aligned on a 16-bit boundary, then ASSERT().
97 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
99 @param Buffer The pointer to the buffer to carry out the sum operation.
100 @param Length The size, in bytes, of Buffer.
102 @return Sum The sum of Buffer with carry bits dropped during additions.
108 IN CONST UINT16
*Buffer
,
116 ASSERT (Buffer
!= NULL
);
117 ASSERT (((UINTN
) Buffer
& 0x1) == 0);
118 ASSERT ((Length
& 0x1) == 0);
119 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
121 Total
= Length
/ sizeof (*Buffer
);
122 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
123 Sum
= (UINT16
) (Sum
+ *(Buffer
+ Count
));
131 Returns the two's complement checksum of all elements in a buffer of
134 This function first calculates the sum of the 16-bit values in the buffer
135 specified by Buffer and Length. The carry bits in the result of addition
136 are dropped. Then, the two's complement of the sum is returned. If Length
137 is 0, then 0 is returned.
139 If Buffer is NULL, then ASSERT().
140 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
141 If Length is not aligned on a 16-bit boundary, then ASSERT().
142 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
144 @param Buffer The pointer to the buffer to carry out the checksum operation.
145 @param Length The size, in bytes, of Buffer.
147 @return Checksum The 2's complement checksum of Buffer.
152 CalculateCheckSum16 (
153 IN CONST UINT16
*Buffer
,
159 CheckSum
= CalculateSum16 (Buffer
, Length
);
162 // Return the checksum based on 2's complement.
164 return (UINT16
) (0x10000 - CheckSum
);
169 Returns the sum of all elements in a buffer of 32-bit values. During
170 calculation, the carry bits are dropped.
172 This function calculates the sum of the 32-bit values in the buffer
173 specified by Buffer and Length. The carry bits in result of addition are dropped.
174 The 32-bit result is returned. If Length is 0, then 0 is returned.
176 If Buffer is NULL, then ASSERT().
177 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
178 If Length is not aligned on a 32-bit boundary, then ASSERT().
179 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
181 @param Buffer The pointer to the buffer to carry out the sum operation.
182 @param Length The size, in bytes, of Buffer.
184 @return Sum The sum of Buffer with carry bits dropped during additions.
190 IN CONST UINT32
*Buffer
,
198 ASSERT (Buffer
!= NULL
);
199 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
200 ASSERT ((Length
& 0x3) == 0);
201 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
203 Total
= Length
/ sizeof (*Buffer
);
204 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
205 Sum
= Sum
+ *(Buffer
+ Count
);
213 Returns the two's complement checksum of all elements in a buffer of
216 This function first calculates the sum of the 32-bit values in the buffer
217 specified by Buffer and Length. The carry bits in the result of addition
218 are dropped. Then, the two's complement of the sum is returned. If Length
219 is 0, then 0 is returned.
221 If Buffer is NULL, then ASSERT().
222 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
223 If Length is not aligned on a 32-bit boundary, then ASSERT().
224 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
226 @param Buffer The pointer to the buffer to carry out the checksum operation.
227 @param Length The size, in bytes, of Buffer.
229 @return Checksum The 2's complement checksum of Buffer.
234 CalculateCheckSum32 (
235 IN CONST UINT32
*Buffer
,
241 CheckSum
= CalculateSum32 (Buffer
, Length
);
244 // Return the checksum based on 2's complement.
246 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
251 Returns the sum of all elements in a buffer of 64-bit values. During
252 calculation, the carry bits are dropped.
254 This function calculates the sum of the 64-bit values in the buffer
255 specified by Buffer and Length. The carry bits in result of addition are dropped.
256 The 64-bit result is returned. If Length is 0, then 0 is returned.
258 If Buffer is NULL, then ASSERT().
259 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
260 If Length is not aligned on a 64-bit boundary, then ASSERT().
261 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
263 @param Buffer The pointer to the buffer to carry out the sum operation.
264 @param Length The size, in bytes, of Buffer.
266 @return Sum The sum of Buffer with carry bits dropped during additions.
272 IN CONST UINT64
*Buffer
,
280 ASSERT (Buffer
!= NULL
);
281 ASSERT (((UINTN
) Buffer
& 0x7) == 0);
282 ASSERT ((Length
& 0x7) == 0);
283 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
285 Total
= Length
/ sizeof (*Buffer
);
286 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
287 Sum
= Sum
+ *(Buffer
+ Count
);
295 Returns the two's complement checksum of all elements in a buffer of
298 This function first calculates the sum of the 64-bit values in the buffer
299 specified by Buffer and Length. The carry bits in the result of addition
300 are dropped. Then, the two's complement of the sum is returned. If Length
301 is 0, then 0 is returned.
303 If Buffer is NULL, then ASSERT().
304 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
305 If Length is not aligned on a 64-bit boundary, then ASSERT().
306 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
308 @param Buffer The pointer to the buffer to carry out the checksum operation.
309 @param Length The size, in bytes, of Buffer.
311 @return Checksum The 2's complement checksum of Buffer.
316 CalculateCheckSum64 (
317 IN CONST UINT64
*Buffer
,
323 CheckSum
= CalculateSum64 (Buffer
, Length
);
326 // Return the checksum based on 2's complement.
328 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);
331 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT32 mCrcTable
[256] = {
591 Computes and returns a 32-bit CRC for a data buffer.
592 CRC32 value bases on ITU-T V.42.
594 If Buffer is NULL, then ASSERT().
595 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
597 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed.
598 @param[in] Length The number of bytes in the buffer Data.
600 @retval Crc32 The 32-bit CRC was computed for the data buffer.
614 ASSERT (Buffer
!= NULL
);
615 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
621 for (Index
= 0, Ptr
= Buffer
; Index
< Length
; Index
++, Ptr
++) {
622 Crc
= (Crc
>> 8) ^ mCrcTable
[(UINT8
) Crc
^ *Ptr
];
625 return Crc
^ 0xffffffff;