Esempio n. 1
0
 public static void ThrowIfNotSuccess(this SCardError error, PcscExceptionHandler onException = null)
 {
     if (error != SCardError.Successs)
     {
         Throw(error, onException);
     }
 }
Esempio n. 2
0
 public void Cancel(PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscContext), nameof(Cancel));
     }
     Provider.SCardCancel(Handle).ThrowIfNotSuccess(onException);
 }
Esempio n. 3
0
 private void ReleaseInternal(PcscExceptionHandler onException = null)
 {
     if (!IsEstablished)
     {
         return;
     }
     Provider.SCardReleaseContext(Handle).ThrowIfNotSuccess(onException);
     Handle = SCardContext.Default;
 }
Esempio n. 4
0
 public PcscConnection BeginTransaction(PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscConnection), nameof(BeginTransaction));
     }
     Provider.SCardBeginTransaction(Handle).ThrowIfNotSuccess(onException);
     return(this);
 }
Esempio n. 5
0
 public PcscContext Release(PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscContext), nameof(Release));
     }
     ReleaseInternal(onException);
     return(this);
 }
Esempio n. 6
0
 public PcscContext Validate(PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscContext), nameof(Validate));
     }
     Provider.SCardIsValidContext(Handle).ThrowIfNotSuccess(onException);
     return(this);
 }
Esempio n. 7
0
        public static void Throw(this SCardError error, PcscExceptionHandler onException = null)
        {
            var exception = new PcscException(error);

            if (onException != null)
            {
                onException(exception);
                if (!exception.ThrowIt)
                {
                    return;
                }
            }
            throw exception;
        }
Esempio n. 8
0
        public unsafe PcscContext Establish(SCardScope scope, PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscContext), nameof(Establish));
            }
            if (IsEstablished)
            {
                throw new InvalidOperationException("Context has been established.");
            }
            SCardContext handle;

            Provider.SCardEstablishContext(scope, null, null, &handle).ThrowIfNotSuccess(onException);
            Handle = handle;
            return(this);
        }
Esempio n. 9
0
        public unsafe byte[] Control(int code, byte[] send, int bufferSize, PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscConnection), nameof(Control));
            }
            if (bufferSize <= 0)
            {
                if (send == null)
                {
                    Provider.SCardControl(Handle, code, null, 0, null, 0, &bufferSize).ThrowIfNotSuccess(onException);
                }
                else
                {
                    fixed(byte *fSend = send)
                    {
                        Provider.SCardControl(Handle, code, fSend, send.Length, null, 0, &bufferSize).ThrowIfNotSuccess(onException);
                    }
                }
                return(new byte[0]);
            }
            else
            {
                var recv = new byte[bufferSize];
                fixed(byte *fRecv = recv)
                {
                    if (send == null)
                    {
                        Provider.SCardControl(Handle, code, null, 0, fRecv, bufferSize, &bufferSize).ThrowIfNotSuccess(onException);
                    }
                    else
                    {
                        fixed(byte *fSend = send)
                        {
                            Provider.SCardControl(Handle, code, fSend, send.Length, fRecv, bufferSize, &bufferSize).ThrowIfNotSuccess(onException);
                        }
                    }
                }

                if (bufferSize <= 0)
                {
                    return(new byte[0]);
                }
                Array.Resize(ref recv, bufferSize);
                return(recv);
            }
        }
Esempio n. 10
0
        public IEnumerable <string> GetReaderNames(string group, PcscExceptionHandler onException = null)
        {
            var readerNames = Provider.GetReaderNames(SCardContext.Default, group, onException);

            if (readerNames == null)
            {
                yield break;
            }
            for (int offset = 0, offsetNull, length = readerNames.Length; ;)
            {
                if (offset >= length || (offsetNull = readerNames.IndexOf('\0', offset)) <= offset)
                {
                    yield break;
                }
                yield return(readerNames.Substring(offset, offsetNull - offset));

                offset = offsetNull + 1;
            }
        }
Esempio n. 11
0
        public IEnumerable <string> GetReaderGroupNames(PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscContext), nameof(GetReaderGroupNames));
            }
            var groupNames = Provider.GetReaderGroupNames(Handle, onException);

            if (groupNames == null)
            {
                yield break;
            }
            for (int offset = 0, offsetNull, length = groupNames.Length; ;)
            {
                if (offset >= length || (offsetNull = groupNames.IndexOf('\0', offset)) <= offset)
                {
                    yield break;
                }
                yield return(groupNames.Substring(offset, offsetNull - offset));

                offset = offsetNull + 1;
            }
        }
