libthai-idcard 0.2.0

A Rust library for reading Thai National ID smart cards via PC/SC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
 * cpp_usage.cpp — Example of using libthaiidcard from C++.
 *
 * Supports macOS, Linux, and Windows.
 *
 * Compile (dynamic loading):
 *   macOS / Linux:   g++ -std=c++17 -o cpp_usage examples/cpp_usage.cpp -ldl
 *   Windows (MSVC):  cl /EHsc examples/cpp_usage.cpp
 *   Windows (MinGW): g++ -std=c++17 -o cpp_usage.exe examples/cpp_usage.cpp
 *   ./cpp_usage
 *
 * Compile-time linking (macOS/Linux):
 *   g++ -std=c++17 -o cpp_usage examples/cpp_usage.cpp \
 *       -Ltarget/debug -lthaiidcard -lpcsclite -Wl,-rpath,target/debug
 *   ./cpp_usage
 */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>

/* ---------------------------------------------------------------------------
 * Platform abstraction for dynamic library loading
 * -------------------------------------------------------------------------*/
#ifdef _WIN32
#include <windows.h>
using LibHandle = HMODULE;
#define LIB_LOAD(name)    LoadLibraryA(name)
#define LIB_SYM(handle,n) GetProcAddress(handle, n)
#define LIB_CLOSE(handle) FreeLibrary(handle)
static std::string last_lib_error()
{
    static char buf[256];
    DWORD err = GetLastError();
    if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
                       buf, sizeof(buf), NULL))
        return buf;
    return "error code " + std::to_string(err);
}
#else
#include <dlfcn.h>
using LibHandle = void *;
#define LIB_LOAD(name)    dlopen(name, RTLD_LAZY)
#define LIB_SYM(handle,n) dlsym(handle, n)
#define LIB_CLOSE(handle) dlclose(handle)
static std::string last_lib_error() { return dlerror(); }
#endif

/* ---------------------------------------------------------------------------
 * Opaque handle type
 * -------------------------------------------------------------------------*/
using ThaiIdHandle = struct thaiid_card_data;

/* ---------------------------------------------------------------------------
 * RAII wrapper for the shared library
 * -------------------------------------------------------------------------*/
class DynamicLibrary
{
    LibHandle handle_;

public:
    DynamicLibrary() : handle_(nullptr) {}

    explicit DynamicLibrary(const std::string &path)
        : handle_(LIB_LOAD(path.c_str()))
    {
        if (!handle_)
            throw std::runtime_error("Failed to load " + path + ": " + last_lib_error());
    }

    ~DynamicLibrary()
    {
        if (handle_)
            LIB_CLOSE(handle_);
    }

    DynamicLibrary(const DynamicLibrary &) = delete;
    DynamicLibrary &operator=(const DynamicLibrary &) = delete;

    DynamicLibrary(DynamicLibrary &&other) noexcept : handle_(other.handle_)
    {
        other.handle_ = nullptr;
    }
    DynamicLibrary &operator=(DynamicLibrary &&other) noexcept
    {
        if (this != &other)
        {
            if (handle_)
                LIB_CLOSE(handle_);
            handle_ = other.handle_;
            other.handle_ = nullptr;
        }
        return *this;
    }

    /** Resolve a symbol from the library. Returns nullptr on failure. */
    template <typename T>
    T resolve(const char *name) const
    {
        auto *ptr = LIB_SYM(handle_, name);
        if (!ptr)
            throw std::runtime_error(std::string("Missing symbol: ") + name + " (" + last_lib_error() + ")");
        return reinterpret_cast<T>(ptr);
    }
};

/* ---------------------------------------------------------------------------
 * Function pointer types for the C API
 * -------------------------------------------------------------------------*/
using ReadFn       = ThaiIdHandle *(*)(const char *, int, int, int);
using FreeFn       = void (*)(ThaiIdHandle *);
using GetErrorFn   = const char *(*)();
using GetStrFn     = const char *(*)(const ThaiIdHandle *);

/* ---------------------------------------------------------------------------
 * High-level wrapper that owns a card data handle and provides typed accessors
 * -------------------------------------------------------------------------*/
class CardData
{
    ThaiIdHandle *handle_;
    FreeFn free_;

