12void escape_ascii(
const char *str,
const char *escape_char,
char *out_buffer,
size_t out_buffer_size) {
13 if (escape_char == NULL || str == NULL || !out_buffer || out_buffer_size == 0) {
19 size_t escape_count = 0;
20 for (
size_t i = 0; i < strlen(str); i++) {
21 for (
size_t j = 0; j < strlen(escape_char); j++) {
22 if (str[i] == escape_char[j]) {
29 size_t total_len = strlen(str) + escape_count;
30 if (out_buffer_size < total_len) {
36 for (
size_t i = 0; i < strlen(str); i++) {
37 bool needs_escape =
false;
38 for (
size_t j = 0; j < strlen(escape_char); j++) {
39 if (str[i] == escape_char[j]) {
46 out_buffer[out_pos++] =
'\\';
48 out_buffer[out_pos++] = str[i];
51 out_buffer[out_pos] =
'\0';
61 const char *dangerous =
";|$`\\\"'<>()[]{}*?!~#@ \t\n\r";
63 for (
size_t i = 0; str[i] !=
'\0'; i++) {
64 unsigned char c = (
unsigned char)str[i];
73 bool is_allowed =
false;
74 for (
size_t j = 0; allowed_chars[j] !=
'\0'; j++) {
75 if (c == (
unsigned char)allowed_chars[j]) {
86 bool is_dangerous =
false;
87 for (
size_t j = 0; dangerous[j] !=
'\0'; j++) {
88 if (c == (
unsigned char)dangerous[j]) {
102 if (!str || !out_buffer || out_buffer_size == 0) {
108 size_t required_size = 2;
109 for (
size_t i = 0; str[i] !=
'\0'; i++) {
110 if (str[i] ==
'\'') {
117 if (out_buffer_size < required_size) {
123 out_buffer[out_pos++] =
'\'';
125 for (
size_t i = 0; str[i] !=
'\0'; i++) {
126 if (str[i] ==
'\'') {
128 out_buffer[out_pos++] =
'\'';
129 out_buffer[out_pos++] =
'\\';
130 out_buffer[out_pos++] =
'\'';
131 out_buffer[out_pos++] =
'\'';
133 out_buffer[out_pos++] = str[i];
137 out_buffer[out_pos++] =
'\'';
138 out_buffer[out_pos] =
'\0';
144 if (!str || !out_buffer || out_buffer_size == 0) {
151 size_t required_size = 2;
152 for (
size_t i = 0; str[i] !=
'\0'; i++) {
153 if (str[i] ==
'"' || str[i] ==
'\\') {
160 if (out_buffer_size < required_size) {
166 out_buffer[out_pos++] =
'"';
168 for (
size_t i = 0; str[i] !=
'\0'; i++) {
171 out_buffer[out_pos++] =
'\\';
172 out_buffer[out_pos++] =
'"';
173 }
else if (str[i] ==
'\\') {
175 out_buffer[out_pos++] =
'\\';
176 out_buffer[out_pos++] =
'\\';
178 out_buffer[out_pos++] = str[i];
182 out_buffer[out_pos++] =
'"';
183 out_buffer[out_pos] =
'\0';
189 if (!str || str[0] ==
'\0') {
194 for (
size_t i = 0; str[i] !=
'\0'; i++) {
198 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r') {
203 if (c ==
'"' || c ==
'\'' || c ==
'$' || c ==
'`' || c ==
'\\' || c ==
'<' || c ==
'>' || c ==
'&' || c ==
';' ||
204 c ==
'|' || c ==
'(' || c ==
')' || c ==
'[' || c ==
']' || c ==
'{' || c ==
'}' || c ==
'*' || c ==
'?' ||
205 c ==
'!' || c ==
'~' || c ==
'#' || c ==
'@') {
214 if (!path || !out_buffer || out_buffer_size == 0) {
222 size_t path_len = strlen(path);
223 if (out_buffer_size <= path_len) {
#define SAFE_STRNCPY(dst, src, size)
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
bool escape_shell_double_quotes(const char *str, char *out_buffer, size_t out_buffer_size)
Escape a string for safe use in shell commands (double quotes, Windows-compatible)
bool escape_shell_single_quotes(const char *str, char *out_buffer, size_t out_buffer_size)
Escape a string for safe use in shell commands (single quotes)
bool string_needs_shell_quoting(const char *str)
Check if a string needs shell quoting (contains spaces or special chars)
bool validate_shell_safe(const char *str, const char *allowed_chars)
Validate that a string contains only safe characters for shell commands.
void escape_ascii(const char *str, const char *escape_char, char *out_buffer, size_t out_buffer_size)
Escape ASCII characters in a string.
bool escape_path_for_shell(const char *path, char *out_buffer, size_t out_buffer_size)
Escape a path for safe use in shell commands (auto-platform)
🔤 String Manipulation and Shell Escaping Utilities