Share via


2.6 Checksum Algorithm

Calculates the checksum value of a buffer. Checksum is based on 1's compliment of the buffer content 

buffer: A pointer to the buffer whose checksum has to be calculated

length: The Length of the above buffer

addressToIgnore: Range of addresses to be ignored in the buffer

return: Checksum value of the buffer

  
 ULONG  
  CalculateChecksum (  
     PVOID buffer,  
     ULONG length,  
     ULONG* addressToIgnore  
     )  
  
 {  
     PUCHAR address;  
     ULONG checksum; 
  
     checksum = 0;  
     address = (PUCHAR)buffer;  
     while (length != 0) {  
         if ((address >= (PUCHAR)addressToIgnore) &&  
             (address < (PUCHAR)(addressToIgnore + 1))) {  
         } else {  
             checksum += *address;  
         }  
  
         length -= 1;  
         address += 1;  
     }  
  
     return ~checksum;  
 }