    GetStrFn get_cid_;
    GetStrFn get_name_thai_;
    GetStrFn get_name_en_;
    GetStrFn get_dob_;
    GetStrFn get_gender_;
    GetStrFn get_card_issuer_;
    GetStrFn get_issue_date_;
    GetStrFn get_expire_date_;
    GetStrFn get_address_;
    GetStrFn get_face_image_;
    GetStrFn get_laser_id_;
    GetStrFn get_main_inscl_;
    GetStrFn get_sub_inscl_;
    GetStrFn get_main_hospital_;
    GetStrFn get_sub_hospital_;
    GetStrFn get_paid_type_;
    GetStrFn get_nhso_issue_date_;
    GetStrFn get_nhso_expire_date_;
    GetStrFn get_nhso_update_date_;
    GetStrFn get_change_hospital_amount_;

    static std::string cstr(const char *s) { return s ? s : ""; }

public:
    CardData(ThaiIdHandle *handle, FreeFn free_fn, GetStrFn *getters)
        : handle_(handle), free_(free_fn),
          get_cid_(getters[0]), get_name_thai_(getters[1]),
          get_name_en_(getters[2]), get_dob_(getters[3]),
          get_gender_(getters[4]), get_card_issuer_(getters[5]),
          get_issue_date_(getters[6]), get_expire_date_(getters[7]),
          get_address_(getters[8]), get_face_image_(getters[9]),
          get_laser_id_(getters[10]), get_main_inscl_(getters[11]),
          get_sub_inscl_(getters[12]), get_main_hospital_(getters[13]),
          get_sub_hospital_(getters[14]), get_paid_type_(getters[15]),
          get_nhso_issue_date_(getters[16]), get_nhso_expire_date_(getters[17]),
          get_nhso_update_date_(getters[18]), get_change_hospital_amount_(getters[19])
    {}

    ~CardData()
    {
        if (handle_)
            free_(handle_);
    }

    CardData(const CardData &) = delete;
    CardData &operator=(const CardData &) = delete;
    CardData(CardData &&other) noexcept
        : handle_(other.handle_), free_(other.free_),
          get_cid_(other.get_cid_), get_name_thai_(other.get_name_thai_),
          get_name_en_(other.get_name_en_), get_dob_(other.get_dob_),
          get_gender_(other.get_gender_), get_card_issuer_(other.get_card_issuer_),
          get_issue_date_(other.get_issue_date_), get_expire_date_(other.get_expire_date_),
          get_address_(other.get_address_), get_face_image_(other.get_face_image_),
          get_laser_id_(other.get_laser_id_), get_main_inscl_(other.get_main_inscl_),
          get_sub_inscl_(other.get_sub_inscl_), get_main_hospital_(other.get_main_hospital_),
          get_sub_hospital_(other.get_sub_hospital_), get_paid_type_(other.get_paid_type_),
          get_nhso_issue_date_(other.get_nhso_issue_date_),
          get_nhso_expire_date_(other.get_nhso_expire_date_),
          get_nhso_update_date_(other.get_nhso_update_date_),
          get_change_hospital_amount_(other.get_change_hospital_amount_)
    {
        other.handle_ = nullptr;
    }
    CardData &operator=(CardData &&other) noexcept
    {
        if (this != &other)
        {
            if (handle_)
                free_(handle_);
            handle_ = other.handle_;
            other.handle_ = nullptr;
            free_ = other.free_;
            get_cid_ = other.get_cid_;
            get_name_thai_ = other.get_name_thai_;
            get_name_en_ = other.get_name_en_;
            get_dob_ = other.get_dob_;
            get_gender_ = other.get_gender_;
            get_card_issuer_ = other.get_card_issuer_;
            get_issue_date_ = other.get_issue_date_;
            get_expire_date_ = other.get_expire_date_;
            get_address_ = other.get_address_;
            get_face_image_ = other.get_face_image_;
            get_laser_id_ = other.get_laser_id_;
            get_main_inscl_ = other.get_main_inscl_;
            get_sub_inscl_ = other.get_sub_inscl_;
            get_main_hospital_ = other.get_main_hospital_;
            get_sub_hospital_ = other.get_sub_hospital_;
            get_paid_type_ = other.get_paid_type_;
            get_nhso_issue_date_ = other.get_nhso_issue_date_;
            get_nhso_expire_date_ = other.get_nhso_expire_date_;
            get_nhso_update_date_ = other.get_nhso_update_date_;
            get_change_hospital_amount_ = other.get_change_hospital_amount_;
        }
        return *this;
    }

