/[rdesktop]/jpeg/rdesktop/trunk/scard.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /jpeg/rdesktop/trunk/scard.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1312 by stargo, Wed Nov 1 21:26:55 2006 UTC revision 1328 by stargo, Fri Nov 3 23:51:35 2006 UTC
# Line 44  Line 44 
44    
45  static struct stream out[STREAM_COUNT];  static struct stream out[STREAM_COUNT];
46  static int cur_stream_id = 0;  static int cur_stream_id = 0;
47  static pthread_mutex_t *tcp_sendcontrol_mutex = NULL;  static pthread_mutex_t **scard_mutex = NULL;
48    
49  static uint32 curDevice = 0, curId = 0, curBytesOut = 0;  static uint32 curDevice = 0, curId = 0, curBytesOut = 0;
50  static PSCNameMapRec nameMapList = NULL;  static PSCNameMapRec nameMapList = NULL;
# Line 54  static pthread_t queueHandler; Line 54  static pthread_t queueHandler;
54  static pthread_mutex_t queueAccess;  static pthread_mutex_t queueAccess;
55  static pthread_mutex_t queueEmpty;  static pthread_mutex_t queueEmpty;
56  static pthread_mutex_t hcardAccess;  static pthread_mutex_t hcardAccess;
 /* static pthread_mutex_t sendControl; */  
57    
58  static PMEM_HANDLE threadListHandle = NULL;  static PMEM_HANDLE threadListHandle = NULL;
59  static PThreadListElement threadList = NULL;  static PThreadListElement threadList = NULL;
# Line 126  scard_enum_devices(uint32 * id, char *op Line 125  scard_enum_devices(uint32 * id, char *op
125          rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);          rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
126          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
127          {          {
128                  printf("[SMART CARD: PCSC service not available]\n");                  error("[SMART CARD: PCSC service not available]\n");
129                  return 0;                  return 0;
130          }          }
131          else          else
# Line 136  scard_enum_devices(uint32 * id, char *op Line 135  scard_enum_devices(uint32 * id, char *op
135    
136          if (0 != pthread_mutex_init(&queueAccess, NULL))          if (0 != pthread_mutex_init(&queueAccess, NULL))
137          {          {
138                  printf("[SMART CARD: Can't initialize queue access mutex]\n");                  error("[SMART CARD: Can't initialize queue access mutex]\n");
139                  return 0;                  return 0;
140          }          }
141    
142          if (0 != pthread_mutex_init(&queueEmpty, NULL))          if (0 != pthread_mutex_init(&queueEmpty, NULL))
143          {          {
144                  printf("[SMART CARD: Can't initialize queue control mutex]\n");                  error("[SMART CARD: Can't initialize queue control mutex]\n");
145                  return 0;                  return 0;
146          }          }
147    
148          if (0 != pthread_mutex_init(&hcardAccess, NULL))          if (0 != pthread_mutex_init(&hcardAccess, NULL))
149          {          {
150                  printf("[SMART CARD: Can't initialize hcard list access mutex]\n");                  error("[SMART CARD: Can't initialize hcard list access mutex]\n");
151                  return 0;                  return 0;
152          }          }
153    
 #if 0  
         if (0 != pthread_mutex_init(&sendControl, NULL))  
         {  
                 printf("[SMART CARD: Can't initialize send control mutex]\n");  
                 return 0;  
         }  
 #endif  
   
   
154          if (0 !=          if (0 !=
155              pthread_create(&queueHandler, NULL, (void *(*)(void *)) queue_handler_function, NULL))              pthread_create(&queueHandler, NULL, (void *(*)(void *)) queue_handler_function, NULL))
156          {          {
157                  printf("[SMART CARD: Can't create queue handling Thread]\n");                  error("[SMART CARD: Can't create queue handling Thread]\n");
158                  return 0;                  return 0;
159          }          }
160    
# Line 233  scard_enum_devices(uint32 * id, char *op Line 223  scard_enum_devices(uint32 * id, char *op
223                  }                  }
224          }          }
225    
   
         printf("******************************************************\n");  
         printf("* Smart Card support for RDesktop is initialized.    *\n");  
         printf("* Copyright (C) by Alexi Volkov, alexi@myrealbox.com *\n");  
         printf("******************************************************\n");  
   
226          return count;          return count;
227  }  }
228    
# Line 497  outBufferFinish(STREAM out, char *buffer Line 481  outBufferFinish(STREAM out, char *buffer
481  }  }
482    
483  static void  static void
484  outForceAllignment(STREAM out, unsigned int seed)  outForceAlignment(STREAM out, unsigned int seed)
485  {  {
486          SERVER_DWORD add = (seed - (out->p - out->data) % seed) % seed;          SERVER_DWORD add = (seed - (out->p - out->data) % seed) % seed;
487          if (add > 0)          if (add > 0)
         {  
488                  out_uint8s(out, add);                  out_uint8s(out, add);
         }  
489  }  }
490    
491  static unsigned int  static unsigned int
# Line 620  TS_SCardEstablishContext(STREAM in, STRE Line 602  TS_SCardEstablishContext(STREAM in, STRE
602          MYPCSC_SCARDCONTEXT hContext;          MYPCSC_SCARDCONTEXT hContext;
603          /* code segment  */          /* code segment  */
604    
605  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("Establishing PC/SC Context... \n"));
         printf("Establishing PC/SC Context... \n");  
 #endif  
606          rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);          rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 #ifdef WITH_SCARD_DEBUG  
607          if (rv)          if (rv)
608                  printf("<--ERROR SCardEstablishContext Code=0x%.8x]-->\n", (unsigned int) rv);          {
609                    DEBUG_SCARD(("<--ERROR SCardEstablishContext Code=0x%.8x]-->\n",
610                                 (unsigned int) rv));
611            }
612          else          else
613                  printf("<--SUCCESS SCardEstablishContext-->\n");          {
614  #endif                  DEBUG_SCARD(("<--SUCCESS SCardEstablishContext-->\n"));
615            }
616    
617          out_uint32_le(out, 0x00000004);          out_uint32_le(out, 0x00000004);
618          out_uint32_le(out, (SERVER_DWORD) hContext);    /* must not be 0 (Seems to be pointer), don't know what is this (I use hContext as value) */          out_uint32_le(out, (SERVER_DWORD) hContext);    /* must not be 0 (Seems to be pointer), don't know what is this (I use hContext as value) */
# Line 647  TS_SCardReleaseContext(STREAM in, STREAM Line 630  TS_SCardReleaseContext(STREAM in, STREAM
630    
631          in->p += 0x1C;          in->p += 0x1C;
632          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
633  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
634          printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("Releasing context... \n"));
         printf("Releasing context... \n");  
 #endif  
635          rv = SCardReleaseContext((MYPCSC_SCARDCONTEXT) hContext);          rv = SCardReleaseContext((MYPCSC_SCARDCONTEXT) hContext);
636  #ifdef WITH_SCARD_DEBUG  
637          if (rv)          if (rv)
638                  printf("<--ERROR SCardReleaseContext Code=0x%.8x-->\n", (unsigned int) rv);          {
639                    DEBUG_SCARD(("<--ERROR SCardReleaseContext Code=0x%.8x-->\n", (unsigned int) rv));
640            }
641          else          else
642                  printf("<--SUCCESS SCardReleaseContext-->\n");          {
643  #endif                  DEBUG_SCARD(("<--SUCCESS SCardReleaseContext-->\n"));
644            }
645    
646          return rv;          return rv;
647  }  }
648    
# Line 672  TS_SCardIsValidContext(STREAM in, STREAM Line 657  TS_SCardIsValidContext(STREAM in, STREAM
657    
658          in->p += 0x1C;          in->p += 0x1C;
659          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
660  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
661          printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("Checking... \n"));
         printf("Checking... \n");  
 #endif  
662          /* There is no realization of SCardIsValidContext in PC/SC Lite so we call SCardListReaders */          /* There is no realization of SCardIsValidContext in PC/SC Lite so we call SCardListReaders */
663    
664          readers = SC_xmalloc(&lcHandle, 1024);          readers = SC_xmalloc(&lcHandle, 1024);
# Line 683  TS_SCardIsValidContext(STREAM in, STREAM Line 666  TS_SCardIsValidContext(STREAM in, STREAM
666                  return SC_returnNoMemoryError(&lcHandle, in, out);                  return SC_returnNoMemoryError(&lcHandle, in, out);
667    
668          rv = SCardListReaders((MYPCSC_SCARDCONTEXT) hContext, NULL, readers, &readerCount);          rv = SCardListReaders((MYPCSC_SCARDCONTEXT) hContext, NULL, readers, &readerCount);
669    
670          if (rv)          if (rv)
671          {          {
672  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardListReaders (no SCardIsValidContext) Code=0x%.8x-->\n",
673                  printf("<--ERROR SCardListReaders (no SCardIsValidContext) Code=0x%.8x-->\n",                               (unsigned int) rv));
                        (unsigned int) rv);  
 #endif  
674                  rv = SCARD_E_INVALID_HANDLE;                  rv = SCARD_E_INVALID_HANDLE;
675          }          }
 #ifdef WITH_SCARD_DEBUG  
676          else          else
677                  printf("<--SUCCESS SCardListReaders (no SCardIsValidContext)-->\n");          {
678  #endif                  DEBUG_SCARD(("<--SUCCESS SCardListReaders (no SCardIsValidContext)-->\n"));
679          outForceAllignment(out, 8);          }
680    
681            outForceAlignment(out, 8);
682          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
683          return rv;          return rv;
684  }  }
# Line 715  TS_SCardListReaders(STREAM in, STREAM ou Line 698  TS_SCardListReaders(STREAM in, STREAM ou
698    
699          in->p += 0x2C;          in->p += 0x2C;
700          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
701  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
         printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);  
 #endif  
