]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/CheckSum.c
2 Utility functions to generate checksum based on 2's complement
5 Copyright (c) 2007, Intel Corporation
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
18 #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().
72 @param Buffer Pointer to the buffer to carry out the checksum operation.
73 @param Length The size, in bytes, of Buffer.
75 @return Checksum The 2's complement checksum of Buffer.
81 IN CONST UINT8
*Buffer
,
87 CheckSum
= CalculateSum8 (Buffer
, Length
);
90 // Return the checksum based on 2's complement.
92 return (UINT8
) (0x100 - CheckSum
);
96 Returns the sum of all elements in a buffer of 16-bit values. During
97 calculation, the carry bits are dropped.
99 This function calculates the sum of the 16-bit values in the buffer
100 specified by Buffer and Length. The carry bits in result of addition are dropped.
101 The 16-bit result is returned. If Length is 0, then 0 is returned.
103 If Buffer is NULL, then ASSERT().
104 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
105 If Length is not aligned on a 16-bit boundary, then ASSERT().
106 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
108 @param Buffer Pointer to the buffer to carry out the sum operation.
109 @param Length The size, in bytes, of Buffer.
111 @return Sum The sum of Buffer with carry bits dropped during additions.
117 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));
130 for (Sum
= 0, Count
= 0; Count
< Length
; 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
,
205 ASSERT (Buffer
!= NULL
);
206 ASSERT (((UINTN
) Buffer
& 0x3) == 0);
207 ASSERT ((Length
& 0x3) == 0);
208 ASSERT (Length
<= (MAX_ADDRESS
- ((UINTN
) Buffer
) + 1));
211 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
212 Sum
= Sum
+ *(Buffer
+ Count
);
220 Returns the two's complement checksum of all elements in a buffer of
223 This function first calculates the sum of the 32-bit values in the buffer
224 specified by Buffer and Length. The carry bits in the result of addition
225 are dropped. Then, the two's complement of the sum is returned. If Length
226 is 0, then 0 is returned.
228 If Buffer is NULL, then ASSERT().
229 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
230 If Length is not aligned on a 32-bit boundary, then ASSERT().
231 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
233 @param Buffer Pointer to the buffer to carry out the checksum operation.
234 @param Length The size, in bytes, of Buffer.
236 @return Checksum The 2's complement checksum of Buffer.
241 CalculateCheckSum32 (
242 IN CONST UINT32
*Buffer
,
248 CheckSum
= CalculateSum32 (Buffer
, Length
);
251 // Return the checksum based on 2's complement.
253 return (UINT32
) ((UINT32
)(-1) - CheckSum
+ 1);
258 Returns the sum of all elements in a buffer of 64-bit values. During
259 calculation, the carry bits are dropped.
261 This function calculates the sum of the 64-bit values in the buffer
262 specified by Buffer and Length. The carry bits in result of addition are dropped.
263 The 64-bit result is returned. If Length is 0, then 0 is returned.
265 If Buffer is NULL, then ASSERT().
266 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
267 If Length is not aligned on a 64-bit boundary, then ASSERT().
268 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
270 @param Buffer Pointer to the buffer to carry out the sum operation.
271 @param Length The size, in bytes, of Buffer.
273 @return Sum The sum of Buffer with carry bits dropped during additions.
279 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 for (Sum
= 0, Count
= 0; Count
< Length
; Count
++) {
292 Sum
= Sum
+ *(Buffer
+ Count
);
300 Returns the two's complement checksum of all elements in a buffer of
303 This function first calculates the sum of the 64-bit values in the buffer
304 specified by Buffer and Length. The carry bits in the result of addition
305 are dropped. Then, the two's complement of the sum is returned. If Length
306 is 0, then 0 is returned.
308 If Buffer is NULL, then ASSERT().
309 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
310 If Length is not aligned on a 64-bit boundary, then ASSERT().
311 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
313 @param Buffer Pointer to the buffer to carry out the checksum operation.
314 @param Length The size, in bytes, of Buffer.
316 @return Checksum The 2's complement checksum of Buffer.
321 CalculateCheckSum64 (
322 IN CONST UINT64
*Buffer
,
328 CheckSum
= CalculateSum64 (Buffer
, Length
);
331 // Return the checksum based on 2's complement.
333 return (UINT64
) ((UINT64
)(-1) - CheckSum
+ 1);