-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_helpers.c
51 lines (35 loc) · 900 Bytes
/
str_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include "str_helpers.h"
size_t sanitize_str(char *in, char **out) {
char *writep;
size_t string_len = 0;
char *buf_pass = in;
char *copy_pass = in;
*out = NULL;
while (*buf_pass) {
if ((((int) *buf_pass >= 0x00 && (int) *buf_pass <= 0x1F) || (int) *buf_pass == 0x7F)) {
buf_pass++;
continue;
}
if (*buf_pass == '"' || *buf_pass == '\\')
string_len++;
string_len++;
buf_pass++;
}
if (string_len == 0)
return 0;
*out = (char *) calloc(string_len + 1, sizeof(char));
writep = *out;
if (*out == NULL)
return 0;
while (*copy_pass) {
if (((int) *copy_pass >= 0x00 && (int) *copy_pass <= 0x1F) || (int) *copy_pass == 0x7F) {
copy_pass++;
continue;
}
if (*copy_pass == '"' || *copy_pass == '\\')
*writep++ = '\\';
*writep++ = *copy_pass++;
}
return string_len;
}