]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/CheckSum.c
2 Utility functions to generate checksum based on 2's complement
5 Copyright (c) 2007, Intel Corporation<BR>
6 All rights reserved. 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.
17 // Include common header file for this module.
19 #include <BaseLibInternals.h>
23 Calculate the sum of all elements in a buffer in unit of UINT8.
24 During calculation, the carry bits are dropped.
26 This function calculates the sum of all elements in a buffer
27 in unit of UINT8. The carry bits in result of addition are dropped.
28 The result is returned as UINT8. If Length is Zero, then Zero is
31 If Buffer is NULL, then ASSERT().
32 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
34 @param Buffer Pointer to the buffer to carry out the sum operation.
35 @param Length The size, in bytes, of Buffer .
37 @return Sum The sum of Buffer with carry bits dropped during additions.
43 IN CONST UINT8
*Buffer
,
50 ASSERT (Buffer
!= NULL
);
51 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
53 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
54 Sum
= (UINT8
) (Sum
+ *(Buffer
+ Count
));
62 Returns the two's complement checksum of all elements in a buffer
65 This function first calculates the sum of the 8-bit values in the
66 buffer specified by Buffer and Length. The carry bits in the result
67 of addition are dropped. Then, the two's complement of the sum is
68 returned. If Length is 0, then 0 is returned.
70 If Buffer is NULL, then ASSERT().
71 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
74 @param Buffer Pointer to the buffer to carry out the checksum operation.
75 @param Length The size, in bytes, of Buffer.
77 @return Checksum The 2's complement checksum of Buffer.
83 IN CONST UINT8
*Buffer
,
89 CheckSum
= CalculateSum8 (Buffer
, Length
);
92 // Return the checksum based on 2's complement.
94 return (UINT8
) (0x100 - CheckSum
);
98 Returns the sum of all elements in a buffer of 16-bit values. During
99 calculation, the carry bits are dropped.
101 This function calculates the sum of the 16-bit values in the buffer
102 specified by Buffer and Length. The carry bits in result of addition are dropped.
103 The 16-bit result is returned. If Length is 0, then 0 is returned.
105 If Buffer is NULL, then ASSERT().
106 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
107 If Length is not aligned on a 16-bit boundary, then ASSERT().
108 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
110 @param Buffer Pointer to the buffer to carry out the sum operation.
111 @param Length The size, in bytes, of Buffer.
113 @return Sum The sum of Buffer with carry bits dropped during additions.
119 IN CONST UINT16
*Buffer
,
127 ASSERT (Buffer
!= NULL
);
128 ASSERT (((UINTN
) Buffer
& 0x1) == 0);
129 ASSERT ((Length
& 0x1) == 0);
130 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
132 Total
= Length
/ sizeof (*Buffer
);
133 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
134 Sum
= (UINT16
) (Sum
+ *(Buffer
+ Count
));
142 Returns the two's complement checksum of all elements in a buffer of
145 This function first calculates the sum of the 16-bit values in the buffer
146 specified by Buffer and Length. The carry bits in the result of addition
147 are dropped. Then, the two's complement of the sum is returned. If Length
148 is 0, then 0 is returned.
150 If Buffer is NULL, then ASSERT().
151 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
152 If Length is not aligned on a 16-bit boundary, then ASSERT().
153 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
155 @param Buffer Pointer to the buffer to carry out the checksum operation.
156 @param Length The size, in bytes, of Buffer.
158 @return Checksum The 2's complement checksum of Buffer.
163 CalculateCheckSum16 (
164 IN CONST UINT16
*Buffer
,
170 CheckSum
= CalculateSum16 (Buffer
, Length
);
173 // Return the checksum based on 2's complement.
175 return (UINT16
) (0x10000 - CheckSum
);
180 Returns the sum of all elements in a buffer of 32-bit values. During
181 calculation, the carry bits are dropped.
183 This function calculates the sum of the 32-bit values in the buffer
184 specified by Buffer and Length. The carry bits in result of addition are dropped.
185 The 32-bit result is returned. If Length is 0, then 0 is returned.
187 If Buffer is NULL, then ASSERT().
188 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
189 If Length is not aligned on a 32-bit boundary, then ASSERT().
190 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
192 @param Buffer Pointer to the buffer to carry out the sum operation.
193 @param Length The size, in bytes, of Buffer.
195 @return Sum The sum of Buffer with carry bits dropped during additions.
201 IN CONST UINT32
*Buffer
,
209 ASSERT (Buffer
!= NULL
);
210 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
211 ASSERT ((Length
& 0x3) == 0);
212 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
214 Total
= Length
/ sizeof (*Buffer
);
215 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
216 Sum
= Sum
+ *(Buffer
+ Count
);
224 Returns the two's complement checksum of all elements in a buffer of
227 This function first calculates the sum of the 32-bit values in the buffer
228 specified by Buffer and Length. The carry bits in the result of addition
229 are dropped. Then, the two's complement of the sum is returned. If Length
230 is 0, then 0 is returned.
232 If Buffer is NULL, then ASSERT().
233 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
234 If Length is not aligned on a 32-bit boundary, then ASSERT().
235 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
237 @param Buffer Pointer to the buffer to carry out the checksum operation.
238 @param Length The size, in bytes, of Buffer.
240 @return Checksum The 2's complement checksum of Buffer.
245 CalculateCheckSum32 (
246 IN CONST UINT32
*Buffer
,
252 CheckSum
= CalculateSum32 (Buffer
, Length
);
255 // Return the checksum based on 2's complement.
257 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
262 Returns the sum of all elements in a buffer of 64-bit values. During
263 calculation, the carry bits are dropped.
265 This function calculates the sum of the 64-bit values in the buffer
266 specified by Buffer and Length. The carry bits in result of addition are dropped.
267 The 64-bit result is returned. If Length is 0, then 0 is returned.
269 If Buffer is NULL, then ASSERT().
270 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
271 If Length is not aligned on a 64-bit boundary, then ASSERT().
272 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
274 @param Buffer Pointer to the buffer to carry out the sum operation.
275 @param Length The size, in bytes, of Buffer.
277 @return Sum The sum of Buffer with carry bits dropped during additions.
283 IN CONST UINT64
*Buffer
,
291 ASSERT (Buffer
!= NULL
);
292 ASSERT (((UINTN
) Buffer
& 0x7) == 0);
293 ASSERT ((Length
& 0x7) == 0);
294 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
296 Total
= Length
/ sizeof (*Buffer
);
297 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
298 Sum
= Sum
+ *(Buffer
+ Count
);
306 Returns the two's complement checksum of all elements in a buffer of
309 This function first calculates the sum of the 64-bit values in the buffer
310 specified by Buffer and Length. The carry bits in the result of addition
311 are dropped. Then, the two's complement of the sum is returned. If Length
312 is 0, then 0 is returned.
314 If Buffer is NULL, then ASSERT().
315 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
316 If Length is not aligned on a 64-bit boundary, then ASSERT().
317 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
319 @param Buffer Pointer to the buffer to carry out the checksum operation.
320 @param Length The size, in bytes, of Buffer.
322 @return Checksum The 2's complement checksum of Buffer.
327 CalculateCheckSum64 (
328 IN CONST UINT64
*Buffer
,
334 CheckSum
= CalculateSum64 (Buffer
, Length
);
337 // Return the checksum based on 2's complement.
339 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);