Coverage Report

Created: 2026-05-30 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/common/libscdl.c
Line
Count
Source
1
/*
2
 * libscdl.c: wrappers for dlfcn() interfaces
3
 *
4
 * Copyright (C) 2010  Martin Paljak <martin@martinpaljak.net>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include <config.h>
23
#endif
24
25
#include "libscdl.h"
26
27
#ifdef _WIN32
28
#include <shlwapi.h>
29
#include <stdio.h>
30
#include <windows.h>
31
32
void *sc_dlopen(const char *filename)
33
{
34
  DWORD flags = PathIsRelativeA(filename) ? 0 : LOAD_WITH_ALTERED_SEARCH_PATH;
35
  return (void *)LoadLibraryExA(filename, NULL, flags);
36
}
37
void *
38
sc_dlopen_deep(const char *filename)
39
{
40
  return sc_dlopen(filename);
41
}
42
43
void *sc_dlsym(void *handle, const char *symbol)
44
{
45
  return GetProcAddress((HMODULE)handle, symbol);
46
}
47
48
const char *sc_dlerror()
49
{
50
  static char msg[1024];
51
  DWORD err = GetLastError();
52
  DWORD rv = 0;
53
54
  if (err == ERROR_BAD_EXE_FORMAT) {
55
    return "LoadLibrary/GetProcAddress failed: check module architecture matches application architecture";
56
  }
57
58
  rv = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
59
      NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
60
      msg, sizeof(msg), NULL);
61
  if (rv == 0) {
62
    snprintf(msg, sizeof(msg), "LoadLibrary/GetProcAddress failed: %lx", err);
63
  }
64
  return msg;
65
}
66
67
int sc_dlclose(void *handle)
68
{
69
  return FreeLibrary((HMODULE)handle);
70
}
71
72
#else
73
74
#include <dlfcn.h>
75
76
void *sc_dlopen(const char *filename)
77
0
{
78
0
  return dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
79
0
}
80
81
void *
82
sc_dlopen_deep(const char *filename)
83
0
{
84
0
  return dlopen(filename, RTLD_LAZY | RTLD_LOCAL
85
0
#ifdef RTLD_DEEPBIND
86
0
      | RTLD_DEEPBIND
87
0
#endif
88
0
      );
89
0
}
90
91
void *sc_dlsym(void *handle, const char *symbol)
92
0
{
93
0
  return dlsym(handle, symbol);
94
0
}
95
96
const char *sc_dlerror(void)
97
0
{
98
0
  return dlerror();
99
0
}
100
101
int sc_dlclose(void *handle)
102
0
{
103
0
  return dlclose(handle);
104
0
}
105
#endif