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 File Reference

PCRE2 compilation and match helpers. More...

Go to the source code of this file.

Macros

#define UTIL_PCRE2_TEMP_BUFFER_SIZE   64

Functions

pcre2_code * asciichat_pcre2_compile (const char *pattern, uint32_t flags)
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.
char * asciichat_pcre2_extract_group (pcre2_match_data *match_data, int group_num, const char *subject)
 Extract numbered capture group as allocated string.
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).
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.
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)

Detailed Description

PCRE2 compilation and match helpers.

Definition in file pcre2.c.

Macro Definition Documentation

◆ UTIL_PCRE2_TEMP_BUFFER_SIZE

#define UTIL_PCRE2_TEMP_BUFFER_SIZE   64

Definition at line 8 of file pcre2.c.

Referenced by asciichat_pcre2_extract_group_ulong().

Function Documentation

◆ asciichat_pcre2_compile()

pcre2_code * asciichat_pcre2_compile ( const char * pattern,
uint32_t flags )

Compile a pattern owned by the caller, who must call pcre2_code_free().

Definition at line 16 of file pcre2.c.

16 {
17 int error;
18 PCRE2_SIZE offset;
19 return pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, flags, &error, &offset, NULL);
20}

Referenced by grep_init(), log_search_enter_mode(), log_search_exit_mode(), and log_search_init().

◆ asciichat_pcre2_extract_group()

char * asciichat_pcre2_extract_group ( pcre2_match_data * match_data,
int group_num,
const char * subject )

Extract numbered capture group as allocated string.

Extracts a numbered capture group from match data and returns an allocated string. The caller is responsible for freeing the returned string with SAFE_FREE().

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
Returns
Allocated string containing the matched substring, or NULL if group not matched

Example:

char *foundation = asciichat_pcre2_extract_group(match_data, 1, line);
if (foundation) {
// Use foundation...
SAFE_FREE(foundation);
}
#define SAFE_FREE(ptr)
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

Definition at line 72 of file pcre2.c.

72 {
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}
#define SAFE_MALLOC(size, cast)
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628

References log_error, and SAFE_MALLOC.

◆ asciichat_pcre2_extract_group_ptr()

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).

Returns a pointer to the matched substring within the original subject string. No memory allocation occurs - the returned pointer is only valid as long as the subject string remains valid.

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
out_lenOutput parameter for substring length
Returns
Pointer to matched substring within subject, or NULL if group not matched

Example:

size_t len;
const char *video_id = asciichat_pcre2_extract_group_ptr(match_data, 1, url, &len);
if (video_id) {
printf("Video ID: %.*s\n", (int)len, video_id);
}
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

Definition at line 103 of file pcre2.c.

103 {
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}

Referenced by stun_servers_parse().

◆ asciichat_pcre2_extract_group_ulong()

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.

Extracts a numbered capture group and converts it to an unsigned long integer. Handles extraction and parsing in one step.

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
out_valueOutput parameter for parsed integer value
Returns
true if extraction and parsing succeeded, false otherwise

Example:

unsigned long component_id;
if (asciichat_pcre2_extract_group_ulong(match_data, 2, line, &component_id)) {
candidate->component_id = (uint32_t)component_id;
}
unsigned int uint32_t
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

Definition at line 124 of file pcre2.c.

124 {
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}
#define UTIL_PCRE2_TEMP_BUFFER_SIZE
Definition pcre2.c:8

References UTIL_PCRE2_TEMP_BUFFER_SIZE.

◆ asciichat_pcre2_extract_named_group()

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.

Extracts a named capture group from match data and returns an allocated string. The caller is responsible for freeing the returned string with SAFE_FREE().

Parameters
regexCompiled PCRE2 regex with named groups
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_nameName of the capture group to extract
subjectOriginal subject string that was matched
Returns
Allocated string containing the matched substring, or NULL if group not matched

Definition at line 34 of file pcre2.c.

34 {
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}

References log_error, and SAFE_MALLOC.

Referenced by crypto_regex_extract_gpg_keygrip(), crypto_regex_extract_pem_base64(), crypto_regex_match_known_hosts(), crypto_regex_match_public_key(), and url_parse().

◆ asciichat_pcre2_match()

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 )

Match with the JIT fast path, using the interpreter when JIT is unavailable. Returns the native PCRE2 capture count or negative PCRE2 error code.

Definition at line 161 of file pcre2.c.

161 {
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}

Referenced by grep_highlight_colored(), grep_should_output(), stun_servers_parse(), and url_parse().