Fixes for GetAttrib and some refactroing
authorArmin Novak <armin.novak@thincast.com>
Mon, 17 Feb 2020 13:07:20 +0000 (14:07 +0100)
committerArmin Novak <armin.novak@thincast.com>
Thu, 20 Feb 2020 12:59:28 +0000 (13:59 +0100)
* Fix accidental buffer free before transfer
* Refactored code to eliminate a bunch of warnings
* Updated copyright headers

channels/smartcard/client/smartcard_operations.c
channels/smartcard/client/smartcard_pack.c
channels/smartcard/client/smartcard_pack.h
scripts/test-scard.cpp
winpr/include/winpr/smartcard.h
winpr/libwinpr/smartcard/smartcard.c
winpr/libwinpr/smartcard/smartcard_inspect.c
winpr/libwinpr/smartcard/smartcard_inspect.h
winpr/libwinpr/smartcard/smartcard_pcsc.c
winpr/libwinpr/smartcard/smartcard_pcsc.h

index f76b0fb..fbf8b4a 100644 (file)
@@ -9,6 +9,8 @@
  * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
  * Copyright 2017 Armin Novak <armin.novak@thincast.com>
  * Copyright 2017 Thincast Technologies GmbH
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -1167,7 +1169,7 @@ static LONG smartcard_GetStatusChangeA_Call(SMARTCARD_DEVICE* smartcard,
                ret.rgReaderStates[index].dwCurrentState = call->rgReaderStates[index].dwCurrentState;
                ret.rgReaderStates[index].dwEventState = call->rgReaderStates[index].dwEventState;
                ret.rgReaderStates[index].cbAtr = call->rgReaderStates[index].cbAtr;
-               CopyMemory(&(ret.rgReaderStates[index].rgbAtr), &(call->rgReaderStates[index].rgbAtr), 32);
+               CopyMemory(&(ret.rgReaderStates[index].rgbAtr), &(call->rgReaderStates[index].rgbAtr), 36);
        }
 
        smartcard_pack_get_status_change_return(smartcard, irp->output, &ret, FALSE);
@@ -1231,7 +1233,7 @@ static LONG smartcard_GetStatusChangeW_Call(SMARTCARD_DEVICE* smartcard,
                ret.rgReaderStates[index].dwCurrentState = call->rgReaderStates[index].dwCurrentState;
                ret.rgReaderStates[index].dwEventState = call->rgReaderStates[index].dwEventState;
                ret.rgReaderStates[index].cbAtr = call->rgReaderStates[index].cbAtr;
-               CopyMemory(&(ret.rgReaderStates[index].rgbAtr), &(call->rgReaderStates[index].rgbAtr), 32);
+               CopyMemory(&(ret.rgReaderStates[index].rgbAtr), &(call->rgReaderStates[index].rgbAtr), 36);
        }
 
        smartcard_pack_get_status_change_return(smartcard, irp->output, &ret, TRUE);
@@ -1313,13 +1315,6 @@ static LONG smartcard_ConnectA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERA
                          call->Common.dwPreferredProtocols, &hCard, &ret.dwActiveProtocol);
        smartcard_scard_context_native_to_redir(smartcard, &(ret.hContext), operation->hContext);
        smartcard_scard_handle_native_to_redir(smartcard, &(ret.hCard), hCard);
-       smartcard_trace_connect_return(smartcard, &ret);
-
-       if (status)
-       {
-               log_status_error(TAG, "SCardConnectA", status);
-               goto out_fail;
-       }
 
        status = smartcard_pack_connect_return(smartcard, irp->output, &ret);
        if (status != SCARD_S_SUCCESS)
@@ -1367,13 +1362,6 @@ static LONG smartcard_ConnectW_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERA
                          call->Common.dwPreferredProtocols, &hCard, &ret.dwActiveProtocol);
        smartcard_scard_context_native_to_redir(smartcard, &(ret.hContext), operation->hContext);
        smartcard_scard_handle_native_to_redir(smartcard, &(ret.hCard), hCard);
-       smartcard_trace_connect_return(smartcard, &ret);
-
-       if (status)
-       {
-               log_status_error(TAG, "SCardConnectW", status);
-               goto out_fail;
-       }
 
        status = smartcard_pack_connect_return(smartcard, irp->output, &ret);
        if (status != SCARD_S_SUCCESS)
@@ -1624,7 +1612,7 @@ static LONG smartcard_StatusW_Decode(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPER
 static LONG smartcard_StatusW_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation)
 {
        LONG status;
-       Status_Return ret;
+       Status_Return ret = { 0 };
        DWORD cchReaderLen = 0;
        LPWSTR mszReaderNames = NULL;
        IRP* irp = operation->irp;
@@ -1834,23 +1822,14 @@ static LONG smartcard_GetAttrib_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPER
                           autoAllocate ? (LPBYTE) & (ret.pbAttr) : ret.pbAttr, &cbAttrLen);
        log_status_error(TAG, "SCardGetAttrib", ret.ReturnCode);
        ret.cbAttrLen = cbAttrLen;
-       free(ret.pbAttr);
-
-       if (ret.ReturnCode)
-       {
-               WLog_WARN(TAG, "SCardGetAttrib: %s (0x%08" PRIX32 ") cbAttrLen: %" PRIu32 "",
-                         SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->cbAttrLen);
-               Stream_Zero(irp->output, 256);
-
-               return ret.ReturnCode;
-       }
 
        status = smartcard_pack_get_attrib_return(smartcard, irp->output, &ret, call->dwAttrId);
 
-       if (status != SCARD_S_SUCCESS)
-               return status;
-
-       return ret.ReturnCode;
+       if (autoAllocate)
+               SCardFreeMemory(operation->hContext, ret.pbAttr);
+       else
+               free(ret.pbAttr);
+       return status;
 }
 
 static LONG smartcard_SetAttrib_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation)
@@ -2138,7 +2117,7 @@ static LONG smartcard_LocateCardsByATRA_Call(SMARTCARD_DEVICE* smartcard,
                ret.rgReaderStates[i].dwCurrentState = state->dwCurrentState;
                ret.rgReaderStates[i].dwEventState = state->dwEventState;
                ret.rgReaderStates[i].cbAtr = state->cbAtr;
-               CopyMemory(&(ret.rgReaderStates[i].rgbAtr), &(state->rgbAtr), 32);
+               CopyMemory(&(ret.rgReaderStates[i].rgbAtr), &(state->rgbAtr), 36);
        }
 
        free(states);
@@ -2474,7 +2453,9 @@ LONG smartcard_irp_device_control_call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OP
         * Since it's a SHOULD and not a MUST, we don't care
         * about it, but we still reserve at least 2048 bytes.
         */
-       Stream_EnsureRemainingCapacity(irp->output, 2048);
+       if (!Stream_EnsureRemainingCapacity(irp->output, 2048))
+               return SCARD_E_NO_MEMORY;
+
        /* Device Control Response */
        Stream_Seek_UINT32(irp->output); /* OutputBufferLength (4 bytes) */
        Stream_Seek(irp->output, SMARTCARD_COMMON_TYPE_HEADER_LENGTH); /* CommonTypeHeader (8 bytes) */
@@ -2710,7 +2691,7 @@ LONG smartcard_irp_device_control_call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OP
 
        irp->IoStatus = STATUS_SUCCESS;
 
-       if ((result & 0xC0000000) == 0xC0000000)
+       if ((result & 0xC0000000L) == 0xC0000000L)
        {
                /* NTSTATUS error */
                irp->IoStatus = (UINT32)result;
@@ -2727,7 +2708,7 @@ LONG smartcard_irp_device_control_call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OP
        smartcard_pack_common_type_header(smartcard, irp->output); /* CommonTypeHeader (8 bytes) */
        smartcard_pack_private_type_header(smartcard, irp->output,
                                           objectBufferLength); /* PrivateTypeHeader (8 bytes) */
-       Stream_Write_UINT32(irp->output, result);               /* Result (4 bytes) */
+       Stream_Write_INT32(irp->output, result);                /* Result (4 bytes) */
        Stream_SetPosition(irp->output, Stream_Length(irp->output));
        return SCARD_S_SUCCESS;
 }
index 9d7733a..5b64796 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  * Copyright 2015 Thincast Technologies GmbH
  * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +30,8 @@
 
 #include "smartcard_pack.h"
 
+static const DWORD g_LogLevel = WLOG_INFO;
+
 #define smartcard_unpack_redir_scard_context(smartcard, s, context, index)                \
        smartcard_unpack_redir_scard_context_((smartcard), (s), (context), (index), __FILE__, \
                                              __FUNCTION__, __LINE__)
@@ -39,12 +43,12 @@ static LONG smartcard_unpack_redir_scard_context_(SMARTCARD_DEVICE* smartcard, w
                                                   REDIR_SCARDCONTEXT* context, UINT32* index,
                                                   const char* file, const char* function, int line);
 static LONG smartcard_pack_redir_scard_context(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                               const REDIR_SCARDCONTEXT* context);
+                                               const REDIR_SCARDCONTEXT* context, DWORD* index);
 static LONG smartcard_unpack_redir_scard_handle_(SMARTCARD_DEVICE* smartcard, wStream* s,
                                                  REDIR_SCARDHANDLE* handle, UINT32* index,
                                                  const char* file, const char* function, int line);
 static LONG smartcard_pack_redir_scard_handle(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                              const REDIR_SCARDHANDLE* handle);
