/** -*- mode: c; tabs: 4 -*- ************************************************* * Module name : url.c * Module type : CMDFILE source file * Environment(s): n/a * * Description: URL encodingfunctions Returns: convert to/from URL to native paths. * * Version Who Date Description APY 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: $ *...........................................................................*/ /* RFC1798 Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL." "Reserved characters" Characters Character Character (Hex) (Dec) Dollar ("$") 24 36 Ampersand ("&") 26 38 Plus ("+") 2B 43 Comma (",") 2C 44 Forward slash/Virgule ("/") 2F 47 Colon (":") 3A 58 Semi-colon (";") 3B 59 Equals ("=") 3D 61 Question mark ("?") 3F 63 'At' symbol ("@") 40 64 "Unsafe characters" Characters Character Character (Hex) (Dec) Space 20 32 Quotation marks 22 34 'Less Than' symbol ("<") 3C 60 'Greater Than' symbol (">") 3D 62 Misc. characters: Left Curly Brace ("{") 7B 123 Right Curly Brace ("}") 7D 125 Vertical Bar/Pipe ("|") 7C 124 Backslash ("\") 5C 92 Caret ("^") 5E 94 Tilde ("~") 7E 126 Left Square Bracket ("[") 5B 91 Right Square Bracket ("]") 5D 93 Grave Accent ("`") 60 96 */ #include #include #include "cmdfile.h" static char * url2path( const char *path, char *buf ); static char * path2url( const char *path, char *buf ); int do_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); } int do_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); }