ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
util/parsing.c
Go to the documentation of this file.
1
6
9#include <string.h>
10#include <errno.h>
11#include <limits.h>
12#include <stdlib.h>
14#include <ascii-chat/log/log.h>
15
16asciichat_error_t safe_parse_size_message(const char *message, unsigned int *width, unsigned int *height) {
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}
63
64asciichat_error_t safe_parse_audio_message(const char *message, unsigned int *num_samples) {
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}
92
93/* ============================================================================
94 * Integer Parsing Functions
95 * ============================================================================
96 */
97
98asciichat_error_t parse_long(const char *str, long *out_value, long min_value, long max_value) {
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}
138
139asciichat_error_t parse_ulong(const char *str, unsigned long *out_value, unsigned long min_value,
140 unsigned long max_value) {
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}
180
181asciichat_error_t parse_ulonglong(const char *str, unsigned long long *out_value, unsigned long long min_value,
182 unsigned long long max_value) {
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}
222
223asciichat_error_t parse_port(const char *str, uint16_t *out_port) {
224 if (!str) {
225 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
226 }
227
228 if (!out_port) {
229 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
230 }
231
232 if (*str == '\0') {
233 return SET_ERRNO(ERROR_INVALID_PARAM, "Empty string cannot be parsed as port");
234 }
235
236 if (str[0] == '0' && str[1] != '\0') {
237 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (leading zeros)", str);
238 }
239 unsigned long port_value = 0;
240 for (const char *p = str; *p; p++) {
241 if (*p < '0' || *p > '9') {
242 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (must be 0-65535)", str);
243 }
244 port_value = port_value * 10 + (unsigned long)(*p - '0');
245 if (port_value > 65535) {
246 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid port number: %s (must be 0-65535)", str);
247 }
248 }
249
250 *out_port = (uint16_t)port_value;
251 return ASCIICHAT_OK;
252}
253
254asciichat_error_t parse_int32(const char *str, int32_t *out_value, int32_t min_value, int32_t max_value) {
255 if (!str) {
256 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
257 }
258
259 if (!out_value) {
260 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
261 }
262
263 long value;
264 asciichat_error_t err = parse_long(str, &value, (long)min_value, (long)max_value);
265 if (err != ASCIICHAT_OK) {
266 return err;
267 }
268
269 *out_value = (int32_t)value;
270 return ASCIICHAT_OK;
271}
272
273asciichat_error_t parse_uint32(const char *str, uint32_t *out_value, uint32_t min_value, uint32_t max_value) {
274 if (!str) {
275 return SET_ERRNO(ERROR_INVALID_PARAM, "String pointer is NULL");
276 }
277
278 if (!out_value) {
279 return SET_ERRNO(ERROR_INVALID_PARAM, "Output pointer is NULL");
280 }
281
282 unsigned long value;
283 asciichat_error_t err = parse_ulong(str, &value, (unsigned long)min_value, (unsigned long)max_value);
284 if (err != ASCIICHAT_OK) {
285 return err;
286 }
287
288 *out_value = (uint32_t)value;
289 return ASCIICHAT_OK;
290}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
unsigned short uint16_t
Definition common.h:61
unsigned int uint32_t
Definition common.h:62
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define ERANGE
int errno
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