annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
1 #include <sys/types.h>
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
2 #include <ctype.h>
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdio.h>
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
5 static
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
6 decode_hex_digit(c)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
7 {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
8 if (isdigit(c))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
9 return c - '0';
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
10 else if (islower(c))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
11 return c - 'a' + 10;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
12 else
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
13 return c - 'A' + 10;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
14 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
15
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
16 parse_hex_input(inbuf, outbuf)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
17 char *inbuf;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
18 u_char *outbuf;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
19 {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
20 char *cp;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
21 unsigned count;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
22
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
23 count = 0;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
24 for (cp = inbuf; ; ) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
25 while (isspace(*cp))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
26 cp++;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
27 if (!*cp)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
28 break;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
29 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
30 printf("error: invalid hex APDU input\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
31 return(-1);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
32 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
33 if (count >= 260) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
34 printf("error: command APDU is too long\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
35 return(-1);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
36 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
37 outbuf[count++] = (decode_hex_digit(cp[0]) << 4) |
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
38 decode_hex_digit(cp[1]);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
39 cp += 2;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
40 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
41 return count;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
42 }