ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
regex.c
Go to the documentation of this file.
1
9
11#include <recast/defer.h>
13#include <ascii-chat/common.h>
15#include <pcre2.h>
16#include <string.h>
17#include <stdlib.h>
18
19/* ═══════════════════════════════════════════════════════════════════════════
20 * PCRE2 REGEX PATTERNS
21 * ═══════════════════════════════════════════════════════════════════════════ */
22
33static const char *KNOWN_HOSTS_REGEX_PATTERN = "^(?<ip_port>\\S+)" // IP:port (non-whitespace)
34 "\\s+" // Whitespace separator
35 "(?<key_type>\\S+)" // Key type (x25519 or no-identity)
36 "(?:" // Optional key and comment group
37 "\\s+" // Whitespace separator
38 "(?<hex_key>[0-9a-fA-F]{64})?" // Optional 64-char hex key
39 "(?:\\s+(?<comment>.*))?)" // Optional comment
40 "\\s*$"; // Optional trailing whitespace
41
50static const char *SSH_PUBLIC_KEY_REGEX_PATTERN = "ssh-ed25519" // Literal prefix
51 "\\s+" // Whitespace separator
52 "(?<base64_key>[A-Za-z0-9+/]+=*)" // Base64 key with optional padding
53 "(?:\\s+(?<comment>.*))?"; // Optional comment
54
64static const char *OPENSSH_PEM_REGEX_PATTERN = "-----BEGIN OPENSSH PRIVATE KEY-----\\s*" // Header with optional whitespace
65 "(?<base64_data>[A-Za-z0-9+/=\\s]+?)" // Multiline base64 (lazy match)
66 "\\s*-----END OPENSSH PRIVATE KEY-----"; // Footer with optional whitespace
67
75static const char *GPG_KEYGRIP_REGEX_PATTERN = "^grp:(?:[^:]*:){8}(?<keygrip>[A-Fa-f0-9]{40}):"; // GPG keygrip format
76
77/* ═══════════════════════════════════════════════════════════════════════════
78 * PCRE2 REGEX SINGLETONS
79 *
80 * Patterns are compiled for each operation.
81 * Each operation owns its compiled pattern.
82 * ═══════════════════════════════════════════════════════════════════════════ */
83
87static pcre2_code *crypto_regex_get_known_hosts(void) {
88 return asciichat_pcre2_compile(KNOWN_HOSTS_REGEX_PATTERN, PCRE2_MULTILINE | PCRE2_UCP | PCRE2_UTF);
89}
90
94static pcre2_code *crypto_regex_get_ssh_public_key(void) {
95 return asciichat_pcre2_compile(SSH_PUBLIC_KEY_REGEX_PATTERN, PCRE2_CASELESS | PCRE2_UCP | PCRE2_UTF);
96}
97
101static pcre2_code *crypto_regex_get_openssh_pem(void) {
102 return asciichat_pcre2_compile(OPENSSH_PEM_REGEX_PATTERN, PCRE2_MULTILINE | PCRE2_DOTALL | PCRE2_UCP | PCRE2_UTF);
103}
104
108static pcre2_code *crypto_regex_get_gpg_keygrip(void) {
109 return asciichat_pcre2_compile(GPG_KEYGRIP_REGEX_PATTERN, PCRE2_UCP | PCRE2_UTF);
110}
111
112static int crypto_regex_match(pcre2_code *regex, PCRE2_SPTR subject, PCRE2_SIZE length, pcre2_match_data *match_data) {
113 int result = asciichat_pcre2_match(regex, subject, length, 0, 0, match_data, NULL);
114 if (result == PCRE2_ERROR_JIT_BADOPTION) {
115 result = pcre2_match(regex, subject, length, 0, 0, match_data, NULL);
116 }
117 return result;
118}
119
120static void crypto_regex_free_code(pcre2_code *regex) {
121 pcre2_code_free(regex);
122}
123
124static void crypto_regex_free_match_data(pcre2_match_data *match_data) {
125 pcre2_match_data_free(match_data);
126}
127
128/* ═══════════════════════════════════════════════════════════════════════════
129 * PUBLIC API IMPLEMENTATION
130 * ═══════════════════════════════════════════════════════════════════════════ */
131
132bool crypto_regex_match_known_hosts(const char *line, char **ip_port_out, char **key_type_out, char **hex_key_out, char **comment_out) {
133 if (!line || !ip_port_out || !key_type_out || !hex_key_out || !comment_out) {
134 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
135 return false;
136 }
137
138 pcre2_code *regex = crypto_regex_get_known_hosts();
139 if (!regex) {
140 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
141 return false;
142 }
143
144 defer(crypto_regex_free_code(regex));
145 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
146 if (!match_data) {
147 return false;
148 }
149
150 defer(crypto_regex_free_match_data(match_data));
151 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
152
153 if (rc < 0) {
154 return false;
155 }
156
157 /* Extract named groups */
158 *ip_port_out = asciichat_pcre2_extract_named_group(regex, match_data, "ip_port", line);
159 *key_type_out = asciichat_pcre2_extract_named_group(regex, match_data, "key_type", line);
160 *hex_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "hex_key", line);
161 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
162
163 /* Verify we got required fields */
164 if (!*ip_port_out || !*key_type_out) {
165 SAFE_FREE(*ip_port_out);
166 SAFE_FREE(*key_type_out);
167 SAFE_FREE(*hex_key_out);
168 SAFE_FREE(*comment_out);
169 return false;
170 }
171
172 return true;
173}
174
175bool crypto_regex_match_public_key(const char *line, char **base64_key_out, char **comment_out) {
176 if (!line || !base64_key_out || !comment_out) {
177 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
178 return false;
179 }
180
181 pcre2_code *regex = crypto_regex_get_ssh_public_key();
182 if (!regex) {
183 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
184 return false;
185 }
186
187 defer(crypto_regex_free_code(regex));
188 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
189 if (!match_data) {
190 return false;
191 }
192
193 defer(crypto_regex_free_match_data(match_data));
194 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
195
196 if (rc < 0) {
197 return false;
198 }
199
200 /* Extract named groups */
201 *base64_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_key", line);
202 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
203
204 /* Verify we got required field */
205 if (!*base64_key_out) {
206 SAFE_FREE(*base64_key_out);
207 SAFE_FREE(*comment_out);
208 return false;
209 }
210
211 return true;
212}
213
214bool crypto_regex_extract_pem_base64(const char *file_content, char **base64_data_out) {
215 if (!file_content || !base64_data_out) {
216 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
217 return false;
218 }
219
220 pcre2_code *regex = crypto_regex_get_openssh_pem();
221 if (!regex) {
222 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
223 return false;
224 }
225
226 defer(crypto_regex_free_code(regex));
227 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
228 if (!match_data) {
229 return false;
230 }
231
232 defer(crypto_regex_free_match_data(match_data));
233 int rc = crypto_regex_match(regex, (PCRE2_SPTR)file_content, strlen(file_content), match_data);
234
235 if (rc < 0) {
236 return false;
237 }
238
239 /* Extract base64 data */
240 *base64_data_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_data", file_content);
241
242 return *base64_data_out != NULL;
243}
244
245bool crypto_regex_extract_gpg_keygrip(const char *line, char **keygrip_out) {
246 if (!line || !keygrip_out) {
247 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
248 return false;
249 }
250
251 pcre2_code *regex = crypto_regex_get_gpg_keygrip();
252 if (!regex) {
253 /* Regex not available - caller should use fallback manual parsing */
254 return false;
255 }
256
257 defer(crypto_regex_free_code(regex));
258 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
259 if (!match_data) {
260 return false;
261 }
262
263 defer(crypto_regex_free_match_data(match_data));
264 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
265
266 if (rc < 0) {
267 return false;
268 }
269
270 /* Extract keygrip from named group */
271 *keygrip_out = asciichat_pcre2_extract_named_group(regex, match_data, "keygrip", line);
272
273 return *keygrip_out != NULL;
274}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
@ ERROR_INVALID_STATE
@ ERROR_INVALID_PARAM
⚙️ Common definitions, error codes, macros, and types shared throughout the application
pcre2_code * asciichat_pcre2_compile(const char *pattern, uint32_t flags)
Definition pcre2.c:16
int asciichat_pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, uint32_t options, pcre2_match_data *match_data, pcre2_match_context *context)
Definition pcre2.c:161
char * asciichat_pcre2_extract_named_group(pcre2_code *regex, pcre2_match_data *match_data, const char *group_name, const char *subject)
Extract named substring from PCRE2 match data.
Definition pcre2.c:34
PCRE2 compilation and match helpers.
bool crypto_regex_match_public_key(const char *line, char **base64_key_out, char **comment_out)
Definition regex.c:175
bool crypto_regex_extract_gpg_keygrip(const char *line, char **keygrip_out)
Definition regex.c:245
bool crypto_regex_match_known_hosts(const char *line, char **ip_port_out, char **key_type_out, char **hex_key_out, char **comment_out)
Definition regex.c:132
bool crypto_regex_extract_pem_base64(const char *file_content, char **base64_data_out)
Definition regex.c:214
PCRE2-based regex patterns for cryptographic parsing.
🔤 String Manipulation and Shell Escaping Utilities