X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fcrc.c;h=070a261642db4df11a994f0361165860c5af10b5;hb=e4c86af69014f1a129098c5edf8706e9d08dbb4e;hp=2f818840b3c1d29832eca1e3d6b0ef5ffde3ad53;hpb=ae7dfec55384883a72867d4877b5283a3c1c8aa1;p=libucw.git diff --git a/ucw/crc.c b/ucw/crc.c index 2f818840..070a2616 100644 --- a/ucw/crc.c +++ b/ucw/crc.c @@ -32,7 +32,12 @@ static void crc32_update_by4(crc32_context *ctx, const byte *buf, uint len) { uint init_bytes, words; u32 crc = ctx->state; - u32 term1, term2, *buf32; + u32 term1, term2; + const u32 *buf32; + + // Special case + if (len < 4) + goto small; // Align start address to a multiple of 4 bytes init_bytes = ((uintptr_t) buf) & 3; @@ -47,7 +52,7 @@ static void crc32_update_by4(crc32_context *ctx, const byte *buf, uint len) // Process 4 bytes at a time words = len/4; len -= 4*words; - buf32 = (u32 *) buf; + buf32 = (const u32 *) buf; while (words--) { crc ^= *buf32++; @@ -59,7 +64,8 @@ static void crc32_update_by4(crc32_context *ctx, const byte *buf, uint len) } // Process remaining up to 7 bytes - buf = (byte *) buf32; + buf = (const byte *) buf32; +small: while (len--) crc = crc_tableil8_o32[(crc ^ *buf++) & 0x000000FF] ^ (crc >> 8); @@ -70,7 +76,12 @@ static void crc32_update_by8(crc32_context *ctx, const byte *buf, uint len) { uint init_bytes, quads; u32 crc = ctx->state; - u32 term1, term2, *buf32; + u32 term1, term2; + const u32 *buf32; + + // Special case + if (len < 8) + goto small; // Align start address to a multiple of 8 bytes init_bytes = ((uintptr_t) buf) & 7; @@ -85,7 +96,7 @@ static void crc32_update_by8(crc32_context *ctx, const byte *buf, uint len) // Process 8 bytes at a time quads = len/8; len -= 8*quads; - buf32 = (u32 *) buf; + buf32 = (const u32 *) buf; while (quads--) { crc ^= *buf32++; @@ -107,7 +118,8 @@ static void crc32_update_by8(crc32_context *ctx, const byte *buf, uint len) } // Process remaining up to 7 bytes - buf = (byte *) buf32; + buf = (const byte *) buf32; +small: while (len--) crc = crc_tableil8_o32[(crc ^ *buf++) & 0x000000FF] ^ (crc >> 8);