+                                              const REDIR_SCARDHANDLE* handle, DWORD* index);
 static LONG smartcard_unpack_redir_scard_context_ref(SMARTCARD_DEVICE* smartcard, wStream* s,
                                                      REDIR_SCARDCONTEXT* context);
 static LONG smartcard_pack_redir_scard_context_ref(SMARTCARD_DEVICE* smartcard, wStream* s,
@@ -179,6 +183,87 @@ static LONG smartcard_ndr_read(wStream* s, BYTE** data, size_t min, size_t eleme
        return STATUS_SUCCESS;
 }
 
+static BOOL smartcard_ndr_pointer_write(wStream* s, UINT32* index, DWORD length)
+{
+       const UINT32 ndrPtr = 0x20000 + (*index) * 4;
+
+       if (!s)
+               return FALSE;
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return FALSE;
+
+       if (length > 0)
+       {
+               Stream_Write_UINT32(s, ndrPtr); /* mszGroupsNdrPtr (4 bytes) */
+               (*index) = (*index) + 1;
+       }
+       else
+               Stream_Write_UINT32(s, 0);
+       return TRUE;
+}
+
+static LONG smartcard_ndr_write(wStream* s, const BYTE* data, UINT32 size, UINT32 elementSize,
+                                ndr_ptr_t type)
+{
+       const UINT32 offset = 0;
+       const UINT32 len = size;
+       const UINT32 dataLen = size * elementSize;
+       size_t required;
+
+       if (size == 0)
+               return SCARD_S_SUCCESS;
+
+       switch (type)
+       {
+               case NDR_PTR_FULL:
+                       required = 12;
+                       break;
+               case NDR_PTR_SIMPLE:
+                       required = 4;
+                       break;
+               case NDR_PTR_FIXED:
+                       required = 0;
+                       break;
+       }
+
+       if (!Stream_EnsureRemainingCapacity(s, required + dataLen))
+               return STATUS_BUFFER_TOO_SMALL;
+
+       switch (type)
+       {
+               case NDR_PTR_FULL:
+                       Stream_Write_UINT32(s, len);
+                       Stream_Write_UINT32(s, offset);
+                       Stream_Write_UINT32(s, len);
+                       break;
+               case NDR_PTR_SIMPLE:
+                       Stream_Write_UINT32(s, len);
+                       break;
+               case NDR_PTR_FIXED:
+                       break;
+       }
+
+       if (data)
+               Stream_Write(s, data, dataLen);
+       else
+               Stream_Zero(s, dataLen);
+       smartcard_pack_write_size_align(NULL, s, len, 4);
+
+       return STATUS_SUCCESS;
+}
+
+static LONG smartcard_ndr_write_state(wStream* s, const ReaderState_Return* data, UINT32 size,
+                                      ndr_ptr_t type)
+{
+       union {
+               const ReaderState_Return* reader;
+               const BYTE* data;
+       } cnv;
+
+       cnv.reader = data;
+       return smartcard_ndr_write(s, cnv.data, size, sizeof(ReaderState_Return), type);
+}
+
 static LONG smartcard_ndr_read_atrmask(wStream* s, LocateCards_ATRMask** data, size_t min,
                                        ndr_ptr_t type)
 {
@@ -336,7 +421,7 @@ static void smartcard_log_redir_handle(const char* tag, const REDIR_SCARDHANDLE*
 {
        char buffer[128];
 
-       WLog_DBG(tag, "hContext: %s",
+       WLog_LVL(tag, g_LogLevel, "hContext: %s",
                 smartcard_array_dump(pHandle->pbHandle, pHandle->cbHandle, buffer, sizeof(buffer)));
 }
 
@@ -352,14 +437,14 @@ static void smartcard_trace_context_and_string_call_a(const char* name,
                                                       const REDIR_SCARDCONTEXT* phContext,
                                                       const CHAR* sz)
 {
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "%s {", name);
+       WLog_LVL(TAG, g_LogLevel, "%s {", name);
        smartcard_log_context(TAG, phContext);
-       WLog_DBG(TAG, "  sz=%s", sz);
+       WLog_LVL(TAG, g_LogLevel, "  sz=%s", sz);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_context_and_string_call_w(const char* name,
@@ -367,16 +452,16 @@ static void smartcard_trace_context_and_string_call_w(const char* name,
                                                       const WCHAR* sz)
 {
        char* tmp;
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "%s {", name);
+       WLog_LVL(TAG, g_LogLevel, "%s {", name);
        smartcard_log_context(TAG, phContext);
        ConvertFromUnicode(CP_UTF8, 0, sz, -1, &tmp, 0, NULL, NULL);
-       WLog_DBG(TAG, "  sz=%s", tmp);
+       WLog_LVL(TAG, g_LogLevel, "  sz=%s", tmp);
        free(tmp);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_context_call(SMARTCARD_DEVICE* smartcard, const Context_Call* call,
@@ -384,13 +469,13 @@ static void smartcard_trace_context_call(SMARTCARD_DEVICE* smartcard, const Cont
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "%s_Call {", name);
+       WLog_LVL(TAG, g_LogLevel, "%s_Call {", name);
        smartcard_log_context(TAG, &call->hContext);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_list_reader_groups_call(SMARTCARD_DEVICE* smartcard,
@@ -398,15 +483,15 @@ static void smartcard_trace_list_reader_groups_call(SMARTCARD_DEVICE* smartcard,
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ListReaderGroups%S_Call {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ListReaderGroups%S_Call {", unicode ? "W" : "A");
        smartcard_log_context(TAG, &call->hContext);
 
-       WLog_DBG(TAG, "fmszGroupsIsNULL: %" PRId32 " cchGroups: 0x%08" PRIx32, call->fmszGroupsIsNULL,
-                call->cchGroups);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "fmszGroupsIsNULL: %" PRId32 " cchGroups: 0x%08" PRIx32,
+                call->fmszGroupsIsNULL, call->cchGroups);
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_status_change_w_call(SMARTCARD_DEVICE* smartcard,
@@ -418,13 +503,13 @@ static void smartcard_trace_get_status_change_w_call(SMARTCARD_DEVICE* smartcard
        LPSCARD_READERSTATEW readerState;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetStatusChangeW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetStatusChangeW_Call {");
        smartcard_log_context(TAG, &call->hContext);
 
-       WLog_DBG(TAG, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
+       WLog_LVL(TAG, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
                 call->cReaders);
 
        for (index = 0; index < call->cReaders; index++)
@@ -432,20 +517,20 @@ static void smartcard_trace_get_status_change_w_call(SMARTCARD_DEVICE* smartcard
                char* szReaderA = NULL;
                readerState = &call->rgReaderStates[index];
                ConvertFromUnicode(CP_UTF8, 0, readerState->szReader, -1, &szReaderA, 0, NULL, NULL);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index, szReaderA,
-                        readerState->cbAtr);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
+                        szReaderA, readerState->cbAtr);
                szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
                szEventState = SCardGetReaderStateString(readerState->dwEventState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
                         szCurrentState, readerState->dwCurrentState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index, szEventState,
-                        readerState->dwEventState);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
+                        szEventState, readerState->dwEventState);
                free(szCurrentState);
                free(szEventState);
                free(szReaderA);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_list_reader_groups_return(SMARTCARD_DEVICE* smartcard,
@@ -455,16 +540,16 @@ static void smartcard_trace_list_reader_groups_return(SMARTCARD_DEVICE* smartcar
        char* mszA = NULL;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
        mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
 
-       WLog_DBG(TAG, "ListReaderGroups%s_Return {", unicode ? "W" : "A");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIx32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "ListReaderGroups%s_Return {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIx32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
+       WLog_LVL(TAG, g_LogLevel, "}");
        free(mszA);
 }
 
@@ -474,19 +559,19 @@ static void smartcard_trace_list_readers_call(SMARTCARD_DEVICE* smartcard,
        char* mszGroupsA = NULL;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
        mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
 
-       WLog_DBG(TAG, "ListReaders%s_Call {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ListReaders%s_Call {", unicode ? "W" : "A");
        smartcard_log_context(TAG, &call->hContext);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "cBytes: %" PRIu32 " mszGroups: %s fmszReadersIsNULL: %" PRId32
                 " cchReaders: 0x%08" PRIX32 "",
                 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 
        free(mszGroupsA);
 }
@@ -497,45 +582,38 @@ static void smartcard_trace_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartca
        UINT32 index;
        char* szEventState;
        char* szCurrentState;
-       char* rgbAtr;
-       LPSCARD_READERSTATEA readerState;
+
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "LocateCardsByATRA_Call {");
+       WLog_LVL(TAG, g_LogLevel, "LocateCardsByATRA_Call {");
        smartcard_log_context(TAG, &call->hContext);
 
        for (index = 0; index < call->cReaders; index++)
        {
-               readerState = (LPSCARD_READERSTATEA)&call->rgReaderStates[index];
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
+               char buffer[1024];
+               const LPSCARD_READERSTATEA readerState = &call->rgReaderStates[index];
+
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
                         readerState->szReader, readerState->cbAtr);
                szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
                szEventState = SCardGetReaderStateString(readerState->dwEventState);
-               rgbAtr = winpr_BinToHexString((BYTE*)&(readerState->rgbAtr), readerState->cbAtr, FALSE);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
                         szCurrentState, readerState->dwCurrentState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index, szEventState,
-                        readerState->dwEventState);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
+                        szEventState, readerState->dwEventState);
 
-               if (rgbAtr)
-               {
-                       WLog_DBG(TAG, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index,
-                                readerState->cbAtr, rgbAtr);
-               }
-               else
-               {
-                       WLog_DBG(TAG, "\t[%" PRIu32 "]: cbAtr: 0 rgbAtr: n/a", index);
-               }
+               WLog_DBG(
+                   TAG, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index, readerState->cbAtr,
+                   smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer, sizeof(buffer)));
 
                free(szCurrentState);
                free(szEventState);
-               free(rgbAtr);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_locate_cards_a_call(SMARTCARD_DEVICE* smartcard,
@@ -544,90 +622,90 @@ static void smartcard_trace_locate_cards_a_call(SMARTCARD_DEVICE* smartcard,
        char buffer[8192];
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "LocateCardsA_Call {");
+       WLog_LVL(TAG, g_LogLevel, "LocateCardsA_Call {");
        smartcard_log_context(TAG, &call->hContext);
-       WLog_DBG(TAG, " cBytes=%" PRId32, call->cBytes);
-       WLog_DBG(TAG, " mszCards=%s",
+       WLog_LVL(TAG, g_LogLevel, " cBytes=%" PRId32, call->cBytes);
+       WLog_LVL(TAG, g_LogLevel, " mszCards=%s",
                 smartcard_msz_dump_a(call->mszCards, call->cBytes, buffer, sizeof(buffer)));
-       WLog_DBG(TAG, " cReaders=%" PRId32, call->cReaders);
-       // WLog_DBG(TAG, " cReaders=%" PRId32, call->rgReaderStates);
+       WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->cReaders);
+       // WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->rgReaderStates);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_locate_cards_return(SMARTCARD_DEVICE* smartcard,
                                                 const LocateCards_Return* ret)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "LocateCards_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "LocateCards_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
        if (ret->ReturnCode == SCARD_S_SUCCESS)
        {
-               WLog_DBG(TAG, " cReaders=%" PRId32, ret->cReaders);
-               // WLog_DBG(TAG, " cReaders=%" PRId32, call->rgReaderStates);
+               WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, ret->cReaders);
+               // WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->rgReaderStates);
        }
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_reader_icon_return(SMARTCARD_DEVICE* smartcard,
                                                    const GetReaderIcon_Return* ret)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetReaderIcon_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "GetReaderIcon_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
        if (ret->ReturnCode == SCARD_S_SUCCESS)
        {
-               WLog_DBG(TAG, " cbDataLen=%" PRId32, ret->cbDataLen);
-               // WLog_DBG(TAG, " cReaders=%" PRId32, call->pbData);
+               WLog_LVL(TAG, g_LogLevel, " cbDataLen=%" PRId32, ret->cbDataLen);
+               // WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->pbData);
        }
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_transmit_count_return(SMARTCARD_DEVICE* smartcard,
                                                       const GetTransmitCount_Return* ret)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
-       WLog_DBG(TAG, " cTransmitCount=%" PRIu32, ret->cTransmitCount);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, " cTransmitCount=%" PRIu32, ret->cTransmitCount);
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_read_cache_return(SMARTCARD_DEVICE* smartcard,
                                               const ReadCache_Return* ret)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ReadCache_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "ReadCache_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
        if (ret->ReturnCode == SCARD_S_SUCCESS)
        {
-               WLog_DBG(TAG, " cbDataLen=%" PRId32, ret->cbDataLen);
-               // WLog_DBG(TAG, " cReaders=%" PRId32, call->cReaders);
+               WLog_LVL(TAG, g_LogLevel, " cbDataLen=%" PRId32, ret->cbDataLen);
+               // WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->cReaders);
        }
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_locate_cards_w_call(SMARTCARD_DEVICE* smartcard,
@@ -635,17 +713,17 @@ static void smartcard_trace_locate_cards_w_call(SMARTCARD_DEVICE* smartcard,
 {
        char buffer[8192];
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "LocateCardsW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "LocateCardsW_Call {");
        smartcard_log_context(TAG, &call->hContext);
-       WLog_DBG(TAG, " cBytes=%" PRId32, call->cBytes);
-       WLog_DBG(TAG, " sz2=%s",
+       WLog_LVL(TAG, g_LogLevel, " cBytes=%" PRId32, call->cBytes);
+       WLog_LVL(TAG, g_LogLevel, " sz2=%s",
                 smartcard_msz_dump_w(call->mszCards, call->cBytes, buffer, sizeof(buffer)));
-       WLog_DBG(TAG, " cReaders=%" PRId32, call->cReaders);
-       // WLog_DBG(TAG, " sz2=%s", call->rgReaderStates);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, " cReaders=%" PRId32, call->cReaders);
+       // WLog_LVL(TAG, g_LogLevel, " sz2=%s", call->rgReaderStates);
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_list_readers_return(SMARTCARD_DEVICE* smartcard,
@@ -654,23 +732,23 @@ static void smartcard_trace_list_readers_return(SMARTCARD_DEVICE* smartcard,
        char* mszA = NULL;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ListReaders%s_Return {", unicode ? "W" : "A");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "ListReaders%s_Return {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
        if (ret->ReturnCode != SCARD_S_SUCCESS)
        {
-               WLog_DBG(TAG, "}");
+               WLog_LVL(TAG, g_LogLevel, "}");
                return;
        }
 
        mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
 
-       WLog_DBG(TAG, "cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
+       WLog_LVL(TAG, g_LogLevel, "}");
        free(mszA);
 }
 
@@ -679,52 +757,52 @@ static void smartcard_trace_get_status_change_return(SMARTCARD_DEVICE* smartcard
                                                      BOOL unicode)
 {
        UINT32 index;
-       char* rgbAtr;
        char* szEventState;
        char* szCurrentState;
-       ReaderState_Return* rgReaderState;
+
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetStatusChange%s_Return {", unicode ? "W" : "A");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "cReaders: %" PRIu32 "", ret->cReaders);
+       WLog_LVL(TAG, g_LogLevel, "GetStatusChange%s_Return {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "cReaders: %" PRIu32 "", ret->cReaders);
 
        for (index = 0; index < ret->cReaders; index++)
        {
-               rgReaderState = &(ret->rgReaderStates[index]);
+               char buffer[1024];
+               const ReaderState_Return* rgReaderState = &(ret->rgReaderStates[index]);
                szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
                szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
-               rgbAtr = winpr_BinToHexString((BYTE*)&(rgReaderState->rgbAtr), rgReaderState->cbAtr, FALSE);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
                         szCurrentState, rgReaderState->dwCurrentState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index, szEventState,
-                        rgReaderState->dwEventState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index, rgReaderState->cbAtr,
-                        rgbAtr);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
+                        szEventState, rgReaderState->dwEventState);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index,
+                        rgReaderState->cbAtr,
+                        smartcard_array_dump(rgReaderState->rgbAtr, rgReaderState->cbAtr, buffer,
+                                             sizeof(buffer)));
                free(szCurrentState);
                free(szEventState);
-               free(rgbAtr);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_context_and_two_strings_a_call(SMARTCARD_DEVICE* smartcard,
                                                            const ContextAndTwoStringA_Call* call)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ContextAndTwoStringW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "ContextAndTwoStringW_Call {");
        smartcard_log_context(TAG, &call->hContext);
-       WLog_DBG(TAG, " sz1=%s", call->sz1);
-       WLog_DBG(TAG, " sz2=%s", call->sz2);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, " sz1=%s", call->sz1);
+       WLog_LVL(TAG, g_LogLevel, " sz2=%s", call->sz2);
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_context_and_two_strings_w_call(SMARTCARD_DEVICE* smartcard,
@@ -734,33 +812,33 @@ static void smartcard_trace_context_and_two_strings_w_call(SMARTCARD_DEVICE* sma
        CHAR* sz2 = NULL;
 
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ContextAndTwoStringW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "ContextAndTwoStringW_Call {");
        smartcard_log_context(TAG, &call->hContext);
        ConvertFromUnicode(CP_UTF8, 0, call->sz1, -1, &sz1, 0, NULL, NULL);
        ConvertFromUnicode(CP_UTF8, 0, call->sz2, -1, &sz2, 0, NULL, NULL);
-       WLog_DBG(TAG, " sz1=%s", sz1);
-       WLog_DBG(TAG, " sz2=%s", sz2);
+       WLog_LVL(TAG, g_LogLevel, " sz1=%s", sz1);
+       WLog_LVL(TAG, g_LogLevel, " sz2=%s", sz2);
        free(sz1);
        free(sz2);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_transmit_count_call(SMARTCARD_DEVICE* smartcard,
                                                     const GetTransmitCount_Call* call)
 {
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_write_cache_a_call(SMARTCARD_DEVICE* smartcard,
@@ -768,23 +846,23 @@ static void smartcard_trace_write_cache_a_call(SMARTCARD_DEVICE* smartcard,
 {
        char buffer[1024];
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Call {");
 
-       WLog_DBG(TAG, "  szLookupName=%s", call->szLookupName);
+       WLog_LVL(TAG, g_LogLevel, "  szLookupName=%s", call->szLookupName);
 
        smartcard_log_context(TAG, &call->Common.hContext);
        WLog_DBG(
            TAG, "..CardIdentifier=%s",
            smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
-       WLog_DBG(TAG, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
+       WLog_LVL(TAG, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
+       WLog_LVL(TAG, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
        WLog_DBG(
            TAG, "  pbData=%s",
            smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_write_cache_w_call(SMARTCARD_DEVICE* smartcard,
@@ -793,24 +871,24 @@ static void smartcard_trace_write_cache_w_call(SMARTCARD_DEVICE* smartcard,
        char* tmp;
        char buffer[1024];
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Call {");
 
        ConvertFromUnicode(CP_UTF8, 0, call->szLookupName, -1, &tmp, 0, NULL, NULL);
-       WLog_DBG(TAG, "  szLookupName=%s", tmp);
+       WLog_LVL(TAG, g_LogLevel, "  szLookupName=%s", tmp);
        free(tmp);
        smartcard_log_context(TAG, &call->Common.hContext);
        WLog_DBG(
            TAG, "..CardIdentifier=%s",
            smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
-       WLog_DBG(TAG, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
+       WLog_LVL(TAG, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
+       WLog_LVL(TAG, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
        WLog_DBG(
            TAG, "  pbData=%s",
            smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_read_cache_a_call(SMARTCARD_DEVICE* smartcard,
@@ -818,21 +896,21 @@ static void smartcard_trace_read_cache_a_call(SMARTCARD_DEVICE* smartcard,
 {
        char buffer[1024];
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Call {");
 
-       WLog_DBG(TAG, "  szLookupName=%s", call->szLookupName);
+       WLog_LVL(TAG, g_LogLevel, "  szLookupName=%s", call->szLookupName);
        smartcard_log_context(TAG, &call->Common.hContext);
        WLog_DBG(
            TAG, "..CardIdentifier=%s",
            smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
-       WLog_DBG(TAG, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
-       WLog_DBG(TAG, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
+       WLog_LVL(TAG, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
+       WLog_LVL(TAG, g_LogLevel, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
+       WLog_LVL(TAG, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_read_cache_w_call(SMARTCARD_DEVICE* smartcard,
@@ -841,23 +919,23 @@ static void smartcard_trace_read_cache_w_call(SMARTCARD_DEVICE* smartcard,
        char* tmp;
        char buffer[1024];
        WINPR_UNUSED(smartcard);
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetTransmitCount_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetTransmitCount_Call {");
 
        ConvertFromUnicode(CP_UTF8, 0, call->szLookupName, -1, &tmp, 0, NULL, NULL);
-       WLog_DBG(TAG, "  szLookupName=%s", tmp);
+       WLog_LVL(TAG, g_LogLevel, "  szLookupName=%s", tmp);
        free(tmp);
        smartcard_log_context(TAG, &call->Common.hContext);
        WLog_DBG(
            TAG, "..CardIdentifier=%s",
            smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
-       WLog_DBG(TAG, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
-       WLog_DBG(TAG, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
+       WLog_LVL(TAG, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
+       WLog_LVL(TAG, g_LogLevel, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
+       WLog_LVL(TAG, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_transmit_call(SMARTCARD_DEVICE* smartcard, const Transmit_Call* call)
@@ -866,10 +944,10 @@ static void smartcard_trace_transmit_call(SMARTCARD_DEVICE* smartcard, const Tra
        BYTE* pbExtraBytes;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Transmit_Call {");
+       WLog_LVL(TAG, g_LogLevel, "Transmit_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
@@ -877,56 +955,57 @@ static void smartcard_trace_transmit_call(SMARTCARD_DEVICE* smartcard, const Tra
        {
                cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
                pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
-               WLog_DBG(TAG, "pioSendPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
+               WLog_LVL(TAG, g_LogLevel, "pioSendPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
                         call->pioSendPci->dwProtocol, cbExtraBytes);
 
                if (cbExtraBytes)
                {
-                       char* szExtraBytes = winpr_BinToHexString(pbExtraBytes, cbExtraBytes, TRUE);
-                       WLog_DBG(TAG, "pbExtraBytes: %s", szExtraBytes);
-                       free(szExtraBytes);
+                       char buffer[1024];
+                       WLog_LVL(TAG, g_LogLevel, "pbExtraBytes: %s",
+                                smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
                }
        }
        else
        {
-               WLog_DBG(TAG, "pioSendPci: null");
+               WLog_LVL(TAG, g_LogLevel, "pioSendPci: null");
        }
 
-       WLog_DBG(TAG, "cbSendLength: %" PRIu32 "", call->cbSendLength);
+       WLog_LVL(TAG, g_LogLevel, "cbSendLength: %" PRIu32 "", call->cbSendLength);
 
        if (call->pbSendBuffer)
        {
-               char* szSendBuffer = winpr_BinToHexString(call->pbSendBuffer, call->cbSendLength, TRUE);
-               WLog_DBG(TAG, "pbSendBuffer: %s", szSendBuffer);
-               free(szSendBuffer);
+               char buffer[1024];
+               WLog_DBG(
+                   TAG, "pbSendBuffer: %s",
+                   smartcard_array_dump(call->pbSendBuffer, call->cbSendLength, buffer, sizeof(buffer)));
        }
        else
        {
-               WLog_DBG(TAG, "pbSendBuffer: null");
+               WLog_LVL(TAG, g_LogLevel, "pbSendBuffer: null");
        }
 
        if (call->pioRecvPci)
        {
                cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
                pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
-               WLog_DBG(TAG, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
+               WLog_LVL(TAG, g_LogLevel, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
                         call->pioRecvPci->dwProtocol, cbExtraBytes);
 
                if (cbExtraBytes)
                {
-                       char* szExtraBytes = winpr_BinToHexString(pbExtraBytes, cbExtraBytes, TRUE);
-                       WLog_DBG(TAG, "pbExtraBytes: %s", szExtraBytes);
-                       free(szExtraBytes);
+                       char buffer[1024];
+                       WLog_LVL(TAG, g_LogLevel, "pbExtraBytes: %s",
+                                smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
                }
        }
        else
        {
-               WLog_DBG(TAG, "pioRecvPci: null");
+               WLog_LVL(TAG, g_LogLevel, "pioRecvPci: null");
        }
 
-       WLog_DBG(TAG, "fpbRecvBufferIsNULL: %" PRId32 " cbRecvLength: %" PRIu32 "",
+       WLog_LVL(TAG, g_LogLevel, "fpbRecvBufferIsNULL: %" PRId32 " cbRecvLength: %" PRIu32 "",
                 call->fpbRecvBufferIsNULL, call->cbRecvLength);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_locate_cards_by_atr_w_call(SMARTCARD_DEVICE* smartcard,
@@ -935,49 +1014,42 @@ static void smartcard_trace_locate_cards_by_atr_w_call(SMARTCARD_DEVICE* smartca
        UINT32 index;
        char* szEventState;
        char* szCurrentState;
-       char* rgbAtr;
+
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "LocateCardsByATRW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "LocateCardsByATRW_Call {");
        smartcard_log_context(TAG, &call->hContext);
 
        for (index = 0; index < call->cReaders; index++)
        {
+               char buffer[1024];
                char* tmp = NULL;
                const LPSCARD_READERSTATEW readerState =
                    (const LPSCARD_READERSTATEW)&call->rgReaderStates[index];
 
                ConvertFromUnicode(CP_UTF8, 0, readerState->szReader, -1, &tmp, 0, NULL, NULL);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index, tmp,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index, tmp,
                         readerState->cbAtr);
                szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
                szEventState = SCardGetReaderStateString(readerState->dwEventState);
-               rgbAtr = winpr_BinToHexString((BYTE*)&(readerState->rgbAtr), readerState->cbAtr, FALSE);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
                         szCurrentState, readerState->dwCurrentState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index, szEventState,
-                        readerState->dwEventState);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
+                        szEventState, readerState->dwEventState);
 
-               if (rgbAtr)
-               {
-                       WLog_DBG(TAG, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index,
-                                readerState->cbAtr, rgbAtr);
-               }
-               else
-               {
-                       WLog_DBG(TAG, "\t[%" PRIu32 "]: cbAtr: 0 rgbAtr: n/a", index);
-               }
+               WLog_DBG(
+                   TAG, "\t[%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index, readerState->cbAtr,
+                   smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer, sizeof(buffer)));
 
                free(szCurrentState);
                free(szEventState);
-               free(rgbAtr);
                free(tmp);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_transmit_return(SMARTCARD_DEVICE* smartcard, const Transmit_Return* ret)
@@ -986,86 +1058,88 @@ static void smartcard_trace_transmit_return(SMARTCARD_DEVICE* smartcard, const T
        BYTE* pbExtraBytes;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Transmit_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "Transmit_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
 
        if (ret->pioRecvPci)
        {
                cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
                pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
-               WLog_DBG(TAG, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
+               WLog_LVL(TAG, g_LogLevel, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
                         ret->pioRecvPci->dwProtocol, cbExtraBytes);
 
                if (cbExtraBytes)
                {
-                       char* szExtraBytes = winpr_BinToHexString(pbExtraBytes, cbExtraBytes, TRUE);
-                       WLog_DBG(TAG, "pbExtraBytes: %s", szExtraBytes);
-                       free(szExtraBytes);
+                       char buffer[1024];
+                       WLog_LVL(TAG, g_LogLevel, "pbExtraBytes: %s",
+                                smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
                }
        }
        else
        {
-               WLog_DBG(TAG, "pioRecvPci: null");
+               WLog_LVL(TAG, g_LogLevel, "pioRecvPci: null");
        }
 
-       WLog_DBG(TAG, "cbRecvLength: %" PRIu32 "", ret->cbRecvLength);
+       WLog_LVL(TAG, g_LogLevel, "cbRecvLength: %" PRIu32 "", ret->cbRecvLength);
 
        if (ret->pbRecvBuffer)
        {
-               char* szRecvBuffer = winpr_BinToHexString(ret->pbRecvBuffer, ret->cbRecvLength, TRUE);
-               WLog_DBG(TAG, "pbRecvBuffer: %s", szRecvBuffer);
-               free(szRecvBuffer);
+               char buffer[1024];
+               WLog_DBG(
+                   TAG, "pbRecvBuffer: %s",
+                   smartcard_array_dump(ret->pbRecvBuffer, ret->cbRecvLength, buffer, sizeof(buffer)));
        }
        else
        {
-               WLog_DBG(TAG, "pbRecvBuffer: null");
+               WLog_LVL(TAG, g_LogLevel, "pbRecvBuffer: null");
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_control_return(SMARTCARD_DEVICE* smartcard, const Control_Return* ret)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Control_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "cbOutBufferSize: %" PRIu32 "", ret->cbOutBufferSize);
+       WLog_LVL(TAG, g_LogLevel, "Control_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "cbOutBufferSize: %" PRIu32 "", ret->cbOutBufferSize);
 
        if (ret->pvOutBuffer)
        {
-               char* szOutBuffer = winpr_BinToHexString(ret->pvOutBuffer, ret->cbOutBufferSize, TRUE);
-               WLog_DBG(TAG, "pvOutBuffer: %s", szOutBuffer);
-               free(szOutBuffer);
+               char buffer[1024];
+               WLog_DBG(
+                   TAG, "pvOutBuffer: %s",
+                   smartcard_array_dump(ret->pvOutBuffer, ret->cbOutBufferSize, buffer, sizeof(buffer)));
        }
        else
        {
-               WLog_DBG(TAG, "pvOutBuffer: null");
+               WLog_LVL(TAG, g_LogLevel, "pvOutBuffer: null");
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_control_call(SMARTCARD_DEVICE* smartcard, const Control_Call* call)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Control_Call {");
+       WLog_LVL(TAG, g_LogLevel, "Control_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "dwControlCode: 0x%08" PRIX32 " cbInBufferSize: %" PRIu32
                 " fpvOutBufferIsNULL: %" PRId32 " cbOutBufferSize: %" PRIu32 "",
                 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
@@ -1073,16 +1147,17 @@ static void smartcard_trace_control_call(SMARTCARD_DEVICE* smartcard, const Cont
 
        if (call->pvInBuffer)
        {
-               char* szInBuffer = winpr_BinToHexString(call->pvInBuffer, call->cbInBufferSize, TRUE);
-               WLog_DBG(TAG, "pbInBuffer: %s", szInBuffer);
-               free(szInBuffer);
+               char buffer[1024];
+               WLog_DBG(
+                   TAG, "pbInBuffer: %s",
+                   smartcard_array_dump(call->pvInBuffer, call->cbInBufferSize, buffer, sizeof(buffer)));
        }
        else
        {
-               WLog_DBG(TAG, "pvInBuffer: null");
+               WLog_LVL(TAG, g_LogLevel, "pvInBuffer: null");
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_set_attrib_call(SMARTCARD_DEVICE* smartcard, const SetAttrib_Call* call)
@@ -1090,17 +1165,17 @@ static void smartcard_trace_set_attrib_call(SMARTCARD_DEVICE* smartcard, const S
        char buffer[8192];
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetAttrib_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetAttrib_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
-       WLog_DBG(TAG, "dwAttrId: 0x%08" PRIX32, call->dwAttrId);
-       WLog_DBG(TAG, "cbAttrLen: 0x%08" PRId32, call->cbAttrLen);
-       WLog_DBG(TAG, "pbAttr: %s",
+       WLog_LVL(TAG, g_LogLevel, "dwAttrId: 0x%08" PRIX32, call->dwAttrId);
+       WLog_LVL(TAG, g_LogLevel, "cbAttrLen: 0x%08" PRId32, call->cbAttrLen);
+       WLog_LVL(TAG, g_LogLevel, "pbAttr: %s",
                 smartcard_array_dump(call->pbAttr, call->cbAttrLen, buffer, sizeof(buffer)));
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_attrib_return(SMARTCARD_DEVICE* smartcard,
@@ -1108,45 +1183,45 @@ static void smartcard_trace_get_attrib_return(SMARTCARD_DEVICE* smartcard,
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetAttrib_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "dwAttrId: %s (0x%08" PRIX32 ") cbAttrLen: 0x%08" PRIX32 "",
+       WLog_LVL(TAG, g_LogLevel, "GetAttrib_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "dwAttrId: %s (0x%08" PRIX32 ") cbAttrLen: 0x%08" PRIX32 "",
                 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
 
        if (dwAttrId == SCARD_ATTR_VENDOR_NAME)
        {
-               WLog_DBG(TAG, "pbAttr: %.*s", ret->cbAttrLen, (char*)ret->pbAttr);
+               WLog_LVL(TAG, g_LogLevel, "pbAttr: %.*s", ret->cbAttrLen, (char*)ret->pbAttr);
        }
        else if (dwAttrId == SCARD_ATTR_CURRENT_PROTOCOL_TYPE)
        {
                UINT32 dwProtocolType = *((UINT32*)ret->pbAttr);
-               WLog_DBG(TAG, "dwProtocolType: %s (0x%08" PRIX32 ")",
+               WLog_LVL(TAG, g_LogLevel, "dwProtocolType: %s (0x%08" PRIX32 ")",
                         SCardGetProtocolString(dwProtocolType), dwProtocolType);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_get_attrib_call(SMARTCARD_DEVICE* smartcard, const GetAttrib_Call* call)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetAttrib_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetAttrib_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "dwAttrId: %s (0x%08" PRIX32 ") fpbAttrIsNULL: %" PRId32 " cbAttrLen: 0x%08" PRIX32 "",
                 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
                 call->cbAttrLen);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_status_call(SMARTCARD_DEVICE* smartcard, const Status_Call* call,
@@ -1154,45 +1229,66 @@ static void smartcard_trace_status_call(SMARTCARD_DEVICE* smartcard, const Statu
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Status%s_Call {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "Status%s_Call {", unicode ? "W" : "A");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "fmszReaderNamesIsNULL: %" PRId32 " cchReaderLen: %" PRIu32 " cbAtrLen: %" PRIu32 "",
                 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_status_return(SMARTCARD_DEVICE* smartcard, const Status_Return* ret,
                                           BOOL unicode)
 {
-       char* pbAtr = NULL;
        char* mszReaderNamesA = NULL;
+       char buffer[1024];
+
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
        mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, ret->cBytes, unicode);
 
-       pbAtr = winpr_BinToHexString(ret->pbAtr, ret->cbAtrLen, FALSE);
-       WLog_DBG(TAG, "Status%s_Return {", unicode ? "W" : "A");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "dwState: %s (0x%08" PRIX32 ") dwProtocol: %s (0x%08" PRIX32 ")",
+       WLog_LVL(TAG, g_LogLevel, "Status%s_Return {", unicode ? "W" : "A");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "dwState: %s (0x%08" PRIX32 ") dwProtocol: %s (0x%08" PRIX32 ")",
                 SCardGetCardStateString(ret->dwState), ret->dwState,
                 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
 
-       WLog_DBG(TAG, "cBytes: %" PRIu32 " mszReaderNames: %s", ret->cBytes, mszReaderNamesA);
+       WLog_LVL(TAG, g_LogLevel, "cBytes: %" PRIu32 " mszReaderNames: %s", ret->cBytes,
+                mszReaderNamesA);
 
-       WLog_DBG(TAG, "cbAtrLen: %" PRIu32 " pbAtr: %s", ret->cbAtrLen, pbAtr);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "cbAtrLen: %" PRIu32 " pbAtr: %s", ret->cbAtrLen,
+                smartcard_array_dump(ret->pbAtr, ret->cbAtrLen, buffer, sizeof(buffer)));
+       WLog_LVL(TAG, g_LogLevel, "}");
        free(mszReaderNamesA);
-       free(pbAtr);
+}
+
+static void smartcard_trace_state_return(SMARTCARD_DEVICE* smartcard, const State_Return* ret)
+{
+       char buffer[1024];
+
+       WINPR_UNUSED(smartcard);
+
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
+               return;
+
+       WLog_LVL(TAG, g_LogLevel, "Reconnect_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "dwState:    %s (0x%08" PRIX32 ")", ret->dwState);
+       WLog_LVL(TAG, g_LogLevel, "dwProtocol: %s (0x%08" PRIX32 ")", ret->dwProtocol);
+       WLog_LVL(TAG, g_LogLevel, "cbAtrLen:   %s (0x%08" PRIX32 ")", ret->cbAtrLen);
+       WLog_LVL(TAG, g_LogLevel, "rgAtr:      %s",
+                smartcard_array_dump(ret->rgAtr, sizeof(ret->rgAtr), buffer, sizeof(buffer)));
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_reconnect_return(SMARTCARD_DEVICE* smartcard,
@@ -1200,34 +1296,34 @@ static void smartcard_trace_reconnect_return(SMARTCARD_DEVICE* smartcard,
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Reconnect_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "dwActiveProtocol: %s (0x%08" PRIX32 ")",
+       WLog_LVL(TAG, g_LogLevel, "Reconnect_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "dwActiveProtocol: %s (0x%08" PRIX32 ")",
                 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_connect_a_call(SMARTCARD_DEVICE* smartcard, const ConnectA_Call* call)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "ConnectA_Call {");
+       WLog_LVL(TAG, g_LogLevel, "ConnectA_Call {");
        smartcard_log_context(TAG, &call->Common.hContext);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "szReader: %s dwShareMode: %s (0x%08" PRIX32 ") dwPreferredProtocols: %s (0x%08" PRIX32
                 ")",
                 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
                 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
                 call->Common.dwPreferredProtocols);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_connect_w_call(SMARTCARD_DEVICE* smartcard, const ConnectW_Call* call)
@@ -1235,20 +1331,20 @@ static void smartcard_trace_connect_w_call(SMARTCARD_DEVICE* smartcard, const Co
        char* szReaderA = NULL;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
        ConvertFromUnicode(CP_UTF8, 0, call->szReader, -1, &szReaderA, 0, NULL, NULL);
-       WLog_DBG(TAG, "ConnectW_Call {");
+       WLog_LVL(TAG, g_LogLevel, "ConnectW_Call {");
        smartcard_log_context(TAG, &call->Common.hContext);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "szReader: %s dwShareMode: %s (0x%08" PRIX32 ") dwPreferredProtocols: %s (0x%08" PRIX32
                 ")",
                 szReaderA, SCardGetShareModeString(call->Common.dwShareMode), call->Common.dwShareMode,
                 SCardGetProtocolString(call->Common.dwPreferredProtocols),
                 call->Common.dwPreferredProtocols);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
        free(szReaderA);
 }
 
@@ -1258,16 +1354,16 @@ static void smartcard_trace_hcard_and_disposition_call(SMARTCARD_DEVICE* smartca
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "%s_Call {", name);
+       WLog_LVL(TAG, g_LogLevel, "%s_Call {", name);
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG, "dwDisposition: %s (0x%08" PRIX32 ")",
+       WLog_LVL(TAG, g_LogLevel, "dwDisposition: %s (0x%08" PRIX32 ")",
                 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_establish_context_call(SMARTCARD_DEVICE* smartcard,
@@ -1275,13 +1371,13 @@ static void smartcard_trace_establish_context_call(SMARTCARD_DEVICE* smartcard,
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "EstablishContext_Call {");
-       WLog_DBG(TAG, "dwScope: %s (0x%08" PRIX32 ")", SCardGetScopeString(call->dwScope),
+       WLog_LVL(TAG, g_LogLevel, "EstablishContext_Call {");
+       WLog_LVL(TAG, g_LogLevel, "dwScope: %s (0x%08" PRIX32 ")", SCardGetScopeString(call->dwScope),
                 call->dwScope);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_establish_context_return(SMARTCARD_DEVICE* smartcard,
@@ -1289,15 +1385,15 @@ static void smartcard_trace_establish_context_return(SMARTCARD_DEVICE* smartcard
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "EstablishContext_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "EstablishContext_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
        smartcard_log_context(TAG, &ret->hContext);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 void smartcard_trace_long_return(SMARTCARD_DEVICE* smartcard, const Long_Return* ret,
@@ -1305,51 +1401,51 @@ void smartcard_trace_long_return(SMARTCARD_DEVICE* smartcard, const Long_Return*
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "%s_Return {", name);
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "%s_Return {", name);
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
-void smartcard_trace_connect_return(SMARTCARD_DEVICE* smartcard, const Connect_Return* ret)
+static void smartcard_trace_connect_return(SMARTCARD_DEVICE* smartcard, const Connect_Return* ret)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Connect_Return {");
-       WLog_DBG(TAG, "ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "Connect_Return {");
+       WLog_LVL(TAG, g_LogLevel, "ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
        smartcard_log_context(TAG, &ret->hContext);
        smartcard_log_redir_handle(TAG, &ret->hCard);
 
-       WLog_DBG(TAG, "dwActiveProtocol: %s (0x%08" PRIX32 ")",
+       WLog_LVL(TAG, g_LogLevel, "dwActiveProtocol: %s (0x%08" PRIX32 ")",
                 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 void smartcard_trace_reconnect_call(SMARTCARD_DEVICE* smartcard, const Reconnect_Call* call)
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "Reconnect_Call {");
+       WLog_LVL(TAG, g_LogLevel, "Reconnect_Call {");
        smartcard_log_context(TAG, &call->hContext);
        smartcard_log_redir_handle(TAG, &call->hCard);
 
-       WLog_DBG(TAG,
+       WLog_LVL(TAG, g_LogLevel,
                 "dwShareMode: %s (0x%08" PRIX32 ") dwPreferredProtocols: %s (0x%08" PRIX32
                 ") dwInitialization: %s (0x%08" PRIX32 ")",
                 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
                 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
                 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static void smartcard_trace_device_type_id_return(SMARTCARD_DEVICE* smartcard,
@@ -1357,15 +1453,15 @@ static void smartcard_trace_device_type_id_return(SMARTCARD_DEVICE* smartcard,
 {
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetDeviceTypeId_Return {");
-       WLog_DBG(TAG, "  ReturnCode: %s (0x%08" PRIX32 ")", SCardGetErrorString(ret->ReturnCode),
-                ret->ReturnCode);
-       WLog_DBG(TAG, "  dwDeviceId=%08" PRIx32, ret->dwDeviceId);
+       WLog_LVL(TAG, g_LogLevel, "GetDeviceTypeId_Return {");
+       WLog_LVL(TAG, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
+                SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
+       WLog_LVL(TAG, g_LogLevel, "  dwDeviceId=%08" PRIx32, ret->dwDeviceId);
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static LONG smartcard_unpack_common_context_and_string_a(SMARTCARD_DEVICE* smartcard, wStream* s,
@@ -1671,13 +1767,20 @@ LONG smartcard_unpack_redir_scard_context_(SMARTCARD_DEVICE* smartcard, wStream*
 }
 
 LONG smartcard_pack_redir_scard_context(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                        const REDIR_SCARDCONTEXT* context)
+                                        const REDIR_SCARDCONTEXT* context, DWORD* index)
 {
-       UINT32 pbContextNdrPtr;
+       const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
        WINPR_UNUSED(smartcard);
-       pbContextNdrPtr = (context->cbContext) ? 0x00020001 : 0;
-       Stream_Write_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
-       Stream_Write_UINT32(s, pbContextNdrPtr);    /* pbContextNdrPtr (4 bytes) */
+
+       if (context->cbContext != 0)
+       {
+               Stream_Write_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
+               Stream_Write_UINT32(s, pbContextNdrPtr);    /* pbContextNdrPtr (4 bytes) */
+               *index = *index + 1;
+       }
+       else
+               Stream_Zero(s, 8);
+
        return SCARD_S_SUCCESS;
 }
 
@@ -1770,13 +1873,19 @@ LONG smartcard_unpack_redir_scard_handle_(SMARTCARD_DEVICE* smartcard, wStream*
 }
 
 LONG smartcard_pack_redir_scard_handle(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                       const REDIR_SCARDHANDLE* handle)
+                                       const REDIR_SCARDHANDLE* handle, DWORD* index)
 {
-       UINT32 pbHandleNdrPtr;
+       const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
        WINPR_UNUSED(smartcard);
-       pbHandleNdrPtr = (handle->cbHandle) ? 0x00020002 : 0;
-       Stream_Write_UINT32(s, handle->cbHandle); /* cbHandle (4 bytes) */
-       Stream_Write_UINT32(s, pbHandleNdrPtr);   /* pbHandleNdrPtr (4 bytes) */
+
+       if (handle->cbHandle != 0)
+       {
+               Stream_Write_UINT32(s, handle->cbHandle); /* cbContext (4 bytes) */
+               Stream_Write_UINT32(s, pbContextNdrPtr);  /* pbContextNdrPtr (4 bytes) */
+               *index = *index + 1;
+       }
+       else
+               Stream_Zero(s, 8);
        return SCARD_S_SUCCESS;
 }
 
@@ -1854,19 +1963,16 @@ LONG smartcard_pack_establish_context_return(SMARTCARD_DEVICE* smartcard, wStrea
                                              const EstablishContext_Return* ret)
 {
        LONG status;
+       DWORD index = 0;
 
        smartcard_trace_establish_context_return(smartcard, ret);
-       if ((status = smartcard_pack_redir_scard_context(smartcard, s, &(ret->hContext))))
-       {
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_context failed with error %" PRId32 "", status);
-               return status;
-       }
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
 
-       if ((status = smartcard_pack_redir_scard_context_ref(smartcard, s, &(ret->hContext))))
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_context_ref failed with error %" PRId32 "",
-                        status);
+       if ((status = smartcard_pack_redir_scard_context(smartcard, s, &(ret->hContext), &index)))
+               return status;
 
-       return status;
+       return smartcard_pack_redir_scard_context_ref(smartcard, s, &(ret->hContext));
 }
 
 LONG smartcard_unpack_context_call(SMARTCARD_DEVICE* smartcard, wStream* s, Context_Call* call,
@@ -1917,27 +2023,19 @@ LONG smartcard_unpack_list_reader_groups_call(SMARTCARD_DEVICE* smartcard, wStre
 LONG smartcard_pack_list_reader_groups_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                               const ListReaderGroups_Return* ret)
 {
-       UINT32 mszNdrPtr;
+       DWORD index = 0;
        smartcard_trace_list_reader_groups_return(smartcard, ret, FALSE);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
 
-       mszNdrPtr = (ret->cBytes) ? 0x00020008 : 0;
-       Stream_EnsureRemainingCapacity(s, ret->cBytes + 32);
-       Stream_Write_UINT32(s, ret->cBytes); /* cBytes (4 bytes) */
-       Stream_Write_UINT32(s, mszNdrPtr);   /* mszNdrPtr (4 bytes) */
-
-       if (mszNdrPtr)
-       {
-               Stream_Write_UINT32(s, ret->cBytes); /* mszNdrLen (4 bytes) */
-
-               if (ret->msz)
-                       Stream_Write(s, ret->msz, ret->cBytes);
-               else
-                       Stream_Zero(s, ret->cBytes);
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return SCARD_E_NO_MEMORY;
 
-               smartcard_pack_write_size_align(smartcard, s, ret->cBytes, 4);
-       }
+       Stream_Write_UINT32(s, ret->cBytes); /* cBytes (4 bytes) */
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cBytes))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->msz, ret->cBytes, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_list_readers_call(SMARTCARD_DEVICE* smartcard, wStream* s,
@@ -1981,41 +2079,24 @@ LONG smartcard_unpack_list_readers_call(SMARTCARD_DEVICE* smartcard, wStream* s,
 LONG smartcard_pack_list_readers_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                         const ListReaders_Return* ret, BOOL unicode)
 {
-       UINT32 mszNdrPtr;
-       LONG error;
+       DWORD index = 0;
 
        smartcard_trace_list_readers_return(smartcard, ret, unicode);
        if (ret->ReturnCode != SCARD_S_SUCCESS)
                return ret->ReturnCode;
 
-       mszNdrPtr = (ret->cBytes) ? 0x00020008 : 0;
-
-       if (!Stream_EnsureRemainingCapacity(s, ret->cBytes + 32))
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
        }
 
        Stream_Write_UINT32(s, ret->cBytes); /* cBytes (4 bytes) */
-       Stream_Write_UINT32(s, mszNdrPtr);   /* mszNdrPtr (4 bytes) */
-
-       if (mszNdrPtr)
-       {
-               Stream_Write_UINT32(s, ret->cBytes); /* mszNdrLen (4 bytes) */
-
-               if (ret->msz)
-                       Stream_Write(s, ret->msz, ret->cBytes);
-               else
-                       Stream_Zero(s, ret->cBytes);
-
-               if ((error = smartcard_pack_write_size_align(smartcard, s, ret->cBytes, 4)))
-               {
-                       WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", error);
-                       return error;
-               }
-       }
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cBytes))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->msz, ret->cBytes, unicode ? sizeof(WCHAR) : sizeof(CHAR),
+                                  NDR_PTR_SIMPLE);
 }
 
 static LONG smartcard_unpack_connect_common(SMARTCARD_DEVICE* smartcard, wStream* s,
@@ -2093,36 +2174,32 @@ LONG smartcard_unpack_connect_w_call(SMARTCARD_DEVICE* smartcard, wStream* s, Co
        return status;
 }
 
-LONG smartcard_pack_connect_return(SMARTCARD_DEVICE* smartcard, wStream* s, Connect_Return* ret)
+LONG smartcard_pack_connect_return(SMARTCARD_DEVICE* smartcard, wStream* s,
+                                   const Connect_Return* ret)
 {
        LONG status;
+       DWORD index = 0;
 
-       if ((status = smartcard_pack_redir_scard_context(smartcard, s, &(ret->hContext))))
-       {
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_context failed with error %" PRId32 "", status);
-               return status;
-       }
+       smartcard_trace_connect_return(smartcard, ret);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
 
-       if ((status = smartcard_pack_redir_scard_handle(smartcard, s, &(ret->hCard))))
-       {
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_handle failed with error %" PRId32 "", status);
+       status = smartcard_pack_redir_scard_context(smartcard, s, &ret->hContext, &index);
+       if (status)
                return status;
-       }
 
-       Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
-
-       if ((status = smartcard_pack_redir_scard_context_ref(smartcard, s, &(ret->hContext))))
-       {
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_context_ref failed with error %" PRId32 "",
-                        status);
+       status = smartcard_pack_redir_scard_handle(smartcard, s, &ret->hCard, &index);
+       if (status)
                return status;
-       }
 
-       if ((status = smartcard_pack_redir_scard_handle_ref(smartcard, s, &(ret->hCard))))
-               WLog_ERR(TAG, "smartcard_pack_redir_scard_handle_ref failed with error %" PRId32 "",
-                        status);
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return SCARD_E_NO_MEMORY;
 
-       return status;
+       Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
+       status = smartcard_pack_redir_scard_context_ref(smartcard, s, &ret->hContext);
+       if (status)
+               return status;
+       return smartcard_pack_redir_scard_handle_ref(smartcard, s, &(ret->hCard));
 }
 
 LONG smartcard_unpack_reconnect_call(SMARTCARD_DEVICE* smartcard, wStream* s, Reconnect_Call* call)
@@ -2166,8 +2243,12 @@ LONG smartcard_unpack_reconnect_call(SMARTCARD_DEVICE* smartcard, wStream* s, Re
 LONG smartcard_pack_reconnect_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                      const Reconnect_Return* ret)
 {
-       WINPR_UNUSED(smartcard);
        smartcard_trace_reconnect_return(smartcard, ret);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return SCARD_E_NO_MEMORY;
        Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
        return SCARD_S_SUCCESS;
 }
@@ -2214,31 +2295,31 @@ static void smartcard_trace_get_status_change_a_call(SMARTCARD_DEVICE* smartcard
        LPSCARD_READERSTATEA readerState;
        WINPR_UNUSED(smartcard);
 
-       if (!WLog_IsLevelActive(WLog_Get(TAG), WLOG_DEBUG))
+       if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
                return;
 
-       WLog_DBG(TAG, "GetStatusChangeA_Call {");
+       WLog_LVL(TAG, g_LogLevel, "GetStatusChangeA_Call {");
        smartcard_log_context(TAG, &call->hContext);
 
-       WLog_DBG(TAG, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
+       WLog_LVL(TAG, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
                 call->cReaders);
 
        for (index = 0; index < call->cReaders; index++)
        {
                readerState = &call->rgReaderStates[index];
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
                         readerState->szReader, readerState->cbAtr);
                szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
                szEventState = SCardGetReaderStateString(readerState->dwEventState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
                         szCurrentState, readerState->dwCurrentState);
-               WLog_DBG(TAG, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index, szEventState,
-                        readerState->dwEventState);
+               WLog_LVL(TAG, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
+                        szEventState, readerState->dwEventState);
                free(szCurrentState);
                free(szEventState);
        }
 
-       WLog_DBG(TAG, "}");
+       WLog_LVL(TAG, g_LogLevel, "}");
 }
 
 static LONG smartcard_unpack_reader_state_a(wStream* s, LPSCARD_READERSTATEA* ppcReaders,
@@ -2278,8 +2359,7 @@ static LONG smartcard_unpack_reader_state_a(wStream* s, LPSCARD_READERSTATEA* pp
                Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
                Stream_Read_UINT32(s, readerState->dwEventState);   /* dwEventState (4 bytes) */
                Stream_Read_UINT32(s, readerState->cbAtr);          /* cbAtr (4 bytes) */
-               Stream_Read(s, readerState->rgbAtr, 32);            /* rgbAtr [0..32] (32 bytes) */
-               Stream_Seek(s, 4);                                  /* rgbAtr [32..36] (4 bytes) */
+               Stream_Read(s, readerState->rgbAtr, 36);            /* rgbAtr [0..36] (36 bytes) */
        }
 
        for (index = 0; index < cReaders; index++)
@@ -2344,8 +2424,7 @@ static LONG smartcard_unpack_reader_state_w(wStream* s, LPSCARD_READERSTATEW* pp
                Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
                Stream_Read_UINT32(s, readerState->dwEventState);   /* dwEventState (4 bytes) */
                Stream_Read_UINT32(s, readerState->cbAtr);          /* cbAtr (4 bytes) */
-               Stream_Read(s, readerState->rgbAtr, 32);            /* rgbAtr [0..32] (32 bytes) */
-               Stream_Seek(s, 4);                                  /* rgbAtr [32..36] (4 bytes) */
+               Stream_Read(s, readerState->rgbAtr, 36);            /* rgbAtr [0..36] (36 bytes) */
        }
 
        for (index = 0; index < cReaders; index++)
@@ -2456,27 +2535,19 @@ LONG smartcard_unpack_get_status_change_w_call(SMARTCARD_DEVICE* smartcard, wStr
 LONG smartcard_pack_get_status_change_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                              const GetStatusChange_Return* ret, BOOL unicode)
 {
-       UINT32 index;
-       ReaderState_Return* rgReaderState;
-       WINPR_UNUSED(smartcard);
+       UINT32 index = 0;
 
        smartcard_trace_get_status_change_return(smartcard, ret, unicode);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
 
-       Stream_Write_UINT32(s, ret->cReaders); /* cReaders (4 bytes) */
-       Stream_Write_UINT32(s, 0x00020100);    /* rgReaderStatesNdrPtr (4 bytes) */
-       Stream_Write_UINT32(s, ret->cReaders); /* rgReaderStatesNdrCount (4 bytes) */
-
-       for (index = 0; index < ret->cReaders; index++)
-       {
-               rgReaderState = &(ret->rgReaderStates[index]);
-               Stream_Write_UINT32(s, rgReaderState->dwCurrentState); /* dwCurrentState (4 bytes) */
-               Stream_Write_UINT32(s, rgReaderState->dwEventState);   /* dwEventState (4 bytes) */
-               Stream_Write_UINT32(s, rgReaderState->cbAtr);          /* cbAtr (4 bytes) */
-               Stream_Write(s, rgReaderState->rgbAtr, 32);            /* rgbAtr [0..32] (32 bytes) */
-               Stream_Zero(s, 4);                                     /* rgbAtr [32..36] (32 bytes) */
-       }
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       Stream_Write_UINT32(s, ret->cReaders); /* cReaders (4 bytes) */
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cReaders))
+               return SCARD_E_NO_MEMORY;
+       return smartcard_ndr_write_state(s, ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_state_call(SMARTCARD_DEVICE* smartcard, wStream* s, State_Call* call)
@@ -2512,18 +2583,18 @@ LONG smartcard_unpack_state_call(SMARTCARD_DEVICE* smartcard, wStream* s, State_
 
 LONG smartcard_pack_state_return(SMARTCARD_DEVICE* smartcard, wStream* s, const State_Return* ret)
 {
-       LONG status;
+       DWORD index = 0;
+
+       smartcard_trace_state_return(smartcard, ret);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
        Stream_Write_UINT32(s, ret->dwState);       /* dwState (4 bytes) */
        Stream_Write_UINT32(s, ret->dwProtocol);    /* dwProtocol (4 bytes) */
        Stream_Write_UINT32(s, ret->cbAtrLen);      /* cbAtrLen (4 bytes) */
-       Stream_Write_UINT32(s, 0x00020020);         /* rgAtrNdrPtr (4 bytes) */
-       Stream_Write_UINT32(s, ret->cbAtrLen);      /* rgAtrLength (4 bytes) */
-       Stream_Write(s, ret->rgAtr, ret->cbAtrLen); /* rgAtr */
-
-       if ((status = smartcard_pack_write_size_align(smartcard, s, ret->cbAtrLen, 4)))
-               WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", status);
-
-       return status;
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cbAtrLen))
+               return SCARD_E_NO_MEMORY;
+       return smartcard_ndr_write(s, ret->rgAtr, ret->cbAtrLen, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_status_call(SMARTCARD_DEVICE* smartcard, wStream* s, Status_Call* call,
@@ -2562,32 +2633,28 @@ LONG smartcard_unpack_status_call(SMARTCARD_DEVICE* smartcard, wStream* s, Statu
 LONG smartcard_pack_status_return(SMARTCARD_DEVICE* smartcard, wStream* s, const Status_Return* ret,
                                   BOOL unicode)
 {
-       LONG status;
+       DWORD index = 0;
 
        smartcard_trace_status_return(smartcard, ret, unicode);
-       if (!Stream_EnsureRemainingCapacity(s, ret->cBytes + 64))
-       {
-               WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
                return SCARD_F_INTERNAL_ERROR;
-       }
 
        Stream_Write_UINT32(s, ret->cBytes);     /* cBytes (4 bytes) */
-       Stream_Write_UINT32(s, 0x00020010);      /* mszReaderNamesNdrPtr (4 bytes) */
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cBytes))
+               return SCARD_E_NO_MEMORY;
+
+       if (!Stream_EnsureRemainingCapacity(s, 44))
+               return SCARD_F_INTERNAL_ERROR;
+
        Stream_Write_UINT32(s, ret->dwState);    /* dwState (4 bytes) */
        Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
        Stream_Write(s, ret->pbAtr, 32);         /* pbAtr (32 bytes) */
        Stream_Write_UINT32(s, ret->cbAtrLen);   /* cbAtrLen (4 bytes) */
-       Stream_Write_UINT32(s, ret->cBytes);     /* mszReaderNamesNdrLen (4 bytes) */
-
-       if (ret->mszReaderNames)
-               Stream_Write(s, ret->mszReaderNames, ret->cBytes);
-       else
-               Stream_Zero(s, ret->cBytes);
-
-       if ((status = smartcard_pack_write_size_align(smartcard, s, ret->cBytes, 4)))
-               WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", status);
-
-       return status;
+       return smartcard_ndr_write(s, ret->mszReaderNames, ret->cBytes,
+                                  unicode ? sizeof(WCHAR) : sizeof(CHAR), NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_get_attrib_call(SMARTCARD_DEVICE* smartcard, wStream* s, GetAttrib_Call* call)
@@ -2626,28 +2693,19 @@ LONG smartcard_unpack_get_attrib_call(SMARTCARD_DEVICE* smartcard, wStream* s, G
 LONG smartcard_pack_get_attrib_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                       const GetAttrib_Return* ret, DWORD dwAttrId)
 {
-       LONG status;
-
+       DWORD index = 0;
        smartcard_trace_get_attrib_return(smartcard, ret, dwAttrId);
-       if (!Stream_EnsureRemainingCapacity(s, ret->cbAttrLen + 32))
-       {
-               WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
                return SCARD_F_INTERNAL_ERROR;
-       }
 
        Stream_Write_UINT32(s, ret->cbAttrLen); /* cbAttrLen (4 bytes) */
-       Stream_Write_UINT32(s, 0x00020080);     /* pbAttrNdrPtr (4 bytes) */
-       Stream_Write_UINT32(s, ret->cbAttrLen); /* pbAttrNdrCount (4 bytes) */
-
-       if (!ret->pbAttr)
-               Stream_Zero(s, ret->cbAttrLen); /* pbAttr */
-       else
-               Stream_Write(s, ret->pbAttr, ret->cbAttrLen); /* pbAttr */
-
-       if ((status = smartcard_pack_write_size_align(smartcard, s, ret->cbAttrLen, 4)))
-               WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", status);
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cbAttrLen))
+               return SCARD_E_NO_MEMORY;
 
-       return status;
+       return smartcard_ndr_write(s, ret->pbAttr, ret->cbAttrLen, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_control_call(SMARTCARD_DEVICE* smartcard, wStream* s, Control_Call* call)
@@ -2699,31 +2757,20 @@ LONG smartcard_unpack_control_call(SMARTCARD_DEVICE* smartcard, wStream* s, Cont
 LONG smartcard_pack_control_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                    const Control_Return* ret)
 {
-       LONG error;
+       DWORD index = 0;
 
        smartcard_trace_control_return(smartcard, ret);
-       if (!Stream_EnsureRemainingCapacity(s, ret->cbOutBufferSize + 32))
-       {
-               WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
                return SCARD_F_INTERNAL_ERROR;
-       }
 
        Stream_Write_UINT32(s, ret->cbOutBufferSize); /* cbOutBufferSize (4 bytes) */
-       Stream_Write_UINT32(s, 0x00020040);           /* pvOutBufferPointer (4 bytes) */
-       Stream_Write_UINT32(s, ret->cbOutBufferSize); /* pvOutBufferLength (4 bytes) */
-
-       if (ret->cbOutBufferSize > 0)
-       {
-               Stream_Write(s, ret->pvOutBuffer, ret->cbOutBufferSize); /* pvOutBuffer */
-
-               if ((error = smartcard_pack_write_size_align(smartcard, s, ret->cbOutBufferSize, 4)))
-               {
-                       WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", error);
-                       return error;
-               }
-       }
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cbOutBufferSize))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->pvOutBuffer, ret->cbOutBufferSize, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_transmit_call(SMARTCARD_DEVICE* smartcard, wStream* s, Transmit_Call* call)
@@ -2960,29 +3007,30 @@ LONG smartcard_unpack_transmit_call(SMARTCARD_DEVICE* smartcard, wStream* s, Tra
 LONG smartcard_pack_transmit_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                     const Transmit_Return* ret)
 {
-       UINT32 cbExtraBytes;
-       BYTE* pbExtraBytes;
-       UINT32 pioRecvPciNdrPtr;
-       UINT32 pbRecvBufferNdrPtr;
-       UINT32 pbExtraBytesNdrPtr;
+       DWORD index = 0;
        LONG error;
        UINT32 cbRecvLength = ret->cbRecvLength;
+       UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
 
        smartcard_trace_transmit_return(smartcard, ret);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
        if (!ret->pbRecvBuffer)
                cbRecvLength = 0;
 
-       pioRecvPciNdrPtr = (ret->pioRecvPci) ? 0x00020000 : 0;
-       pbRecvBufferNdrPtr = (ret->pbRecvBuffer) ? 0x00020004 : 0;
-       Stream_Write_UINT32(s, pioRecvPciNdrPtr);   /* pioRecvPciNdrPtr (4 bytes) */
-       Stream_Write_UINT32(s, cbRecvLength);       /* cbRecvLength (4 bytes) */
-       Stream_Write_UINT32(s, pbRecvBufferNdrPtr); /* pbRecvBufferNdrPtr (4 bytes) */
+       if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
+               return SCARD_E_NO_MEMORY;
+       if (!Stream_EnsureRemainingCapacity(s, 4))
+               return SCARD_E_NO_MEMORY;
+       Stream_Write_UINT32(s, cbRecvLength); /* cbRecvLength (4 bytes) */
+       if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
+               return SCARD_E_NO_MEMORY;
 
-       if (pioRecvPciNdrPtr)
+       if (ret->pioRecvPci)
        {
-               cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
-               pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
-               pbExtraBytesNdrPtr = cbExtraBytes ? 0x00020008 : 0;
+               UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
+               BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
 
                if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
                {
@@ -2992,41 +3040,14 @@ LONG smartcard_pack_transmit_return(SMARTCARD_DEVICE* smartcard, wStream* s,
 
                Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol); /* dwProtocol (4 bytes) */
                Stream_Write_UINT32(s, cbExtraBytes);                /* cbExtraBytes (4 bytes) */
-               Stream_Write_UINT32(s, pbExtraBytesNdrPtr);          /* pbExtraBytesNdrPtr (4 bytes) */
-
-               if (pbExtraBytesNdrPtr)
-               {
-                       Stream_Write_UINT32(s, cbExtraBytes); /* Length (4 bytes) */
-                       Stream_Write(s, pbExtraBytes, cbExtraBytes);
-
-                       if ((error = smartcard_pack_write_size_align(smartcard, s, cbExtraBytes, 4)))
-                       {
-                               WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "!",
-                                        error);
-                               return error;
-                       }
-               }
-       }
-
-       if (pbRecvBufferNdrPtr)
-       {
-               if (!Stream_EnsureRemainingCapacity(s, ret->cbRecvLength + 16))
-               {
-                       WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
-                       return SCARD_F_INTERNAL_ERROR;
-               }
-
-               Stream_Write_UINT32(s, ret->cbRecvLength); /* pbRecvBufferNdrLen (4 bytes) */
-               Stream_Write(s, ret->pbRecvBuffer, ret->cbRecvLength);
-
-               if ((error = smartcard_pack_write_size_align(smartcard, s, ret->cbRecvLength, 4)))
-               {
-                       WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "!", error);
+               if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
+                       return SCARD_E_NO_MEMORY;
+               error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
+               if (error)
                        return error;
-               }
        }
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_unpack_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartcard, wStream* s,
@@ -3270,29 +3291,6 @@ LONG smartcard_unpack_set_attrib_call(SMARTCARD_DEVICE* smartcard, wStream* s, S
        return SCARD_S_SUCCESS;
 }
 
-LONG smartcard_pack_set_attrib_return(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                      const GetAttrib_Return* ret)
-{
-       LONG error;
-
-       if (!Stream_EnsureRemainingCapacity(s, ret->cbAttrLen + 8))
-       {
-               WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
-               return SCARD_F_INTERNAL_ERROR;
-       }
-
-       Stream_Write_UINT32(s, ret->cbAttrLen); /* cbOutBufferSize (4 bytes) */
-       Stream_Write(s, ret->pbAttr, ret->cbAttrLen);
-
-       if ((error = smartcard_pack_write_size_align(smartcard, s, ret->cbAttrLen, 4)))
-       {
-               WLog_ERR(TAG, "smartcard_pack_write_size_align failed with error %" PRId32 "", error);
-               return error;
-       }
-
-       return SCARD_S_SUCCESS;
-}
-
 LONG smartcard_unpack_locate_cards_by_atr_w_call(SMARTCARD_DEVICE* smartcard, wStream* s,
                                                  LocateCardsByATRW_Call* call)
 {
@@ -3609,10 +3607,11 @@ LONG smartcard_unpack_get_device_type_id_call(SMARTCARD_DEVICE* smartcard, wStre
 LONG smartcard_pack_device_type_id_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                           const GetDeviceTypeId_Return* ret)
 {
-       WINPR_UNUSED(smartcard);
        smartcard_trace_device_type_id_return(smartcard, ret);
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
 
-       if (!Stream_EnsureRemainingCapacity(s, 8))
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
@@ -3626,78 +3625,53 @@ LONG smartcard_pack_device_type_id_return(SMARTCARD_DEVICE* smartcard, wStream*
 LONG smartcard_pack_locate_cards_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                         const LocateCards_Return* ret)
 {
-
-       UINT32 pbDataNdrPtr = (ret->rgReaderStates) ? 0x00020000 : 0;
+       DWORD index = 0;
        smartcard_trace_locate_cards_return(smartcard, ret);
-       if (!Stream_EnsureRemainingCapacity(s, 8))
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
        }
 
        Stream_Write_UINT32(s, ret->cReaders); /* cBytes (4 cbDataLen) */
-       Stream_Write_UINT32(s, pbDataNdrPtr);
-       if (ret->rgReaderStates)
-       {
-               size_t x, size;
-
-               size = sizeof(ReaderState_Return) * ret->cReaders;
-               if (!Stream_EnsureRemainingCapacity(s, size + 8))
-               {
-                       WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
-                       return SCARD_F_INTERNAL_ERROR;
-               }
-
-               Stream_Write_UINT32(s, (UINT32)size);
-               for (x = 0; x < ret->cReaders; x++)
-               {
-                       ReaderState_Return* reader = &ret->rgReaderStates[x];
-                       Stream_Write_UINT32(s, reader->dwCurrentState);
-                       Stream_Write_UINT32(s, reader->dwEventState);
-                       Stream_Write_UINT32(s, reader->cbAtr);
-                       Stream_Write(s, reader->rgbAtr, sizeof(reader->rgbAtr));
-               }
-               smartcard_pack_write_size_align(smartcard, s, (UINT32)size, 4);
-       }
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cReaders))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write_state(s, ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_pack_get_reader_icon_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                            const GetReaderIcon_Return* ret)
 {
-       UINT32 pbDataNdrPtr = (ret->pbData) ? 0x00020000 : 0;
+       DWORD index = 0;
        smartcard_trace_get_reader_icon_return(smartcard, ret);
-       if (!Stream_EnsureRemainingCapacity(s, 8))
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
        }
 
        Stream_Write_UINT32(s, ret->cbDataLen); /* cBytes (4 cbDataLen) */
-       Stream_Write_UINT32(s, pbDataNdrPtr);
-       if (ret->pbData)
-       {
-               if (!Stream_EnsureRemainingCapacity(s, ret->cbDataLen + 8))
-               {
-                       WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
-                       return SCARD_F_INTERNAL_ERROR;
-               }
-
-               Stream_Write_UINT32(s, ret->cbDataLen);
-               Stream_Write(s, ret->pbData, ret->cbDataLen);
-               smartcard_pack_write_size_align(smartcard, s, ret->cbDataLen, 4);
-       }
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cbDataLen))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->pbData, ret->cbDataLen, 1, NDR_PTR_SIMPLE);
 }
 
 LONG smartcard_pack_get_transmit_count_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                               const GetTransmitCount_Return* ret)
 {
-
        smartcard_trace_get_transmit_count_return(smartcard, ret);
-       if (!Stream_EnsureRemainingCapacity(s, 8))
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
@@ -3711,28 +3685,21 @@ LONG smartcard_pack_get_transmit_count_return(SMARTCARD_DEVICE* smartcard, wStre
 LONG smartcard_pack_read_cache_return(SMARTCARD_DEVICE* smartcard, wStream* s,
                                       const ReadCache_Return* ret)
 {
-       UINT32 pbDataNdrPtr = (ret->pbData) ? 0x00020000 : 0;
+       DWORD index = 0;
+
        smartcard_trace_read_cache_return(smartcard, ret);
-       if (!Stream_EnsureRemainingCapacity(s, 8))
+       if (ret->ReturnCode != SCARD_S_SUCCESS)
+               return ret->ReturnCode;
+
+       if (!Stream_EnsureRemainingCapacity(s, 4))
        {
                WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
                return SCARD_F_INTERNAL_ERROR;
        }
 
        Stream_Write_UINT32(s, ret->cbDataLen); /* cBytes (4 cbDataLen) */
-       Stream_Write_UINT32(s, pbDataNdrPtr);
-       if (ret->pbData)
-       {
-               if (!Stream_EnsureRemainingCapacity(s, ret->cbDataLen + 8))
-               {
-                       WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
-                       return SCARD_F_INTERNAL_ERROR;
-               }
-
-               Stream_Write_UINT32(s, ret->cbDataLen);
-               Stream_Write(s, ret->pbData, ret->cbDataLen);
-               smartcard_pack_write_size_align(smartcard, s, ret->cbDataLen, 4);
-       }
+       if (!smartcard_ndr_pointer_write(s, &index, ret->cbDataLen))
+               return SCARD_E_NO_MEMORY;
 
-       return SCARD_S_SUCCESS;
+       return smartcard_ndr_write(s, ret->pbData, ret->cbDataLen, 1, NDR_PTR_SIMPLE);
 }
index c590210..5bd07d8 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  * Copyright 2015 Thincast Technologies GmbH
  * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -515,8 +517,8 @@ LONG smartcard_unpack_connect_a_call(SMARTCARD_DEVICE* smartcard, wStream* s, Co
 
 LONG smartcard_unpack_connect_w_call(SMARTCARD_DEVICE* smartcard, wStream* s, ConnectW_Call* call);
 
-LONG smartcard_pack_connect_return(SMARTCARD_DEVICE* smartcard, wStream* s, Connect_Return* ret);
-void smartcard_trace_connect_return(SMARTCARD_DEVICE* smartcard, const Connect_Return* ret);
+LONG smartcard_pack_connect_return(SMARTCARD_DEVICE* smartcard, wStream* s,
+                                   const Connect_Return* ret);
 
 LONG smartcard_unpack_reconnect_call(SMARTCARD_DEVICE* smartcard, wStream* s, Reconnect_Call* call);
 
@@ -553,9 +555,6 @@ LONG smartcard_pack_get_attrib_return(SMARTCARD_DEVICE* smartcard, wStream* s,
 LONG smartcard_unpack_set_attrib_call(SMARTCARD_DEVICE* smartcard, wStream* s,
                                       SetAttrib_Call* call);
 
-LONG smartcard_pack_set_attrib_return(SMARTCARD_DEVICE* smartcard, wStream* s,
-                                      const GetAttrib_Return* ret);
-
 LONG smartcard_unpack_control_call(SMARTCARD_DEVICE* smartcard, wStream* s, Control_Call* call);
 
 LONG smartcard_pack_control_return(SMARTCARD_DEVICE* smartcard, wStream* s,
index 7ad016b..3ed85b4 100644 (file)
@@ -9,6 +9,7 @@
  * machine.
  *
  * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
index 8584031..764e552 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
index f9b93a5..22d6b03 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
index b11483b..1406731 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
index c489485..482dd97 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
index 4e5380f..c19cc70 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -242,7 +244,7 @@ static LONG PCSC_SCard_LogError(const char* what)
        return SCARD_E_UNSUPPORTED_FEATURE;
 }
 
-static LONG PCSC_MapErrorCodeToWinSCard(LONG errorCode)
+static LONG PCSC_MapErrorCodeToWinSCard(PCSC_LONG errorCode)
 {
        /**
         * pcsc-lite returns SCARD_E_UNEXPECTED when it
@@ -257,6 +259,7 @@ static LONG PCSC_MapErrorCodeToWinSCard(LONG errorCode)
                if (errorCode == SCARD_E_UNEXPECTED)
                        errorCode = SCARD_E_UNSUPPORTED_FEATURE;
        }
+
        return errorCode;
 }
 
@@ -688,15 +691,14 @@ static LONG WINAPI PCSC_SCardEstablishContext_Internal(DWORD dwScope, LPCVOID pv
                                                        LPSCARDCONTEXT phContext)
 {
        WINPR_UNUSED(dwScope); /* SCARD_SCOPE_SYSTEM is the only scope supported by pcsc-lite */
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
 
        if (!g_PCSC.pfnSCardEstablishContext)
                return PCSC_SCard_LogError("g_PCSC.pfnSCardEstablishContext");
 
-       status = (LONG)g_PCSC.pfnSCardEstablishContext(SCARD_SCOPE_SYSTEM, pvReserved1, pvReserved2,
-                                                      phContext);
-       status = PCSC_MapErrorCodeToWinSCard(status);
-       return status;
+       status =
+           g_PCSC.pfnSCardEstablishContext(SCARD_SCOPE_SYSTEM, pvReserved1, pvReserved2, phContext);
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
@@ -714,7 +716,7 @@ static LONG WINAPI PCSC_SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1
 
 static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
 
        if (!g_PCSC.pfnSCardReleaseContext)
                return PCSC_SCard_LogError("g_PCSC.pfnSCardReleaseContext");
@@ -722,12 +724,11 @@ static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext)
        if (!hContext)
        {
                WLog_ERR(TAG, "SCardReleaseContext: null hContext");
-               return status;
+               return PCSC_MapErrorCodeToWinSCard(status);
        }
 
-       status = (LONG)g_PCSC.pfnSCardReleaseContext(hContext);
-       status = PCSC_MapErrorCodeToWinSCard(status);
-       return status;
+       status = g_PCSC.pfnSCardReleaseContext(hContext);
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardReleaseContext(SCARDCONTEXT hContext)
@@ -744,20 +745,19 @@ static LONG WINAPI PCSC_SCardReleaseContext(SCARDCONTEXT hContext)
 
 static LONG WINAPI PCSC_SCardIsValidContext(SCARDCONTEXT hContext)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
 
        if (!g_PCSC.pfnSCardIsValidContext)
                return PCSC_SCard_LogError("g_PCSC.pfnSCardIsValidContext");
 
-       status = (LONG)g_PCSC.pfnSCardIsValidContext(hContext);
-       status = PCSC_MapErrorCodeToWinSCard(status);
-       return status;
+       status = g_PCSC.pfnSCardIsValidContext(hContext);
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LPSTR mszGroups,
                                                        LPDWORD pcchGroups)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        BOOL pcchGroupsAlloc = FALSE;
        LPSTR* pMszGroups = (LPSTR*)mszGroups;
        PCSC_DWORD pcsc_cchGroups = 0;
@@ -776,7 +776,7 @@ static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LP
        if (pcchGroupsAlloc && !g_SCardAutoAllocate)
        {
                pcsc_cchGroups = 0;
-               status = (LONG)g_PCSC.pfnSCardListReaderGroups(hContext, NULL, &pcsc_cchGroups);
+               status = g_PCSC.pfnSCardListReaderGroups(hContext, NULL, &pcsc_cchGroups);
 
                if (status == SCARD_S_SUCCESS)
                {
@@ -785,7 +785,7 @@ static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LP
                        if (!*pMszGroups)
                                return SCARD_E_NO_MEMORY;
 
-                       status = (LONG)g_PCSC.pfnSCardListReaderGroups(hContext, *pMszGroups, &pcsc_cchGroups);
+                       status = g_PCSC.pfnSCardListReaderGroups(hContext, *pMszGroups, &pcsc_cchGroups);
 
                        if (status != SCARD_S_SUCCESS)
                                free(*pMszGroups);
@@ -795,12 +795,11 @@ static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LP
        }
        else
        {
-               status = (LONG)g_PCSC.pfnSCardListReaderGroups(hContext, mszGroups, &pcsc_cchGroups);
+               status = g_PCSC.pfnSCardListReaderGroups(hContext, mszGroups, &pcsc_cchGroups);
        }
 
-       status = PCSC_MapErrorCodeToWinSCard(status);
        *pcchGroups = (DWORD)pcsc_cchGroups;
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardListReaderGroupsA(SCARDCONTEXT hContext, LPSTR mszGroups,
@@ -854,7 +853,7 @@ static LONG WINAPI PCSC_SCardListReaderGroupsW(SCARDCONTEXT hContext, LPWSTR msz
 static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR mszGroups,
                                                   LPSTR mszReaders, LPDWORD pcchReaders)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        BOOL pcchReadersAlloc = FALSE;
        LPSTR* pMszReaders = (LPSTR*)mszReaders;
        PCSC_DWORD pcsc_cchReaders = 0;
@@ -875,7 +874,7 @@ static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR
        if (pcchReadersAlloc && !g_SCardAutoAllocate)
        {
                pcsc_cchReaders = 0;
-               status = (LONG)g_PCSC.pfnSCardListReaders(hContext, mszGroups, NULL, &pcsc_cchReaders);
+               status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, NULL, &pcsc_cchReaders);
 
                if (status == SCARD_S_SUCCESS)
                {
@@ -884,8 +883,8 @@ static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR
                        if (!*pMszReaders)
                                return SCARD_E_NO_MEMORY;
 
-                       status = (LONG)g_PCSC.pfnSCardListReaders(hContext, mszGroups, *pMszReaders,
-                                                                 &pcsc_cchReaders);
+                       status =
+                           g_PCSC.pfnSCardListReaders(hContext, mszGroups, *pMszReaders, &pcsc_cchReaders);
 
                        if (status != SCARD_S_SUCCESS)
                                free(*pMszReaders);
@@ -895,13 +894,11 @@ static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR
        }
        else
        {
-               status =
-                   (LONG)g_PCSC.pfnSCardListReaders(hContext, mszGroups, mszReaders, &pcsc_cchReaders);
+               status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, mszReaders, &pcsc_cchReaders);
        }
 
-       status = PCSC_MapErrorCodeToWinSCard(status);
        *pcchReaders = (DWORD)pcsc_cchReaders;
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardListReadersA(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders,
@@ -1176,7 +1173,7 @@ static LONG WINAPI PCSC_SCardForgetCardTypeW(SCARDCONTEXT hContext, LPCWSTR szCa
 
 static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID pvMem)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
 
        if (PCSC_RemoveMemoryBlock(hContext, pvMem))
        {
@@ -1187,12 +1184,11 @@ static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID p
        {
                if (g_PCSC.pfnSCardFreeMemory)
                {
-                       status = (LONG)g_PCSC.pfnSCardFreeMemory(hContext, pvMem);
-                       status = PCSC_MapErrorCodeToWinSCard(status);
+                       status = g_PCSC.pfnSCardFreeMemory(hContext, pvMem);
                }
        }
 
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardFreeMemory(SCARDCONTEXT hContext, LPVOID pvMem)
@@ -1296,10 +1292,10 @@ static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWO
                                                       DWORD cReaders)
 {
        PCSC_DWORD i, j;
-       int* map;
+       INT64* map;
        PCSC_DWORD cMappedReaders;
        PCSC_SCARD_READERSTATE* states;
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_DWORD pcsc_dwTimeout = (PCSC_DWORD)dwTimeout;
        PCSC_DWORD pcsc_cReaders = (PCSC_DWORD)cReaders;
 
@@ -1324,7 +1320,7 @@ static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWO
         * To work around this apparent lack of "\\\\?PnP?\\Notification" support,
         * we have to filter rgReaderStates to exclude the special PnP reader name.
         */
-       map = (int*)calloc(pcsc_cReaders, sizeof(int));
+       map = (INT64*)calloc(pcsc_cReaders, sizeof(INT64));
 
        if (!map)
                return SCARD_E_NO_MEMORY;
@@ -1348,7 +1344,7 @@ static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWO
                        }
                }
 
-               map[i] = j;
+               map[i] = (INT64)j;
                states[j].szReader = rgReaderStates[i].szReader;
                states[j].dwCurrentState = rgReaderStates[i].dwCurrentState;
                states[j].pvUserData = rgReaderStates[i].pvUserData;
@@ -1362,9 +1358,7 @@ static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWO
 
        if (cMappedReaders > 0)
        {
-               status =
-                   (LONG)g_PCSC.pfnSCardGetStatusChange(hContext, pcsc_dwTimeout, states, cMappedReaders);
-               status = PCSC_MapErrorCodeToWinSCard(status);
+               status = g_PCSC.pfnSCardGetStatusChange(hContext, pcsc_dwTimeout, states, cMappedReaders);
        }
        else
        {
@@ -1376,16 +1370,16 @@ static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWO
                if (map[i] < 0)
                        continue; /* unmapped */
 
-               j = map[i];
-               rgReaderStates[i].dwCurrentState = states[j].dwCurrentState;
-               rgReaderStates[i].cbAtr = states[j].cbAtr;
+               j = (PCSC_DWORD)map[i];
+               rgReaderStates[i].dwCurrentState = (DWORD)states[j].dwCurrentState;
+               rgReaderStates[i].cbAtr = (DWORD)states[j].cbAtr;
                CopyMemory(&(rgReaderStates[i].rgbAtr), &(states[j].rgbAtr), PCSC_MAX_ATR_SIZE);
-               rgReaderStates[i].dwEventState = states[j].dwEventState;
+               rgReaderStates[i].dwEventState = (DWORD)states[j].dwEventState;
        }
 
        free(map);
        free(states);
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardGetStatusChangeA(SCARDCONTEXT hContext, DWORD dwTimeout,
@@ -1459,14 +1453,13 @@ static LONG WINAPI PCSC_SCardGetStatusChangeW(SCARDCONTEXT hContext, DWORD dwTim
 
 static LONG WINAPI PCSC_SCardCancel(SCARDCONTEXT hContext)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
 
        if (!g_PCSC.pfnSCardCancel)
                return PCSC_SCard_LogError("g_PCSC.pfnSCardCancel");
 
-       status = (LONG)g_PCSC.pfnSCardCancel(hContext);
-       status = PCSC_MapErrorCodeToWinSCard(status);
-       return status;
+       status = g_PCSC.pfnSCardCancel(hContext);
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szReader,
@@ -1474,8 +1467,8 @@ static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szRe
                                               LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
 {
        BOOL shared;
-       char* szReaderPCSC;
-       LONG status = SCARD_S_SUCCESS;
+       const char* szReaderPCSC;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode;
        PCSC_DWORD pcsc_dwPreferredProtocols = 0;
@@ -1486,7 +1479,7 @@ static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szRe
 
        shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE;
        PCSC_WaitForCardAccess(hContext, 0, shared);
-       szReaderPCSC = (char*)szReader;
+       szReaderPCSC = szReader;
 
        /**
         * As stated here :
@@ -1501,10 +1494,8 @@ static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szRe
                pcsc_dwPreferredProtocols =
                    (PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
 
-       status =
-           (LONG)g_PCSC.pfnSCardConnect(hContext, szReaderPCSC, pcsc_dwShareMode,
-                                        pcsc_dwPreferredProtocols, phCard, &pcsc_dwActiveProtocol);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardConnect(hContext, szReaderPCSC, pcsc_dwShareMode,
+                                       pcsc_dwPreferredProtocols, phCard, &pcsc_dwActiveProtocol);
 
        if (status == SCARD_S_SUCCESS)
        {
@@ -1514,7 +1505,7 @@ static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szRe
                PCSC_WaitForCardAccess(hContext, pCard->hSharedContext, shared);
        }
 
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardConnectA(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode,
@@ -1563,7 +1554,7 @@ static LONG WINAPI PCSC_SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
                                        LPDWORD pdwActiveProtocol)
 {
        BOOL shared;
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode;
        PCSC_DWORD pcsc_dwPreferredProtocols = 0;
        PCSC_DWORD pcsc_dwInitialization = (PCSC_DWORD)dwInitialization;
@@ -1575,23 +1566,22 @@ static LONG WINAPI PCSC_SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
        shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE;
        PCSC_WaitForCardAccess(0, hCard, shared);
        pcsc_dwPreferredProtocols = (PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
-       status = (LONG)g_PCSC.pfnSCardReconnect(hCard, pcsc_dwShareMode, pcsc_dwPreferredProtocols,
-                                               pcsc_dwInitialization, &pcsc_dwActiveProtocol);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardReconnect(hCard, pcsc_dwShareMode, pcsc_dwPreferredProtocols,
+                                         pcsc_dwInitialization, &pcsc_dwActiveProtocol);
+
        *pdwActiveProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwActiveProtocol);
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition;
 
        if (!g_PCSC.pfnSCardDisconnect)
                return PCSC_SCard_LogError("g_PCSC.pfnSCardDisconnect");
 
-       status = (LONG)g_PCSC.pfnSCardDisconnect(hCard, pcsc_dwDisposition);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardDisconnect(hCard, pcsc_dwDisposition);
 
        if (status == SCARD_S_SUCCESS)
        {
@@ -1599,12 +1589,12 @@ static LONG WINAPI PCSC_SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
        }
 
        PCSC_ReleaseCardAccess(0, hCard);
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardBeginTransaction(SCARDHANDLE hCard)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_SCARDCONTEXT* pContext = NULL;
 
@@ -1624,15 +1614,15 @@ static LONG WINAPI PCSC_SCardBeginTransaction(SCARDHANDLE hCard)
        if (pContext->isTransactionLocked)
                return SCARD_S_SUCCESS; /* disable nested transactions */
 
-       status = (LONG)g_PCSC.pfnSCardBeginTransaction(hCard);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardBeginTransaction(hCard);
+
        pContext->isTransactionLocked = TRUE;
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_SCARDCONTEXT* pContext = NULL;
        PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition;
@@ -1655,10 +1645,10 @@ static LONG WINAPI PCSC_SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDispositi
        if (!pContext->isTransactionLocked)
                return SCARD_S_SUCCESS; /* disable nested transactions */
 
-       status = (LONG)g_PCSC.pfnSCardEndTransaction(hCard, pcsc_dwDisposition);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardEndTransaction(hCard, pcsc_dwDisposition);
+
        pContext->isTransactionLocked = FALSE;
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardCancelTransaction(SCARDHANDLE hCard)
@@ -1673,7 +1663,7 @@ static LONG WINAPI PCSC_SCardState(SCARDHANDLE hCard, LPDWORD pdwState, LPDWORD
        PCSC_DWORD cchReaderLen;
        SCARDCONTEXT hContext = 0;
        LPSTR mszReaderNames = NULL;
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD pcsc_dwState = 0;
        PCSC_DWORD pcsc_dwProtocol = 0;
@@ -1694,9 +1684,8 @@ static LONG WINAPI PCSC_SCardState(SCARDHANDLE hCard, LPDWORD pdwState, LPDWORD
                return SCARD_E_INVALID_VALUE;
 
        cchReaderLen = SCARD_AUTOALLOCATE;
-       status = (LONG)g_PCSC.pfnSCardStatus(hCard, (LPSTR)&mszReaderNames, &cchReaderLen,
-                                            &pcsc_dwState, &pcsc_dwProtocol, pbAtr, &pcsc_cbAtrLen);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardStatus(hCard, (LPSTR)&mszReaderNames, &cchReaderLen, &pcsc_dwState,
+                                      &pcsc_dwProtocol, pbAtr, &pcsc_cbAtrLen);
 
        if (mszReaderNames)
                PCSC_SCardFreeMemory_Internal(hContext, mszReaderNames);
@@ -1704,7 +1693,7 @@ static LONG WINAPI PCSC_SCardState(SCARDHANDLE hCard, LPDWORD pdwState, LPDWORD
        *pdwState = (DWORD)pcsc_dwState;
        *pdwProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwProtocol);
        *pcbAtrLen = (DWORD)pcsc_cbAtrLen;
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 /*
@@ -1718,7 +1707,7 @@ static LONG WINAPI PCSC_SCardStatus_Internal(SCARDHANDLE hCard, LPSTR mszReaderN
 {
        PCSC_SCARDHANDLE* pCard = NULL;
        SCARDCONTEXT hContext;
-       LONG status;
+       PCSC_LONG status;
        PCSC_DWORD pcsc_cchReaderLen = 0;
        PCSC_DWORD pcsc_cbAtrLen = 0;
        PCSC_DWORD pcsc_dwState = 0;
@@ -1744,8 +1733,8 @@ static LONG WINAPI PCSC_SCardStatus_Internal(SCARDHANDLE hCard, LPSTR mszReaderN
        if (!hContext)
                return SCARD_E_INVALID_VALUE;
 
-       status = (LONG)g_PCSC.pfnSCardStatus(hCard, NULL, &pcsc_cchReaderLen, NULL, NULL, NULL,
-                                            &pcsc_cbAtrLen);
+       status =
+           g_PCSC.pfnSCardStatus(hCard, NULL, &pcsc_cchReaderLen, NULL, NULL, NULL, &pcsc_cbAtrLen);
 
        if (status != STATUS_SUCCESS)
                return PCSC_MapErrorCodeToWinSCard(status);
@@ -1810,8 +1799,8 @@ static LONG WINAPI PCSC_SCardStatus_Internal(SCARDHANDLE hCard, LPSTR mszReaderN
                atr = tATR;
        }
 
-       status = (LONG)g_PCSC.pfnSCardStatus(hCard, readerNames, &pcsc_cchReaderLen, &pcsc_dwState,
-                                            &pcsc_dwProtocol, atr, &pcsc_cbAtrLen);
+       status = g_PCSC.pfnSCardStatus(hCard, readerNames, &pcsc_cchReaderLen, &pcsc_dwState,
+                                      &pcsc_dwProtocol, atr, &pcsc_cbAtrLen);
 
        if (status != STATUS_SUCCESS)
                goto out_fail;
@@ -1910,7 +1899,7 @@ static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pio
                                       LPSCARD_IO_REQUEST pioRecvPci, LPBYTE pbRecvBuffer,
                                       LPDWORD pcbRecvLength)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD cbExtraBytes = 0;
        BYTE* pbExtraBytes = NULL;
@@ -1948,8 +1937,8 @@ static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pio
                 * pcsc-lite cannot have a null pioSendPci parameter, unlike WinSCard.
                 * Query the current protocol and use default SCARD_IO_REQUEST for it.
                 */
-               status = (LONG)g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState, &dwProtocol,
-                                                    NULL, &cbAtrLen);
+               status = g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState, &dwProtocol, NULL,
+                                              &cbAtrLen);
 
                if (status == SCARD_S_SUCCESS)
                {
@@ -1998,9 +1987,9 @@ static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pio
                CopyMemory(pcsc_pbExtraBytes, pbExtraBytes, cbExtraBytes);
        }
 
-       status = (LONG)g_PCSC.pfnSCardTransmit(hCard, pcsc_pioSendPci, pbSendBuffer, pcsc_cbSendLength,
-                                              pcsc_pioRecvPci, pbRecvBuffer, &pcsc_cbRecvLength);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardTransmit(hCard, pcsc_pioSendPci, pbSendBuffer, pcsc_cbSendLength,
+                                        pcsc_pioRecvPci, pbRecvBuffer, &pcsc_cbRecvLength);
+
        *pcbRecvLength = (DWORD)pcsc_cbRecvLength;
 
        if (pioSendPci)
@@ -2017,7 +2006,8 @@ static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pio
                                          non null */
        }
 
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
+       ;
 }
 
 static LONG WINAPI PCSC_SCardGetTransmitCount(SCARDHANDLE hCard, LPDWORD pcTransmitCount)
@@ -2043,7 +2033,7 @@ static LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPC
        DWORD IoCtlAccess = 0;
        DWORD IoCtlDeviceType = 0;
        BOOL getFeatureRequest = FALSE;
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD pcsc_dwControlCode = 0;
        PCSC_DWORD pcsc_cbInBufferSize = (PCSC_DWORD)cbInBufferSize;
@@ -2081,10 +2071,9 @@ static LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPC
                dwControlCode = PCSC_SCARD_CTL_CODE(IoCtlFunction);
 
        pcsc_dwControlCode = (PCSC_DWORD)dwControlCode;
-       status =
-           (LONG)g_PCSC.pfnSCardControl(hCard, pcsc_dwControlCode, lpInBuffer, pcsc_cbInBufferSize,
-                                        lpOutBuffer, pcsc_cbOutBufferSize, &pcsc_BytesReturned);
-       status = PCSC_MapErrorCodeToWinSCard(status);
+       status = g_PCSC.pfnSCardControl(hCard, pcsc_dwControlCode, lpInBuffer, pcsc_cbInBufferSize,
+                                       lpOutBuffer, pcsc_cbOutBufferSize, &pcsc_BytesReturned);
+
        *lpBytesReturned = (DWORD)pcsc_BytesReturned;
 
        if (getFeatureRequest)
@@ -2105,7 +2094,8 @@ static LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPC
                }
        }
 
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
+       ;
 }
 
 static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr,
@@ -2114,7 +2104,7 @@ static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrI
        SCARDCONTEXT hContext = 0;
        BOOL pcbAttrLenAlloc = FALSE;
        LPBYTE* pPbAttr = (LPBYTE*)pbAttr;
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId;
        PCSC_DWORD pcsc_cbAttrLen = 0;
@@ -2144,7 +2134,7 @@ static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrI
        if (pcbAttrLenAlloc && !g_SCardAutoAllocate)
        {
                pcsc_cbAttrLen = 0;
-               status = (LONG)g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, NULL, &pcsc_cbAttrLen);
+               status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, NULL, &pcsc_cbAttrLen);
 
                if (status == SCARD_S_SUCCESS)
                {
@@ -2153,8 +2143,7 @@ static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrI
                        if (!*pPbAttr)
                                return SCARD_E_NO_MEMORY;
 
-                       status =
-                           (LONG)g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, *pPbAttr, &pcsc_cbAttrLen);
+                       status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, *pPbAttr, &pcsc_cbAttrLen);
 
                        if (status != SCARD_S_SUCCESS)
                                free(*pPbAttr);
@@ -2164,15 +2153,12 @@ static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrI
        }
        else
        {
-               status = (LONG)g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, pbAttr, &pcsc_cbAttrLen);
+               status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, pbAttr, &pcsc_cbAttrLen);
        }
 
-       status = PCSC_MapErrorCodeToWinSCard(status);
-
        if (status == SCARD_S_SUCCESS)
                *pcbAttrLen = (DWORD)pcsc_cbAttrLen;
-
-       return status;
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardGetAttrib_FriendlyName(SCARDHANDLE hCard, DWORD dwAttrId,
@@ -2353,6 +2339,7 @@ static LONG WINAPI PCSC_SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE
        }
        else
        {
+
                if (dwAttrId == SCARD_ATTR_CURRENT_PROTOCOL_TYPE)
                {
                        if (!pcbAttrLenAlloc)
@@ -2459,7 +2446,7 @@ static LONG WINAPI PCSC_SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE
 static LONG WINAPI PCSC_SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr,
                                        DWORD cbAttrLen)
 {
-       LONG status = SCARD_S_SUCCESS;
+       PCSC_LONG status = SCARD_S_SUCCESS;
        PCSC_SCARDHANDLE* pCard = NULL;
        PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId;
        PCSC_DWORD pcsc_cbAttrLen = (PCSC_DWORD)cbAttrLen;
@@ -2473,9 +2460,8 @@ static LONG WINAPI PCSC_SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYT
                return SCARD_E_INVALID_VALUE;
 
        PCSC_WaitForCardAccess(0, hCard, pCard->shared);
-       status = (LONG)g_PCSC.pfnSCardSetAttrib(hCard, pcsc_dwAttrId, pbAttr, pcsc_cbAttrLen);
-       status = PCSC_MapErrorCodeToWinSCard(status);
-       return status;
+       status = g_PCSC.pfnSCardSetAttrib(hCard, pcsc_dwAttrId, pbAttr, pcsc_cbAttrLen);
+       return PCSC_MapErrorCodeToWinSCard(status);
 }
 
 static LONG WINAPI PCSC_SCardUIDlgSelectCardA(LPOPENCARDNAMEA_EX pDlgStruc)
@@ -2640,7 +2626,7 @@ static LONG WINAPI PCSC_SCardWriteCacheA(SCARDCONTEXT hContext, UUID* CardIdenti
 
        HashTable_Remove(ctx->cache, id);
        HashTable_Add(ctx->cache, id, data);
-       free(id);
+
        return SCARD_S_SUCCESS;
 }
 
@@ -2675,7 +2661,7 @@ static LONG WINAPI PCSC_SCardWriteCacheW(SCARDCONTEXT hContext, UUID* CardIdenti
 
        HashTable_Remove(ctx->cache, id);
        HashTable_Add(ctx->cache, id, data);
-       free(id);
+
        return SCARD_S_SUCCESS;
 }
 
index 79e1c68..a8ea443 100644 (file)
@@ -3,6 +3,8 @@
  * Smart Card API
  *
  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2020 Armin Novak <armin.novak@thincast.com>
+ * Copyright 2020 Thincast Technologies GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 #ifdef __APPLE__
-typedef unsigned int PCSC_DWORD;
-typedef PCSC_DWORD *PCSC_PDWORD, *PCSC_LPDWORD;
-typedef unsigned int PCSC_ULONG;
+
+#include <stdint.h>
+
+#ifndef BYTE
+typedef uint8_t PCSC_BYTE;
+#endif
+typedef uint8_t PCSC_UCHAR;
+typedef PCSC_UCHAR* PCSC_PUCHAR;
+typedef uint16_t PCSC_USHORT;
+
+#ifndef __COREFOUNDATION_CFPLUGINCOM__
+typedef uint32_t PCSC_ULONG;
+typedef void* PCSC_LPVOID;
+typedef int16_t PCSC_BOOL;
+#endif
+
 typedef PCSC_ULONG* PCSC_PULONG;
-typedef int PCSC_LONG;
+typedef const void* PCSC_LPCVOID;
+typedef uint32_t PCSC_DWORD;
+typedef PCSC_DWORD* PCSC_PDWORD;
+typedef uint16_t PCSC_WORD;
+typedef int32_t PCSC_LONG;
+typedef const char* PCSC_LPCSTR;
+typedef const PCSC_BYTE* PCSC_LPCBYTE;
+typedef PCSC_BYTE* PCSC_LPBYTE;
+typedef PCSC_DWORD* PCSC_LPDWORD;
+typedef char* PCSC_LPSTR;
+
 #else
-typedef unsigned long PCSC_DWORD;
-typedef PCSC_DWORD *PCSC_PDWORD, *PCSC_LPDWORD;
+
+#ifndef BYTE
+typedef unsigned char PCSC_BYTE;
+#endif
+typedef unsigned char PCSC_UCHAR;
+typedef PCSC_UCHAR* PCSC_PUCHAR;
+typedef unsigned short PCSC_USHORT;
+
+#ifndef __COREFOUNDATION_CFPLUGINCOM__
 typedef unsigned long PCSC_ULONG;
-typedef PCSC_ULONG* PCSC_PULONG;
+typedef void* PCSC_LPVOID;
+#endif
+
+typedef const void* PCSC_LPCVOID;
+typedef unsigned long PCSC_DWORD;
+typedef PCSC_DWORD* PCSC_PDWORD;
 typedef long PCSC_LONG;
+typedef const char* PCSC_LPCSTR;
+typedef const PCSC_BYTE* PCSC_LPCBYTE;
+typedef PCSC_BYTE* PCSC_LPBYTE;
+typedef PCSC_DWORD* PCSC_LPDWORD;
+typedef char* PCSC_LPSTR;
+
+/* these types were deprecated but still used by old drivers and
+ * applications. So just declare and use them. */
+typedef PCSC_LPSTR PCSC_LPTSTR;
+typedef PCSC_LPCSTR PCSC_LPCTSTR;
+
+/* types unused by pcsc-lite */
+typedef short PCSC_BOOL;
+typedef unsigned short PCSC_WORD;
+typedef PCSC_ULONG* PCSC_PULONG;
+
 #endif
+
 #define PCSC_SCARD_UNKNOWN 0x0001
 #define PCSC_SCARD_ABSENT 0x0002
 #define PCSC_SCARD_PRESENT 0x0004