Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
pcsc.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 pcsc.c
12 * @brief PC/SC smartcard helper registries and lookup utilities.
13 *
14 * Provides small fixed-size registries for PC/SC contexts and card handles,
15 * plus allocation and lookup helpers used by VM opcodes when FUN_WITH_PCSC
16 * is enabled.
17 */
18
19#ifdef FUN_WITH_PCSC
20#if defined(__has_include)
21#if __has_include(<PCSC/winscard.h>)
22#include <PCSC/winscard.h>
23#include <PCSC/wintypes.h>
24#elif __has_include(<winscard.h>)
25#include <winscard.h>
26#else
27#error "FUN_WITH_PCSC is enabled but PCSC headers were not found"
28#endif
29#else
30#include <PCSC/winscard.h>
31#include <PCSC/wintypes.h>
32#endif
33#include <string.h>
34
35typedef struct {
36 SCARDCONTEXT ctx;
37 int in_use;
39
40typedef struct {
41 SCARDHANDLE h;
42 DWORD proto;
43 int in_use;
45
48
49/**
50 * @brief Allocate a free context slot in the PC/SC registry.
51 *
52 * @return A 1-based slot id on success, or 0 if no free slot is available.
53 */
54static int pcsc_alloc_ctx_slot(void) {
55 for (int i = 0; i < (int)(sizeof(g_pcsc_ctx) / sizeof(g_pcsc_ctx[0])); ++i) {
56 if (!g_pcsc_ctx[i].in_use) {
57 g_pcsc_ctx[i].in_use = 1;
58 g_pcsc_ctx[i].ctx = 0;
59 return i + 1;
60 }
61 }
62 return 0;
63}
64
65/**
66 * @brief Allocate a free card slot in the PC/SC registry.
67 *
68 * @return A 1-based slot id on success, or 0 if no free slot is available.
69 */
70static int pcsc_alloc_card_slot(void) {
71 for (int i = 0; i < (int)(sizeof(g_pcsc_card) / sizeof(g_pcsc_card[0])); ++i) {
72 if (!g_pcsc_card[i].in_use) {
73 g_pcsc_card[i].in_use = 1;
74 g_pcsc_card[i].h = 0;
75 g_pcsc_card[i].proto = 0;
76 return i + 1;
77 }
78 }
79 return 0;
80}
81
82/**
83 * @brief Lookup a context slot by id.
84 *
85 * @param id 1-based context id previously returned by pcsc_alloc_ctx_slot().
86 * @return Pointer to the registry entry if valid and in use; NULL otherwise.
87 */
89 if (id <= 0) return NULL;
90 int idx = id - 1;
91 if (idx < 0 || idx >= (int)(sizeof(g_pcsc_ctx) / sizeof(g_pcsc_ctx[0]))) return NULL;
92 if (!g_pcsc_ctx[idx].in_use) return NULL;
93 return &g_pcsc_ctx[idx];
94}
95
96/**
97 * @brief Lookup a card slot by id.
98 *
99 * @param id 1-based card id previously returned by pcsc_alloc_card_slot().
100 * @return Pointer to the registry entry if valid and in use; NULL otherwise.
101 */
103 if (id <= 0) return NULL;
104 int idx = id - 1;
105 if (idx < 0 || idx >= (int)(sizeof(g_pcsc_card) / sizeof(g_pcsc_card[0]))) return NULL;
106 if (!g_pcsc_card[idx].in_use) return NULL;
107 return &g_pcsc_card[idx];
108}
109#endif
ce in_use
Definition disconnect.c:41
int idx
Definition index_of.c:38
static pcsc_card_entry g_pcsc_card[32]
Definition pcsc.c:47
static pcsc_card_entry * pcsc_get_card(int id)
Lookup a card slot by id.
Definition pcsc.c:102
static int pcsc_alloc_ctx_slot(void)
Allocate a free context slot in the PC/SC registry.
Definition pcsc.c:54
static pcsc_ctx_entry g_pcsc_ctx[8]
Definition pcsc.c:46
static int pcsc_alloc_card_slot(void)
Allocate a free card slot in the PC/SC registry.
Definition pcsc.c:70
static pcsc_ctx_entry * pcsc_get_ctx(int id)
Lookup a context slot by id.
Definition pcsc.c:88
Definition pcsc.c:40
DWORD proto
Definition pcsc.c:42
SCARDHANDLE h
Definition pcsc.c:41
int in_use
Definition pcsc.c:43
Definition pcsc.c:35
SCARDCONTEXT ctx
Definition pcsc.c:36
int in_use
Definition pcsc.c:37