comparison simtool/a38.c @ 152:250d172662ca

fc-simtool a38 command: use generalized hex string parsing function
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 00:23:06 +0000
parents 141489d31667
children
comparison
equal deleted inserted replaced
151:d515cfbb3f39 152:250d172662ca
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <pcsclite.h> 10 #include <pcsclite.h>
11 #include <winscard.h> 11 #include <winscard.h>
12 #include "globals.h" 12 #include "globals.h"
13 13
14 static
15 hexarg_16bytes(arg, databuf)
16 char *arg;
17 u_char *databuf;
18 {
19 unsigned count;
20
21 for (count = 0; ; count++) {
22 while (isspace(*arg))
23 arg++;
24 if (!*arg)
25 break;
26 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
27 fprintf(stderr, "error: invalid hex string input\n");
28 return(-1);
29 }
30 if (count >= 16) {
31 fprintf(stderr,
32 "error: hex string is longer than required 16 bytes\n");
33 return(-1);
34 }
35 databuf[count] = (decode_hex_digit(arg[0]) << 4) |
36 decode_hex_digit(arg[1]);
37 arg += 2;
38 }
39 if (count < 16) {
40 fprintf(stderr,
41 "error: hex string is shorter than required 16 bytes\n");
42 return(-1);
43 }
44 return(0);
45 }
46
47 cmd_a38(argc, argv) 14 cmd_a38(argc, argv)
48 char **argv; 15 char **argv;
49 { 16 {
50 u_char cmd[21]; 17 u_char cmd[21];
51 int rc; 18 int rc;
54 cmd[0] = 0xA0; 21 cmd[0] = 0xA0;
55 cmd[1] = 0x88; 22 cmd[1] = 0x88;
56 cmd[2] = 0; 23 cmd[2] = 0;
57 cmd[3] = 0; 24 cmd[3] = 0;
58 cmd[4] = 16; 25 cmd[4] = 16;
59 rc = hexarg_16bytes(argv[1], cmd + 5); 26 rc = decode_hex_data_from_string(argv[1], cmd + 5, 16, 16);
60 if (rc < 0) 27 if (rc < 0)
61 return(rc); 28 return(rc);
62 rc = apdu_exchange(cmd, 21); 29 rc = apdu_exchange(cmd, 21);
63 if (rc < 0) 30 if (rc < 0)
64 return(rc); 31 return(rc);