2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 int urldecode(struct sink
*sink
, const char *ptr
, size_t n
) {
36 if((d1
= unhexdigit(ptr
[0])) == -1
37 || (d2
= unhexdigit(ptr
[1])) == -1)
49 if(sink_writec(sink
,c
) < 0)
55 static char *decode(const char *ptr
, size_t n
) {
61 if(urldecode(s
, ptr
, n
))
67 struct kvp
*kvp_urldecode(const char *ptr
, size_t n
) {
68 struct kvp
*kvp
, **kk
= &kvp
, *k
;
69 const char *q
, *r
, *top
= ptr
+ n
, *next
;
72 *kk
= k
= xmalloc(sizeof *k
);
73 if(!(q
= memchr(ptr
, '=', top
- ptr
)))
75 if(!(k
->name
= decode(ptr
, q
- ptr
))) break;
76 if((r
= memchr(ptr
, '&', top
- ptr
)))
82 if(!(k
->value
= decode(q
+ 1, r
- (q
+ 1)))) break;
90 int urlencode(struct sink
*sink
, const char *s
, size_t n
) {
98 if((c
>= '0' && c
<= '9')
99 || (c
>= 'a' && c
<= 'z')
100 || (c
>= 'A' && c
<= 'Z')) {
101 /* RFC2396 2.3 unreserved characters */
111 /* additional unreserved characters */
113 if(sink_writec(sink
, c
) < 0)
116 if(sink_printf(sink
, "%%%02x", (unsigned int)c
) < 0)
123 /** @brief URL-encode @p s
124 * @param s String to encode
125 * @return Encoded string
127 char *urlencodestring(const char *s
) {
131 urlencode(sink_dynstr(&d
), s
, strlen(s
));
132 dynstr_terminate(&d
);
136 /** @brief URL-decode @p s
137 * @param s String to decode
138 * @param ns Length of string
139 * @return Decoded string or NULL
141 char *urldecodestring(const char *s
, size_t ns
) {
145 if(urldecode(sink_dynstr(&d
), s
, ns
))
147 dynstr_terminate(&d
);
151 char *kvp_urlencode(const struct kvp
*kvp
, size_t *np
) {
156 sink
= sink_dynstr(&d
);
158 urlencode(sink
, kvp
->name
, strlen(kvp
->name
));
159 dynstr_append(&d
, '=');
160 urlencode(sink
, kvp
->value
, strlen(kvp
->value
));
161 if((kvp
= kvp
->next
))
162 dynstr_append(&d
, '&');
165 dynstr_terminate(&d
);
171 int kvp_set(struct kvp
**kvpp
, const char *name
, const char *value
) {
174 for(kk
= kvpp
; (k
= *kk
) && strcmp(name
, k
->name
); kk
= &k
->next
)
178 if(strcmp(k
->value
, value
)) {
179 k
->value
= xstrdup(value
);
189 *kk
= k
= xmalloc(sizeof *k
);
190 k
->name
= xstrdup(name
);
191 k
->value
= xstrdup(value
);
198 const char *kvp_get(const struct kvp
*kvp
, const char *name
) {
199 for(;kvp
&& strcmp(kvp
->name
, name
); kvp
= kvp
->next
)
201 return kvp ? kvp
->value
: 0;