Esempio n. 12
0
 public static IEnumerable <string> GetReaderNames(SCardReaderGroup group = SCardReaderGroup.NotSpecified, PcscExceptionHandler onException = null)
 {
     return(Instance.GetReaderNames(group.GetDefinedValue(), onException));
 }
Esempio n. 13
0
 public static IEnumerable <string> GetReaderNames(string group, PcscExceptionHandler onException = null)
 {
     return(Instance.GetReaderNames(group, onException));
 }
Esempio n. 14
0
 public PcscConnection Disconnect(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscConnection), nameof(Disconnect));
     }
     DisconnectInternal(disposition, onException);
     return(this);
 }
Esempio n. 15
0
 public static PcscContext EstablishContext(SCardScope scope, PcscExceptionHandler onException = null)
 {
     return(Instance.EstablishContext(scope, onException));
 }
Esempio n. 16
0
 public PcscConnection EndTransaction(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscConnection), nameof(EndTransaction));
     }
     Provider.SCardEndTransaction(Handle, disposition).ThrowIfNotSuccess(onException);
     return(this);
 }
Esempio n. 17
0
 public PcscContext EstablishContext(SCardScope scope, PcscExceptionHandler onException = null)
 {
     return(CreateContext().Establish(scope, onException));
 }
Esempio n. 18
0
 public unsafe byte[] Transmit(byte[] send, int bufferSize, PcscExceptionHandler onException = null)
 {
     return(Transmit(send, null, bufferSize, onException));
 }
Esempio n. 19
0
 public unsafe PcscConnection Reconnect(SCardShare shareMode, SCardProtocols protocol, SCardDisposition initializationDisposition = SCardDisposition.Leave, PcscExceptionHandler onException = null)
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(nameof(PcscConnection), nameof(Reconnect));
     }
     Provider.SCardReconnect(Handle, shareMode, protocol, initializationDisposition, &protocol).ThrowIfNotSuccess(onException);
     ShareMode = shareMode;
     Protocol  = protocol;
     return(this);
 }
Esempio n. 20
0
        public unsafe static string GetReaderNames(this IPcscProvider provider, SCardContext handle, string group, PcscExceptionHandler onException)
        {
            string readerNames = null;
            byte * pReaderNames;
            var    charCount = PcscProvider.SCardAutoAllocate;
            var    err       = provider.SCardListReaders(handle, group, &pReaderNames, &charCount);

            try
            {
                switch (err)
                {
                case SCardError.NoReadersAvailable:
                    // In Windows, it seems to still return a `NULL` character with `SCardError.Success` status even none of reader names is found.
                    break;

                case SCardError.Successs:
                    /*
                     * Providers can use ANSI (e.g., WinSCard and pcsc-lite) or Unicode (e.g., WinSCard) for the encoding of characters.
                     * In ANSI, `charCount` means the byte count of string.
                     * In Unicode, `charCount` means the number of Unicode characters of string.
                     */
                    readerNames = provider.AllocateString(pReaderNames, charCount);
                    break;

                default:
                    err.Throw(onException);
                    break;
                }
            }
            finally
            {
                if (pReaderNames != null)
                {
                    provider.SCardFreeMemory(handle, pReaderNames).ThrowIfNotSuccess(onException);
                }
            }
            return(readerNames);
        }
Esempio n. 21
0
 public byte[] Control(SCardControlFunction function, byte[] send, int bufferSize, PcscExceptionHandler onException = null)
 {
     return(Control(Provider.SCardCtlCode(function), send, bufferSize, onException));
 }
Esempio n. 22
0
 public void Cancel(PcscExceptionHandler onException = null)
 {
     Context.Cancel(onException);
 }
Esempio n. 23
0
 private void DisconnectInternal(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null)
 {
     if (!IsConnect)
     {
         return;
     }
     Provider.SCardDisconnect(Handle, disposition).ThrowIfNotSuccess(onException);
     Handle    = SCardHandle.Default;
     ShareMode = SCardShare.Undefined;
     Protocol  = SCardProtocols.Undefined;
 }
