diff options
Diffstat (limited to 'futility/file_type.c')
-rw-r--r-- | futility/file_type.c | 88 |
1 files changed, 36 insertions, 52 deletions
diff --git a/futility/file_type.c b/futility/file_type.c index 11b6dda3..aa017138 100644 --- a/futility/file_type.c +++ b/futility/file_type.c @@ -18,45 +18,53 @@ #include "futility.h" #include "gbb_header.h" -/* Human-readable strings */ -static const struct { - const char * const name; - const char * const desc; -} type_strings[] = { -#define FILE_TYPE(A, B, C) {B, C}, +/* Description and functions to handle each file type */ +struct futil_file_type_s { + /* Short name for this type */ + const char *name; + /* Human-readable description */ + const char *desc; + /* Functions to identify, display, and sign this type of file. */ + enum futil_file_type (*recognize)(uint8_t *buf, uint32_t len); + int (*show)(const char *name, uint8_t *buf, uint32_t len, void *data); + int (*sign)(const char *name, uint8_t *buf, uint32_t len, void *data); +}; + +/* Populate a list of file types and operator functions. */ +static const struct futil_file_type_s const futil_file_types[] = { + {"unknown", "not something we know about", 0, 0, 0}, +#define R_(x) x +#define S_(x) x +#define NONE 0 +#define FILE_TYPE(A, B, C, D, E, F) {B, C, D, E, F}, #include "file_type.inc" #undef FILE_TYPE +#undef NONE +#undef S_ +#undef R_ }; const char * const futil_file_type_name(enum futil_file_type type) { - if ((int) type < 0 || type >= NUM_FILE_TYPES) - type = FILE_TYPE_UNKNOWN; - - return type_strings[type].name; + return futil_file_types[type].name; } const char * const futil_file_type_desc(enum futil_file_type type) { - if ((int) type < 0 || type >= NUM_FILE_TYPES) - type = FILE_TYPE_UNKNOWN; - - return type_strings[type].desc; + return futil_file_types[type].desc; } /* Name to enum. Returns true on success. */ -int futil_file_str_to_type(const char *str, enum futil_file_type *tptr) +int futil_str_to_file_type(const char *str, enum futil_file_type *type) { int i; for (i = 0; i < NUM_FILE_TYPES; i++) - if (!strcasecmp(str, type_strings[i].name)) { - if (tptr) - *tptr = i; + if (!strcasecmp(str, futil_file_types[i].name)) { + *type = i; return 1; } - if (tptr) - *tptr = FILE_TYPE_UNKNOWN; + *type = FILE_TYPE_UNKNOWN; return 0; } @@ -66,49 +74,25 @@ void print_file_types_and_exit(int retval) int i; printf("\nValid file types are:\n\n"); for (i = 0; i < NUM_FILE_TYPES; i++) - printf(" %-20s%s\n", type_strings[i].name, - type_strings[i].desc); + printf(" %-20s%s\n", futil_file_types[i].name, + futil_file_types[i].desc); printf("\n"); exit(retval); } -/* Try these in order so we recognize the larger objects first */ -enum futil_file_type (*recognizers[])(uint8_t *buf, uint32_t len) = { - &recognize_gpt, - &recognize_bios_image, - &recognize_gbb, - &recognize_vblock1, - &recognize_vb1_key, - &recognize_vb2_key, - &recognize_pem, -}; - -int futil_str_to_file_type(const char *str, enum futil_file_type *type) -{ - int i; - for (i = 0; i < NUM_FILE_TYPES; i++) - if (!strcasecmp(str, type_strings[i].name)) - break; - if (i < NUM_FILE_TYPES) { - *type = i; - return 1; - } - - return 0; -} - - /* Try to figure out what we're looking at */ enum futil_file_type futil_file_type_buf(uint8_t *buf, uint32_t len) { enum futil_file_type type; int i; - for (i = 0; i < ARRAY_SIZE(recognizers); i++) { - type = recognizers[i](buf, len); - if (type != FILE_TYPE_UNKNOWN) - return type; + for (i = 0; i < NUM_FILE_TYPES; i++) { + if (futil_file_types[i].recognize) { + type = futil_file_types[i].recognize(buf, len); + if (type != FILE_TYPE_UNKNOWN) + return type; + } } return FILE_TYPE_UNKNOWN; |