]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/CheckSum.c
957a2224f924bf0281d1700658c803ce6d46f852
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.
14 Module Name: CheckSum.c
19 Calculate 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 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
= 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().
70 @param Buffer Pointer to the buffer to carry out the checksum operation.
71 @param Length The size, in bytes, of Buffer.
73 @return Checksum The 2's complement checksum of Buffer.
79 IN CONST UINT8
*Buffer
,
85 CheckSum
= CalculateSum8 (Buffer
, Length
);
88 // Return the checksum based on 2's complement.
90 return (UINT8
) (0x100 - CheckSum
);
94 Returns the sum of all elements in a buffer of 16-bit values. During
95 calculation, the carry bits are dropped.
97 This function calculates the sum of the 16-bit values in the buffer
98 specified by Buffer and Length. The carry bits in result of addition are dropped.
99 The 16-bit result is returned. If Length is 0, then 0 is returned.
101 If Buffer is NULL, then ASSERT().
102 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
103 If Length is not aligned on a 16-bit boundary, then ASSERT().
104 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
106 @param Buffer Pointer to the buffer to carry out the sum operation.
107 @param Length The size, in bytes, of Buffer.
109 @return Sum The sum of Buffer with carry bits dropped during additions.
115 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));
128 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
129 Sum
= 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 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 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
,
203 ASSERT (Buffer
!= NULL
);
204 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
205 ASSERT ((Length
& 0x3) == 0);
206 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
209 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
210 Sum
= Sum
+ *(Buffer
+ Count
);
218 Returns the two's complement checksum of all elements in a buffer of
221 This function first calculates the sum of the 32-bit values in the buffer
222 specified by Buffer and Length. The carry bits in the result of addition
223 are dropped. Then, the two's complement of the sum is returned. If Length
224 is 0, then 0 is returned.
226 If Buffer is NULL, then ASSERT().
227 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
228 If Length is not aligned on a 32-bit boundary, then ASSERT().
229 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
231 @param Buffer Pointer to the buffer to carry out the checksum operation.
232 @param Length The size, in bytes, of Buffer.
234 @return Checksum The 2's complement checksum of Buffer.
239 CalculateCheckSum32 (
240 IN CONST UINT32
*Buffer
,
246 CheckSum
= CalculateSum32 (Buffer
, Length
);
249 // Return the checksum based on 2's complement.
251 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
256 Returns the sum of all elements in a buffer of 64-bit values. During
257 calculation, the carry bits are dropped.
259 This function calculates the sum of the 64-bit values in the buffer
260 specified by Buffer and Length. The carry bits in result of addition are dropped.
261 The 64-bit result is returned. If Length is 0, then 0 is returned.
263 If Buffer is NULL, then ASSERT().
264 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
265 If Length is not aligned on a 64-bit boundary, then ASSERT().
266 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
268 @param Buffer Pointer to the buffer to carry out the sum operation.
269 @param Length The size, in bytes, of Buffer.
271 @return Sum The sum of Buffer with carry bits dropped during additions.
277 IN CONST UINT64
*Buffer
,
284 ASSERT (Buffer
!= NULL
);
285 ASSERT (((UINTN
) Buffer
& 0x7) == 0);
286 ASSERT ((Length
& 0x7) == 0);
287 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
289 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
290 Sum
= Sum
+ *(Buffer
+ Count
);
298 Returns the two's complement checksum of all elements in a buffer of
301 This function first calculates the sum of the 64-bit values in the buffer
302 specified by Buffer and Length. The carry bits in the result of addition
303 are dropped. Then, the two's complement of the sum is returned. If Length
304 is 0, then 0 is returned.
306 If Buffer is NULL, then ASSERT().
307 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
308 If Length is not aligned on a 64-bit boundary, then ASSERT().
309 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
311 @param Buffer Pointer to the buffer to carry out the checksum operation.
312 @param Length The size, in bytes, of Buffer.
314 @return Checksum The 2's complement checksum of Buffer.
319 CalculateCheckSum64 (
320 IN CONST UINT64
*Buffer
,
326 CheckSum
= CalculateSum64 (Buffer
, Length
);
329 // Return the checksum based on 2's complement.
331 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);