ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
util/parsing.c
Go to the documentation of this file.
1
7
12#include <errno.h>
13#include <libflags/values.h>
14#include <limits.h>
15#include <stdlib.h>
16#include <string.h>
17
19 unsigned int *width,
20 unsigned int *height) {
21 if (!message || !width || !height) {
23 "Invalid parameters for size message parsing");
24 }
25
26 // Check if message starts with "SIZE:"
27 if (strncmp(message, "SIZE:", 5) != 0) {
29 "Message does not start with 'SIZE:'");
30 }
31
32 const char *ptr = message + 5; // Skip "SIZE:"
33 char *endptr;
34
35 // Parse first number (width)
36 errno = 0;
37 unsigned long w = strtoul(ptr, &endptr, 10);
38 if (errno != 0 || endptr == ptr || w > UINT_MAX || w == 0) {
40 "Invalid width value in size message");
41 }
42
43 // Check for comma separator
44 if (*endptr != ',') {
46 "Missing comma separator in size message");
47 }
48 ptr = endptr + 1;
49
50 // Parse second number (height)
51 errno = 0;
52 unsigned long h = strtoul(ptr, &endptr, 10);
53 if (errno != 0 || endptr == ptr || h > UINT_MAX || h == 0) {
55 "Invalid height value in size message");
56 }
57
58 // Should end with newline or null terminator
59 if (*endptr != '\n' && *endptr != '\0') {
61 "Invalid format: size message should end with newline or "
62 "null terminator");
63 }
64
65 // Additional bounds checking
66 if (w > 65535 || h > 65535) {
67 return SET_ERRNO(ERROR_INVALID_PARAM, "Size values too large (max 65535)");
68 }
69
70 *width = (unsigned int)w;
71 *height = (unsigned int)h;
72 return ASCIICHAT_OK;
73}
74
76 unsigned int *num_samples) {
77 if (!message || !num_samples) {
79 "Invalid parameters for audio message parsing");
80 }
81
82 // Check if message starts with "AUDIO:"
83 if (strncmp(message, "AUDIO:", 6) != 0) {
85 "Message does not start with 'AUDIO:'");
86 }
87
88 const char *ptr = message + 6; // Skip "AUDIO:"
89 char *endptr;
90
91 // Parse number
92 errno = 0;
93 unsigned long samples = strtoul(ptr, &endptr, 10);
94 if (errno != 0 || endptr == ptr || samples > UINT_MAX || samples == 0) {
96 "Invalid sample count value in audio message");
97 }
98
99 // Should end with newline or null terminator
100 if (*endptr != '\n' && *endptr != '\0') {
102 "Invalid format: audio message should end with newline or "
103 "null terminator");
104 }
105
106 *num_samples = (unsigned int)samples;
107 return ASCIICHAT_OK;
108}
109
110/* ============================================================================
111 * Integer Parsing Functions
112 * ============================================================================
113 */
114
115asciichat_error_t parse_long(const char *str, long *out_value, long min_value,
116 long max_value) {
117 if (!str) {
118 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
119 }
120
121 if (!out_value) {
122 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
123 }
124
125 if (*str == '\0') {
127 "Empty string cannot be parsed as integer");
128 }
129
130 // Clear errno before calling strtol to detect errors
131 errno = 0;
132 char *endptr = NULL;
133 long value = strtol(str, &endptr, 10);
134
135 // Check for conversion errors
136 if (errno == ERANGE) {
137 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s",
138 str);
139 }
140
141 // Check that entire string was consumed (no extra characters)
142 if (endptr == str || *endptr != '\0') {
143 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
144 }
145
146 // Check user-specified range
147 if (value < min_value) {
149 "Integer value %ld is below minimum %ld", value,
150 min_value);
151 }
152
153 if (value > max_value) {
155 "Integer value %ld exceeds maximum %ld", value, max_value);
156 }
157
158 *out_value = value;
159 return ASCIICHAT_OK;
160}
161
162asciichat_error_t parse_ulong(const char *str, unsigned long *out_value,
163 unsigned long min_value,
164 unsigned long max_value) {
165 if (!str) {
166 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
167 }
168
169 if (!out_value) {
170 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
171 }
172
173 if (*str == '\0') {
175 "Empty string cannot be parsed as integer");
176 }
177
178 // Clear errno before calling strtoul to detect errors
179 errno = 0;
180 char *endptr = NULL;
181 unsigned long value = strtoul(str, &endptr, 10);
182
183 // Check for conversion errors
184 if (errno == ERANGE) {
185 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s",
186 str);
187 }
188
189 // Check that entire string was consumed (no extra characters)
190 if (endptr == str || *endptr != '\0') {
191 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
192 }
193
194 // Check user-specified range
195 if (value < min_value) {
197 "Integer value %lu is below minimum %lu", value,
198 min_value);
199 }
200
201 if (value > max_value) {
203 "Integer value %lu exceeds maximum %lu", value, max_value);
204 }
205
206 *out_value = value;
207 return ASCIICHAT_OK;
208}
209
211 unsigned long long *out_value,
212 unsigned long long min_value,
213 unsigned long long max_value) {
214 if (!str) {
215 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
216 }
217
218 if (!out_value) {
219 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
220 }
221
222 if (*str == '\0') {
224 "Empty string cannot be parsed as integer");
225 }
226
227 // Clear errno before calling strtoull to detect errors
228 errno = 0;
229 char *endptr = NULL;
230 unsigned long long value = strtoull(str, &endptr, 10);
231
232 // Check for conversion errors
233 if (errno == ERANGE) {
234 return SET_ERRNO(ERROR_INVALID_PARAM, "Integer value out of range: %s",
235 str);
236 }
237
238 // Check that entire string was consumed (no extra characters)
239 if (endptr == str || *endptr != '\0') {
240 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid integer format: %s", str);
241 }
242
243 // Check user-specified range
244 if (value < min_value) {
246 "Integer value too small (below minimum)");
247 }
248
249 if (value > max_value) {
251 "Integer value too large (exceeds maximum)");
252 }
253
254 *out_value = value;
255 return ASCIICHAT_OK;
256}
257
258asciichat_error_t parse_port(const char *str, uint16_t *out_port) {
259 if (!str) {
260 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
261 }
262
263 if (!out_port) {
264 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
265 }
266
267 if (*str == '\0') {
269 "Empty string cannot be parsed as port");
270 }
271
272 if (str[0] == '0' && str[1] != '\0') {
274 "Invalid port number: %s (leading zeros)", str);
275 }
276 if (!flags_parse_port(str, out_port))
278 "Invalid port number: %s (must be 0-65535)", str);
279 return ASCIICHAT_OK;
280}
281
282asciichat_error_t parse_int32(const char *str, int32_t *out_value,
283 int32_t min_value, int32_t max_value) {
284 if (!str) {
285 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
286 }
287
288 if (!out_value) {
289 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
290 }
291
292 long value = 0;
294 parse_long(str, &value, (long)min_value, (long)max_value);
295 if (err != ASCIICHAT_OK) {
296 return err;
297 }
298
299 *out_value = (int32_t)value;
300 return ASCIICHAT_OK;
301}
302
303asciichat_error_t parse_uint32(const char *str, uint32_t *out_value,
304 uint32_t min_value, uint32_t max_value) {
305 if (!str) {
306 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
307 }
308
309 if (!out_value) {
310 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
311 }
312
313 unsigned long value = 0;
314 asciichat_error_t err = parse_ulong(str, &value, (unsigned long)min_value,
315 (unsigned long)max_value);
316 if (err != ASCIICHAT_OK) {
317 return err;
318 }
319
320 *out_value = (uint32_t)value;
321 return ASCIICHAT_OK;
322}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned short uint16_t
unsigned int uint32_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
asciichat_error_t safe_parse_size_message(const char *message, unsigned int *width, unsigned int *height)
Parse SIZE message format.
asciichat_error_t safe_parse_audio_message(const char *message, unsigned int *num_samples)
Parse AUDIO message format.
📝 Logging API with multiple log levels and terminal output control
PCRE2 compilation and match helpers.
🔤 String Manipulation and Shell Escaping Utilities
asciichat_error_t parse_ulonglong(const char *str, unsigned long long *out_value, unsigned long long min_value, unsigned long long max_value)
Parse unsigned long long integer with range validation.
asciichat_error_t parse_uint32(const char *str, uint32_t *out_value, uint32_t min_value, uint32_t max_value)
Parse unsigned 32-bit integer with range validation.
asciichat_error_t parse_ulong(const char *str, unsigned long *out_value, unsigned long min_value, unsigned long max_value)
Parse unsigned long integer with range validation.
asciichat_error_t parse_long(const char *str, long *out_value, long min_value, long max_value)
Parse signed long integer with range validation.
asciichat_error_t parse_int32(const char *str, int32_t *out_value, int32_t min_value, int32_t max_value)
Parse signed 32-bit integer with range validation.
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.
🔍 Safe Parsing Utilities