Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
services/http/util/urlencode.cpp@0:ac21159e27f4, 2010-07-02 (annotated)
- Committer:
- darran
- Date:
- Fri Jul 02 17:21:58 2010 +0000
- Revision:
- 0:ac21159e27f4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darran | 0:ac21159e27f4 | 1 | |
darran | 0:ac21159e27f4 | 2 | /* |
darran | 0:ac21159e27f4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
darran | 0:ac21159e27f4 | 4 | |
darran | 0:ac21159e27f4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
darran | 0:ac21159e27f4 | 6 | of this software and associated documentation files (the "Software"), to deal |
darran | 0:ac21159e27f4 | 7 | in the Software without restriction, including without limitation the rights |
darran | 0:ac21159e27f4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
darran | 0:ac21159e27f4 | 9 | copies of the Software, and to permit persons to whom the Software is |
darran | 0:ac21159e27f4 | 10 | furnished to do so, subject to the following conditions: |
darran | 0:ac21159e27f4 | 11 | |
darran | 0:ac21159e27f4 | 12 | The above copyright notice and this permission notice shall be included in |
darran | 0:ac21159e27f4 | 13 | all copies or substantial portions of the Software. |
darran | 0:ac21159e27f4 | 14 | |
darran | 0:ac21159e27f4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
darran | 0:ac21159e27f4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
darran | 0:ac21159e27f4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
darran | 0:ac21159e27f4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
darran | 0:ac21159e27f4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
darran | 0:ac21159e27f4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
darran | 0:ac21159e27f4 | 21 | THE SOFTWARE. |
darran | 0:ac21159e27f4 | 22 | */ |
darran | 0:ac21159e27f4 | 23 | |
darran | 0:ac21159e27f4 | 24 | #include "url.h" |
darran | 0:ac21159e27f4 | 25 | #include <stdlib.h> |
darran | 0:ac21159e27f4 | 26 | #include <ctype.h> |
darran | 0:ac21159e27f4 | 27 | |
darran | 0:ac21159e27f4 | 28 | //From http://www.geekhideout.com/urlcode.shtml |
darran | 0:ac21159e27f4 | 29 | |
darran | 0:ac21159e27f4 | 30 | char from_hex(char ch); |
darran | 0:ac21159e27f4 | 31 | char to_hex(char code); |
darran | 0:ac21159e27f4 | 32 | |
darran | 0:ac21159e27f4 | 33 | /* Converts a hex character to its integer value */ |
darran | 0:ac21159e27f4 | 34 | char from_hex(char ch) { |
darran | 0:ac21159e27f4 | 35 | return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
darran | 0:ac21159e27f4 | 36 | } |
darran | 0:ac21159e27f4 | 37 | |
darran | 0:ac21159e27f4 | 38 | /* Converts an integer value to its hex character*/ |
darran | 0:ac21159e27f4 | 39 | char to_hex(char code) { |
darran | 0:ac21159e27f4 | 40 | static char hex[] = "0123456789abcdef"; |
darran | 0:ac21159e27f4 | 41 | return hex[code & 15]; |
darran | 0:ac21159e27f4 | 42 | } |
darran | 0:ac21159e27f4 | 43 | |
darran | 0:ac21159e27f4 | 44 | /* Returns a url-encoded version of str */ |
darran | 0:ac21159e27f4 | 45 | /* IMPORTANT: be sure to free() the returned string after use */ |
darran | 0:ac21159e27f4 | 46 | char *url_encode(char *str) { |
darran | 0:ac21159e27f4 | 47 | char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
darran | 0:ac21159e27f4 | 48 | while (*pstr) { |
darran | 0:ac21159e27f4 | 49 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
darran | 0:ac21159e27f4 | 50 | *pbuf++ = *pstr; |
darran | 0:ac21159e27f4 | 51 | else if (*pstr == ' ') |
darran | 0:ac21159e27f4 | 52 | *pbuf++ = '+'; |
darran | 0:ac21159e27f4 | 53 | else |
darran | 0:ac21159e27f4 | 54 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
darran | 0:ac21159e27f4 | 55 | pstr++; |
darran | 0:ac21159e27f4 | 56 | } |
darran | 0:ac21159e27f4 | 57 | *pbuf = '\0'; |
darran | 0:ac21159e27f4 | 58 | return buf; |
darran | 0:ac21159e27f4 | 59 | } |
darran | 0:ac21159e27f4 | 60 | |
darran | 0:ac21159e27f4 | 61 | /* Returns a url-decoded version of str */ |
darran | 0:ac21159e27f4 | 62 | /* IMPORTANT: be sure to free() the returned string after use */ |
darran | 0:ac21159e27f4 | 63 | char *url_decode(char *str) { |
darran | 0:ac21159e27f4 | 64 | char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
darran | 0:ac21159e27f4 | 65 | while (*pstr) { |
darran | 0:ac21159e27f4 | 66 | if (*pstr == '%') { |
darran | 0:ac21159e27f4 | 67 | if (pstr[1] && pstr[2]) { |
darran | 0:ac21159e27f4 | 68 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
darran | 0:ac21159e27f4 | 69 | pstr += 2; |
darran | 0:ac21159e27f4 | 70 | } |
darran | 0:ac21159e27f4 | 71 | } else if (*pstr == '+') { |
darran | 0:ac21159e27f4 | 72 | *pbuf++ = ' '; |
darran | 0:ac21159e27f4 | 73 | } else { |
darran | 0:ac21159e27f4 | 74 | *pbuf++ = *pstr; |
darran | 0:ac21159e27f4 | 75 | } |
darran | 0:ac21159e27f4 | 76 | pstr++; |
darran | 0:ac21159e27f4 | 77 | } |
darran | 0:ac21159e27f4 | 78 | *pbuf = '\0'; |
darran | 0:ac21159e27f4 | 79 | return buf; |
darran | 0:ac21159e27f4 | 80 | } |