X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseLib%2FCheckSum.c;h=4acaf42a6f48a08eb2b2dcd593913f805fb7bcff;hb=d1be17ab87c51fd53381491610ede29b6dbd10fa;hp=e851164998f19b85aad940ef88706b454cb5acb7;hpb=15f83a8852a33624902bf4476a20ac9e5f3adae5;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseLib/CheckSum.c b/MdePkg/Library/BaseLib/CheckSum.c index e851164998..4acaf42a6f 100644 --- a/MdePkg/Library/BaseLib/CheckSum.c +++ b/MdePkg/Library/BaseLib/CheckSum.c @@ -2,7 +2,7 @@ Utility functions to generate checksum based on 2's complement algorithm. - Copyright (c) 2007, Intel Corporation
+ Copyright (c) 2007 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -11,24 +11,24 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: CheckSum.c - **/ +#include "BaseLibInternals.h" + /** - Calculate the sum of all elements in a buffer in unit of UINT8. + Returns the sum of all elements in a buffer in unit of UINT8. During calculation, the carry bits are dropped. - This function calculates the sum of all elements in a buffer - in unit of UINT8. The carry bits in result of addition are dropped. - The result is returned as UINT8. If Length is Zero, then Zero is + This function calculates the sum of all elements in a buffer + in unit of UINT8. The carry bits in result of addition are dropped. + The result is returned as UINT8. If Length is Zero, then Zero is returned. - + If Buffer is NULL, then ASSERT(). - If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the sum operation. - @param Length The size, in bytes, of Buffer . + @param Buffer Pointer to the buffer to carry out the sum operation. + @param Length The size, in bytes, of Buffer. @return Sum The sum of Buffer with carry bits dropped during additions. @@ -36,8 +36,8 @@ UINT8 EFIAPI CalculateSum8 ( - IN CONST UINT8 *Buffer, - IN UINTN Length + IN CONST UINT8 *Buffer, + IN UINTN Length ) { UINT8 Sum; @@ -55,29 +55,28 @@ CalculateSum8 ( /** - Returns the two's complement checksum of all elements in a buffer + Returns the two's complement checksum of all elements in a buffer of 8-bit values. - This function first calculates the sum of the 8-bit values in the - buffer specified by Buffer and Length. The carry bits in the result - of addition are dropped. Then, the two's complement of the sum is + This function first calculates the sum of the 8-bit values in the + buffer specified by Buffer and Length. The carry bits in the result + of addition are dropped. Then, the two's complement of the sum is returned. If Length is 0, then 0 is returned. - + If Buffer is NULL, then ASSERT(). If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + @param Buffer Pointer to the buffer to carry out the checksum operation. + @param Length The size, in bytes, of Buffer. - @param Buffer Pointer to the buffer to carry out the checksum operation. - @param Length The size, in bytes, of Buffer. - - @return Checksum The 2's complement checksum of Buffer. + @return Checksum The 2's complement checksum of Buffer. **/ UINT8 EFIAPI CalculateCheckSum8 ( - IN CONST UINT8 *Buffer, - IN UINTN Length + IN CONST UINT8 *Buffer, + IN UINTN Length ) { UINT8 CheckSum; @@ -91,20 +90,20 @@ CalculateCheckSum8 ( } /** - Returns the sum of all elements in a buffer of 16-bit values. During + Returns the sum of all elements in a buffer of 16-bit values. During calculation, the carry bits are dropped. - This function calculates the sum of the 16-bit values in the buffer - specified by Buffer and Length. The carry bits in result of addition are dropped. - The 16-bit result is returned. If Length is 0, then 0 is returned. - + This function calculates the sum of the 16-bit values in the buffer + specified by Buffer and Length. The carry bits in result of addition are dropped. + The 16-bit result is returned. If Length is 0, then 0 is returned. + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 16-bit boundary, then ASSERT(). If Length is not aligned on a 16-bit boundary, then ASSERT(). If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the sum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the sum operation. + @param Length The size, in bytes, of Buffer. @return Sum The sum of Buffer with carry bits dropped during additions. @@ -112,20 +111,21 @@ CalculateCheckSum8 ( UINT16 EFIAPI CalculateSum16 ( - IN CONST UINT16 *Buffer, - IN UINTN Length + IN CONST UINT16 *Buffer, + IN UINTN Length ) { UINT16 Sum; UINTN Count; + UINTN Total; ASSERT (Buffer != NULL); ASSERT (((UINTN) Buffer & 0x1) == 0); ASSERT ((Length & 0x1) == 0); ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1)); - - for (Sum = 0, Count = 0; Count < Length; Count++) { + Total = Length / sizeof (*Buffer); + for (Sum = 0, Count = 0; Count < Total; Count++) { Sum = (UINT16) (Sum + *(Buffer + Count)); } @@ -134,30 +134,30 @@ CalculateSum16 ( /** - Returns the two's complement checksum of all elements in a buffer of + Returns the two's complement checksum of all elements in a buffer of 16-bit values. - This function first calculates the sum of the 16-bit values in the buffer - specified by Buffer and Length. The carry bits in the result of addition - are dropped. Then, the two's complement of the sum is returned. If Length + This function first calculates the sum of the 16-bit values in the buffer + specified by Buffer and Length. The carry bits in the result of addition + are dropped. Then, the two's complement of the sum is returned. If Length is 0, then 0 is returned. - + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 16-bit boundary, then ASSERT(). If Length is not aligned on a 16-bit boundary, then ASSERT(). - If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the checksum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the checksum operation. + @param Length The size, in bytes, of Buffer. - @return Checksum The 2's complement checksum of Buffer. + @return Checksum The 2's complement checksum of Buffer. **/ UINT16 EFIAPI CalculateCheckSum16 ( - IN CONST UINT16 *Buffer, - IN UINTN Length + IN CONST UINT16 *Buffer, + IN UINTN Length ) { UINT16 CheckSum; @@ -172,20 +172,20 @@ CalculateCheckSum16 ( /** - Returns the sum of all elements in a buffer of 32-bit values. During + Returns the sum of all elements in a buffer of 32-bit values. During calculation, the carry bits are dropped. - This function calculates the sum of the 32-bit values in the buffer - specified by Buffer and Length. The carry bits in result of addition are dropped. - The 32-bit result is returned. If Length is 0, then 0 is returned. - + This function calculates the sum of the 32-bit values in the buffer + specified by Buffer and Length. The carry bits in result of addition are dropped. + The 32-bit result is returned. If Length is 0, then 0 is returned. + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 32-bit boundary, then ASSERT(). If Length is not aligned on a 32-bit boundary, then ASSERT(). If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the sum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the sum operation. + @param Length The size, in bytes, of Buffer. @return Sum The sum of Buffer with carry bits dropped during additions. @@ -193,20 +193,21 @@ CalculateCheckSum16 ( UINT32 EFIAPI CalculateSum32 ( - IN CONST UINT32 *Buffer, - IN UINTN Length + IN CONST UINT32 *Buffer, + IN UINTN Length ) { UINT32 Sum; UINTN Count; + UINTN Total; ASSERT (Buffer != NULL); ASSERT (((UINTN) Buffer & 0x3) == 0); ASSERT ((Length & 0x3) == 0); ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1)); - - for (Sum = 0, Count = 0; Count < Length; Count++) { + Total = Length / sizeof (*Buffer); + for (Sum = 0, Count = 0; Count < Total; Count++) { Sum = Sum + *(Buffer + Count); } @@ -215,30 +216,30 @@ CalculateSum32 ( /** - Returns the two's complement checksum of all elements in a buffer of + Returns the two's complement checksum of all elements in a buffer of 32-bit values. - This function first calculates the sum of the 32-bit values in the buffer - specified by Buffer and Length. The carry bits in the result of addition - are dropped. Then, the two's complement of the sum is returned. If Length + This function first calculates the sum of the 32-bit values in the buffer + specified by Buffer and Length. The carry bits in the result of addition + are dropped. Then, the two's complement of the sum is returned. If Length is 0, then 0 is returned. - + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 32-bit boundary, then ASSERT(). If Length is not aligned on a 32-bit boundary, then ASSERT(). - If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the checksum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the checksum operation. + @param Length The size, in bytes, of Buffer. - @return Checksum The 2's complement checksum of Buffer. + @return Checksum The 2's complement checksum of Buffer. **/ UINT32 EFIAPI CalculateCheckSum32 ( - IN CONST UINT32 *Buffer, - IN UINTN Length + IN CONST UINT32 *Buffer, + IN UINTN Length ) { UINT32 CheckSum; @@ -253,20 +254,20 @@ CalculateCheckSum32 ( /** - Returns the sum of all elements in a buffer of 64-bit values. During + Returns the sum of all elements in a buffer of 64-bit values. During calculation, the carry bits are dropped. - This function calculates the sum of the 64-bit values in the buffer - specified by Buffer and Length. The carry bits in result of addition are dropped. - The 64-bit result is returned. If Length is 0, then 0 is returned. - + This function calculates the sum of the 64-bit values in the buffer + specified by Buffer and Length. The carry bits in result of addition are dropped. + The 64-bit result is returned. If Length is 0, then 0 is returned. + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 64-bit boundary, then ASSERT(). If Length is not aligned on a 64-bit boundary, then ASSERT(). If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the sum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the sum operation. + @param Length The size, in bytes, of Buffer. @return Sum The sum of Buffer with carry bits dropped during additions. @@ -274,19 +275,21 @@ CalculateCheckSum32 ( UINT64 EFIAPI CalculateSum64 ( - IN CONST UINT64 *Buffer, - IN UINTN Length + IN CONST UINT64 *Buffer, + IN UINTN Length ) { UINT64 Sum; UINTN Count; + UINTN Total; ASSERT (Buffer != NULL); ASSERT (((UINTN) Buffer & 0x7) == 0); ASSERT ((Length & 0x7) == 0); ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1)); - for (Sum = 0, Count = 0; Count < Length; Count++) { + Total = Length / sizeof (*Buffer); + for (Sum = 0, Count = 0; Count < Total; Count++) { Sum = Sum + *(Buffer + Count); } @@ -295,30 +298,30 @@ CalculateSum64 ( /** - Returns the two's complement checksum of all elements in a buffer of + Returns the two's complement checksum of all elements in a buffer of 64-bit values. - This function first calculates the sum of the 64-bit values in the buffer - specified by Buffer and Length. The carry bits in the result of addition - are dropped. Then, the two's complement of the sum is returned. If Length + This function first calculates the sum of the 64-bit values in the buffer + specified by Buffer and Length. The carry bits in the result of addition + are dropped. Then, the two's complement of the sum is returned. If Length is 0, then 0 is returned. - + If Buffer is NULL, then ASSERT(). If Buffer is not aligned on a 64-bit boundary, then ASSERT(). If Length is not aligned on a 64-bit boundary, then ASSERT(). - If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - @param Buffer Pointer to the buffer to carry out the checksum operation. - @param Length The size, in bytes, of Buffer. + @param Buffer Pointer to the buffer to carry out the checksum operation. + @param Length The size, in bytes, of Buffer. - @return Checksum The 2's complement checksum of Buffer. + @return Checksum The 2's complement checksum of Buffer. **/ UINT64 EFIAPI CalculateCheckSum64 ( - IN CONST UINT64 *Buffer, - IN UINTN Length + IN CONST UINT64 *Buffer, + IN UINTN Length ) { UINT64 CheckSum;