aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <dvlasenk@redhat.com>2010-10-16 01:08:32 +0200
committerDenys Vlasenko <dvlasenk@redhat.com>2010-10-16 01:08:32 +0200
commit70186711f45816bdf4562831af3bd995172eb47b (patch)
tree6a4729427b042f9841bd9d6c96f5207ab0c9fbdd
parent3d160984944a915cfac13da2b835fa29fc556321 (diff)
downloadbusybox-w32-70186711f45816bdf4562831af3bd995172eb47b.tar.gz
busybox-w32-70186711f45816bdf4562831af3bd995172eb47b.tar.bz2
busybox-w32-70186711f45816bdf4562831af3bd995172eb47b.zip
libbb/md5: code shrink
function old new delta md5_end 123 117 -6 md5_begin 49 42 -7 md5_hash 119 104 -15 Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/md5.c48
2 files changed, 26 insertions, 23 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 8d7beffe7..034016f4a 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1540,7 +1540,6 @@ typedef struct md5_ctx_t {
1540 uint32_t C; 1540 uint32_t C;
1541 uint32_t D; 1541 uint32_t D;
1542 uint64_t total; 1542 uint64_t total;
1543 uint32_t buflen;
1544 char buffer[128]; 1543 char buffer[128];
1545} md5_ctx_t; 1544} md5_ctx_t;
1546#else 1545#else
diff --git a/libbb/md5.c b/libbb/md5.c
index 9e5b496ce..1f9d59e6e 100644
--- a/libbb/md5.c
+++ b/libbb/md5.c
@@ -1,7 +1,7 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * md5.c - Compute MD5 checksum of strings according to the 3 * Compute MD5 checksum of strings according to the
4 * definition of MD5 in RFC 1321 from April 1992. 4 * definition of MD5 in RFC 1321 from April 1992.
5 * 5 *
6 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. 6 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
7 * 7 *
@@ -34,7 +34,6 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
34 ctx->C = 0x98badcfe; 34 ctx->C = 0x98badcfe;
35 ctx->D = 0x10325476; 35 ctx->D = 0x10325476;
36 ctx->total = 0; 36 ctx->total = 0;
37 ctx->buflen = 0;
38} 37}
39 38
40/* These are the four functions used in the four steps of the MD5 algorithm 39/* These are the four functions used in the four steps of the MD5 algorithm
@@ -355,35 +354,39 @@ static void md5_hash_block(const void *buffer, md5_ctx_t *ctx)
355 ctx->D = D; 354 ctx->D = D;
356} 355}
357 356
357/* The size of filled part of ctx->buffer: */
358#define BUFLEN(ctx) (((unsigned)ctx->total) & 63)
359
358/* 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()
359 * 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.
360 * This function's internal buffer remembers previous data until it has 64 362 * This function's internal buffer remembers previous data until it has 64
361 * 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. */
362void FAST_FUNC md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx) 364void FAST_FUNC md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx)
363{ 365{
364 char *buf = (char *)buffer; 366 const char *buf = buffer;
367 unsigned buflen = BUFLEN(ctx);
365 368
366 /* RFC 1321 specifies the possible length of the file up to 2^64 bits, 369 /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
367 * Here we only track the number of bytes. */ 370 * Here we only track the number of bytes. */
368 ctx->total += len; 371 ctx->total += len;
369 372
370 /* Process all input. */ 373 /* Process all input. */
371 while (len) { 374 while (1) {
372 unsigned i = 64 - ctx->buflen; 375 unsigned i = 64 - buflen;
373
374 /* Copy data into aligned buffer. */
375 if (i > len) 376 if (i > len)
376 i = len; 377 i = len;
377 memcpy(ctx->buffer + ctx->buflen, buf, i); 378 /* Copy data into aligned buffer. */
379 memcpy(ctx->buffer + buflen, buf, i);
378 len -= i; 380 len -= i;
379 ctx->buflen += i;
380 buf += i; 381 buf += i;
381 382 buflen += i;
382 /* When buffer fills up, process it. */ 383 /* clever way to do "if (buflen != 64) break; ... ; buflen = 0;" */
383 if (ctx->buflen == 64) { 384 buflen -= 64;
384 md5_hash_block(ctx->buffer, ctx); 385 if (buflen != 0)
385 ctx->buflen = 0; 386 break;
386 } 387 /* Buffer is filled up, process it. */
388 md5_hash_block(ctx->buffer, ctx);
389 /*buflen = 0; - already is */
387 } 390 }
388} 391}
389 392
@@ -396,18 +399,19 @@ void FAST_FUNC md5_end(void *resbuf, md5_ctx_t *ctx)
396{ 399{
397 uint64_t total; 400 uint64_t total;
398 char *buf = ctx->buffer; 401 char *buf = ctx->buffer;
399 int i; 402 unsigned i;
403 unsigned buflen = BUFLEN(ctx);
400 404
401 /* Pad data to block size. */ 405 /* Pad data to block size. */
402 buf[ctx->buflen++] = 0x80; 406 buf[buflen++] = 0x80;
403 memset(buf + ctx->buflen, 0, 128 - ctx->buflen); 407 memset(buf + buflen, 0, 128 - buflen);
404 408
405 /* Put the 64-bit file length, expressed in *bits*, 409 /* Put the 64-bit file length, expressed in *bits*,
406 * at the end of the buffer. 410 * at the end of the buffer.
407 */ 411 */
412 /* clever way to do "if (buflen > 56) buf += 64": */
413 buf += ((buflen + 7) & 64);
408 total = ctx->total << 3; 414 total = ctx->total << 3;
409 if (ctx->buflen > 56)
410 buf += 64;
411 for (i = 0; i < 8; i++) { 415 for (i = 0; i < 8; i++) {
412 buf[56 + i] = total; 416 buf[56 + i] = total;
413 total >>= 8; 417 total >>= 8;