1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
38 * Return the type of a URL without modifying the string
40 enum url_type
url_type(const char *url
)
48 if (q
[1] == '/' && q
[2] == '/')
58 * Decompose a URL into its components. This is done in-place;
59 * this routine does not allocate any additional storage. Freeing the
60 * original buffer frees all storage used.
62 void parse_url(struct url_info
*ui
, char *url
)
68 memset(ui
, 0, sizeof *ui
);
71 if (q
&& (q
[1] == '/' && q
[2] == '/')) {
72 ui
->type
= URL_NORMAL
;
110 ui
->port
= ui
->port
* 10 + c
;
113 } else if (q
&& q
[1] == ':') {
118 ui
->type
= URL_OLD_TFTP
;
121 ui
->type
= URL_SUFFIX
;
126 * Escapes unsafe characters in a URL.
127 * This does *not* escape things like query characters!
128 * Returns the number of characters in the total output.
130 size_t url_escape_unsafe(char *output
, const char *input
, size_t bufsize
)
132 static const char uchexchar
[] = "0123456789ABCDEF";
139 for (p
= input
; (c
= *p
); p
++) {
140 if (c
<= ' ' || c
> '~') {
141 if (++n
< bufsize
) *q
++ = '%';
142 if (++n
< bufsize
) *q
++ = uchexchar
[c
>> 4];
143 if (++n
< bufsize
) *q
++ = uchexchar
[c
& 15];
145 if (++n
< bufsize
) *q
++ = c
;
153 static int hexdigit(char c
)
155 if (c
>= '0' && c
<= '9')
158 if (c
>= 'a' && c
<= 'f')
164 * Unescapes a buffer, optionally ending at an *unescaped* terminator
165 * (like ; for TFTP). The unescaping is done in-place.
167 * If a terminator is reached, return a pointer to the first character
168 * after the terminator.
170 char *url_unescape(char *buffer
, char terminator
)
178 if (c
== terminator
) {
202 int main(int argc
, char *argv
[])
207 for (i
= 1; i
< argc
; i
++) {
208 parse_url(&url
, argv
[i
]);
209 printf("scheme: %s\n"
216 url
.scheme
, url
.user
, url
.passwd
, url
.host
, url
.port
,