702          plen1 = out->p;          plen1 = out->p;
703          out_uint32_le(out, 0x00000000); /* Temp value for data length as 0x0 */          out_uint32_le(out, 0x00000000); /* Temp value for data length as 0x0 */
704          out_uint32_le(out, 0x01760650);          out_uint32_le(out, 0x01760650);
# Line 738  TS_SCardListReaders(STREAM in, STREAM ou Line 719  TS_SCardListReaders(STREAM in, STREAM ou
719          {          {
720                  int i;                  int i;
721                  PSCNameMapRec tmpMap;                  PSCNameMapRec tmpMap;
722  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
723                  printf("[CALL RESULT of SCardListReaders 0x%.8x]\n", (unsigned int) rv);                  DEBUG_SCARD(("[CALL RESULT of SCardListReaders 0x%.8x]\n", (unsigned int) rv));
724                  hexdump((void *) readers, cchReaders);                  hexdump((void *) readers, cchReaders);
725  #endif  #endif
726                  for (i = 0, tmpMap = nameMapList; i < nameMapCount; i++, tmpMap++)                  for (i = 0, tmpMap = nameMapList; i < nameMapCount; i++, tmpMap++)
# Line 770  TS_SCardListReaders(STREAM in, STREAM ou Line 751  TS_SCardListReaders(STREAM in, STREAM ou
751          out_uint32_le(out, dataLength);          out_uint32_le(out, dataLength);
752          out->p = pend;          out->p = pend;
753    
754          outForceAllignment(out, 8);          outForceAlignment(out, 8);
755          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
756          return rv;          return rv;
757  }  }
# Line 793  TS_SCardConnect(STREAM in, STREAM out, B Line 774  TS_SCardConnect(STREAM in, STREAM out, B
774          in->p += 0x1C;          in->p += 0x1C;
775          in_uint32_le(in, dwShareMode);          in_uint32_le(in, dwShareMode);
776          in_uint32_le(in, dwPreferredProtocol);          in_uint32_le(in, dwPreferredProtocol);
777  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[SHARE %8x]\n", (unsigned int) dwShareMode));
778          printf("[SHARE %8x]\n", (unsigned int) dwShareMode);          DEBUG_SCARD(("[PROTO %8x]\n", (unsigned int) dwPreferredProtocol));
         printf("[PROTO %8x]\n", (unsigned int) dwPreferredProtocol);  
 #endif  
779          inReaderName(&lcHandle, in, &szReader, wide);          inReaderName(&lcHandle, in, &szReader, wide);
780  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONNECT TO READER \"%s\"\n", (szReader != NULL) ? (szReader) : ("NULL")));
         printf("[CONNECT TO READER \"%s\"\n", (szReader != NULL) ? (szReader) : ("NULL"));  
 #endif  
781          in->p += 0x04;          in->p += 0x04;
782          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
783  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
         printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);  
 #endif  
784          rv = SCardConnect(hContext, szReader, (MYPCSC_DWORD) dwShareMode,          rv = SCardConnect(hContext, szReader, (MYPCSC_DWORD) dwShareMode,
785                            (MYPCSC_DWORD) dwPreferredProtocol, &myHCard, &dwActiveProtocol);                            (MYPCSC_DWORD) dwPreferredProtocol, &myHCard, &dwActiveProtocol);
786          hCard = scHandleToServer(myHCard);          hCard = scHandleToServer(myHCard);
787  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[RECEIVED HCARD 0x%016lx]\n", (unsigned long) myHCard));
788          printf("[RECEIVED HCARD 0x%016lx]\n", myHCard);          DEBUG_SCARD(("[MANGLED  HCARD 0x%08x]\n", hCard));
         printf("[MANGLED  HCARD 0x%08x]\n", hCard);  
 #endif  
789          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
790          {          {
791  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardConnect Code=0x%.8x-->\n", (unsigned int) rv));
                 printf("<--ERROR SCardConnect Code=0x%.8x-->\n", (unsigned int) rv);  
 #endif  
792          }          }
793          else          else
794          {          {
795                  char *szVendor = getVendor(szReader);                  char *szVendor = getVendor(szReader);
796  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--SUCCESS ScardConnect-->\n"));
                 printf("<--SUCCESS ScardConnect-->\n");  
 #endif  
797                  if (szVendor && (strlen(szVendor) > 0))                  if (szVendor && (strlen(szVendor) > 0))
798                  {                  {
799  #ifdef WITH_SCARD_DEBUG                          DEBUG_SCARD(("Set Attribute ATTR_VENDOR_NAME\n"));
                         printf("Set Attribute ATTR_VENDOR_NAME\n");  
 #endif  
800                          pthread_mutex_lock(&hcardAccess);                          pthread_mutex_lock(&hcardAccess);
801                          PSCHCardRec hcard = xmalloc(sizeof(TSCHCardRec));                          PSCHCardRec hcard = xmalloc(sizeof(TSCHCardRec));
802                          if (hcard)                          if (hcard)
# Line 859  TS_SCardConnect(STREAM in, STREAM out, B Line 826  TS_SCardConnect(STREAM in, STREAM out, B
826          out_uint32_le(out, 0x00000004);          out_uint32_le(out, 0x00000004);
827          out_uint32_le(out, hCard);          out_uint32_le(out, hCard);
828    
829          outForceAllignment(out, 8);          outForceAlignment(out, 8);
830          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
831          return rv;          return rv;
832  }  }
# Line 885  TS_SCardReconnect(STREAM in, STREAM out) Line 852  TS_SCardReconnect(STREAM in, STREAM out)
852          in->p += 0x04;          in->p += 0x04;
853          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
854          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
855  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[SHARE = 0x%.8x]\n", (unsigned int) dwShareMode));
856          printf("[SHARE = 0x%.8x]\n", (unsigned int) dwShareMode);          DEBUG_SCARD(("[PROTO = 0x%.8x]\n", (unsigned int) dwPreferredProtocol));
857          printf("[PROTO = 0x%.8x]\n", (unsigned int) dwPreferredProtocol);          DEBUG_SCARD(("[INIT  = 0x%.8x]\n", (unsigned int) dwInitialization));
858          printf("[INIT  = 0x%.8x]\n", (unsigned int) dwInitialization);          DEBUG_SCARD(("[hContext = 0x%.8x]\n", (unsigned int) hContext));
859          printf("[hContext = 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
860          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[myHCard = 0x%016lx]\n", (unsigned long) myHCard));
         printf("[myHCard = 0x%016lx]\n", myHCard);  
 #endif  
861          rv = SCardReconnect(myHCard, (MYPCSC_DWORD) dwShareMode, (MYPCSC_DWORD) dwPreferredProtocol,          rv = SCardReconnect(myHCard, (MYPCSC_DWORD) dwShareMode, (MYPCSC_DWORD) dwPreferredProtocol,
862                              (MYPCSC_DWORD) dwInitialization, &dwActiveProtocol);                              (MYPCSC_DWORD) dwInitialization, &dwActiveProtocol);
 #ifdef WITH_SCARD_DEBUG  
863          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
864                  printf("<--ERROR SCardReconnect Code=0x%.8x-->\n", (unsigned int) rv);          {
865                    DEBUG_SCARD(("<--ERROR SCardReconnect Code=0x%.8x-->\n", (unsigned int) rv));
866            }
867          else          else
868                  printf("<--SUCCESS SCardReconnect-->\n");          {
869  #endif                  DEBUG_SCARD(("<--SUCCESS SCardReconnect-->\n"));
870            }
871    
872          outForceAllignment(out, 8);          outForceAlignment(out, 8);
873          out_uint32_le(out, (SERVER_DWORD) dwActiveProtocol);          out_uint32_le(out, (SERVER_DWORD) dwActiveProtocol);
874          return rv;          return rv;
875  }  }
# Line 923  TS_SCardDisconnect(STREAM in, STREAM out Line 890  TS_SCardDisconnect(STREAM in, STREAM out
890          in->p += 0x04;          in->p += 0x04;
891          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
892    
893  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[hContext = 0x%.8x]\n", (unsigned int) hContext));
894          printf("[hContext = 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
895          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[dwDisposition = 0x%.8x]\n", (unsigned int) dwDisposition));
         printf("[dwDisposition = 0x%.8x]\n", (unsigned int) dwDisposition);  
 #endif  
896    
897          pthread_mutex_lock(&hcardAccess);          pthread_mutex_lock(&hcardAccess);
898          PSCHCardRec hcard = hcardFirst;          PSCHCardRec hcard = hcardFirst;
# Line 951  TS_SCardDisconnect(STREAM in, STREAM out Line 916  TS_SCardDisconnect(STREAM in, STREAM out
916          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
917          rv = SCardDisconnect(myHCard, (MYPCSC_DWORD) dwDisposition);          rv = SCardDisconnect(myHCard, (MYPCSC_DWORD) dwDisposition);
918    
 #ifdef WITH_SCARD_DEBUG  
919          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
920                  printf("<--ERROR SCardDisconnect Code=0x%.8x-->\n", (unsigned int) rv);          {
921                    DEBUG_SCARD(("<--ERROR SCardDisconnect Code=0x%.8x-->\n", (unsigned int) rv));
922            }
923          else          else
924                  printf("<--SUCCESS SCardDisconnect-->\n");          {
925  #endif                  DEBUG_SCARD(("<--SUCCESS SCardDisconnect-->\n"));
926          outForceAllignment(out, 8);          }
927    
928            outForceAlignment(out, 8);
929          return rv;          return rv;
930  }  }
931    
# Line 1064  TS_SCardGetStatusChange(STREAM in, STREA Line 1032  TS_SCardGetStatusChange(STREAM in, STREA
1032          in->p += 0x08;          in->p += 0x08;
1033          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
1034          in->p += 0x04;          in->p += 0x04;
1035  #ifdef WITH_SCARD_DEBUG  
1036          printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
1037          printf("[dwTimeout 0x%.8x]\n", (unsigned int) dwTimeout);          DEBUG_SCARD(("[dwTimeout 0x%.8x]\n", (unsigned int) dwTimeout));
1038          printf("[COUNT %d]\n", (unsigned int) dwCount);          DEBUG_SCARD(("[COUNT %d]\n", (unsigned int) dwCount));
1039          printf("[TYPE SIZE %d]\n", (unsigned int) sizeof(SERVER_SCARD_READERSTATE_A));          DEBUG_SCARD(("[TYPE SIZE %d]\n", (unsigned int) sizeof(SERVER_SCARD_READERSTATE_A)));
1040  #endif  
1041          if (dwCount > 0)          if (dwCount > 0)
1042          {          {
1043                  rsArray = SC_xmalloc(&lcHandle, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));                  rsArray = SC_xmalloc(&lcHandle, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));
# Line 1086  TS_SCardGetStatusChange(STREAM in, STREA Line 1054  TS_SCardGetStatusChange(STREAM in, STREA
1054                          in->p += 0x04;                          in->p += 0x04;
1055                          in_uint8a(in, cur, SERVER_SCARDSTATESIZE);                          in_uint8a(in, cur, SERVER_SCARDSTATESIZE);
1056                  }                  }
1057  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1058                  printf("[READERS DUMP 1]------------------\n");                  DEBUG_SCARD(("[READERS DUMP 1]------------------\n"));
1059                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));
1060  #endif  #endif
1061    
# Line 1124  TS_SCardGetStatusChange(STREAM in, STREA Line 1092  TS_SCardGetStatusChange(STREAM in, STREA
1092    
1093                          in->p += 0x08;                          in->p += 0x08;
1094                          in_uint32_le(in, dataLength);                          in_uint32_le(in, dataLength);
1095  #ifdef WITH_SCARD_DEBUG                          DEBUG_SCARD(("[%d] Data Length %d]\n", (unsigned int) i, dataLength));
                         printf("[%d] Data Length %d]\n", (unsigned int) i, dataLength);  
 #endif  
