X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/8160fbfcfe7cd5ca0b430d056c0e7c392c0f4f31..a607fe54c9f20934b57e29e9040b9dd0a4decf3a:/sshcrc.c diff --git a/sshcrc.c b/sshcrc.c index 63050b95..ed20395b 100644 --- a/sshcrc.c +++ b/sshcrc.c @@ -69,17 +69,19 @@ #include +#include "ssh.h" + /* ---------------------------------------------------------------------- * Multi-function module. Can be compiled three ways. * * - Compile with no special #defines. Will generate a table * that's already initialised at compile time, and one function - * crc32(buf,len) that uses it. Normal usage. + * crc32_compute(buf,len) that uses it. Normal usage. * * - Compile with INITFUNC defined. Will generate an uninitialised - * array as the table, and as well as crc32(buf,len) it will - * also generate void crc32_init(void) which sets up the table - * at run time. Useful if binary size is important. + * array as the table, and as well as crc32_compute(buf,len) it + * will also generate void crc32_init(void) which sets up the + * table at run time. Useful if binary size is important. * * - Compile with GENPROGRAM defined. Will create a standalone * program that does the initialisation and outputs the table as @@ -89,7 +91,7 @@ #define POLY (0xEDB88320L) #ifdef GENPROGRAM -#define INITFUNC /* the gen program needs the init func :-) */ +#define INITFUNC /* the gen program needs the init func :-) */ #endif #ifdef INITFUNC @@ -100,21 +102,22 @@ */ static unsigned long crc32_table[256]; -void crc32_init(void) { +void crc32_init(void) +{ unsigned long crcword; int i; - + for (i = 0; i < 256; i++) { - unsigned long newbyte, x32term; - int j; - crcword = 0; - newbyte = i; - for (j = 0; j < 8; j++) { - x32term = (crcword ^ newbyte) & 1; - crcword = (crcword >> 1) ^ (x32term * POLY); - newbyte >>= 1; - } - crc32_table[i] = crcword; + unsigned long newbyte, x32term; + int j; + crcword = 0; + newbyte = i; + for (j = 0; j < 8; j++) { + x32term = (crcword ^ newbyte) & 1; + crcword = (crcword >> 1) ^ (x32term * POLY); + newbyte >>= 1; + } + crc32_table[i] = crcword; } } @@ -193,29 +196,35 @@ static const unsigned long crc32_table[256] = { #endif #ifdef GENPROGRAM -int main(void) { +int main(void) +{ unsigned long crcword; int i; crc32_init(); for (i = 0; i < 256; i++) { - printf("%s0x%08XL%s", - (i % 4 == 0 ? " " : " "), - crc32_table[i], - (i % 4 == 3 ? (i == 255 ? "\n" : ",\n") : ",")); + printf("%s0x%08XL%s", + (i % 4 == 0 ? " " : " "), + crc32_table[i], + (i % 4 == 3 ? (i == 255 ? "\n" : ",\n") : ",")); } return 0; } #endif -unsigned long crc32(const void *buf, size_t len) { - unsigned long crcword = 0L; +unsigned long crc32_update(unsigned long crcword, const void *buf, size_t len) +{ const unsigned char *p = (const unsigned char *) buf; while (len--) { - unsigned long newbyte = *p++; - newbyte ^= crcword & 0xFFL; - crcword = (crcword >> 8) ^ crc32_table[newbyte]; + unsigned long newbyte = *p++; + newbyte ^= crcword & 0xFFL; + crcword = (crcword >> 8) ^ crc32_table[newbyte]; } return crcword; } + +unsigned long crc32_compute(const void *buf, size_t len) +{ + return crc32_update(0L, buf, len); +}