NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers urlencode.cpp Source File

urlencode.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "url.h"
00025 #include <stdlib.h>
00026 #include <ctype.h>
00027 
00028 //From http://www.geekhideout.com/urlcode.shtml
00029 
00030 char from_hex(char ch);
00031 char to_hex(char code);
00032 
00033 /* Converts a hex character to its integer value */
00034 char from_hex(char ch) {
00035   return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
00036 }
00037 
00038 /* Converts an integer value to its hex character*/
00039 char to_hex(char code) {
00040   static char hex[] = "0123456789abcdef";
00041   return hex[code & 15];
00042 }
00043 
00044 /* Returns a url-encoded version of str */
00045 /* IMPORTANT: be sure to free() the returned string after use */
00046 char *url_encode(char *str) {
00047   char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
00048   while (*pstr) {
00049     if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') 
00050       *pbuf++ = *pstr;
00051     else if (*pstr == ' ') 
00052       *pbuf++ = '+';
00053     else 
00054       *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
00055     pstr++;
00056   }
00057   *pbuf = '\0';
00058   return buf;
00059 }
00060 
00061 /* Returns a url-decoded version of str */
00062 /* IMPORTANT: be sure to free() the returned string after use */
00063 char *url_decode(char *str) {
00064   char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf;
00065   while (*pstr) {
00066     if (*pstr == '%') {
00067       if (pstr[1] && pstr[2]) {
00068         *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
00069         pstr += 2;
00070       }
00071     } else if (*pstr == '+') { 
00072       *pbuf++ = ' ';
00073     } else {
00074       *pbuf++ = *pstr;
00075     }
00076     pstr++;
00077   }
00078   *pbuf = '\0';
00079   return buf;
00080 }