From 1a7d30cebf76ff9ed057d05c03afabbaf36ebff6 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 7 Aug 2012 20:10:48 +0200 Subject: image: Implement image_hash_from_data --- src/image.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/image.c') diff --git a/src/image.c b/src/image.c index c59577a..224468f 100644 --- a/src/image.c +++ b/src/image.c @@ -223,9 +223,8 @@ struct image * image_alloc_from_shared_fd(int fd, size_t size, size_t offset, ui if ( image->hash != hash ) { fprintf(stderr, "Error: Image hash mishmash (expected %#04x)\n", image->hash); -// image_free(image); -// return NULL; - image->hash = hash; + image_free(image); + return NULL; } return image; @@ -352,12 +351,27 @@ void image_list_del(struct image_list * list) { } +static uint16_t do_hash(uint16_t * b, int len) { + + uint16_t result = 0; + + for ( len >>= 1; len--; b = b+1 ) + result^=b[0]; + + return result; + +} uint16_t image_hash_from_data(struct image * image) { - /* TODO */ - printf("image_hash_from_data is not implemented\n"); + unsigned char buf[0x20000]; + uint16_t hash = 0; + int ret; + + while ( ( ret = image_read(image, &buf, sizeof(buf)) ) ) + hash ^= do_hash((uint16_t *)&buf, ret); + return hash; } static const char * image_types[] = { -- cgit v1.2.3