ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
media/input.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
4#include <ctype.h>
5#include <string.h>
6
7static int hex_digit(unsigned char c) {
8 if (c >= '0' && c <= '9')
9 return c - '0';
10 c = (unsigned char)tolower(c);
11 return c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
12}
13
14asciichat_error_t media_input_normalize(const char *text, char *output,
15 size_t capacity) {
16 if (!text || !output || capacity < 2)
17 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid media input buffer");
18 output[0] = '\0';
19 while (isspace((unsigned char)*text))
20 ++text;
21 size_t length = strlen(text);
22 while (length && isspace((unsigned char)text[length - 1]))
23 --length;
24 if (!length)
25 return SET_ERRNO(ERROR_INVALID_PARAM, "Enter one media path or URL");
26 if (length > 2 && text[0] == '&' && text[1] == ' ') {
27 text += 2;
28 length -= 2;
29 }
30 char quote = (*text == '\'' || *text == '"') ? *text : 0;
31 if (quote) {
32 if (length < 2 || text[length - 1] != quote)
34 "Choose one file with matching quotes");
35 ++text;
36 length -= 2;
37 }
38 bool uri =
39 length >= 7 && strncmp(text, "file://", sizeof("file://") - 1) == 0;
40 bool url = !uri && (strstr(text, "://") != NULL);
41 if (uri) {
42 text += 7;
43 length -= 7;
44 if (length >= 10 &&
45 strncmp(text, "localhost/", sizeof("localhost/") - 1) == 0) {
46 text += 9;
47 length -= 9;
48 } else if (*text != '/') {
50 "Only local file URIs are supported");
51 }
52#ifdef _WIN32
53 if (length >= 3 && text[0] == '/' && isalpha((unsigned char)text[1]) &&
54 text[2] == ':') {
55 ++text;
56 --length;
57 }
58#endif
59 }
60 bool windows_path =
61 length >= 2 && (text[1] == ':' || (text[0] == '\\' && text[1] == '\\'));
62 size_t used = 0;
63 size_t i = 0;
64 while (i < length) {
65 unsigned char c = (unsigned char)text[i];
66 if (uri && c == '%') {
67 if (i + 2 >= length || hex_digit((unsigned char)text[i + 1]) < 0 ||
68 hex_digit((unsigned char)text[i + 2]) < 0)
69 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid escape in file URI");
70 c = (unsigned char)(hex_digit((unsigned char)text[i + 1]) * 16 +
71 hex_digit((unsigned char)text[i + 2]));
72 i += 2;
73 } else if (!url && !uri && !quote && !windows_path && c == '\\' &&
74 i + 1 < length &&
75 (text[i + 1] == ' ' || text[i + 1] == '\'' ||
76 text[i + 1] == '"' || text[i + 1] == '\\')) {
77 c = (unsigned char)text[++i];
78 } else if (!url && !uri && (c == '\'' || c == '"')) {
79 if (quote && c == (unsigned char)quote)
81 "Choose one media file at a time");
82 }
83 if (c < 32 || c == 127)
84 return SET_ERRNO(
86 "Choose one media path or URL without control characters");
87 if (used + 1 >= capacity)
88 return SET_ERRNO(ERROR_INVALID_PARAM, "Media path or URL is too long");
89 output[used++] = (char)c;
90 ++i;
91 }
92 output[used] = '\0';
93 if (!utf8_is_valid(output))
95 "Media path or URL is not valid UTF-8");
96 return used ? ASCIICHAT_OK
97 : SET_ERRNO(ERROR_INVALID_PARAM, "Empty media path");
98}
#define SET_ERRNO(code,...)
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
bool utf8_is_valid(const char *str)
Check if a string is valid UTF-8.
Definition utf8.c:132
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t media_input_normalize(const char *text, char *output, size_t capacity)
Definition media/input.c:14
🔤 String Manipulation and Shell Escaping Utilities
🔤 UTF-8 Encoding and Decoding Utilities