Add option to list predefined CRCs

This commit is contained in:
Mario Hüttel 2022-08-19 22:16:11 +02:00
parent 3040d8a23f
commit 9b803a887e
4 changed files with 46 additions and 4 deletions

View File

@ -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")

View File

@ -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_ */

12
main.c
View File

@ -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;
}

View File

@ -1,6 +1,8 @@
#include <patchelfcrc/named_crcs.h>
#include <stddef.h>
#include <string.h>
#include <fort.h>
#include <stdio.h>
#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);
}