diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-16 22:43:34 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-16 22:43:34 +0200 |
commit | 273abcbf664adc92ef3bc1d9752a2b571623ad52 (patch) | |
tree | ae4bdff4451ce36e4bb0568e7020d9cd9f150ba8 /libbb/md5.c | |
parent | 1ac476bb8561f57703b84f090ed7a575fa512823 (diff) | |
download | busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.tar.gz busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.tar.bz2 busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.zip |
shaN: small code shrink
function old new delta
sha512_hash 134 128 -6
sha1_hash 114 106 -8
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/md5.c')
-rw-r--r-- | libbb/md5.c | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/libbb/md5.c b/libbb/md5.c index f192d0e47..cf3825a34 100644 --- a/libbb/md5.c +++ b/libbb/md5.c | |||
@@ -354,8 +354,8 @@ static void md5_hash_block(md5_ctx_t *ctx) | |||
354 | ctx->D = D; | 354 | ctx->D = D; |
355 | } | 355 | } |
356 | 356 | ||
357 | /* The size of filled part of ctx->buffer: */ | 357 | /* The first unused position in ctx->buffer: */ |
358 | #define BUFLEN(ctx) (((unsigned)ctx->total) & 63) | 358 | #define BUFPOS(ctx) (((unsigned)ctx->total) & 63) |
359 | 359 | ||
360 | /* Feed data through a temporary buffer to call md5_hash_aligned_block() | 360 | /* Feed data through a temporary buffer to call md5_hash_aligned_block() |
361 | * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. | 361 | * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. |
@@ -363,31 +363,52 @@ static void md5_hash_block(md5_ctx_t *ctx) | |||
363 | * bytes worth to pass on. Call md5_end() to flush this buffer. */ | 363 | * bytes worth to pass on. Call md5_end() to flush this buffer. */ |
364 | void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) | 364 | void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) |
365 | { | 365 | { |
366 | const char *buf = buffer; | 366 | #if 1 |
367 | unsigned buflen = BUFLEN(ctx); | 367 | /* Tiny bit smaller code */ |
368 | unsigned bufpos = BUFPOS(ctx); | ||
368 | 369 | ||
369 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. | 370 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. |
370 | * Here we only track the number of bytes. */ | 371 | * Here we only track the number of bytes. */ |
371 | ctx->total += len; | 372 | ctx->total += len; |
372 | 373 | ||
373 | /* Process all input. */ | ||
374 | while (1) { | 374 | while (1) { |
375 | unsigned i = 64 - buflen; | 375 | unsigned remaining = 64 - bufpos; |
376 | if (i > len) | 376 | if (remaining > len) |
377 | i = len; | 377 | remaining = len; |
378 | /* Copy data into aligned buffer. */ | 378 | /* Copy data into aligned buffer. */ |
379 | memcpy(ctx->buffer + buflen, buf, i); | 379 | memcpy(ctx->buffer + bufpos, buffer, remaining); |
380 | len -= i; | 380 | len -= remaining; |
381 | buf += i; | 381 | buffer = (const char *)buffer + remaining; |
382 | buflen += i; | 382 | bufpos += remaining; |
383 | /* clever way to do "if (buflen != 64) break; ... ; buflen = 0;" */ | 383 | /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ |
384 | buflen -= 64; | 384 | bufpos -= 64; |
385 | if (buflen != 0) | 385 | if (bufpos != 0) |
386 | break; | 386 | break; |
387 | /* Buffer is filled up, process it. */ | 387 | /* Buffer is filled up, process it. */ |
388 | md5_hash_block(ctx); | 388 | md5_hash_block(ctx); |
389 | /*buflen = 0; - already is */ | 389 | /*bufpos = 0; - already is */ |
390 | } | 390 | } |
391 | #else | ||
392 | unsigned bufpos = BUFPOS(ctx); | ||
393 | unsigned add = 64 - bufpos; | ||
394 | |||
395 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. | ||
396 | * Here we only track the number of bytes. */ | ||
397 | ctx->total += len; | ||
398 | |||
399 | /* Hash whole blocks */ | ||
400 | while (len >= add) { | ||
401 | memcpy(ctx->buffer + bufpos, buffer, add); | ||
402 | buffer = (const char *)buffer + add; | ||
403 | len -= add; | ||
404 | add = 64; | ||
405 | bufpos = 0; | ||
406 | md5_hash_block(ctx); | ||
407 | } | ||
408 | |||
409 | /* Save last, partial blosk */ | ||
410 | memcpy(ctx->buffer + bufpos, buffer, len); | ||
411 | #endif | ||
391 | } | 412 | } |
392 | 413 | ||
393 | /* Process the remaining bytes in the buffer and put result from CTX | 414 | /* Process the remaining bytes in the buffer and put result from CTX |
@@ -399,13 +420,13 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) | |||
399 | { | 420 | { |
400 | uint64_t total; | 421 | uint64_t total; |
401 | unsigned i; | 422 | unsigned i; |
402 | unsigned buflen = BUFLEN(ctx); | 423 | unsigned bufpos = BUFPOS(ctx); |
403 | 424 | ||
404 | /* Pad data to block size. */ | 425 | /* Pad data to block size. */ |
405 | ctx->buffer[buflen++] = 0x80; | 426 | ctx->buffer[bufpos++] = 0x80; |
406 | memset(ctx->buffer + buflen, 0, 64 - buflen); | 427 | memset(ctx->buffer + bufpos, 0, 64 - bufpos); |
407 | 428 | ||
408 | if (buflen > 56) { | 429 | if (bufpos > 56) { |
409 | md5_hash_block(ctx); | 430 | md5_hash_block(ctx); |
410 | memset(ctx->buffer, 0, 64); | 431 | memset(ctx->buffer, 0, 64); |
411 | } | 432 | } |