diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-09-19 11:28:28 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-09-19 18:30:01 +0300 |
commit | 1153970626892573e5966e135e8d81185f4ea53c (patch) | |
tree | 773fdf5a8d00ec2ef752c3a8fd5f32c79829cf23 /lib/urlencode.c | |
download | eclat-1153970626892573e5966e135e8d81185f4ea53c.tar.gz eclat-1153970626892573e5966e135e8d81185f4ea53c.tar.bz2 |
Initial commit
Diffstat (limited to 'lib/urlencode.c')
-rw-r--r-- | lib/urlencode.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/urlencode.c b/lib/urlencode.c new file mode 100644 index 0000000..829d461 --- /dev/null +++ b/lib/urlencode.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* This file is part of Eclat | ||
2 | Copyright (C) 2012 Sergey Poznyakoff | ||
3 | |||
4 | Eclat is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | Eclat is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include "libeclat.h" | ||
18 | #include <grecs.h> | ||
19 | |||
20 | static int prtch[] = { | ||
21 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
22 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
23 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, | ||
24 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, | ||
25 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
26 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, | ||
27 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
28 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, | ||
29 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
30 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
31 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
32 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
33 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
34 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
35 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
36 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
37 | }; | ||
38 | |||
39 | void | ||
40 | urlencode(const char *input, size_t len, char **poutput, size_t *poutlen) | ||
41 | { | ||
42 | size_t i, outlen; | ||
43 | char *output; | ||
44 | static char xdig[] = "0123456789ABCDEF"; | ||
45 | |||
46 | outlen = len; | ||
47 | for (i = 0; i < len; i++) | ||
48 | if (!prtch[input[i]]) | ||
49 | outlen += 2; | ||
50 | |||
51 | if (poutlen) | ||
52 | *poutlen = outlen; | ||
53 | |||
54 | output = grecs_malloc(outlen + 1); | ||
55 | *poutput = output; | ||
56 | |||
57 | for (i = 0; i < len; i++) { | ||
58 | if (prtch[input[i]]) | ||
59 | *output++ = input[i]; | ||
60 | else { | ||
61 | *output++ = '%'; | ||
62 | *output++ = xdig[((unsigned char)input[i]) >> 4]; | ||
63 | *output++ = xdig[((unsigned char)input[i]) & 0x0f]; | ||
64 | } | ||
65 | } | ||
66 | *output = 0; | ||
67 | } | ||
68 | |||