1096                          inRepos(in,                          inRepos(in,
1097                                  inString(&lcHandle, in, (char **) &(cur->szReader), dataLength,                                  inString(&lcHandle, in, (char **) &(cur->szReader), dataLength,
1098                                           wide));                                           wide));
# Line 1134  TS_SCardGetStatusChange(STREAM in, STREA Line 1100  TS_SCardGetStatusChange(STREAM in, STREA
1100                          if (strcmp(cur->szReader, "\\\\?PnP?\\Notification") == 0)                          if (strcmp(cur->szReader, "\\\\?PnP?\\Notification") == 0)
1101                                  cur->dwCurrentState |= SCARD_STATE_IGNORE;                                  cur->dwCurrentState |= SCARD_STATE_IGNORE;
1102                  }                  }
1103  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1104                  printf("[READERS DUMP 2]------------------\n");                  DEBUG_SCARD(("[READERS DUMP 2]------------------\n"));
1105                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));
1106  #endif  #endif
1107          }          }
# Line 1145  TS_SCardGetStatusChange(STREAM in, STREA Line 1111  TS_SCardGetStatusChange(STREAM in, STREA
1111                  stateArray = NULL;                  stateArray = NULL;
1112          }          }
1113    
1114  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("\nCalling SCardGetStatusChange...\n"));
         printf("\nCalling SCardGetStatusChange...\n");  
 #endif  
1115    
1116    
1117          myRsArray = SC_xmalloc(&lcHandle, dwCount * sizeof(MYPCSC_SCARD_READERSTATE_A));          myRsArray = SC_xmalloc(&lcHandle, dwCount * sizeof(MYPCSC_SCARD_READERSTATE_A));
# Line 1155  TS_SCardGetStatusChange(STREAM in, STREA Line 1119  TS_SCardGetStatusChange(STREAM in, STREA
1119                  return SC_returnNoMemoryError(&lcHandle, in, out);                  return SC_returnNoMemoryError(&lcHandle, in, out);
1120          memset(myRsArray, 0, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));          memset(myRsArray, 0, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));
1121          copyReaderState_ServerToMyPCSC(rsArray, myRsArray, (SERVER_DWORD) dwCount);          copyReaderState_ServerToMyPCSC(rsArray, myRsArray, (SERVER_DWORD) dwCount);
1122  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1123          printf("[TRANSLATION OF READERS]--------------------\n");          DEBUG_SCARD(("[TRANSLATION OF READERS]--------------------\n"));
1124          hexdump((void *) myRsArray, dwCount * sizeof(MYPCSC_SCARD_READERSTATE_A));          hexdump((void *) myRsArray, dwCount * sizeof(MYPCSC_SCARD_READERSTATE_A));
1125  #endif  #endif
1126    
# Line 1164  TS_SCardGetStatusChange(STREAM in, STREA Line 1128  TS_SCardGetStatusChange(STREAM in, STREA
1128                                    myRsArray, (MYPCSC_DWORD) dwCount);                                    myRsArray, (MYPCSC_DWORD) dwCount);
1129          copyReaderState_MyPCSCToServer(myRsArray, rsArray, (MYPCSC_DWORD) dwCount);          copyReaderState_MyPCSCToServer(myRsArray, rsArray, (MYPCSC_DWORD) dwCount);
1130    
 #ifdef WITH_SCARD_DEBUG  
1131          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1132                  printf("<--ERROR SCardGetStatusChange Code=0x%.8x-->\n", (unsigned int) rv);          {
1133                    DEBUG_SCARD(("<--ERROR SCardGetStatusChange Code=0x%.8x-->\n", (unsigned int) rv));
1134            }
1135          else          else
1136                  printf("<--SUCCESS SCardGetStatusChange-->\n");          {
1137                    DEBUG_SCARD(("<--SUCCESS SCardGetStatusChange-->\n"));
1138            }
1139    
1140    #ifdef WITH_DEBUG_SCARD
1141          if (dwCount > 0)          if (dwCount > 0)
1142          {          {
1143                  printf("[READERS DUMP]------------------\n");                  DEBUG_SCARD(("[READERS DUMP]------------------\n"));
1144                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));                  hexdump((void *) rsArray, dwCount * sizeof(SERVER_SCARD_READERSTATE_A));
1145          }          }
1146  #endif  #endif
# Line 1214  TS_SCardGetStatusChange(STREAM in, STREA Line 1182  TS_SCardGetStatusChange(STREAM in, STREA
1182                  out_uint8p(out, (void *) ((unsigned char **) cur + 2),                  out_uint8p(out, (void *) ((unsigned char **) cur + 2),
1183                             sizeof(SERVER_SCARD_READERSTATE_A) - 2 * sizeof(unsigned char *));                             sizeof(SERVER_SCARD_READERSTATE_A) - 2 * sizeof(unsigned char *));
1184          }          }
1185          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1186          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1187          return rv;          return rv;
1188  }  }
# Line 1227  TS_SCardCancel(STREAM in, STREAM out) Line 1195  TS_SCardCancel(STREAM in, STREAM out)
1195    
1196          in->p += 0x1C;          in->p += 0x1C;
1197          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
1198  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
1199          printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("Canceling... \n"));
         printf("Canceling... \n");  
 #endif  
1200          rv = SCardCancel((MYPCSC_SCARDCONTEXT) hContext);          rv = SCardCancel((MYPCSC_SCARDCONTEXT) hContext);
 #ifdef WITH_SCARD_DEBUG  
1201          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1202                  printf("<--ERROR SCardCancel Code=0x%.8x-->\n", (unsigned int) rv);          {
1203                    DEBUG_SCARD(("<--ERROR SCardCancel Code=0x%.8x-->\n", (unsigned int) rv));
1204            }
1205          else          else
1206                  printf("<--SUCCESS SCardCancel-->\n");          {
1207  #endif                  DEBUG_SCARD(("<--SUCCESS SCardCancel-->\n"));
1208          outForceAllignment(out, 8);          }
1209            outForceAlignment(out, 8);
1210          return rv;          return rv;
1211  }  }
1212    
# Line 1259  TS_SCardLocateCardsByATR(STREAM in, STRE Line 1227  TS_SCardLocateCardsByATR(STREAM in, STRE
1227    
1228          in->p += 0x2C;          in->p += 0x2C;
1229          in_uint32_le(in, hContext);          in_uint32_le(in, hContext);
1230  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CONTEXT 0x%.8x]\n", (unsigned int) hContext));
         printf("[CONTEXT 0x%.8x]\n", (unsigned int) hContext);  
 #endif  
1231          in_uint32_le(in, atrMaskCount);          in_uint32_le(in, atrMaskCount);
1232          pAtrMasks = SC_xmalloc(&lcHandle, atrMaskCount * sizeof(SCARD_ATRMASK_L));          pAtrMasks = SC_xmalloc(&lcHandle, atrMaskCount * sizeof(SCARD_ATRMASK_L));
1233          if (!pAtrMasks)          if (!pAtrMasks)
# Line 1289  TS_SCardLocateCardsByATR(STREAM in, STRE Line 1255  TS_SCardLocateCardsByATR(STREAM in, STRE
1255          for (i = 0, rsCur = rsArray; i < readerCount; i++, rsCur++)          for (i = 0, rsCur = rsArray; i < readerCount; i++, rsCur++)
1256          {          {
1257                  inReaderName(&lcHandle, in, (char **) &rsCur->szReader, wide);                  inReaderName(&lcHandle, in, (char **) &rsCur->szReader, wide);
1258  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("[CHECK READER %s]\n",
1259                  printf("[CHECK READER %s]\n", (rsCur->szReader) ? (rsCur->szReader) : ("NULL"));                               (rsCur->szReader) ? (rsCur->szReader) : ("NULL")));
 #endif  
1260          }          }
1261    
1262  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[CALL subfunction \"SCardGetStatusChange\"]\n"));
         printf("[CALL subfunction \"SCardGetStatusChange\"]\n");  
 #endif  
