ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
parsing.c File Reference

🔍 Safe string parsing utilities for integers, sizes, and protocol messages More...

Go to the source code of this file.

Functions

asciichat_error_t safe_parse_size_message (const char *message, unsigned int *width, unsigned int *height)
 
asciichat_error_t safe_parse_audio_message (const char *message, unsigned int *num_samples)
 
asciichat_error_t parse_long (const char *str, long *out_value, long min_value, long max_value)
 
asciichat_error_t parse_ulong (const char *str, unsigned long *out_value, unsigned long min_value, unsigned long max_value)
 
asciichat_error_t parse_ulonglong (const char *str, unsigned long long *out_value, unsigned long long min_value, unsigned long long max_value)
 
asciichat_error_t parse_port (const char *str, uint16_t *out_port)
 
asciichat_error_t parse_int32 (const char *str, int32_t *out_value, int32_t min_value, int32_t max_value)
 
asciichat_error_t parse_uint32 (const char *str, uint32_t *out_value, uint32_t min_value, uint32_t max_value)
 

Detailed Description

🔍 Safe string parsing utilities for integers, sizes, and protocol messages

Definition in file parsing.c.

Function Documentation

◆ parse_int32()

asciichat_error_t parse_int32 ( const char *  str,
int32_t *  out_value,
int32_t  min_value,
int32_t  max_value 
)

Definition at line 303 of file parsing.c.

303 {
304 if (!str) {
305 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
306 }
307
308 if (!out_value) {
309 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
310 }
311
312 long value;
313 asciichat_error_t err = parse_long(str, &value, (long)min_value, (long)max_value);
314 if (err != ASCIICHAT_OK) {
315 return err;
316 }
317
318 *out_value = (int32_t)value;
319 return ASCIICHAT_OK;
320}
asciichat_error_t parse_long(const char *str, long *out_value, long min_value, long max_value)
Definition parsing.c:98

References parse_long().

Referenced by strtoint_safe().

◆ parse_long()

asciichat_error_t parse_long ( const char *  str,
long *  out_value,
long  min_value,
long  max_value 
)

Definition at line 98 of file parsing.c.

98 {
99 if (!str) {
100 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
101 }
102
103 if (!out_value) {
104 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
105 }
106
107 if (*str == '\0') {
108 return SET_ERRNO(ERROR_INVALID_PARAM, "Empty string cannot be parsed as integer");
109 }
110
111 // Clear errno before calling strtol to detect errors
112 errno = 0;
113 char *endptr = NULL;
114 long value = strtol(str, &endptr, 10);
115
116 // Check for conversion errors
117 if (errno == ERANGE) {
118 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s", str);
119 }
120
121 // Check that entire string was consumed (no extra characters)
122 if (endptr == str || *endptr != '\0') {
123 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
124 }
125
126 // Check user-specified range
127 if (value < min_value) {
128 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value %ld is below minimum %ld", value, min_value);
129 }
130
131 if (value > max_value) {
132 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value %ld exceeds maximum %ld", value, max_value);
133 }
134
135 *out_value = value;
136 return ASCIICHAT_OK;
137}

Referenced by parse_int32().

◆ parse_port()

asciichat_error_t parse_port ( const char *  str,
uint16_t *  out_port 
)

Definition at line 251 of file parsing.c.

251 {
252 if (!str) {
253 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
254 }
255
256 if (!out_port) {
257 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
258 }
259
260 if (*str == '\0') {
261 return SET_ERRNO(ERROR_INVALID_PARAM, "Empty string cannot be parsed as port");
262 }
263
264 pcre2_code *regex = port_regex_get();
265 if (!regex) {
266 /* Fallback: use original parse_ulong method if PCRE2 not available */
267 unsigned long port_ulong;
268 asciichat_error_t err = parse_ulong(str, &port_ulong, 1, 65535);
269 if (err != ASCIICHAT_OK) {
270 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (must be 1-65535)", str);
271 }
272 *out_port = (uint16_t)port_ulong;
273 return ASCIICHAT_OK;
274 }
275
276 /* Validate port using PCRE2 regex */
277 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
278 if (!match_data) {
279 log_error("Failed to allocate match data for port validation");
280 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (must be 1-65535)", str);
281 }
282
283 int match_result = pcre2_jit_match(regex, (PCRE2_SPTR8)str, strlen(str), 0, 0, match_data, NULL);
284 pcre2_match_data_free(match_data);
285
286 if (match_result < 0) {
287 /* Format validation failed */
288 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (must be 1-65535)", str);
289 }
290
291 /* Extract matched port string and convert to uint16_t */
292 char *endptr = NULL;
293 unsigned long port_value = strtoul(str, &endptr, 10);
294
295 if (port_value < 1 || port_value > 65535) {
296 return SET_ERRNO(ERROR_INVALID_PARAM, "Port out of range: %lu", port_value);
297 }
298
299 *out_port = (uint16_t)port_value;
300 return ASCIICHAT_OK;
301}
asciichat_error_t parse_ulong(const char *str, unsigned long *out_value, unsigned long min_value, unsigned long max_value)
Definition parsing.c:139

References parse_ulong().

Referenced by main(), parse_address_with_optional_port(), parse_ip_with_port(), parse_port_option(), validate_opt_port(), and validate_port_opt().

◆ parse_uint32()

asciichat_error_t parse_uint32 ( const char *  str,
uint32_t *  out_value,
uint32_t  min_value,
uint32_t  max_value 
)

Definition at line 322 of file parsing.c.

