diff options
author | Jim Meyering <jim@meyering.net> | 2003-02-19 19:09:01 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2003-02-19 19:09:01 +0000 |
commit | 47de14a1c5d138c8abfcee6d6a81e4f04e2df837 (patch) | |
tree | 429f2d94f3dcf297efe1f547da20560f9d454c93 /lib | |
parent | 071bd2881a8a3e2a279c88308ef52ccf6d3c9a50 (diff) | |
download | coreutils-47de14a1c5d138c8abfcee6d6a81e4f04e2df837.tar.xz |
Merge in some clean-up and optimization changes from glibc's md5.c.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sha.c | 49 |
1 files changed, 39 insertions, 10 deletions
@@ -217,13 +217,14 @@ sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx) memcpy (&ctx->buffer[left_over], buffer, add); ctx->buflen += add; - if (left_over + add > 64) + if (ctx->buflen > 64) { - sha_process_block (ctx->buffer, (left_over + add) & ~63, ctx); + sha_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; /* The regions in the following copy operation cannot overlap. */ memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - (left_over + add) & 63); - ctx->buflen = (left_over + add) & 63; + ctx->buflen); } buffer = (const char *) buffer + add; @@ -231,18 +232,46 @@ sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx) } /* Process available complete blocks. */ - if (len > 64) + if (len >= 64) { - sha_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; +#if !_STRING_ARCH_unaligned +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) +# else +# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (len > 64) + { + sha_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + sha_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } } /* Move remaining bytes in internal buffer. */ if (len > 0) { - memcpy (ctx->buffer, buffer, len); - ctx->buflen = len; + size_t left_over = ctx->buflen; + + memcpy (&ctx->buffer[left_over], buffer, len); + left_over += len; + if (left_over >= 64) + { + sha_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; } } |