1263          /* FIXME segfault here. */          /* FIXME segfault here. */
1264          myRsArray = SC_xmalloc(&lcHandle, readerCount * sizeof(MYPCSC_SCARD_READERSTATE_A));          myRsArray = SC_xmalloc(&lcHandle, readerCount * sizeof(MYPCSC_SCARD_READERSTATE_A));
1265          if (!myRsArray)          if (!myRsArray)
# Line 1306  TS_SCardLocateCardsByATR(STREAM in, STRE Line 1269  TS_SCardLocateCardsByATR(STREAM in, STRE
1269          copyReaderState_MyPCSCToServer(myRsArray, rsArray, readerCount);          copyReaderState_MyPCSCToServer(myRsArray, rsArray, readerCount);
1270          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1271          {          {
1272  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardGetStatusChange (no SCardLocateCardsByATR) Code=0x%.8x-->\n", (unsigned int) rv));
                 printf("<--ERROR SCardGetStatusChange (no SCardLocateCardsByATR) Code=0x%.8x-->\n",  
                        (unsigned int) rv);  
 #endif  
1273          }          }
1274          else          else
1275          {          {
1276  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--SUCCESS SCardGetStatusChange (no SCardLocateCardsByATR)-->\n"));
                 printf("<--SUCCESS SCardGetStatusChange (no SCardLocateCardsByATR)-->\n");  
 #endif  
1277                  cur = pAtrMasks;                  cur = pAtrMasks;
1278                  for (i = 0, cur = pAtrMasks; i < atrMaskCount; i++, cur++)                  for (i = 0, cur = pAtrMasks; i < atrMaskCount; i++, cur++)
1279                  {                  {
# Line 1335  TS_SCardLocateCardsByATR(STREAM in, STRE Line 1293  TS_SCardLocateCardsByATR(STREAM in, STRE
1293                                  }                                  }
1294                                  if (equal)                                  if (equal)
1295                                  {                                  {
1296  #ifdef WITH_SCARD_DEBUG                                          DEBUG_SCARD(("[FOUND]\n"));
                                         printf("[FOUND]\n");  
 #endif  
1297                                          rsCur->dwEventState |= 0x00000040;      /* SCARD_STATE_ATRMATCH 0x00000040 */                                          rsCur->dwEventState |= 0x00000040;      /* SCARD_STATE_ATRMATCH 0x00000040 */
1298                                          memcpy(ResArray + j, rsCur, sizeof(SCARD_READERSTATE_A));                                          memcpy(ResArray + j, rsCur, sizeof(SCARD_READERSTATE_A));
1299                                  }                                  }
# Line 1355  TS_SCardLocateCardsByATR(STREAM in, STRE Line 1311  TS_SCardLocateCardsByATR(STREAM in, STRE
1311                             sizeof(SCARD_READERSTATE_A) - 2 * sizeof(unsigned char *));                             sizeof(SCARD_READERSTATE_A) - 2 * sizeof(unsigned char *));
1312          }          }
1313    
1314          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1315          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1316          return rv;          return rv;
1317  }  }
# Line 1370  TS_SCardBeginTransaction(STREAM in, STRE Line 1326  TS_SCardBeginTransaction(STREAM in, STRE
1326          in->p += 0x30;          in->p += 0x30;
1327          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
1328          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
1329  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
1330          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[myHCard = 0x%016lx]\n", (unsigned long) myHCard));
         printf("[myHCard = 0x%016lx]\n", myHCard);  
 #endif  
1331          rv = SCardBeginTransaction(myHCard);          rv = SCardBeginTransaction(myHCard);
 #ifdef WITH_SCARD_DEBUG  
1332          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1333                  printf("<--ERROR SCardBeginTransaction Code=0x%.8x-->\n", (unsigned int) rv);          {
1334                    DEBUG_SCARD(("<--ERROR SCardBeginTransaction Code=0x%.8x-->\n", (unsigned int) rv));
1335            }
1336          else          else
1337                  printf("<--SUCCESS SCardBeginTransaction-->\n");          {
1338  #endif                  DEBUG_SCARD(("<--SUCCESS SCardBeginTransaction-->\n"));
1339          outForceAllignment(out, 8);          }
1340            outForceAlignment(out, 8);
1341          return rv;          return rv;
1342  }  }
1343    
# Line 1398  TS_SCardEndTransaction(STREAM in, STREAM Line 1354  TS_SCardEndTransaction(STREAM in, STREAM
1354          in->p += 0x0C;          in->p += 0x0C;
1355          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
1356          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
1357  #ifdef WITH_SCARD_DEBUG  
1358          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
1359          printf("[myHCard = 0x%016lx]\n", myHCard);          DEBUG_SCARD(("[myHCard = 0x%016lx]\n", (unsigned long) myHCard));
1360          printf("[dwDisposition = 0x%.8x]\n", (unsigned int) dwDisposition);          DEBUG_SCARD(("[dwDisposition = 0x%.8x]\n", (unsigned int) dwDisposition));
1361  #endif  
1362          rv = SCardEndTransaction(myHCard, (MYPCSC_DWORD) dwDisposition);          rv = SCardEndTransaction(myHCard, (MYPCSC_DWORD) dwDisposition);
 #ifdef WITH_SCARD_DEBUG  
1363          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1364                  printf("<--ERROR SCardEndTransaction Code=0x%.8x-->\n", (unsigned int) rv);          {
1365                    DEBUG_SCARD(("<--ERROR SCardEndTransaction Code=0x%.8x-->\n", (unsigned int) rv));
1366            }
1367          else          else
1368                  printf("<--SUCCESS SCardEndTransaction-->\n");          {
1369  #endif                  DEBUG_SCARD(("<--SUCCESS SCardEndTransaction-->\n"));
1370          outForceAllignment(out, 8);          }
1371            outForceAlignment(out, 8);
1372          return rv;          return rv;
1373  }  }
1374    
# Line 1509  TS_SCardTransmit(STREAM in, STREAM out) Line 1467  TS_SCardTransmit(STREAM in, STREAM out)
1467    
1468          if (cbRecvLength)          if (cbRecvLength)
1469          {          {
                 /* FIXME: handle responses with length > 448 bytes */  
                 if (cbRecvLength > 448)  
                 {  
 #ifdef WITH_SCARD_DEBUG  
                         printf("[RECV LEN %d -> %d]\n", (unsigned int) cbRecvLength, 400);  
 #endif  
                         cbRecvLength = 448;  
                 }  
   
   
1470                  recvBuf = SC_xmalloc(&lcHandle, cbRecvLength);                  recvBuf = SC_xmalloc(&lcHandle, cbRecvLength);
1471                  if (!recvBuf)                  if (!recvBuf)
1472                          return SC_returnNoMemoryError(&lcHandle, in, out);                          return SC_returnNoMemoryError(&lcHandle, in, out);
# Line 1552  TS_SCardTransmit(STREAM in, STREAM out) Line 1500  TS_SCardTransmit(STREAM in, STREAM out)
1500          else          else
1501                  pioRecvPci = NULL;                  pioRecvPci = NULL;
1502    
1503  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1504          printf("++++++++++\n");          DEBUG_SCARD(("++++++++++\n"));
1505          printf("[SEND LEN = %d]\n", (unsigned int) cbSendLength);          DEBUG_SCARD(("[SEND LEN = %d]\n", (unsigned int) cbSendLength));
1506          printf("[RECV LEN = %d]\n", (unsigned int) cbRecvLength);          DEBUG_SCARD(("[RECV LEN = %d]\n", (unsigned int) cbRecvLength));
1507          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
1508          printf("[myHCard = 0x%016lx]\n", myHCard);          DEBUG_SCARD(("[myHCard = 0x%016lx]\n", (unsigned long) myHCard));
1509          printf("[pioSendPci]\n");          DEBUG_SCARD(("[pioSendPci]\n"));
1510          if (pioSendPci == NULL)          if (pioSendPci == NULL)
1511                  printf("NULL\n");          {
1512                    DEBUG_SCARD(("NULL\n"));
1513            }
1514          else          else
1515            {
1516                  hexdump((void *) pioSendPci, pioSendPci->cbPciLength);                  hexdump((void *) pioSendPci, pioSendPci->cbPciLength);
1517          printf("[pioRecvPci]\n");          }
1518    
1519            DEBUG_SCARD(("[pioRecvPci]\n"));
1520          if (pioRecvPci == NULL)          if (pioRecvPci == NULL)
1521                  printf("NULL\n");          {
1522                    DEBUG_SCARD(("NULL\n"));
1523            }
1524          else          else
1525            {
1526                  hexdump((void *) pioRecvPci, pioRecvPci->cbPciLength);                  hexdump((void *) pioRecvPci, pioRecvPci->cbPciLength);
1527          printf("[sendBuf]\n");          }
1528            DEBUG_SCARD(("[sendBuf]\n"));
1529          hexdump(sendBuf, cbSendLength);          hexdump(sendBuf, cbSendLength);
1530          printf("++++++++++\n");          DEBUG_SCARD(("++++++++++\n"));
1531  #endif  #endif
1532    
1533          myCbRecvLength = cbRecvLength;          myCbRecvLength = cbRecvLength;
# Line 1598  TS_SCardTransmit(STREAM in, STREAM out) Line 1555  TS_SCardTransmit(STREAM in, STREAM out)
1555          rv = SCardTransmit(myHCard, myPioSendPci, sendBuf, (MYPCSC_DWORD) cbSendLength,          rv = SCardTransmit(myHCard, myPioSendPci, sendBuf, (MYPCSC_DWORD) cbSendLength,
1556                             myPioRecvPci, recvBuf, &myCbRecvLength);                             myPioRecvPci, recvBuf, &myCbRecvLength);
1557          cbRecvLength = myCbRecvLength;          cbRecvLength = myCbRecvLength;
1558    
1559            /* FIXME: handle responses with length > 448 bytes */
1560            if (cbRecvLength > 448)
1561            {
1562                    warning("Card response limited from %d to 448 bytes!\n", cbRecvLength);
1563                    DEBUG_SCARD(("[RECV LEN %d -> %d]\n", (unsigned int) cbRecvLength, 400));
1564                    cbRecvLength = 448;
1565            }
1566    
1567          if (pioRecvPci)          if (pioRecvPci)
1568          {          {
1569                  copyIORequest_MyPCSCToServer(myPioRecvPci, pioRecvPci);                  copyIORequest_MyPCSCToServer(myPioRecvPci, pioRecvPci);
# Line 1605  TS_SCardTransmit(STREAM in, STREAM out) Line 1571  TS_SCardTransmit(STREAM in, STREAM out)
1571    
1572          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1573          {          {
1574  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardTransmit Code=0x%.8x-->\n", (unsigned int) rv));
                 printf("<--ERROR SCardTransmit Code=0x%.8x-->\n", (unsigned int) rv);  
 #endif  
1575          }          }
1576          else          else
1577          {          {
1578  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1579                  printf("<--SUCCESS SCardTransmit-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardTransmit-->\n"));
1580                  printf("RESULT %d\n", (unsigned int) cbRecvLength);                  DEBUG_SCARD(("RESULT %d\n", (unsigned int) cbRecvLength));
1581                  hexdump(recvBuf, cbRecvLength);                  hexdump(recvBuf, cbRecvLength);
1582                  if (myPioRecvPci)                  if (myPioRecvPci)
1583                  {                  {
1584                          printf("--- myPioRecvPci ---\n");                          DEBUG_SCARD(("--- myPioRecvPci ---\n"));
1585                          hexdump((void *) myPioRecvPci, myPioRecvPci->cbPciLength);                          hexdump((void *) myPioRecvPci, myPioRecvPci->cbPciLength);
1586                  }                  }
1587                  printf("------------------\n");                  DEBUG_SCARD(("------------------\n"));
1588  #endif  #endif
1589  #if 0  #if 0
1590                  if ((pioRecvPci != NULL) && (pioRecvPci->cbPciLength > 0))                  if ((pioRecvPci != NULL) && (pioRecvPci->cbPciLength > 0))
# Line 1647  TS_SCardTransmit(STREAM in, STREAM out) Line 1611  TS_SCardTransmit(STREAM in, STREAM out)
1611    
1612                  outBufferFinish(out, (char *) recvBuf, cbRecvLength);                  outBufferFinish(out, (char *) recvBuf, cbRecvLength);
1613          }          }
1614          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1615          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1616          return rv;          return rv;
1617  }  }
# Line 1672  TS_SCardStatus(STREAM in, STREAM out, BO Line 1636  TS_SCardStatus(STREAM in, STREAM out, BO
1636          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
1637          in->p += 0x04;          in->p += 0x04;
1638          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
1639  #ifdef WITH_SCARD_DEBUG  
1640          printf("[hCard 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[hCard 0x%.8x]\n", (unsigned int) hCard));
1641          printf("[myHCard 0x%016lx]\n", myHCard);          DEBUG_SCARD(("[myHCard 0x%016lx]\n", (unsigned long) myHCard));
1642          printf("[dwReaderLen %d]\n", (unsigned int) dwReaderLen);          DEBUG_SCARD(("[dwReaderLen %d]\n", (unsigned int) dwReaderLen));
1643          printf("[dwAtrLen %d]\n", (unsigned int) dwAtrLen);          DEBUG_SCARD(("[dwAtrLen %d]\n", (unsigned int) dwAtrLen));
 #endif  
