-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathutils.c
More file actions
63 lines (61 loc) · 1.3 KB
/
utils.c
File metadata and controls
63 lines (61 loc) · 1.3 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
#include <string.h>
#include <stdio.h>
#include "utils.h"
// Replace / with '+#sl+'. This is needed to sneak / characters past Tomcat.
// It is based on the assumption that the string will be enclosed in
// single quotes.
int escape_forward_slash(char* dst, size_t dstlen, const char* src) {
for (;; src++) {
const char c = *src;
if (c == '\0') {
if (dstlen < 1) {
return -1;
}
*dst = '\0';
return 0;
} else if (c == '/') {
if (dstlen < 7) {
return -1;
}
memcpy(dst, "'+#sl+'", 7);
dst += 7;
dstlen -= 7;
} else {
if (dstlen < 1) {
return -1;
}
*dst = c;
dst++;
dstlen--;
}
}
}
int urlencode(char* dst, size_t dstlen, const char* src) {
for (;; src++) {
const char c = *src;
if (c == '\0') {
if (dstlen < 1) {
return -1;
}
*dst = '\0';
return 0;
} else if (('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~') {
if (dstlen < 1) {
return -1;
}
*dst = c;
dst++;
dstlen--;
} else {
if (dstlen < 3) {
return -1;
}
sprintf(dst, "%%%.2x", c);
dst += 3;
dstlen -= 3;
}
}
}