From 5cbd5e92b878dfe1aa6c9623b21c38030c23b5ff Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Jun 2017 23:47:10 +0200 Subject: image: Add new function image_alloc_from_fd() --- src/image.c | 33 ++++++++++++++++++++++----------- src/image.h | 1 + 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/image.c b/src/image.c index 43478f6..b78633b 100644 --- a/src/image.c +++ b/src/image.c @@ -246,22 +246,33 @@ static struct image * image_alloc(void) { struct image * image_alloc_from_file(const char * file, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { + int fd; + + fd = open(file, O_RDONLY); + if ( fd < 0 ) { + ERROR_INFO("Cannot open image file %s", file); + return NULL; + } + + return image_alloc_from_fd(fd, file, type, device, hwrevs, version, layout); + +} + +struct image * image_alloc_from_fd(int fd, const char * orig_filename, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { + off_t offset; struct image * image = image_alloc(); - if ( ! image ) + if ( ! image ) { + close(fd); return NULL; + } image->is_shared_fd = 0; - image->fd = open(file, O_RDONLY); - if ( image->fd < 0 ) { - ERROR_INFO("Cannot open image file %s", file); - free(image); - return NULL; - } + image->fd = fd; offset = lseek(image->fd, 0, SEEK_END); if ( offset == (off_t)-1 ) { - ERROR_INFO("Cannot seek to end of file %s", file); + ERROR_INFO("Cannot seek to end of file %s", orig_filename); close(image->fd); free(image); return NULL; @@ -270,10 +281,10 @@ struct image * image_alloc_from_file(const char * file, const char * type, const image->size = offset; image->offset = 0; image->cur = 0; - image->orig_filename = strdup(file); + image->orig_filename = strdup(orig_filename); if ( lseek(image->fd, 0, SEEK_SET) == (off_t)-1 ) { - ERROR_INFO("Cannot seek to begin of file %s", file); + ERROR_INFO("Cannot seek to begin of file %s", orig_filename); close(image->fd); free(image->orig_filename); free(image); @@ -284,7 +295,7 @@ struct image * image_alloc_from_file(const char * file, const char * type, const return NULL; if ( ( ! type || ! type[0] ) && ( ! device || ! device[0] ) && ( ! hwrevs || ! hwrevs[0] ) && ( ! version || ! version[0] ) ) - image_missing_values_from_name(image, file); + image_missing_values_from_name(image, orig_filename); image_align(image); diff --git a/src/image.h b/src/image.h index 709fef7..42111bf 100644 --- a/src/image.h +++ b/src/image.h @@ -64,6 +64,7 @@ struct image_list { }; struct image * image_alloc_from_file(const char * file, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout); +struct image * image_alloc_from_fd(int fd, const char * orig_filename, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout); struct image * image_alloc_from_shared_fd(int fd, size_t size, size_t offset, uint16_t hash, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout); void image_free(struct image * image); void image_seek(struct image * image, size_t whence); -- cgit v1.2.3