2 PCSCv2part10.c: helper functions for PC/SC v2 part 10 services
3 Copyright (C) 2012 Ludovic Rousseau
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <arpa/inet.h>
24 #include <PCSC/winscard.h>
25 #include <PCSC/wintypes.h>
30 #include "PCSCv2part10.h"
32 int PCSCv2Part10_find_TLV_property_by_tag_from_buffer(
33 unsigned char *buffer, int length, int property, int * value_int)
38 int ret = -1; /* not found by default */
41 while (p-buffer < length)
65 value = *p + (*(p+1)<<8);
68 value = *p + (*(p+1)<<8) + (*(p+2)<<16) + (*(p+3)<<24);
71 /* wrong length for an integer */
80 } /* PCSCv2Part10_find_TLV_property_by_tag_from_buffer */
82 int PCSCv2Part10_find_TLV_property_by_tag_from_hcard(SCARDHANDLE hCard,
83 int property, int * value)
85 unsigned char buffer[MAX_BUFFER_SIZE];
89 PCSC_TLV_STRUCTURE *pcsc_tlv;
90 DWORD properties_in_tlv_ioctl;
93 rv = SCardControl(hCard, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0,
94 buffer, sizeof buffer, &length);
95 if (rv != SCARD_S_SUCCESS)
98 /* get the number of elements instead of the complete size */
99 length /= sizeof(PCSC_TLV_STRUCTURE);
101 pcsc_tlv = (PCSC_TLV_STRUCTURE *)buffer;
103 for (i = 0; i < length; i++)
105 if (FEATURE_GET_TLV_PROPERTIES == pcsc_tlv[i].tag)
107 properties_in_tlv_ioctl = ntohl(pcsc_tlv[i].value);
115 rv= SCardControl(hCard, properties_in_tlv_ioctl, NULL, 0,
116 buffer, sizeof buffer, &length);
117 if (rv != SCARD_S_SUCCESS)
120 return PCSCv2Part10_find_TLV_property_by_tag_from_buffer(buffer,
121 length, property, value);