-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlcode.c
More file actions
93 lines (84 loc) · 2.33 KB
/
Copy pathurlcode.c
File metadata and controls
93 lines (84 loc) · 2.33 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/** urlcode.c ---
*
* Copyright (C) 2010 PhoeniX11 Kayo
*
* Author: PhoeniX11 Kayo <phoenix11@users.sf.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "urlcode.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* Converts a hex character to its integer value */
static char from_hex(char ch) {
return isdigit(ch) ? ch-'0' : tolower(ch)-'a'+10;
}
/* Converts an integer value to its hex character*/
static char to_hex(char code) {
static char hex[]="0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(const char *str) {
const char *pstr = str;
char *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(const char *str)
{
const char *pstr = str;
char *buf = malloc(strlen(str) + 1), *pbuf = buf;
while (*pstr)
{
switch (*pstr)
{
case '%':
if (pstr[1] && pstr[2])
{
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
}
break;
case '+':
*pbuf++ = ' ';
break;
default:
*pbuf++ = *pstr;
break;
}
pstr++;
}
*pbuf = '\0';
return buf;
}
void url_free(char *str)
{
free(str);
}