1644    
1645          if (dwReaderLen <= 0 || dwReaderLen == SCARD_AUTOALLOCATE || dwReaderLen > SCARD_MAX_MEM)          if (dwReaderLen <= 0 || dwReaderLen == SCARD_AUTOALLOCATE || dwReaderLen > SCARD_MAX_MEM)
1646                  dwReaderLen = SCARD_MAX_MEM;                  dwReaderLen = SCARD_MAX_MEM;
# Line 1705  TS_SCardStatus(STREAM in, STREAM out, BO Line 1668  TS_SCardStatus(STREAM in, STREAM out, BO
1668    
1669          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1670          {          {
1671  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardStatus Code=0x%.8x-->\n", (unsigned int) rv));
                 printf("<--ERROR SCardStatus Code=0x%.8x-->\n", (unsigned int) rv);  
 #endif  
1672                  return SC_returnCode(rv, &lcHandle, in, out);                  return SC_returnCode(rv, &lcHandle, in, out);
1673          }          }
1674          else          else
1675          {          {
1676  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1677                  printf("<--SUCCESS SCardStatus-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardStatus-->\n"));
1678                  printf("[dwState 0x%.8x]\n", (unsigned int) dwState);                  DEBUG_SCARD(("[dwState 0x%.8x]\n", (unsigned int) dwState));
1679                  printf("[dwProtocol 0x%.8x]\n", (unsigned int) dwProtocol);                  DEBUG_SCARD(("[dwProtocol 0x%.8x]\n", (unsigned int) dwProtocol));
1680                  printf("[Reader Name]\n");                  DEBUG_SCARD(("[Reader Name]\n"));
1681                  hexdump((unsigned char *) readerName, dwReaderLen);                  hexdump((unsigned char *) readerName, dwReaderLen);
1682                  printf("[Atr]\n");                  DEBUG_SCARD(("[Atr]\n"));
1683                  hexdump(atr, dwAtrLen);                  hexdump(atr, dwAtrLen);
1684  #endif  #endif
1685                  if (dwState & (SCARD_SPECIFIC | SCARD_NEGOTIABLE))                  if (dwState & (SCARD_SPECIFIC | SCARD_NEGOTIABLE))
# Line 1766  TS_SCardStatus(STREAM in, STREAM out, BO Line 1727  TS_SCardStatus(STREAM in, STREAM out, BO
1727                  out_uint32_le(out, dataLength);                  out_uint32_le(out, dataLength);
1728                  out->p = psave;                  out->p = psave;
1729          }          }
1730          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1731          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1732          return rv;          return rv;
1733  }  }
# Line 1789  TS_SCardState(STREAM in, STREAM out) Line 1750  TS_SCardState(STREAM in, STREAM out)
1750          in_uint32_le(in, hCard);          in_uint32_le(in, hCard);
1751          in->p += 0x04;          in->p += 0x04;
1752          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
 #ifdef WITH_SCARD_DEBUG  
         printf("[hCard 0x%.8x]\n", (unsigned int) hCard);  
         printf("[myHCard 0x%.8x]\n", (unsigned int) myHCard);  
         printf("[dwAtrLen %d]\n", (unsigned int) dwAtrLen);  
 #endif  
1753    
1754            DEBUG_SCARD(("[hCard 0x%.8x]\n", (unsigned int) hCard));
1755            DEBUG_SCARD(("[myHCard 0x%.8x]\n", (unsigned int) myHCard));
1756            DEBUG_SCARD(("[dwAtrLen %d]\n", (unsigned int) dwAtrLen));
1757    
1758          dwReaderLen = SCARD_MAX_MEM;          dwReaderLen = SCARD_MAX_MEM;
1759          if (dwAtrLen <= 0 || dwAtrLen == SCARD_AUTOALLOCATE || dwAtrLen > SCARD_MAX_MEM)          if (dwAtrLen <= 0 || dwAtrLen == SCARD_AUTOALLOCATE || dwAtrLen > SCARD_MAX_MEM)
# Line 1820  TS_SCardState(STREAM in, STREAM out) Line 1779  TS_SCardState(STREAM in, STREAM out)
1779    
1780          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1781          {          {
1782  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardStatus (no ScardState) Code=0x%.8x-->\n",
1783                  printf("<--ERROR SCardStatus (no ScardState) Code=0x%.8x-->\n", (unsigned int) rv);                               (unsigned int) rv));
 #endif  
1784                  return SC_returnCode(rv, &lcHandle, in, out);                  return SC_returnCode(rv, &lcHandle, in, out);
1785          }          }
1786          else          else
1787          {          {
1788  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1789                  printf("<--SUCCESS SCardStatus (no ScardState)-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardStatus (no ScardState)-->\n"));
1790                  printf("[dwState 0x%.8x]\n", (unsigned int) dwState);                  DEBUG_SCARD(("[dwState 0x%.8x]\n", (unsigned int) dwState));
1791                  printf("[dwProtocol 0x%.8x]\n", (unsigned int) dwProtocol);                  DEBUG_SCARD(("[dwProtocol 0x%.8x]\n", (unsigned int) dwProtocol));
1792                  printf("[Atr]\n");                  DEBUG_SCARD(("[Atr]\n"));
1793                  hexdump(atr, dwAtrLen);                  hexdump(atr, dwAtrLen);
1794  #endif  #endif
1795                  if (dwState & (SCARD_SPECIFIC | SCARD_NEGOTIABLE))                  if (dwState & (SCARD_SPECIFIC | SCARD_NEGOTIABLE))
# Line 1863  TS_SCardState(STREAM in, STREAM out) Line 1821  TS_SCardState(STREAM in, STREAM out)
1821                  out_uint8p(out, atr, dwAtrLen);                  out_uint8p(out, atr, dwAtrLen);
1822                  outRepos(out, dwAtrLen);                  outRepos(out, dwAtrLen);
1823          }          }
1824          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1825          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1826          return rv;          return rv;
1827  }  }
# Line 1899  TS_SCardListReaderGroups(STREAM in, STRE Line 1857  TS_SCardListReaderGroups(STREAM in, STRE
1857          dwGroups = groups;          dwGroups = groups;
1858    
1859          if (rv)          if (rv)
 #ifdef WITH_SCARD_DEBUG  
1860          {          {
1861                  printf("<--ERROR SCardListReaderGroups Code=0x%.8x-->\n", (unsigned int) rv);                  DEBUG_SCARD(("<--ERROR SCardListReaderGroups Code=0x%.8x-->\n", (unsigned int) rv));
 #endif  
1862                  return SC_returnCode(rv, &lcHandle, in, out);                  return SC_returnCode(rv, &lcHandle, in, out);
 #ifdef WITH_SCARD_DEBUG  
1863          }          }
1864          else          else
1865          {          {
1866                  printf("<--SUCCESS SCardListReaderGroups-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardListReaderGroups-->\n"));
1867          }          }
 #endif  
1868    
1869    
1870          out_uint32_le(out, dwGroups);          out_uint32_le(out, dwGroups);
# Line 1920  TS_SCardListReaderGroups(STREAM in, STRE Line 1874  TS_SCardListReaderGroups(STREAM in, STRE
1874          outRepos(out, dwGroups);          outRepos(out, dwGroups);
1875          out_uint32_le(out, 0x00000000);          out_uint32_le(out, 0x00000000);
1876    
1877          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1878          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
1879          return rv;          return rv;
1880  }  }
# Line 1946  TS_SCardGetAttrib(STREAM in, STREAM out) Line 1900  TS_SCardGetAttrib(STREAM in, STREAM out)
1900    
1901          dwAttrId = dwAttrId & 0x0000FFFF;          dwAttrId = dwAttrId & 0x0000FFFF;
1902    
1903  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
1904          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[myHCard 0x%.8x]\n", (unsigned int) myHCard));
1905          printf("[myHCard 0x%.8x]\n", (unsigned int) myHCard);          DEBUG_SCARD(("[dwAttrId = 0x%.8x]\n", (unsigned int) dwAttrId));
1906          printf("[dwAttrId = 0x%.8x]\n", (unsigned int) dwAttrId);          DEBUG_SCARD(("[dwAttrLen = 0x%.8x]\n", (unsigned int) dwAttrLen));
         printf("[dwAttrLen = 0x%.8x]\n", (unsigned int) dwAttrLen);  
 #endif  