Esempio n. 24
0
        public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, int bufferSize, int recvInformationLength, out byte[] recvInformation, PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscConnection), nameof(Transmit));
            }
            var recv          = new byte[bufferSize];
            var sendIORequest = Provider.AllocateIORequest(sendInformation?.Length ?? 0);
            var recvIORequest = Provider.AllocateIORequest(recvInformationLength);

            fixed(byte *fSend = send, fRecv = recv, fSendIORequest = sendIORequest, fRecvIORequest = recvIORequest)
            {
                var pSendIORequest = fSendIORequest;
                var pRecvIORequest = fRecvIORequest;

                Provider.WriteIORequest(pSendIORequest, Protocol, sendIORequest.Length, sendInformation);
                Provider.WriteIORequest(pRecvIORequest, SCardProtocols.Undefined, recvIORequest.Length, null);
                Provider.SCardTransmit(Handle, pSendIORequest, fSend, send.Length, pRecvIORequest, fRecv, &bufferSize).ThrowIfNotSuccess(onException);
                SCardProtocols protocol;

                Provider.ReadIORequest(pRecvIORequest, out protocol, out recvInformation);
                Protocol = protocol;
            }

            Array.Resize(ref recv, bufferSize);
            return(recv);
        }
Esempio n. 25
0
 public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, int recvInformationLength, out byte[] recvInformation, PcscExceptionHandler onException = null)
 {
     return(Transmit(send, sendInformation, TransmitBufferSize, recvInformationLength, out recvInformation, onException));
 }
Esempio n. 26
0
 public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, PcscExceptionHandler onException = null)
 {
     return(Transmit(send, sendInformation, TransmitBufferSize, onException));
 }
Esempio n. 27
0
        public unsafe PcscReaderStatus WaitForChanged(int timeout = Timeout.Infinite, PcscExceptionHandler onException = null)
        {
            if (Context.IsDisposed)
                throw new ObjectDisposedException(nameof(PcscContext), nameof(WaitForChanged));
            fixed(byte *fReaderStates = _readerStates)
            {
                var pReaderStates = fReaderStates;

                try
                {
                    for (var i = 0; i < Count; ++i)
                    {
                        Provider.WriteReaderState(pReaderStates, i, Provider.AllocateString(Items[i].ReaderName));
                    }
                    Provider.SCardGetStatusChange(Context.Handle, timeout, pReaderStates, Count).ThrowIfNotSuccess(onException);
                }
                finally
                {
                    for (var i = 0; i < Count; ++i)
                    {
                        Provider.ReadReaderState(pReaderStates, i, out void *pReaderName, out SCardReaderStates currentState, out SCardReaderStates eventState, out byte[] atr);
                        Provider.WriteReaderState(pReaderStates, i, eventState);
                        var readerState = Items[i];
                        readerState.Atr         = atr;
                        readerState.EventNumber = ((int)eventState >> 16) & 0x0000FFFF;
                        readerState.State       = eventState & (SCardReaderStates)0x0000FFFF;
                        Provider.FreeString(pReaderName);
                    }
                }
            }

            return(this);
        }
Esempio n. 28
0
        public unsafe PcscConnection Connect(SCardShare shareMode, SCardProtocols protocol, PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscConnection), nameof(Connect));
            }
            SCardHandle handle;

            Provider.SCardConnect(Context.Handle, ReaderName, shareMode, protocol, &handle, &protocol).ThrowIfNotSuccess(onException);
            Handle    = handle;
            ShareMode = shareMode;
            Protocol  = protocol;
            return(this);
        }
Esempio n. 29
0
        public unsafe static string GetReaderGroupNames(this IPcscProvider provider, SCardContext handle, PcscExceptionHandler onException)
        {
            byte *pGroupNames = null;
            var   charCount   = PcscProvider.SCardAutoAllocate;

            try
            {
                provider.SCardListReaderGroups(handle, &pGroupNames, &charCount).ThrowIfNotSuccess(onException);
                return(provider.AllocateString(pGroupNames, charCount));
            }
            finally
            {
                if (pGroupNames != null)
                {
                    provider.SCardFreeMemory(handle, pGroupNames).ThrowIfNotSuccess(onException);
                }
            }
        }
Esempio n. 30
0
 public byte[] Control(int code, byte[] send, PcscExceptionHandler onException = null)
 {
     return(Control(code, send, ControlBufferSize, onException));
 }