]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/CheckSum.c
Update the copyright notice format
[mirror_edk2.git] / MdePkg / Library / BaseLib / CheckSum.c
index 5f755451c4af0a36f1f952521b27834b64be02ff..fbf9342d176b7d6a7713c22c5d5dcf8d2ef57ecd 100644 (file)
@@ -2,8 +2,8 @@
   Utility functions to generate checksum based on 2's complement\r
   algorithm.\r
 \r
-  Copyright (c) 2007, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
 \r
 **/\r
 \r
-//\r
-// Include common header file for this module.\r
-//\r
-#include <BaseLibInternals.h>\r
-\r
+#include "BaseLibInternals.h"\r
 \r
 /**\r
-  Calculate the sum of all elements in a buffer in unit of UINT8. \r
+  Returns the sum of all elements in a buffer in unit of UINT8.\r
   During calculation, the carry bits are dropped.\r
 \r
-  This function calculates the sum of all elements in a buffer \r
-  in unit of UINT8. The carry bits in result of addition are dropped. \r
-  The result is returned as UINT8. If Length is Zero, then Zero is \r
+  This function calculates the sum of all elements in a buffer\r
+  in unit of UINT8. The carry bits in result of addition are dropped.\r
+  The result is returned as UINT8. If Length is Zero, then Zero is\r
   returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
-  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the sum operation.\r
-  @param  Length           The size, in bytes, of Buffer .\r
+  @param  Buffer      Pointer to the buffer to carry out the sum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
   @return Sum         The sum of Buffer with carry bits dropped during additions.\r
 \r
@@ -40,8 +36,8 @@
 UINT8\r
 EFIAPI\r
 CalculateSum8 (\r
-  IN      CONST UINT8                  *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT8              *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT8     Sum;\r
@@ -59,29 +55,28 @@ CalculateSum8 (
 \r
 \r
 /**\r
-  Returns the two's complement checksum of all elements in a buffer \r
+  Returns the two's complement checksum of all elements in a buffer\r
   of 8-bit values.\r
 \r
-  This function first calculates the sum of the 8-bit values in the \r
-  buffer specified by Buffer and Length.  The carry bits in the result \r
-  of addition are dropped. Then, the two's complement of the sum is \r
+  This function first calculates the sum of the 8-bit values in the\r
+  buffer specified by Buffer and Length.  The carry bits in the result\r
+  of addition are dropped. Then, the two's complement of the sum is\r
   returned.  If Length is 0, then 0 is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
+  @param  Buffer      Pointer to the buffer to carry out the checksum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the checksum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
-\r
-  @return Checksum       The 2's complement checksum of Buffer.\r
+  @return Checksum    The 2's complement checksum of Buffer.\r
 \r
 **/\r
 UINT8\r
 EFIAPI\r
 CalculateCheckSum8 (\r
-  IN      CONST UINT8                  *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT8              *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT8     CheckSum;\r
@@ -95,20 +90,20 @@ CalculateCheckSum8 (
 }\r
 \r
 /**\r
-  Returns the sum of all elements in a buffer of 16-bit values.  During \r
+  Returns the sum of all elements in a buffer of 16-bit values.  During\r
   calculation, the carry bits are dropped.\r
 \r
-  This function calculates the sum of the 16-bit values in the buffer \r
-  specified by Buffer and Length. The carry bits in result of addition are dropped. \r
-  The 16-bit result is returned.  If Length is 0, then 0 is returned.  \r
-  \r
+  This function calculates the sum of the 16-bit values in the buffer\r
+  specified by Buffer and Length. The carry bits in result of addition are dropped.\r
+  The 16-bit result is returned.  If Length is 0, then 0 is returned.\r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the sum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the sum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
   @return Sum         The sum of Buffer with carry bits dropped during additions.\r
 \r
@@ -116,20 +111,21 @@ CalculateCheckSum8 (
 UINT16\r
 EFIAPI\r
 CalculateSum16 (\r
-  IN      CONST UINT16                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT16             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT16    Sum;\r
   UINTN     Count;\r
+  UINTN     Total;\r
 \r
   ASSERT (Buffer != NULL);\r
   ASSERT (((UINTN) Buffer & 0x1) == 0);\r
   ASSERT ((Length & 0x1) == 0);\r
   ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r
 \r
-\r
-  for (Sum = 0, Count = 0; Count < Length; Count++) {\r
+  Total = Length / sizeof (*Buffer);\r
+  for (Sum = 0, Count = 0; Count < Total; Count++) {\r
     Sum = (UINT16) (Sum + *(Buffer + Count));\r
   }\r
   \r
@@ -138,30 +134,30 @@ CalculateSum16 (
 \r
 \r
 /**\r
-  Returns the two's complement checksum of all elements in a buffer of \r
+  Returns the two's complement checksum of all elements in a buffer of\r
   16-bit values.\r
 \r
-  This function first calculates the sum of the 16-bit values in the buffer \r
-  specified by Buffer and Length.  The carry bits in the result of addition \r
-  are dropped. Then, the two's complement of the sum is returned.  If Length \r
+  This function first calculates the sum of the 16-bit values in the buffer\r
+  specified by Buffer and Length.  The carry bits in the result of addition\r
+  are dropped. Then, the two's complement of the sum is returned.  If Length\r
   is 0, then 0 is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length is not aligned on a 16-bit boundary, then ASSERT().\r
-  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the checksum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the checksum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
-  @return Checksum       The 2's complement checksum of Buffer.\r
+  @return Checksum    The 2's complement checksum of Buffer.\r
 \r
 **/\r
 UINT16\r
 EFIAPI\r
 CalculateCheckSum16 (\r
-  IN      CONST UINT16                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT16             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT16     CheckSum;\r
@@ -176,20 +172,20 @@ CalculateCheckSum16 (
 \r
 \r
 /**\r
-  Returns the sum of all elements in a buffer of 32-bit values.  During \r
+  Returns the sum of all elements in a buffer of 32-bit values. During\r
   calculation, the carry bits are dropped.\r
 \r
-  This function calculates the sum of the 32-bit values in the buffer \r
-  specified by Buffer and Length. The carry bits in result of addition are dropped. \r
-  The 32-bit result is returned.  If Length is 0, then 0 is returned.  \r
-  \r
+  This function calculates the sum of the 32-bit values in the buffer\r
+  specified by Buffer and Length. The carry bits in result of addition are dropped.\r
+  The 32-bit result is returned. If Length is 0, then 0 is returned.\r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
   If Length is not aligned on a 32-bit boundary, then ASSERT().\r
   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the sum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the sum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
   @return Sum         The sum of Buffer with carry bits dropped during additions.\r
 \r
@@ -197,20 +193,21 @@ CalculateCheckSum16 (
 UINT32\r
 EFIAPI\r
 CalculateSum32 (\r
-  IN      CONST UINT32                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT32             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT32    Sum;\r
   UINTN     Count;\r
+  UINTN     Total;\r
 \r
   ASSERT (Buffer != NULL);\r
   ASSERT (((UINTN) Buffer & 0x3) == 0);\r
   ASSERT ((Length & 0x3) == 0);\r
   ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r
 \r
-\r
-  for (Sum = 0, Count = 0; Count < Length; Count++) {\r
+  Total = Length / sizeof (*Buffer);\r
+  for (Sum = 0, Count = 0; Count < Total; Count++) {\r
     Sum = Sum + *(Buffer + Count);\r
   }\r
   \r
@@ -219,30 +216,30 @@ CalculateSum32 (
 \r
 \r
 /**\r
-  Returns the two's complement checksum of all elements in a buffer of \r
+  Returns the two's complement checksum of all elements in a buffer of\r
   32-bit values.\r
 \r
-  This function first calculates the sum of the 32-bit values in the buffer \r
-  specified by Buffer and Length.  The carry bits in the result of addition \r
-  are dropped. Then, the two's complement of the sum is returned.  If Length \r
+  This function first calculates the sum of the 32-bit values in the buffer\r
+  specified by Buffer and Length.  The carry bits in the result of addition\r
+  are dropped. Then, the two's complement of the sum is returned.  If Length\r
   is 0, then 0 is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
   If Length is not aligned on a 32-bit boundary, then ASSERT().\r
-  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the checksum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the checksum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
-  @return Checksum       The 2's complement checksum of Buffer.\r
+  @return Checksum    The 2's complement checksum of Buffer.\r
 \r
 **/\r
 UINT32\r
 EFIAPI\r
 CalculateCheckSum32 (\r
-  IN      CONST UINT32                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT32             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT32     CheckSum;\r
@@ -257,20 +254,20 @@ CalculateCheckSum32 (
 \r
 \r
 /**\r
-  Returns the sum of all elements in a buffer of 64-bit values.  During \r
+  Returns the sum of all elements in a buffer of 64-bit values.  During\r
   calculation, the carry bits are dropped.\r
 \r
-  This function calculates the sum of the 64-bit values in the buffer \r
-  specified by Buffer and Length. The carry bits in result of addition are dropped. \r
-  The 64-bit result is returned.  If Length is 0, then 0 is returned.  \r
-  \r
+  This function calculates the sum of the 64-bit values in the buffer\r
+  specified by Buffer and Length. The carry bits in result of addition are dropped.\r
+  The 64-bit result is returned.  If Length is 0, then 0 is returned.\r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r
   If Length is not aligned on a 64-bit boundary, then ASSERT().\r
   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the sum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the sum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
   @return Sum         The sum of Buffer with carry bits dropped during additions.\r
 \r
@@ -278,19 +275,21 @@ CalculateCheckSum32 (
 UINT64\r
 EFIAPI\r
 CalculateSum64 (\r
-  IN      CONST UINT64                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT64             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT64    Sum;\r
   UINTN     Count;\r
+  UINTN     Total;\r
 \r
   ASSERT (Buffer != NULL);\r
   ASSERT (((UINTN) Buffer & 0x7) == 0);\r
   ASSERT ((Length & 0x7) == 0);\r
   ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r
 \r
-  for (Sum = 0, Count = 0; Count < Length; Count++) {\r
+  Total = Length / sizeof (*Buffer);\r
+  for (Sum = 0, Count = 0; Count < Total; Count++) {\r
     Sum = Sum + *(Buffer + Count);\r
   }\r
   \r
@@ -299,30 +298,30 @@ CalculateSum64 (
 \r
 \r
 /**\r
-  Returns the two's complement checksum of all elements in a buffer of \r
+  Returns the two's complement checksum of all elements in a buffer of\r
   64-bit values.\r
 \r
-  This function first calculates the sum of the 64-bit values in the buffer \r
-  specified by Buffer and Length.  The carry bits in the result of addition \r
-  are dropped. Then, the two's complement of the sum is returned.  If Length \r
+  This function first calculates the sum of the 64-bit values in the buffer\r
+  specified by Buffer and Length.  The carry bits in the result of addition\r
+  are dropped. Then, the two's complement of the sum is returned.  If Length\r
   is 0, then 0 is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
   If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r
   If Length is not aligned on a 64-bit boundary, then ASSERT().\r
-  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
-  @param  Buffer                       Pointer to the buffer to carry out the checksum operation.\r
-  @param  Length           The size, in bytes, of Buffer.\r
+  @param  Buffer      Pointer to the buffer to carry out the checksum operation.\r
+  @param  Length      The size, in bytes, of Buffer.\r
 \r
-  @return Checksum       The 2's complement checksum of Buffer.\r
+  @return Checksum    The 2's complement checksum of Buffer.\r
 \r
 **/\r
 UINT64\r
 EFIAPI\r
 CalculateCheckSum64 (\r
-  IN      CONST UINT64                 *Buffer,\r
-  IN      UINTN                                      Length\r
+  IN      CONST UINT64             *Buffer,\r
+  IN      UINTN                     Length\r
   )\r
 {\r
   UINT64     CheckSum;\r