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
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;
38} pcsc_ctx_entry;
39
40typedef struct {
41 SCARDHANDLE h;
42 DWORD proto;
43 int in_use;
44} pcsc_card_entry;
45
46static pcsc_ctx_entry g_pcsc_ctx[8];
47static pcsc_card_entry g_pcsc_card[32];
48
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
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
88static pcsc_ctx_entry *pcsc_get_ctx(int id) {
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
102static pcsc_card_entry *pcsc_get_card(int id) {
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
int idx
Definition index_of.c:38