<commit_before>/*
* SMARTCARDPP
* 
* This software is released under either the GNU Library General Public
* License (see LICENSE.LGPL) or the BSD License (see LICENSE.BSD).
* 
* Note that the only valid version of the LGPL license as far as this
* project is concerned is the original GNU Library General Public License
* Version 2.1, February 1999
*
*/
#ifndef PCSCMANAGER_H
#define PCSCMANAGER_H

#ifndef WIN32
	#include <stdio.h>
	#include <cstdlib>
	#include <stdarg.h>
	#include <unistd.h>
	#include <PCSC/wintypes.h>
	#include <PCSC/pcsclite.h>
	#include <PCSC/winscard.h>
	#include <arpa/inet.h>
	#include <sys/time.h>
	
	#include <sys/time.h>
	#ifndef __APPLE__
		#include <reader.h>
		//#define SCARD_E_NO_READERS_AVAILABLE 0x8010002EL
	#else
		#define ERROR_NO_MEDIA_IN_DRIVE          1112L
	#endif
#else
  #pragma warning(push)
  #pragma warning(disable:4201)
  #undef UNICODE
  #include <winscard.h>
  #pragma warning(pop)
  #include <Windows.h>
  #include <time.h>
  #include <WinNT.h>
  #include <Psapi.h>
  #include <stdlib.h>
#endif

#include <string.h>
#include <sys/stat.h>
#include "common.h"
#include "SCError.h"
#include "SCardLog.h"
#include "helperMacro.h"

#ifndef EINVAL
    #define	EINVAL		22
#endif


#ifndef CM_IOCTL_GET_FEATURE_REQUEST

// FIXME: We should use internal-winscard.h from OpenSC project instead
//        This will clean up PCSCManager.h header considerably and
//        allows us to use Mingw32 for building

#include "internal-pcsc22.h"
#endif

#define SS(a) if ((theState & SCARD_STATE_##a ) == SCARD_STATE_##a) stateStr += string(#a) + string("|")

struct ConnectionBase;
struct PCSCConnection;


/// WinSCard/PCSCLite wrapper
/** PCSCManager represents WinSCard subsystem on Windows or PCSCLite libary
  on platforms where it is available. It loads the libraries dynamically to avoid
 linked-in dependencies */
class PCSCManager
{
private:
	bool mOwnContext;
	SCARDCONTEXT hContext;
	SCARDHANDLE hScard;
	std::vector<char > mReaders;
	std::vector<SCARD_READERSTATE> mReaderStates;
	unsigned int transactionID;
	unsigned int connectionID;
	int readerLanguageId;
	uint cIndex;
	
	#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
		#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
	#else
	  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
	#endif

	struct timezone 
	{
	  int  tz_minuteswest; /* minutes W of Greenwich */
	  int  tz_dsttime;     /* type of dst correction */
	};
    
    struct osver {
        int minor;
        int sub;
    } ;
    typedef struct osver osxver;

	void construct(void);
	void ensureReaders();
	void execPinCommand(/*ConnectionBase *c, */bool verify, std::vector<byte> &cmd);
	std::string translateReaderState(DWORD state);
	std::string translateDwEventState(DWORD state);
	void resetCurrentContext();

	DWORD proto;
	DWORD verify_ioctl;
	DWORD verify_ioctl_start;
	DWORD verify_ioctl_finish;
	DWORD modify_ioctl;
	DWORD modify_ioctl_start;
	DWORD modify_ioctl_finish;
	bool display;
	bool pinpad;
	std::string readerName;
	bool mOwnConnection;
	
#ifdef WIN32
	int gettimeofday(struct timeval *tv, struct timezone *tz);
#elif __APPLE__<commit_msg>IB-3790<commit_after>/*
* SMARTCARDPP
* 
* This software is released under either the GNU Library General Public
* License (see LICENSE.LGPL) or the BSD License (see LICENSE.BSD).
* 
* Note that the only valid version of the LGPL license as far as this
* project is concerned is the original GNU Library General Public License
* Version 2.1, February 1999
*
*/
#ifndef PCSCMANAGER_H
#define PCSCMANAGER_H

#ifndef WIN32
	#include <stdio.h>
	#include <cstdlib>
	#include <stdarg.h>
	#include <unistd.h>
	#include <PCSC/wintypes.h>
	#include <PCSC/pcsclite.h>
	#include <PCSC/winscard.h>
	#include <arpa/inet.h>
	#include <sys/time.h>
	
	#include <sys/time.h>
	#ifndef __APPLE__
		#include <reader.h>
		//#define SCARD_E_NO_READERS_AVAILABLE 0x8010002EL
	#else
		#define ERROR_NO_MEDIA_IN_DRIVE          1112L
	#endif
#else
  #pragma warning(push)
  #pragma warning(disable:4201)
  #undef UNICODE
  #include <winscard.h>
  #pragma warning(pop)
  #include <Windows.h>
  #include <time.h>
  #include <WinNT.h>
  #include <Psapi.h>
  #include <stdlib.h>
#endif

#include <string.h>
#include <sys/stat.h>
#include "common.h"
#include "SCError.h"
#include "SCardLog.h"
#include "helperMacro.h"

#ifndef EINVAL
    #define	EINVAL		22
#endif


#ifndef CM_IOCTL_GET_FEATURE_REQUEST

// FIXME: We should use internal-winscard.h from OpenSC project instead
//        This will clean up PCSCManager.h header considerably and
//        allows us to use Mingw32 for building

#include "internal-pcsc22.h"
#endif

#define SS(a) if ((theState & SCARD_STATE_##a ) == SCARD_STATE_##a) stateStr += string(#a) + string("|")

struct ConnectionBase;
struct PCSCConnection;


/// WinSCard/PCSCLite wrapper
/** PCSCManager represents WinSCard subsystem on Windows or PCSCLite libary
  on platforms where it is available. It loads the libraries dynamically to avoid
 linked-in dependencies */
class PCSCManager
{
private:
	bool mOwnContext;
	SCARDCONTEXT hContext;
	SCARDHANDLE hScard;
	std::vector<char > mReaders;
	std::vector<SCARD_READERSTATE> mReaderStates;
	unsigned int transactionID;
	unsigned int connectionID;
	int readerLanguageId;
	uint cIndex;
	
	#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
		#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
	#else
	  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
	#endif

	struct timezone 
	{
	  int  tz_minuteswest; /* minutes W of Greenwich */
	  int  tz_dsttime;     /* type of dst correction */
	};
    
    struct osver {
        int minor;
        int sub;
    } ;
    typedef struct osver osxver;

	void construct(void);
	void ensureReaders();
	void execPinCommand(/*ConnectionBase *c, */bool verify, std::vector<byte> &cmd);
	std::string translateReaderState(DWORD state);
	std::string translateDwEventState(DWORD state);
	void resetCurrentContext();

	DWORD proto;
	DWORD verify_ioctl;
	DWORD verify_ioctl_start;
	DWORD verify_ioctl_finish;
	DWORD modify_ioctl;
	DWORD modify_ioctl_start;
	DWORD modify_ioctl_finish;
	bool display;
	bool pinpad;
	std::string readerName;
	bool mOwnConnection;
	
#ifdef WIN32
	int gettimeofday(struct timeval *tv, struct timezone *tz);
#elif __APPLE__
    int errno;
{'fraction_non_alphanumeric': 0.09820435404417607, 'fraction_numerical': 0.02224694104560623, 'mean_word_length': 4.397941680960549, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6458270', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2222, 'n_words': 615}