From 9b803a887e224bcb53943a8f33f2c641050ba7b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 19 Aug 2022 22:16:11 +0200 Subject: [PATCH] Add option to list predefined CRCs --- CMakeLists.txt | 5 ++++- include/patchelfcrc/named_crcs.h | 2 ++ main.c | 12 ++++++++++++ named_crcs.c | 31 ++++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31299e3..cb9e4ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,8 +23,11 @@ add_custom_target( add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter) +set(FORT_ENABLE_TESTING OFF CACHE INTERNAL "") +add_subdirectory(3rdparty/libfort) + add_executable(${PROJECT_NAME} ${CFILES}) -target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES} fort) target_link_directories(${PROJECT_NAME} PRIVATE ${ELF_LIBRARY_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE ${ELF_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include") diff --git a/include/patchelfcrc/named_crcs.h b/include/patchelfcrc/named_crcs.h index 33c52a2..6c151e0 100644 --- a/include/patchelfcrc/named_crcs.h +++ b/include/patchelfcrc/named_crcs.h @@ -20,4 +20,6 @@ const struct named_crc *reverse_lookup_named_crc(const struct crc_settings *sett const struct named_crc *lookup_named_crc(const char *name); +void list_predefined_crcs(void); + #endif /* _NAMED_CRCS_H_ */ diff --git a/main.c b/main.c index dc8578c..d3366cd 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,7 @@ enum crc_format { #define ARG_KEY_DRY_RUN (1) #define ARG_KEY_START_MAGIC (2) #define ARG_KEY_END_MAGIC (3) +#define ARG_KEY_LIST (4) struct command_line_options { bool little_endian; @@ -42,6 +43,7 @@ struct command_line_options { uint32_t start_magic; bool has_end_magic; uint32_t end_magic; + bool list; }; static error_t parse_opt(int key, char *arg, struct argp_state *state) @@ -60,6 +62,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) args->has_end_magic = true; args->end_magic = strtoul(arg, NULL, 0); break; + case ARG_KEY_LIST: + args->list = true; + break; case 'l': args->little_endian = true; break; @@ -104,6 +109,7 @@ static int parse_cmdline_options(int *argc, char ***argv, struct command_line_op {"crc-format", 'F', "FORMAT", 0, "Output Format for CRCs.", 2}, {"start-magic", ARG_KEY_START_MAGIC, "STARTMAGIC", 0, "Check output section for start magic (uint32)", 2}, {"end-magic", ARG_KEY_END_MAGIC, "STARTMAGIC", 0, "Check output section for start magic (uint32)", 2}, + {"list-crcs", ARG_KEY_LIST, 0, 0 , "List predefined CRCs", 0}, /* Sentinel */ {NULL, 0, 0, 0, NULL, 0} }; @@ -134,6 +140,7 @@ static void prepare_default_opts(struct command_line_options *opts) opts->format = FORMAT_BARE; opts->has_end_magic = false; opts->has_start_magic = false; + opts->list = false; } static void print_verbose_start_info(const struct command_line_options *cmd_opts) @@ -168,5 +175,10 @@ int main(int argc, char **argv) verbose = cmd_opts.verbose || cmd_opts.dry_run; print_verbose_start_info(&cmd_opts); + if (cmd_opts.list) { + list_predefined_crcs(); + return 0; + } + return 0; } diff --git a/named_crcs.c b/named_crcs.c index 253a4f3..1f9aae1 100644 --- a/named_crcs.c +++ b/named_crcs.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #define NAMED_CRC(crc_name, poly, reverse, init, outxor) { \ .name = crc_name, \ @@ -35,9 +37,9 @@ const struct named_crc predefined_crc_table[] = { NAMED_CRC("x-25", 0x1021, true, 0x0000, 0xFFFF), NAMED_CRC("xmodem", 0x1021, false, 0x0000, 0x0000), NAMED_CRC("modbus", 0x8005, true, 0xFFFF, 0x0000), - NAMED_CRC("kermit [1]", 0x1021, true, 0x0000, 0x0000), - NAMED_CRC("crc-ccitt-false [1]", 0x1021, false, 0xFFFF, 0x0000), - NAMED_CRC("crc-aug-ccitt [1]", 0x1021, false, 0x1D0F, 0x0000), + NAMED_CRC("kermit", 0x1021, true, 0x0000, 0x0000), + NAMED_CRC("crc-ccitt-false", 0x1021, false, 0xFFFF, 0x0000), + NAMED_CRC("crc-aug-ccitt", 0x1021, false, 0x1D0F, 0x0000), NAMED_CRC("crc-24", 0x864CFB, false, 0xB704CE, 0x000000), NAMED_CRC("crc-24-flexray-a", 0x5D6DCB, false, 0xFEDCBA, 0x000000), NAMED_CRC("crc-24-flexray-b", 0x5D6DCB, false, 0xABCDEF, 0x000000), @@ -86,3 +88,26 @@ const struct named_crc *lookup_named_crc(const char *name) return found; } + +void list_predefined_crcs(void) +{ + ft_table_t *table; + const struct named_crc *iter; + + table = ft_create_table(); + + ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); + ft_write_ln(table, "Name", "Polynomial", "Reversed", "Start Value", "Output XOR"); + + for (iter = predefined_crc_table; iter->name; iter++) { + ft_printf_ln(table, "%s|0x%x|%s|0x%x|0x%x", + iter->name, + iter->settings.polynomial, + iter->settings.rev ? "yes" : "no", + iter->settings.start_value, + iter->settings.xor); + } + + printf("%s\n", ft_to_string(table)); + ft_destroy_table(table); +}