Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
transmit.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file transmit.c
12 * @brief Implements the OP_PCSC_TRANSMIT opcode (conditional build).
13 *
14 * Transmits an APDU to the connected smart card associated with a given
15 * handle. Returns a map with the response bytes and status words. When PCSC
16 * support is disabled at build time, consumes its arguments and returns a map
17 * with code = -2.
18 *
19 * OP_PCSC_TRANSMIT: (handle_id:int, apdu:array<int>) -> map
20 *
21 * - Pops: apdu array, then handle_id.
22 * - Pushes: map { data: array<int>, sw1:int, sw2:int, code:int }.
23 * - On success (code == SCARD_S_SUCCESS), data contains response bytes (no
24 * status words) and sw1/sw2 are the trailing status bytes.
25 * - On error, data is [], sw1/sw2 remain -1, and code is the PCSC error.
26 * - Notes: Negotiates protocol from the connected handle (T0/T1) and uses
27 * SCardTransmit. APDU bytes are taken modulo 256 from array ints.
28 */
29
30/* PCSC transmit */
32#ifdef FUN_WITH_PCSC
33 /* pops apdu array, handle_id */
34 Value vapdu = pop_value(vm);
36 int hid = (int)vh.i;
39
41 map_set(&m, "data", make_array_from_values(NULL, 0));
42 map_set(&m, "sw1", make_int(-1));
43 map_set(&m, "sw2", make_int(-1));
44 map_set(&m, "code", make_int(-1));
45
46 if (!ce || vapdu.type != VAL_ARRAY) {
47 free_value(vapdu);
48 push_value(vm, m);
49 break;
50 }
51 int n = array_length(&vapdu);
52 if (n < 0) n = 0;
53 unsigned char sbuf[4096];
54 if (n > (int)sizeof(sbuf)) n = (int)sizeof(sbuf);
55 for (int i = 0; i < n; ++i) {
56 Value tmp;
57 if (array_get_copy(&vapdu, i, &tmp)) {
58 int64_t v = tmp.type == VAL_INT ? tmp.i : 0;
59 sbuf[i] = (unsigned char)(v & 0xFF);
60 free_value(tmp);
61 } else {
62 sbuf[i] = 0;
63 }
64 }
65 free_value(vapdu);
66
67 SCARD_IO_REQUEST pio;
68 if (ce->proto == SCARD_PROTOCOL_T0)
69 pio = *SCARD_PCI_T0;
70 else if (ce->proto == SCARD_PROTOCOL_T1)
71 pio = *SCARD_PCI_T1;
72 else {
73 pio = *SCARD_PCI_T1;
74 }
75
76 unsigned char rbuf[4096];
77 DWORD rlen = sizeof(rbuf);
78 LONG rv = SCardTransmit(ce->h, &pio, sbuf, (DWORD)n, NULL, rbuf, &rlen);
79
80 int sw1 = -1, sw2 = -1;
81 int datalen = 0;
82 if (rv == SCARD_S_SUCCESS && rlen >= 2) {
83 sw1 = rbuf[rlen - 2];
84 sw2 = rbuf[rlen - 1];
85 datalen = (int)rlen - 2;
86 }
87
88 map_set(&m, "sw1", make_int(sw1));
89 map_set(&m, "sw2", make_int(sw2));
90 map_set(&m, "code", make_int((int)rv));
91
92 if (datalen > 0) {
93 Value *vals = (Value *)malloc(sizeof(Value) * (size_t)datalen);
94 if (!vals) {
95 map_set(&m, "data", make_array_from_values(NULL, 0));
96 } else {
97 for (int i = 0; i < datalen; ++i)
98 vals[i] = make_int((int64_t)rbuf[i]);
100 for (int i = 0; i < datalen; ++i)
101 free_value(vals[i]);
102 free(vals);
103 map_set(&m, "data", arr);
104 }
105 }
106
108#else
109 Value vapdu = pop_value(vm);
110 free_value(vapdu);
111 Value vh = pop_value(vm);
112 free_value(vh);
114 map_set(&m, "data", make_array_from_values(NULL, 0));
115 map_set(&m, "sw1", make_int(-1));
116 map_set(&m, "sw2", make_int(-1));
117 map_set(&m, "code", make_int(-2));
118 push_value(vm, m);
119#endif
120 break;
121}
@ OP_PCSC_TRANSMIT
Definition bytecode.h:189
Value v
Definition cast.c:22
array_clear & arr
Definition clear.c:38
pcsc_card_entry * ce
Definition connect.c:51
LONG rv
Definition connect.c:53
int hid
Definition disconnect.c:33
Value vh
Definition get_bool.c:38
Value m
Definition has_key.c:27
int n
Definition insert.c:41
Value * vals
Definition make_array.c:39
free(vals)
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Definition map.c:79
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
static pcsc_card_entry * pcsc_get_card(int id)
Lookup a card slot by id.
Definition pcsc.c:102
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Definition pcsc.c:40
int sw1
Definition transmit.c:80
free_value(vh)
push_value(vm, m)
unsigned char rbuf[4096]
Definition transmit.c:76
unsigned char sbuf[4096]
Definition transmit.c:53
SCARD_IO_REQUEST pio
Definition transmit.c:67
DWORD rlen
Definition transmit.c:77
int sw2
Definition transmit.c:80
int datalen
Definition transmit.c:81
int array_length(const Value *v)
Get the element count of an array Value.
Definition value.c:176
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Definition value.c:192
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
@ VAL_ARRAY
Definition value.h:55
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580