    // --- Accessors ---------------------------------------------------------
    std::string cid()                   const { return cstr(get_cid_(handle_)); }
    std::string name_thai()             const { return cstr(get_name_thai_(handle_)); }
    std::string name_en()               const { return cstr(get_name_en_(handle_)); }
    std::string dob()                   const { return cstr(get_dob_(handle_)); }
    std::string gender()                const { return cstr(get_gender_(handle_)); }
    std::string card_issuer()           const { return cstr(get_card_issuer_(handle_)); }
    std::string issue_date()            const { return cstr(get_issue_date_(handle_)); }
    std::string expire_date()           const { return cstr(get_expire_date_(handle_)); }
    std::string address()               const { return cstr(get_address_(handle_)); }
    std::string face_image()            const { return cstr(get_face_image_(handle_)); }
    std::string laser_id()              const { return cstr(get_laser_id_(handle_)); }
    std::string main_inscl()            const { return cstr(get_main_inscl_(handle_)); }
    std::string sub_inscl()             const { return cstr(get_sub_inscl_(handle_)); }
    std::string main_hospital()         const { return cstr(get_main_hospital_(handle_)); }
    std::string sub_hospital()          const { return cstr(get_sub_hospital_(handle_)); }
    std::string paid_type()             const { return cstr(get_paid_type_(handle_)); }
    std::string nhso_issue_date()       const { return cstr(get_nhso_issue_date_(handle_)); }
    std::string nhso_expire_date()      const { return cstr(get_nhso_expire_date_(handle_)); }
    std::string nhso_update_date()      const { return cstr(get_nhso_update_date_(handle_)); }
    std::string change_hospital_amount() const { return cstr(get_change_hospital_amount_(handle_)); }
};

/* ---------------------------------------------------------------------------
 * Auto-discover the shared library path
 * -------------------------------------------------------------------------*/
static std::string library_filename()
{
#if defined(_WIN32)
    return "thaiidcard.dll";
#elif defined(__APPLE__)
    return "libthaiidcard.dylib";
#else
    return "libthaiidcard.so";
#endif
}

static std::string find_library()
{
    auto name = library_filename();

    /* Relative paths (cwd = project root when running from repo root) */
    const char *relative[] = { "target/debug/", "target/release/", nullptr };
    for (int i = 0; relative[i]; ++i)
    {
        auto path = std::string(relative[i]) + name;
        if (FILE *f = std::fopen(path.c_str(), "r"))
        {
            std::fclose(f);
            return path;
        }
    }

#ifdef __APPLE__
    const char *syspaths[] = { "/usr/local/lib/", "/opt/homebrew/lib/", nullptr };
#elif defined(_WIN32)
    const char *syspaths[] = { nullptr };  // pass manually on Windows
#else
    const char *syspaths[] = {
        "/usr/local/lib/", "/usr/lib/",
        "/usr/lib/x86_64-linux-gnu/", "/usr/lib/aarch64-linux-gnu/",
        nullptr
    };
#endif

#ifndef _WIN32
    for (int i = 0; syspaths[i]; ++i)
    {
        auto path = std::string(syspaths[i]) + name;
        if (FILE *f = std::fopen(path.c_str(), "r"))
        {
            std::fclose(f);
            return path;
        }
    }
#endif

    throw std::runtime_error(
        "libthaiidcard not found. Pass the path as the first argument, "
        "or build with: make shared");
}

/* ---------------------------------------------------------------------------
 * Print card data
 * -------------------------------------------------------------------------*/
