Debian Codegraph @

src/shared/pcre2-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-dlopen.h"
4
5 #include "dlfcn-util.h"
6 #include "hash-funcs.h"
7 #include "log.h"
8 #include "pcre2-util.h"
9
10 #if HAVE_PCRE2
11 DLSYM_PROTOTYPE(pcre2_match_data_create) = NULL;
12 DLSYM_PROTOTYPE(pcre2_match_data_free) = NULL;
13 DLSYM_PROTOTYPE(pcre2_code_free) = NULL;
14 DLSYM_PROTOTYPE(pcre2_compile) = NULL;
15 DLSYM_PROTOTYPE(pcre2_get_error_message) = NULL;
16 DLSYM_PROTOTYPE(pcre2_match) = NULL;
17 DLSYM_PROTOTYPE(pcre2_get_ovector_pointer) = NULL;
18
19 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
20 pcre2_code_hash_ops_free,
21 pcre2_code,
22 (void (*)(const pcre2_code *, struct siphash*))trivial_hash_func,
23 (int (*)(const pcre2_code *, const pcre2_code*))trivial_compare_func,
24 sym_pcre2_code_free);
25 #else
26 const struct hash_ops pcre2_code_hash_ops_free = {};
27 #endif
28
29 int dlopen_pcre2(int log_level) {
30 #if HAVE_PCRE2
31 static void *pcre2_dl = NULL;
32
33 SD_ELF_NOTE_DLOPEN(
34 "pcre2",
35 "Support for regular expressions",
36 SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
37 "libpcre2-8.so.0");
38
39 /* So here's something weird: PCRE2 actually renames the symbols exported by the library via C
40 * macros, so that the exported symbols carry a suffix "_8" but when used from C the suffix is
41 * gone. In the argument list below we ignore this mangling. Surprisingly (at least to me), we
42 * actually get away with that. That's because DLSYM_ARG() uses STRINGIFY() to generate a string
43 * version of the symbol name, and that resolves the macro mapping implicitly already, so that the
44 * string actually contains the "_8" suffix already due to that and we don't have to append it
45 * manually anymore. C is weird. 🤯 */
46
47 return dlopen_many_sym_or_warn(
48 &pcre2_dl, "libpcre2-8.so.0", log_level,
49 DLSYM_ARG(pcre2_match_data_create),
50 DLSYM_ARG(pcre2_match_data_free),
51 DLSYM_ARG(pcre2_code_free),
52 DLSYM_ARG(pcre2_compile),
53 DLSYM_ARG(pcre2_get_error_message),
54 DLSYM_ARG(pcre2_match),
55 DLSYM_ARG(pcre2_get_ovector_pointer));
56 #else
57 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
58 "PCRE2 support is not compiled in.");
59 #endif
60 }
61
62 int pattern_compile_and_log(const char *pattern, PatternCompileCase case_, pcre2_code **ret) {
63 #if HAVE_PCRE2
64 PCRE2_SIZE erroroffset;
65 _cleanup_(pcre2_code_freep) pcre2_code *p = NULL;
66 unsigned flags = 0;
67 int errorcode, r;
68
69 assert(pattern);
70
71 r = dlopen_pcre2(LOG_ERR);
72 if (r < 0)
73 return r;
74
75 if (case_ == PATTERN_COMPILE_CASE_INSENSITIVE)
76 flags = PCRE2_CASELESS;
77 else if (case_ == PATTERN_COMPILE_CASE_AUTO) {
78 _cleanup_(pcre2_match_data_freep) pcre2_match_data *md = NULL;
79 bool has_case;
80 _cleanup_(pcre2_code_freep) pcre2_code *cs = NULL;
81
82 md = sym_pcre2_match_data_create(1, NULL);
83 if (!md)
84 return log_oom();
85
86 r = pattern_compile_and_log("[[:upper:]]", PATTERN_COMPILE_CASE_SENSITIVE, &cs);
87 if (r < 0)
88 return r;
89
90 r = sym_pcre2_match(cs, (PCRE2_SPTR8) pattern, PCRE2_ZERO_TERMINATED, 0, 0, md, NULL);
91 has_case = r >= 0;
92
93 flags = !has_case * PCRE2_CASELESS;
94 }
95
96 log_debug("Doing case %s matching based on %s",
97 flags & PCRE2_CASELESS ? "insensitive" : "sensitive",
98 case_ != PATTERN_COMPILE_CASE_AUTO ? "request" : "pattern casing");
99
100 p = sym_pcre2_compile((PCRE2_SPTR8) pattern,
101 PCRE2_ZERO_TERMINATED, flags, &errorcode, &erroroffset, NULL);
102 if (!p) {
103 unsigned char buf[LINE_MAX];
104
105 r = sym_pcre2_get_error_message(errorcode, buf, sizeof buf);
106
107 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
108 "Bad pattern \"%s\": %s", pattern,
109 r < 0 ? "unknown error" : (char *)buf);
110 }
111
112 if (ret)
113 *ret = TAKE_PTR(p);
114
115 return 0;
116 #else
117 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PCRE2 support is not compiled in.");
118 #endif
119 }
120
121 int pattern_matches_and_log(pcre2_code *compiled_pattern, const char *message, size_t size, size_t *ret_ovec) {
122 #if HAVE_PCRE2
123 _cleanup_(pcre2_match_data_freep) pcre2_match_data *md = NULL;
124 int r;
125
126 assert(compiled_pattern);
127 assert(message);
128 /* pattern_compile_and_log() must be called before this function is called and that function already
129 * dlopens pcre2 so we can assert on it being available here. */
130 assert(sym_pcre2_match);
131
132 md = sym_pcre2_match_data_create(1, NULL);
133 if (!md)
134 return log_oom();
135
136 r = sym_pcre2_match(compiled_pattern,
137 (const unsigned char *)message,
138 size,
139 0, /* start at offset 0 in the subject */
140 0, /* default options */
141 md,
142 NULL);
143 if (r == PCRE2_ERROR_NOMATCH)
144 return false;
145 if (r < 0) {
146 unsigned char buf[LINE_MAX];
147
148 r = sym_pcre2_get_error_message(r, buf, sizeof(buf));
149 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Pattern matching failed: %s",
150 r < 0 ? "unknown error" : (char*) buf);
151 }
152
153 if (ret_ovec) {
154 ret_ovec[0] = sym_pcre2_get_ovector_pointer(md)[0];
155 ret_ovec[1] = sym_pcre2_get_ovector_pointer(md)[1];
156 }
157
158 return true;
159 #else
160 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PCRE2 support is not compiled in.");
161 #endif
162 }