Added basic PC/SC (WinSCard) smartcard support. (0.17.0)
This commit is contained in:
parent
1ac163a01e
commit
945d668ccb
18 changed files with 693 additions and 4 deletions
79
lib/hex.fun
Normal file
79
lib/hex.fun
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
// Hex utility functions
|
||||
|
||||
fun hex_val(ch)
|
||||
if (ch == "0")
|
||||
return 0
|
||||
else if (ch == "1")
|
||||
return 1
|
||||
else if (ch == "2")
|
||||
return 2
|
||||
else if (ch == "3")
|
||||
return 3
|
||||
else if (ch == "4")
|
||||
return 4
|
||||
else if (ch == "5")
|
||||
return 5
|
||||
else if (ch == "6")
|
||||
return 6
|
||||
else if (ch == "7")
|
||||
return 7
|
||||
else if (ch == "8")
|
||||
return 8
|
||||
else if (ch == "9")
|
||||
return 9
|
||||
else if (ch == "a" || ch == "A")
|
||||
return 10
|
||||
else if (ch == "b" || ch == "B")
|
||||
return 11
|
||||
else if (ch == "c" || ch == "C")
|
||||
return 12
|
||||
else if (ch == "d" || ch == "D")
|
||||
return 13
|
||||
else if (ch == "e" || ch == "E")
|
||||
return 14
|
||||
else if (ch == "f" || ch == "F")
|
||||
return 15
|
||||
else
|
||||
return 0
|
||||
|
||||
fun hex_to_bytes(hex)
|
||||
s = to_string(hex)
|
||||
out = []
|
||||
number i = 0
|
||||
number n = len(s)
|
||||
while i + 1 < n
|
||||
number b = hex_val(substr(s, i, 1)) * 16 + hex_val(substr(s, i + 1, 1))
|
||||
push(out, b)
|
||||
i = i + 2
|
||||
return out
|
||||
|
||||
fun two_hex(n)
|
||||
n = n % 256
|
||||
hexd = "0123456789abcdef"
|
||||
number hi = (n / 16) % 16
|
||||
number lo = n % 16
|
||||
c1 = substr(hexd, hi, 1)
|
||||
c2 = substr(hexd, lo, 1)
|
||||
return c1 + c2
|
||||
|
||||
fun bytes_to_hex(arr)
|
||||
res = ""
|
||||
number i = 0
|
||||
number N = len(arr)
|
||||
while i < N
|
||||
res = res + two_hex(arr[i])
|
||||
i = i + 1
|
||||
return res
|
||||
80
lib/io/pcsc.fun
Normal file
80
lib/io/pcsc.fun
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
// __ns_alias__: PCSC
|
||||
|
||||
// PCSC stdlib abstraction class wrapping VM opcodes.
|
||||
// Provides snake_case methods and hex helpers.
|
||||
// All methods are defensive and work even if PCSC is unsupported (returning []/0 or a default map).
|
||||
|
||||
#include <hex.fun>
|
||||
|
||||
class PCSC()
|
||||
// ---- hex helpers moved to lib/hex.fun ----
|
||||
|
||||
// ---- PCSC wrappers ----
|
||||
fun establish(this)
|
||||
// returns ctx id (>0) or 0
|
||||
return pcsc_establish()
|
||||
|
||||
fun release(this, ctx)
|
||||
// returns 1 on success or 0
|
||||
return pcsc_release(ctx)
|
||||
|
||||
fun list_readers(this, ctx)
|
||||
readers = pcsc_list_readers(ctx)
|
||||
if readers == nil
|
||||
return []
|
||||
return readers
|
||||
|
||||
fun connect(this, ctx, reader_name)
|
||||
// returns handle id (>0) or 0
|
||||
return pcsc_connect(ctx, to_string(reader_name))
|
||||
|
||||
fun disconnect(this, handle)
|
||||
// returns 1 on success or 0
|
||||
return pcsc_disconnect(handle)
|
||||
|
||||
// Transmit raw APDU bytes (array of numbers 0..255). Returns map {"data":[], "sw1":n, "sw2":n, "code":n}
|
||||
fun transmit_bytes(this, handle, bytes)
|
||||
res = pcsc_transmit(handle, bytes)
|
||||
if res == nil
|
||||
m = {}
|
||||
m["data"] = []
|
||||
m["sw1"] = -1
|
||||
m["sw2"] = -1
|
||||
m["code"] = -2
|
||||
return m
|
||||
if res["data"] == nil
|
||||
res["data"] = []
|
||||
return res
|
||||
|
||||
// Transmit hex APDU string, return map with hex "data_hex" and numeric sw1/sw2/code for convenience
|
||||
fun transmit_hex(this, handle, hex)
|
||||
arr = hex_to_bytes(hex)
|
||||
res = pcsc_transmit(handle, arr)
|
||||
if res == nil
|
||||
res = {}
|
||||
res["data"] = []
|
||||
res["sw1"] = -1
|
||||
res["sw2"] = -1
|
||||
res["code"] = -2
|
||||
if res["data"] == nil
|
||||
res["data"] = []
|
||||
dh = bytes_to_hex(res["data"])
|
||||
m = {}
|
||||
m["data_hex"] = dh
|
||||
m["sw1"] = res["sw1"]
|
||||
m["sw2"] = res["sw2"]
|
||||
m["code"] = res["code"]
|
||||
return m
|
||||
Loading…
Add table
Add a link
Reference in a new issue