FreeCalypso > hg > fc-sim-tools
comparison serial/hexinput.c @ 43:be27d1c85861
serial: main function implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 20 Mar 2021 21:49:59 +0000 |
parents | pcsc/main.c@60fd23186e2e |
children |
comparison
equal
deleted
inserted
replaced
42:6cc3eea720cb | 43:be27d1c85861 |
---|---|
1 #include <sys/types.h> | |
2 #include <ctype.h> | |
3 #include <stdio.h> | |
4 | |
5 static | |
6 decode_hex_digit(c) | |
7 { | |
8 if (isdigit(c)) | |
9 return c - '0'; | |
10 else if (islower(c)) | |
11 return c - 'a' + 10; | |
12 else | |
13 return c - 'A' + 10; | |
14 } | |
15 | |
16 parse_hex_input(inbuf, outbuf) | |
17 char *inbuf; | |
18 u_char *outbuf; | |
19 { | |
20 char *cp; | |
21 unsigned count; | |
22 | |
23 count = 0; | |
24 for (cp = inbuf; ; ) { | |
25 while (isspace(*cp)) | |
26 cp++; | |
27 if (!*cp) | |
28 break; | |
29 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) { | |
30 printf("error: invalid hex APDU input\n"); | |
31 return(-1); | |
32 } | |
33 if (count >= 260) { | |
34 printf("error: command APDU is too long\n"); | |
35 return(-1); | |
36 } | |
37 outbuf[count++] = (decode_hex_digit(cp[0]) << 4) | | |
38 decode_hex_digit(cp[1]); | |
39 cp += 2; | |
40 } | |
41 return count; | |
42 } |