Blame | Last modification | View Log | RSS feed
/** -*- mode: c; tabs: 4 -*- ************************************************** Module name : url.c* Module type : CMDFILE source file* Environment(s): n/a** Description:URL encodingfunctionsReturns:convert to/from URL to native paths.** Version Who Date DescriptionAPY 26/04/04 Created** $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/url.c,v $* $Revision: 1.2 $ $Date: 2004/10/08 03:07:37 $ $State: Exp $* $Author: ayoung $ $Locker: $*...........................................................................*//*RFC1798Only alphanumerics [0-9a-zA-Z], the special characters"$-_.+!*'()," [not including the quotes - ed], and reservedcharacters used for their reserved purposes may be usedunencoded within a URL.""Reserved characters"Characters Character Character(Hex) (Dec)Dollar ("$") 24 36Ampersand ("&") 26 38Plus ("+") 2B 43Comma (",") 2C 44Forward slash/Virgule ("/") 2F 47Colon (":") 3A 58Semi-colon (";") 3B 59Equals ("=") 3D 61Question mark ("?") 3F 63'At' symbol ("@") 40 64"Unsafe characters"Characters Character Character(Hex) (Dec)Space 20 32Quotation marks 22 34'Less Than' symbol ("<") 3C 60'Greater Than' symbol (">") 3D 62Misc. characters:Left Curly Brace ("{") 7B 123Right Curly Brace ("}") 7D 125Vertical Bar/Pipe ("|") 7C 124Backslash ("\") 5C 92Caret ("^") 5E 94Tilde ("~") 7E 126Left Square Bracket ("[") 5B 91Right Square Bracket ("]") 5D 93Grave Accent ("`") 60 96*/#include <ctype.h>#include <unistd.h>#include "cmdfile.h"static char * url2path( const char *path, char *buf );static char * path2url( const char *path, char *buf );intdo_url2path(String_t *str, const char *s){char path[ PATH_MAX ], buf[ PATH_MAX ];int len;if (expected(s++, ',') > 0 &&(len = pathargument(s, path, PATH_MAX)) > 0){const char *p;p = url2path( path, buf );verbose( " url2path(%s) = %s\n", path, p );StringCat(str, p);return (1 + len);}return (-1);}intdo_path2url(String_t *str, const char *s){char path[ PATH_MAX ], buf[ PATH_MAX ];int len;if (expected(s++, ',') > 0 &&(len = pathargument(s, path, PATH_MAX)) > 0){const char *p;p = path2url( path, buf );verbose( " path2url(%s) = %s", path, p );StringCat(str, p);return (1 + len);}return (-1);}static char *url2path( const char *path, char *buf ){char *obuf = buf;char hex[5];while (*path){switch (*path){case '+': /* Convert all + chars to space chars */*buf++ = ' ';path++;break;case '%': /* Convert all %xy hex codes into ASCII chars */path++;hex[0] = *path++;hex[1] = *path++;hex[2] = '\0';*buf++ = (char)strtol(hex, NULL, 16);break;default: /* Make an exact copy of anything else */*buf++ = *path++;break;}}*buf++ = '\0'; /* terminate */return (obuf);}static char *path2url( const char *path, char *buf ){char *obuf = buf;char ch;while (*path){ch = *path++; /* current character */if ( ch <= 31 || ch >= 127 ||strchr( "$&+,;=?@ \"<>{}|^~[]`", ch ) ){ /* convert *//* URL special characters, excluding** ":", "/" and "\"*/*buf++ = '%';(void) sprintf(buf, "%-2.2X", ch);buf += 2;}else{ /* quote */*buf++ = ch;}}*buf++ = '\0'; /* terminate */return (obuf);}