]>
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 <BaseLibInternals.h>
21 Calculate the sum of all elements in a buffer in unit of UINT8.
22 During calculation, the carry bits are dropped.
24 This function calculates the sum of all elements in a buffer
25 in unit of UINT8. The carry bits in result of addition are dropped.
26 The result is returned as UINT8. If Length is Zero, then Zero is
29 If Buffer is NULL, then ASSERT().
30 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
32 @param Buffer Pointer to the buffer to carry out the sum operation.
33 @param Length The size, in bytes, of Buffer .
35 @return Sum The sum of Buffer with carry bits dropped during additions.
41 IN CONST UINT8
*Buffer
,
48 ASSERT (Buffer
!= NULL
);
49 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
51 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
52 Sum
= (UINT8
) (Sum
+ *(Buffer
+ Count
));
60 Returns the two's complement checksum of all elements in a buffer
63 This function first calculates the sum of the 8-bit values in the
64 buffer specified by Buffer and Length. The carry bits in the result
65 of addition are dropped. Then, the two's complement of the sum is
66 returned. If Length is 0, then 0 is returned.
68 If Buffer is NULL, then ASSERT().
69 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
71 @param Buffer Pointer to the buffer to carry out the checksum operation.
72 @param Length The size, in bytes, of Buffer.
74 @return Checksum The 2's complement checksum of Buffer.
80 IN CONST UINT8
*Buffer
,
86 CheckSum
= CalculateSum8 (Buffer
, Length
);
89 // Return the checksum based on 2's complement.
91 return (UINT8
) (0x100 - CheckSum
);
95 Returns the sum of all elements in a buffer of 16-bit values. During
96 calculation, the carry bits are dropped.
98 This function calculates the sum of the 16-bit values in the buffer
99 specified by Buffer and Length. The carry bits in result of addition are dropped.
100 The 16-bit result is returned. If Length is 0, then 0 is returned.
102 If Buffer is NULL, then ASSERT().
103 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
104 If Length is not aligned on a 16-bit boundary, then ASSERT().
105 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
107 @param Buffer Pointer to the buffer to carry out the sum operation.
108 @param Length The size, in bytes, of Buffer.
110 @return Sum The sum of Buffer with carry bits dropped during additions.
116 IN CONST UINT16
*Buffer
,
124 ASSERT (Buffer
!= NULL
);
125 ASSERT (((UINTN
) Buffer
& 0x1) == 0);
126 ASSERT ((Length
& 0x1) == 0);
127 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
129 Total
= Length
/ sizeof (*Buffer
);
130 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
131 Sum
= (UINT16
) (Sum
+ *(Buffer
+ Count
));
139 Returns the two's complement checksum of all elements in a buffer of
142 This function first calculates the sum of the 16-bit values in the buffer
143 specified by Buffer and Length. The carry bits in the result of addition
144 are dropped. Then, the two's complement of the sum is returned. If Length
145 is 0, then 0 is returned.
147 If Buffer is NULL, then ASSERT().
148 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
149 If Length is not aligned on a 16-bit boundary, then ASSERT().
150 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
152 @param Buffer Pointer to the buffer to carry out the checksum operation.
153 @param Length The size, in bytes, of Buffer.
155 @return Checksum The 2's complement checksum of Buffer.
160 CalculateCheckSum16 (
161 IN CONST UINT16
*Buffer
,
167 CheckSum
= CalculateSum16 (Buffer
, Length
);
170 // Return the checksum based on 2's complement.
172 return (UINT16
) (0x10000 - CheckSum
);
177 Returns the sum of all elements in a buffer of 32-bit values. During
178 calculation, the carry bits are dropped.
180 This function calculates the sum of the 32-bit values in the buffer
181 specified by Buffer and Length. The carry bits in result of addition are dropped.
182 The 32-bit result is returned. If Length is 0, then 0 is returned.
184 If Buffer is NULL, then ASSERT().
185 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
186 If Length is not aligned on a 32-bit boundary, then ASSERT().
187 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
189 @param Buffer Pointer to the buffer to carry out the sum operation.
190 @param Length The size, in bytes, of Buffer.
192 @return Sum The sum of Buffer with carry bits dropped during additions.
198 IN CONST UINT32
*Buffer
,
206 ASSERT (Buffer
!= NULL
);
207 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
208 ASSERT ((Length
& 0x3) == 0);
209 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
211 Total
= Length
/ sizeof (*Buffer
);
212 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
213 Sum
= Sum
+ *(Buffer
+ Count
);
221 Returns the two's complement checksum of all elements in a buffer of
224 This function first calculates the sum of the 32-bit values in the buffer
225 specified by Buffer and Length. The carry bits in the result of addition
226 are dropped. Then, the two's complement of the sum is returned. If Length
227 is 0, then 0 is returned.
229 If Buffer is NULL, then ASSERT().
230 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
231 If Length is not aligned on a 32-bit boundary, then ASSERT().
232 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
234 @param Buffer Pointer to the buffer to carry out the checksum operation.
235 @param Length The size, in bytes, of Buffer.
237 @return Checksum The 2's complement checksum of Buffer.
242 CalculateCheckSum32 (
243 IN CONST UINT32
*Buffer
,
249 CheckSum
= CalculateSum32 (Buffer
, Length
);
252 // Return the checksum based on 2's complement.
254 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
259 Returns the sum of all elements in a buffer of 64-bit values. During
260 calculation, the carry bits are dropped.
262 This function calculates the sum of the 64-bit values in the buffer
263 specified by Buffer and Length. The carry bits in result of addition are dropped.
264 The 64-bit result is returned. If Length is 0, then 0 is returned.
266 If Buffer is NULL, then ASSERT().
267 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
268 If Length is not aligned on a 64-bit boundary, then ASSERT().
269 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
271 @param Buffer Pointer to the buffer to carry out the sum operation.
272 @param Length The size, in bytes, of Buffer.
274 @return Sum The sum of Buffer with carry bits dropped during additions.
280 IN CONST UINT64
*Buffer
,
288 ASSERT (Buffer
!= NULL
);
289 ASSERT (((UINTN
) Buffer
& 0x7) == 0);
290 ASSERT ((Length
& 0x7) == 0);
291 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
293 Total
= Length
/ sizeof (*Buffer
);
294 for (Sum
= 0, Count
= 0; Count
< Total
; Count
++) {
295 Sum
= Sum
+ *(Buffer
+ Count
);
303 Returns the two's complement checksum of all elements in a buffer of
306 This function first calculates the sum of the 64-bit values in the buffer
307 specified by Buffer and Length. The carry bits in the result of addition
308 are dropped. Then, the two's complement of the sum is returned. If Length
309 is 0, then 0 is returned.
311 If Buffer is NULL, then ASSERT().
312 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
313 If Length is not aligned on a 64-bit boundary, then ASSERT().
314 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
316 @param Buffer Pointer to the buffer to carry out the checksum operation.
317 @param Length The size, in bytes, of Buffer.
319 @return Checksum The 2's complement checksum of Buffer.
324 CalculateCheckSum64 (
325 IN CONST UINT64
*Buffer
,
331 CheckSum
= CalculateSum64 (Buffer
, Length
);
334 // Return the checksum based on 2's complement.
336 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);