2 * Copyright 2022 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define WIN32_NO_STATUS
31 #include "wine/unixlib.h"
35 LONG
SCardEstablishContext( UINT32
, const void *, const void *, UINT32
* );
36 LONG
SCardReleaseContext( UINT32
);
37 LONG
SCardIsValidContext( UINT32
);
38 LONG
SCardGetStatusChange( UINT32
, UINT32
, void *, UINT32
);
39 LONG
SCardCancel( UINT32
);
40 LONG
SCardListReaders( UINT32
, const char *, char *, UINT32
* );
41 LONG
SCardListReaderGroups( UINT32
, char *, UINT32
* );
42 LONG
SCardConnect( UINT32
, const char *, UINT32
, UINT32
, UINT32
*, UINT32
* );
43 LONG
SCardStatus( UINT32
, char *, UINT32
*, UINT32
*, UINT32
*, BYTE
*, UINT32
* );
44 LONG
SCardDisconnect( UINT32
, UINT32
);
45 LONG
SCardReconnect( UINT32
, UINT32
, UINT32
, UINT32
, UINT32
* );
46 LONG
SCardBeginTransaction( UINT32
);
47 LONG
SCardEndTransaction( UINT32
, UINT32
);
48 LONG
SCardTransmit( UINT32
, const void *, const BYTE
*, UINT32
, void *, BYTE
*, UINT32
* );
49 LONG
SCardControl132( UINT32
, UINT32
, const void *, UINT32
, void *, UINT32
, UINT32
* );
50 LONG
SCardGetAttrib( UINT32
, UINT32
, BYTE
*, UINT32
* );
51 LONG
SCardSetAttrib( UINT32
, UINT32
, const BYTE
*, UINT32
);
53 LONG
SCardEstablishContext( UINT64
, const void *, const void *, UINT64
* );
54 LONG
SCardReleaseContext( UINT64
);
55 LONG
SCardIsValidContext( UINT64
);
56 LONG
SCardGetStatusChange( UINT64
, UINT64
, void *, UINT64
);
57 LONG
SCardCancel( UINT64
);
58 LONG
SCardListReaders( UINT64
, const char *, char *, UINT64
* );
59 LONG
SCardListReaderGroups( UINT64
, char *, UINT64
* );
60 LONG
SCardConnect( UINT64
, const char *, UINT64
, UINT64
, UINT64
*, UINT64
* );
61 LONG
SCardStatus( UINT64
, char *, UINT64
*, UINT64
*, UINT64
*, BYTE
*, UINT64
* );
62 LONG
SCardDisconnect( UINT64
, UINT64
);
63 LONG
SCardReconnect( UINT64
, UINT64
, UINT64
, UINT64
, UINT64
* );
64 LONG
SCardBeginTransaction( UINT64
);
65 LONG
SCardEndTransaction( UINT64
, UINT64
);
66 LONG
SCardTransmit( UINT64
, const void *, const BYTE
*, UINT64
, void *, BYTE
*, UINT64
* );
67 LONG
SCardControl( UINT64
, UINT64
, const void *, UINT64
, void *, UINT64
, UINT64
* );
68 LONG
SCardGetAttrib( UINT64
, UINT64
, BYTE
*, UINT64
* );
69 LONG
SCardSetAttrib( UINT64
, UINT64
, const BYTE
*, UINT64
);
72 static NTSTATUS
scard_establish_context( void *args
)
74 struct scard_establish_context_params
*params
= args
;
78 if (!(status
= SCardEstablishContext( params
->scope
, NULL
, NULL
, &handle
))) *params
->handle
= handle
;
81 return SCardEstablishContext( params
->scope
, NULL
, NULL
, params
->handle
);
85 static NTSTATUS
scard_release_context( void *args
)
87 struct scard_release_context_params
*params
= args
;
88 return SCardReleaseContext( params
->handle
);
91 static NTSTATUS
scard_is_valid_context( void *args
)
93 struct scard_is_valid_context_params
*params
= args
;
94 return SCardIsValidContext( params
->handle
);
98 struct reader_state_macos
102 UINT32 current_state
;
105 unsigned char atr
[MAX_ATR_SIZE
];
108 static struct reader_state_macos
*map_states_in( const struct reader_state
*state
, unsigned int count
)
110 struct reader_state_macos
*ret
;
113 if (!(ret
= malloc( sizeof(*ret
) * count
))) return NULL
;
114 for (i
= 0; i
< count
; i
++)
116 ret
[i
].reader
= state
[i
].reader
;
117 ret
[i
].userdata
= state
[i
].userdata
;
118 ret
[i
].current_state
= state
[i
].current_state
;
119 ret
[i
].event_state
= state
[i
].event_state
;
120 memcpy( ret
[i
].atr
, state
[i
].atr
, state
[i
].atr_size
);
121 ret
[i
].atr_size
= state
[i
].atr_size
;
126 static void map_states_out( const struct reader_state_macos
*state_macos
, struct reader_state
*state
, unsigned int count
)
129 for (i
= 0; i
< count
; i
++)
131 state
[i
].current_state
= state_macos
[i
].current_state
;
132 state
[i
].event_state
= state_macos
[i
].event_state
;
133 memcpy( state
[i
].atr
, state_macos
[i
].atr
, state_macos
[i
].atr_size
);
134 state
[i
].atr_size
= state_macos
[i
].atr_size
;
139 static NTSTATUS
scard_get_status_change( void *args
)
141 struct scard_get_status_change_params
*params
= args
;
144 struct reader_state_macos
*states
= map_states_in( params
->states
, params
->count
);
146 if (!states
) return STATUS_NO_MEMORY
;
147 if (!(status
= SCardGetStatusChange( params
->handle
, params
->timeout
, states
, params
->count
)))
148 map_states_out( states
, params
->states
, params
->count
);
152 return SCardGetStatusChange( params
->handle
, params
->timeout
, params
->states
, params
->count
);
156 static NTSTATUS
scard_cancel( void *args
)
158 struct scard_cancel_params
*params
= args
;
159 return SCardCancel( params
->handle
);
162 static NTSTATUS
scard_list_readers( void *args
)
164 struct scard_list_readers_params
*params
= args
;
167 UINT32 len
= *params
->readers_len
;
168 if (!(status
= SCardListReaders( params
->handle
, params
->groups
, params
->readers
, &len
))) *params
->readers_len
= len
;
171 return SCardListReaders( params
->handle
, params
->groups
, params
->readers
, params
->readers_len
);
175 static NTSTATUS
scard_list_reader_groups( void *args
)
177 struct scard_list_reader_groups_params
*params
= args
;
180 UINT32 len
= *params
->groups_len
;
181 if (!(status
= SCardListReaderGroups( params
->handle
, params
->groups
, &len
))) *params
->groups_len
= len
;
184 return SCardListReaderGroups( params
->handle
, params
->groups
, params
->groups_len
);
188 static NTSTATUS
scard_connect( void *args
)
190 struct scard_connect_params
*params
= args
;
193 UINT32 handle
, protocol
;
194 if (!(status
= SCardConnect( params
->context_handle
, params
->reader
, params
->share_mode
,
195 params
->preferred_protocols
, &handle
, &protocol
)))
197 *params
->connect_handle
= handle
;
198 *params
->protocol
= protocol
;
202 return SCardConnect( params
->context_handle
, params
->reader
, params
->share_mode
, params
->preferred_protocols
,
203 params
->connect_handle
, params
->protocol
);
207 static NTSTATUS
scard_status( void *args
)
209 struct scard_status_params
*params
= args
;
212 UINT32 names_len
= params
->names_len
? *params
->names_len
: 0;
213 UINT32 atr_len
= params
->atr_len
? *params
->atr_len
: 0;
214 UINT32 state
, protocol
;
216 if (!(status
= SCardStatus( params
->handle
, params
->names
, params
->names_len
? &names_len
: NULL
,
217 params
->state
? &state
: NULL
, params
->protocol
? &protocol
: NULL
, params
->atr
,
218 params
->atr_len
? &atr_len
: NULL
)))
220 if (params
->names_len
) *params
->names_len
= names_len
;
221 if (params
->state
) *params
->state
= state
;
222 if (params
->protocol
) *params
->protocol
= protocol
;
223 if (params
->atr_len
) *params
->atr_len
= atr_len
;
227 return SCardStatus( params
->handle
, params
->names
, params
->names_len
, params
->state
, params
->protocol
,
228 params
->atr
, params
->atr_len
);
232 static NTSTATUS
scard_reconnect( void *args
)
234 struct scard_reconnect_params
*params
= args
;
238 if (!(status
= SCardReconnect( params
->handle
, params
->share_mode
, params
->preferred_protocols
,
239 params
->initialization
, &protocol
))) *params
->protocol
= protocol
;
242 return SCardReconnect( params
->handle
, params
->share_mode
, params
->preferred_protocols
, params
->initialization
,
247 static NTSTATUS
scard_disconnect( void *args
)
249 struct scard_disconnect_params
*params
= args
;
250 return SCardDisconnect( params
->handle
, params
->disposition
);
253 static NTSTATUS
scard_begin_transaction( void *args
)
255 struct scard_begin_transaction_params
*params
= args
;
256 return SCardBeginTransaction( params
->handle
);
259 static NTSTATUS
scard_end_transaction( void *args
)
261 struct scard_end_transaction_params
*params
= args
;
262 return SCardEndTransaction( params
->handle
, params
->disposition
);
265 static NTSTATUS
scard_transmit( void *args
)
267 struct scard_transmit_params
*params
= args
;
270 UINT32 len
= *params
->recv_buflen
;
277 send
.protocol
= params
->send
->protocol
;
278 send
.pci_len
= params
->send
->pci_len
;
281 recv
.protocol
= params
->recv
->protocol
;
282 recv
.pci_len
= params
->recv
->pci_len
;
284 if (!(status
= SCardTransmit( params
->handle
, &send
, params
->send_buf
, params
->send_buflen
, &recv
,
285 params
->recv_buf
, &len
)))
289 params
->recv
->protocol
= recv
.protocol
;
290 params
->recv
->pci_len
= recv
.pci_len
;
292 *params
->recv_buflen
= len
;
296 return SCardTransmit( params
->handle
, params
->send
, params
->send_buf
, params
->send_buflen
, params
->recv
,
297 params
->recv_buf
, params
->recv_buflen
);
301 static NTSTATUS
scard_control( void *args
)
303 struct scard_control_params
*params
= args
;
307 if (!(status
= SCardControl132( params
->handle
, params
->code
, params
->in_buf
, params
->in_buflen
, params
->out_buf
,
308 params
->out_buflen
, &len
)))
309 *params
->ret_len
= len
;
312 return SCardControl( params
->handle
, params
->code
, params
->in_buf
, params
->in_buflen
, params
->out_buf
,
313 params
->out_buflen
, params
->ret_len
);
317 static NTSTATUS
scard_get_attrib( void *args
)
319 struct scard_get_attrib_params
*params
= args
;
322 UINT32 len
= *params
->attr_len
;
323 if (!(status
= SCardGetAttrib( params
->handle
, params
->id
, params
->attr
, &len
))) *params
->attr_len
= len
;
326 return SCardGetAttrib( params
->handle
, params
->id
, params
->attr
, params
->attr_len
);
330 static NTSTATUS
scard_set_attrib( void *args
)
332 struct scard_set_attrib_params
*params
= args
;
333 return SCardSetAttrib( params
->handle
, params
->id
, params
->attr
, params
->attr_len
);
336 const unixlib_entry_t __wine_unix_call_funcs
[] =
338 scard_establish_context
,
339 scard_release_context
,
340 scard_is_valid_context
,
341 scard_get_status_change
,
344 scard_list_reader_groups
,
349 scard_begin_transaction
,
350 scard_end_transaction
,
357 C_ASSERT( ARRAYSIZE(__wine_unix_call_funcs
) == unix_funcs_count
);
363 static NTSTATUS
wow64_scard_establish_context( void *args
)
369 } const *params32
= args
;
371 struct scard_establish_context_params params
=
374 ULongToPtr(params32
->handle
)
376 return scard_establish_context( ¶ms
);
379 static NTSTATUS
wow64_scard_release_context( void *args
)
384 } const *params32
= args
;
386 struct scard_release_context_params params
=
390 return scard_release_context( ¶ms
);
393 static NTSTATUS
wow64_scard_is_valid_context( void *args
)
398 } const *params32
= args
;
400 struct scard_is_valid_context_params params
=
404 return scard_is_valid_context( ¶ms
);
407 static NTSTATUS
wow64_scard_get_status_change( void *args
)
415 } const *params32
= args
;
417 struct scard_get_status_change_params params
=
421 ULongToPtr(params32
->states
),
424 return scard_get_status_change( ¶ms
);
427 static NTSTATUS
wow64_scard_cancel( void *args
)
432 } const *params32
= args
;
434 struct scard_cancel_params params
=
438 return scard_cancel( ¶ms
);
441 static NTSTATUS
wow64_scard_list_readers( void *args
)
451 struct scard_list_readers_params params
=
454 ULongToPtr(params32
->groups
),
455 ULongToPtr(params32
->readers
),
456 ULongToPtr(params32
->readers_len
)
458 return scard_list_readers( ¶ms
);
461 static NTSTATUS
wow64_scard_list_reader_groups( void *args
)
470 struct scard_list_reader_groups_params params
=
473 ULongToPtr(params32
->groups
),
474 ULongToPtr(params32
->groups_len
)
476 return scard_list_reader_groups( ¶ms
);
479 static NTSTATUS
wow64_scard_connect( void *args
)
483 UINT64 context_handle
;
486 UINT64 preferred_protocols
;
487 PTR32 connect_handle
;
491 struct scard_connect_params params
=
493 params32
->context_handle
,
494 ULongToPtr(params32
->reader
),
495 params32
->share_mode
,
496 params32
->preferred_protocols
,
497 ULongToPtr(params32
->connect_handle
),
498 ULongToPtr(params32
->protocol
)
500 return scard_connect( ¶ms
);
503 static NTSTATUS
wow64_scard_status( void *args
)
516 struct scard_status_params params
=
519 ULongToPtr(params32
->names
),
520 ULongToPtr(params32
->names_len
),
521 ULongToPtr(params32
->state
),
522 ULongToPtr(params32
->protocol
),
523 ULongToPtr(params32
->atr
),
524 ULongToPtr(params32
->atr_len
)
526 return scard_status( ¶ms
);
529 static NTSTATUS
wow64_scard_reconnect( void *args
)
535 UINT64 preferred_protocols
;
536 UINT64 initialization
;
540 struct scard_reconnect_params params
=
543 params32
->share_mode
,
544 params32
->preferred_protocols
,
545 params32
->initialization
,
546 ULongToPtr(params32
->protocol
)
548 return scard_reconnect( ¶ms
);
551 static NTSTATUS
wow64_scard_disconnect( void *args
)
559 struct scard_disconnect_params params
=
562 params32
->disposition
564 return scard_disconnect( ¶ms
);
567 static NTSTATUS
wow64_scard_begin_transaction( void *args
)
574 struct scard_begin_transaction_params params
=
578 return scard_begin_transaction( ¶ms
);
581 static NTSTATUS
wow64_scard_end_transaction( void *args
)
589 struct scard_end_transaction_params params
=
592 params32
->disposition
594 return scard_end_transaction( ¶ms
);
597 static NTSTATUS
wow64_scard_transmit( void *args
)
610 struct scard_transmit_params params
=
613 ULongToPtr(params32
->send
),
614 ULongToPtr(params32
->send_buf
),
615 params32
->send_buflen
,
616 ULongToPtr(params32
->recv
),
617 ULongToPtr(params32
->recv_buf
),
618 ULongToPtr(params32
->recv_buflen
)
620 return scard_transmit( ¶ms
);
623 static NTSTATUS
wow64_scard_control( void *args
)
636 struct scard_control_params params
=
640 ULongToPtr(params32
->in_buf
),
642 ULongToPtr(params32
->out_buf
),
643 params32
->out_buflen
,
644 ULongToPtr(params32
->ret_len
)
646 return scard_control( ¶ms
);
649 static NTSTATUS
wow64_scard_get_attrib( void *args
)
659 struct scard_get_attrib_params params
=
663 ULongToPtr(params32
->attr
),
664 ULongToPtr(params32
->attr_len
)
666 return scard_get_attrib( ¶ms
);
669 static NTSTATUS
wow64_scard_set_attrib( void *args
)
679 struct scard_set_attrib_params params
=
683 ULongToPtr(params32
->attr
),
686 return scard_set_attrib( ¶ms
);
689 const unixlib_entry_t __wine_unix_call_wow64_funcs
[] =
691 wow64_scard_establish_context
,
692 wow64_scard_release_context
,
693 wow64_scard_is_valid_context
,
694 wow64_scard_get_status_change
,
696 wow64_scard_list_readers
,
697 wow64_scard_list_reader_groups
,
700 wow64_scard_reconnect
,
701 wow64_scard_disconnect
,
702 wow64_scard_begin_transaction
,
703 wow64_scard_end_transaction
,
704 wow64_scard_transmit
,
706 wow64_scard_get_attrib
,
707 wow64_scard_set_attrib
,
710 C_ASSERT( ARRAYSIZE(__wine_unix_call_wow64_funcs
) == unix_funcs_count
);