static void print_card(const CardData &data)
{
    auto field = [](const char *label, const std::string &val)
    {
        printf("  %-20s %s\n", label, val.c_str());
    };

    auto face  = data.face_image();
    auto laser = data.laser_id();
    auto inscl = data.main_inscl();

    printf("\n=== Personal Information ===\n");
    field("CID:",         data.cid());
    field("Name (TH):",   data.name_thai());
    field("Name (EN):",   data.name_en());
    field("DOB:",         data.dob());
    field("Gender:",      data.gender());
    field("Card Issuer:", data.card_issuer());
    field("Issue Date:",  data.issue_date());
    field("Expire Date:", data.expire_date());
    field("Address:",     data.address());

    if (!face.empty())
        printf("  %-20s [%zu bytes base64]\n", "Face Image:", face.size());

    if (!laser.empty())
    {
        printf("\n=== Card Info ===\n");
        field("Laser ID:", laser);
    }

    if (!inscl.empty())
    {
        printf("\n=== NHSO Information ===\n");
        field("Main Inscl:",        data.main_inscl());
        field("Sub Inscl:",         data.sub_inscl());
        field("Main Hosp:",         data.main_hospital());
        field("Sub Hosp:",          data.sub_hospital());
        field("Paid Type:",         data.paid_type());
        field("NHSO Issue:",        data.nhso_issue_date());
        field("NHSO Expire:",       data.nhso_expire_date());
        field("NHSO Update:",       data.nhso_update_date());
        field("Change Hosp:",       data.change_hospital_amount());
    }
}

/* ---------------------------------------------------------------------------
 * Main
 * -------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
    const char *libpath = nullptr;
    const char *reader  = nullptr;  /* nullptr = auto-detect */

    if (argc > 1) libpath = argv[1];
    if (argc > 2) reader  = argv[2];

    try
    {
        std::string found_path;
        if (!libpath)
        {
            found_path = find_library();
            libpath = found_path.c_str();
        }

        printf("Using library: %s\n", libpath);
        printf("Reader:        %s\n", reader ? reader : "(auto-detect)");

        DynamicLibrary lib(libpath);

        /* Resolve all symbols */
        auto read_fn        = lib.resolve<ReadFn>("thaiid_read");
        auto free_fn        = lib.resolve<FreeFn>("thaiid_free");
        auto get_last_error = lib.resolve<GetErrorFn>("thaiid_get_last_error");

        GetStrFn getters[20] = {
            lib.resolve<GetStrFn>("thaiid_get_cid"),
            lib.resolve<GetStrFn>("thaiid_get_name_thai"),
            lib.resolve<GetStrFn>("thaiid_get_name_en"),
            lib.resolve<GetStrFn>("thaiid_get_dob"),
            lib.resolve<GetStrFn>("thaiid_get_gender"),
            lib.resolve<GetStrFn>("thaiid_get_card_issuer"),
            lib.resolve<GetStrFn>("thaiid_get_issue_date"),
            lib.resolve<GetStrFn>("thaiid_get_expire_date"),
            lib.resolve<GetStrFn>("thaiid_get_address"),
            lib.resolve<GetStrFn>("thaiid_get_face_image"),
            lib.resolve<GetStrFn>("thaiid_get_laser_id"),
            lib.resolve<GetStrFn>("thaiid_get_main_inscl"),
            lib.resolve<GetStrFn>("thaiid_get_sub_inscl"),
            lib.resolve<GetStrFn>("thaiid_get_main_hospital"),
            lib.resolve<GetStrFn>("thaiid_get_sub_hospital"),
            lib.resolve<GetStrFn>("thaiid_get_paid_type"),
            lib.resolve<GetStrFn>("thaiid_get_nhso_issue_date"),
            lib.resolve<GetStrFn>("thaiid_get_nhso_expire_date"),
            lib.resolve<GetStrFn>("thaiid_get_nhso_update_date"),
            lib.resolve<GetStrFn>("thaiid_get_change_hospital_amount"),
        };

        printf("Waiting for card...\n");

        auto *handle = read_fn(reader, 1, 1, 0);
        if (!handle)
        {
            auto err = get_last_error();
            throw std::runtime_error(err ? err : "Unknown error");
        }

        {
            CardData data(handle, free_fn, getters);
            print_card(data);
        }  /* CardData destructor calls thaiid_free */

        printf("\nDone.\n");
    }
    catch (const std::exception &e)
    {
        fprintf(stderr, "Error: %s\n", e.what());
        return 1;
    }

    return 0;
}