1907    
1908          if (dwAttrLen > MAX_BUFFER_SIZE)          if (dwAttrLen > MAX_BUFFER_SIZE)
1909                  dwAttrLen = MAX_BUFFER_SIZE;                  dwAttrLen = MAX_BUFFER_SIZE;
# Line 1977  TS_SCardGetAttrib(STREAM in, STREAM out) Line 1929  TS_SCardGetAttrib(STREAM in, STREAM out)
1929    
1930          if (dwAttrId == 0x00000100 && rv != SCARD_S_SUCCESS)          if (dwAttrId == 0x00000100 && rv != SCARD_S_SUCCESS)
1931          {          {
1932  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("Get Attribute ATTR_VENDOR_NAME\n"));
                 printf("Get Attribute ATTR_VENDOR_NAME\n");  
 #endif  
1933                  pthread_mutex_lock(&hcardAccess);                  pthread_mutex_lock(&hcardAccess);
1934                  PSCHCardRec hcard = hcardFirst;                  PSCHCardRec hcard = hcardFirst;
1935                  while (hcard)                  while (hcard)
# Line 1994  TS_SCardGetAttrib(STREAM in, STREAM out) Line 1944  TS_SCardGetAttrib(STREAM in, STREAM out)
1944                          hcard = hcard->next;                          hcard = hcard->next;
1945                  }                  }
1946                  pthread_mutex_unlock(&hcardAccess);                  pthread_mutex_unlock(&hcardAccess);
1947  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("[0x%.8x]\n", (unsigned int) rv));
                 printf("[0x%.8x]\n", (unsigned int) rv);  
 #endif  
1948          }          }
1949    
1950          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
1951          {          {
1952  #ifdef WITH_SCARD_DEBUG                  DEBUG_SCARD(("<--ERROR SCardGetAttrib Code=0x%.8x-->\n", (unsigned int) rv));
                 printf("<--ERROR SCardGetAttrib Code=0x%.8x-->\n", (unsigned int) rv);  
 #endif  
1953                  return SC_returnCode(rv, &lcHandle, in, out);                  return SC_returnCode(rv, &lcHandle, in, out);
1954          }          }
1955          else          else
1956          {          {
1957  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
1958                  printf("<--SUCCESS SCardGetAttrib-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardGetAttrib-->\n"));
1959                  printf("[LENGTH %d]\n", (unsigned int) dwAttrLen);                  DEBUG_SCARD(("[LENGTH %d]\n", (unsigned int) dwAttrLen));
1960                  printf("+++++++++++++++++++++\n");                  DEBUG_SCARD(("+++++++++++++++++++++\n"));
1961                  hexdump(pbAttr, dwAttrLen);                  hexdump(pbAttr, dwAttrLen);
1962                  printf("+++++++++++++++++++++\n");                  DEBUG_SCARD(("+++++++++++++++++++++\n"));
1963  #endif  #endif
1964                  out_uint32_le(out, dwAttrLen);                  out_uint32_le(out, dwAttrLen);
1965                  out_uint32_le(out, 0x00000200);                  out_uint32_le(out, 0x00000200);
# Line 2029  TS_SCardGetAttrib(STREAM in, STREAM out) Line 1975  TS_SCardGetAttrib(STREAM in, STREAM out)
1975                  outRepos(out, dwAttrLen);                  outRepos(out, dwAttrLen);
1976                  out_uint32_le(out, 0x00000000);                  out_uint32_le(out, 0x00000000);
1977          }          }
1978          outForceAllignment(out, 8);          outForceAlignment(out, 8);
1979          return rv;          return rv;
1980  }  }
1981    
# Line 2054  TS_SCardSetAttrib(STREAM in, STREAM out) Line 2000  TS_SCardSetAttrib(STREAM in, STREAM out)
2000    
2001          dwAttrId = dwAttrId & 0x0000FFFF;          dwAttrId = dwAttrId & 0x0000FFFF;
2002    
2003  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
2004          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[myHCard 0x%.8x]\n", (unsigned int) myHCard));
2005          printf("[myHCard 0x%.8x]\n", (unsigned int) myHCard);          DEBUG_SCARD(("[dwAttrId = 0x%.8x]\n", (unsigned int) dwAttrId));
2006          printf("[dwAttrId = 0x%.8x]\n", (unsigned int) dwAttrId);          DEBUG_SCARD(("[dwAttrLen = 0x%.8x]\n", (unsigned int) dwAttrLen));
         printf("[dwAttrLen = 0x%.8x]\n", (unsigned int) dwAttrLen);  
 #endif  
2007    
2008          if (dwAttrLen > MAX_BUFFER_SIZE)          if (dwAttrLen > MAX_BUFFER_SIZE)
2009                  dwAttrLen = MAX_BUFFER_SIZE;                  dwAttrLen = MAX_BUFFER_SIZE;
# Line 2071  TS_SCardSetAttrib(STREAM in, STREAM out) Line 2015  TS_SCardSetAttrib(STREAM in, STREAM out)
2015          in_uint8a(in, pbAttr, dwAttrLen);          in_uint8a(in, pbAttr, dwAttrLen);
2016          rv = SCardSetAttrib(myHCard, (MYPCSC_DWORD) dwAttrId, pbAttr, (MYPCSC_DWORD) dwAttrLen);          rv = SCardSetAttrib(myHCard, (MYPCSC_DWORD) dwAttrId, pbAttr, (MYPCSC_DWORD) dwAttrLen);
2017    
 #ifdef WITH_SCARD_DEBUG  
2018          if (rv)          if (rv)
2019                  printf("<--ERROR SCardSetAttrib Code=0x%.8x-->\n", (unsigned int) rv);          {
2020                    DEBUG_SCARD(("<--ERROR SCardSetAttrib Code=0x%.8x-->\n", (unsigned int) rv));
2021            }
2022          else          else
2023                  printf("<--SUCCESS SCardSetAttrib-->\n");          {
2024  #endif                  DEBUG_SCARD(("<--SUCCESS SCardSetAttrib-->\n"));
2025            }
2026    
2027          out_uint32_le(out, 0x00000000);          out_uint32_le(out, 0x00000000);
2028          out_uint32_le(out, 0x00000200);          out_uint32_le(out, 0x00000200);
2029          out_uint32_le(out, 0x00000000);          out_uint32_le(out, 0x00000000);
2030          out_uint32_le(out, 0x00000000);          out_uint32_le(out, 0x00000000);
2031          outForceAllignment(out, 8);          outForceAlignment(out, 8);
2032          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
2033          return rv;          return rv;
2034  }  }
# Line 2144  TS_SCardControl(STREAM in, STREAM out) Line 2091  TS_SCardControl(STREAM in, STREAM out)
2091          if (!pOutBuffer)          if (!pOutBuffer)
2092                  return SC_returnNoMemoryError(&lcHandle, in, out);                  return SC_returnNoMemoryError(&lcHandle, in, out);
2093    
2094  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
2095          printf("[hContext = 0x%.8x]\n", (unsigned int) hContext);          DEBUG_SCARD(("[hContext = 0x%.8x]\n", (unsigned int) hContext));
2096          printf("[hCard = 0x%.8x]\n", (unsigned int) hCard);          DEBUG_SCARD(("[hCard = 0x%.8x]\n", (unsigned int) hCard));
2097          printf("[dwControlCode = 0x%.8x]\n", (unsigned int) dwControlCode);          DEBUG_SCARD(("[dwControlCode = 0x%.8x]\n", (unsigned int) dwControlCode));
2098          printf("[nInBufferSize  = %d]\n", (unsigned int) nInBufferSize);          DEBUG_SCARD(("[nInBufferSize  = %d]\n", (unsigned int) nInBufferSize));
2099          printf("[nOutBufferSize  = %d]\n", (unsigned int) nOutBufferSize);          DEBUG_SCARD(("[nOutBufferSize  = %d]\n", (unsigned int) nOutBufferSize));
2100          if (nInBufferSize > 0)          if (nInBufferSize > 0)
2101          {          {
2102                  printf("[In buffer]\n");                  DEBUG_SCARD(("[In buffer]\n"));
2103                  hexdump((unsigned char *) pInBuffer, nInBufferSize);                  hexdump((unsigned char *) pInBuffer, nInBufferSize);
2104          }          }
2105          printf("---> Calling SCardControl\n");          DEBUG_SCARD(("---> Calling SCardControl\n"));
2106  #endif  #endif
2107    
 #ifdef WITH_PCSC120  
2108          sc_nBytesReturned = nBytesReturned;          sc_nBytesReturned = nBytesReturned;
2109          myHCard = scHandleToMyPCSC(hCard);          myHCard = scHandleToMyPCSC(hCard);
2110    #ifdef WITH_PCSC120
2111          rv = SCardControl(myHCard, pInBuffer, (MYPCSC_DWORD) nInBufferSize, pOutBuffer,          rv = SCardControl(myHCard, pInBuffer, (MYPCSC_DWORD) nInBufferSize, pOutBuffer,
2112                            &sc_nBytesReturned);                            &sc_nBytesReturned);
         nBytesReturned = sc_nBytesReturned;  
2113  #else  #else
         sc_nBytesReturned = nBytesReturned;  
         myHCard = scHandleToMyPCSC(hCard);  
2114          rv = SCardControl(myHCard, (MYPCSC_DWORD) dwControlCode, pInBuffer,          rv = SCardControl(myHCard, (MYPCSC_DWORD) dwControlCode, pInBuffer,
2115                            (MYPCSC_DWORD) nInBufferSize, pOutBuffer,                            (MYPCSC_DWORD) nInBufferSize, pOutBuffer,
2116                            (MYPCSC_DWORD) nOutBufferRealSize, &sc_nBytesReturned);                            (MYPCSC_DWORD) nOutBufferRealSize, &sc_nBytesReturned);
         nBytesReturned = sc_nBytesReturned;  
