FreeCalypso > hg > fc-sim-tools
diff calypso/main.c @ 4:deeeef558279
fcsim-calypso-be put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 14 Mar 2021 05:07:34 +0000 |
parents | |
children | f6b03af63bf7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/calypso/main.c Sun Mar 14 05:07:34 2021 +0000 @@ -0,0 +1,104 @@ +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +static +is_string_all_hex(str) + char *str; +{ + char *cp; + + for (cp = str; *cp; cp++) + if (!isxdigit(*cp)) + return(0); + return(1); +} + +cmd_exchange(input) + char *input; +{ + char *targv[3]; + int rc; + + targv[0] = "X"; + targv[1] = input; + targv[2] = 0; + + tpinterf_make_cmd(targv); + rc = tpinterf_send_cmd(); + if (rc < 0) + return(rc); + return tpinterf_pass_output(20); +} + +cmd_atr() +{ + static char *atr_argv[2] = {"atr", 0}; + int rc; + + tpinterf_make_cmd(atr_argv); + rc = tpinterf_send_cmd(); + if (rc < 0) + return(rc); + return tpinterf_pass_output(1); +} + +cmd_poweroff() +{ + static char *poweroff_argv[2] = {"poweroff", 0}; + + tpinterf_make_cmd(poweroff_argv); + tpinterf_send_cmd(); +} + +main(argc, argv) + char **argv; +{ + char inbuf[576], *cp; + unsigned len; + + parse_target_fd_opt(argc, argv); + putchar('\n'); + + while (fgets(inbuf, sizeof inbuf, stdin)) { + cp = index(inbuf, '\n'); + if (!cp) { + printf("back end error: missing newline on input\n"); + continue; + } + *cp = '\0'; + if (!strcmp(inbuf, "atr")) { + cmd_atr(); + continue; + } + if (!strcmp(inbuf, "poweroff")) { + cmd_poweroff(); + exit(0); + } + if (!is_string_all_hex(inbuf)) { + printf("back end error: input is not all hex\n"); + continue; + } + len = strlen(inbuf); + if (len & 1) { + printf( + "back end error: input has odd number of hex digits\n"); + continue; + } + if (len < 10) { + printf( + "back end error: input is too short for command APDU\n"); + continue; + } + if (len > 520) { + printf( + "back end error: input is too long for command APDU\n"); + continue; + } + cmd_exchange(inbuf); + } + + exit(0); +}