]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/idn-util.c
75d815df38d1718c2a6c720c79f75f10e473bbe1
[thirdparty/systemd.git] / src / shared / idn-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if HAVE_LIBIDN2
4 # include <idn2.h>
5 #elif HAVE_LIBIDN
6 # include <idna.h>
7 # include <stringprep.h>
8 #endif
9
10 #include "alloc-util.h"
11 #include "dlfcn-util.h"
12 #include "idn-util.h"
13
14 #if HAVE_LIBIDN || HAVE_LIBIDN2
15 static void* idn_dl = NULL;
16 #endif
17
18 #if HAVE_LIBIDN2
19 int (*sym_idn2_lookup_u8)(const uint8_t* src, uint8_t** lookupname, int flags) = NULL;
20 const char *(*sym_idn2_strerror)(int rc) = NULL;
21 int (*sym_idn2_to_unicode_8z8z)(const char * input, char ** output, int flags) = NULL;
22
23 int dlopen_idn(void) {
24 _cleanup_(dlclosep) void *dl = NULL;
25 int r;
26
27 if (idn_dl)
28 return 0; /* Already loaded */
29
30 dl = dlopen("libidn2.so.0", RTLD_LAZY);
31 if (!dl)
32 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
33 "libidn2 support is not installed: %s", dlerror());
34
35 r = dlsym_many_and_warn(
36 dl,
37 LOG_DEBUG,
38 &sym_idn2_lookup_u8, "idn2_lookup_u8",
39 &sym_idn2_strerror, "idn2_strerror",
40 &sym_idn2_to_unicode_8z8z, "idn2_to_unicode_8z8z",
41 NULL);
42 if (r < 0)
43 return r;
44
45 /* Note that we never release the reference here, because there's no real reason to, after all this
46 * was traditionally a regular shared library dependency which lives forever too. */
47 idn_dl = TAKE_PTR(dl);
48
49 return 1;
50 }
51 #endif
52
53 #if HAVE_LIBIDN
54 int (*sym_idna_to_ascii_4i)(const uint32_t * in, size_t inlen, char *out, int flags);
55 int (*sym_idna_to_unicode_44i)(const uint32_t * in, size_t inlen,uint32_t * out, size_t * outlen, int flags);
56 char* (*sym_stringprep_ucs4_to_utf8)(const uint32_t * str, ssize_t len, size_t * items_read, size_t * items_written);
57 uint32_t* (*sym_stringprep_utf8_to_ucs4)(const char *str, ssize_t len, size_t *items_written);
58
59 int dlopen_idn(void) {
60 _cleanup_(dlclosep) void *dl = NULL;
61 int r;
62
63 if (idn_dl)
64 return 0; /* Already loaded */
65
66 dl = dlopen("libidn.so.12", RTLD_LAZY);
67 if (!dl) {
68 /* libidn broke ABI in 1.34, but not in a way we care about (a new field got added to an
69 * open-coded struct we do not use), hence support both versions. */
70 dl = dlopen("libidn.so.11", RTLD_LAZY);
71 if (!dl)
72 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
73 "libidn support is not installed: %s", dlerror());
74 }
75
76 r = dlsym_many_and_warn(
77 dl,
78 LOG_DEBUG,
79 &sym_idna_to_ascii_4i, "idna_to_ascii_4i",
80 &sym_idna_to_unicode_44i, "idna_to_unicode_44i",
81 &sym_stringprep_ucs4_to_utf8, "stringprep_ucs4_to_utf8",
82 &sym_stringprep_utf8_to_ucs4, "stringprep_utf8_to_ucs4",
83 NULL);
84 if (r < 0)
85 return r;
86
87 idn_dl = TAKE_PTR(dl);
88
89 return 1;
90 }
91 #endif