image: Unconditionally parse PE/COFF data

Rather than requiring an explicit image_pecoff_parse, do it
unconditionally in image_load. We don't have any instances where we need
to do this separately.

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
Jeremy Kerr 2012-06-13 17:39:34 +08:00
parent 186f1d59d9
commit 376974e386
5 changed files with 51 additions and 52 deletions

97
image.c
View file

@ -34,51 +34,6 @@
#define DATA_DIR_CERT_TABLE 4
struct image *image_load(const char *filename)
{
struct stat statbuf;
struct image *image;
int rc;
image = talloc(NULL, struct image);
if (!image) {
perror("talloc(image)");
return NULL;
}
image->fd = open(filename, O_RDONLY);
if (image->fd < 0) {
perror("open");
goto err;
}
rc = fstat(image->fd, &statbuf);
if (rc) {
perror("fstat");
goto err;
}
image->size = statbuf.st_size;
image->buf = talloc_size(image, image->size);
if (!image->buf) {
perror("talloc(buf)");
goto err;
}
if (!read_all(image->fd, image->buf, image->size)) {
perror("read_all");
fprintf(stderr, "error reading input file\n");
goto err;
}
lseek(image->fd, 0, SEEK_SET);
return image;
err:
talloc_free(image);
return NULL;
}
/**
* The PE/COFF headers export struct fields as arrays of chars. So, define
* a couple of accessor functions that allow fields to be deferenced as their
@ -105,7 +60,7 @@ static uint16_t __pehdr_u16(char field[])
#define pehdr_u32(f) __pehdr_u32(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 4))
#define pehdr_u16(f) __pehdr_u16(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 2))
int image_pecoff_parse(struct image *image)
static int image_pecoff_parse(struct image *image)
{
char nt_sig[] = {'P', 'E', 0, 0};
size_t size = image->size;
@ -184,6 +139,56 @@ int image_pecoff_parse(struct image *image)
return 0;
}
struct image *image_load(const char *filename)
{
struct stat statbuf;
struct image *image;
int rc;
image = talloc(NULL, struct image);
if (!image) {
perror("talloc(image)");
return NULL;
}
image->fd = open(filename, O_RDONLY);
if (image->fd < 0) {
perror("open");
goto err;
}
rc = fstat(image->fd, &statbuf);
if (rc) {
perror("fstat");
goto err;
}
image->size = statbuf.st_size;
image->buf = talloc_size(image, image->size);
if (!image->buf) {
perror("talloc(buf)");
goto err;
}
if (!read_all(image->fd, image->buf, image->size)) {
perror("read_all");
fprintf(stderr, "error reading input file\n");
goto err;
}
lseek(image->fd, 0, SEEK_SET);
rc = image_pecoff_parse(image);
if (rc)
goto err;
return image;
err:
talloc_free(image);
return NULL;
}
static int align_up(int size, int align)
{
return (size + align - 1) & ~(align - 1);

View file

@ -77,7 +77,6 @@ struct cert_table_header {
struct image *image_load(const char *filename);
int image_pecoff_parse(struct image *image);
int image_find_regions(struct image *image);
int image_hash_sha256(struct image *image, uint8_t digest[]);
int image_write(struct image *image, const char *filename);

View file

@ -214,8 +214,6 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
image_pecoff_parse(image);
rc = 0;
if (action == ACTION_ATTACH)

View file

@ -166,8 +166,6 @@ int main(int argc, char **argv)
talloc_steal(ctx, ctx->image);
image_pecoff_parse(ctx->image);
image_find_regions(ctx->image);
ERR_load_crypto_strings();

View file

@ -239,7 +239,6 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
image_pecoff_parse(image);
image_find_regions(image);
if (detached_sig_filename)