322 {
323 if (!str) {
324 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
325 }
326
327 if (!out_value) {
328 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
329 }
330
331 unsigned long value;
332 asciichat_error_t err = parse_ulong(str, &value, (unsigned long)min_value, (unsigned long)max_value);
333 if (err != ASCIICHAT_OK) {
334 return err;
335 }
336
337 *out_value = (uint32_t)value;
338 return ASCIICHAT_OK;
339}

References parse_ulong().

◆ parse_ulong()

asciichat_error_t parse_ulong ( const char *  str,
unsigned long *  out_value,
unsigned long  min_value,
unsigned long  max_value 
)

Definition at line 139 of file parsing.c.

140 {
141 if (!str) {
142 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
143 }
144
145 if (!out_value) {
146 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
147 }
148
149 if (*str == '\0') {
150 return SET_ERRNO(ERROR_INVALID_PARAM, "Empty string cannot be parsed as integer");
151 }
152
153 // Clear errno before calling strtoul to detect errors
154 errno = 0;
155 char *endptr = NULL;
156 unsigned long value = strtoul(str, &endptr, 10);
157
158 // Check for conversion errors
159 if (errno == ERANGE) {
160 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s", str);
161 }
162
163 // Check that entire string was consumed (no extra characters)
164 if (endptr == str || *endptr != '\0') {
165 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
166 }
167
168 // Check user-specified range
169 if (value < min_value) {
170 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value %lu is below minimum %lu", value, min_value);
171 }
172
173 if (value > max_value) {
174 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value %lu exceeds maximum %lu", value, max_value);
175 }
176
177 *out_value = value;
178 return ASCIICHAT_OK;
179}

Referenced by main(), parse_port(), and parse_uint32().

◆ parse_ulonglong()

asciichat_error_t parse_ulonglong ( const char *  str,
unsigned long long *  out_value,
unsigned long long  min_value,
unsigned long long  max_value 
)

Definition at line 181 of file parsing.c.

182 {
183 if (!str) {
184 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
185 }
186
187 if (!out_value) {
188 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
189 }
190
191 if (*str == '\0') {
192 return SET_ERRNO(ERROR_INVALID_PARAM, "Empty string cannot be parsed as integer");
193 }
194
195 // Clear errno before calling strtoull to detect errors
196 errno = 0;
197 char *endptr = NULL;
198 unsigned long long value = strtoull(str, &endptr, 10);
199
200 // Check for conversion errors
201 if (errno == ERANGE) {
202 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s", str);
203 }
204
205 // Check that entire string was consumed (no extra characters)
206 if (endptr == str || *endptr != '\0') {
207 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
208 }
209
210 // Check user-specified range
211 if (value < min_value) {
212 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value too small (below minimum)");
213 }
214
215 if (value > max_value) {
216 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value too large (exceeds maximum)");
217 }
218
219 *out_value = value;
220 return ASCIICHAT_OK;
221}

◆ safe_parse_audio_message()

asciichat_error_t safe_parse_audio_message ( const char *  message,
unsigned int *  num_samples 
)

Definition at line 64 of file parsing.c.

64 {
65 if (!message || !num_samples) {
66 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for audio message parsing");
67 }
68
69 // Check if message starts with "AUDIO:"
70 if (strncmp(message, "AUDIO:", 6) != 0) {
71 return SET_ERRNO(ERROR_INVALID_PARAM, "Message does not start with 'AUDIO:'");
72 }
73
74 const char *ptr = message + 6; // Skip "AUDIO:"
75 char *endptr;
76
77 // Parse number
78 errno = 0;
79 unsigned long samples = strtoul(ptr, &endptr, 10);
80 if (errno != 0 || endptr == ptr || samples > UINT_MAX || samples == 0) {
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid sample count value in audio message");
82 }
83
84 // Should end with newline or null terminator
85 if (*endptr != '\n' && *endptr != '\0') {
86 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid format: audio message should end with newline or null terminator");
87 }
88
89 *num_samples = (unsigned int)samples;
90 return ASCIICHAT_OK;
91}

◆ safe_parse_size_message()

asciichat_error_t safe_parse_size_message ( const char *  message,
unsigned int *  width,
unsigned int *  height 
)

Definition at line 16 of file parsing.c.

16 {
17 if (!message || !width || !height) {
18 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for size message parsing");
19 }
20
21 // Check if message starts with "SIZE:"
22 if (strncmp(message, "SIZE:", 5) != 0) {
23 return SET_ERRNO(ERROR_INVALID_PARAM, "Message does not start with 'SIZE:'");
24 }
25
26 const char *ptr = message + 5; // Skip "SIZE:"
27 char *endptr;
28
29 // Parse first number (width)
30 errno = 0;
31 unsigned long w = strtoul(ptr, &endptr, 10);
32 if (errno != 0 || endptr == ptr || w > UINT_MAX || w == 0) {
33 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid width value in size message");
34 }
35
36 // Check for comma separator
37 if (*endptr != ',') {
38 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing comma separator in size message");
39 }
40 ptr = endptr + 1;
41
42 // Parse second number (height)
43 errno = 0;
44 unsigned long h = strtoul(ptr, &endptr, 10);
45 if (errno != 0 || endptr == ptr || h > UINT_MAX || h == 0) {
46 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid height value in size message");
47 }
48
49 // Should end with newline or null terminator
50 if (*endptr != '\n' && *endptr != '\0') {
51 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid format: size message should end with newline or null terminator");
52 }
53
54 // Additional bounds checking
55 if (w > 65535 || h > 65535) {
56 return SET_ERRNO(ERROR_INVALID_PARAM, "Size values too large (max 65535)");
57 }
58
59 *width = (unsigned int)w;
60 *height = (unsigned int)h;
61 return ASCIICHAT_OK;
62}