2117  #endif  #endif
2118            nBytesReturned = sc_nBytesReturned;
2119    
2120  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
2121          if (rv != SCARD_S_SUCCESS)          if (rv != SCARD_S_SUCCESS)
2122                  printf("<--ERROR SCardControl Code=0x%.8x-->\n", (unsigned int) rv);          {
2123                    DEBUG_SCARD(("<--ERROR SCardControl Code=0x%.8x-->\n", (unsigned int) rv));
2124            }
2125          else          else
2126          {          {
2127                  printf("<--SUCCESS SCardControl-->\n");                  DEBUG_SCARD(("<--SUCCESS SCardControl-->\n"));
2128                  printf("[LENGTH %d]\n", (unsigned int) nBytesReturned);                  DEBUG_SCARD(("[LENGTH %d]\n", (unsigned int) nBytesReturned));
2129                  printf("+++++++++++++++++++++\n");                  DEBUG_SCARD(("+++++++++++++++++++++\n"));
2130                  hexdump((unsigned char *) pOutBuffer, nBytesReturned);                  hexdump((unsigned char *) pOutBuffer, nBytesReturned);
2131                  printf("+++++++++++++++++++++\n");                  DEBUG_SCARD(("+++++++++++++++++++++\n"));
2132          }          }
2133  #endif  #endif
2134    
# Line 2195  TS_SCardControl(STREAM in, STREAM out) Line 2141  TS_SCardControl(STREAM in, STREAM out)
2141                  outRepos(out, nBytesReturned);                  outRepos(out, nBytesReturned);
2142          }          }
2143    
2144          outForceAllignment(out, 8);          outForceAlignment(out, 8);
2145          SC_xfreeallmemory(&lcHandle);          SC_xfreeallmemory(&lcHandle);
2146          return rv;          return rv;
2147  }  }
# Line 2214  scard_device_control(NTHANDLE handle, ui Line 2160  scard_device_control(NTHANDLE handle, ui
2160          SERVER_DWORD Result = 0x00000000;          SERVER_DWORD Result = 0x00000000;
2161          unsigned char *psize, *pend, *pStatusCode;          unsigned char *psize, *pend, *pStatusCode;
2162          SERVER_DWORD addToEnd = 0;          SERVER_DWORD addToEnd = 0;
2163    #ifdef WITH_DEBUG_SCARD
 #ifdef WITH_SCARD_DEBUG  
2164          unsigned char *pbeg = out->p;          unsigned char *pbeg = out->p;
2165          printf("--------------------------------\n");  
2166          printf("[NTHANDLE %08X]\n", handle);          DEBUG_SCARD(("--------------------------------\n"));
2167          printf("[REQUEST %08X]\n", request);          DEBUG_SCARD(("[NTHANDLE %08X]\n", handle));
2168          printf("[INPUT DUMP]--------------------\n");          DEBUG_SCARD(("[REQUEST %08X]\n", request));
2169            DEBUG_SCARD(("[INPUT DUMP]--------------------\n"));
2170          hexdump(in->p, in->end - in->p);          hexdump(in->p, in->end - in->p);
2171  #endif  #endif
2172          /* Processing request */          /* Processing request */
# Line 2238  scard_device_control(NTHANDLE handle, ui Line 2184  scard_device_control(NTHANDLE handle, ui
2184                          /* SCardEstablishContext */                          /* SCardEstablishContext */
2185                  case SC_ESTABLISH_CONTEXT:                  case SC_ESTABLISH_CONTEXT:
2186                          {                          {
2187  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardEstablishContext--->\n"));
                                 printf("<---SCardEstablishContext--->\n");  
 #endif  
2188                                  Result = (SERVER_DWORD) TS_SCardEstablishContext(in, out);                                  Result = (SERVER_DWORD) TS_SCardEstablishContext(in, out);
2189                                  break;                                  break;
2190                          }                          }
2191                          /* SCardReleaseContext */                          /* SCardReleaseContext */
2192                  case SC_RELEASE_CONTEXT:                  case SC_RELEASE_CONTEXT:
2193                          {                          {
2194  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardReleaseContext--->\n"));
                                 printf("<---SCardReleaseContext--->\n");  
 #endif  
2195                                  Result = (SERVER_DWORD) TS_SCardReleaseContext(in, out);                                  Result = (SERVER_DWORD) TS_SCardReleaseContext(in, out);
2196                                  break;                                  break;
2197                          }                          }
2198                          /* SCardIsValidContext */                          /* SCardIsValidContext */
2199                  case SC_IS_VALID_CONTEXT:                  case SC_IS_VALID_CONTEXT:
2200                          {                          {
2201  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardIsValidContext--->\n"));
                                 printf("<---SCardIsValidContext--->\n");  
 #endif  
2202                                  Result = (SERVER_DWORD) TS_SCardIsValidContext(in, out);                                  Result = (SERVER_DWORD) TS_SCardIsValidContext(in, out);
2203                                  break;                                  break;
2204                          }                          }
# Line 2267  scard_device_control(NTHANDLE handle, ui Line 2207  scard_device_control(NTHANDLE handle, ui
2207                  case SC_LIST_READERS + 4:       /* SCardListReadersW */                  case SC_LIST_READERS + 4:       /* SCardListReadersW */
2208                          {                          {
2209                                  BOOL wide = request != SC_LIST_READERS;                                  BOOL wide = request != SC_LIST_READERS;
2210  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardListReaders---> (%s)\n",
2211                                  printf("<---SCardListReaders---> (%s)\n",                                               (wide) ? ("WIDE") : ("ASCII")));
                                        (wide) ? ("WIDE") : ("ASCII"));  
 #endif  
2212                                  Result = (SERVER_DWORD) TS_SCardListReaders(in, out, wide);                                  Result = (SERVER_DWORD) TS_SCardListReaders(in, out, wide);
2213                                  break;                                  break;
2214                          }                          }
# Line 2279  scard_device_control(NTHANDLE handle, ui Line 2217  scard_device_control(NTHANDLE handle, ui
2217                  case SC_CONNECT + 4:    /* SCardConnectW */                  case SC_CONNECT + 4:    /* SCardConnectW */
2218                          {                          {
2219                                  BOOL wide = request != SC_CONNECT;                                  BOOL wide = request != SC_CONNECT;
2220  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardConnect---> (%s)\n",
2221                                  printf("<---SCardConnect---> (%s)\n",                                               (wide) ? ("WIDE") : ("ASCII")));
                                        (wide) ? ("WIDE") : ("ASCII"));  
 #endif  
2222                                  Result = (SERVER_DWORD) TS_SCardConnect(in, out, wide);                                  Result = (SERVER_DWORD) TS_SCardConnect(in, out, wide);
2223                                  break;                                  break;
2224                          }                          }
2225                          /* ScardReconnect */                          /* ScardReconnect */
2226                  case SC_RECONNECT:                  case SC_RECONNECT:
2227                          {                          {
2228  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardReconnect--->\n"));
                                 printf("<---SCardReconnect--->\n");  
 #endif  
2229                                  Result = (SERVER_DWORD) TS_SCardReconnect(in, out);                                  Result = (SERVER_DWORD) TS_SCardReconnect(in, out);
2230                                  break;                                  break;
2231                          }                          }
2232                          /* ScardDisconnect */                          /* ScardDisconnect */
2233                  case SC_DISCONNECT:                  case SC_DISCONNECT:
2234                          {                          {
2235  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardDisconnect--->\n"));
                                 printf("<---SCardDisconnect--->\n");  
 #endif  
2236                                  Result = (SERVER_DWORD) TS_SCardDisconnect(in, out);                                  Result = (SERVER_DWORD) TS_SCardDisconnect(in, out);
2237                                  break;                                  break;
2238                          }                          }
# Line 2309  scard_device_control(NTHANDLE handle, ui Line 2241  scard_device_control(NTHANDLE handle, ui
2241                  case SC_GET_STATUS_CHANGE + 4:  /* SCardGetStatusChangeW */                  case SC_GET_STATUS_CHANGE + 4:  /* SCardGetStatusChangeW */
2242                          {                          {
2243                                  BOOL wide = request != SC_GET_STATUS_CHANGE;                                  BOOL wide = request != SC_GET_STATUS_CHANGE;
2244  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardGetStatusChange---> (%s)\n",
2245                                  printf("<---SCardGetStatusChange---> (%s)\n",                                               (wide) ? ("WIDE") : ("ASCII")));
                                        (wide) ? ("WIDE") : ("ASCII"));  
 #endif  
2246                                  Result = (SERVER_DWORD) TS_SCardGetStatusChange(in, out, wide);                                  Result = (SERVER_DWORD) TS_SCardGetStatusChange(in, out, wide);
2247                                  break;                                  break;
2248                          }                          }
2249                          /* SCardCancel */                          /* SCardCancel */
2250                  case SC_CANCEL:                  case SC_CANCEL:
2251                          {                          {
2252  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardCancel--->\n"));
                                 printf("<---SCardCancel--->\n");  
 #endif  
2253                                  Result = (SERVER_DWORD) TS_SCardCancel(in, out);                                  Result = (SERVER_DWORD) TS_SCardCancel(in, out);
2254                                  break;                                  break;
2255                          }                          }
# Line 2330  scard_device_control(NTHANDLE handle, ui Line 2258  scard_device_control(NTHANDLE handle, ui
2258                  case SC_LOCATE_CARDS_BY_ATR + 4:        /* SCardLocateCardsByATRW */                  case SC_LOCATE_CARDS_BY_ATR + 4:        /* SCardLocateCardsByATRW */
2259                          {                          {
2260                                  BOOL wide = request != SC_LOCATE_CARDS_BY_ATR;                                  BOOL wide = request != SC_LOCATE_CARDS_BY_ATR;
2261  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardLocateCardsByATR---> (%s)\n",
2262                                  printf("<---SCardLocateCardsByATR---> (%s)\n",                                               (wide) ? ("WIDE") : ("ASCII")));
                                        (wide) ? ("WIDE") : ("ASCII"));  
 #endif  
