]>
git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Library/UnixBaseLib/CheckSum.c
2 Utility functions to generate checksum based on 2's complement
5 Copyright (c) 2007 - 2010, 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.
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.
16 #include "BaseLibInternals.h"
19 Returns the sum of all elements in a buffer in unit of UINT8.
20 During calculation, the carry bits are dropped.
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
27 If Buffer is NULL, then ASSERT().
28 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
30 @param Buffer The pointer to the buffer to carry out the sum operation.
31 @param Length The size, in bytes, of Buffer.
33 @return Sum The sum of Buffer with carry bits dropped during additions.
39 IN CONST UINT8
*Buffer
,
46 ASSERT (Buffer
!= NULL
);
47 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
49 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
50 Sum
= (UINT8
) (Sum
+ *(Buffer
+ Count
));
58 Returns the two's complement checksum of all elements in a buffer
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.
66 If Buffer is NULL, then ASSERT().
67 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
69 @param Buffer The pointer to the buffer to carry out the checksum operation.
70 @param Length The size, in bytes, of Buffer.
72 @return Checksum The 2's complement checksum of Buffer.
78 IN CONST UINT8
*Buffer
,
84 CheckSum
= CalculateSum8 (Buffer
, Length
);
87 // Return the checksum based on 2's complement.
89 return (UINT8
) (0x100 - CheckSum
);
93 Returns the sum of all elements in a buffer of 16-bit values. During
94 calculation, the carry bits are dropped.
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.
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().
105 @param Buffer The pointer to the buffer to carry out the sum operation.
106 @param Length The size, in bytes, of Buffer.
108 @return Sum The sum of Buffer with carry bits dropped during additions.
114 IN CONST UINT16
*Buffer
,
122 ASSERT (Buffer
!= NULL
);
123 ASSERT (((UINTN
) Buffer
& 0x1) == 0);
124 ASSERT ((Length
& 0x1) == 0);
125 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
127 Total
= Length
/ sizeof (*Buffer
);
128 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
129 Sum
= (UINT16
) (Sum
+ *(Buffer
+ Count
));
137 Returns the two's complement checksum of all elements in a buffer of
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.
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().
150 @param Buffer The pointer to the buffer to carry out the checksum operation.
151 @param Length The size, in bytes, of Buffer.
153 @return Checksum The 2's complement checksum of Buffer.
158 CalculateCheckSum16 (
159 IN CONST UINT16
*Buffer
,
165 CheckSum
= CalculateSum16 (Buffer
, Length
);
168 // Return the checksum based on 2's complement.
170 return (UINT16
) (0x10000 - CheckSum
);
175 Returns the sum of all elements in a buffer of 32-bit values. During
176 calculation, the carry bits are dropped.
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.
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().
187 @param Buffer The pointer to the buffer to carry out the sum operation.
188 @param Length The size, in bytes, of Buffer.
190 @return Sum The sum of Buffer with carry bits dropped during additions.
196 IN CONST UINT32
*Buffer
,
204 ASSERT (Buffer
!= NULL
);
205 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
206 ASSERT ((Length
& 0x3) == 0);
207 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
209 Total
= Length
/ sizeof (*Buffer
);
210 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
211 Sum
= Sum
+ *(Buffer
+ Count
);
219 Returns the two's complement checksum of all elements in a buffer of
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.
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().
232 @param Buffer The pointer to the buffer to carry out the checksum operation.
233 @param Length The size, in bytes, of Buffer.
235 @return Checksum The 2's complement checksum of Buffer.
240 CalculateCheckSum32 (
241 IN CONST UINT32
*Buffer
,
247 CheckSum
= CalculateSum32 (Buffer
, Length
);
250 // Return the checksum based on 2's complement.
252 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
257 Returns the sum of all elements in a buffer of 64-bit values. During
258 calculation, the carry bits are dropped.
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.
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().
269 @param Buffer The pointer to the buffer to carry out the sum operation.
270 @param Length The size, in bytes, of Buffer.
272 @return Sum The sum of Buffer with carry bits dropped during additions.
278 IN CONST UINT64
*Buffer
,
286 ASSERT (Buffer
!= NULL
);
287 ASSERT (((UINTN
) Buffer
& 0x7) == 0);
288 ASSERT ((Length
& 0x7) == 0);
289 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
291 Total
= Length
/ sizeof (*Buffer
);
292 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
293 Sum
= Sum
+ *(Buffer
+ Count
);
301 Returns the two's complement checksum of all elements in a buffer of
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.
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().
314 @param Buffer The pointer to the buffer to carry out the checksum operation.
315 @param Length The size, in bytes, of Buffer.
317 @return Checksum The 2's complement checksum of Buffer.
322 CalculateCheckSum64 (
323 IN CONST UINT64
*Buffer
,
329 CheckSum
= CalculateSum64 (Buffer
, Length
);
332 // Return the checksum based on 2's complement.
334 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);