From 88a99f9a30d4f35437645f4e8916ccfa43ed06e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Fri, 10 Jul 2009 10:02:33 +0000 Subject: [PATCH] mac: other types --- src/pcsc.hxx | 67 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/src/pcsc.hxx b/src/pcsc.hxx index 3d92007..2803d30 100644 --- a/src/pcsc.hxx +++ b/src/pcsc.hxx @@ -10,9 +10,35 @@ #ifndef PCSC_HXX #define PCSC_HXX -#include -#include -#include +#ifdef WIN32 + #include "WinSCard.h" + #ifndef MAX_ATR_SIZE + #define MAX_ATR_SIZE 33 + #endif +namespace pcsc { + //! stupid windows needs std::wstring + std::wstring strconv(std::string s) { + return std::wstring(s.begin(), s.end()); + } + std::string strconv(std::wstring s) { + return std::string(s.begin(), s.end()); + } + typedef wchar_t char_t; + typedef std::wstring string; +} +#else + #include + #include + #include + #include +namespace pcsc { + const std::string& strconv(const std::string& s) { + return s; + } + typedef char char_t; + typedef std::string string; +} +#endif #include #include @@ -169,9 +195,9 @@ namespace pcsc { //! Get reader status. Status status() { - unsigned long dummy(0); - unsigned long s; - unsigned long len(MAX_ATR_SIZE); + DWORD dummy(0); + DWORD s; + DWORD len(MAX_ATR_SIZE); unsigned char a[len]; check(SCardStatus(_id, 0, &dummy, &s, &_protocol, a, &len)); return Status(s, std::string((char*)a, len)); @@ -179,7 +205,7 @@ namespace pcsc { //! Transmit data to reader. std::string transmit(std::string in) { - unsigned long len(1024); // arbitrary + DWORD len(1024); // arbitrary unsigned char buff[len]; SCARD_IO_REQUEST rPci; check(SCardTransmit(_id, pci(), @@ -217,7 +243,7 @@ namespace pcsc { } /*! @throw not_implemented if _protocol is unknown. */ - SCARD_IO_REQUEST* pci() { + const SCARD_IO_REQUEST* pci() { switch(_protocol) { case SCARD_PROTOCOL_T0: return SCARD_PCI_T0; case SCARD_PROTOCOL_T1: return SCARD_PCI_T1; @@ -240,7 +266,7 @@ namespace pcsc { //! Establishes a connection to the given named cardreader Reader(const std::string& nm, Connection& c): name(nm), _connection(c) { - check(SCardConnect(_connection._id, name.c_str(), + check(SCardConnect(_connection._id, strconv(name).c_str(), SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1, &_id, &_protocol)); @@ -256,9 +282,9 @@ namespace pcsc { private: Connection& _connection; - long _id; + SCARDHANDLE _id; long _state; - unsigned long _protocol; + DWORD _protocol; }; //------------------------------------------------------------------Reader @@ -319,14 +345,15 @@ namespace pcsc { Strings scan(const Strings& groups = Strings()) { Strings res; std::string grp(join(groups)); - unsigned long num(0); - if (!check(SCardListReaders(_id, grp.size()?grp.data():0, 0, &num))) + DWORD num(0); + if (!check(SCardListReaders(_id, grp.size()?strconv(grp).data():0, 0, + &num))) return res; - std::auto_ptr nm(new char[num]); - if (!check(SCardListReaders(_id, grp.size()?grp.data():0, + std::auto_ptr nm(new char_t[num]); + if (!check(SCardListReaders(_id, grp.size()?strconv(grp).data():0, nm.get(), &num))) return res; - return res = split(std::string(nm.get(), num-1)); + return res = split(strconv(string(nm.get(), num-1))); } //! Get a reader, open a connection if not already open. @@ -352,7 +379,13 @@ namespace pcsc { //! Get the describing text of the last error std::string error() const { + #ifdef WIN32 + std::stringstream ss; + ss<<"PCSC state: "<<_state; + return ss.str(); + #else return pcsc_stringify_error(_state); + #endif } //................................................................methods @@ -396,7 +429,7 @@ namespace pcsc { private: bool _exc; - long _id; + SCARDCONTEXT _id; long _state; std::map > _reader;