summaryrefslogtreecommitdiffhomepage
path: root/libtomcrypt/src/hashes/helper/hash_filehandle.c
diff options
context:
space:
mode:
Diffstat (limited to 'libtomcrypt/src/hashes/helper/hash_filehandle.c')
-rw-r--r--libtomcrypt/src/hashes/helper/hash_filehandle.c48
1 files changed, 25 insertions, 23 deletions
diff --git a/libtomcrypt/src/hashes/helper/hash_filehandle.c b/libtomcrypt/src/hashes/helper/hash_filehandle.c
index af8164a..1d72f25 100644
--- a/libtomcrypt/src/hashes/helper/hash_filehandle.c
+++ b/libtomcrypt/src/hashes/helper/hash_filehandle.c
@@ -5,11 +5,10 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
+#ifndef LTC_NO_FILE
/**
@file hash_filehandle.c
Hash open files, Tom St Denis
@@ -25,12 +24,8 @@
*/
int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
{
-#ifdef LTC_NO_FILE
- (void)hash; (void)in; (void)out; (void)outlen;
- return CRYPT_NOP;
-#else
hash_state md;
- unsigned char buf[512];
+ unsigned char *buf;
size_t x;
int err;
@@ -38,35 +33,42 @@ int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outle
LTC_ARGCHK(outlen != NULL);
LTC_ARGCHK(in != NULL);
+ if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+ return CRYPT_MEM;
+ }
+
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
- return err;
+ goto LBL_ERR;
}
if (*outlen < hash_descriptor[hash].hashsize) {
*outlen = hash_descriptor[hash].hashsize;
- return CRYPT_BUFFER_OVERFLOW;
+ err = CRYPT_BUFFER_OVERFLOW;
+ goto LBL_ERR;
}
if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
- return err;
+ goto LBL_ERR;
}
- *outlen = hash_descriptor[hash].hashsize;
do {
- x = fread(buf, 1, sizeof(buf), in);
- if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) {
- return err;
+ x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
+ if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
+ goto LBL_CLEANBUF;
+ }
+ } while (x == LTC_FILE_READ_BUFSIZE);
+ if ((err = hash_descriptor[hash].done(&md, out)) == CRYPT_OK) {
+ *outlen = hash_descriptor[hash].hashsize;
}
- } while (x == sizeof(buf));
- err = hash_descriptor[hash].done(&md, out);
-#ifdef LTC_CLEAN_STACK
- zeromem(buf, sizeof(buf));
-#endif
+LBL_CLEANBUF:
+ zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
+ XFREE(buf);
return err;
-#endif
}
+#endif /* #ifndef LTC_NO_FILE */
-/* $Source$ */
-/* $Revision$ */
-/* $Date$ */
+/* ref: $Format:%D$ */
+/* git commit: $Format:%H$ */
+/* commit time: $Format:%ai$ */