#include <stdio.h>
#include <arpa/inet.h>
#ifdef __APPLE__
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#else
#include <winscard.h>
#endif
#include "PCSCv2part10.h"
int PCSCv2Part10_find_TLV_property_by_tag_from_buffer(
unsigned char *buffer, int length, int property, int * value_int)
{
unsigned char *p;
int found = 0, len;
int value = -1;
int ret = -1;
p = buffer;
while (p-buffer < length)
{
if (*p++ == property)
{
found = 1;
break;
}
len = *p++;
p += len;
}
if (found)
{
len = *p++;
ret = 0;
switch(len)
{
case 1:
value = *p;
break;
case 2:
value = *p + (*(p+1)<<8);
break;
case 4:
value = *p + (*(p+1)<<8) + (*(p+2)<<16) + (*(p+3)<<24);
break;
default:
ret = -2;
}
}
if (value_int)
*value_int = value;
return ret;
}
int PCSCv2Part10_find_TLV_property_by_tag_from_hcard(SCARDHANDLE hCard,
int property, int * value)
{
unsigned char buffer[MAX_BUFFER_SIZE];
LONG rv;
DWORD length;
unsigned int i;
PCSC_TLV_STRUCTURE *pcsc_tlv;
DWORD properties_in_tlv_ioctl;
int found;
rv = SCardControl(hCard, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0,
buffer, sizeof buffer, &length);
if (rv != SCARD_S_SUCCESS)
return -1;
length /= sizeof(PCSC_TLV_STRUCTURE);
pcsc_tlv = (PCSC_TLV_STRUCTURE *)buffer;
found = 0;
for (i = 0; i < length; i++)
{
if (FEATURE_GET_TLV_PROPERTIES == pcsc_tlv[i].tag)
{
properties_in_tlv_ioctl = ntohl(pcsc_tlv[i].value);
found = 1;
}
}
if (! found)
return -3;
rv= SCardControl(hCard, properties_in_tlv_ioctl, NULL, 0,
buffer, sizeof buffer, &length);
if (rv != SCARD_S_SUCCESS)
return -1;
return PCSCv2Part10_find_TLV_property_by_tag_from_buffer(buffer,
length, property, value);
}