diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-11-20 21:30:58 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-11-20 21:30:58 +0000 |
commit | 78cd84da8a0fe5588f5778439cd68d95ab1f446a (patch) | |
tree | 5134094c11a4203762df0de13c9f0447d7c33193 | |
parent | e857122c97b934a00252889a978db69d4e774df7 (diff) | |
download | busybox-w32-78cd84da8a0fe5588f5778439cd68d95ab1f446a.tar.gz busybox-w32-78cd84da8a0fe5588f5778439cd68d95ab1f446a.tar.bz2 busybox-w32-78cd84da8a0fe5588f5778439cd68d95ab1f446a.zip |
Dont over-read fd, use function pointer for hash function.
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/hash_fd.c | 72 |
2 files changed, 44 insertions, 30 deletions
diff --git a/include/libbb.h b/include/libbb.h index 88f43d01c..22a77e610 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -479,7 +479,7 @@ extern void xregcomp(regex_t *preg, const char *regex, int cflags); | |||
479 | 479 | ||
480 | #define HASH_SHA1 1 | 480 | #define HASH_SHA1 1 |
481 | #define HASH_MD5 2 | 481 | #define HASH_MD5 2 |
482 | extern int hash_fd(int fd, const off_t size, const uint8_t hash_algo, uint8_t *hashval); | 482 | extern int hash_fd(int fd, const size_t size, const uint8_t hash_algo, uint8_t *hashval); |
483 | 483 | ||
484 | 484 | ||
485 | #endif /* __LIBCONFIG_H__ */ | 485 | #endif /* __LIBCONFIG_H__ */ |
diff --git a/libbb/hash_fd.c b/libbb/hash_fd.c index ad0e4f446..770ba09c4 100644 --- a/libbb/hash_fd.c +++ b/libbb/hash_fd.c | |||
@@ -166,10 +166,11 @@ static void sha1_begin(sha1_ctx_t *ctx) | |||
166 | 166 | ||
167 | /* SHA1 hash data in an array of bytes into hash buffer and call the */ | 167 | /* SHA1 hash data in an array of bytes into hash buffer and call the */ |
168 | /* hash_compile function as required. */ | 168 | /* hash_compile function as required. */ |
169 | static void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx_t *ctx) | 169 | static void sha1_hash(const void *data, size_t len, void *ctx_v) |
170 | { | 170 | { |
171 | uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK), | 171 | sha1_ctx_t *ctx = (sha1_ctx_t *) ctx_v; |
172 | freeb = SHA1_BLOCK_SIZE - pos; | 172 | uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK); |
173 | uint32_t freeb = SHA1_BLOCK_SIZE - pos; | ||
173 | const unsigned char *sp = data; | 174 | const unsigned char *sp = data; |
174 | 175 | ||
175 | if ((ctx->count[0] += len) < len) | 176 | if ((ctx->count[0] += len) < len) |
@@ -706,6 +707,15 @@ static void md5_hash_bytes(const void *buffer, size_t len, md5_ctx_t *ctx) | |||
706 | } | 707 | } |
707 | } | 708 | } |
708 | 709 | ||
710 | static void md5_hash(const void *buffer, size_t length, void *md5_ctx) | ||
711 | { | ||
712 | if (length % 64 == 0) { | ||
713 | md5_hash_block(buffer, length, md5_ctx); | ||
714 | } else { | ||
715 | md5_hash_bytes(buffer, length, md5_ctx); | ||
716 | } | ||
717 | } | ||
718 | |||
709 | /* Process the remaining bytes in the buffer and put result from CTX | 719 | /* Process the remaining bytes in the buffer and put result from CTX |
710 | * in first 16 bytes following RESBUF. The result is always in little | 720 | * in first 16 bytes following RESBUF. The result is always in little |
711 | * endian byte order, so that a byte-wise output yields to the wanted | 721 | * endian byte order, so that a byte-wise output yields to the wanted |
@@ -760,13 +770,17 @@ static void *md5_end(void *resbuf, md5_ctx_t *ctx) | |||
760 | 770 | ||
761 | 771 | ||
762 | 772 | ||
763 | extern int hash_fd(int src_fd, const off_t size, const uint8_t hash_algo, | 773 | extern int hash_fd(int src_fd, const size_t size, const uint8_t hash_algo, |
764 | uint8_t * hashval) | 774 | uint8_t * hashval) |
765 | { | 775 | { |
766 | int result = EXIT_SUCCESS; | 776 | int result = EXIT_SUCCESS; |
767 | off_t hashed_count = 0; | 777 | // size_t hashed_count = 0; |
768 | unsigned int blocksize = 0; | 778 | size_t blocksize = 0; |
779 | size_t remaining = size; | ||
769 | unsigned char *buffer = NULL; | 780 | unsigned char *buffer = NULL; |
781 | void (*hash_fn_ptr)(const void *, size_t, void *) = NULL; | ||
782 | void *cx = NULL; | ||
783 | |||
770 | #ifdef CONFIG_SHA1SUM | 784 | #ifdef CONFIG_SHA1SUM |
771 | sha1_ctx_t sha1_cx; | 785 | sha1_ctx_t sha1_cx; |
772 | #endif | 786 | #endif |
@@ -779,13 +793,17 @@ extern int hash_fd(int src_fd, const off_t size, const uint8_t hash_algo, | |||
779 | if (hash_algo == HASH_SHA1) { | 793 | if (hash_algo == HASH_SHA1) { |
780 | /* Ensure that BLOCKSIZE is a multiple of 64. */ | 794 | /* Ensure that BLOCKSIZE is a multiple of 64. */ |
781 | blocksize = 65536; | 795 | blocksize = 65536; |
782 | buffer = malloc(blocksize); | 796 | buffer = xmalloc(blocksize); |
797 | hash_fn_ptr = sha1_hash; | ||
798 | cx = &sha1_cx; | ||
783 | } | 799 | } |
784 | #endif | 800 | #endif |
785 | #ifdef CONFIG_MD5SUM | 801 | #ifdef CONFIG_MD5SUM |
786 | if (hash_algo == HASH_MD5) { | 802 | if (hash_algo == HASH_MD5) { |
787 | blocksize = 4096; | 803 | blocksize = 4096; |
788 | buffer = malloc(blocksize + 72); | 804 | buffer = xmalloc(blocksize + 72); |
805 | hash_fn_ptr = md5_hash; | ||
806 | cx = &md5_cx; | ||
789 | } | 807 | } |
790 | #endif | 808 | #endif |
791 | 809 | ||
@@ -801,33 +819,29 @@ extern int hash_fd(int src_fd, const off_t size, const uint8_t hash_algo, | |||
801 | } | 819 | } |
802 | #endif | 820 | #endif |
803 | /* Iterate over full file contents. */ | 821 | /* Iterate over full file contents. */ |
804 | do { | 822 | while ((remaining == (size_t) -1) || (remaining > 0)) { |
805 | const ssize_t count = bb_full_read(src_fd, buffer, blocksize); | 823 | size_t read_try; |
806 | 824 | ssize_t read_got; | |
807 | if (count < 1) { | 825 | |
826 | if (remaining > blocksize) { | ||
827 | read_try = blocksize; | ||
828 | } else { | ||
829 | read_try = remaining; | ||
830 | } | ||
831 | read_got = bb_full_read(src_fd, buffer, read_try); | ||
832 | if (read_got < 1) { | ||
808 | /* count == 0 means short read | 833 | /* count == 0 means short read |
809 | * count == -1 means read error */ | 834 | * count == -1 means read error */ |
810 | result = count - 1; | 835 | result = read_got - 1; |
811 | break; | 836 | break; |
812 | } | 837 | } |
813 | hashed_count += count; | 838 | if (remaining != (size_t) -1) { |
839 | remaining -= read_got; | ||
840 | } | ||
814 | 841 | ||
815 | /* Process buffer */ | 842 | /* Process buffer */ |
816 | #ifdef CONFIG_SHA1SUM | 843 | hash_fn_ptr(buffer, read_got, cx); |
817 | if (hash_algo == HASH_SHA1) { | 844 | } |
818 | sha1_hash(buffer, count, &sha1_cx); | ||
819 | } | ||
820 | #endif | ||
821 | #ifdef CONFIG_MD5SUM | ||
822 | if (hash_algo == HASH_MD5) { | ||
823 | if (count % 64 == 0) { | ||
824 | md5_hash_block(buffer, count, &md5_cx); | ||
825 | } else { | ||
826 | md5_hash_bytes(buffer, count, &md5_cx); | ||
827 | } | ||
828 | } | ||
829 | #endif | ||
830 | } while ((size == (off_t) - 1) || (hashed_count < size)); | ||
831 | 845 | ||
832 | /* Finalize and write the hash into our buffer. */ | 846 | /* Finalize and write the hash into our buffer. */ |
833 | #ifdef CONFIG_SHA1SUM | 847 | #ifdef CONFIG_SHA1SUM |