annotate libcommon/cardconnect.c @ 32:b37fcb235848

recent libcommon/cardconnect.c change: missed SCardReleaseContext() on error
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 17:22:25 +0000
parents 84d1c31d0fad
children 8a4f3d00d997
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <string.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <strings.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdio.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <stdlib.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <pcsclite.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <winscard.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 SCARDCONTEXT hContext;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 SCARDHANDLE hCard;
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
10 char *reader_list, *selected_reader;
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 setup_pcsc_context()
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 fprintf(stderr, "SCardEstablishContext: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 return(0);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
25 get_reader_list()
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 DWORD dwReaders;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 fprintf(stderr, "SCardListReaders 1st call: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (dwReaders < 1) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 fprintf(stderr,
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 "error: dwReaders returned by SCardListReaders() is less than 1\n");
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
43 reader_list = malloc(dwReaders);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
44 if (!reader_list) {
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 perror("malloc for readers list");
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
49 reader_list[0] = '\0';
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
50 rv = SCardListReaders(hContext, NULL, reader_list, &dwReaders);
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
57 return(0);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
58 }
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
59
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
60 select_reader_num(select_num)
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
61 unsigned select_num;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
62 {
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
63 char *cp;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
64 unsigned num;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
65
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
66 for (cp = reader_list, num = 0; *cp; num++) {
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
67 if (num == select_num) {
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
68 selected_reader = cp;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
69 return(0);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
70 }
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
71 cp += strlen(cp) + 1;
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
73 fprintf(stderr,
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
74 "error: requested reader #%u, but only %u readers found\n",
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
75 select_num, num);
32
b37fcb235848 recent libcommon/cardconnect.c change: missed
Mychaela Falconia <falcon@freecalypso.org>
parents: 30
diff changeset
76 SCardReleaseContext(hContext);
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
77 exit(1);
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 connect_to_card()
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 DWORD dwActiveProtocol;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
85 rv = SCardConnect(hContext, selected_reader, SCARD_SHARE_EXCLUSIVE,
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 return(0);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }