diff options
author | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2011-10-08 16:41:56 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2011-10-12 08:52:49 +0200 |
commit | 05bd7afb3aa581366b41334be91dd01a185d803f (patch) | |
tree | aac8cc37a6f191860a7178c55e3cde9363d5e2c8 /commands/digest.c | |
parent | 4ac3a1f9e0d00530204eb81267c9a920e8b27b02 (diff) | |
download | barebox-05bd7afb3aa581366b41334be91dd01a185d803f.tar.gz barebox-05bd7afb3aa581366b41334be91dd01a185d803f.tar.xz |
digest: factorise file digest to common/digest.c
rename it to digest_file_window
introduce digest_file to digest a file and digest_file_by_name
where we specify the algo by name
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/digest.c')
-rw-r--r-- | commands/digest.c | 78 |
1 files changed, 16 insertions, 62 deletions
diff --git a/commands/digest.c b/commands/digest.c index 2a699e625c..1fbffb6ec9 100644 --- a/commands/digest.c +++ b/commands/digest.c @@ -29,71 +29,12 @@ #include <malloc.h> #include <digest.h> -static int file_digest(struct digest *d, char *filename, - ulong start, ulong size) -{ - ulong len = 0; - int fd, now, i, ret = 0; - unsigned char *buf; - - d->init(d); - - fd = open(filename, O_RDONLY); - if (fd < 0) { - perror(filename); - return fd; - } - - if (start > 0) { - ret = lseek(fd, start, SEEK_SET); - if (ret == -1) { - perror("lseek"); - goto out; - } - } - - buf = xmalloc(4096); - - while (size) { - now = min((ulong)4096, size); - now = read(fd, buf, now); - if (now < 0) { - ret = now; - perror("read"); - goto out_free; - } - if (!now) - break; - - if (ctrlc()) { - ret = -EINTR; - goto out_free; - } - - d->update(d, buf, now); - size -= now; - len += now; - } - - d->final(d, buf); - - for (i = 0; i < d->length; i++) - printf("%02x", buf[i]); - - printf(" %s\t0x%08lx ... 0x%08lx\n", filename, start, start + len); - -out_free: - free(buf); -out: - close(fd); - - return ret; -} - static int do_digest(char *algorithm, int argc, char *argv[]) { struct digest *d; int ret = 0; + int i; + unsigned char *hash; d = digest_get_by_name(algorithm); BUG_ON(!d); @@ -101,6 +42,12 @@ static int do_digest(char *algorithm, int argc, char *argv[]) if (argc < 2) return COMMAND_ERROR_USAGE; + hash = calloc(d->length, sizeof(unsigned char)); + if (!hash) { + perror("calloc"); + return COMMAND_ERROR_USAGE; + } + argv++; while (*argv) { char *filename = "/dev/mem"; @@ -113,12 +60,19 @@ static int do_digest(char *algorithm, int argc, char *argv[]) argv++; } - if (file_digest(d, filename, start, size) < 0) + if (digest_file_window(d, filename, hash, start, size) < 0) ret = 1; + for (i = 0; i < d->length; i++) + printf("%02x", hash[i]); + + printf(" %s\t0x%08lx ... 0x%08lx\n", filename, start, start + size); + argv++; } + free(hash); + return ret; } |