2025-10-02 20:08:21 +02:00
|
|
|
/**
|
|
|
|
|
* This file is part of the Fun programming language.
|
2025-10-09 01:59:19 +02:00
|
|
|
* https://fun-lang.xyz/
|
2025-10-02 20:08:21 +02:00
|
|
|
*
|
|
|
|
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
|
|
|
|
* Licensed under the terms of the Apache-2.0 license.
|
|
|
|
|
* https://opensource.org/license/apache-2-0
|
|
|
|
|
*
|
|
|
|
|
* Added: 2025-10-02
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* PCSC connect */
|
|
|
|
|
case OP_PCSC_CONNECT: {
|
|
|
|
|
#ifdef FUN_WITH_PCSC
|
2026-03-18 20:52:00 +01:00
|
|
|
/* Stack: [..., ctx_id, reader_name] -> pops reader_name first, then ctx_id */
|
|
|
|
|
Value vreader = pop_value(vm);
|
|
|
|
|
Value vctx = pop_value(vm);
|
2025-10-02 20:08:21 +02:00
|
|
|
|
2026-03-18 20:52:00 +01:00
|
|
|
int ctx_id = (int)vctx.i;
|
|
|
|
|
free_value(vctx);
|
|
|
|
|
char *rname = value_to_string_alloc(&vreader);
|
|
|
|
|
free_value(vreader);
|
2025-10-02 20:08:21 +02:00
|
|
|
|
2026-03-18 20:52:00 +01:00
|
|
|
pcsc_ctx_entry *e = pcsc_get_ctx(ctx_id);
|
|
|
|
|
if (!e || !rname) {
|
|
|
|
|
if (rname) free(rname);
|
2025-10-02 20:08:21 +02:00
|
|
|
push_value(vm, make_int(0));
|
|
|
|
|
break;
|
2026-03-18 20:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int hslot = pcsc_alloc_card_slot();
|
|
|
|
|
if (!hslot) {
|
|
|
|
|
free(rname);
|
|
|
|
|
push_value(vm, make_int(0));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pcsc_card_entry *ce = pcsc_get_card(hslot);
|
|
|
|
|
DWORD dwActive = 0;
|
|
|
|
|
LONG rv = SCardConnect(e->ctx, rname, SCARD_SHARE_SHARED,
|
|
|
|
|
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
|
|
|
|
|
&ce->h, &dwActive);
|
|
|
|
|
free(rname);
|
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
ce->in_use = 0;
|
|
|
|
|
push_value(vm, make_int(0));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ce->proto = dwActive;
|
|
|
|
|
push_value(vm, make_int(hslot));
|
|
|
|
|
#else
|
|
|
|
|
Value vreader = pop_value(vm);
|
|
|
|
|
free_value(vreader);
|
|
|
|
|
Value vctx = pop_value(vm);
|
|
|
|
|
free_value(vctx);
|
|
|
|
|
push_value(vm, make_int(0));
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
2025-10-02 20:08:21 +02:00
|
|
|
}
|