Fix bug in Named CRC lookup

This commit is contained in:
Mario Hüttel 2022-09-06 22:26:07 +02:00
parent 8f19470db3
commit ea00a0756e
2 changed files with 14 additions and 2 deletions

12
main.c
View File

@ -69,6 +69,7 @@ struct command_line_options {
static error_t parse_opt(int key, char *arg, struct argp_state *state) static error_t parse_opt(int key, char *arg, struct argp_state *state)
{ {
struct command_line_options *args = (struct command_line_options *)state->input; struct command_line_options *args = (struct command_line_options *)state->input;
const struct named_crc *looked_up_crc;
char *endptr; char *endptr;
switch (key) { switch (key) {
@ -91,8 +92,12 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
/* Polyniomial */ /* Polyniomial */
args->crc.polynomial = strtoull(arg, &endptr, 0); args->crc.polynomial = strtoull(arg, &endptr, 0);
if (endptr == arg) { if (endptr == arg) {
if ((looked_up_crc = lookup_named_crc(arg))) {
memcpy(&args->crc, &looked_up_crc->settings, sizeof(struct crc_settings));
} else {
argp_error(state, "Error parsing polynomial: %s\n", arg); argp_error(state, "Error parsing polynomial: %s\n", arg);
} }
}
break; break;
case 'l': case 'l':
args->little_endian = true; args->little_endian = true;
@ -246,6 +251,13 @@ int main(int argc, char **argv)
/* Build the CRC */ /* Build the CRC */
crc_init(&crc, &cmd_opts.crc); crc_init(&crc, &cmd_opts.crc);
/* Perform the check test */
crc_push_bytes(&crc, "123456789", 9u);
crc_finish_calc(&crc);
printf("CRC Check value: 0x%08x\n", crc_get_value(&crc));
crc_destroy(&crc);
free_cmds: free_cmds:
free_cmd_args(&cmd_opts); free_cmd_args(&cmd_opts);

View File

@ -97,7 +97,7 @@ const struct named_crc *lookup_named_crc(const char *name)
const struct named_crc *found = NULL; const struct named_crc *found = NULL;
for (iter = predefined_crc_table; iter->name; iter++) { for (iter = predefined_crc_table; iter->name; iter++) {
if (strcmp(iter->name, name)) { if (!strcmp(iter->name, name)) {
found = iter; found = iter;
break; break;
} }