Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6837 lines
201 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. w32drive
  5. Abstract:
  6. This module defines a child of the client-side RDP
  7. device redirection, the "w32scard" W32SCard to provide
  8. SmartCard sub-system redirection on 32bit windows
  9. Author:
  10. reidk
  11. Revision History:
  12. --*/
  13. #include <precom.h>
  14. #define TRC_GROUP TRC_GROUP_NETWORK
  15. #define TRC_FILE "W32SCard"
  16. #include <atrcapi.h>
  17. #include <w32scard.h>
  18. #include <delayimp.h>
  19. #include "proc.h"
  20. #include "drconfig.h"
  21. #include "w32utl.h"
  22. #include "utl.h"
  23. #include "drfsfile.h"
  24. #include "scredir.h"
  25. #include "scioctl.h"
  26. #include "winsmcrd.h"
  27. #ifdef OS_WINCE
  28. #include <wcescard.h>
  29. #define LoadLibraryA(x) LoadLibrary(L##x)
  30. #define pfnSCardFreeMemory(hcontext, pv)
  31. #endif
  32. #define _TRY_status(x) \
  33. __try \
  34. { \
  35. x; \
  36. } \
  37. __except(EXCEPTION_EXECUTE_HANDLER) \
  38. { \
  39. status = STATUS_UNSUCCESSFUL; \
  40. goto ErrorReturn; \
  41. }
  42. #define _TRY_lReturn(x) \
  43. __try \
  44. { \
  45. x; \
  46. } \
  47. __except(EXCEPTION_EXECUTE_HANDLER) \
  48. { \
  49. lReturn = SCARD_E_UNEXPECTED; \
  50. goto ErrorReturn; \
  51. }
  52. #define _TRY_2(x) __try \
  53. { \
  54. x; \
  55. } \
  56. __except(EXCEPTION_EXECUTE_HANDLER){} // do nothing
  57. #define SCARD_CONTEXT_LIST_ALLOC_SIZE 6
  58. #define SCARD_THREAD_LIST_ALLOC_SIZE 6
  59. #define SCARD_IOREQUEST_LIST_ALLOC_SIZE 6
  60. #define ATR_COPY_SIZE 36
  61. void
  62. SafeMesHandleFree(handle_t *ph)
  63. {
  64. if (*ph != 0)
  65. {
  66. MesHandleFree(*ph);
  67. *ph = 0;
  68. }
  69. }
  70. //---------------------------------------------------------------------------------------
  71. //
  72. // MIDL allocation routines
  73. //
  74. //---------------------------------------------------------------------------------------
  75. void __RPC_FAR *__RPC_USER MIDL_user_allocate(size_t size)
  76. {
  77. return (HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size));
  78. }
  79. void __RPC_USER MIDL_user_free(void __RPC_FAR *pv)
  80. {
  81. if (pv != NULL)
  82. {
  83. HeapFree(GetProcessHeap(), 0, pv);
  84. }
  85. }
  86. ///////////////////////////////////////////////////////////////
  87. //
  88. // W32SCard Methods
  89. //
  90. //
  91. W32SCard::W32SCard(
  92. ProcObj *processObject,
  93. ULONG deviceID,
  94. const TCHAR *deviceName,
  95. const TCHAR *devicePath) : W32DrDeviceAsync(processObject, deviceID, devicePath)
  96. /*++
  97. Routine Description:
  98. Constructor
  99. Arguments:
  100. processObject - Associated process object.
  101. deviceName - Name of the drive.
  102. id - Device ID for the drive.
  103. devicePath - Path that can be opened by CreateFile
  104. for drive.
  105. Return Value:
  106. NA
  107. --*/
  108. {
  109. unsigned len;
  110. DWORD i;
  111. HRESULT hr;
  112. DC_BEGIN_FN("W32SCard::W32SCard");
  113. _deviceName = NULL;
  114. _pFileObj = NULL;
  115. _rgSCardContextList = NULL;
  116. _dwSCardContextListSize = 0;
  117. _rghThreadList = NULL;
  118. _dwThreadListSize = 0;
  119. _fInDestructor = FALSE;
  120. _fFlushing = FALSE;
  121. _hModWinscard = NULL;
  122. _fCritSecsInitialized = FALSE;
  123. _rgIORequestList = NULL;
  124. _dwIORequestListSize = 0;
  125. _fNewFailed = FALSE;
  126. #ifndef OS_WINCE
  127. _hModKernel32 = NULL;
  128. _hStartedEvent = NULL;
  129. _hRegisterWaitForStartedEvent = NULL;
  130. _fCloseStartedEvent = FALSE;
  131. _fUseRegisterWaitFuncs = FALSE;
  132. #endif
  133. SetDeviceProperty();
  134. //
  135. // Initialize the critical sections.
  136. //
  137. __try
  138. {
  139. InitializeCriticalSection(&_csContextList);
  140. }
  141. __except(EXCEPTION_EXECUTE_HANDLER)
  142. {
  143. TRC_ERR((
  144. TB,
  145. _T("InitializeCriticalSection() failed - Exception Code: %lx"),
  146. GetExceptionCode()));
  147. goto InvalidObject;
  148. }
  149. __try
  150. {
  151. InitializeCriticalSection(&_csThreadList);
  152. }
  153. __except(EXCEPTION_EXECUTE_HANDLER)
  154. {
  155. TRC_ERR((
  156. TB,
  157. _T("InitializeCriticalSection() failed - Exception Code: %lx"),
  158. GetExceptionCode()));
  159. DeleteCriticalSection(&_csContextList);
  160. goto InvalidObject;
  161. }
  162. __try
  163. {
  164. InitializeCriticalSection(&_csWaitForStartedEvent);
  165. }
  166. __except(EXCEPTION_EXECUTE_HANDLER)
  167. {
  168. TRC_ERR((
  169. TB,
  170. _T("InitializeCriticalSection() failed - Exception Code: %lx"),
  171. GetExceptionCode()));
  172. DeleteCriticalSection(&_csContextList);
  173. DeleteCriticalSection(&_csThreadList);
  174. goto InvalidObject;
  175. }
  176. _fCritSecsInitialized = TRUE;
  177. #ifdef OS_WINCE
  178. if ((gpCESCard = new CESCard()) == NULL)
  179. {
  180. TRC_ERR((TB, _T("Failed to create synchronization object") ));
  181. goto InvalidObject;
  182. }
  183. #endif
  184. //
  185. // Load the SCard* function pointers
  186. //
  187. if (!BindToSCardFunctions())
  188. {
  189. goto InvalidObject;
  190. }
  191. //
  192. // Record the drive name.
  193. //
  194. TRC_ASSERT((deviceName != NULL), (TB, _T("deviceName is NULL")));
  195. len = (STRLEN(deviceName) + 1);
  196. _deviceName = new TCHAR[len];
  197. if (_deviceName != NULL)
  198. {
  199. hr = StringCchCopy(_deviceName, len, deviceName);
  200. TRC_ASSERT(SUCCEEDED(hr),(TB,_T("Pre checked copy failed: 0x%x"), hr));
  201. }
  202. else
  203. {
  204. goto InvalidObject;
  205. }
  206. //
  207. // Initial allocation for context, thread, and IORequest lists
  208. //
  209. _rgSCardContextList = new SCARDCONTEXT[SCARD_CONTEXT_LIST_ALLOC_SIZE];
  210. _rghThreadList = new HANDLE[SCARD_THREAD_LIST_ALLOC_SIZE];
  211. _rgIORequestList = new PRDPDR_IOREQUEST_PACKET[SCARD_IOREQUEST_LIST_ALLOC_SIZE];
  212. if ((_rgSCardContextList == NULL) ||
  213. (_rghThreadList == NULL) ||
  214. (_rgIORequestList == NULL))
  215. {
  216. goto InvalidObject;
  217. }
  218. else
  219. {
  220. _dwSCardContextListSize = SCARD_CONTEXT_LIST_ALLOC_SIZE;
  221. _dwThreadListSize = SCARD_THREAD_LIST_ALLOC_SIZE;
  222. _dwIORequestListSize = SCARD_IOREQUEST_LIST_ALLOC_SIZE;
  223. memset(
  224. _rgSCardContextList,
  225. 0,
  226. sizeof(SCARDCONTEXT) * SCARD_CONTEXT_LIST_ALLOC_SIZE);
  227. for (i=0; i<SCARD_THREAD_LIST_ALLOC_SIZE; i++)
  228. {
  229. _rghThreadList[i] = NULL;
  230. }
  231. for (i=0; i<SCARD_IOREQUEST_LIST_ALLOC_SIZE; i++)
  232. {
  233. _rgIORequestList[i] = NULL;
  234. }
  235. }
  236. Return:
  237. DC_END_FN();
  238. return;
  239. InvalidObject:
  240. _fNewFailed = TRUE;
  241. SetValid(FALSE);
  242. goto Return;
  243. }
  244. W32SCard::~W32SCard()
  245. /*++
  246. Routine Description:
  247. Destructor
  248. Arguments:
  249. NA
  250. Return Value:
  251. NA
  252. --*/
  253. {
  254. DC_BEGIN_FN("W32SCard::~W32SCard");
  255. _fInDestructor = TRUE;
  256. #ifndef OS_WINCE
  257. PVOID pv;
  258. pv = InterlockedExchangePointer(&_hRegisterWaitForStartedEvent, NULL);
  259. if (pv != NULL)
  260. {
  261. pfnUnregisterWaitEx(pv, INVALID_HANDLE_VALUE);
  262. }
  263. #endif
  264. if (IsValid())
  265. {
  266. FlushIRPs();
  267. }
  268. if (_deviceName != NULL)
  269. {
  270. delete[]_deviceName;
  271. }
  272. if (_pFileObj != NULL)
  273. {
  274. _pFileObj->Release();
  275. }
  276. if (_rgSCardContextList != NULL)
  277. {
  278. delete[]_rgSCardContextList;
  279. }
  280. if (_rghThreadList != NULL)
  281. {
  282. delete[]_rghThreadList;
  283. }
  284. if (_rgIORequestList != NULL)
  285. {
  286. delete[]_rgIORequestList;
  287. }
  288. if (_hModWinscard != NULL)
  289. {
  290. FreeLibrary(_hModWinscard);
  291. }
  292. #ifndef OS_WINCE
  293. if (_hModKernel32 != NULL)
  294. {
  295. FreeLibrary(_hModKernel32);
  296. }
  297. if (_hStartedEvent != NULL)
  298. {
  299. ReleaseStartedEvent();;
  300. }
  301. #endif
  302. if (_fCritSecsInitialized)
  303. {
  304. DeleteCriticalSection(&_csContextList);
  305. DeleteCriticalSection(&_csThreadList);
  306. DeleteCriticalSection(&_csWaitForStartedEvent);
  307. }
  308. #ifdef OS_WINCE
  309. delete gpCESCard;
  310. #endif
  311. DC_END_FN();
  312. }
  313. #ifndef OS_WINCE
  314. extern LPCSTR g_szTscControlName;
  315. #else
  316. extern LPCWSTR g_szTscControlName;
  317. #endif
  318. HMODULE
  319. W32SCard::AddRefCurrentModule()
  320. {
  321. #ifndef OS_WINCE
  322. return (LoadLibraryA(g_szTscControlName));
  323. #else
  324. return LoadLibrary(g_szTscControlName);
  325. #endif
  326. }
  327. VOID
  328. W32SCard::FlushIRPs()
  329. {
  330. DC_BEGIN_FN("W32SCard::FlushIRPs");
  331. DWORD i, j;
  332. DWORD dwNumThreads = 0;
  333. HANDLE *rgHandles = NULL;
  334. _fFlushing = TRUE;
  335. EnterCriticalSection(&_csContextList);
  336. EnterCriticalSection(&_csThreadList);
  337. //
  338. // Clean up any oustanding threads that are blocked
  339. //
  340. if ((_rgSCardContextList != NULL) && (_rghThreadList != NULL))
  341. {
  342. //
  343. // Count number of blocked threads
  344. //
  345. dwNumThreads = 0;
  346. for (i=0; i<_dwThreadListSize; i++)
  347. {
  348. if (_rghThreadList[i] != NULL)
  349. {
  350. dwNumThreads++;
  351. }
  352. }
  353. //
  354. // Build an array of thread handles to wait for
  355. //
  356. #ifdef OS_WINCE
  357. if (dwNumThreads > 0)
  358. #endif
  359. rgHandles = new HANDLE[dwNumThreads];
  360. if (rgHandles != NULL)
  361. {
  362. dwNumThreads = 0;
  363. for (i=0; i<_dwThreadListSize; i++)
  364. {
  365. if (_rghThreadList[i] != NULL)
  366. {
  367. #ifndef OS_WINCE
  368. if (!DuplicateHandle(
  369. GetCurrentProcess(),
  370. _rghThreadList[i],
  371. GetCurrentProcess(),
  372. &(rgHandles[dwNumThreads]),
  373. 0,
  374. FALSE,
  375. DUPLICATE_SAME_ACCESS))
  376. {
  377. TRC_ERR((TB, _T("DuplicateHandle failed.")));
  378. //
  379. // Nothing we can do... just clean up the already
  380. // duplicated handles
  381. //
  382. for (j=0; j<dwNumThreads; j++)
  383. {
  384. CloseHandle(rgHandles[j]);
  385. }
  386. //
  387. // Setting dwNumThread to 0 will cause no wait
  388. //
  389. dwNumThreads = 0;
  390. break;
  391. }
  392. #else //CE does not support DuplicateHandle for threads
  393. rgHandles[dwNumThreads] = _rghThreadList[i];
  394. #endif
  395. dwNumThreads++;
  396. }
  397. }
  398. }
  399. else
  400. {
  401. TRC_ERR((TB, _T("new failed.")));
  402. }
  403. //
  404. // Cancel any outstanding calls
  405. //
  406. for (i=0; i<_dwSCardContextListSize; i++)
  407. {
  408. if (_rgSCardContextList[i] != NULL)
  409. {
  410. pfnSCardCancel(_rgSCardContextList[i]);
  411. }
  412. }
  413. }
  414. LeaveCriticalSection(&_csContextList);
  415. LeaveCriticalSection(&_csThreadList);
  416. //
  417. // Do the wait
  418. //
  419. if (dwNumThreads > 0)
  420. {
  421. #ifndef OS_WINCE
  422. if (WaitForMultipleObjects(
  423. dwNumThreads,
  424. rgHandles,
  425. TRUE,
  426. INFINITE) == WAIT_TIMEOUT)
  427. {
  428. TRC_ERR((TB, _T("WaitForMultipleObjects timed out")));
  429. }
  430. #else
  431. //CE does not support waiting for all at once
  432. for (j=0; j<dwNumThreads; j++)
  433. {
  434. DWORD dwWait = WaitForSingleObject(rgHandles[j], 30 * 1000);
  435. if (dwWait != WAIT_OBJECT_0)
  436. {
  437. TRC_ERR((TB, _T("WaitForSingleObject 0x%08x returned 0x%08x. GLE=%d(0x%08x)"), rgHandles[j], dwWait, GetLastError(), GetLastError()));
  438. }
  439. }
  440. #endif
  441. }
  442. //
  443. // Close the duplicate handles
  444. //
  445. for (i=0; i<dwNumThreads; i++)
  446. {
  447. CloseHandle(rgHandles[i]);
  448. }
  449. #ifdef OS_WINCE
  450. gpCESCard->FlushIRPs();
  451. #endif
  452. if (rgHandles != NULL)
  453. {
  454. delete[]rgHandles;
  455. }
  456. _fFlushing = FALSE;
  457. DC_END_FN();
  458. }
  459. DWORD
  460. W32SCard::Enumerate(
  461. IN ProcObj *procObj,
  462. IN DrDeviceMgr *deviceMgr
  463. )
  464. /*++
  465. Routine Description:
  466. Enumerate devices of this type by adding appropriate device
  467. instances to the device manager.
  468. Arguments:
  469. procObj - Corresponding process object.
  470. deviceMgr - Device manager to add devices to.
  471. Return Value:
  472. ERROR_SUCCESS on success. Otherwise, an error code is returned.
  473. --*/
  474. {
  475. W32SCard *pScardDeviceObj = NULL;
  476. RDPDR_VERSION serverVer;
  477. DrFile *pFileObj = NULL;
  478. OSVERSIONINFOA osVersion;
  479. DC_BEGIN_FN("W32SCard::Enumerate");
  480. //
  481. // Make sure we are on an OS that we support
  482. //
  483. memset(&osVersion, 0x00, sizeof(osVersion));
  484. osVersion.dwOSVersionInfoSize = sizeof(osVersion);
  485. if (!GetVersionExA(&osVersion))
  486. {
  487. TRC_ERR((TB, _T("GetVersionEx() failed")));
  488. return ERROR_SUCCESS; // don't blcok anything else from running
  489. }
  490. if (osVersion.dwMajorVersion < 5)
  491. {
  492. TRC_DBG((TB,_T("SmartCard redirection does not work on platforms below Win2k, bailing out")));
  493. return ERROR_SUCCESS;
  494. }
  495. if(!procObj->GetVCMgr().GetInitData()->fEnableSCardRedirection)
  496. {
  497. TRC_DBG((TB,_T("SmartCard redirection disabled, bailing out")));
  498. return ERROR_SUCCESS;
  499. }
  500. serverVer = procObj->serverVersion();
  501. //
  502. // If the server doesn't support scard device redirection,
  503. // then don't bother enumerate the scard device, simply
  504. // return success
  505. //
  506. if (COMPARE_VERSION(serverVer.Minor, serverVer.Major,
  507. RDPDR_MINOR_VERSION, RDPDR_MAJOR_VERSION) < 0)
  508. {
  509. TRC_NRM((TB, _T("Skipping scard device enumeration")));
  510. return ERROR_SUCCESS;
  511. }
  512. //
  513. // Create the scard device object
  514. //
  515. pScardDeviceObj = new W32SCard(
  516. procObj,
  517. deviceMgr->GetUniqueObjectID(),
  518. SZ_SCARD_DEVICE_NAME,
  519. SZ_SCARD_DEVICE_NAME);
  520. if (pScardDeviceObj == NULL)
  521. {
  522. TRC_ERR((TB, _T("new W32SCard() failed")));
  523. }
  524. else
  525. {
  526. if (!(pScardDeviceObj->_fNewFailed))
  527. {
  528. pScardDeviceObj->Initialize();
  529. }
  530. if (!pScardDeviceObj->IsValid())
  531. {
  532. TRC_ERR((TB, _T("new W32SCard object is not valid")));
  533. delete (pScardDeviceObj);
  534. pScardDeviceObj = NULL;
  535. }
  536. else
  537. {
  538. //
  539. // Create the single file object to be used for the Scard device object
  540. //
  541. // BTW, we can use INVALID_HANDLE_VALUE as the actual file handle since
  542. // we don't really use the DrFile object except for the FileId
  543. //
  544. pScardDeviceObj->_pFileObj = new DrFile(
  545. pScardDeviceObj,
  546. DR_SMARTCARD_FILEID,
  547. INVALID_HANDLE_VALUE);
  548. if (pScardDeviceObj->_pFileObj == NULL)
  549. {
  550. TRC_ERR((TB, _T("new DrFile() failed")));
  551. delete (pScardDeviceObj);
  552. pScardDeviceObj = NULL;
  553. }
  554. else
  555. {
  556. pScardDeviceObj->_pFileObj->AddRef();
  557. //
  558. // Add the Scard device to the device manager
  559. //
  560. if (deviceMgr->AddObject(pScardDeviceObj) != STATUS_SUCCESS)
  561. {
  562. TRC_ERR((TB, _T("deviceMgr->AddObject() failed")));
  563. delete (pScardDeviceObj);
  564. pScardDeviceObj = NULL;
  565. }
  566. }
  567. }
  568. }
  569. DC_END_FN();
  570. return ERROR_SUCCESS;
  571. }
  572. ULONG
  573. W32SCard::GetDevAnnounceDataSize()
  574. /*++
  575. Routine Description:
  576. Return the size (in bytes) of a device announce packet for
  577. this device.
  578. Arguments:
  579. NA
  580. Return Value:
  581. The size (in bytes) of a device announce packet for this device.
  582. --*/
  583. {
  584. ULONG size = 0;
  585. DC_BEGIN_FN("W32SCard::GetDevAnnounceDataSize");
  586. TRC_ASSERT((IsValid()), (TB, _T("Invalid W32SCard object")));
  587. if (!IsValid()) { return 0; }
  588. size = 0;
  589. //
  590. // Add the base announce size.
  591. //
  592. size += sizeof(RDPDR_DEVICE_ANNOUNCE);
  593. DC_END_FN();
  594. return size;
  595. }
  596. VOID W32SCard::GetDevAnnounceData(
  597. IN PRDPDR_DEVICE_ANNOUNCE pDeviceAnnounce
  598. )
  599. /*++
  600. Routine Description:
  601. Add a device announce packet for this device to the input buffer.
  602. Arguments:
  603. pDeviceAnnounce - Device Announce Buf for this Device
  604. Return Value:
  605. NA
  606. --*/
  607. {
  608. DC_BEGIN_FN("W32SCard::GetDevAnnounceData");
  609. TRC_ASSERT((IsValid()), (TB, _T("Invalid W32SCcard object")));
  610. if (!IsValid()) {
  611. DC_END_FN();
  612. return;
  613. }
  614. pDeviceAnnounce->DeviceId = GetID();
  615. pDeviceAnnounce->DeviceType = GetDeviceType();
  616. pDeviceAnnounce->DeviceDataLength = 0;
  617. //
  618. // Record the device name in ANSI.
  619. //
  620. #ifdef UNICODE
  621. RDPConvertToAnsi(_deviceName, (LPSTR) pDeviceAnnounce->PreferredDosName,
  622. sizeof(pDeviceAnnounce->PreferredDosName)
  623. );
  624. #else
  625. STRNCPY((char *)pDeviceAnnounce->PreferredDosName, _deviceName, PREFERRED_DOS_NAME_SIZE);
  626. pDeviceAnnounce->PreferredDosName[PREFERRED_DOS_NAME_SIZE - 1] = '\0';
  627. #endif
  628. DC_END_FN();
  629. }
  630. //---------------------------------------------------------------------------------------
  631. //
  632. // These methods implement a list that is used for tracking all open SCardContexts,
  633. // so that if we get disconnected with an open SCardContext that has blocked calls,
  634. // we can call SCardCancel to get the threads back before the W32SCard object is
  635. // fully deleted
  636. //
  637. //---------------------------------------------------------------------------------------
  638. BOOL
  639. W32SCard::AddSCardContextToList(
  640. SCARDCONTEXT SCardContext)
  641. {
  642. DC_BEGIN_FN("W32SCard::AddSCardContextToList");
  643. DWORD i = 0;
  644. DWORD dwOpenSlot = 0xffffffff;
  645. SCARDCONTEXT *pTemp = NULL;
  646. BOOL fRet = TRUE;
  647. EnterCriticalSection(&_csContextList);
  648. //
  649. // See if there is already an entry for this context,
  650. // and keep track of the LAST open slot in case there isn't
  651. //
  652. for (i=0; i<_dwSCardContextListSize; i++)
  653. {
  654. if (_rgSCardContextList[i] == SCardContext)
  655. {
  656. //
  657. // already exists
  658. //
  659. goto Return;
  660. }
  661. else if (_rgSCardContextList[i] == NULL)
  662. {
  663. dwOpenSlot = i;
  664. }
  665. }
  666. //
  667. // check to see if an open slot was found
  668. //
  669. if (dwOpenSlot != 0xffffffff)
  670. {
  671. //
  672. // found
  673. //
  674. _rgSCardContextList[dwOpenSlot] = SCardContext;
  675. }
  676. else
  677. {
  678. //
  679. // need to allocate more space
  680. //
  681. pTemp = new SCARDCONTEXT[ _dwSCardContextListSize +
  682. SCARD_CONTEXT_LIST_ALLOC_SIZE];
  683. if (pTemp == NULL)
  684. {
  685. TRC_ERR((TB, _T("new failed")));
  686. fRet = FALSE;
  687. LeaveCriticalSection(&_csContextList);
  688. goto Return;
  689. }
  690. memset(
  691. pTemp,
  692. 0,
  693. sizeof(SCARDCONTEXT) *
  694. (_dwSCardContextListSize + SCARD_CONTEXT_LIST_ALLOC_SIZE));
  695. //
  696. // populate newly allocated array with all current entries
  697. //
  698. for (i=0; i<_dwSCardContextListSize; i++)
  699. {
  700. pTemp[i] = _rgSCardContextList[i];
  701. }
  702. //
  703. // add the new entry
  704. //
  705. pTemp[_dwSCardContextListSize] = SCardContext;
  706. //
  707. // free old list
  708. //
  709. delete[]_rgSCardContextList;
  710. //
  711. // reset member pointer and size
  712. //
  713. _rgSCardContextList = pTemp;
  714. _dwSCardContextListSize += SCARD_CONTEXT_LIST_ALLOC_SIZE;
  715. }
  716. LeaveCriticalSection(&_csContextList);
  717. Return:
  718. DC_END_FN();
  719. return (fRet);
  720. }
  721. void
  722. W32SCard::RemoveSCardContextFromList(
  723. SCARDCONTEXT SCardContext)
  724. {
  725. DC_BEGIN_FN("W32SCard::RemoveSCardContextFromList");
  726. DWORD i = 0;
  727. EnterCriticalSection(&_csContextList);
  728. while (i < _dwSCardContextListSize)
  729. {
  730. if (_rgSCardContextList[i] == SCardContext)
  731. {
  732. pfnSCardCancel(_rgSCardContextList[i]);
  733. _rgSCardContextList[i] = NULL;
  734. break;
  735. }
  736. i++;
  737. }
  738. LeaveCriticalSection(&_csContextList);
  739. Return:
  740. DC_END_FN();
  741. }
  742. //---------------------------------------------------------------------------------------
  743. //
  744. // These methods implement a list that is used for tracking all threads that are
  745. // currently operating on a SCard* call. The list is used during the W32SCard object
  746. // destructor to wait on all the threads to return before allowing the object to be
  747. // completely deleted.
  748. //
  749. //---------------------------------------------------------------------------------------
  750. BOOL
  751. W32SCard::AddThreadToList(HANDLE hThread)
  752. {
  753. DC_BEGIN_FN("W32SCard::AddThreadToList");
  754. DWORD i = 0;
  755. HANDLE *rghTemp = NULL;
  756. BOOL fRet = TRUE;
  757. DWORD dwNewSize = 0;
  758. EnterCriticalSection(&_csThreadList);
  759. //
  760. // Search for an open slot
  761. //
  762. while (i < _dwThreadListSize)
  763. {
  764. if (_rghThreadList[i] == NULL)
  765. {
  766. //
  767. // open slot found
  768. //
  769. break;
  770. }
  771. i++;
  772. }
  773. //
  774. // check to see if an open slot was found
  775. //
  776. if (i < _dwThreadListSize)
  777. {
  778. //
  779. // found
  780. //
  781. _rghThreadList[i] = hThread;
  782. }
  783. else
  784. {
  785. //
  786. // need to allocate more space
  787. //
  788. dwNewSize = _dwThreadListSize + SCARD_THREAD_LIST_ALLOC_SIZE;
  789. rghTemp = new HANDLE[dwNewSize];
  790. if (rghTemp == NULL)
  791. {
  792. TRC_ERR((TB, _T("new failed")));
  793. fRet = FALSE;
  794. LeaveCriticalSection(&_csThreadList);
  795. goto Return;
  796. }
  797. //
  798. // populate newly allocated array with all current thread handles
  799. //
  800. for (i=0; i<_dwThreadListSize; i++)
  801. {
  802. rghTemp[i] = _rghThreadList[i];
  803. }
  804. //
  805. // Initialize new entries
  806. //
  807. for (i=_dwThreadListSize; i<dwNewSize; i++)
  808. {
  809. rghTemp[i] = NULL;
  810. }
  811. //
  812. // add the new entry
  813. //
  814. rghTemp[_dwThreadListSize] = hThread;
  815. //
  816. // free old list
  817. //
  818. delete[]_rghThreadList;
  819. //
  820. // reset member pointer and size
  821. //
  822. _rghThreadList = rghTemp;
  823. _dwThreadListSize += SCARD_THREAD_LIST_ALLOC_SIZE;
  824. }
  825. LeaveCriticalSection(&_csThreadList);
  826. Return:
  827. DC_END_FN();
  828. return (fRet);
  829. }
  830. void
  831. W32SCard::RemoveThreadFromList(HANDLE hThread)
  832. {
  833. DC_BEGIN_FN("W32SCard::RemoveThreadFromList");
  834. DWORD i = 0;
  835. EnterCriticalSection(&_csThreadList);
  836. while (i < _dwThreadListSize)
  837. {
  838. if (_rghThreadList[i] == hThread)
  839. {
  840. CloseHandle(_rghThreadList[i]);
  841. _rghThreadList[i] = NULL;
  842. break;
  843. }
  844. i++;
  845. }
  846. LeaveCriticalSection(&_csThreadList);
  847. Return:
  848. DC_END_FN();
  849. }
  850. //---------------------------------------------------------------------------------------
  851. //
  852. // This method implements a list that is used for tracking IORequests waiting for the
  853. // started event
  854. //
  855. //---------------------------------------------------------------------------------------
  856. BOOL
  857. W32SCard::AddIORequestToList(
  858. PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  859. {
  860. DC_BEGIN_FN("W32SCard::AddIORequestToList");
  861. DWORD i = 0;
  862. PRDPDR_IOREQUEST_PACKET *rghTemp = NULL;
  863. BOOL fRet = TRUE;
  864. DWORD dwNewSize = 0;
  865. //
  866. // Only called in one place, which is wrapped in a CritSec, so no need for one here
  867. //
  868. //
  869. // Search for an open slot
  870. //
  871. while (i < _dwIORequestListSize)
  872. {
  873. if (_rgIORequestList[i] == NULL)
  874. {
  875. //
  876. // open slot found
  877. //
  878. break;
  879. }
  880. i++;
  881. }
  882. //
  883. // check to see if an open slot was found
  884. //
  885. if (i < _dwIORequestListSize)
  886. {
  887. //
  888. // found
  889. //
  890. _rgIORequestList[i] = pIoRequestPacket;
  891. }
  892. else
  893. {
  894. //
  895. // need to allocate more space
  896. //
  897. dwNewSize = _dwIORequestListSize + SCARD_IOREQUEST_LIST_ALLOC_SIZE;
  898. rghTemp = new PRDPDR_IOREQUEST_PACKET[dwNewSize];
  899. if (rghTemp == NULL)
  900. {
  901. TRC_ERR((TB, _T("new failed")));
  902. fRet = FALSE;
  903. goto Return;
  904. }
  905. //
  906. // populate newly allocated array with all current IoRequests
  907. //
  908. for (i=0; i<_dwIORequestListSize; i++)
  909. {
  910. rghTemp[i] = _rgIORequestList[i];
  911. }
  912. //
  913. // Initialize new entries
  914. //
  915. for (i=_dwIORequestListSize; i<dwNewSize; i++)
  916. {
  917. rghTemp[i] = NULL;
  918. }
  919. //
  920. // add the new entry
  921. //
  922. rghTemp[_dwIORequestListSize] = pIoRequestPacket;
  923. //
  924. // free old list
  925. //
  926. delete[]_rgIORequestList;
  927. //
  928. // reset member pointer and size
  929. //
  930. _rgIORequestList = rghTemp;
  931. _dwIORequestListSize += SCARD_IOREQUEST_LIST_ALLOC_SIZE;
  932. }
  933. Return:
  934. DC_END_FN();
  935. return (fRet);
  936. }
  937. DWORD WINAPI
  938. W32SCard::SCardHandleCall_ThreadProc(
  939. LPVOID lpParameter)
  940. {
  941. DC_BEGIN_FN("W32SCard::SCardHandleCall_ThreadProc");
  942. LONG lReturn = SCARD_S_SUCCESS;
  943. SCARDHANDLECALLSTRUCT *pStruct = (SCARDHANDLECALLSTRUCT *) lpParameter;
  944. W32SCard *pTHIS = pStruct->pTHIS;
  945. HANDLE hThread = pStruct->hThread;
  946. HMODULE hModExtraRefCount = pStruct->hModExtraRefCount;
  947. pStruct = (SCARDHANDLECALLSTRUCT *) lpParameter;
  948. //
  949. // pStruct->hThread will be NULL if it wasn't added to the thread list...
  950. // it wasn't added to the thread list then just get out.
  951. //
  952. if (pStruct->hThread == NULL)
  953. {
  954. FreeLibraryAndExitThread(pStruct->hModExtraRefCount, 0);
  955. }
  956. switch (pStruct->dwCallType)
  957. {
  958. case SCARD_IOCTL_CONNECTA:
  959. TRC_DBG((TB, _T("SCARD_IOCTL_CONNECTA")));
  960. pTHIS->Connect(
  961. pStruct,
  962. SCARD_IOCTL_CONNECTA);
  963. break;
  964. case SCARD_IOCTL_CONNECTW:
  965. TRC_DBG((TB, _T("SCARD_IOCTL_CONNECTW")));
  966. pTHIS->Connect(
  967. pStruct,
  968. SCARD_IOCTL_CONNECTW);
  969. break;
  970. case SCARD_IOCTL_RECONNECT:
  971. TRC_DBG((TB, _T("SCARD_IOCTL_RECONNECT")));
  972. pTHIS->Reconnect(
  973. pStruct);
  974. break;
  975. case SCARD_IOCTL_DISCONNECT:
  976. TRC_DBG((TB, _T("SCARD_IOCTL_DISCONNECT")));
  977. pTHIS->HandleHCardAndDispositionCall(
  978. pStruct,
  979. SCARD_IOCTL_DISCONNECT);
  980. break;
  981. case SCARD_IOCTL_BEGINTRANSACTION:
  982. TRC_DBG((TB, _T("SCARD_IOCTL_BEGINTRANSACTION")));
  983. pTHIS->HandleHCardAndDispositionCall(
  984. pStruct,
  985. SCARD_IOCTL_BEGINTRANSACTION);
  986. break;
  987. case SCARD_IOCTL_ENDTRANSACTION:
  988. TRC_DBG((TB, _T("SCARD_IOCTL_ENDTRANSACTION")));
  989. pTHIS->HandleHCardAndDispositionCall(
  990. pStruct,
  991. SCARD_IOCTL_ENDTRANSACTION);
  992. break;
  993. #ifndef OS_WINCE
  994. case SCARD_IOCTL_STATE:
  995. TRC_DBG((TB, _T("SCARD_IOCTL_STATE")));
  996. pTHIS->State(
  997. pStruct);
  998. break;
  999. #endif
  1000. case SCARD_IOCTL_STATUSA:
  1001. TRC_DBG((TB, _T("SCARD_IOCTL_STATUSA")));
  1002. pTHIS->Status(
  1003. pStruct,
  1004. SCARD_IOCTL_STATUSA);
  1005. break;
  1006. case SCARD_IOCTL_STATUSW:
  1007. TRC_DBG((TB, _T("SCARD_IOCTL_STATUSW")));
  1008. pTHIS->Status(
  1009. pStruct,
  1010. SCARD_IOCTL_STATUSW);
  1011. break;
  1012. case SCARD_IOCTL_TRANSMIT:
  1013. TRC_DBG((TB, _T("SCARD_IOCTL_TRANSMIT")));
  1014. pTHIS->Transmit(
  1015. pStruct);
  1016. break;
  1017. case SCARD_IOCTL_CONTROL:
  1018. TRC_DBG((TB, _T("SCARD_IOCTL_CONTROL")));
  1019. pTHIS->Control(
  1020. pStruct);
  1021. break;
  1022. case SCARD_IOCTL_GETATTRIB:
  1023. TRC_DBG((TB, _T("SCARD_IOCTL_GETATTRIB")));
  1024. pTHIS->GetAttrib(
  1025. pStruct);
  1026. break;
  1027. case SCARD_IOCTL_SETATTRIB:
  1028. TRC_DBG((TB, _T("SCARD_IOCTL_SETATTRIB")));
  1029. pTHIS->SetAttrib(
  1030. pStruct);
  1031. break;
  1032. }
  1033. #if defined (OS_WINCE) && defined(DEBUG)
  1034. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pStruct->pIoRequestPacket->IoRequest);
  1035. TRC_DATA_DBG("Input buffer",(char *) (pIoRequest + 1), pIoRequest->Parameters.DeviceIoControl.InputBufferLength);
  1036. #endif
  1037. pTHIS->RemoveThreadFromList(hThread);
  1038. FreeLibraryAndExitThread(hModExtraRefCount, 0);
  1039. DC_END_FN();
  1040. #ifdef OS_WINCE
  1041. return 0;
  1042. #endif
  1043. }
  1044. VOID
  1045. W32SCard::DefaultIORequestMsgHandleWrapper(
  1046. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1047. IN NTSTATUS serverReturnStatus
  1048. )
  1049. {
  1050. if (!_fInDestructor && !_fFlushing)
  1051. {
  1052. DefaultIORequestMsgHandle(pIoRequestPacket, serverReturnStatus);
  1053. }
  1054. else
  1055. {
  1056. //
  1057. // Just drop it on the floor if we are shutting down or flushing
  1058. //
  1059. delete(pIoRequestPacket);
  1060. }
  1061. }
  1062. VOID
  1063. W32SCard::MsgIrpDeviceControl(
  1064. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1065. IN UINT32 packetLen
  1066. )
  1067. /*++
  1068. Routine Description:
  1069. Handle a file system control request from the server.
  1070. Arguments:
  1071. pIoRequestPacket - Server IO request packet.
  1072. packetLen - Length of the packet
  1073. Return Value:
  1074. NA
  1075. --*/
  1076. {
  1077. DC_BEGIN_FN("W32SCard::MsgIrpDeviceControl");
  1078. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1079. HMODULE hModExtraRefCount = NULL;
  1080. SCARDHANDLECALLSTRUCT *pStruct = NULL;
  1081. HANDLE hThread;
  1082. DWORD dwThreadId;
  1083. if (!pIoRequest->Parameters.DeviceIoControl.InputBufferLength)
  1084. {
  1085. // no data, get out
  1086. return;
  1087. }
  1088. switch(pIoRequest->Parameters.DeviceIoControl.IoControlCode)
  1089. {
  1090. case SCARD_IOCTL_ESTABLISHCONTEXT:
  1091. TRC_DBG((TB, _T("SCARD_IOCTL_ESTABLISHCONTEXT")));
  1092. EstablishContext(pIoRequestPacket);
  1093. break;
  1094. case SCARD_IOCTL_RELEASECONTEXT:
  1095. TRC_DBG((TB, _T("SCARD_IOCTL_RELEASECONTEXT")));
  1096. HandleContextCallWithLongReturn(
  1097. pIoRequestPacket,
  1098. SCARD_IOCTL_RELEASECONTEXT);
  1099. break;
  1100. case SCARD_IOCTL_ISVALIDCONTEXT:
  1101. TRC_DBG((TB, _T("SCARD_IOCTL_ISVALIDCONTEXT")));
  1102. HandleContextCallWithLongReturn(
  1103. pIoRequestPacket,
  1104. SCARD_IOCTL_ISVALIDCONTEXT);
  1105. break;
  1106. case SCARD_IOCTL_LISTREADERGROUPSA:
  1107. TRC_DBG((TB, _T("SCARD_IOCTL_LISTREADERGROUPSA")));
  1108. ListReaderGroups(
  1109. pIoRequestPacket,
  1110. SCARD_IOCTL_LISTREADERGROUPSA);
  1111. break;
  1112. case SCARD_IOCTL_LISTREADERGROUPSW:
  1113. TRC_DBG((TB, _T("SCARD_IOCTL_LISTREADERGROUPSW")));
  1114. ListReaderGroups(
  1115. pIoRequestPacket,
  1116. SCARD_IOCTL_LISTREADERGROUPSW);
  1117. break;
  1118. case SCARD_IOCTL_LISTREADERSA:
  1119. TRC_DBG((TB, _T("SCARD_IOCTL_LISTREADERSA")));
  1120. ListReaders(
  1121. pIoRequestPacket,
  1122. SCARD_IOCTL_LISTREADERSA);
  1123. break;
  1124. case SCARD_IOCTL_LISTREADERSW:
  1125. TRC_DBG((TB, _T("SCARD_IOCTL_LISTREADERSW")));
  1126. ListReaders(
  1127. pIoRequestPacket,
  1128. SCARD_IOCTL_LISTREADERSW);
  1129. break;
  1130. case SCARD_IOCTL_INTRODUCEREADERGROUPA:
  1131. TRC_DBG((TB, _T("SCARD_IOCTL_INTRODUCEREADERGROUPA")));
  1132. HandleContextAndStringCallWithLongReturn(
  1133. pIoRequestPacket,
  1134. SCARD_IOCTL_INTRODUCEREADERGROUPA);
  1135. break;
  1136. case SCARD_IOCTL_INTRODUCEREADERGROUPW:
  1137. TRC_DBG((TB, _T("SCARD_IOCTL_INTRODUCEREADERGROUPW")));
  1138. HandleContextAndStringCallWithLongReturn(
  1139. pIoRequestPacket,
  1140. SCARD_IOCTL_INTRODUCEREADERGROUPW);
  1141. break;
  1142. case SCARD_IOCTL_FORGETREADERGROUPA:
  1143. TRC_DBG((TB, _T("SCARD_IOCTL_FORGETREADERGROUPA")));
  1144. HandleContextAndStringCallWithLongReturn(
  1145. pIoRequestPacket,
  1146. SCARD_IOCTL_FORGETREADERGROUPA);
  1147. break;
  1148. case SCARD_IOCTL_FORGETREADERGROUPW:
  1149. TRC_DBG((TB, _T("SCARD_IOCTL_FORGETREADERGROUPW")));
  1150. HandleContextAndStringCallWithLongReturn(
  1151. pIoRequestPacket,
  1152. SCARD_IOCTL_FORGETREADERGROUPW);
  1153. break;
  1154. case SCARD_IOCTL_INTRODUCEREADERA:
  1155. TRC_DBG((TB, _T("SCARD_IOCTL_INTRODUCEREADERA")));
  1156. HandleContextAndTwoStringCallWithLongReturn(
  1157. pIoRequestPacket,
  1158. SCARD_IOCTL_INTRODUCEREADERA);
  1159. break;
  1160. case SCARD_IOCTL_INTRODUCEREADERW:
  1161. TRC_DBG((TB, _T("SCARD_IOCTL_INTRODUCEREADERW")));
  1162. HandleContextAndTwoStringCallWithLongReturn(
  1163. pIoRequestPacket,
  1164. SCARD_IOCTL_INTRODUCEREADERW);
  1165. break;
  1166. case SCARD_IOCTL_FORGETREADERA:
  1167. TRC_DBG((TB, _T("SCARD_IOCTL_FORGETREADERA")));
  1168. HandleContextAndStringCallWithLongReturn(
  1169. pIoRequestPacket,
  1170. SCARD_IOCTL_FORGETREADERA);
  1171. break;
  1172. case SCARD_IOCTL_FORGETREADERW:
  1173. TRC_DBG((TB, _T("SCARD_IOCTL_FORGETREADERW")));
  1174. HandleContextAndStringCallWithLongReturn(
  1175. pIoRequestPacket,
  1176. SCARD_IOCTL_FORGETREADERW);
  1177. break;
  1178. case SCARD_IOCTL_ADDREADERTOGROUPA:
  1179. TRC_DBG((TB, _T("SCARD_IOCTL_ADDREADERTOGROUPA")));
  1180. HandleContextAndTwoStringCallWithLongReturn(
  1181. pIoRequestPacket,
  1182. SCARD_IOCTL_ADDREADERTOGROUPA);
  1183. break;
  1184. case SCARD_IOCTL_ADDREADERTOGROUPW:
  1185. TRC_DBG((TB, _T("SCARD_IOCTL_ADDREADERTOGROUPW")));
  1186. HandleContextAndTwoStringCallWithLongReturn(
  1187. pIoRequestPacket,
  1188. SCARD_IOCTL_ADDREADERTOGROUPW);
  1189. break;
  1190. case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
  1191. TRC_DBG((TB, _T("SCARD_IOCTL_REMOVEREADERFROMGROUPA")));
  1192. HandleContextAndTwoStringCallWithLongReturn(
  1193. pIoRequestPacket,
  1194. SCARD_IOCTL_REMOVEREADERFROMGROUPA);
  1195. break;
  1196. case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
  1197. TRC_DBG((TB, _T("SCARD_IOCTL_REMOVEREADERFROMGROUPW")));
  1198. HandleContextAndTwoStringCallWithLongReturn(
  1199. pIoRequestPacket,
  1200. SCARD_IOCTL_REMOVEREADERFROMGROUPW);
  1201. break;
  1202. case SCARD_IOCTL_LOCATECARDSA:
  1203. TRC_DBG((TB, _T("SCARD_IOCTL_LOCATECARDSA")));
  1204. LocateCardsA(pIoRequestPacket);
  1205. break;
  1206. case SCARD_IOCTL_LOCATECARDSW:
  1207. TRC_DBG((TB, _T("SCARD_IOCTL_LOCATECARDSW")));
  1208. LocateCardsW(pIoRequestPacket);
  1209. break;
  1210. case SCARD_IOCTL_LOCATECARDSBYATRA:
  1211. TRC_DBG((TB, _T("SCARD_IOCTL_LOCATECARDSBYATRA")));
  1212. LocateCardsByATRA(pIoRequestPacket);
  1213. break;
  1214. case SCARD_IOCTL_LOCATECARDSBYATRW:
  1215. TRC_DBG((TB, _T("SCARD_IOCTL_LOCATECARDSBYATRW")));
  1216. LocateCardsByATRW(pIoRequestPacket);
  1217. break;
  1218. case SCARD_IOCTL_GETSTATUSCHANGEA:
  1219. TRC_DBG((TB, _T("SCARD_IOCTL_GETSTATUSCHANGEA")));
  1220. GetStatusChangeWrapper(
  1221. pIoRequestPacket,
  1222. SCARD_IOCTL_GETSTATUSCHANGEA);
  1223. break;
  1224. case SCARD_IOCTL_GETSTATUSCHANGEW:
  1225. TRC_DBG((TB, _T("SCARD_IOCTL_GETSTATUSCHANGEW")));
  1226. GetStatusChangeWrapper(
  1227. pIoRequestPacket,
  1228. SCARD_IOCTL_GETSTATUSCHANGEW);
  1229. break;
  1230. case SCARD_IOCTL_CANCEL:
  1231. TRC_DBG((TB, _T("SCARD_IOCTL_CANCEL")));
  1232. HandleContextCallWithLongReturn(
  1233. pIoRequestPacket,
  1234. SCARD_IOCTL_CANCEL);
  1235. break;
  1236. //
  1237. // Possibly blocking calls, so create a thread to make the call
  1238. //
  1239. case SCARD_IOCTL_CONNECTA:
  1240. case SCARD_IOCTL_CONNECTW:
  1241. case SCARD_IOCTL_RECONNECT:
  1242. case SCARD_IOCTL_DISCONNECT:
  1243. case SCARD_IOCTL_BEGINTRANSACTION:
  1244. case SCARD_IOCTL_ENDTRANSACTION:
  1245. #ifndef OS_WINCE
  1246. case SCARD_IOCTL_STATE:
  1247. #endif
  1248. case SCARD_IOCTL_STATUSA:
  1249. case SCARD_IOCTL_STATUSW:
  1250. case SCARD_IOCTL_TRANSMIT:
  1251. case SCARD_IOCTL_CONTROL:
  1252. case SCARD_IOCTL_GETATTRIB:
  1253. case SCARD_IOCTL_SETATTRIB:
  1254. //
  1255. // Get a ref count on our dll so that we know the dll
  1256. // code won't disappear from underneath us. The thread will
  1257. // release this ref count when it exits
  1258. //
  1259. hModExtraRefCount = AddRefCurrentModule();
  1260. if (hModExtraRefCount == NULL)
  1261. {
  1262. TRC_ERR((TB, _T("AddRefCurrentModule() failed.")));
  1263. goto ErrorReturn;
  1264. }
  1265. //
  1266. // Allocate the struct to pass to the thread
  1267. //
  1268. pStruct = (SCARDHANDLECALLSTRUCT *)
  1269. MIDL_user_allocate(sizeof(SCARDHANDLECALLSTRUCT));
  1270. if (pStruct == NULL)
  1271. {
  1272. TRC_ERR((TB, _T("MIDL_user_allocate failed to alloc %ld bytes."), sizeof(SCARDHANDLECALLSTRUCT)));
  1273. goto ErrorReturn;
  1274. }
  1275. pStruct->pTHIS = this;
  1276. pStruct->dwCallType = pIoRequest->Parameters.DeviceIoControl.IoControlCode;
  1277. pStruct->pIoRequestPacket = pIoRequestPacket;
  1278. pStruct->hModExtraRefCount = hModExtraRefCount;
  1279. pStruct->hThread = NULL;
  1280. //
  1281. // Create a thread that will do the actual work
  1282. //
  1283. EnterCriticalSection(&_csThreadList);
  1284. //
  1285. // If the object is currently being destroyed, then don't create a new thread.
  1286. //
  1287. if (_fInDestructor)
  1288. {
  1289. LeaveCriticalSection(&_csThreadList);
  1290. goto ErrorReturn;
  1291. }
  1292. hThread = CreateThread(
  1293. NULL,
  1294. 0,
  1295. SCardHandleCall_ThreadProc,
  1296. pStruct,
  1297. CREATE_SUSPENDED,
  1298. &dwThreadId);
  1299. if (hThread == NULL)
  1300. {
  1301. TRC_ERR((TB, _T("CreateThread failed with %lx."), GetLastError()));
  1302. LeaveCriticalSection(&_csThreadList);
  1303. goto ErrorReturn;
  1304. }
  1305. if (!AddThreadToList(hThread))
  1306. {
  1307. LeaveCriticalSection(&_csThreadList);
  1308. ResumeThread(hThread);
  1309. WaitForSingleObject(hThread, INFINITE);
  1310. CloseHandle(hThread);
  1311. //
  1312. // do this so we don't do an extra FreeLibrary. (since the thread actually
  1313. // got created the thread istself will do the FreeLibrary).
  1314. //
  1315. hModExtraRefCount = NULL;
  1316. goto ErrorReturn;
  1317. }
  1318. LeaveCriticalSection(&_csThreadList);
  1319. //
  1320. // Now let the thread go
  1321. //
  1322. pStruct->hThread = hThread;
  1323. ResumeThread(hThread);
  1324. //
  1325. // return here and let the thread we just created
  1326. // make the EncodeAndChannelWriteLongReturn call,
  1327. // which will send the return the calling server
  1328. //
  1329. break;
  1330. case SCARD_IOCTL_ACCESSSTARTEDEVENT:
  1331. TRC_DBG((TB, _T("SCARD_IOCTL_ACCESSSTARTEDEVENT")));
  1332. AccessStartedEvent(
  1333. pIoRequestPacket);
  1334. break;
  1335. #ifdef OS_WINCE
  1336. default:
  1337. TRC_ERR((TB, _T("Unsupported ioctl=0x%x(%d) function = %d"), pIoRequest->Parameters.DeviceIoControl.IoControlCode,
  1338. pIoRequest->Parameters.DeviceIoControl.IoControlCode, ((pIoRequest->Parameters.DeviceIoControl.IoControlCode & 0x3FFC) >> 2)));
  1339. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, STATUS_NOT_SUPPORTED);
  1340. break;
  1341. #endif
  1342. }
  1343. #if defined(OS_WINCE) && defined(DEBUG)
  1344. if (hModExtraRefCount != NULL)
  1345. {
  1346. TRC_DATA_DBG("Input buffer",(char *) (pIoRequest + 1), pIoRequest->Parameters.DeviceIoControl.InputBufferLength);
  1347. }
  1348. #endif
  1349. Return:
  1350. DC_END_FN();
  1351. return;
  1352. ErrorReturn:
  1353. if (hModExtraRefCount != NULL)
  1354. {
  1355. FreeLibrary(hModExtraRefCount);
  1356. }
  1357. MIDL_user_free(pStruct);
  1358. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, STATUS_NO_MEMORY);
  1359. goto Return;
  1360. }
  1361. //---------------------------------------------------------------------------------------
  1362. //
  1363. // W32SCard::AllocateAndChannelWriteReplyPacket
  1364. //
  1365. //---------------------------------------------------------------------------------------
  1366. void
  1367. W32SCard::AllocateAndChannelWriteReplyPacket(
  1368. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1369. IN char *pbEncodedBuffer,
  1370. IN unsigned long cbEncodedBuffer)
  1371. {
  1372. DC_BEGIN_FN("W32SCard::AllocateAndChannelWriteReplyPacket");
  1373. NTSTATUS status = STATUS_SUCCESS;
  1374. PRDPDR_IOCOMPLETION_PACKET pReplyPacket = NULL;
  1375. ULONG replyPacketSize = 0;
  1376. //
  1377. // If we are deleting this object, then just get out.
  1378. //
  1379. if (_fInDestructor || _fFlushing)
  1380. {
  1381. delete(pIoRequestPacket);
  1382. return;
  1383. }
  1384. //
  1385. // Check the size of the output buffer.
  1386. //
  1387. status = DrUTL_CheckIOBufOutputSize(pIoRequestPacket, cbEncodedBuffer);
  1388. //
  1389. // Allocate reply buffer
  1390. //
  1391. if (status == STATUS_SUCCESS)
  1392. {
  1393. status = DrUTL_AllocateReplyBuf(
  1394. pIoRequestPacket,
  1395. &pReplyPacket,
  1396. &replyPacketSize);
  1397. }
  1398. //
  1399. // Write reply to channel
  1400. //
  1401. if (status == STATUS_SUCCESS)
  1402. {
  1403. memcpy(
  1404. pReplyPacket->IoCompletion.Parameters.DeviceIoControl.OutputBuffer,
  1405. pbEncodedBuffer,
  1406. cbEncodedBuffer);
  1407. pReplyPacket->IoCompletion.Parameters.DeviceIoControl.OutputBufferLength
  1408. = cbEncodedBuffer;
  1409. pReplyPacket->IoCompletion.IoStatus = STATUS_SUCCESS;
  1410. //
  1411. // in DrUTL_AllocateReplyBuf the replyPacketSize is set to the max size
  1412. // allowed given the calling servers output buffer size, but we just need
  1413. // cbEncodedBuffer size. And, we know there is enough space since the
  1414. // DrUTL_CheckIOBufOutputSize call succeeded
  1415. //
  1416. replyPacketSize = cbEncodedBuffer +
  1417. (ULONG)FIELD_OFFSET(RDPDR_IOCOMPLETION_PACKET,
  1418. IoCompletion.Parameters.DeviceIoControl.OutputBuffer);
  1419. ProcessObject()->GetVCMgr().ChannelWrite(pReplyPacket, replyPacketSize);
  1420. }
  1421. else
  1422. {
  1423. goto ErrorReturn;
  1424. }
  1425. delete(pIoRequestPacket);
  1426. Return:
  1427. DC_END_FN();
  1428. return;
  1429. ErrorReturn:
  1430. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  1431. goto Return;
  1432. }
  1433. //---------------------------------------------------------------------------------------
  1434. //
  1435. // W32SCard::DecodeContextCall
  1436. //
  1437. //---------------------------------------------------------------------------------------
  1438. LONG
  1439. W32SCard::DecodeContextCall(
  1440. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1441. OUT SCARDCONTEXT *pSCardContext)
  1442. {
  1443. DC_BEGIN_FN("W32SCard::DecodeContextCall");
  1444. RPC_STATUS rpcStatus = RPC_S_OK;
  1445. LONG lReturn = SCARD_S_SUCCESS;
  1446. handle_t hDec = 0;
  1447. Context_Call ContextCall;
  1448. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1449. //
  1450. // Do the decode
  1451. //
  1452. rpcStatus = MesDecodeBufferHandleCreate(
  1453. (char *) (pIoRequest + 1), // bytes are at end of struct
  1454. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  1455. &hDec);
  1456. if (rpcStatus != RPC_S_OK)
  1457. {
  1458. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  1459. lReturn = SCARD_E_NO_MEMORY;
  1460. goto ErrorReturn;
  1461. }
  1462. memset(&ContextCall, 0, sizeof(ContextCall));
  1463. _TRY_lReturn(Context_Call_Decode(hDec, &ContextCall))
  1464. //
  1465. // Copy the decoded context to the callers memory
  1466. //
  1467. if (sizeof(SCARDCONTEXT) != ContextCall.Context.cbContext)
  1468. {
  1469. TRC_ERR((TB, _T("Invalid context from server")));
  1470. lReturn = SCARD_E_INVALID_PARAMETER;
  1471. goto ErrorReturn;
  1472. }
  1473. *pSCardContext = *((SCARDCONTEXT *) ContextCall.Context.pbContext);
  1474. //
  1475. // Free the resources used for decode of parameters
  1476. //
  1477. _TRY_2(Context_Call_Free(hDec, &ContextCall))
  1478. Return:
  1479. SafeMesHandleFree(&hDec);
  1480. DC_END_FN();
  1481. return (lReturn);
  1482. ErrorReturn:
  1483. goto Return;
  1484. }
  1485. //---------------------------------------------------------------------------------------
  1486. //
  1487. // W32SCard::DecodeContextAndStringCallA
  1488. //
  1489. //---------------------------------------------------------------------------------------
  1490. LONG
  1491. W32SCard::DecodeContextAndStringCallA(
  1492. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1493. OUT SCARDCONTEXT *pSCardContext,
  1494. OUT LPSTR *ppsz)
  1495. {
  1496. DC_BEGIN_FN("W32SCard::DecodeContextAndStringCallA");
  1497. RPC_STATUS rpcStatus = RPC_S_OK;
  1498. LONG lReturn = SCARD_S_SUCCESS;
  1499. handle_t hDec = 0;
  1500. BOOL fFreeDecode = FALSE;
  1501. ContextAndStringA_Call ContextAndStringCallA;
  1502. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1503. UINT cbStrLen = 0;
  1504. HRESULT hr;
  1505. //
  1506. // Decode input parameters
  1507. //
  1508. rpcStatus = MesDecodeBufferHandleCreate(
  1509. (char *) (pIoRequest + 1), // bytes are at end of struct
  1510. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  1511. &hDec);
  1512. if (rpcStatus != RPC_S_OK)
  1513. {
  1514. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  1515. lReturn = SCARD_E_NO_MEMORY;
  1516. goto ErrorReturn;
  1517. }
  1518. memset(&ContextAndStringCallA, 0, sizeof(ContextAndStringCallA));
  1519. _TRY_lReturn(ContextAndStringA_Call_Decode(hDec, &ContextAndStringCallA))
  1520. fFreeDecode = TRUE;
  1521. //
  1522. // Copy the contents to the callers out params
  1523. //
  1524. if (ContextAndStringCallA.Context.pbContext != NULL)
  1525. {
  1526. if (sizeof(SCARDCONTEXT) != ContextAndStringCallA.Context.cbContext)
  1527. {
  1528. TRC_ERR((TB, _T("Invalid context from server")));
  1529. lReturn = SCARD_E_INVALID_PARAMETER;
  1530. goto ErrorReturn;
  1531. }
  1532. *pSCardContext = *((SCARDCONTEXT *) ContextAndStringCallA.Context.pbContext);
  1533. }
  1534. else
  1535. {
  1536. *pSCardContext = NULL;
  1537. }
  1538. if (ContextAndStringCallA.sz == NULL)
  1539. {
  1540. TRC_ERR((TB, _T("Invalid string from server")));
  1541. lReturn = SCARD_E_INVALID_VALUE;
  1542. goto ErrorReturn;
  1543. }
  1544. cbStrLen = (strlen(ContextAndStringCallA.sz) + 1) * sizeof(char);
  1545. *ppsz = (LPSTR)
  1546. MIDL_user_allocate(cbStrLen);
  1547. if (*ppsz == NULL)
  1548. {
  1549. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  1550. lReturn = SCARD_E_NO_MEMORY;
  1551. goto ErrorReturn;
  1552. }
  1553. hr = StringCbCopyA(*ppsz, cbStrLen, ContextAndStringCallA.sz);
  1554. TRC_ASSERT(SUCCEEDED(hr),(TB,_T("Pre checked copy failed: 0x%x"), hr));
  1555. Return:
  1556. if (fFreeDecode)
  1557. {
  1558. _TRY_2(ContextAndStringA_Call_Free(hDec, &ContextAndStringCallA))
  1559. }
  1560. SafeMesHandleFree(&hDec);
  1561. DC_END_FN();
  1562. return (lReturn);
  1563. ErrorReturn:
  1564. goto Return;
  1565. }
  1566. //---------------------------------------------------------------------------------------
  1567. //
  1568. // W32SCard::DecodeContextAndStringCallW
  1569. //
  1570. //---------------------------------------------------------------------------------------
  1571. LONG
  1572. W32SCard::DecodeContextAndStringCallW(
  1573. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1574. OUT SCARDCONTEXT *pSCardContext,
  1575. OUT LPWSTR *ppwsz)
  1576. {
  1577. DC_BEGIN_FN("W32SCard::DecodeContextAndStringCallW");
  1578. RPC_STATUS rpcStatus = RPC_S_OK;
  1579. LONG lReturn = SCARD_S_SUCCESS;
  1580. handle_t hDec = 0;
  1581. BOOL fFreeDecode = FALSE;
  1582. ContextAndStringW_Call ContextAndStringCallW;
  1583. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1584. UINT cbStrLen = 0;
  1585. HRESULT hr;
  1586. //
  1587. // Decode input parameters
  1588. //
  1589. rpcStatus = MesDecodeBufferHandleCreate(
  1590. (char *) (pIoRequest + 1), // bytes are at end of struct
  1591. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  1592. &hDec);
  1593. if (rpcStatus != RPC_S_OK)
  1594. {
  1595. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  1596. lReturn = SCARD_E_NO_MEMORY;
  1597. goto ErrorReturn;
  1598. }
  1599. memset(&ContextAndStringCallW, 0, sizeof(ContextAndStringCallW));
  1600. _TRY_lReturn(ContextAndStringW_Call_Decode(hDec, &ContextAndStringCallW))
  1601. fFreeDecode = TRUE;
  1602. //
  1603. // Copy the contents to the callers out params
  1604. //
  1605. if (ContextAndStringCallW.Context.pbContext != NULL)
  1606. {
  1607. if (sizeof(SCARDCONTEXT) != ContextAndStringCallW.Context.cbContext)
  1608. {
  1609. TRC_ERR((TB, _T("Invalid context from server")));
  1610. lReturn = SCARD_E_INVALID_PARAMETER;
  1611. goto ErrorReturn;
  1612. }
  1613. *pSCardContext = *((SCARDCONTEXT *) ContextAndStringCallW.Context.pbContext);
  1614. }
  1615. else
  1616. {
  1617. *pSCardContext = NULL;
  1618. }
  1619. if (ContextAndStringCallW.sz == NULL)
  1620. {
  1621. TRC_ERR((TB, _T("Invalid string from server")));
  1622. lReturn = SCARD_E_INVALID_VALUE;
  1623. goto ErrorReturn;
  1624. }
  1625. cbStrLen = (wcslen(ContextAndStringCallW.sz) + 1) * sizeof(WCHAR);
  1626. *ppwsz = (LPWSTR)
  1627. MIDL_user_allocate(cbStrLen);
  1628. if (*ppwsz == NULL)
  1629. {
  1630. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  1631. lReturn = SCARD_E_NO_MEMORY;
  1632. goto ErrorReturn;
  1633. }
  1634. hr = StringCbCopyW(*ppwsz, cbStrLen, ContextAndStringCallW.sz);
  1635. TRC_ASSERT(SUCCEEDED(hr),(TB,_T("Pre checked copy failed: 0x%x"), hr));
  1636. Return:
  1637. if (fFreeDecode)
  1638. {
  1639. _TRY_2(ContextAndStringW_Call_Free(hDec, &ContextAndStringCallW))
  1640. }
  1641. SafeMesHandleFree(&hDec);
  1642. DC_END_FN();
  1643. return (lReturn);
  1644. ErrorReturn:
  1645. goto Return;
  1646. }
  1647. //---------------------------------------------------------------------------------------
  1648. //
  1649. // W32SCard::EncodeAndChannelWriteLongReturn
  1650. //
  1651. //---------------------------------------------------------------------------------------
  1652. void
  1653. W32SCard::EncodeAndChannelWriteLongReturn(
  1654. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1655. IN LONG lReturn)
  1656. {
  1657. DC_BEGIN_FN("W32SCard::EncodeAndChannelWriteLongReturn");
  1658. RPC_STATUS rpcStatus = RPC_S_OK;
  1659. NTSTATUS status = STATUS_SUCCESS;
  1660. char *pbEncodedBuffer = NULL;
  1661. unsigned long cbEncodedBuffer = 0;
  1662. handle_t hEnc = 0;
  1663. Long_Return LongReturn;
  1664. //
  1665. // Initialiaze struct to be encoded
  1666. //
  1667. LongReturn.ReturnCode = lReturn;
  1668. //
  1669. // Encode the return
  1670. //
  1671. rpcStatus = MesEncodeDynBufferHandleCreate(
  1672. &pbEncodedBuffer,
  1673. &cbEncodedBuffer,
  1674. &hEnc);
  1675. if (rpcStatus != RPC_S_OK)
  1676. {
  1677. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  1678. status = STATUS_NO_MEMORY;
  1679. goto ErrorReturn;
  1680. }
  1681. _TRY_status(Long_Return_Encode(hEnc, &LongReturn))
  1682. //
  1683. // Send the return
  1684. //
  1685. AllocateAndChannelWriteReplyPacket(
  1686. pIoRequestPacket,
  1687. pbEncodedBuffer,
  1688. cbEncodedBuffer);
  1689. Return:
  1690. SafeMesHandleFree(&hEnc);
  1691. MIDL_user_free(pbEncodedBuffer);
  1692. DC_END_FN();
  1693. return;
  1694. ErrorReturn:
  1695. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  1696. goto Return;
  1697. }
  1698. //---------------------------------------------------------------------------------------
  1699. //
  1700. // W32SCard::EstablishContext
  1701. //
  1702. //---------------------------------------------------------------------------------------
  1703. void
  1704. W32SCard::EstablishContext(
  1705. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  1706. {
  1707. DC_BEGIN_FN("W32SCard::EstablishContext");
  1708. RPC_STATUS rpcStatus = RPC_S_OK;
  1709. NTSTATUS status = STATUS_SUCCESS;
  1710. char *pbEncodedBuffer = NULL;
  1711. unsigned long cbEncodedBuffer = 0;
  1712. PRDPDR_IOCOMPLETION_PACKET pReplyPacket = NULL;
  1713. ULONG replyPacketSize = 0;
  1714. handle_t hDec = 0;
  1715. handle_t hEnc = 0;
  1716. BOOL fFreeDecode = FALSE;
  1717. BOOL fFreeContext = FALSE;
  1718. EstablishContext_Call EstablishContextCall;
  1719. EstablishContext_Return EstablishContextReturn;
  1720. #ifndef OS_WINCE
  1721. DWORD dwScope;
  1722. #endif
  1723. SCARDCONTEXT SCardContext;
  1724. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1725. //
  1726. // Decode parameters
  1727. //
  1728. rpcStatus = MesDecodeBufferHandleCreate(
  1729. (char *) (pIoRequest + 1), // bytes are at end of struct
  1730. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  1731. &hDec);
  1732. if (rpcStatus != RPC_S_OK)
  1733. {
  1734. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  1735. status = STATUS_NO_MEMORY;
  1736. goto ErrorReturn;
  1737. }
  1738. memset(&EstablishContextCall, 0, sizeof(EstablishContextCall));
  1739. _TRY_status(EstablishContext_Call_Decode(hDec, &EstablishContextCall))
  1740. fFreeDecode = TRUE;
  1741. //
  1742. // Make the call to the SCard subsystem
  1743. //
  1744. memset(&EstablishContextReturn, 0, sizeof(EstablishContextReturn));
  1745. EstablishContextReturn.ReturnCode =
  1746. pfnSCardEstablishContext(
  1747. EstablishContextCall.dwScope,
  1748. NULL,
  1749. NULL,
  1750. &SCardContext);
  1751. if (EstablishContextReturn.ReturnCode == SCARD_S_SUCCESS)
  1752. {
  1753. EstablishContextReturn.Context.pbContext = (BYTE *) &SCardContext;
  1754. EstablishContextReturn.Context.cbContext = sizeof(SCARDCONTEXT);
  1755. fFreeContext = TRUE;
  1756. }
  1757. //
  1758. // Encode the return
  1759. //
  1760. rpcStatus = MesEncodeDynBufferHandleCreate(
  1761. &pbEncodedBuffer,
  1762. &cbEncodedBuffer,
  1763. &hEnc);
  1764. if (rpcStatus != RPC_S_OK)
  1765. {
  1766. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  1767. status = STATUS_NO_MEMORY;
  1768. goto ErrorReturn;
  1769. }
  1770. _TRY_status(EstablishContext_Return_Encode(hEnc, &EstablishContextReturn))
  1771. //
  1772. // Add the new context to the list before returing to caller
  1773. //
  1774. if (!AddSCardContextToList(SCardContext))
  1775. {
  1776. status = STATUS_NO_MEMORY;
  1777. goto ErrorReturn;
  1778. }
  1779. AllocateAndChannelWriteReplyPacket(
  1780. pIoRequestPacket,
  1781. pbEncodedBuffer,
  1782. cbEncodedBuffer);
  1783. Return:
  1784. if (fFreeDecode)
  1785. {
  1786. _TRY_2(EstablishContext_Call_Free(hDec, &EstablishContextCall))
  1787. }
  1788. SafeMesHandleFree(&hDec);
  1789. SafeMesHandleFree(&hEnc);
  1790. MIDL_user_free(pbEncodedBuffer);
  1791. DC_END_FN();
  1792. return;
  1793. ErrorReturn:
  1794. if (fFreeContext)
  1795. {
  1796. pfnSCardReleaseContext(SCardContext);
  1797. }
  1798. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  1799. goto Return;
  1800. }
  1801. //---------------------------------------------------------------------------------------
  1802. //
  1803. // W32SCard::HandleContextCallWithLongReturn
  1804. //
  1805. //---------------------------------------------------------------------------------------
  1806. void
  1807. W32SCard::HandleContextCallWithLongReturn(
  1808. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1809. IN DWORD dwCallType)
  1810. {
  1811. DC_BEGIN_FN("W32SCard::HandleContextCallWithLongReturn");
  1812. NTSTATUS status = STATUS_SUCCESS;
  1813. LONG lReturn = SCARD_S_SUCCESS;
  1814. SCARDCONTEXT SCardContext;
  1815. //
  1816. // Decode the context being released
  1817. //
  1818. lReturn = DecodeContextCall(pIoRequestPacket, &SCardContext);
  1819. if (lReturn == SCARD_S_SUCCESS)
  1820. {
  1821. //
  1822. // Make SCard subsystem call
  1823. //
  1824. switch(dwCallType)
  1825. {
  1826. case SCARD_IOCTL_RELEASECONTEXT:
  1827. #ifndef OS_WINCE
  1828. lReturn = pfnSCardReleaseContext(SCardContext);
  1829. #endif
  1830. RemoveSCardContextFromList(SCardContext);
  1831. #ifdef OS_WINCE
  1832. lReturn = pfnSCardReleaseContext(SCardContext); //the context must be released after you cancel any operations on the card
  1833. #endif
  1834. break;
  1835. case SCARD_IOCTL_ISVALIDCONTEXT:
  1836. lReturn = pfnSCardIsValidContext(SCardContext);
  1837. break;
  1838. case SCARD_IOCTL_CANCEL:
  1839. lReturn = pfnSCardCancel(SCardContext);
  1840. break;
  1841. }
  1842. }
  1843. //
  1844. // encode and write the return
  1845. //
  1846. EncodeAndChannelWriteLongReturn(pIoRequestPacket, lReturn);
  1847. Return:
  1848. DC_END_FN();
  1849. return;
  1850. ErrorReturn:
  1851. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  1852. goto Return;
  1853. }
  1854. //---------------------------------------------------------------------------------------
  1855. //
  1856. // W32SCard::EncodeAndChannelWriteLongAndMultiStringReturn
  1857. //
  1858. //---------------------------------------------------------------------------------------
  1859. void
  1860. W32SCard::EncodeAndChannelWriteLongAndMultiStringReturn(
  1861. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1862. IN LONG lReturn,
  1863. IN BYTE *pb,
  1864. IN DWORD cch,
  1865. IN BOOL fUnicode)
  1866. {
  1867. DC_BEGIN_FN("W32SCard::EncodeAndChannelWriteLongAndMultiStringReturn");
  1868. RPC_STATUS rpcStatus = RPC_S_OK;
  1869. NTSTATUS status = STATUS_SUCCESS;
  1870. char *pbEncodedBuffer = NULL;
  1871. unsigned long cbEncodedBuffer = 0;
  1872. handle_t h = 0;
  1873. BOOL fFree = FALSE;
  1874. struct _LongAndMultiString_Return LongAndMultiSzReturn;
  1875. //
  1876. // Initialiaze struct to be encoded
  1877. //
  1878. LongAndMultiSzReturn.ReturnCode = lReturn;
  1879. LongAndMultiSzReturn.cBytes = fUnicode ? (cch * sizeof(WCHAR)) : cch;
  1880. //
  1881. // If we are just returning the byte count then send back a junk buffer
  1882. //
  1883. if (pb == NULL)
  1884. {
  1885. LongAndMultiSzReturn.msz = (BYTE *) MIDL_user_allocate(LongAndMultiSzReturn.cBytes);
  1886. if (LongAndMultiSzReturn.msz == NULL)
  1887. {
  1888. status = STATUS_NO_MEMORY;
  1889. goto ErrorReturn;
  1890. }
  1891. fFree = TRUE;
  1892. memset(LongAndMultiSzReturn.msz, 0, LongAndMultiSzReturn.cBytes);
  1893. }
  1894. else
  1895. {
  1896. LongAndMultiSzReturn.msz = pb;
  1897. }
  1898. //
  1899. // Encode the return
  1900. //
  1901. rpcStatus = MesEncodeDynBufferHandleCreate(
  1902. &pbEncodedBuffer,
  1903. &cbEncodedBuffer,
  1904. &h);
  1905. if (rpcStatus != RPC_S_OK)
  1906. {
  1907. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  1908. status = STATUS_NO_MEMORY;
  1909. goto ErrorReturn;
  1910. }
  1911. _TRY_status(ListReaderGroups_Return_Encode(h, &LongAndMultiSzReturn))
  1912. //
  1913. // Send the return
  1914. //
  1915. AllocateAndChannelWriteReplyPacket(
  1916. pIoRequestPacket,
  1917. pbEncodedBuffer,
  1918. cbEncodedBuffer);
  1919. Return:
  1920. if (fFree)
  1921. {
  1922. MIDL_user_free(LongAndMultiSzReturn.msz);
  1923. }
  1924. SafeMesHandleFree(&h);
  1925. MIDL_user_free(pbEncodedBuffer);
  1926. DC_END_FN();
  1927. return;
  1928. ErrorReturn:
  1929. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  1930. goto Return;
  1931. }
  1932. //---------------------------------------------------------------------------------------
  1933. //
  1934. // W32SCard::ListReaderGroups
  1935. //
  1936. //---------------------------------------------------------------------------------------
  1937. void
  1938. W32SCard::ListReaderGroups(
  1939. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  1940. IN DWORD dwCallType)
  1941. {
  1942. DC_BEGIN_FN("W32SCard::ListReaderGroups");
  1943. LONG lReturn = SCARD_S_SUCCESS;
  1944. RPC_STATUS rpcStatus = RPC_S_OK;
  1945. NTSTATUS status = STATUS_SUCCESS;
  1946. handle_t hDec = 0;
  1947. SCARDCONTEXT SCardContext;
  1948. ListReaderGroups_Call ListReaderGroupsCall;
  1949. DWORD cch = 0;
  1950. BYTE *pb = NULL;
  1951. BOOL fFreeDecode = FALSE;
  1952. BOOL fDoAllocationLocally = FALSE;
  1953. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  1954. //
  1955. // Decode parameters
  1956. //
  1957. rpcStatus = MesDecodeBufferHandleCreate(
  1958. (char *) (pIoRequest + 1), // bytes are at end of struct
  1959. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  1960. &hDec);
  1961. if (rpcStatus != RPC_S_OK)
  1962. {
  1963. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  1964. status = STATUS_NO_MEMORY;
  1965. goto ErrorReturn;
  1966. }
  1967. memset(&ListReaderGroupsCall, 0, sizeof(ListReaderGroupsCall));
  1968. _TRY_status(ListReaderGroups_Call_Decode(hDec, &ListReaderGroupsCall))
  1969. fFreeDecode = TRUE;
  1970. if (ListReaderGroupsCall.Context.pbContext != NULL)
  1971. {
  1972. if (sizeof(SCARDCONTEXT) == ListReaderGroupsCall.Context.cbContext)
  1973. {
  1974. SCardContext = *((SCARDCONTEXT *) ListReaderGroupsCall.Context.pbContext);
  1975. }
  1976. else
  1977. {
  1978. TRC_ERR((TB, _T("Invalid context sent from server.")));
  1979. lReturn = SCARD_E_INVALID_PARAMETER;
  1980. }
  1981. }
  1982. else
  1983. {
  1984. SCardContext = NULL;
  1985. }
  1986. cch = ListReaderGroupsCall.cchGroups;
  1987. if (lReturn == SCARD_S_SUCCESS)
  1988. {
  1989. //
  1990. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  1991. //
  1992. fDoAllocationLocally =
  1993. (!ListReaderGroupsCall.fmszGroupsIsNULL &&
  1994. (cch != SCARD_AUTOALLOCATE));
  1995. #ifdef OS_WINCE
  1996. if (!fDoAllocationLocally)
  1997. {
  1998. lReturn = pfnSCardListReaderGroupsW(
  1999. SCardContext,
  2000. NULL,
  2001. &cch);
  2002. if ((lReturn == SCARD_S_SUCCESS) && (cch > 0))
  2003. fDoAllocationLocally = TRUE;
  2004. else
  2005. {
  2006. TRC_ERR((TB, _T("SCardListReaderGroupsW failed")));
  2007. status = STATUS_UNSUCCESSFUL;
  2008. goto ErrorReturn;
  2009. }
  2010. }
  2011. #endif
  2012. //
  2013. // Make the ListReaderGroups call
  2014. //
  2015. if (dwCallType == SCARD_IOCTL_LISTREADERGROUPSA)
  2016. {
  2017. LPSTR psz = NULL;
  2018. if (fDoAllocationLocally)
  2019. {
  2020. psz = (LPSTR) MIDL_user_allocate(cch * sizeof(CHAR));
  2021. if (psz == NULL)
  2022. {
  2023. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2024. status = STATUS_NO_MEMORY;
  2025. goto ErrorReturn;
  2026. }
  2027. }
  2028. lReturn = pfnSCardListReaderGroupsA(
  2029. SCardContext,
  2030. (cch == SCARD_AUTOALLOCATE) ? (LPSTR) &psz : psz,
  2031. &cch);
  2032. pb = (LPBYTE) psz;
  2033. }
  2034. else
  2035. {
  2036. LPWSTR pwsz = NULL;
  2037. if (fDoAllocationLocally)
  2038. {
  2039. pwsz = (LPWSTR) MIDL_user_allocate(cch * sizeof(WCHAR));
  2040. if (pwsz == NULL)
  2041. {
  2042. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2043. status = STATUS_NO_MEMORY;
  2044. goto ErrorReturn;
  2045. }
  2046. }
  2047. lReturn = pfnSCardListReaderGroupsW(
  2048. SCardContext,
  2049. (cch == SCARD_AUTOALLOCATE) ? (LPWSTR) &pwsz : pwsz,
  2050. &cch);
  2051. pb = (LPBYTE) pwsz;
  2052. }
  2053. }
  2054. //
  2055. // If anything failed, make sure we don't return a string
  2056. //
  2057. if (lReturn != SCARD_S_SUCCESS)
  2058. {
  2059. if (fDoAllocationLocally)
  2060. {
  2061. MIDL_user_free(pb);
  2062. }
  2063. pb = NULL;
  2064. cch = 0;
  2065. }
  2066. //
  2067. // write the return to the channel
  2068. //
  2069. EncodeAndChannelWriteLongAndMultiStringReturn(
  2070. pIoRequestPacket,
  2071. lReturn,
  2072. pb,
  2073. cch,
  2074. (dwCallType == SCARD_IOCTL_LISTREADERGROUPSA) ? FALSE : TRUE);
  2075. Return:
  2076. if (pb != NULL)
  2077. {
  2078. //
  2079. // Check to see whether we allocated or SCard allcated for us
  2080. //
  2081. if (fDoAllocationLocally)
  2082. {
  2083. MIDL_user_free(pb);
  2084. }
  2085. else
  2086. {
  2087. pfnSCardFreeMemory(SCardContext, pb);
  2088. }
  2089. }
  2090. if (fFreeDecode)
  2091. {
  2092. _TRY_2(ListReaderGroups_Call_Free(hDec, &ListReaderGroupsCall))
  2093. }
  2094. SafeMesHandleFree(&hDec);
  2095. DC_END_FN();
  2096. return;
  2097. ErrorReturn:
  2098. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2099. goto Return;
  2100. }
  2101. //---------------------------------------------------------------------------------------
  2102. //
  2103. // W32SCard::ListReaderGroups
  2104. //
  2105. //---------------------------------------------------------------------------------------
  2106. void
  2107. W32SCard::ListReaders(
  2108. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  2109. IN DWORD dwCallType)
  2110. {
  2111. DC_BEGIN_FN("W32SCard::ListReaders");
  2112. LONG lReturn = SCARD_S_SUCCESS;
  2113. RPC_STATUS rpcStatus = RPC_S_OK;
  2114. NTSTATUS status = STATUS_SUCCESS;
  2115. handle_t hDec = 0;
  2116. SCARDCONTEXT SCardContext;
  2117. ListReaders_Call ListReadersCall;
  2118. DWORD cch = 0;
  2119. BYTE *pb = NULL;
  2120. BOOL fFreeDecode = FALSE;
  2121. BOOL fDoAllocationLocally = FALSE;
  2122. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  2123. //
  2124. // Decode parameters
  2125. //
  2126. rpcStatus = MesDecodeBufferHandleCreate(
  2127. (char *) (pIoRequest + 1), // bytes are at end of struct
  2128. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  2129. &hDec);
  2130. if (rpcStatus != RPC_S_OK)
  2131. {
  2132. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  2133. status = STATUS_NO_MEMORY;
  2134. goto ErrorReturn;
  2135. }
  2136. memset(&ListReadersCall, 0, sizeof(ListReadersCall));
  2137. _TRY_status(ListReaders_Call_Decode(hDec, &ListReadersCall))
  2138. fFreeDecode = TRUE;
  2139. if (ListReadersCall.Context.pbContext != NULL)
  2140. {
  2141. if (sizeof(SCARDCONTEXT) == ListReadersCall.Context.cbContext)
  2142. {
  2143. SCardContext = *((SCARDCONTEXT *) ListReadersCall.Context.pbContext);
  2144. }
  2145. else
  2146. {
  2147. TRC_ERR((TB, _T("Invalid context sent from server.")));
  2148. lReturn = SCARD_E_INVALID_PARAMETER;
  2149. }
  2150. }
  2151. else
  2152. {
  2153. SCardContext = NULL;
  2154. }
  2155. cch = ListReadersCall.cchReaders;
  2156. if (lReturn == SCARD_S_SUCCESS)
  2157. {
  2158. //
  2159. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  2160. //
  2161. fDoAllocationLocally =
  2162. (!ListReadersCall.fmszReadersIsNULL &&
  2163. (cch != SCARD_AUTOALLOCATE));
  2164. //
  2165. // Make the ListReaders call
  2166. //
  2167. if (dwCallType == SCARD_IOCTL_LISTREADERSA)
  2168. {
  2169. LPSTR psz = NULL;
  2170. #ifdef OS_WINCE
  2171. if (!fDoAllocationLocally)
  2172. {
  2173. lReturn = pfnSCardListReadersA(
  2174. SCardContext,
  2175. (LPSTR) ListReadersCall.mszGroups,
  2176. NULL,
  2177. &cch);
  2178. if ((lReturn == SCARD_S_SUCCESS) && (cch > 0))
  2179. fDoAllocationLocally = TRUE;
  2180. else
  2181. {
  2182. TRC_DBG((TB, _T("SCardListReadersA failed")));
  2183. status = STATUS_UNSUCCESSFUL;
  2184. goto ErrorReturn;
  2185. }
  2186. }
  2187. #endif
  2188. if (fDoAllocationLocally)
  2189. {
  2190. psz = (LPSTR) MIDL_user_allocate(cch * sizeof(CHAR));
  2191. if (psz == NULL)
  2192. {
  2193. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2194. status = STATUS_NO_MEMORY;
  2195. goto ErrorReturn;
  2196. }
  2197. }
  2198. lReturn = pfnSCardListReadersA(
  2199. SCardContext,
  2200. (LPSTR) ListReadersCall.mszGroups,
  2201. (cch == SCARD_AUTOALLOCATE) ? (LPSTR) &psz : psz,
  2202. &cch);
  2203. pb = (LPBYTE) psz;
  2204. }
  2205. else
  2206. {
  2207. LPWSTR pwsz = NULL;
  2208. #ifdef OS_WINCE
  2209. if (!fDoAllocationLocally)
  2210. {
  2211. lReturn = pfnSCardListReadersW(
  2212. SCardContext,
  2213. SCARD_ALL_READERS,
  2214. NULL,
  2215. &cch);
  2216. if ((lReturn == SCARD_S_SUCCESS) && (cch > 0))
  2217. fDoAllocationLocally = TRUE;
  2218. else
  2219. {
  2220. TRC_NRM((TB, _T("SCardListReadersW failed")));
  2221. status = STATUS_UNSUCCESSFUL;
  2222. goto ErrorReturn;
  2223. }
  2224. }
  2225. #endif
  2226. if (fDoAllocationLocally)
  2227. {
  2228. pwsz = (LPWSTR) MIDL_user_allocate(cch * sizeof(WCHAR));
  2229. if (pwsz == NULL)
  2230. {
  2231. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2232. status = STATUS_NO_MEMORY;
  2233. goto ErrorReturn;
  2234. }
  2235. }
  2236. lReturn = pfnSCardListReadersW(
  2237. SCardContext,
  2238. #ifndef OS_WINCE
  2239. (LPWSTR) ListReadersCall.mszGroups,
  2240. #else
  2241. SCARD_ALL_READERS,
  2242. #endif
  2243. (cch == SCARD_AUTOALLOCATE) ? (LPWSTR) &pwsz : pwsz,
  2244. &cch);
  2245. pb = (LPBYTE) pwsz;
  2246. }
  2247. }
  2248. //
  2249. // If anything failed, make sure we don't return a string
  2250. //
  2251. if (lReturn != SCARD_S_SUCCESS)
  2252. {
  2253. if (fDoAllocationLocally)
  2254. {
  2255. MIDL_user_free(pb);
  2256. }
  2257. pb = NULL;
  2258. cch = 0;
  2259. }
  2260. //
  2261. // write the return to the channel
  2262. //
  2263. EncodeAndChannelWriteLongAndMultiStringReturn(
  2264. pIoRequestPacket,
  2265. lReturn,
  2266. pb,
  2267. cch,
  2268. (dwCallType == SCARD_IOCTL_LISTREADERSA) ? FALSE : TRUE);
  2269. Return:
  2270. if (pb != NULL)
  2271. {
  2272. //
  2273. // Check to see whether we allocated or SCard allcated for us
  2274. //
  2275. if (fDoAllocationLocally)
  2276. {
  2277. MIDL_user_free(pb);
  2278. }
  2279. else
  2280. {
  2281. #ifndef OS_WINCE
  2282. pfnSCardFreeMemory(SCardContext, pb);
  2283. #else
  2284. TRC_ASSERT(FALSE, (TB, _T("Shouldnt get here")));
  2285. #endif
  2286. }
  2287. }
  2288. if (fFreeDecode)
  2289. {
  2290. _TRY_2(ListReaders_Call_Free(hDec, &ListReadersCall))
  2291. }
  2292. SafeMesHandleFree(&hDec);
  2293. DC_END_FN();
  2294. return;
  2295. ErrorReturn:
  2296. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2297. goto Return;
  2298. }
  2299. //---------------------------------------------------------------------------------------
  2300. //
  2301. // W32SCard::HandleContextAndStringCallWithLongReturn
  2302. //
  2303. //---------------------------------------------------------------------------------------
  2304. void
  2305. W32SCard::HandleContextAndStringCallWithLongReturn(
  2306. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  2307. IN DWORD dwCallType)
  2308. {
  2309. DC_BEGIN_FN("W32SCard::HandleContextAndStringCallWithLongReturn");
  2310. LONG lReturn = SCARD_S_SUCCESS;
  2311. NTSTATUS status = STATUS_SUCCESS;
  2312. SCARDCONTEXT SCardContext;
  2313. LPSTR sz = NULL;
  2314. LPWSTR wsz = NULL;
  2315. BOOL fASCIICall;
  2316. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  2317. fASCIICall = ( (dwCallType == SCARD_IOCTL_INTRODUCEREADERGROUPA) ||
  2318. (dwCallType == SCARD_IOCTL_FORGETREADERGROUPA) ||
  2319. (dwCallType == SCARD_IOCTL_FORGETREADERA));
  2320. //
  2321. // Decode input params
  2322. //
  2323. if (fASCIICall)
  2324. {
  2325. lReturn = DecodeContextAndStringCallA(
  2326. pIoRequestPacket,
  2327. &SCardContext,
  2328. &sz);
  2329. }
  2330. else
  2331. {
  2332. lReturn = DecodeContextAndStringCallW(
  2333. pIoRequestPacket,
  2334. &SCardContext,
  2335. &wsz);
  2336. }
  2337. if (lReturn == SCARD_S_SUCCESS)
  2338. {
  2339. //
  2340. // Make the SCard* call
  2341. //
  2342. switch (dwCallType)
  2343. {
  2344. case SCARD_IOCTL_INTRODUCEREADERGROUPA:
  2345. lReturn = pfnSCardIntroduceReaderGroupA(
  2346. SCardContext,
  2347. sz);
  2348. break;
  2349. case SCARD_IOCTL_INTRODUCEREADERGROUPW:
  2350. lReturn = pfnSCardIntroduceReaderGroupW(
  2351. SCardContext,
  2352. wsz);
  2353. break;
  2354. case SCARD_IOCTL_FORGETREADERGROUPA:
  2355. lReturn = pfnSCardForgetReaderGroupA(
  2356. SCardContext,
  2357. sz);
  2358. break;
  2359. case SCARD_IOCTL_FORGETREADERGROUPW:
  2360. lReturn = pfnSCardForgetReaderGroupW(
  2361. SCardContext,
  2362. wsz);
  2363. break;
  2364. case SCARD_IOCTL_FORGETREADERA:
  2365. lReturn = pfnSCardForgetReaderA(
  2366. SCardContext,
  2367. sz);
  2368. break;
  2369. case SCARD_IOCTL_FORGETREADERW:
  2370. lReturn = pfnSCardForgetReaderW(
  2371. SCardContext,
  2372. wsz);
  2373. break;
  2374. }
  2375. }
  2376. //
  2377. // send the return
  2378. //
  2379. EncodeAndChannelWriteLongReturn(pIoRequestPacket, lReturn);
  2380. Return:
  2381. MIDL_user_free(sz);
  2382. MIDL_user_free(wsz);
  2383. DC_END_FN();
  2384. return;
  2385. ErrorReturn:
  2386. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2387. goto Return;
  2388. }
  2389. //---------------------------------------------------------------------------------------
  2390. //
  2391. // W32SCard::HandleContextAndTwoStringCallWithLongReturn
  2392. //
  2393. //---------------------------------------------------------------------------------------
  2394. void
  2395. W32SCard::HandleContextAndTwoStringCallWithLongReturn(
  2396. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  2397. IN DWORD dwCallType)
  2398. {
  2399. DC_BEGIN_FN("W32SCard::HandleContextAndTwoStringCallWithLongReturn");
  2400. RPC_STATUS rpcStatus = RPC_S_OK;
  2401. LONG lReturn = SCARD_S_SUCCESS;
  2402. NTSTATUS status = STATUS_SUCCESS;
  2403. SCARDCONTEXT SCardContext;
  2404. handle_t hDec = 0;
  2405. ContextAndTwoStringA_Call ContextAndTwoStringCallA;
  2406. ContextAndTwoStringW_Call ContextAndTwoStringCallW;
  2407. BOOL fASCIICall;
  2408. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  2409. //
  2410. // Decode input parameters
  2411. //
  2412. rpcStatus = MesDecodeBufferHandleCreate(
  2413. (char *) (pIoRequest + 1), // bytes are at end of struct
  2414. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  2415. &hDec);
  2416. if (rpcStatus != RPC_S_OK)
  2417. {
  2418. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  2419. status = STATUS_NO_MEMORY;
  2420. goto ErrorReturn;
  2421. }
  2422. fASCIICall = ( (dwCallType == SCARD_IOCTL_INTRODUCEREADERA) ||
  2423. (dwCallType == SCARD_IOCTL_ADDREADERTOGROUPA) ||
  2424. (dwCallType == SCARD_IOCTL_REMOVEREADERFROMGROUPA));
  2425. if (fASCIICall)
  2426. {
  2427. memset(&ContextAndTwoStringCallA, 0, sizeof(ContextAndTwoStringCallA));
  2428. _TRY_status(ContextAndTwoStringA_Call_Decode(hDec, &ContextAndTwoStringCallA))
  2429. if (sizeof(SCARDCONTEXT) == ContextAndTwoStringCallA.Context.cbContext)
  2430. {
  2431. SCardContext = *((SCARDCONTEXT *) ContextAndTwoStringCallA.Context.pbContext);
  2432. }
  2433. else
  2434. {
  2435. TRC_ERR((TB, _T("Invalid context sent from server.")));
  2436. lReturn = SCARD_E_INVALID_PARAMETER;
  2437. }
  2438. }
  2439. else
  2440. {
  2441. memset(&ContextAndTwoStringCallW, 0, sizeof(ContextAndTwoStringCallW));
  2442. _TRY_status(ContextAndTwoStringW_Call_Decode(hDec, &ContextAndTwoStringCallW))
  2443. if (sizeof(SCARDCONTEXT) == ContextAndTwoStringCallW.Context.cbContext)
  2444. {
  2445. SCardContext = *((SCARDCONTEXT *) ContextAndTwoStringCallW.Context.pbContext);
  2446. }
  2447. else
  2448. {
  2449. TRC_ERR((TB, _T("Invalid context sent from server.")));
  2450. lReturn = SCARD_E_INVALID_PARAMETER;
  2451. }
  2452. }
  2453. //
  2454. // Check for NULL input strings
  2455. //
  2456. switch (dwCallType)
  2457. {
  2458. case SCARD_IOCTL_INTRODUCEREADERA:
  2459. case SCARD_IOCTL_ADDREADERTOGROUPA:
  2460. case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
  2461. if ((ContextAndTwoStringCallA.sz1 == NULL) ||
  2462. (ContextAndTwoStringCallA.sz2 == NULL))
  2463. {
  2464. lReturn = SCARD_E_INVALID_VALUE;
  2465. }
  2466. break;
  2467. case SCARD_IOCTL_INTRODUCEREADERW:
  2468. case SCARD_IOCTL_ADDREADERTOGROUPW:
  2469. case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
  2470. if ((ContextAndTwoStringCallW.sz1 == NULL) ||
  2471. (ContextAndTwoStringCallW.sz2 == NULL))
  2472. {
  2473. lReturn = SCARD_E_INVALID_VALUE;
  2474. }
  2475. break;
  2476. }
  2477. if (lReturn == SCARD_S_SUCCESS)
  2478. {
  2479. //
  2480. // Make the SCard* call
  2481. //
  2482. switch (dwCallType)
  2483. {
  2484. case SCARD_IOCTL_INTRODUCEREADERA:
  2485. lReturn = pfnSCardIntroduceReaderA(
  2486. SCardContext,
  2487. ContextAndTwoStringCallA.sz1,
  2488. ContextAndTwoStringCallA.sz2);
  2489. break;
  2490. case SCARD_IOCTL_INTRODUCEREADERW:
  2491. lReturn = pfnSCardIntroduceReaderW(
  2492. SCardContext,
  2493. ContextAndTwoStringCallW.sz1,
  2494. ContextAndTwoStringCallW.sz2);
  2495. break;
  2496. case SCARD_IOCTL_ADDREADERTOGROUPA:
  2497. lReturn = pfnSCardAddReaderToGroupA(
  2498. SCardContext,
  2499. ContextAndTwoStringCallA.sz1,
  2500. ContextAndTwoStringCallA.sz2);
  2501. break;
  2502. case SCARD_IOCTL_ADDREADERTOGROUPW:
  2503. lReturn = pfnSCardAddReaderToGroupW(
  2504. SCardContext,
  2505. ContextAndTwoStringCallW.sz1,
  2506. ContextAndTwoStringCallW.sz2);
  2507. break;
  2508. case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
  2509. lReturn = pfnSCardRemoveReaderFromGroupA(
  2510. SCardContext,
  2511. ContextAndTwoStringCallA.sz1,
  2512. ContextAndTwoStringCallA.sz2);
  2513. break;
  2514. case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
  2515. lReturn = pfnSCardRemoveReaderFromGroupW(
  2516. SCardContext,
  2517. ContextAndTwoStringCallW.sz1,
  2518. ContextAndTwoStringCallW.sz2);
  2519. break;
  2520. }
  2521. }
  2522. //
  2523. // Free up resources used for decode
  2524. //
  2525. if (fASCIICall)
  2526. {
  2527. _TRY_2(ContextAndTwoStringA_Call_Free(hDec, &ContextAndTwoStringCallA))
  2528. }
  2529. else
  2530. {
  2531. _TRY_2(ContextAndTwoStringW_Call_Free(hDec, &ContextAndTwoStringCallW))
  2532. }
  2533. //
  2534. // send the return
  2535. //
  2536. EncodeAndChannelWriteLongReturn(pIoRequestPacket, lReturn);
  2537. Return:
  2538. SafeMesHandleFree(&hDec);
  2539. DC_END_FN();
  2540. return;
  2541. ErrorReturn:
  2542. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2543. goto Return;
  2544. }
  2545. //---------------------------------------------------------------------------------------
  2546. //
  2547. // W32SCard::AllocateAndCopyReaderStateStructsForCall*
  2548. //
  2549. //---------------------------------------------------------------------------------------
  2550. LONG
  2551. W32SCard::AllocateAndCopyReaderStateStructsForCallA(
  2552. IN DWORD cReaders,
  2553. IN ReaderStateA *rgReaderStatesFromDecode,
  2554. OUT LPSCARD_READERSTATE_A *prgReadersStatesForSCardCall)
  2555. {
  2556. DC_BEGIN_FN("W32SCard::AllocateAndCopyReaderStateStructsForCallA");
  2557. LPSCARD_READERSTATE_A rgReadersStatesForSCardCall = NULL;
  2558. DWORD i;
  2559. rgReadersStatesForSCardCall = (LPSCARD_READERSTATE_A)
  2560. MIDL_user_allocate(cReaders * sizeof(SCARD_READERSTATE_A));
  2561. if (rgReadersStatesForSCardCall == NULL)
  2562. {
  2563. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2564. return (SCARD_E_NO_MEMORY);
  2565. }
  2566. memset(rgReadersStatesForSCardCall, 0, cReaders * sizeof(SCARD_READERSTATE_A));
  2567. for (i=0; i<cReaders; i++)
  2568. {
  2569. rgReadersStatesForSCardCall[i].dwCurrentState =
  2570. rgReaderStatesFromDecode[i].Common.dwCurrentState;
  2571. rgReadersStatesForSCardCall[i].dwEventState =
  2572. rgReaderStatesFromDecode[i].Common.dwEventState;
  2573. rgReadersStatesForSCardCall[i].cbAtr =
  2574. rgReaderStatesFromDecode[i].Common.cbAtr;
  2575. memcpy(
  2576. rgReadersStatesForSCardCall[i].rgbAtr,
  2577. rgReaderStatesFromDecode[i].Common.rgbAtr,
  2578. ATR_COPY_SIZE);
  2579. //
  2580. // just reference string in decoded struct instead of copying.
  2581. // this means the decode can't be free'd until the SCard* call is made
  2582. //
  2583. rgReadersStatesForSCardCall[i].szReader =
  2584. rgReaderStatesFromDecode[i].szReader;
  2585. }
  2586. *prgReadersStatesForSCardCall = rgReadersStatesForSCardCall;
  2587. DC_END_FN();
  2588. return (SCARD_S_SUCCESS);
  2589. }
  2590. LONG
  2591. W32SCard::AllocateAndCopyReaderStateStructsForCallW(
  2592. IN DWORD cReaders,
  2593. IN ReaderStateW *rgReaderStatesFromDecode,
  2594. OUT LPSCARD_READERSTATE_W *prgReadersStatesForSCardCall)
  2595. {
  2596. DC_BEGIN_FN("W32SCard::AllocateAndCopyReaderStateStructsForCallW");
  2597. LPSCARD_READERSTATE_W rgReadersStatesForSCardCall = NULL;
  2598. DWORD i;
  2599. rgReadersStatesForSCardCall = (LPSCARD_READERSTATE_W)
  2600. MIDL_user_allocate(cReaders * sizeof(SCARD_READERSTATE_W));
  2601. if (rgReadersStatesForSCardCall == NULL)
  2602. {
  2603. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2604. return (SCARD_E_NO_MEMORY);
  2605. }
  2606. memset(rgReadersStatesForSCardCall, 0, cReaders * sizeof(SCARD_READERSTATE_W));
  2607. for (i=0; i<cReaders; i++)
  2608. {
  2609. rgReadersStatesForSCardCall[i].dwCurrentState =
  2610. rgReaderStatesFromDecode[i].Common.dwCurrentState;
  2611. rgReadersStatesForSCardCall[i].dwEventState =
  2612. rgReaderStatesFromDecode[i].Common.dwEventState;
  2613. rgReadersStatesForSCardCall[i].cbAtr =
  2614. rgReaderStatesFromDecode[i].Common.cbAtr;
  2615. memcpy(
  2616. rgReadersStatesForSCardCall[i].rgbAtr,
  2617. rgReaderStatesFromDecode[i].Common.rgbAtr,
  2618. ATR_COPY_SIZE);
  2619. //
  2620. // just reference string in decoded struct instead of copying.
  2621. // this means the decode can't be free'd until the SCard* call is made
  2622. //
  2623. rgReadersStatesForSCardCall[i].szReader =
  2624. rgReaderStatesFromDecode[i].szReader;
  2625. }
  2626. *prgReadersStatesForSCardCall = rgReadersStatesForSCardCall;
  2627. DC_END_FN();
  2628. return (SCARD_S_SUCCESS);
  2629. }
  2630. //---------------------------------------------------------------------------------------
  2631. //
  2632. // W32SCard::AllocateAndCopyATRMasksForCall
  2633. //
  2634. //---------------------------------------------------------------------------------------
  2635. LONG
  2636. W32SCard::AllocateAndCopyATRMasksForCall(
  2637. IN DWORD cAtrs,
  2638. IN LocateCards_ATRMask *rgATRMasksFromDecode,
  2639. OUT SCARD_ATRMASK **prgATRMasksForCall)
  2640. {
  2641. DC_BEGIN_FN("W32SCard::AllocateAndCopyATRMasksForCall");
  2642. SCARD_ATRMASK *rgATRMasksForCall = NULL;
  2643. DWORD i;
  2644. rgATRMasksForCall = (SCARD_ATRMASK *)
  2645. MIDL_user_allocate(cAtrs * sizeof(SCARD_ATRMASK));
  2646. if (rgATRMasksForCall == NULL)
  2647. {
  2648. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2649. return (SCARD_E_NO_MEMORY);
  2650. }
  2651. memset(rgATRMasksForCall, 0, cAtrs * sizeof(SCARD_ATRMASK));
  2652. for (i=0; i<cAtrs; i++)
  2653. {
  2654. rgATRMasksForCall[i].cbAtr = rgATRMasksFromDecode[i].cbAtr;
  2655. memcpy(
  2656. rgATRMasksForCall[i].rgbAtr,
  2657. rgATRMasksFromDecode[i].rgbAtr,
  2658. ATR_COPY_SIZE);
  2659. memcpy(
  2660. rgATRMasksForCall[i].rgbMask,
  2661. rgATRMasksFromDecode[i].rgbMask,
  2662. ATR_COPY_SIZE);
  2663. }
  2664. *prgATRMasksForCall = rgATRMasksForCall;
  2665. DC_END_FN();
  2666. return (SCARD_S_SUCCESS);
  2667. }
  2668. //---------------------------------------------------------------------------------------
  2669. //
  2670. // W32SCard::AllocateAndCopyReaderStateStructsForReturn*
  2671. //
  2672. //---------------------------------------------------------------------------------------
  2673. LONG
  2674. W32SCard::AllocateAndCopyReaderStateStructsForReturnA(
  2675. IN DWORD cReaders,
  2676. IN LPSCARD_READERSTATE_A rgReaderStatesFromSCardCall,
  2677. OUT ReaderState_Return **prgReaderStatesForReturn)
  2678. {
  2679. DC_BEGIN_FN("W32SCard::AllocateAndCopyReaderStateStructsForReturnA");
  2680. ReaderState_Return *rgReaderStatesForReturn = NULL;
  2681. DWORD i;
  2682. rgReaderStatesForReturn = (ReaderState_Return *)
  2683. MIDL_user_allocate(cReaders * sizeof(ReaderState_Return));
  2684. if (rgReaderStatesForReturn == NULL)
  2685. {
  2686. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2687. return (SCARD_E_NO_MEMORY);
  2688. }
  2689. for (i=0; i<cReaders; i++)
  2690. {
  2691. rgReaderStatesForReturn[i].dwCurrentState =
  2692. rgReaderStatesFromSCardCall[i].dwCurrentState;
  2693. rgReaderStatesForReturn[i].dwEventState =
  2694. rgReaderStatesFromSCardCall[i].dwEventState;
  2695. rgReaderStatesForReturn[i].cbAtr =
  2696. rgReaderStatesFromSCardCall[i].cbAtr;
  2697. memcpy(
  2698. rgReaderStatesForReturn[i].rgbAtr,
  2699. rgReaderStatesFromSCardCall[i].rgbAtr,
  2700. ATR_COPY_SIZE);
  2701. }
  2702. *prgReaderStatesForReturn = rgReaderStatesForReturn;
  2703. DC_END_FN();
  2704. return (SCARD_S_SUCCESS);
  2705. }
  2706. LONG
  2707. W32SCard::AllocateAndCopyReaderStateStructsForReturnW(
  2708. IN DWORD cReaders,
  2709. IN LPSCARD_READERSTATE_W rgReaderStatesFromSCardCall,
  2710. OUT ReaderState_Return **prgReaderStatesForReturn)
  2711. {
  2712. DC_BEGIN_FN("W32SCard::AllocateAndCopyReaderStateStructsForReturnW");
  2713. ReaderState_Return *rgReaderStatesForReturn = NULL;
  2714. DWORD i;
  2715. rgReaderStatesForReturn = (ReaderState_Return *)
  2716. MIDL_user_allocate(cReaders * sizeof(ReaderState_Return));
  2717. if (rgReaderStatesForReturn == NULL)
  2718. {
  2719. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  2720. return (SCARD_E_NO_MEMORY);
  2721. }
  2722. for (i=0; i<cReaders; i++)
  2723. {
  2724. rgReaderStatesForReturn[i].dwCurrentState =
  2725. rgReaderStatesFromSCardCall[i].dwCurrentState;
  2726. rgReaderStatesForReturn[i].dwEventState =
  2727. rgReaderStatesFromSCardCall[i].dwEventState;
  2728. rgReaderStatesForReturn[i].cbAtr =
  2729. rgReaderStatesFromSCardCall[i].cbAtr;
  2730. memcpy(
  2731. rgReaderStatesForReturn[i].rgbAtr,
  2732. rgReaderStatesFromSCardCall[i].rgbAtr,
  2733. ATR_COPY_SIZE);
  2734. }
  2735. *prgReaderStatesForReturn = rgReaderStatesForReturn;
  2736. DC_END_FN();
  2737. return (SCARD_S_SUCCESS);
  2738. }
  2739. //---------------------------------------------------------------------------------------
  2740. //
  2741. // W32SCard::LocateCardsA
  2742. //
  2743. //---------------------------------------------------------------------------------------
  2744. void
  2745. W32SCard::LocateCardsA(
  2746. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  2747. {
  2748. DC_BEGIN_FN("W32SCard::LocateCardsA");
  2749. RPC_STATUS rpcStatus = RPC_S_OK;
  2750. NTSTATUS status = STATUS_SUCCESS;
  2751. char *pbEncodedBuffer = NULL;
  2752. unsigned long cbEncodedBuffer = 0;
  2753. handle_t hDec = 0;
  2754. handle_t hEnc = 0;
  2755. SCARDCONTEXT SCardContext;
  2756. LocateCardsA_Call LocateCardsCallA;
  2757. LocateCards_Return LocateCardsReturn;
  2758. LPSCARD_READERSTATE_A rgReaderStatesA = NULL;
  2759. BOOL fFreeDecode = FALSE;
  2760. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  2761. memset(&LocateCardsReturn, 0, sizeof(LocateCardsReturn));
  2762. LocateCardsReturn.ReturnCode = SCARD_S_SUCCESS;
  2763. //
  2764. // Decode input parameters
  2765. //
  2766. rpcStatus = MesDecodeBufferHandleCreate(
  2767. (char *) (pIoRequest + 1), // bytes are at end of struct
  2768. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  2769. &hDec);
  2770. if (rpcStatus != RPC_S_OK)
  2771. {
  2772. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  2773. status = STATUS_NO_MEMORY;
  2774. goto ErrorReturn;
  2775. }
  2776. //
  2777. // Decode and copy the input params
  2778. //
  2779. memset(&LocateCardsCallA, 0, sizeof(LocateCardsCallA));
  2780. _TRY_status(LocateCardsA_Call_Decode(hDec, &LocateCardsCallA))
  2781. fFreeDecode = TRUE;
  2782. if (sizeof(SCARDCONTEXT) == LocateCardsCallA.Context.cbContext)
  2783. {
  2784. SCardContext = *((SCARDCONTEXT *) LocateCardsCallA.Context.pbContext);
  2785. }
  2786. else
  2787. {
  2788. TRC_ERR((TB, _T("Invalid context sent from server.")));
  2789. LocateCardsReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  2790. }
  2791. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2792. {
  2793. LocateCardsReturn.ReturnCode =
  2794. AllocateAndCopyReaderStateStructsForCallA(
  2795. LocateCardsCallA.cReaders,
  2796. LocateCardsCallA.rgReaderStates,
  2797. &rgReaderStatesA);
  2798. }
  2799. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2800. {
  2801. //
  2802. // Make the call
  2803. //
  2804. LocateCardsReturn.ReturnCode =
  2805. pfnSCardLocateCardsA(
  2806. SCardContext,
  2807. (LPCSTR) LocateCardsCallA.mszCards,
  2808. rgReaderStatesA,
  2809. LocateCardsCallA.cReaders);
  2810. }
  2811. //
  2812. // encode the return
  2813. //
  2814. rpcStatus = MesEncodeDynBufferHandleCreate(
  2815. &pbEncodedBuffer,
  2816. &cbEncodedBuffer,
  2817. &hEnc);
  2818. if (rpcStatus != RPC_S_OK)
  2819. {
  2820. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  2821. status = STATUS_NO_MEMORY;
  2822. goto ErrorReturn;
  2823. }
  2824. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2825. {
  2826. LocateCardsReturn.ReturnCode =
  2827. AllocateAndCopyReaderStateStructsForReturnA(
  2828. LocateCardsCallA.cReaders,
  2829. rgReaderStatesA,
  2830. &(LocateCardsReturn.rgReaderStates));
  2831. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2832. {
  2833. LocateCardsReturn.cReaders = LocateCardsCallA.cReaders;
  2834. }
  2835. }
  2836. _TRY_status(LocateCards_Return_Encode(hEnc, &LocateCardsReturn))
  2837. //
  2838. // Send return
  2839. //
  2840. AllocateAndChannelWriteReplyPacket(
  2841. pIoRequestPacket,
  2842. pbEncodedBuffer,
  2843. cbEncodedBuffer);
  2844. Return:
  2845. if (fFreeDecode)
  2846. {
  2847. _TRY_2(LocateCardsA_Call_Free(hDec, &LocateCardsCallA))
  2848. }
  2849. SafeMesHandleFree(&hDec);
  2850. SafeMesHandleFree(&hEnc);
  2851. MIDL_user_free(rgReaderStatesA);
  2852. MIDL_user_free(LocateCardsReturn.rgReaderStates);
  2853. MIDL_user_free(pbEncodedBuffer);
  2854. DC_END_FN();
  2855. return;
  2856. ErrorReturn:
  2857. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2858. goto Return;
  2859. }
  2860. //---------------------------------------------------------------------------------------
  2861. //
  2862. // W32SCard::LocateCardsW
  2863. //
  2864. //---------------------------------------------------------------------------------------
  2865. void
  2866. W32SCard::LocateCardsW(
  2867. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  2868. {
  2869. DC_BEGIN_FN("W32SCard::LocateCardsW");
  2870. RPC_STATUS rpcStatus = RPC_S_OK;
  2871. NTSTATUS status = STATUS_SUCCESS;
  2872. char *pbEncodedBuffer = NULL;
  2873. unsigned long cbEncodedBuffer = 0;
  2874. handle_t hDec = 0;
  2875. handle_t hEnc = 0;
  2876. SCARDCONTEXT SCardContext;
  2877. LocateCardsW_Call LocateCardsCallW;
  2878. LocateCards_Return LocateCardsReturn;
  2879. LPSCARD_READERSTATE_W rgReaderStatesW = NULL;
  2880. BOOL fFreeDecode = FALSE;
  2881. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  2882. memset(&LocateCardsReturn, 0, sizeof(LocateCardsReturn));
  2883. LocateCardsReturn.ReturnCode = SCARD_S_SUCCESS;
  2884. //
  2885. // Decode input parameters
  2886. //
  2887. rpcStatus = MesDecodeBufferHandleCreate(
  2888. (char *) (pIoRequest + 1), // bytes are at end of struct
  2889. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  2890. &hDec);
  2891. if (rpcStatus != RPC_S_OK)
  2892. {
  2893. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  2894. status = STATUS_NO_MEMORY;
  2895. goto ErrorReturn;
  2896. }
  2897. //
  2898. // Decode and copy the input params
  2899. //
  2900. memset(&LocateCardsCallW, 0, sizeof(LocateCardsCallW));
  2901. _TRY_status(LocateCardsW_Call_Decode(hDec, &LocateCardsCallW))
  2902. fFreeDecode = TRUE;
  2903. if (sizeof(SCARDCONTEXT) == LocateCardsCallW.Context.cbContext)
  2904. {
  2905. SCardContext = *((SCARDCONTEXT *) LocateCardsCallW.Context.pbContext);
  2906. }
  2907. else
  2908. {
  2909. TRC_ERR((TB, _T("Invalid context sent from server.")));
  2910. LocateCardsReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  2911. }
  2912. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2913. {
  2914. LocateCardsReturn.ReturnCode =
  2915. AllocateAndCopyReaderStateStructsForCallW(
  2916. LocateCardsCallW.cReaders,
  2917. LocateCardsCallW.rgReaderStates,
  2918. &rgReaderStatesW);
  2919. }
  2920. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2921. {
  2922. //
  2923. // Make the call
  2924. //
  2925. LocateCardsReturn.ReturnCode =
  2926. pfnSCardLocateCardsW(
  2927. SCardContext,
  2928. (LPCWSTR) LocateCardsCallW.mszCards,
  2929. rgReaderStatesW,
  2930. LocateCardsCallW.cReaders);
  2931. }
  2932. //
  2933. // encode the return
  2934. //
  2935. rpcStatus = MesEncodeDynBufferHandleCreate(
  2936. &pbEncodedBuffer,
  2937. &cbEncodedBuffer,
  2938. &hEnc);
  2939. if (rpcStatus != RPC_S_OK)
  2940. {
  2941. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  2942. status = STATUS_NO_MEMORY;
  2943. goto ErrorReturn;
  2944. }
  2945. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2946. {
  2947. LocateCardsReturn.ReturnCode =
  2948. AllocateAndCopyReaderStateStructsForReturnW(
  2949. LocateCardsCallW.cReaders,
  2950. rgReaderStatesW,
  2951. &(LocateCardsReturn.rgReaderStates));
  2952. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  2953. {
  2954. LocateCardsReturn.cReaders = LocateCardsCallW.cReaders;
  2955. }
  2956. }
  2957. _TRY_status(LocateCards_Return_Encode(hEnc, &LocateCardsReturn))
  2958. //
  2959. // Send return
  2960. //
  2961. AllocateAndChannelWriteReplyPacket(
  2962. pIoRequestPacket,
  2963. pbEncodedBuffer,
  2964. cbEncodedBuffer);
  2965. Return:
  2966. if (fFreeDecode)
  2967. {
  2968. _TRY_2(LocateCardsW_Call_Free(hDec, &LocateCardsCallW))
  2969. }
  2970. SafeMesHandleFree(&hDec);
  2971. SafeMesHandleFree(&hEnc);
  2972. MIDL_user_free(rgReaderStatesW);
  2973. MIDL_user_free(LocateCardsReturn.rgReaderStates);
  2974. MIDL_user_free(pbEncodedBuffer);
  2975. DC_END_FN();
  2976. return;
  2977. ErrorReturn:
  2978. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  2979. goto Return;
  2980. }
  2981. //---------------------------------------------------------------------------------------
  2982. //
  2983. // W32SCard::LocateCardsByATRA
  2984. //
  2985. //---------------------------------------------------------------------------------------
  2986. void
  2987. W32SCard::LocateCardsByATRA(
  2988. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  2989. {
  2990. DC_BEGIN_FN("W32SCard::LocateCardsByATRA");
  2991. RPC_STATUS rpcStatus = RPC_S_OK;
  2992. NTSTATUS status = STATUS_SUCCESS;
  2993. char *pbEncodedBuffer = NULL;
  2994. unsigned long cbEncodedBuffer = 0;
  2995. handle_t hDec = 0;
  2996. handle_t hEnc = 0;
  2997. SCARDCONTEXT SCardContext;
  2998. LocateCardsByATRA_Call LocateCardsByATRCallA;
  2999. LocateCards_Return LocateCardsReturn;
  3000. LPSCARD_READERSTATE_A rgReaderStatesA = NULL;
  3001. SCARD_ATRMASK *rgATRMasksForCall = NULL;
  3002. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3003. memset(&LocateCardsReturn, 0, sizeof(LocateCardsReturn));
  3004. LocateCardsReturn.ReturnCode = SCARD_S_SUCCESS;
  3005. //
  3006. // Decode input parameters
  3007. //
  3008. rpcStatus = MesDecodeBufferHandleCreate(
  3009. (char *) (pIoRequest + 1), // bytes are at end of struct
  3010. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3011. &hDec);
  3012. if (rpcStatus != RPC_S_OK)
  3013. {
  3014. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3015. status = STATUS_NO_MEMORY;
  3016. goto ErrorReturn;
  3017. }
  3018. //
  3019. // Decode and copy the input params
  3020. //
  3021. memset(&LocateCardsByATRCallA, 0, sizeof(LocateCardsByATRCallA));
  3022. _TRY_status(LocateCardsByATRA_Call_Decode(hDec, &LocateCardsByATRCallA))
  3023. if (sizeof(SCARDCONTEXT) == LocateCardsByATRCallA.Context.cbContext)
  3024. {
  3025. SCardContext = *((SCARDCONTEXT *) LocateCardsByATRCallA.Context.pbContext);
  3026. }
  3027. else
  3028. {
  3029. TRC_ERR((TB, _T("Invalid context sent from server.")));
  3030. LocateCardsReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3031. }
  3032. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3033. {
  3034. LocateCardsReturn.ReturnCode =
  3035. AllocateAndCopyATRMasksForCall(
  3036. LocateCardsByATRCallA.cAtrs,
  3037. LocateCardsByATRCallA.rgAtrMasks,
  3038. &rgATRMasksForCall);
  3039. }
  3040. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3041. {
  3042. LocateCardsReturn.ReturnCode =
  3043. AllocateAndCopyReaderStateStructsForCallA(
  3044. LocateCardsByATRCallA.cReaders,
  3045. LocateCardsByATRCallA.rgReaderStates,
  3046. &rgReaderStatesA);
  3047. }
  3048. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3049. {
  3050. //
  3051. // Make the call... if it is available
  3052. //
  3053. #ifndef OS_WINCE
  3054. if (pfnSCardLocateCardsByATRW != NULL)
  3055. #else
  3056. if (pfnSCardLocateCardsByATRA != NULL)
  3057. #endif
  3058. {
  3059. LocateCardsReturn.ReturnCode =
  3060. pfnSCardLocateCardsByATRA(
  3061. SCardContext,
  3062. rgATRMasksForCall,
  3063. LocateCardsByATRCallA.cAtrs,
  3064. rgReaderStatesA,
  3065. LocateCardsByATRCallA.cReaders);
  3066. }
  3067. else
  3068. {
  3069. LocateCardsReturn.ReturnCode = ERROR_CALL_NOT_IMPLEMENTED;
  3070. }
  3071. }
  3072. //
  3073. // encode the return
  3074. //
  3075. rpcStatus = MesEncodeDynBufferHandleCreate(
  3076. &pbEncodedBuffer,
  3077. &cbEncodedBuffer,
  3078. &hEnc);
  3079. if (rpcStatus != RPC_S_OK)
  3080. {
  3081. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3082. status = STATUS_NO_MEMORY;
  3083. goto ErrorReturn;
  3084. }
  3085. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3086. {
  3087. LocateCardsReturn.ReturnCode =
  3088. AllocateAndCopyReaderStateStructsForReturnA(
  3089. LocateCardsByATRCallA.cReaders,
  3090. rgReaderStatesA,
  3091. &(LocateCardsReturn.rgReaderStates));
  3092. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3093. {
  3094. LocateCardsReturn.cReaders = LocateCardsByATRCallA.cReaders;
  3095. }
  3096. }
  3097. _TRY_status(LocateCards_Return_Encode(hEnc, &LocateCardsReturn))
  3098. //
  3099. // Send return
  3100. //
  3101. AllocateAndChannelWriteReplyPacket(
  3102. pIoRequestPacket,
  3103. pbEncodedBuffer,
  3104. cbEncodedBuffer);
  3105. Return:
  3106. SafeMesHandleFree(&hDec);
  3107. SafeMesHandleFree(&hEnc);
  3108. MIDL_user_free(rgReaderStatesA);
  3109. MIDL_user_free(LocateCardsReturn.rgReaderStates);
  3110. MIDL_user_free(rgATRMasksForCall);
  3111. MIDL_user_free(pbEncodedBuffer);
  3112. DC_END_FN();
  3113. return;
  3114. ErrorReturn:
  3115. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3116. goto Return;
  3117. }
  3118. //---------------------------------------------------------------------------------------
  3119. //
  3120. // W32SCard::LocateCardsByATRW
  3121. //
  3122. //---------------------------------------------------------------------------------------
  3123. void
  3124. W32SCard::LocateCardsByATRW(
  3125. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  3126. {
  3127. DC_BEGIN_FN("W32SCard::LocateCardsByATRW");
  3128. RPC_STATUS rpcStatus = RPC_S_OK;
  3129. NTSTATUS status = STATUS_SUCCESS;
  3130. char *pbEncodedBuffer = NULL;
  3131. unsigned long cbEncodedBuffer = 0;
  3132. handle_t hDec = 0;
  3133. handle_t hEnc = 0;
  3134. SCARDCONTEXT SCardContext;
  3135. LocateCardsByATRW_Call LocateCardsByATRCallW;
  3136. LocateCards_Return LocateCardsReturn;
  3137. LPSCARD_READERSTATE_W rgReaderStatesW = NULL;
  3138. SCARD_ATRMASK *rgATRMasksForCall = NULL;
  3139. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3140. memset(&LocateCardsReturn, 0, sizeof(LocateCardsReturn));
  3141. LocateCardsReturn.ReturnCode = SCARD_S_SUCCESS;
  3142. //
  3143. // Decode input parameters
  3144. //
  3145. rpcStatus = MesDecodeBufferHandleCreate(
  3146. (char *) (pIoRequest + 1), // bytes are at end of struct
  3147. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3148. &hDec);
  3149. if (rpcStatus != RPC_S_OK)
  3150. {
  3151. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3152. status = STATUS_NO_MEMORY;
  3153. goto ErrorReturn;
  3154. }
  3155. //
  3156. // Decode and copy the input params
  3157. //
  3158. memset(&LocateCardsByATRCallW, 0, sizeof(LocateCardsByATRCallW));
  3159. _TRY_status(LocateCardsByATRW_Call_Decode(hDec, &LocateCardsByATRCallW))
  3160. if (sizeof(SCARDCONTEXT) == LocateCardsByATRCallW.Context.cbContext)
  3161. {
  3162. SCardContext = *((SCARDCONTEXT *) LocateCardsByATRCallW.Context.pbContext);
  3163. }
  3164. else
  3165. {
  3166. TRC_ERR((TB, _T("Invalid context sent from server.")));
  3167. LocateCardsReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3168. }
  3169. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3170. {
  3171. LocateCardsReturn.ReturnCode =
  3172. AllocateAndCopyATRMasksForCall(
  3173. LocateCardsByATRCallW.cAtrs,
  3174. LocateCardsByATRCallW.rgAtrMasks,
  3175. &rgATRMasksForCall);
  3176. }
  3177. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3178. {
  3179. LocateCardsReturn.ReturnCode =
  3180. AllocateAndCopyReaderStateStructsForCallW(
  3181. LocateCardsByATRCallW.cReaders,
  3182. LocateCardsByATRCallW.rgReaderStates,
  3183. &rgReaderStatesW);
  3184. }
  3185. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3186. {
  3187. //
  3188. // Make the call... if it is available
  3189. //
  3190. if (pfnSCardLocateCardsByATRW != NULL)
  3191. {
  3192. LocateCardsReturn.ReturnCode =
  3193. pfnSCardLocateCardsByATRW(
  3194. SCardContext,
  3195. rgATRMasksForCall,
  3196. LocateCardsByATRCallW.cAtrs,
  3197. rgReaderStatesW,
  3198. LocateCardsByATRCallW.cReaders);
  3199. }
  3200. else
  3201. {
  3202. LocateCardsReturn.ReturnCode = ERROR_CALL_NOT_IMPLEMENTED;
  3203. }
  3204. }
  3205. //
  3206. // encode the return
  3207. //
  3208. rpcStatus = MesEncodeDynBufferHandleCreate(
  3209. &pbEncodedBuffer,
  3210. &cbEncodedBuffer,
  3211. &hEnc);
  3212. if (rpcStatus != RPC_S_OK)
  3213. {
  3214. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3215. status = STATUS_NO_MEMORY;
  3216. goto ErrorReturn;
  3217. }
  3218. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3219. {
  3220. LocateCardsReturn.ReturnCode =
  3221. AllocateAndCopyReaderStateStructsForReturnW(
  3222. LocateCardsByATRCallW.cReaders,
  3223. rgReaderStatesW,
  3224. &(LocateCardsReturn.rgReaderStates));
  3225. if (LocateCardsReturn.ReturnCode == SCARD_S_SUCCESS)
  3226. {
  3227. LocateCardsReturn.cReaders = LocateCardsByATRCallW.cReaders;
  3228. }
  3229. }
  3230. _TRY_status(LocateCards_Return_Encode(hEnc, &LocateCardsReturn))
  3231. //
  3232. // Send return
  3233. //
  3234. AllocateAndChannelWriteReplyPacket(
  3235. pIoRequestPacket,
  3236. pbEncodedBuffer,
  3237. cbEncodedBuffer);
  3238. Return:
  3239. SafeMesHandleFree(&hDec);
  3240. SafeMesHandleFree(&hEnc);
  3241. MIDL_user_free(rgReaderStatesW);
  3242. MIDL_user_free(LocateCardsReturn.rgReaderStates);
  3243. MIDL_user_free(rgATRMasksForCall);
  3244. MIDL_user_free(pbEncodedBuffer);
  3245. DC_END_FN();
  3246. return;
  3247. ErrorReturn:
  3248. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3249. goto Return;
  3250. }
  3251. //---------------------------------------------------------------------------------------
  3252. //
  3253. // W32SCard::GetStatusChangeThreadProc and W32SCard::GetStatusChangeWrapper
  3254. //
  3255. //---------------------------------------------------------------------------------------
  3256. typedef struct _GETSTATUSCHANGESTRUCT
  3257. {
  3258. W32SCard *pTHIS;
  3259. PRDPDR_IOREQUEST_PACKET pIoRequestPacket;
  3260. DWORD dwCallType;
  3261. HMODULE hModExtraRefCount;
  3262. HANDLE hThread;
  3263. } GETSTATUSCHANGESTRUCT;
  3264. DWORD WINAPI
  3265. W32SCard::GetStatusChangeThreadProc(
  3266. LPVOID lpParameter)
  3267. {
  3268. GETSTATUSCHANGESTRUCT *pGetStatusChangeStruct = (GETSTATUSCHANGESTRUCT *) lpParameter;
  3269. W32SCard *pTHIS = pGetStatusChangeStruct->pTHIS;
  3270. HANDLE hThread = NULL;
  3271. HMODULE hModExtraRefCount = NULL;
  3272. //
  3273. // pGetStatusChangeStruct->hThread will be NULL if it wasn't added to the thread
  3274. // list... if it wasn't added to the thread list then just get out.
  3275. //
  3276. if (pGetStatusChangeStruct->hThread == NULL)
  3277. {
  3278. FreeLibraryAndExitThread(pGetStatusChangeStruct->hModExtraRefCount, 0);
  3279. }
  3280. if (pGetStatusChangeStruct->dwCallType == SCARD_IOCTL_GETSTATUSCHANGEA)
  3281. {
  3282. pTHIS->GetStatusChangeA(pGetStatusChangeStruct->pIoRequestPacket);
  3283. }
  3284. else
  3285. {
  3286. pTHIS->GetStatusChangeW(pGetStatusChangeStruct->pIoRequestPacket);
  3287. }
  3288. hThread = pGetStatusChangeStruct->hThread;
  3289. hModExtraRefCount = pGetStatusChangeStruct->hModExtraRefCount;
  3290. MIDL_user_free(pGetStatusChangeStruct);
  3291. pTHIS->RemoveThreadFromList(hThread);
  3292. FreeLibraryAndExitThread(hModExtraRefCount, 0);
  3293. #ifdef OS_WINCE
  3294. return 0;
  3295. #endif
  3296. }
  3297. void
  3298. W32SCard::GetStatusChangeWrapper(
  3299. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  3300. IN DWORD dwCallType)
  3301. {
  3302. DC_BEGIN_FN("W32SCard::GetStatusChangeWrapper");
  3303. LONG lReturn = SCARD_S_SUCCESS;
  3304. HMODULE hModExtraRefCount = NULL;
  3305. GETSTATUSCHANGESTRUCT *pGetStatusChangeStruct = NULL;
  3306. DWORD dwThreadId;
  3307. HANDLE hThread;
  3308. //
  3309. // Get a ref count on our dll so that we know the dll
  3310. // code won't disappear from underneath us. The thread will
  3311. // release this ref count when it exits
  3312. //
  3313. hModExtraRefCount = AddRefCurrentModule();
  3314. if (hModExtraRefCount == NULL)
  3315. {
  3316. lReturn = SCARD_E_UNEXPECTED;
  3317. goto ImmediateReturn;
  3318. }
  3319. //
  3320. // Create a thread to the actual work of the GetStatusChange call
  3321. //
  3322. // Need to do this since the call can block
  3323. //
  3324. pGetStatusChangeStruct = (GETSTATUSCHANGESTRUCT *)
  3325. MIDL_user_allocate(sizeof(GETSTATUSCHANGESTRUCT));
  3326. if (pGetStatusChangeStruct == NULL)
  3327. {
  3328. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  3329. lReturn = SCARD_E_NO_MEMORY;
  3330. goto ImmediateReturn;
  3331. }
  3332. pGetStatusChangeStruct->pTHIS = this;
  3333. pGetStatusChangeStruct->pIoRequestPacket = pIoRequestPacket;
  3334. pGetStatusChangeStruct->dwCallType = dwCallType;
  3335. pGetStatusChangeStruct->hModExtraRefCount = hModExtraRefCount;
  3336. pGetStatusChangeStruct->hThread = NULL;
  3337. EnterCriticalSection(&_csThreadList);
  3338. //
  3339. // If the object is currently being destroyed, then don't create a new thread.
  3340. //
  3341. if (_fInDestructor)
  3342. {
  3343. LeaveCriticalSection(&_csThreadList);
  3344. goto ImmediateReturn;
  3345. }
  3346. hThread = CreateThread(
  3347. NULL,
  3348. 0,
  3349. GetStatusChangeThreadProc,
  3350. pGetStatusChangeStruct,
  3351. CREATE_SUSPENDED,
  3352. &dwThreadId);
  3353. if (hThread == NULL)
  3354. {
  3355. lReturn = SCARD_E_UNEXPECTED;
  3356. LeaveCriticalSection(&_csThreadList);
  3357. goto ImmediateReturn;
  3358. }
  3359. if (!AddThreadToList(hThread))
  3360. {
  3361. LeaveCriticalSection(&_csThreadList);
  3362. ResumeThread(hThread);
  3363. WaitForSingleObject(hThread, INFINITE);
  3364. CloseHandle(hThread);
  3365. goto ImmediateReturn;
  3366. }
  3367. LeaveCriticalSection(&_csThreadList);
  3368. //
  3369. // Now let the thread go
  3370. //
  3371. pGetStatusChangeStruct->hThread = hThread;
  3372. ResumeThread(hThread);
  3373. //
  3374. // Return here and let the thread that was just created
  3375. // do the real work.
  3376. //
  3377. Return:
  3378. DC_END_FN();
  3379. return;
  3380. ImmediateReturn:
  3381. if (hModExtraRefCount != NULL)
  3382. {
  3383. FreeLibrary(hModExtraRefCount);
  3384. }
  3385. MIDL_user_free(pGetStatusChangeStruct);
  3386. EncodeAndChannelWriteLongReturn(
  3387. pIoRequestPacket,
  3388. lReturn);
  3389. goto Return;
  3390. }
  3391. //---------------------------------------------------------------------------------------
  3392. //
  3393. // W32SCard::GetStatusChangeA
  3394. //
  3395. //---------------------------------------------------------------------------------------
  3396. void
  3397. W32SCard::GetStatusChangeA(
  3398. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  3399. {
  3400. DC_BEGIN_FN("W32SCard::GetStatusChangeA");
  3401. RPC_STATUS rpcStatus = RPC_S_OK;
  3402. NTSTATUS status = STATUS_SUCCESS;
  3403. char *pbEncodedBuffer = NULL;
  3404. unsigned long cbEncodedBuffer = 0;
  3405. handle_t hDec = 0;
  3406. handle_t hEnc = 0;
  3407. SCARDCONTEXT SCardContext;
  3408. GetStatusChangeA_Call GetStatusChangeCallA;
  3409. GetStatusChange_Return GetStatusChangeReturn;
  3410. LPSCARD_READERSTATE_A rgReaderStatesA = NULL;
  3411. BOOL fFreeDecode = FALSE;
  3412. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3413. memset(&GetStatusChangeReturn, 0, sizeof(GetStatusChangeReturn));
  3414. GetStatusChangeReturn.ReturnCode = SCARD_S_SUCCESS;
  3415. //
  3416. // Decode input parameters
  3417. //
  3418. rpcStatus = MesDecodeBufferHandleCreate(
  3419. (char *) (pIoRequest + 1), // bytes are at end of struct
  3420. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3421. &hDec);
  3422. if (rpcStatus != RPC_S_OK)
  3423. {
  3424. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3425. status = STATUS_NO_MEMORY;
  3426. goto ErrorReturn;
  3427. }
  3428. //
  3429. // Decode and copy the input params
  3430. //
  3431. memset(&GetStatusChangeCallA, 0, sizeof(GetStatusChangeCallA));
  3432. _TRY_status(GetStatusChangeA_Call_Decode(hDec, &GetStatusChangeCallA))
  3433. fFreeDecode = TRUE;
  3434. if (sizeof(SCARDCONTEXT) == GetStatusChangeCallA.Context.cbContext)
  3435. {
  3436. SCardContext = *((SCARDCONTEXT *) GetStatusChangeCallA.Context.pbContext);
  3437. }
  3438. else
  3439. {
  3440. TRC_ERR((TB, _T("Invalid context sent from server.")));
  3441. GetStatusChangeReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3442. }
  3443. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3444. {
  3445. GetStatusChangeReturn.ReturnCode =
  3446. AllocateAndCopyReaderStateStructsForCallA(
  3447. GetStatusChangeCallA.cReaders,
  3448. GetStatusChangeCallA.rgReaderStates,
  3449. &rgReaderStatesA);
  3450. }
  3451. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3452. {
  3453. //
  3454. // Make the call
  3455. //
  3456. GetStatusChangeReturn.ReturnCode =
  3457. pfnSCardGetStatusChangeA(
  3458. SCardContext,
  3459. GetStatusChangeCallA.dwTimeOut,
  3460. rgReaderStatesA,
  3461. GetStatusChangeCallA.cReaders);
  3462. }
  3463. #ifdef OS_WINCE
  3464. if (GetStatusChangeReturn.ReturnCode != SCARD_S_SUCCESS)
  3465. {
  3466. for (DWORD i=0; i<GetStatusChangeCallA.cReaders; i++)
  3467. {
  3468. if (strcmp(rgReaderStatesA[i].szReader, SCPNP_NOTIFICATIONA) == 0)
  3469. {
  3470. rgReaderStatesA[i].dwEventState = SCARD_STATE_CHANGED | 0x00010000; //the desktop returns this value. what is it defined to?
  3471. GetStatusChangeReturn.ReturnCode = SCARD_S_SUCCESS;
  3472. }
  3473. }
  3474. }
  3475. #endif
  3476. //
  3477. // encode the return
  3478. //
  3479. rpcStatus = MesEncodeDynBufferHandleCreate(
  3480. &pbEncodedBuffer,
  3481. &cbEncodedBuffer,
  3482. &hEnc);
  3483. if (rpcStatus != RPC_S_OK)
  3484. {
  3485. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3486. status = STATUS_NO_MEMORY;
  3487. goto ErrorReturn;
  3488. }
  3489. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3490. {
  3491. GetStatusChangeReturn.ReturnCode
  3492. = AllocateAndCopyReaderStateStructsForReturnA(
  3493. GetStatusChangeCallA.cReaders,
  3494. rgReaderStatesA,
  3495. &(GetStatusChangeReturn.rgReaderStates));
  3496. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3497. {
  3498. GetStatusChangeReturn.cReaders = GetStatusChangeCallA.cReaders;
  3499. }
  3500. }
  3501. _TRY_status(GetStatusChange_Return_Encode(hEnc, &GetStatusChangeReturn))
  3502. //
  3503. // Send return
  3504. //
  3505. AllocateAndChannelWriteReplyPacket(
  3506. pIoRequestPacket,
  3507. pbEncodedBuffer,
  3508. cbEncodedBuffer);
  3509. Return:
  3510. if (fFreeDecode)
  3511. {
  3512. _TRY_2(GetStatusChangeA_Call_Free(hDec, &GetStatusChangeCallA))
  3513. }
  3514. SafeMesHandleFree(&hDec);
  3515. SafeMesHandleFree(&hEnc);
  3516. MIDL_user_free(rgReaderStatesA);
  3517. MIDL_user_free(GetStatusChangeReturn.rgReaderStates);
  3518. MIDL_user_free(pbEncodedBuffer);
  3519. DC_END_FN();
  3520. return;
  3521. ErrorReturn:
  3522. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3523. goto Return;
  3524. }
  3525. //---------------------------------------------------------------------------------------
  3526. //
  3527. // W32SCard::GetStatusChangeW
  3528. //
  3529. //---------------------------------------------------------------------------------------
  3530. void
  3531. W32SCard::GetStatusChangeW(
  3532. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  3533. {
  3534. DC_BEGIN_FN("W32SCard::GetStatusChangeW");
  3535. RPC_STATUS rpcStatus = RPC_S_OK;
  3536. NTSTATUS status = STATUS_SUCCESS;
  3537. char *pbEncodedBuffer = NULL;
  3538. unsigned long cbEncodedBuffer = 0;
  3539. handle_t hDec = 0;
  3540. handle_t hEnc = 0;
  3541. SCARDCONTEXT SCardContext;
  3542. GetStatusChangeW_Call GetStatusChangeCallW;
  3543. GetStatusChange_Return GetStatusChangeReturn;
  3544. LPSCARD_READERSTATE_W rgReaderStatesW = NULL;
  3545. BOOL fFreeDecode = FALSE;
  3546. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3547. memset(&GetStatusChangeReturn, 0, sizeof(GetStatusChangeReturn));
  3548. GetStatusChangeReturn.ReturnCode = SCARD_S_SUCCESS;
  3549. //
  3550. // Decode input parameters
  3551. //
  3552. rpcStatus = MesDecodeBufferHandleCreate(
  3553. (char *) (pIoRequest + 1), // bytes are at end of struct
  3554. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3555. &hDec);
  3556. if (rpcStatus != RPC_S_OK)
  3557. {
  3558. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3559. status = STATUS_NO_MEMORY;
  3560. goto ErrorReturn;
  3561. }
  3562. //
  3563. // Decode and copy the input params
  3564. //
  3565. memset(&GetStatusChangeCallW, 0, sizeof(GetStatusChangeCallW));
  3566. _TRY_status(GetStatusChangeW_Call_Decode(hDec, &GetStatusChangeCallW))
  3567. fFreeDecode = TRUE;
  3568. if (sizeof(SCARDCONTEXT) == GetStatusChangeCallW.Context.cbContext)
  3569. {
  3570. SCardContext = *((SCARDCONTEXT *) GetStatusChangeCallW.Context.pbContext);
  3571. }
  3572. else
  3573. {
  3574. TRC_ERR((TB, _T("Invalid context sent from server.")));
  3575. GetStatusChangeReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3576. }
  3577. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3578. {
  3579. GetStatusChangeReturn.ReturnCode =
  3580. AllocateAndCopyReaderStateStructsForCallW(
  3581. GetStatusChangeCallW.cReaders,
  3582. GetStatusChangeCallW.rgReaderStates,
  3583. &rgReaderStatesW);
  3584. }
  3585. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3586. {
  3587. //
  3588. // Make the call
  3589. //
  3590. GetStatusChangeReturn.ReturnCode =
  3591. pfnSCardGetStatusChangeW(
  3592. SCardContext,
  3593. GetStatusChangeCallW.dwTimeOut,
  3594. rgReaderStatesW,
  3595. GetStatusChangeCallW.cReaders);
  3596. }
  3597. #ifdef OS_WINCE
  3598. if (GetStatusChangeReturn.ReturnCode != SCARD_S_SUCCESS)
  3599. {
  3600. for (DWORD i=0; i<GetStatusChangeCallW.cReaders; i++)
  3601. {
  3602. if (lstrcmp(rgReaderStatesW[i].szReader, SCPNP_NOTIFICATION) == 0)
  3603. {
  3604. rgReaderStatesW[i].dwEventState = SCARD_STATE_CHANGED | 0x00010000; //the desktop returns this value. what is it defined to?
  3605. GetStatusChangeReturn.ReturnCode = SCARD_S_SUCCESS;
  3606. }
  3607. }
  3608. }
  3609. #endif
  3610. //
  3611. // encode the return
  3612. //
  3613. rpcStatus = MesEncodeDynBufferHandleCreate(
  3614. &pbEncodedBuffer,
  3615. &cbEncodedBuffer,
  3616. &hEnc);
  3617. if (rpcStatus != RPC_S_OK)
  3618. {
  3619. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3620. status = STATUS_NO_MEMORY;
  3621. goto ErrorReturn;
  3622. }
  3623. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3624. {
  3625. GetStatusChangeReturn.ReturnCode =
  3626. AllocateAndCopyReaderStateStructsForReturnW(
  3627. GetStatusChangeCallW.cReaders,
  3628. rgReaderStatesW,
  3629. &(GetStatusChangeReturn.rgReaderStates));
  3630. if (GetStatusChangeReturn.ReturnCode == SCARD_S_SUCCESS)
  3631. {
  3632. GetStatusChangeReturn.cReaders = GetStatusChangeCallW.cReaders;
  3633. }
  3634. }
  3635. _TRY_status(GetStatusChange_Return_Encode(hEnc, &GetStatusChangeReturn))
  3636. //
  3637. // Send return
  3638. //
  3639. AllocateAndChannelWriteReplyPacket(
  3640. pIoRequestPacket,
  3641. pbEncodedBuffer,
  3642. cbEncodedBuffer);
  3643. Return:
  3644. if (fFreeDecode)
  3645. {
  3646. _TRY_2(GetStatusChangeW_Call_Free(hDec, &GetStatusChangeCallW))
  3647. }
  3648. SafeMesHandleFree(&hDec);
  3649. SafeMesHandleFree(&hEnc);
  3650. MIDL_user_free(rgReaderStatesW);
  3651. MIDL_user_free(GetStatusChangeReturn.rgReaderStates);
  3652. MIDL_user_free(pbEncodedBuffer);
  3653. DC_END_FN();
  3654. return;
  3655. ErrorReturn:
  3656. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3657. goto Return;
  3658. }
  3659. //---------------------------------------------------------------------------------------
  3660. //
  3661. // W32SCard::Connect
  3662. //
  3663. //---------------------------------------------------------------------------------------
  3664. void
  3665. W32SCard::Connect(
  3666. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  3667. IN DWORD dwCallType)
  3668. {
  3669. DC_BEGIN_FN("W32SCard::Connect");
  3670. RPC_STATUS rpcStatus = RPC_S_OK;
  3671. NTSTATUS status = STATUS_SUCCESS;
  3672. char *pbEncodedBuffer = NULL;
  3673. unsigned long cbEncodedBuffer = 0;
  3674. handle_t hDec = 0;
  3675. handle_t hEnc = 0;
  3676. SCARDCONTEXT SCardContext;
  3677. ConnectA_Call ConnectCallA;
  3678. ConnectW_Call ConnectCallW;
  3679. Connect_Return ConnectReturn;
  3680. BOOL fFreeDecode = FALSE;
  3681. BOOL fFreeHandle = FALSE;
  3682. SCARDHANDLE SCardHandle;
  3683. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  3684. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3685. memset(&ConnectReturn, 0, sizeof(ConnectReturn));
  3686. ConnectReturn.ReturnCode = SCARD_S_SUCCESS;
  3687. //
  3688. // Decode input parameters
  3689. //
  3690. rpcStatus = MesDecodeBufferHandleCreate(
  3691. (char *) (pIoRequest + 1), // bytes are at end of struct
  3692. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3693. &hDec);
  3694. if (rpcStatus != RPC_S_OK)
  3695. {
  3696. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3697. status = STATUS_NO_MEMORY;
  3698. goto ErrorReturn;
  3699. }
  3700. //
  3701. // Decode input params and make the call to SCard*
  3702. //
  3703. if (dwCallType == SCARD_IOCTL_CONNECTA)
  3704. {
  3705. memset(&ConnectCallA, 0, sizeof(ConnectCallA));
  3706. _TRY_status(ConnectA_Call_Decode(hDec, &ConnectCallA))
  3707. fFreeDecode = TRUE;
  3708. if (sizeof(SCARDCONTEXT) != ConnectCallA.Common.Context.cbContext)
  3709. {
  3710. TRC_ERR((TB, _T("Invalid parameter sent from server.")));
  3711. ConnectReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3712. }
  3713. else if (ConnectCallA.szReader == NULL)
  3714. {
  3715. TRC_ERR((TB, _T("Invalid parameter sent from server.")));
  3716. ConnectReturn.ReturnCode = SCARD_E_INVALID_VALUE;
  3717. }
  3718. else
  3719. {
  3720. SCardContext = *((SCARDCONTEXT *) ConnectCallA.Common.Context.pbContext);
  3721. ConnectReturn.ReturnCode =
  3722. pfnSCardConnectA(
  3723. SCardContext,
  3724. ConnectCallA.szReader,
  3725. ConnectCallA.Common.dwShareMode,
  3726. ConnectCallA.Common.dwPreferredProtocols,
  3727. &SCardHandle,
  3728. &ConnectReturn.dwActiveProtocol);
  3729. }
  3730. }
  3731. else
  3732. {
  3733. memset(&ConnectCallW, 0, sizeof(ConnectCallW));
  3734. _TRY_status(ConnectW_Call_Decode(hDec, &ConnectCallW))
  3735. fFreeDecode = TRUE;
  3736. if (sizeof(SCARDCONTEXT) != ConnectCallW.Common.Context.cbContext)
  3737. {
  3738. TRC_ERR((TB, _T("Invalid parameter sent from server.")));
  3739. ConnectReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3740. }
  3741. else if (ConnectCallW.szReader == NULL)
  3742. {
  3743. TRC_ERR((TB, _T("Invalid parameter sent from server.")));
  3744. ConnectReturn.ReturnCode = SCARD_E_INVALID_VALUE;
  3745. }
  3746. else
  3747. {
  3748. SCardContext = *((SCARDCONTEXT *) ConnectCallW.Common.Context.pbContext);
  3749. ConnectReturn.ReturnCode =
  3750. pfnSCardConnectW(
  3751. SCardContext,
  3752. ConnectCallW.szReader,
  3753. ConnectCallW.Common.dwShareMode,
  3754. ConnectCallW.Common.dwPreferredProtocols,
  3755. &SCardHandle,
  3756. &ConnectReturn.dwActiveProtocol);
  3757. }
  3758. }
  3759. if (ConnectReturn.ReturnCode == SCARD_S_SUCCESS)
  3760. {
  3761. ConnectReturn.hCard.Context.pbContext = 0;
  3762. ConnectReturn.hCard.Context.pbContext = NULL;
  3763. ConnectReturn.hCard.pbHandle = (BYTE *) &SCardHandle;
  3764. ConnectReturn.hCard.cbHandle = sizeof(SCARDHANDLE);
  3765. fFreeHandle = TRUE;
  3766. }
  3767. //
  3768. // encode the return
  3769. //
  3770. rpcStatus = MesEncodeDynBufferHandleCreate(
  3771. &pbEncodedBuffer,
  3772. &cbEncodedBuffer,
  3773. &hEnc);
  3774. if (rpcStatus != RPC_S_OK)
  3775. {
  3776. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3777. status = STATUS_NO_MEMORY;
  3778. goto ErrorReturn;
  3779. }
  3780. _TRY_status(Connect_Return_Encode(hEnc, &ConnectReturn))
  3781. //
  3782. // Send return
  3783. //
  3784. AllocateAndChannelWriteReplyPacket(
  3785. pIoRequestPacket,
  3786. pbEncodedBuffer,
  3787. cbEncodedBuffer);
  3788. Return:
  3789. if (fFreeDecode)
  3790. {
  3791. if (dwCallType == SCARD_IOCTL_CONNECTA)
  3792. {
  3793. _TRY_2(ConnectA_Call_Free(hDec, &ConnectCallA))
  3794. }
  3795. else
  3796. {
  3797. _TRY_2(ConnectW_Call_Free(hDec, &ConnectCallW))
  3798. }
  3799. }
  3800. SafeMesHandleFree(&hDec);
  3801. SafeMesHandleFree(&hEnc);
  3802. MIDL_user_free(pbEncodedBuffer);
  3803. MIDL_user_free(pSCardHandleCall);
  3804. DC_END_FN();
  3805. return;
  3806. ErrorReturn:
  3807. if (fFreeHandle)
  3808. {
  3809. pfnSCardDisconnect(SCardHandle, SCARD_LEAVE_CARD);
  3810. }
  3811. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3812. goto Return;
  3813. }
  3814. //---------------------------------------------------------------------------------------
  3815. //
  3816. // W32SCard::Reconnect
  3817. //
  3818. //---------------------------------------------------------------------------------------
  3819. void
  3820. W32SCard::Reconnect(
  3821. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  3822. {
  3823. DC_BEGIN_FN("W32SCard::Reconnect");
  3824. RPC_STATUS rpcStatus = RPC_S_OK;
  3825. NTSTATUS status = STATUS_SUCCESS;
  3826. char *pbEncodedBuffer = NULL;
  3827. unsigned long cbEncodedBuffer = 0;
  3828. handle_t hDec = 0;
  3829. handle_t hEnc = 0;
  3830. SCARDHANDLE SCardHandle;
  3831. Reconnect_Call ReconnectCall;
  3832. Reconnect_Return ReconnectReturn;
  3833. BOOL fFreeDecode = FALSE;
  3834. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  3835. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3836. memset(&ReconnectReturn, 0, sizeof(ReconnectReturn));
  3837. ReconnectReturn.ReturnCode = SCARD_S_SUCCESS;
  3838. //
  3839. // Decode input parameters
  3840. //
  3841. rpcStatus = MesDecodeBufferHandleCreate(
  3842. (char *) (pIoRequest + 1), // bytes are at end of struct
  3843. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3844. &hDec);
  3845. if (rpcStatus != RPC_S_OK)
  3846. {
  3847. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3848. status = STATUS_NO_MEMORY;
  3849. goto ErrorReturn;
  3850. }
  3851. //
  3852. // Decode input params and make the call to SCard*
  3853. //
  3854. memset(&ReconnectCall, 0, sizeof(ReconnectCall));
  3855. _TRY_status(Reconnect_Call_Decode(hDec, &ReconnectCall))
  3856. fFreeDecode = TRUE;
  3857. if (sizeof(SCARDHANDLE) == ReconnectCall.hCard.cbHandle)
  3858. {
  3859. SCardHandle = *((SCARDHANDLE *) ReconnectCall.hCard.pbHandle);
  3860. ReconnectReturn.ReturnCode =
  3861. pfnSCardReconnect(
  3862. SCardHandle,
  3863. ReconnectCall.dwShareMode,
  3864. ReconnectCall.dwPreferredProtocols,
  3865. ReconnectCall.dwInitialization,
  3866. &ReconnectReturn.dwActiveProtocol);
  3867. }
  3868. else
  3869. {
  3870. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  3871. ReconnectReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  3872. }
  3873. //
  3874. // encode the return
  3875. //
  3876. rpcStatus = MesEncodeDynBufferHandleCreate(
  3877. &pbEncodedBuffer,
  3878. &cbEncodedBuffer,
  3879. &hEnc);
  3880. if (rpcStatus != RPC_S_OK)
  3881. {
  3882. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  3883. status = STATUS_NO_MEMORY;
  3884. goto ErrorReturn;
  3885. }
  3886. _TRY_status(Reconnect_Return_Encode(hEnc, &ReconnectReturn))
  3887. //
  3888. // Send return
  3889. //
  3890. AllocateAndChannelWriteReplyPacket(
  3891. pIoRequestPacket,
  3892. pbEncodedBuffer,
  3893. cbEncodedBuffer);
  3894. Return:
  3895. if (fFreeDecode)
  3896. {
  3897. _TRY_2(Reconnect_Call_Free(hDec, &ReconnectCall))
  3898. }
  3899. SafeMesHandleFree(&hDec);
  3900. SafeMesHandleFree(&hEnc);
  3901. MIDL_user_free(pbEncodedBuffer);
  3902. MIDL_user_free(pSCardHandleCall);
  3903. DC_END_FN();
  3904. return;
  3905. ErrorReturn:
  3906. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3907. goto Return;
  3908. }
  3909. //---------------------------------------------------------------------------------------
  3910. //
  3911. // W32SCard::HandleHCardAndDispositionCall
  3912. //
  3913. //---------------------------------------------------------------------------------------
  3914. void
  3915. W32SCard::HandleHCardAndDispositionCall(
  3916. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  3917. IN DWORD dwCallType)
  3918. {
  3919. DC_BEGIN_FN("W32SCard::HandleHCardAndDispositionCall");
  3920. LONG lReturn = SCARD_S_SUCCESS;
  3921. RPC_STATUS rpcStatus = RPC_S_OK;
  3922. NTSTATUS status = STATUS_SUCCESS;
  3923. handle_t hDec = 0;
  3924. SCARDHANDLE SCardHandle;
  3925. DWORD dwDisposition;
  3926. HCardAndDisposition_Call HCardAndDispositionCall;
  3927. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  3928. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  3929. //
  3930. // Decode input parameters
  3931. //
  3932. rpcStatus = MesDecodeBufferHandleCreate(
  3933. (char *) (pIoRequest + 1), // bytes are at end of struct
  3934. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  3935. &hDec);
  3936. if (rpcStatus != RPC_S_OK)
  3937. {
  3938. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  3939. status = STATUS_NO_MEMORY;
  3940. goto ErrorReturn;
  3941. }
  3942. memset(&HCardAndDispositionCall, 0, sizeof(HCardAndDispositionCall));
  3943. _TRY_status(HCardAndDisposition_Call_Decode(hDec, &HCardAndDispositionCall))
  3944. if (sizeof(SCARDHANDLE) == HCardAndDispositionCall.hCard.cbHandle)
  3945. {
  3946. SCardHandle = *((SCARDHANDLE *) HCardAndDispositionCall.hCard.pbHandle);
  3947. dwDisposition = HCardAndDispositionCall.dwDisposition;
  3948. }
  3949. else
  3950. {
  3951. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  3952. lReturn = SCARD_E_INVALID_PARAMETER;
  3953. }
  3954. //
  3955. // Free up resources used by decode
  3956. //
  3957. _TRY_2(HCardAndDisposition_Call_Free(hDec, &HCardAndDispositionCall))
  3958. SafeMesHandleFree(&hDec);
  3959. if (lReturn == SCARD_S_SUCCESS)
  3960. {
  3961. //
  3962. // Make SCard subsystem call
  3963. //
  3964. switch(dwCallType)
  3965. {
  3966. case SCARD_IOCTL_DISCONNECT:
  3967. lReturn = pfnSCardDisconnect(SCardHandle, dwDisposition);
  3968. break;
  3969. case SCARD_IOCTL_BEGINTRANSACTION:
  3970. lReturn = pfnSCardBeginTransaction(SCardHandle);
  3971. break;
  3972. case SCARD_IOCTL_ENDTRANSACTION:
  3973. lReturn = pfnSCardEndTransaction(SCardHandle, dwDisposition);
  3974. break;
  3975. }
  3976. }
  3977. //
  3978. // encode and write the return
  3979. //
  3980. EncodeAndChannelWriteLongReturn(pIoRequestPacket, lReturn);
  3981. Return:
  3982. MIDL_user_free(pSCardHandleCall);
  3983. DC_END_FN();
  3984. return;
  3985. ErrorReturn:
  3986. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  3987. goto Return;
  3988. }
  3989. #ifndef OS_WINCE
  3990. //---------------------------------------------------------------------------------------
  3991. //
  3992. // W32SCard::State
  3993. //
  3994. //---------------------------------------------------------------------------------------
  3995. void
  3996. W32SCard::State(
  3997. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  3998. {
  3999. DC_BEGIN_FN("W32SCard::State");
  4000. RPC_STATUS rpcStatus = RPC_S_OK;
  4001. NTSTATUS status = STATUS_SUCCESS;
  4002. char *pbEncodedBuffer = NULL;
  4003. unsigned long cbEncodedBuffer = 0;
  4004. handle_t hDec = 0;
  4005. handle_t hEnc = 0;
  4006. SCARDCONTEXT SCardContext;
  4007. SCARDHANDLE SCardHandle;
  4008. State_Call StateCall;
  4009. State_Return StateReturn;
  4010. BOOL fFreeDecode = FALSE;
  4011. DWORD cbAtr = 0;
  4012. BOOL fDoAllocationLocally = FALSE;
  4013. LPBYTE pbAtr = NULL;
  4014. BOOL fFree = FALSE;
  4015. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  4016. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  4017. memset(&StateReturn, 0, sizeof(StateReturn));
  4018. StateReturn.ReturnCode = SCARD_S_SUCCESS;
  4019. //
  4020. // Decode input parameters
  4021. //
  4022. rpcStatus = MesDecodeBufferHandleCreate(
  4023. (char *) (pIoRequest + 1), // bytes are at end of struct
  4024. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  4025. &hDec);
  4026. if (rpcStatus != RPC_S_OK)
  4027. {
  4028. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  4029. status = STATUS_NO_MEMORY;
  4030. goto ErrorReturn;
  4031. }
  4032. //
  4033. // Decode input params
  4034. //
  4035. memset(&StateCall, 0, sizeof(StateCall));
  4036. _TRY_status(State_Call_Decode(hDec, &StateCall))
  4037. fFreeDecode = TRUE;
  4038. if (sizeof(SCARDHANDLE) == StateCall.hCard.cbHandle)
  4039. {
  4040. SCardContext = *((SCARDCONTEXT *) StateCall.hCard.Context.pbContext);
  4041. SCardHandle = *((SCARDHANDLE *) StateCall.hCard.pbHandle);
  4042. cbAtr = StateCall.cbAtrLen;
  4043. }
  4044. else
  4045. {
  4046. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  4047. StateReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  4048. }
  4049. if (StateReturn.ReturnCode == SCARD_S_SUCCESS)
  4050. {
  4051. //
  4052. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  4053. //
  4054. fDoAllocationLocally =
  4055. (!StateCall.fpbAtrIsNULL &&
  4056. (cbAtr != SCARD_AUTOALLOCATE));
  4057. if (fDoAllocationLocally)
  4058. {
  4059. pbAtr = (LPBYTE) MIDL_user_allocate(cbAtr * sizeof(BYTE));
  4060. if (pbAtr == NULL)
  4061. {
  4062. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4063. StateReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4064. }
  4065. }
  4066. }
  4067. if (StateReturn.ReturnCode == SCARD_S_SUCCESS)
  4068. {
  4069. //
  4070. // Make the call
  4071. //
  4072. StateReturn.ReturnCode =
  4073. pfnSCardState(
  4074. SCardHandle,
  4075. &StateReturn.dwState,
  4076. &StateReturn.dwProtocol,
  4077. (cbAtr == SCARD_AUTOALLOCATE) ? (LPBYTE) &pbAtr : pbAtr,
  4078. &cbAtr);
  4079. }
  4080. if (StateReturn.ReturnCode == SCARD_S_SUCCESS)
  4081. {
  4082. StateReturn.cbAtrLen = cbAtr;
  4083. //
  4084. // If we are just returning the byte count then send back a junk buffer
  4085. //
  4086. if (pbAtr == NULL)
  4087. {
  4088. StateReturn.rgAtr = (BYTE *) MIDL_user_allocate(cbAtr);
  4089. if (StateReturn.rgAtr == NULL)
  4090. {
  4091. status = STATUS_NO_MEMORY;
  4092. goto ErrorReturn;
  4093. }
  4094. fFree = TRUE;
  4095. memset(StateReturn.rgAtr, 0, cbAtr);
  4096. }
  4097. else
  4098. {
  4099. StateReturn.rgAtr = pbAtr;
  4100. }
  4101. }
  4102. //
  4103. // encode the return
  4104. //
  4105. rpcStatus = MesEncodeDynBufferHandleCreate(
  4106. &pbEncodedBuffer,
  4107. &cbEncodedBuffer,
  4108. &hEnc);
  4109. if (rpcStatus != RPC_S_OK)
  4110. {
  4111. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  4112. status = STATUS_NO_MEMORY;
  4113. goto ErrorReturn;
  4114. }
  4115. _TRY_status(State_Return_Encode(hEnc, &StateReturn))
  4116. //
  4117. // Send return
  4118. //
  4119. AllocateAndChannelWriteReplyPacket(
  4120. pIoRequestPacket,
  4121. pbEncodedBuffer,
  4122. cbEncodedBuffer);
  4123. Return:
  4124. if (fFree)
  4125. {
  4126. MIDL_user_free(StateReturn.rgAtr);
  4127. }
  4128. if (pbAtr != NULL)
  4129. {
  4130. //
  4131. // Check to see whether we allocated or SCard allcated for us
  4132. //
  4133. if (fDoAllocationLocally)
  4134. {
  4135. MIDL_user_free(pbAtr);
  4136. }
  4137. else
  4138. {
  4139. pfnSCardFreeMemory(SCardContext, pbAtr);
  4140. }
  4141. }
  4142. if (fFreeDecode)
  4143. {
  4144. _TRY_2(State_Call_Free(hDec, &StateCall))
  4145. }
  4146. SafeMesHandleFree(&hDec);
  4147. SafeMesHandleFree(&hEnc);
  4148. MIDL_user_free(pbEncodedBuffer);
  4149. MIDL_user_free(pSCardHandleCall);
  4150. DC_END_FN();
  4151. return;
  4152. ErrorReturn:
  4153. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  4154. goto Return;
  4155. }
  4156. #endif
  4157. //---------------------------------------------------------------------------------------
  4158. //
  4159. // W32SCard::Status
  4160. //
  4161. //---------------------------------------------------------------------------------------
  4162. void
  4163. W32SCard::Status(
  4164. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  4165. IN DWORD dwCallType)
  4166. {
  4167. DC_BEGIN_FN("W32SCard::Status");
  4168. RPC_STATUS rpcStatus = RPC_S_OK;
  4169. NTSTATUS status = STATUS_SUCCESS;
  4170. char *pbEncodedBuffer = NULL;
  4171. unsigned long cbEncodedBuffer = 0;
  4172. handle_t hDec = 0;
  4173. handle_t hEnc = 0;
  4174. SCARDCONTEXT SCardContext;
  4175. SCARDHANDLE SCardHandle;
  4176. Status_Call StatusCall;
  4177. Status_Return StatusReturn;
  4178. BOOL fFreeDecode = FALSE;
  4179. DWORD cchReaderLen = 0;
  4180. BOOL fDoAllocationLocally = FALSE;
  4181. LPBYTE psz = NULL;
  4182. BOOL fFree = FALSE;
  4183. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  4184. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  4185. memset(&StatusReturn, 0, sizeof(StatusReturn));
  4186. StatusReturn.ReturnCode = SCARD_S_SUCCESS;
  4187. //
  4188. // Decode input parameters
  4189. //
  4190. rpcStatus = MesDecodeBufferHandleCreate(
  4191. (char *) (pIoRequest + 1), // bytes are at end of struct
  4192. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  4193. &hDec);
  4194. if (rpcStatus != RPC_S_OK)
  4195. {
  4196. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  4197. status = STATUS_NO_MEMORY;
  4198. goto ErrorReturn;
  4199. }
  4200. memset(&StatusCall, 0, sizeof(StatusCall));
  4201. _TRY_status(Status_Call_Decode(hDec, &StatusCall))
  4202. fFreeDecode = TRUE;
  4203. if (sizeof(SCARDHANDLE) == StatusCall.hCard.cbHandle)
  4204. {
  4205. SCardContext = *((SCARDCONTEXT *) StatusCall.hCard.Context.pbContext);
  4206. SCardHandle = *((SCARDHANDLE *) StatusCall.hCard.pbHandle);
  4207. cchReaderLen = StatusCall.cchReaderLen;
  4208. }
  4209. else
  4210. {
  4211. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  4212. StatusReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  4213. }
  4214. if (StatusReturn.ReturnCode == SCARD_S_SUCCESS)
  4215. {
  4216. //
  4217. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  4218. //
  4219. fDoAllocationLocally =
  4220. (!StatusCall.fmszReaderNamesIsNULL &&
  4221. (cchReaderLen != SCARD_AUTOALLOCATE));
  4222. #ifdef OS_WINCE
  4223. if (!fDoAllocationLocally)
  4224. {
  4225. StatusReturn.cbAtrLen = ATR_SIZE;
  4226. StatusReturn.ReturnCode =
  4227. pfnSCardStatusW(
  4228. SCardHandle,
  4229. NULL,
  4230. &cchReaderLen,
  4231. &StatusReturn.dwState,
  4232. &StatusReturn.dwProtocol,
  4233. (LPBYTE) &StatusReturn.pbAtr,
  4234. &StatusReturn.cbAtrLen);
  4235. if ((StatusReturn.ReturnCode == SCARD_S_SUCCESS) && (cchReaderLen > 0))
  4236. fDoAllocationLocally = TRUE;
  4237. else
  4238. {
  4239. TRC_ERR((TB, _T("SCardStatusW failed")));
  4240. status = STATUS_UNSUCCESSFUL;
  4241. goto ErrorReturn;
  4242. }
  4243. }
  4244. #endif
  4245. if (fDoAllocationLocally)
  4246. {
  4247. DWORD dwCharSize = (dwCallType == SCARD_IOCTL_STATUSA) ?
  4248. sizeof(char) : sizeof(WCHAR);
  4249. psz = (LPBYTE) MIDL_user_allocate(cchReaderLen * dwCharSize);
  4250. if (psz == NULL)
  4251. {
  4252. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4253. StatusReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4254. }
  4255. }
  4256. }
  4257. if (StatusReturn.ReturnCode == SCARD_S_SUCCESS)
  4258. {
  4259. //
  4260. // Make the call
  4261. //
  4262. StatusReturn.cbAtrLen = ATR_SIZE;
  4263. if (dwCallType == SCARD_IOCTL_STATUSA)
  4264. {
  4265. StatusReturn.ReturnCode =
  4266. pfnSCardStatusA(
  4267. SCardHandle,
  4268. (cchReaderLen == SCARD_AUTOALLOCATE) ? (LPSTR) &psz : (LPSTR) psz,
  4269. &cchReaderLen,
  4270. &StatusReturn.dwState,
  4271. &StatusReturn.dwProtocol,
  4272. (LPBYTE) &StatusReturn.pbAtr,
  4273. &StatusReturn.cbAtrLen);
  4274. if (StatusReturn.ReturnCode == SCARD_S_SUCCESS)
  4275. {
  4276. StatusReturn.cBytes = cchReaderLen * sizeof(char);
  4277. StatusReturn.mszReaderNames = psz;
  4278. }
  4279. }
  4280. else
  4281. {
  4282. StatusReturn.ReturnCode =
  4283. pfnSCardStatusW(
  4284. SCardHandle,
  4285. (cchReaderLen == SCARD_AUTOALLOCATE) ? (LPWSTR) &psz : (LPWSTR) psz,
  4286. &cchReaderLen,
  4287. &StatusReturn.dwState,
  4288. &StatusReturn.dwProtocol,
  4289. (LPBYTE) &StatusReturn.pbAtr,
  4290. &StatusReturn.cbAtrLen);
  4291. if (StatusReturn.ReturnCode == SCARD_S_SUCCESS)
  4292. {
  4293. StatusReturn.cBytes = cchReaderLen * sizeof(WCHAR);
  4294. StatusReturn.mszReaderNames = psz;
  4295. }
  4296. }
  4297. }
  4298. if (StatusReturn.ReturnCode == SCARD_S_SUCCESS)
  4299. {
  4300. //
  4301. // If we are just returning the byte count then send back a junk buffer
  4302. //
  4303. if (StatusReturn.mszReaderNames == NULL)
  4304. {
  4305. StatusReturn.mszReaderNames = (BYTE *) MIDL_user_allocate(StatusReturn.cBytes);
  4306. if (StatusReturn.mszReaderNames == NULL)
  4307. {
  4308. status = STATUS_NO_MEMORY;
  4309. goto ErrorReturn;
  4310. }
  4311. fFree = TRUE;
  4312. memset(StatusReturn.mszReaderNames, 0, StatusReturn.cBytes);
  4313. }
  4314. }
  4315. //
  4316. // encode the return
  4317. //
  4318. rpcStatus = MesEncodeDynBufferHandleCreate(
  4319. &pbEncodedBuffer,
  4320. &cbEncodedBuffer,
  4321. &hEnc);
  4322. if (rpcStatus != RPC_S_OK)
  4323. {
  4324. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  4325. status = STATUS_NO_MEMORY;
  4326. goto ErrorReturn;
  4327. }
  4328. _TRY_status(Status_Return_Encode(hEnc, &StatusReturn))
  4329. //
  4330. // Send return
  4331. //
  4332. AllocateAndChannelWriteReplyPacket(
  4333. pIoRequestPacket,
  4334. pbEncodedBuffer,
  4335. cbEncodedBuffer);
  4336. Return:
  4337. if (fFree)
  4338. {
  4339. MIDL_user_free(StatusReturn.mszReaderNames);
  4340. }
  4341. if (psz != NULL)
  4342. {
  4343. //
  4344. // Check to see whether we allocated or SCard allcated for us
  4345. //
  4346. if (fDoAllocationLocally)
  4347. {
  4348. MIDL_user_free(psz);
  4349. }
  4350. else
  4351. {
  4352. #ifndef OS_WINCE
  4353. pfnSCardFreeMemory(SCardContext, psz);
  4354. #else
  4355. TRC_ASSERT(FALSE, (TB, _T("Shouldnt get here")));
  4356. #endif
  4357. }
  4358. }
  4359. if (fFreeDecode)
  4360. {
  4361. _TRY_2(Status_Call_Free(hDec, &StatusCall))
  4362. }
  4363. SafeMesHandleFree(&hDec);
  4364. SafeMesHandleFree(&hEnc);
  4365. MIDL_user_free(pbEncodedBuffer);
  4366. MIDL_user_free(pSCardHandleCall);
  4367. DC_END_FN();
  4368. return;
  4369. ErrorReturn:
  4370. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  4371. goto Return;
  4372. }
  4373. //---------------------------------------------------------------------------------------
  4374. //
  4375. // W32SCard::Transmit
  4376. //
  4377. //---------------------------------------------------------------------------------------
  4378. void
  4379. W32SCard::Transmit(
  4380. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  4381. {
  4382. DC_BEGIN_FN("W32SCard::Transmit");
  4383. RPC_STATUS rpcStatus = RPC_S_OK;
  4384. NTSTATUS status = STATUS_SUCCESS;
  4385. char *pbEncodedBuffer = NULL;
  4386. unsigned long cbEncodedBuffer = 0;
  4387. handle_t hDec = 0;
  4388. handle_t hEnc = 0;
  4389. SCARDCONTEXT SCardContext;
  4390. SCARDHANDLE SCardHandle;
  4391. Transmit_Call TransmitCall;
  4392. Transmit_Return TransmitReturn;
  4393. BOOL fFreeDecode = FALSE;
  4394. BOOL fDoAllocationLocally = FALSE;
  4395. SCARD_IO_REQUEST *pSendPci = NULL;
  4396. SCARD_IO_REQUEST *pRecvPci = NULL;
  4397. SCardIO_Request *pReturnRecvPci = NULL;
  4398. DWORD cbRecvLength;
  4399. BYTE *pbRecvBuffer = NULL;
  4400. BOOL fFree = FALSE;
  4401. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  4402. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  4403. memset(&TransmitReturn, 0, sizeof(TransmitReturn));
  4404. TransmitReturn.ReturnCode = SCARD_S_SUCCESS;
  4405. //
  4406. // Decode input parameters
  4407. //
  4408. rpcStatus = MesDecodeBufferHandleCreate(
  4409. (char *) (pIoRequest + 1), // bytes are at end of struct
  4410. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  4411. &hDec);
  4412. if (rpcStatus != RPC_S_OK)
  4413. {
  4414. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  4415. status = STATUS_NO_MEMORY;
  4416. goto ErrorReturn;
  4417. }
  4418. //
  4419. // Decode and setup the input params
  4420. //
  4421. memset(&TransmitCall, 0, sizeof(TransmitCall));
  4422. _TRY_status(Transmit_Call_Decode(hDec, &TransmitCall))
  4423. fFreeDecode = TRUE;
  4424. if (sizeof(SCARDHANDLE) == TransmitCall.hCard.cbHandle)
  4425. {
  4426. SCardContext = *((SCARDCONTEXT *) TransmitCall.hCard.Context.pbContext);
  4427. SCardHandle = *((SCARDHANDLE *) TransmitCall.hCard.pbHandle);
  4428. cbRecvLength = TransmitCall.cbRecvLength;
  4429. }
  4430. else
  4431. {
  4432. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  4433. TransmitReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  4434. }
  4435. //
  4436. // setup the pSendPci param of the call based on callers input
  4437. //
  4438. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4439. {
  4440. pSendPci = (LPSCARD_IO_REQUEST)
  4441. MIDL_user_allocate( sizeof(SCARD_IO_REQUEST) +
  4442. TransmitCall.ioSendPci.cbExtraBytes);
  4443. if (pSendPci != NULL)
  4444. {
  4445. pSendPci->dwProtocol = TransmitCall.ioSendPci.dwProtocol;
  4446. pSendPci->cbPciLength = sizeof(SCARD_IO_REQUEST) +
  4447. TransmitCall.ioSendPci.cbExtraBytes;
  4448. if (TransmitCall.ioSendPci.cbExtraBytes != 0)
  4449. {
  4450. memcpy(
  4451. ((LPBYTE) pSendPci) + sizeof(SCARD_IO_REQUEST),
  4452. TransmitCall.ioSendPci.pbExtraBytes,
  4453. TransmitCall.ioSendPci.cbExtraBytes);
  4454. }
  4455. }
  4456. else
  4457. {
  4458. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4459. TransmitReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4460. }
  4461. }
  4462. //
  4463. // setup the pRecvPci param of the call based on callers input
  4464. //
  4465. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4466. {
  4467. if (TransmitCall.pioRecvPci != NULL)
  4468. {
  4469. pRecvPci = (LPSCARD_IO_REQUEST)
  4470. MIDL_user_allocate( sizeof(SCARD_IO_REQUEST) +
  4471. TransmitCall.pioRecvPci->cbExtraBytes);
  4472. if (pRecvPci != NULL)
  4473. {
  4474. pRecvPci->dwProtocol = TransmitCall.pioRecvPci->dwProtocol;
  4475. pRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST) +
  4476. TransmitCall.pioRecvPci->cbExtraBytes;
  4477. if (TransmitCall.ioSendPci.cbExtraBytes != 0)
  4478. {
  4479. memcpy(
  4480. ((LPBYTE) pRecvPci) + sizeof(SCARD_IO_REQUEST),
  4481. TransmitCall.pioRecvPci->pbExtraBytes,
  4482. TransmitCall.pioRecvPci->cbExtraBytes);
  4483. }
  4484. }
  4485. else
  4486. {
  4487. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4488. TransmitReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4489. }
  4490. }
  4491. }
  4492. //
  4493. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  4494. //
  4495. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4496. {
  4497. fDoAllocationLocally =
  4498. (!TransmitCall.fpbRecvBufferIsNULL &&
  4499. (cbRecvLength != SCARD_AUTOALLOCATE));
  4500. #ifdef OS_WINCE
  4501. if (!fDoAllocationLocally)
  4502. {
  4503. TransmitReturn.ReturnCode =
  4504. pfnSCardTransmit(
  4505. SCardHandle,
  4506. pSendPci,
  4507. TransmitCall.pbSendBuffer,
  4508. TransmitCall.cbSendLength,
  4509. pRecvPci,
  4510. NULL,
  4511. &cbRecvLength);
  4512. if ((TransmitReturn.ReturnCode == SCARD_S_SUCCESS) && (cbRecvLength > 0))
  4513. fDoAllocationLocally = TRUE;
  4514. else
  4515. {
  4516. TRC_ERR((TB, _T("SCardTransmit failed")));
  4517. status = STATUS_UNSUCCESSFUL;
  4518. goto ErrorReturn;
  4519. }
  4520. }
  4521. #endif
  4522. if (fDoAllocationLocally)
  4523. {
  4524. pbRecvBuffer = (LPBYTE) MIDL_user_allocate(cbRecvLength * sizeof(BYTE));
  4525. if (pbRecvBuffer == NULL)
  4526. {
  4527. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4528. TransmitReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4529. }
  4530. }
  4531. }
  4532. //
  4533. // Make the call
  4534. //
  4535. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4536. {
  4537. TransmitReturn.ReturnCode =
  4538. pfnSCardTransmit(
  4539. SCardHandle,
  4540. pSendPci,
  4541. TransmitCall.pbSendBuffer,
  4542. TransmitCall.cbSendLength,
  4543. pRecvPci,
  4544. (cbRecvLength == SCARD_AUTOALLOCATE) ?
  4545. (LPBYTE) &pbRecvBuffer : pbRecvBuffer,
  4546. &cbRecvLength);
  4547. }
  4548. //
  4549. // copy over the output the return struct to be encoded
  4550. //
  4551. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4552. {
  4553. if (pRecvPci != NULL)
  4554. {
  4555. //
  4556. // Allocate space for struct plus the extra bytes at the end of it
  4557. // if needed
  4558. //
  4559. pReturnRecvPci = (SCardIO_Request *)
  4560. MIDL_user_allocate(
  4561. sizeof(SCardIO_Request) +
  4562. pRecvPci->cbPciLength);
  4563. if (pReturnRecvPci != NULL)
  4564. {
  4565. pReturnRecvPci->dwProtocol = pRecvPci->dwProtocol;
  4566. pReturnRecvPci->cbExtraBytes = pRecvPci->cbPciLength -
  4567. sizeof(SCARD_IO_REQUEST);
  4568. if (pReturnRecvPci->cbExtraBytes != 0)
  4569. {
  4570. //
  4571. // put bytes at end of struct since we allocated enough space for it
  4572. //
  4573. memcpy(
  4574. ((LPBYTE) pReturnRecvPci) + sizeof(SCardIO_Request),
  4575. ((LPBYTE) pRecvPci) + sizeof(SCARD_IO_REQUEST),
  4576. pReturnRecvPci->cbExtraBytes);
  4577. }
  4578. TransmitReturn.pioRecvPci = pReturnRecvPci;
  4579. }
  4580. else
  4581. {
  4582. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4583. TransmitReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4584. }
  4585. }
  4586. else
  4587. {
  4588. TransmitReturn.pioRecvPci = NULL;
  4589. }
  4590. TransmitReturn.cbRecvLength = cbRecvLength;
  4591. TransmitReturn.pbRecvBuffer = pbRecvBuffer;
  4592. }
  4593. if (TransmitReturn.ReturnCode == SCARD_S_SUCCESS)
  4594. {
  4595. //
  4596. // If we are just returning the byte count then send back a junk buffer
  4597. //
  4598. if (TransmitReturn.pbRecvBuffer == NULL)
  4599. {
  4600. TransmitReturn.pbRecvBuffer = (BYTE *) MIDL_user_allocate(TransmitReturn.cbRecvLength);
  4601. if (TransmitReturn.pbRecvBuffer == NULL)
  4602. {
  4603. status = STATUS_NO_MEMORY;
  4604. goto ErrorReturn;
  4605. }
  4606. fFree = TRUE;
  4607. memset(TransmitReturn.pbRecvBuffer, 0, TransmitReturn.cbRecvLength);
  4608. }
  4609. }
  4610. //
  4611. // encode the return
  4612. //
  4613. rpcStatus = MesEncodeDynBufferHandleCreate(
  4614. &pbEncodedBuffer,
  4615. &cbEncodedBuffer,
  4616. &hEnc);
  4617. if (rpcStatus != RPC_S_OK)
  4618. {
  4619. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  4620. status = STATUS_NO_MEMORY;
  4621. goto ErrorReturn;
  4622. }
  4623. _TRY_status(Transmit_Return_Encode(hEnc, &TransmitReturn))
  4624. //
  4625. // Send return
  4626. //
  4627. AllocateAndChannelWriteReplyPacket(
  4628. pIoRequestPacket,
  4629. pbEncodedBuffer,
  4630. cbEncodedBuffer);
  4631. Return:
  4632. if (fFree)
  4633. {
  4634. MIDL_user_free(TransmitReturn.pbRecvBuffer);
  4635. }
  4636. MIDL_user_free(pSendPci);
  4637. MIDL_user_free(pRecvPci);
  4638. MIDL_user_free(pReturnRecvPci);
  4639. if (pbRecvBuffer != NULL)
  4640. {
  4641. //
  4642. // Check to see whether we allocated or SCard allcated for us
  4643. //
  4644. if (fDoAllocationLocally)
  4645. {
  4646. MIDL_user_free(pbRecvBuffer);
  4647. }
  4648. else
  4649. {
  4650. #ifndef OS_WINCE
  4651. pfnSCardFreeMemory(SCardContext, pbRecvBuffer);
  4652. #else
  4653. TRC_ASSERT(FALSE, (TB, _T("Shouldnt get here")));
  4654. #endif
  4655. }
  4656. }
  4657. if (fFreeDecode)
  4658. {
  4659. _TRY_2(Transmit_Call_Free(hDec, &TransmitCall))
  4660. }
  4661. SafeMesHandleFree(&hDec);
  4662. SafeMesHandleFree(&hEnc);
  4663. MIDL_user_free(pbEncodedBuffer);
  4664. MIDL_user_free(pSCardHandleCall);
  4665. DC_END_FN();
  4666. return;
  4667. ErrorReturn:
  4668. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  4669. goto Return;
  4670. }
  4671. //---------------------------------------------------------------------------------------
  4672. //
  4673. // W32SCard::Control
  4674. //
  4675. //---------------------------------------------------------------------------------------
  4676. void
  4677. W32SCard::Control(
  4678. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  4679. {
  4680. DC_BEGIN_FN("W32SCard::Control");
  4681. RPC_STATUS rpcStatus = RPC_S_OK;
  4682. NTSTATUS status = STATUS_SUCCESS;
  4683. char *pbEncodedBuffer = NULL;
  4684. unsigned long cbEncodedBuffer = 0;
  4685. handle_t hDec = 0;
  4686. handle_t hEnc = 0;
  4687. SCARDCONTEXT SCardContext;
  4688. SCARDHANDLE SCardHandle;
  4689. Control_Call ControlCall;
  4690. Control_Return ControlReturn;
  4691. BOOL fFreeDecode = FALSE;
  4692. BOOL fDoAllocationLocally = FALSE;
  4693. DWORD cbBytesReturned = 0;
  4694. LPVOID lpOutBuffer = NULL;
  4695. BOOL fFree = FALSE;
  4696. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  4697. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  4698. memset(&ControlReturn, 0, sizeof(ControlReturn));
  4699. ControlReturn.ReturnCode = SCARD_S_SUCCESS;
  4700. //
  4701. // Decode input parameters
  4702. //
  4703. rpcStatus = MesDecodeBufferHandleCreate(
  4704. (char *) (pIoRequest + 1), // bytes are at end of struct
  4705. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  4706. &hDec);
  4707. if (rpcStatus != RPC_S_OK)
  4708. {
  4709. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  4710. status = STATUS_NO_MEMORY;
  4711. goto ErrorReturn;
  4712. }
  4713. //
  4714. // Decode input params
  4715. //
  4716. memset(&ControlCall, 0, sizeof(ControlCall));
  4717. _TRY_status(Control_Call_Decode(hDec, &ControlCall))
  4718. fFreeDecode = TRUE;
  4719. if (sizeof(SCARDHANDLE) == ControlCall.hCard.cbHandle)
  4720. {
  4721. SCardContext = *((SCARDCONTEXT *) ControlCall.hCard.Context.pbContext);
  4722. SCardHandle = *((SCARDHANDLE *) ControlCall.hCard.pbHandle);
  4723. cbBytesReturned = ControlCall.cbOutBufferSize;
  4724. }
  4725. else
  4726. {
  4727. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  4728. ControlReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  4729. }
  4730. //
  4731. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  4732. //
  4733. if (ControlReturn.ReturnCode == SCARD_S_SUCCESS)
  4734. {
  4735. fDoAllocationLocally =
  4736. (!ControlCall.fpvOutBufferIsNULL &&
  4737. (cbBytesReturned != SCARD_AUTOALLOCATE));
  4738. #ifdef OS_WINCE
  4739. if (!fDoAllocationLocally)
  4740. {
  4741. ControlReturn.ReturnCode =
  4742. pfnSCardControl(
  4743. SCardHandle,
  4744. ControlCall.dwControlCode,
  4745. ControlCall.pvInBuffer,
  4746. ControlCall.cbInBufferSize,
  4747. NULL,
  4748. cbBytesReturned,
  4749. &cbBytesReturned);
  4750. if ((ControlReturn.ReturnCode == SCARD_S_SUCCESS) && (cbBytesReturned > 0))
  4751. fDoAllocationLocally = TRUE;
  4752. else
  4753. {
  4754. TRC_ERR((TB, _T("SCardControl failed")));
  4755. status = STATUS_UNSUCCESSFUL;
  4756. goto ErrorReturn;
  4757. }
  4758. }
  4759. #endif
  4760. if (fDoAllocationLocally)
  4761. {
  4762. lpOutBuffer = (LPVOID) MIDL_user_allocate(cbBytesReturned * sizeof(BYTE));
  4763. if (lpOutBuffer == NULL)
  4764. {
  4765. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4766. ControlReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4767. }
  4768. }
  4769. }
  4770. //
  4771. // Make the call
  4772. //
  4773. if (ControlReturn.ReturnCode == SCARD_S_SUCCESS)
  4774. {
  4775. ControlReturn.ReturnCode =
  4776. pfnSCardControl(
  4777. SCardHandle,
  4778. ControlCall.dwControlCode,
  4779. ControlCall.pvInBuffer,
  4780. ControlCall.cbInBufferSize,
  4781. (cbBytesReturned == SCARD_AUTOALLOCATE) ? (LPVOID) &lpOutBuffer : lpOutBuffer,
  4782. cbBytesReturned,
  4783. &cbBytesReturned);
  4784. }
  4785. if (ControlReturn.ReturnCode == SCARD_S_SUCCESS)
  4786. {
  4787. ControlReturn.cbOutBufferSize = cbBytesReturned;
  4788. //
  4789. // If we are just returning the byte count then send back a junk buffer
  4790. //
  4791. if (lpOutBuffer == NULL)
  4792. {
  4793. ControlReturn.pvOutBuffer = (BYTE *) MIDL_user_allocate(cbBytesReturned);
  4794. if (ControlReturn.pvOutBuffer == NULL)
  4795. {
  4796. status = STATUS_NO_MEMORY;
  4797. goto ErrorReturn;
  4798. }
  4799. fFree = TRUE;
  4800. memset(ControlReturn.pvOutBuffer, 0, cbBytesReturned);
  4801. }
  4802. else
  4803. {
  4804. ControlReturn.pvOutBuffer = (LPBYTE) lpOutBuffer;
  4805. }
  4806. }
  4807. //
  4808. // encode the return
  4809. //
  4810. rpcStatus = MesEncodeDynBufferHandleCreate(
  4811. &pbEncodedBuffer,
  4812. &cbEncodedBuffer,
  4813. &hEnc);
  4814. if (rpcStatus != RPC_S_OK)
  4815. {
  4816. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  4817. status = STATUS_NO_MEMORY;
  4818. goto ErrorReturn;
  4819. }
  4820. _TRY_status(Control_Return_Encode(hEnc, &ControlReturn))
  4821. //
  4822. // Send return
  4823. //
  4824. AllocateAndChannelWriteReplyPacket(
  4825. pIoRequestPacket,
  4826. pbEncodedBuffer,
  4827. cbEncodedBuffer);
  4828. Return:
  4829. if (fFree)
  4830. {
  4831. MIDL_user_free(ControlReturn.pvOutBuffer);
  4832. }
  4833. if (lpOutBuffer != NULL)
  4834. {
  4835. //
  4836. // Check to see whether we allocated or SCard allcated for us
  4837. //
  4838. if (fDoAllocationLocally)
  4839. {
  4840. MIDL_user_free(lpOutBuffer);
  4841. }
  4842. else
  4843. {
  4844. #ifndef OS_WINCE
  4845. pfnSCardFreeMemory(SCardContext, lpOutBuffer);
  4846. #else
  4847. TRC_ASSERT(FALSE, (TB, _T("Shouldnt get here")));
  4848. #endif
  4849. }
  4850. }
  4851. if (fFreeDecode)
  4852. {
  4853. _TRY_2(Control_Call_Free(hDec, &ControlCall))
  4854. }
  4855. SafeMesHandleFree(&hDec);
  4856. SafeMesHandleFree(&hEnc);
  4857. MIDL_user_free(pbEncodedBuffer);
  4858. MIDL_user_free(pSCardHandleCall);
  4859. DC_END_FN();
  4860. return;
  4861. ErrorReturn:
  4862. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  4863. goto Return;
  4864. }
  4865. //---------------------------------------------------------------------------------------
  4866. //
  4867. // W32SCard::GetAttrib
  4868. //
  4869. //---------------------------------------------------------------------------------------
  4870. void
  4871. W32SCard::GetAttrib(
  4872. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  4873. {
  4874. DC_BEGIN_FN("W32SCard::GetAttrib");
  4875. RPC_STATUS rpcStatus = RPC_S_OK;
  4876. NTSTATUS status = STATUS_SUCCESS;
  4877. char *pbEncodedBuffer = NULL;
  4878. unsigned long cbEncodedBuffer = 0;
  4879. handle_t hDec = 0;
  4880. handle_t hEnc = 0;
  4881. SCARDCONTEXT SCardContext;
  4882. SCARDHANDLE SCardHandle;
  4883. GetAttrib_Call GetAttribCall;
  4884. GetAttrib_Return GetAttribReturn;
  4885. BOOL fFreeDecode = FALSE;
  4886. DWORD cbAttrLen = 0;
  4887. BOOL fDoAllocationLocally = FALSE;
  4888. LPBYTE pbAttr = NULL;
  4889. BOOL fFree = FALSE;
  4890. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  4891. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  4892. memset(&GetAttribReturn, 0, sizeof(GetAttribReturn));
  4893. GetAttribReturn.ReturnCode = SCARD_S_SUCCESS;
  4894. //
  4895. // Decode input parameters
  4896. //
  4897. rpcStatus = MesDecodeBufferHandleCreate(
  4898. (char *) (pIoRequest + 1), // bytes are at end of struct
  4899. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  4900. &hDec);
  4901. if (rpcStatus != RPC_S_OK)
  4902. {
  4903. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  4904. status = STATUS_NO_MEMORY;
  4905. goto ErrorReturn;
  4906. }
  4907. memset(&GetAttribCall, 0, sizeof(GetAttribCall));
  4908. _TRY_status(GetAttrib_Call_Decode(hDec, &GetAttribCall))
  4909. fFreeDecode = TRUE;
  4910. if (sizeof(SCARDHANDLE) == GetAttribCall.hCard.cbHandle)
  4911. {
  4912. SCardContext = *((SCARDCONTEXT *) GetAttribCall.hCard.Context.pbContext);
  4913. SCardHandle = *((SCARDHANDLE *) GetAttribCall.hCard.pbHandle);
  4914. cbAttrLen = GetAttribCall.cbAttrLen;
  4915. }
  4916. else
  4917. {
  4918. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  4919. GetAttribReturn.ReturnCode = SCARD_E_INVALID_PARAMETER;
  4920. }
  4921. if (GetAttribReturn.ReturnCode == SCARD_S_SUCCESS)
  4922. {
  4923. //
  4924. // Allocate if not in SCARD_AUTOALLOCATE mode and not a size only call
  4925. //
  4926. fDoAllocationLocally =
  4927. (!GetAttribCall.fpbAttrIsNULL &&
  4928. (cbAttrLen != SCARD_AUTOALLOCATE));
  4929. #ifdef OS_WINCE
  4930. if (!fDoAllocationLocally)
  4931. {
  4932. GetAttribReturn.ReturnCode =
  4933. pfnSCardGetAttrib(
  4934. SCardHandle,
  4935. GetAttribCall.dwAttrId,
  4936. NULL,
  4937. &cbAttrLen);
  4938. if ((GetAttribReturn.ReturnCode == SCARD_S_SUCCESS) && (cbAttrLen > 0))
  4939. fDoAllocationLocally = TRUE;
  4940. else
  4941. {
  4942. TRC_ERR((TB, _T("SCardGetAttrib failed")));
  4943. status = STATUS_UNSUCCESSFUL;
  4944. goto ErrorReturn;
  4945. }
  4946. }
  4947. #endif
  4948. if (fDoAllocationLocally)
  4949. {
  4950. pbAttr = (LPBYTE) MIDL_user_allocate(cbAttrLen);
  4951. if (pbAttr == NULL)
  4952. {
  4953. TRC_ERR((TB, _T("MIDL_user_allocate failed")));
  4954. GetAttribReturn.ReturnCode = SCARD_E_NO_MEMORY;
  4955. }
  4956. }
  4957. }
  4958. //
  4959. // Make the call
  4960. //
  4961. if (GetAttribReturn.ReturnCode == SCARD_S_SUCCESS)
  4962. {
  4963. GetAttribReturn.ReturnCode =
  4964. pfnSCardGetAttrib(
  4965. SCardHandle,
  4966. GetAttribCall.dwAttrId,
  4967. (cbAttrLen == SCARD_AUTOALLOCATE) ? (LPBYTE) &pbAttr : pbAttr,
  4968. &cbAttrLen);
  4969. if (GetAttribReturn.ReturnCode == SCARD_S_SUCCESS)
  4970. {
  4971. GetAttribReturn.cbAttrLen = cbAttrLen;
  4972. //
  4973. // If we are just returning the byte count then send back a junk buffer
  4974. //
  4975. if (pbAttr == NULL)
  4976. {
  4977. GetAttribReturn.pbAttr = (BYTE *) MIDL_user_allocate(cbAttrLen);
  4978. if (GetAttribReturn.pbAttr == NULL)
  4979. {
  4980. status = STATUS_NO_MEMORY;
  4981. goto ErrorReturn;
  4982. }
  4983. fFree = TRUE;
  4984. memset(GetAttribReturn.pbAttr, 0, cbAttrLen);
  4985. }
  4986. else
  4987. {
  4988. GetAttribReturn.pbAttr = pbAttr;
  4989. }
  4990. }
  4991. }
  4992. //
  4993. // encode the return
  4994. //
  4995. rpcStatus = MesEncodeDynBufferHandleCreate(
  4996. &pbEncodedBuffer,
  4997. &cbEncodedBuffer,
  4998. &hEnc);
  4999. if (rpcStatus != RPC_S_OK)
  5000. {
  5001. TRC_ERR((TB, _T("MesEncodeDynBufferHandleCreate failed with %lx."), rpcStatus));
  5002. status = STATUS_NO_MEMORY;
  5003. goto ErrorReturn;
  5004. }
  5005. _TRY_status(GetAttrib_Return_Encode(hEnc, &GetAttribReturn))
  5006. //
  5007. // Send return
  5008. //
  5009. AllocateAndChannelWriteReplyPacket(
  5010. pIoRequestPacket,
  5011. pbEncodedBuffer,
  5012. cbEncodedBuffer);
  5013. Return:
  5014. if (fFree)
  5015. {
  5016. MIDL_user_free(GetAttribReturn.pbAttr);
  5017. }
  5018. if (pbAttr != NULL)
  5019. {
  5020. //
  5021. // Check to see whether we allocated or SCard allcated for us
  5022. //
  5023. if (fDoAllocationLocally)
  5024. {
  5025. MIDL_user_free(pbAttr);
  5026. }
  5027. else
  5028. {
  5029. #ifndef OS_WINCE
  5030. pfnSCardFreeMemory(SCardContext, pbAttr);
  5031. #else
  5032. TRC_ASSERT(FALSE, (TB, _T("Shouldnt get here")));
  5033. #endif
  5034. }
  5035. }
  5036. if (fFreeDecode)
  5037. {
  5038. _TRY_2(GetAttrib_Call_Free(hDec, &GetAttribCall))
  5039. }
  5040. SafeMesHandleFree(&hDec);
  5041. SafeMesHandleFree(&hEnc);
  5042. MIDL_user_free(pbEncodedBuffer);
  5043. MIDL_user_free(pSCardHandleCall);
  5044. DC_END_FN();
  5045. return;
  5046. ErrorReturn:
  5047. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  5048. goto Return;
  5049. }
  5050. //---------------------------------------------------------------------------------------
  5051. //
  5052. // W32SCard::SetAttrib
  5053. //
  5054. //---------------------------------------------------------------------------------------
  5055. void
  5056. W32SCard::SetAttrib(
  5057. IN SCARDHANDLECALLSTRUCT *pSCardHandleCall)
  5058. {
  5059. DC_BEGIN_FN("W32SCard::SetAttrib");
  5060. LONG lReturn = SCARD_S_SUCCESS;
  5061. RPC_STATUS rpcStatus = RPC_S_OK;
  5062. NTSTATUS status = STATUS_SUCCESS;
  5063. handle_t hDec = 0;
  5064. SCARDCONTEXT SCardContext;
  5065. SCARDHANDLE SCardHandle;
  5066. SetAttrib_Call SetAttribCall;
  5067. BOOL fFreeDecode = FALSE;
  5068. PRDPDR_IOREQUEST_PACKET pIoRequestPacket = pSCardHandleCall->pIoRequestPacket;
  5069. PRDPDR_DEVICE_IOREQUEST pIoRequest = &(pIoRequestPacket->IoRequest);
  5070. //
  5071. // Decode input parameters
  5072. //
  5073. rpcStatus = MesDecodeBufferHandleCreate(
  5074. (char *) (pIoRequest + 1), // bytes are at end of struct
  5075. pIoRequest->Parameters.DeviceIoControl.InputBufferLength,
  5076. &hDec);
  5077. if (rpcStatus != RPC_S_OK)
  5078. {
  5079. TRC_ERR((TB, _T("MesDecodeBufferHandleCreate failed with %lx."), rpcStatus));
  5080. status = STATUS_NO_MEMORY;
  5081. goto ErrorReturn;
  5082. }
  5083. memset(&SetAttribCall, 0, sizeof(SetAttribCall));
  5084. _TRY_status(SetAttrib_Call_Decode(hDec, &SetAttribCall))
  5085. fFreeDecode = TRUE;
  5086. if (sizeof(SCARDHANDLE) == SetAttribCall.hCard.cbHandle)
  5087. {
  5088. SCardContext = *((SCARDCONTEXT *) SetAttribCall.hCard.Context.pbContext);
  5089. SCardHandle = *((SCARDHANDLE *) SetAttribCall.hCard.pbHandle);
  5090. }
  5091. else
  5092. {
  5093. TRC_ERR((TB, _T("Invalid handle sent from server.")));
  5094. lReturn = SCARD_E_INVALID_PARAMETER;
  5095. }
  5096. //
  5097. // Make the call
  5098. //
  5099. if (lReturn == SCARD_S_SUCCESS)
  5100. {
  5101. lReturn =
  5102. pfnSCardSetAttrib(
  5103. SCardHandle,
  5104. SetAttribCall.dwAttrId,
  5105. SetAttribCall.pbAttr,
  5106. SetAttribCall.cbAttrLen);
  5107. }
  5108. //
  5109. // Send return
  5110. //
  5111. EncodeAndChannelWriteLongReturn(
  5112. pIoRequestPacket,
  5113. lReturn);
  5114. Return:
  5115. if (fFreeDecode)
  5116. {
  5117. _TRY_2(SetAttrib_Call_Free(hDec, &SetAttribCall))
  5118. }
  5119. SafeMesHandleFree(&hDec);
  5120. MIDL_user_free(pSCardHandleCall);
  5121. DC_END_FN();
  5122. return;
  5123. ErrorReturn:
  5124. DefaultIORequestMsgHandleWrapper(pIoRequestPacket, status);
  5125. goto Return;
  5126. }
  5127. #ifndef OS_WINCE
  5128. //---------------------------------------------------------------------------------------
  5129. //
  5130. // W32SCard::AccessStartedEvent + supporting WaitForStartedEvent
  5131. //
  5132. //---------------------------------------------------------------------------------------
  5133. void
  5134. W32SCard::WaitForStartedEvent(
  5135. BOOLEAN TimerOrWaitFired)
  5136. {
  5137. DC_BEGIN_FN("W32SCard::WaitForStartedEvent");
  5138. LONG lReturn = SCARD_E_UNEXPECTED;
  5139. DWORD i;
  5140. PVOID pv;
  5141. //
  5142. // If TimerOrWaitFired is FALSE, that means the event was set...
  5143. // otherwise it timed out.
  5144. //
  5145. if (!TimerOrWaitFired)
  5146. {
  5147. lReturn = SCARD_S_SUCCESS;
  5148. }
  5149. EnterCriticalSection(&_csWaitForStartedEvent);
  5150. pv = InterlockedExchangePointer(&_hRegisterWaitForStartedEvent, NULL);
  5151. if (pv != NULL)
  5152. {
  5153. pfnUnregisterWaitEx(pv, NULL);
  5154. }
  5155. //
  5156. // Loop for each outstanding wait and send return
  5157. //
  5158. for (i=0; i<_dwIORequestListSize; i++)
  5159. {
  5160. if (_rgIORequestList[i] != NULL)
  5161. {
  5162. EncodeAndChannelWriteLongReturn(
  5163. _rgIORequestList[i],
  5164. lReturn);
  5165. _rgIORequestList[i] = NULL;
  5166. }
  5167. }
  5168. LeaveCriticalSection(&_csWaitForStartedEvent);
  5169. Return:
  5170. DC_END_FN();
  5171. }
  5172. VOID CALLBACK
  5173. WaitForStartedEventCallback(
  5174. PVOID lpParameter,
  5175. BOOLEAN TimerOrWaitFired)
  5176. {
  5177. W32SCard *pTHIS = (W32SCard *) lpParameter;
  5178. pTHIS->WaitForStartedEvent(TimerOrWaitFired);
  5179. }
  5180. typedef HANDLE (WINAPI FN_SCARDACCESSSTARTEDEVENT)(VOID);
  5181. typedef FN_SCARDACCESSSTARTEDEVENT *PFN_SCARDACCESSSTARTEDEVENT;
  5182. void
  5183. W32SCard::GetStartedEvent()
  5184. {
  5185. DC_BEGIN_FN("W32SCard::GetStartedEvent");
  5186. HMODULE hMod = NULL;
  5187. PFN_SCARDACCESSSTARTEDEVENT pSCardAccessStartedEvent= NULL;
  5188. HANDLE hEvent = NULL;
  5189. hMod = LoadLibraryA("winscard.dll");
  5190. if (hMod == NULL)
  5191. {
  5192. goto Return;
  5193. }
  5194. pSCardAccessStartedEvent = (PFN_SCARDACCESSSTARTEDEVENT)
  5195. GetProcAddress(hMod, "SCardAccessStartedEvent");
  5196. if (pSCardAccessStartedEvent != NULL)
  5197. {
  5198. _hStartedEvent = pSCardAccessStartedEvent();
  5199. }
  5200. else
  5201. {
  5202. TRC_ERR(( TB,
  5203. _T("GetProcAddress(SCardAccessStartedEvent) failed - LastError: %lx."),
  5204. GetLastError()));
  5205. }
  5206. FreeLibrary(hMod);
  5207. Return:
  5208. DC_END_FN();
  5209. }
  5210. typedef VOID (WINAPI FN_SCARDRELEASESTARTEDEVENT)(VOID);
  5211. typedef FN_SCARDRELEASESTARTEDEVENT *PFN_SCARDRELEASESTARTEDEVENT;
  5212. void
  5213. W32SCard::ReleaseStartedEvent()
  5214. {
  5215. DC_BEGIN_FN("W32SCard::ReleaseStartedEvent");
  5216. HMODULE hMod = NULL;
  5217. PFN_SCARDRELEASESTARTEDEVENT pSCardReleaseStartedEvent = NULL;
  5218. hMod = LoadLibraryA("winscard.dll");
  5219. if (hMod == NULL)
  5220. {
  5221. goto Return;
  5222. }
  5223. pSCardReleaseStartedEvent = (PFN_SCARDRELEASESTARTEDEVENT)
  5224. GetProcAddress(hMod, "SCardReleaseStartedEvent");
  5225. if (pSCardReleaseStartedEvent != NULL)
  5226. {
  5227. pSCardReleaseStartedEvent();
  5228. }
  5229. else
  5230. {
  5231. TRC_ERR(( TB,
  5232. _T("GetProcAddress(SCardReleaseStartedEvent) failed - LastError: %lx."),
  5233. GetLastError()));
  5234. }
  5235. FreeLibrary(hMod);
  5236. Return:
  5237. DC_END_FN();
  5238. }
  5239. #endif
  5240. void
  5241. W32SCard::AccessStartedEvent(
  5242. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket)
  5243. {
  5244. DC_BEGIN_FN("W32SCard::AccessStartedEvent");
  5245. LONG lReturn = SCARD_S_SUCCESS;
  5246. //
  5247. // Make sure only one thread registers at a time
  5248. //
  5249. EnterCriticalSection(&_csWaitForStartedEvent);
  5250. #ifndef OS_WINCE
  5251. //
  5252. // First, make sure we can get the started event
  5253. //
  5254. if (_hStartedEvent == NULL)
  5255. {
  5256. GetStartedEvent();
  5257. }
  5258. if (_hStartedEvent == NULL)
  5259. {
  5260. //
  5261. // Couldn't even get the event, so return error
  5262. //
  5263. lReturn = SCARD_E_NO_SERVICE;
  5264. goto ImmediateReturn;
  5265. }
  5266. //
  5267. // Now, check to see if the event is already signaled, if so,
  5268. // then just return success, otherwise, register a wait callback
  5269. // on the event so we don't block this thread
  5270. //
  5271. if (WaitForSingleObject(_hStartedEvent, 0) == WAIT_OBJECT_0)
  5272. {
  5273. //
  5274. // It is signaled, so return success
  5275. //
  5276. lReturn = SCARD_S_SUCCESS;
  5277. goto ImmediateReturn;
  5278. }
  5279. //
  5280. // If the object is currently being destroyed, then don't create a new thread.
  5281. //
  5282. if (_fInDestructor)
  5283. {
  5284. goto ImmediateReturn;
  5285. }
  5286. //
  5287. // Only allow one wait to be registered. The single wait callback will
  5288. // notify all waiting requests
  5289. //
  5290. if ((_hRegisterWaitForStartedEvent == NULL) && _fUseRegisterWaitFuncs)
  5291. {
  5292. if (!pfnRegisterWaitForSingleObject(
  5293. &_hRegisterWaitForStartedEvent,
  5294. _hStartedEvent,
  5295. WaitForStartedEventCallback,
  5296. this,
  5297. INFINITE,
  5298. WT_EXECUTEONLYONCE))
  5299. {
  5300. lReturn = SCARD_E_NO_SERVICE;
  5301. goto ImmediateReturn;
  5302. }
  5303. }
  5304. else if (!_fUseRegisterWaitFuncs)
  5305. {
  5306. lReturn = SCARD_E_UNEXPECTED;
  5307. goto ImmediateReturn;
  5308. }
  5309. #else
  5310. lReturn = SCARD_S_SUCCESS;
  5311. goto ImmediateReturn;
  5312. #endif
  5313. //
  5314. // Add this pIoRequestPacket to the list
  5315. //
  5316. if (!AddIORequestToList(pIoRequestPacket))
  5317. {
  5318. lReturn = SCARD_E_UNEXPECTED;
  5319. goto ImmediateReturn;
  5320. }
  5321. //
  5322. // return here and let the wait we just registered
  5323. // make the EncodeAndChannelWriteLongReturn call,
  5324. // which will send the return the calling server
  5325. //
  5326. Return:
  5327. LeaveCriticalSection(&_csWaitForStartedEvent);
  5328. DC_END_FN();
  5329. return;
  5330. ImmediateReturn:
  5331. EncodeAndChannelWriteLongReturn(
  5332. pIoRequestPacket,
  5333. lReturn);
  5334. goto Return;
  5335. }
  5336. HANDLE
  5337. W32SCard::StartFSFunc(
  5338. IN W32DRDEV_ASYNCIO_PARAMS *params,
  5339. OUT DWORD *status
  5340. )
  5341. /*++
  5342. Routine Description:
  5343. Start a generic asynchronous File System IO operation.
  5344. Arguments:
  5345. params - Context for the IO request.
  5346. status - Return status for IO request in the form of a windows
  5347. error code.
  5348. Return Value:
  5349. Returns a handle to an object that will be signalled when the read
  5350. completes, if it is not completed in this function. Otherwise, NULL
  5351. is returned.
  5352. --*/
  5353. {
  5354. #ifndef OS_WINCE
  5355. PRDPDR_DEVICE_IOREQUEST pIoRequest;
  5356. DrFile* pFile;
  5357. ULONG irpMajor;
  5358. #endif
  5359. DC_BEGIN_FN("W32SCard::StartFSFunc");
  5360. *status = ERROR_SUCCESS;
  5361. DC_END_FN();
  5362. return NULL;
  5363. }
  5364. DWORD
  5365. W32SCard::AsyncNotifyChangeDir(
  5366. IN W32DRDEV_ASYNCIO_PARAMS *params
  5367. )
  5368. /*++
  5369. Routine Description:
  5370. Directory change notification Function
  5371. Arguments:
  5372. params - Context for the IO request.
  5373. Return Value:
  5374. Always returns 0.
  5375. --*/
  5376. {
  5377. DC_BEGIN_FN("W32SCard::AsyncNotifyChangeDir");
  5378. DC_END_FN();
  5379. return ERROR_SUCCESS;
  5380. }
  5381. DWORD
  5382. W32SCard::AsyncDirCtrlFunc(
  5383. IN W32DRDEV_ASYNCIO_PARAMS *params
  5384. )
  5385. /*++
  5386. Routine Description:
  5387. Asynchrous Directory Control Function
  5388. Arguments:
  5389. params - Context for the IO request.
  5390. Return Value:
  5391. Always returns 0.
  5392. --*/
  5393. {
  5394. DC_BEGIN_FN("W32SCard::AsyncDirCtrlFunc");
  5395. DC_END_FN();
  5396. return ERROR_SUCCESS;
  5397. }
  5398. BOOL
  5399. W32SCard::BindToSCardFunctions()
  5400. {
  5401. #ifndef OS_WINCE
  5402. int i;
  5403. #endif
  5404. BOOL fRet = TRUE;
  5405. //
  5406. // Load winscard dll if it exists
  5407. //
  5408. _hModWinscard = LoadLibraryA("winscard.dll");
  5409. if (_hModWinscard != NULL)
  5410. {
  5411. //
  5412. // get the function pointers
  5413. //
  5414. #ifndef OS_WINCE
  5415. pfnSCardEstablishContext = (PFN_SCardEstablishContext) GetProcAddress(_hModWinscard, "SCardEstablishContext");
  5416. pfnSCardReleaseContext = (PFN_SCardReleaseContext) GetProcAddress(_hModWinscard, "SCardReleaseContext");
  5417. pfnSCardIsValidContext = (PFN_SCardIsValidContext) GetProcAddress(_hModWinscard, "SCardIsValidContext");
  5418. pfnSCardListReaderGroupsA = (PFN_SCardListReaderGroupsA) GetProcAddress(_hModWinscard, "SCardListReaderGroupsA");
  5419. pfnSCardListReaderGroupsW = (PFN_SCardListReaderGroupsW) GetProcAddress(_hModWinscard, "SCardListReaderGroupsW");
  5420. pfnSCardListReadersA = (PFN_SCardListReadersA) GetProcAddress(_hModWinscard, "SCardListReadersA");
  5421. pfnSCardListReadersW = (PFN_SCardListReadersW) GetProcAddress(_hModWinscard, "SCardListReadersW");
  5422. pfnSCardIntroduceReaderGroupA = (PFN_SCardIntroduceReaderGroupA) GetProcAddress(_hModWinscard, "SCardIntroduceReaderGroupA");
  5423. pfnSCardIntroduceReaderGroupW = (PFN_SCardIntroduceReaderGroupW) GetProcAddress(_hModWinscard, "SCardIntroduceReaderGroupW");
  5424. pfnSCardForgetReaderGroupA = (PFN_SCardForgetReaderGroupA) GetProcAddress(_hModWinscard, "SCardForgetReaderGroupA");
  5425. pfnSCardForgetReaderGroupW = (PFN_SCardForgetReaderGroupW) GetProcAddress(_hModWinscard, "SCardForgetReaderGroupW");
  5426. pfnSCardIntroduceReaderA = (PFN_SCardIntroduceReaderA) GetProcAddress(_hModWinscard, "SCardIntroduceReaderA");
  5427. pfnSCardIntroduceReaderW = (PFN_SCardIntroduceReaderW) GetProcAddress(_hModWinscard, "SCardIntroduceReaderW");
  5428. pfnSCardForgetReaderA = (PFN_SCardForgetReaderA) GetProcAddress(_hModWinscard, "SCardForgetReaderA");
  5429. pfnSCardForgetReaderW = (PFN_SCardForgetReaderW) GetProcAddress(_hModWinscard, "SCardForgetReaderW");
  5430. pfnSCardAddReaderToGroupA = (PFN_SCardAddReaderToGroupA) GetProcAddress(_hModWinscard, "SCardAddReaderToGroupA");
  5431. pfnSCardAddReaderToGroupW = (PFN_SCardAddReaderToGroupW) GetProcAddress(_hModWinscard, "SCardAddReaderToGroupW");
  5432. pfnSCardRemoveReaderFromGroupA = (PFN_SCardRemoveReaderFromGroupA) GetProcAddress(_hModWinscard, "SCardRemoveReaderFromGroupA");
  5433. pfnSCardRemoveReaderFromGroupW = (PFN_SCardRemoveReaderFromGroupW) GetProcAddress(_hModWinscard, "SCardRemoveReaderFromGroupW");
  5434. pfnSCardFreeMemory = (PFN_SCardFreeMemory) GetProcAddress(_hModWinscard, "SCardFreeMemory");
  5435. pfnSCardLocateCardsA = (PFN_SCardLocateCardsA) GetProcAddress(_hModWinscard, "SCardLocateCardsA");
  5436. pfnSCardLocateCardsW = (PFN_SCardLocateCardsW) GetProcAddress(_hModWinscard, "SCardLocateCardsW");
  5437. pfnSCardLocateCardsByATRA = (PFN_SCardLocateCardsByATRA) GetProcAddress(_hModWinscard, "SCardLocateCardsByATRA");
  5438. pfnSCardLocateCardsByATRW = (PFN_SCardLocateCardsByATRW) GetProcAddress(_hModWinscard, "SCardLocateCardsByATRW");
  5439. pfnSCardGetStatusChangeA = (PFN_SCardGetStatusChangeA) GetProcAddress(_hModWinscard, "SCardGetStatusChangeA");
  5440. pfnSCardGetStatusChangeW = (PFN_SCardGetStatusChangeW) GetProcAddress(_hModWinscard, "SCardGetStatusChangeW");
  5441. pfnSCardCancel = (PFN_SCardCancel) GetProcAddress(_hModWinscard, "SCardCancel");
  5442. pfnSCardConnectA = (PFN_SCardConnectA) GetProcAddress(_hModWinscard, "SCardConnectA");
  5443. pfnSCardConnectW = (PFN_SCardConnectW) GetProcAddress(_hModWinscard, "SCardConnectW");
  5444. pfnSCardReconnect = (PFN_SCardReconnect) GetProcAddress(_hModWinscard, "SCardReconnect");
  5445. pfnSCardDisconnect = (PFN_SCardDisconnect) GetProcAddress(_hModWinscard, "SCardDisconnect");
  5446. pfnSCardBeginTransaction = (PFN_SCardBeginTransaction) GetProcAddress(_hModWinscard, "SCardBeginTransaction");
  5447. pfnSCardEndTransaction = (PFN_SCardEndTransaction) GetProcAddress(_hModWinscard, "SCardEndTransaction");
  5448. pfnSCardState = (PFN_SCardState) GetProcAddress(_hModWinscard, "SCardState");
  5449. pfnSCardStatusA = (PFN_SCardStatusA) GetProcAddress(_hModWinscard, "SCardStatusA");
  5450. pfnSCardStatusW = (PFN_SCardStatusW) GetProcAddress(_hModWinscard, "SCardStatusW");
  5451. pfnSCardTransmit = (PFN_SCardTransmit) GetProcAddress(_hModWinscard, "SCardTransmit");
  5452. pfnSCardControl = (PFN_SCardControl) GetProcAddress(_hModWinscard, "SCardControl");
  5453. pfnSCardGetAttrib = (PFN_SCardGetAttrib) GetProcAddress(_hModWinscard, "SCardGetAttrib");
  5454. pfnSCardSetAttrib = (PFN_SCardSetAttrib) GetProcAddress(_hModWinscard, "SCardSetAttrib");
  5455. #else
  5456. pfnSCardListReadersA = SCardListReadersA;
  5457. pfnSCardLocateCardsA = SCardLocateCardsA;
  5458. pfnSCardLocateCardsByATRA = SCardLocateCardsByATRA;
  5459. pfnSCardGetStatusChangeA = SCardGetStatusChangeA;
  5460. pfnSCardConnectA = SCardConnectA;
  5461. pfnSCardStatusA = SCardStatusA;
  5462. pfnSCardIntroduceReaderA = SCardIntroduceReaderA;
  5463. pfnSCardForgetReaderA = SCardForgetReaderA;
  5464. pfnSCardListReaderGroupsA = SCardListReaderGroupsA;
  5465. pfnSCardListReaderGroupsW = SCardListReaderGroupsW;
  5466. pfnSCardIntroduceReaderGroupA = SCardIntroduceReaderGroupA;
  5467. pfnSCardIntroduceReaderGroupW = SCardIntroduceReaderGroupW;
  5468. pfnSCardForgetReaderGroupA = SCardForgetReaderGroupA;
  5469. pfnSCardForgetReaderGroupW = SCardForgetReaderGroupW;
  5470. pfnSCardAddReaderToGroupA = SCardAddReaderToGroupA;
  5471. pfnSCardAddReaderToGroupW = SCardAddReaderToGroupW;
  5472. pfnSCardRemoveReaderFromGroupA = SCardRemoveReaderFromGroupA;
  5473. pfnSCardRemoveReaderFromGroupW = SCardRemoveReaderFromGroupW;
  5474. pfnSCardConnectW = CESCardConnect;
  5475. pfnSCardReconnect = CESCardReconnect;
  5476. pfnSCardDisconnect = CESCardDisconnect;
  5477. pfnSCardBeginTransaction = CESCardBeginTransaction;
  5478. pfnSCardEndTransaction = CESCardEndTransaction;
  5479. pfnSCardTransmit = CESCardTransmit;
  5480. pfnSCardStatusW = CESCardStatus;
  5481. pfnSCardControl = CESCardControl;
  5482. pfnSCardGetAttrib = CESCardGetAttrib;
  5483. pfnSCardSetAttrib = CESCardSetAttrib;
  5484. gpfnSCardConnectW = (PFN_SCardConnectW) GetProcAddress(_hModWinscard, L"SCardConnectW");
  5485. gpfnSCardReconnect = (PFN_SCardReconnect) GetProcAddress(_hModWinscard, L"SCardReconnect");
  5486. gpfnSCardDisconnect = (PFN_SCardDisconnect) GetProcAddress(_hModWinscard, L"SCardDisconnect");
  5487. gpfnSCardBeginTransaction = (PFN_SCardBeginTransaction) GetProcAddress(_hModWinscard, L"SCardBeginTransaction");
  5488. gpfnSCardEndTransaction = (PFN_SCardEndTransaction) GetProcAddress(_hModWinscard, L"SCardEndTransaction");
  5489. gpfnSCardTransmit = (PFN_SCardTransmit) GetProcAddress(_hModWinscard, L"SCardTransmit");
  5490. gpfnSCardStatusW = (PFN_SCardStatusW) GetProcAddress(_hModWinscard, L"SCardStatusW");
  5491. gpfnSCardControl = (PFN_SCardControl) GetProcAddress(_hModWinscard, L"SCardControl");
  5492. gpfnSCardGetAttrib = (PFN_SCardGetAttrib) GetProcAddress(_hModWinscard, L"SCardGetAttrib");
  5493. gpfnSCardSetAttrib = (PFN_SCardSetAttrib) GetProcAddress(_hModWinscard, L"SCardSetAttrib");
  5494. pfnSCardEstablishContext = (PFN_SCardEstablishContext) GetProcAddress(_hModWinscard, L"SCardEstablishContext");
  5495. pfnSCardReleaseContext = (PFN_SCardReleaseContext) GetProcAddress(_hModWinscard, L"SCardReleaseContext");
  5496. pfnSCardIsValidContext = (PFN_SCardIsValidContext) GetProcAddress(_hModWinscard, L"SCardIsValidContext");
  5497. pfnSCardListReadersW = (PFN_SCardListReadersW) GetProcAddress(_hModWinscard, L"SCardListReadersW");
  5498. pfnSCardIntroduceReaderW = (PFN_SCardIntroduceReaderW) GetProcAddress(_hModWinscard, L"SCardIntroduceReaderW");
  5499. pfnSCardForgetReaderW = (PFN_SCardForgetReaderW) GetProcAddress(_hModWinscard, L"SCardForgetReaderW");
  5500. pfnSCardLocateCardsW = (PFN_SCardLocateCardsW) GetProcAddress(_hModWinscard, L"SCardLocateCardsW");
  5501. pfnSCardLocateCardsByATRW = (PFN_SCardLocateCardsByATRW) GetProcAddress(_hModWinscard, L"SCardLocateCardsByATRW");
  5502. pfnSCardGetStatusChangeW = (PFN_SCardGetStatusChangeW) GetProcAddress(_hModWinscard, L"SCardGetStatusChangeW");
  5503. pfnSCardCancel = (PFN_SCardCancel) GetProcAddress(_hModWinscard, L"SCardCancel");
  5504. gpfnSCardEstablishContext = pfnSCardEstablishContext;
  5505. gpfnSCardReleaseContext = pfnSCardReleaseContext;
  5506. gpfnSCardListReadersW = pfnSCardListReadersW;
  5507. gpfnSCardIntroduceReaderW = pfnSCardIntroduceReaderW;
  5508. gpfnSCardForgetReaderW = pfnSCardForgetReaderW;
  5509. gpfnSCardLocateCardsW = pfnSCardLocateCardsW;
  5510. gpfnSCardLocateCardsByATRW = pfnSCardLocateCardsByATRW;
  5511. gpfnSCardGetStatusChangeW = pfnSCardGetStatusChangeW;
  5512. #endif
  5513. //
  5514. // Note, don't check the pfnSCardLocateCardsByATR* API's since they aren't required.
  5515. //
  5516. if ((pfnSCardEstablishContext == NULL) ||
  5517. (pfnSCardReleaseContext == NULL) ||
  5518. (pfnSCardIsValidContext == NULL) ||
  5519. #ifndef OS_WINCE
  5520. (pfnSCardFreeMemory == NULL) ||
  5521. (pfnSCardState == NULL) ||
  5522. #endif
  5523. (pfnSCardListReaderGroupsA == NULL) ||
  5524. (pfnSCardListReaderGroupsW == NULL) ||
  5525. (pfnSCardListReadersA == NULL) ||
  5526. (pfnSCardListReadersW == NULL) ||
  5527. (pfnSCardIntroduceReaderGroupA == NULL) ||
  5528. (pfnSCardIntroduceReaderGroupW == NULL) ||
  5529. (pfnSCardForgetReaderGroupA == NULL) ||
  5530. (pfnSCardForgetReaderGroupW == NULL) ||
  5531. (pfnSCardIntroduceReaderA == NULL) ||
  5532. (pfnSCardIntroduceReaderW == NULL) ||
  5533. (pfnSCardForgetReaderA == NULL) ||
  5534. (pfnSCardForgetReaderW == NULL) ||
  5535. (pfnSCardAddReaderToGroupA == NULL) ||
  5536. (pfnSCardAddReaderToGroupW == NULL) ||
  5537. (pfnSCardRemoveReaderFromGroupA == NULL) ||
  5538. (pfnSCardRemoveReaderFromGroupW == NULL) ||
  5539. (pfnSCardLocateCardsA == NULL) ||
  5540. (pfnSCardLocateCardsW == NULL) ||
  5541. (pfnSCardGetStatusChangeA == NULL) ||
  5542. (pfnSCardGetStatusChangeW == NULL) ||
  5543. (pfnSCardCancel == NULL) ||
  5544. (pfnSCardConnectA == NULL) ||
  5545. (pfnSCardStatusA == NULL) ||
  5546. #ifndef OS_WINCE
  5547. (pfnSCardConnectW == NULL) ||
  5548. (pfnSCardReconnect == NULL) ||
  5549. (pfnSCardDisconnect == NULL) ||
  5550. (pfnSCardBeginTransaction == NULL) ||
  5551. (pfnSCardEndTransaction == NULL) ||
  5552. (pfnSCardTransmit == NULL) ||
  5553. (pfnSCardStatusW == NULL) ||
  5554. (pfnSCardControl == NULL) ||
  5555. (pfnSCardGetAttrib == NULL) ||
  5556. (pfnSCardSetAttrib == NULL))
  5557. #else
  5558. (gpfnSCardConnectW == NULL) ||
  5559. (gpfnSCardReconnect == NULL) ||
  5560. (gpfnSCardDisconnect == NULL) ||
  5561. (gpfnSCardBeginTransaction == NULL) ||
  5562. (gpfnSCardEndTransaction == NULL) ||
  5563. (gpfnSCardTransmit == NULL) ||
  5564. (gpfnSCardStatusW == NULL) ||
  5565. (gpfnSCardControl == NULL) ||
  5566. (gpfnSCardGetAttrib == NULL) ||
  5567. (gpfnSCardSetAttrib == NULL))
  5568. #endif
  5569. {
  5570. fRet = FALSE;
  5571. }
  5572. #ifndef OS_WINCE
  5573. _hModKernel32 = LoadLibraryA("kernel32.dll");
  5574. if (_hModKernel32 != NULL)
  5575. {
  5576. pfnRegisterWaitForSingleObject = (PFN_RegisterWaitForSingleObject)
  5577. GetProcAddress(_hModKernel32, "RegisterWaitForSingleObject");
  5578. pfnUnregisterWaitEx = (PFN_UnregisterWaitEx)
  5579. GetProcAddress(_hModKernel32, "UnregisterWaitEx");
  5580. if ((pfnRegisterWaitForSingleObject != NULL) &&
  5581. (pfnUnregisterWaitEx != NULL))
  5582. {
  5583. _fUseRegisterWaitFuncs = TRUE;
  5584. }
  5585. }
  5586. #endif
  5587. }
  5588. else
  5589. {
  5590. fRet = FALSE;
  5591. }
  5592. return (fRet);
  5593. }
  5594. //
  5595. // RPCRT4 stubs for dload failure
  5596. //
  5597. RPC_STATUS RPC_ENTRY
  5598. DLoadStub_MesDecodeBufferHandleCreate(
  5599. char * pBuffer,
  5600. unsigned long BufferSize,
  5601. handle_t * pHandle )
  5602. {
  5603. return (RPC_S_OUT_OF_MEMORY);
  5604. }
  5605. RPC_STATUS RPC_ENTRY
  5606. DLoadStub_MesEncodeDynBufferHandleCreate(
  5607. char * * pBuffer,
  5608. unsigned long * pEncodedSize,
  5609. handle_t * pHandle )
  5610. {
  5611. return (RPC_S_OUT_OF_MEMORY);
  5612. }
  5613. void RPC_ENTRY
  5614. DLoadStub_NdrMesTypeEncode2(
  5615. handle_t Handle,
  5616. const MIDL_TYPE_PICKLING_INFO * pPicklingInfo,
  5617. const MIDL_STUB_DESC * pStubDesc,
  5618. PFORMAT_STRING pFormatString,
  5619. const void * pObject )
  5620. {
  5621. throw (RPC_S_OUT_OF_MEMORY);
  5622. return;
  5623. }
  5624. void RPC_ENTRY
  5625. DLoadStub_NdrMesTypeDecode2(
  5626. handle_t Handle,
  5627. const MIDL_TYPE_PICKLING_INFO * pPicklingInfo,
  5628. const MIDL_STUB_DESC * pStubDesc,
  5629. PFORMAT_STRING pFormatString,
  5630. void * pObject )
  5631. {
  5632. throw (RPC_S_OUT_OF_MEMORY);
  5633. return;
  5634. }
  5635. void RPC_ENTRY
  5636. DLoadStub_NdrMesTypeFree2(
  5637. handle_t Handle,
  5638. const MIDL_TYPE_PICKLING_INFO * pPicklingInfo,
  5639. const MIDL_STUB_DESC * pStubDesc,
  5640. PFORMAT_STRING pFormatString,
  5641. void * pObject )
  5642. {
  5643. throw (RPC_S_OUT_OF_MEMORY);
  5644. return;
  5645. }
  5646. RPC_STATUS RPC_ENTRY
  5647. DLoadStub_MesHandleFree( handle_t Handle )
  5648. {
  5649. return (RPC_S_OUT_OF_MEMORY);
  5650. }
  5651. FARPROC WINAPI GetRPCRT4Stubs(LPCSTR szProcName)
  5652. {
  5653. DC_BEGIN_FN("W32SCard::GetRPCRT4Stubs");
  5654. if (0 == _stricmp(szProcName, "MesDecodeBufferHandleCreate"))
  5655. return (FARPROC)DLoadStub_MesDecodeBufferHandleCreate;
  5656. if (0 == _stricmp(szProcName, "MesEncodeDynBufferHandleCreate"))
  5657. return (FARPROC)DLoadStub_MesEncodeDynBufferHandleCreate;
  5658. if (0 == _stricmp(szProcName, "NdrMesTypeEncode2"))
  5659. return (FARPROC)DLoadStub_NdrMesTypeEncode2;
  5660. if (0 == _stricmp(szProcName, "NdrMesTypeDecode2"))
  5661. return (FARPROC)DLoadStub_NdrMesTypeDecode2;
  5662. if (0 == _stricmp(szProcName, "NdrMesTypeFree2"))
  5663. return (FARPROC)DLoadStub_NdrMesTypeFree2;
  5664. if (0 == _stricmp(szProcName, "MesHandleFree"))
  5665. return (FARPROC)DLoadStub_MesHandleFree;
  5666. TRC_ERR(( TB,
  5667. _T("RPCRT4 stub =%s= is missing. Fix it NOW!"),
  5668. szProcName));
  5669. DC_END_FN();
  5670. return (FARPROC) NULL;
  5671. }
  5672. //
  5673. // Dload error handler
  5674. //
  5675. FARPROC WINAPI DliHook(unsigned dliNotify, PDelayLoadInfo pdli)
  5676. {
  5677. DC_BEGIN_FN("W32SCard::DliHook");
  5678. FARPROC fp = 0;
  5679. switch (dliNotify)
  5680. {
  5681. case dliFailLoadLib:
  5682. {
  5683. if (0 == _stricmp("rpcrt4.dll", pdli->szDll))
  5684. {
  5685. fp = (FARPROC) -1;
  5686. }
  5687. }
  5688. break;
  5689. case dliFailGetProc:
  5690. {
  5691. SetLastError(ERROR_PROC_NOT_FOUND);
  5692. if (0 == _stricmp("rpcrt4.dll", pdli->szDll))
  5693. {
  5694. if (pdli->dlp.fImportByName)
  5695. {
  5696. fp = GetRPCRT4Stubs(pdli->dlp.szProcName);
  5697. }
  5698. else
  5699. {
  5700. TRC_ERR(( TB,
  5701. _T("RPCRT4 ordinal stub =%lx= is missing. Fix it NOW!"),
  5702. pdli->dlp.dwOrdinal));
  5703. fp = (FARPROC) NULL;
  5704. }
  5705. }
  5706. }
  5707. break;
  5708. }
  5709. DC_END_FN();
  5710. return fp;
  5711. }
  5712. PfnDliHook __pfnDliFailureHook2 = DliHook;