/* Returns the sum of all elements in a buffer of 16-bit values. During calculation, the carry bits are also been added. */
UINT16 CbCheckSum16(IN UINT16 *Buffer, IN UINTN Length)
/* Returns the sum of all elements in a buffer of 16-bit values. During calculation, the carry bits are also been added. */
UINT16 CbCheckSum16(IN UINT16 *Buffer, IN UINTN Length)
{
  UINT32  Sum;
  UINT32  TmpValue;
  UINTN   Idx;
  UINT8   *TmpPtr;
  Sum    = 0;
  TmpPtr = (UINT8 *)Buffer;
  for (Idx = 0; Idx < Length; Idx++) {
    TmpValue = TmpPtr[Idx];
    if (Idx % 2 == 1) {
      TmpValue <<= 8;
    }
    Sum += TmpValue;
    if (Sum >= 0x10000) {
      Sum = (Sum + (Sum >> 16)) & 0xFFFF;
    }
  }
  return (UINT16)((~Sum) & 0xFFFF);
}