Esempio n. 1
0
static uint32_t
_crc32(const char *buffer, uint32_t length) {

    uint32_t i;
    uint32_t crc32 = ~0L;

    for (i = 0; i < length; i++) {
        CRC32C(crc32, (unsigned char) buffer[i]);
    }

    return ~crc32;

}
unsigned int 
insert_crc32(unsigned char *buffer, unsigned int length) 
{ 
  unsigned int i; 
  unsigned long crc32 = ~0L; 
     
  for (i = 0; i < length; i++){ 
    CRC32C(crc32, buffer[i]); 
  } 
     
  /* and return it */
  return(crc32); 
} 
int
insert_crc32(unsigned char *buffer, unsigned int length)
{
SCTP_message *message;
unsigned int i;
unsigned long crc32 = ~0L;

   /* check packet length */
if (length > NMAX || length < NMIN)
   return -1;

message = (SCTP_message *) buffer;
message->common_header.checksum = 0L;

for (i = 0; i < length; i++)
   {
   CRC32C(crc32, buffer[i]);
   }

   /* and insert it into the message */
message->common_header.checksum = htonl(crc32);
return 1;
}
int
validate_crc32(unsigned char *buffer, unsigned int length)
{
SCTP_message *message;
unsigned int i;
unsigned long original_crc32;
unsigned long crc32 = ~0L;

   /* check packet length */
if (length > NMAX || length < NMIN)
   return -1;

   /* save and zero checksum */
message = (SCTP_message *) buffer;
original_crc32 = ntohl(message->common_header.checksum);
message->common_header.checksum = 0L;

for (i = 0; i < length; i++)
   {
   CRC32C(crc32, buffer[i]);
   }

return ((original_crc32 == crc32) ? 1 : -1);
}