ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
pcre2.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define UTIL_PCRE2_TEMP_BUFFER_SIZE 64
9
11#include <ascii-chat/common.h>
12#include <ascii-chat/log/log.h>
13#include <string.h>
14#include <errno.h>
15
16pcre2_code *asciichat_pcre2_compile(const char *pattern, uint32_t flags) {
17 int error;
18 PCRE2_SIZE offset;
19 return pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, flags, &error, &offset, NULL);
20}
21
34char *asciichat_pcre2_extract_named_group(pcre2_code *regex, pcre2_match_data *match_data, const char *group_name, const char *subject) {
35 if (!regex || !match_data || !group_name || !subject) {
36 log_error("invalid parameters");
37 return NULL;
38 }
39
40 /* Get group number from name */
41 int group_number = pcre2_substring_number_from_name(regex, (PCRE2_SPTR)group_name);
42 if (group_number < 0) {
43 return NULL; /* Group doesn't exist */
44 }
45
46 /* Get ovector (output vector with match offsets) */
47 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
48 PCRE2_SIZE start = ovector[2 * group_number];
49 PCRE2_SIZE end = ovector[2 * group_number + 1];
50
51 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
52 return NULL; /* Group not matched */
53 }
54
55 /* Allocate and copy substring */
56 size_t len = end - start;
57 char *result = SAFE_MALLOC(len + 1, char *);
58 if (!result) {
59 log_error("failed to allocate %zu bytes", len + 1);
60 return NULL;
61 }
62
63 memcpy(result, subject + start, len);
64 result[len] = '\0';
65
66 return result;
67}
68
72char *asciichat_pcre2_extract_group(pcre2_match_data *match_data, int group_num, const char *subject) {
73 if (!match_data || !subject || group_num < 0) {
74 return NULL;
75 }
76
77 /* Get ovector (output vector with match offsets) */
78 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
79 PCRE2_SIZE start = ovector[2 * group_num];
80 PCRE2_SIZE end = ovector[2 * group_num + 1];
81
82 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
83 return NULL; /* Group not matched */
84 }
85
86 /* Allocate and copy substring */
87 size_t len = end - start;
88 char *result = SAFE_MALLOC(len + 1, char *);
89 if (!result) {
90 log_error("failed to allocate %zu bytes", len + 1);
91 return NULL;
92 }
93
94 memcpy(result, subject + start, len);
95 result[len] = '\0';
96
97 return result;
98}
99
103const char *asciichat_pcre2_extract_group_ptr(pcre2_match_data *match_data, int group_num, const char *subject, size_t *out_len) {
104 if (!match_data || !subject || !out_len || group_num < 0) {
105 return NULL;
106 }
107
108 /* Get ovector (output vector with match offsets) */
109 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
110 PCRE2_SIZE start = ovector[2 * group_num];
111 PCRE2_SIZE end = ovector[2 * group_num + 1];
112
113 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
114 return NULL; /* Group not matched */
115 }
116
117 *out_len = end - start;
118 return subject + start;
119}
120
124bool asciichat_pcre2_extract_group_ulong(pcre2_match_data *match_data, int group_num, const char *subject, unsigned long *out_value) {
125 if (!match_data || !subject || !out_value || group_num < 0) {
126 return false;
127 }
128
129 /* Get ovector (output vector with match offsets) */
130 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
131 PCRE2_SIZE start = ovector[2 * group_num];
132 PCRE2_SIZE end = ovector[2 * group_num + 1];
133
134 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
135 return false; /* Group not matched */
136 }
137
138 /* Extract substring into temporary buffer */
139 size_t len = end - start;
140 if (len == 0 || len > 63) { /* Sanity check for reasonable number length */
141 return false;
142 }
143
145 memcpy(temp, subject + start, len);
146 temp[len] = '\0';
147
148 /* Parse as unsigned long */
149 char *endptr;
150 errno = 0;
151 unsigned long value = strtoul(temp, &endptr, 10);
152
153 if (errno != 0 || endptr == temp || *endptr != '\0') {
154 return false; /* Parse failed */
155 }
156
157 *out_value = value;
158 return true;
159}
160
161int 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) {
162 int result = pcre2_jit_match(code, subject, length, startoffset, options, match_data, context);
163 if (result == PCRE2_ERROR_JIT_BADOPTION || result == PCRE2_ERROR_JIT_STACKLIMIT)
164 return pcre2_match(code, subject, length, startoffset, options | PCRE2_NO_JIT, match_data, context);
165 return result;
166}
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
unsigned int uint32_t
#define SAFE_MALLOC(size, cast)
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
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_group(pcre2_match_data *match_data, int group_num, const char *subject)
Extract numbered capture group as allocated string.
Definition pcre2.c:72
#define UTIL_PCRE2_TEMP_BUFFER_SIZE
Definition pcre2.c:8
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
const char * asciichat_pcre2_extract_group_ptr(pcre2_match_data *match_data, int group_num, const char *subject, size_t *out_len)
Extract numbered capture group as pointer into subject (non-allocating).
Definition pcre2.c:103
bool asciichat_pcre2_extract_group_ulong(pcre2_match_data *match_data, int group_num, const char *subject, unsigned long *out_value)
Extract numbered capture group and convert to unsigned long.
Definition pcre2.c:124
PCRE2 compilation and match helpers.
🔤 String Manipulation and Shell Escaping Utilities