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
));
51 Returns the two's complement checksum of all elements in a buffer
54 This function first calculates the sum of the 8-bit values in the
55 buffer specified by Buffer and Length. The carry bits in the result
56 of addition are dropped. Then, the two's complement of the sum is
57 returned. If Length is 0, then 0 is returned.
59 If Buffer is NULL, then ASSERT().
60 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
62 @param Buffer The pointer to the buffer to carry out the checksum operation.
63 @param Length The size, in bytes, of Buffer.
65 @return Checksum The 2's complement checksum of Buffer.
71 IN CONST UINT8
*Buffer
,
77 CheckSum
= CalculateSum8 (Buffer
, Length
);
80 // Return the checksum based on 2's complement.
82 return (UINT8
)(0x100 - CheckSum
);
86 Returns the sum of all elements in a buffer of 16-bit values. During
87 calculation, the carry bits are dropped.
89 This function calculates the sum of the 16-bit values in the buffer
90 specified by Buffer and Length. The carry bits in result of addition are dropped.
91 The 16-bit result is returned. If Length is 0, then 0 is returned.
93 If Buffer is NULL, then ASSERT().
94 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
95 If Length is not aligned on a 16-bit boundary, then ASSERT().
96 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
98 @param Buffer The pointer to the buffer to carry out the sum operation.
99 @param Length The size, in bytes, of Buffer.
101 @return Sum The sum of Buffer with carry bits dropped during additions.
107 IN CONST UINT16
*Buffer
,
115 ASSERT (Buffer
!= NULL
);
116 ASSERT (((UINTN
)Buffer
& 0x1) == 0);
117 ASSERT ((Length
& 0x1) == 0);
118 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
)Buffer
) + 1));
120 Total
= Length
/ sizeof (*Buffer
);
121 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
122 Sum
= (UINT16
)(Sum
+ *(Buffer
+ Count
));
129 Returns the two's complement checksum of all elements in a buffer of
132 This function first calculates the sum of the 16-bit values in the buffer
133 specified by Buffer and Length. The carry bits in the result of addition
134 are dropped. Then, the two's complement of the sum is returned. If Length
135 is 0, then 0 is returned.
137 If Buffer is NULL, then ASSERT().
138 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
139 If Length is not aligned on a 16-bit boundary, then ASSERT().
140 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
142 @param Buffer The pointer to the buffer to carry out the checksum operation.
143 @param Length The size, in bytes, of Buffer.
145 @return Checksum The 2's complement checksum of Buffer.
150 CalculateCheckSum16 (
151 IN CONST UINT16
*Buffer
,
157 CheckSum
= CalculateSum16 (Buffer
, Length
);
160 // Return the checksum based on 2's complement.
162 return (UINT16
)(0x10000 - CheckSum
);
166 Returns the sum of all elements in a buffer of 32-bit values. During
167 calculation, the carry bits are dropped.
169 This function calculates the sum of the 32-bit values in the buffer
170 specified by Buffer and Length. The carry bits in result of addition are dropped.
171 The 32-bit result is returned. If Length is 0, then 0 is returned.
173 If Buffer is NULL, then ASSERT().
174 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
175 If Length is not aligned on a 32-bit boundary, then ASSERT().
176 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
178 @param Buffer The pointer to the buffer to carry out the sum operation.
179 @param Length The size, in bytes, of Buffer.
181 @return Sum The sum of Buffer with carry bits dropped during additions.
187 IN CONST UINT32
*Buffer
,
195 ASSERT (Buffer
!= NULL
);
196 ASSERT (((UINTN
)Buffer
& 0x3) == 0);
197 ASSERT ((Length
& 0x3) == 0);
198 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
)Buffer
) + 1));
200 Total
= Length
/ sizeof (*Buffer
);
201 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
202 Sum
= Sum
+ *(Buffer
+ Count
);
209 Returns the two's complement checksum of all elements in a buffer of
212 This function first calculates the sum of the 32-bit values in the buffer
213 specified by Buffer and Length. The carry bits in the result of addition
214 are dropped. Then, the two's complement of the sum is returned. If Length
215 is 0, then 0 is returned.
217 If Buffer is NULL, then ASSERT().
218 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
219 If Length is not aligned on a 32-bit boundary, then ASSERT().
220 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
222 @param Buffer The pointer to the buffer to carry out the checksum operation.
223 @param Length The size, in bytes, of Buffer.
225 @return Checksum The 2's complement checksum of Buffer.
230 CalculateCheckSum32 (
231 IN CONST UINT32
*Buffer
,
237 CheckSum
= CalculateSum32 (Buffer
, Length
);
240 // Return the checksum based on 2's complement.
242 return (UINT32
)((UINT32
)(-1) - CheckSum
+ 1);
246 Returns the sum of all elements in a buffer of 64-bit values. During
247 calculation, the carry bits are dropped.
249 This function calculates the sum of the 64-bit values in the buffer
250 specified by Buffer and Length. The carry bits in result of addition are dropped.
251 The 64-bit result is returned. If Length is 0, then 0 is returned.
253 If Buffer is NULL, then ASSERT().
254 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
255 If Length is not aligned on a 64-bit boundary, then ASSERT().
256 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
258 @param Buffer The pointer to the buffer to carry out the sum operation.
259 @param Length The size, in bytes, of Buffer.
261 @return Sum The sum of Buffer with carry bits dropped during additions.
267 IN CONST UINT64
*Buffer
,
275 ASSERT (Buffer
!= NULL
);
276 ASSERT (((UINTN
)Buffer
& 0x7) == 0);
277 ASSERT ((Length
& 0x7) == 0);
278 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
)Buffer
) + 1));
280 Total
= Length
/ sizeof (*Buffer
);
281 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
282 Sum
= Sum
+ *(Buffer
+ Count
);
289 Returns the two's complement checksum of all elements in a buffer of
292 This function first calculates the sum of the 64-bit values in the buffer
293 specified by Buffer and Length. The carry bits in the result of addition
294 are dropped. Then, the two's complement of the sum is returned. If Length
295 is 0, then 0 is returned.
297 If Buffer is NULL, then ASSERT().
298 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
299 If Length is not aligned on a 64-bit boundary, then ASSERT().
300 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
302 @param Buffer The pointer to the buffer to carry out the checksum operation.
303 @param Length The size, in bytes, of Buffer.
305 @return Checksum The 2's complement checksum of Buffer.
310 CalculateCheckSum64 (
311 IN CONST UINT64
*Buffer
,
317 CheckSum
= CalculateSum64 (Buffer
, Length
);
320 // Return the checksum based on 2's complement.
322 return (UINT64
)((UINT64
)(-1) - CheckSum
+ 1);
325 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT32 mCrcTable
[256] = {
585 Computes and returns a 32-bit CRC for a data buffer.
586 CRC32 value bases on ITU-T V.42.
588 If Buffer is NULL, then ASSERT().
589 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
591 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed.
592 @param[in] Length The number of bytes in the buffer Data.
594 @retval Crc32 The 32-bit CRC was computed for the data buffer.
608 ASSERT (Buffer
!= NULL
);
609 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
)Buffer
) + 1));
615 for (Index
= 0, Ptr
= Buffer
; Index
< Length
; Index
++, Ptr
++) {
616 Crc
= (Crc
>> 8) ^ mCrcTable
[(UINT8
)Crc
^ *Ptr
];
619 return Crc
^ 0xffffffff;