forked from duosecurity/libduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlenc.c
126 lines (110 loc) · 3.38 KB
/
urlenc.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
urlenc.c
Copyright (c) 2010 Duo Security
Copyright (c) 1996 - 2010, Daniel Stenberg, <[email protected]>.
All rights reserved.
Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Adapted from libcurl to honor RFC 3986 unreserved characters, add
'+' space encoding, and check memory allocation failures
*/
char *
urlenc_encode(const char *string)
{
size_t alloc, newlen;
char *ns = NULL, *testing_ptr = NULL;
unsigned char in;
size_t strindex=0;
size_t length;
if (!string) return strdup("");
alloc = strlen(string) + 1;
newlen = alloc;
if ((ns = malloc(alloc)) == NULL)
return (NULL);
length = alloc-1;
while (length--) {
in = *string;
switch(in){
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
case '_': case '~': case '.': case '-':
ns[strindex++] = in;
break;
default:
newlen += 2; /* this'll become a %XX */
if (newlen > alloc) {
alloc *= 2;
if ((testing_ptr = realloc(ns, alloc)) == NULL) {
free(ns);
return (NULL);
}
ns = testing_ptr;
}
snprintf(&ns[strindex], 4, "%%%02X", in);
strindex += 3;
break;
}
string++;
}
ns[strindex] = 0;
return (ns);
}
char *
urlenc_decode(const char *string, size_t *olen)
{
size_t alloc, strindex=0;
char *ns = NULL;
unsigned char in;
long hex;
if (!string) return NULL;
alloc = strlen(string) + 1;
if ((ns = malloc(alloc)) == NULL)
return (NULL);
while(--alloc > 0) {
in = *string;
if (('%' == in) && isxdigit(string[1]) && isxdigit(string[2])) {
char hexstr[3]; /* '%XX' */
hexstr[0] = string[1];
hexstr[1] = string[2];
hexstr[2] = 0;
hex = strtol(hexstr, NULL, 16);
in = (unsigned char)hex; /* hex is always < 256 */
string += 2;
alloc -= 2;
} else if ('+' == in) {
in = ' ';
}
ns[strindex++] = in;
string++;
}
ns[strindex] = 0;
if (olen) *olen = strindex;
return (ns);
}