blob: 8d957899c2f2c1f319ca808725d834ce09c6142e (
plain)
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
|
#include "urlenc.h"
#include "tools.h"
#include <ctype.h>
#include <string.h>
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Local Shortcuts
*/
/* Call only if 0 <= num && num <= 15. */
static char to_hex(int num)
{
static const char *const hex = "0123456789ABCDEF";
return hex[num];
}
static int is_url(char c)
{
return isalnum(c) || strchr("$-_.+ !*'(),", c);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* URL Encoding
*/
int magi_urlenc_size(const char *plain)
{
int size;
if (!plain) {
return 0;
}
for (size = 0; *plain; ++plain, ++size) {
if (!is_url(*plain)) {
size += 2;
}
}
return size;
}
void magi_urlenc(const char *plain, char *code)
{
if (!plain || !code) {
return;
}
while (*plain) {
if (is_url(*plain)) {
*(code++) = *plain;
} else {
*(code++) = '%';
*(code++) = to_hex(*plain & 0x0F);
*(code++) = to_hex(*plain >> 4);
}
++plain;
}
}
|