2263                                  Result = (SERVER_DWORD) TS_SCardLocateCardsByATR(in, out, wide);                                  Result = (SERVER_DWORD) TS_SCardLocateCardsByATR(in, out, wide);
2264                                  break;                                  break;
2265                          }                          }
2266                          /* SCardBeginTransaction */                          /* SCardBeginTransaction */
2267                  case SC_BEGIN_TRANSACTION:                  case SC_BEGIN_TRANSACTION:
2268                          {                          {
2269  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardBeginTransaction--->\n"));
                                 printf("<---SCardBeginTransaction--->\n");  
 #endif  
2270                                  Result = (SERVER_DWORD) TS_SCardBeginTransaction(in, out);                                  Result = (SERVER_DWORD) TS_SCardBeginTransaction(in, out);
2271                                  break;                                  break;
2272                          }                          }
2273                          /* SCardBeginTransaction */                          /* SCardBeginTransaction */
2274                  case SC_END_TRANSACTION:                  case SC_END_TRANSACTION:
2275                          {                          {
2276  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardEndTransaction--->\n"));
                                 printf("<---SCardEndTransaction--->\n");  
 #endif  
2277                                  Result = (SERVER_DWORD) TS_SCardEndTransaction(in, out);                                  Result = (SERVER_DWORD) TS_SCardEndTransaction(in, out);
2278                                  break;                                  break;
2279                          }                          }
2280                          /* ScardTransmit */                          /* ScardTransmit */
2281                  case SC_TRANSMIT:                  case SC_TRANSMIT:
2282                          {                          {
2283  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardTransmit--->\n"));
                                 printf("<---SCardTransmit--->\n");  
 #endif  
2284                                  Result = (SERVER_DWORD) TS_SCardTransmit(in, out);                                  Result = (SERVER_DWORD) TS_SCardTransmit(in, out);
2285                                  break;                                  break;
2286                          }                          }
2287                          /* SCardControl */                          /* SCardControl */
2288                  case SC_CONTROL:                  case SC_CONTROL:
2289                          {                          {
2290  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardControl--->\n"));
                                 printf("<---SCardControl--->\n");  
 #endif  
2291                                  Result = (SERVER_DWORD) TS_SCardControl(in, out);                                  Result = (SERVER_DWORD) TS_SCardControl(in, out);
2292                                  break;                                  break;
2293                          }                          }
# Line 2377  scard_device_control(NTHANDLE handle, ui Line 2295  scard_device_control(NTHANDLE handle, ui
2295  #ifndef WITH_PCSC120  #ifndef WITH_PCSC120
2296                  case SC_GETATTRIB:                  case SC_GETATTRIB:
2297                          {                          {
2298  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardGetAttrib--->\n"));
                                 printf("<---SCardGetAttrib--->\n");  
 #endif  
2299                                  Result = (SERVER_DWORD) TS_SCardGetAttrib(in, out);                                  Result = (SERVER_DWORD) TS_SCardGetAttrib(in, out);
2300                                  break;                                  break;
2301                          }                          }
2302  #endif  #endif
2303                  case SC_ACCESS_STARTED_EVENT:                  case SC_ACCESS_STARTED_EVENT:
2304                          {                          {
2305  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardAccessStartedEvent-->\n"));
                                 printf("<---SCardAccessStartedEvent-->\n");  
 #endif  
2306                                  Result = (SERVER_DWORD) TS_SCardAccessStartedEvent(in, out);                                  Result = (SERVER_DWORD) TS_SCardAccessStartedEvent(in, out);
2307                                  break;                                  break;
2308                          }                          }
# Line 2396  scard_device_control(NTHANDLE handle, ui Line 2310  scard_device_control(NTHANDLE handle, ui
2310                  case SC_STATUS + 4:     /* SCardStatusW */                  case SC_STATUS + 4:     /* SCardStatusW */
2311                          {                          {
2312                                  BOOL wide = request != SC_STATUS;                                  BOOL wide = request != SC_STATUS;
2313  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardStatus---> (%s)\n",
2314                                  printf("<---SCardStatus---> (%s)\n", (wide) ? ("WIDE") : ("ASCII"));                                               (wide) ? ("WIDE") : ("ASCII")));
 #endif  
2315                                  Result = (SERVER_DWORD) TS_SCardStatus(in, out, wide);                                  Result = (SERVER_DWORD) TS_SCardStatus(in, out, wide);
2316                                  break;                                  break;
2317                          }                          }
2318                  case SC_STATE:  /* SCardState */                  case SC_STATE:  /* SCardState */
2319                          {                          {
2320  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---SCardState--->"));
                                 printf("<---SCardState--->");  
 #endif  
2321                                  Result = (SERVER_DWORD) TS_SCardState(in, out);                                  Result = (SERVER_DWORD) TS_SCardState(in, out);
2322                                  break;                                  break;
2323                          }                          }
2324                  default:                  default:
2325                          {                          {
2326  #ifdef WITH_SCARD_DEBUG                                  DEBUG_SCARD(("<---UNSUPPORTED-FUNC--->\n"));
                                 printf("<---UNSUPPORTED-FUNC--->\n");  
 #endif  
2327                                  Result = 0x80100014;                                  Result = 0x80100014;
2328                                  out_uint8s(out, 256);                                  out_uint8s(out, 256);
2329                                  break;                                  break;
# Line 2441  scard_device_control(NTHANDLE handle, ui Line 2350  scard_device_control(NTHANDLE handle, ui
2350                  out_uint8s(out, addToEnd);                  out_uint8s(out, addToEnd);
2351          }          }
2352    
2353  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
2354          printf("[OUTPUT DUMP]-------------------\n");          DEBUG_SCARD(("[OUTPUT DUMP]-------------------\n"));
2355          hexdump(pbeg, (size_t) (out->p) - (size_t) pbeg);          hexdump(pbeg, (size_t) (out->p) - (size_t) pbeg);
2356          printf("--------------------------------\n");          DEBUG_SCARD(("--------------------------------\n"));
2357  #endif  #endif
2358          return STATUS_SUCCESS;          return STATUS_SUCCESS;
2359  }  }
# Line 2581  SC_deviceControl(PSCThreadData data) Line 2490  SC_deviceControl(PSCThreadData data)
2490          size_t buffer_len = 0;          size_t buffer_len = 0;
2491          scard_device_control(data->handle, data->request, data->in, data->out);          scard_device_control(data->handle, data->request, data->in, data->out);
2492          buffer_len = (size_t) data->out->p - (size_t) data->out->data;          buffer_len = (size_t) data->out->p - (size_t) data->out->data;
 #if 0  
         pthread_mutex_lock(&sendControl);  
 #endif  
2493          rdpdr_send_completion(data->device, data->id, 0, buffer_len, data->out->data, buffer_len);          rdpdr_send_completion(data->device, data->id, 0, buffer_len, data->out->data, buffer_len);
 #if 0  
         pthread_mutex_unlock(&sendControl);  
 #endif  
2494          SC_destroyThreadData(data);          SC_destroyThreadData(data);
2495  }  }
2496    
# Line 2595  SC_deviceControl(PSCThreadData data) Line 2498  SC_deviceControl(PSCThreadData data)
2498  static void *  static void *
2499  thread_function(PThreadListElement listElement)  thread_function(PThreadListElement listElement)
2500  {  {
2501  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
2502          long sTime = 0;          long sTime = 0;
2503  #endif  #endif
2504          if ((listElement != NULL) && (listElement->data != NULL))          if ((listElement != NULL) && (listElement->data != NULL))
2505          {          {
2506                  while (1)                  while (1)
2507                  {                  {
2508  #ifdef WITH_SCARD_DEBUG  #ifdef WITH_DEBUG_SCARD
2509                          sTime = time(NULL);                          sTime = time(NULL);
2510  #endif  #endif
2511                          pthread_mutex_lock(&listElement->nodata);                          pthread_mutex_lock(&listElement->nodata);
2512                          SC_deviceControl(listElement->data);                          SC_deviceControl(listElement->data);
2513                          listElement->data = NULL;                          listElement->data = NULL;
2514  #ifdef WITH_SCARD_DEBUG                          DEBUG_SCARD(("[HANDLING TIME %d]\n", (int) (time(NULL) - sTime)));
                         printf("[HANDLING TIME %d]\n", (int) (time(NULL) - sTime));  
 #endif  
2515                          pthread_mutex_unlock(&listElement->busy);                          pthread_mutex_unlock(&listElement->busy);
2516                  }                  }
2517          }          }
# Line 2624  SC_handleRequest(PSCThreadData data) Line 2525  SC_handleRequest(PSCThreadData data)
2525          int Result = 0;          int Result = 0;
2526          PThreadListElement cur = threadList, last = threadList;          PThreadListElement cur = threadList, last = threadList;
2527    
2528  #ifdef WITH_SCARD_DEBUG          DEBUG_SCARD(("[THREAD COUNT %d]\n", threadCount));
         printf("[THREAD COUNT %d]\n", threadCount);  
 #endif  
2529    
2530          while (cur)          while (cur)
2531          {          {
# Line 2659  SC_handleRequest(PSCThreadData data) Line 2558  SC_handleRequest(PSCThreadData data)
2558          Result = pthread_create(&cur->thread, NULL, (void *(*)(void *)) thread_function, cur);          Result = pthread_create(&cur->thread, NULL, (void *(*)(void *)) thread_function, cur);
2559          if (0 != Result)          if (0 != Result)
2560          {          {
2561                  printf("[THREAD CREATE ERROR 0x%.8x]\n", Result);                  error("[THREAD CREATE ERROR 0x%.8x]\n", Result);
2562                  SC_xfree(&threadListHandle, cur);                  SC_xfree(&threadListHandle, cur);
2563                  SC_destroyThreadData(data);                  SC_destroyThreadData(data);
2564                  data = NULL;                  data = NULL;
# Line 2720  DEVICE_FNS scard_fns = { Line 2619  DEVICE_FNS scard_fns = {
2619  #endif /* MAKE_PROTO */  #endif /* MAKE_PROTO */
2620    
2621  void  void
2622  scard_tcp_lock(void)  scard_lock(int lock)
2623  {  {
2624          if (!tcp_sendcontrol_mutex)          if (!scard_mutex)
2625            {
2626                    scard_mutex =
2627                            (pthread_mutex_t **) xmalloc(sizeof(pthread_mutex_t *) * SCARD_LOCK_LAST);
2628            }
2629    
2630            if (!scard_mutex[lock])
2631          {          {
2632                  tcp_sendcontrol_mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));                  scard_mutex[lock] = (pthread_mutex_t *) xmalloc(sizeof(pthread_mutex_t));
2633                  pthread_mutex_init(tcp_sendcontrol_mutex, NULL);                  pthread_mutex_init(scard_mutex[lock], NULL);
2634          }          }
2635    
2636          pthread_mutex_lock(tcp_sendcontrol_mutex);          pthread_mutex_lock(scard_mutex[lock]);
2637  }  }
2638    
2639  void  void
2640  scard_tcp_unlock(void)  scard_unlock(int lock)
2641  {  {
2642          pthread_mutex_unlock(tcp_sendcontrol_mutex);          pthread_mutex_unlock(scard_mutex[lock]);
2643  }  }
2644    
2645  STREAM  STREAM

Legend:
Removed from v.1312  
changed lines
  Added in v.1328

  ViewVC Help
Powered by ViewVC 1.1.26