Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
313 dpurdie 1
/** -*- mode: c; tabs: 4 -*- *************************************************
2
* Module name   : url.c
3
* Module type   : CMDFILE source file
4
* Environment(s): n/a
5
*
6
* Description:
7
     URL encodingfunctions
8
 
9
Returns:
10
     convert to/from URL to native paths.
11
*
12
* Version   Who      Date        Description
13
            APY      26/04/04    Created
14
*
15
* $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/url.c,v $
16
* $Revision: 1.2 $ $Date: 2004/10/08 03:07:37 $ $State: Exp $
17
* $Author: ayoung $ $Locker:  $
18
*...........................................................................*/
19
 
20
/*
21
     RFC1798
22
 
23
          Only alphanumerics [0-9a-zA-Z], the special characters 
24
          "$-_.+!*'()," [not including the quotes - ed], and reserved 
25
          characters used for their reserved purposes may be used 
26
          unencoded within a URL."
27
 
28
 
29
    "Reserved characters" 
30
 
31
        Characters                          Character   Character
32
                                            (Hex)       (Dec)
33
 
34
        Dollar ("$")                        24          36
35
        Ampersand ("&")                     26          38
36
        Plus ("+")                          2B          43
37
        Comma (",")                         2C          44
38
        Forward slash/Virgule ("/")         2F          47
39
        Colon (":")                         3A          58
40
        Semi-colon (";")                    3B          59
41
        Equals ("=")                        3D          61
42
        Question mark ("?")                 3F          63
43
        'At' symbol ("@")                   40          64
44
 
45
    "Unsafe characters" 
46
 
47
        Characters                          Character   Character
48
                                            (Hex)       (Dec)
49
 
50
        Space                               20          32     
51
 
52
        Quotation marks                     22          34
53
        'Less Than' symbol ("<")            3C          60
54
        'Greater Than' symbol (">")         3D          62
55
 
56
        Misc. characters:
57
            Left Curly Brace ("{")          7B          123
58
            Right Curly Brace ("}")         7D          125
59
            Vertical Bar/Pipe ("|")         7C          124
60
            Backslash ("\")                 5C          92
61
            Caret ("^")                     5E          94
62
            Tilde ("~")                     7E          126
63
            Left Square Bracket ("[")       5B          91
64
            Right Square Bracket ("]")      5D          93
65
            Grave Accent ("`")              60          96
66
*/
67
#include <ctype.h>
68
#include <unistd.h>
69
#include "cmdfile.h"
70
 
71
static char *           url2path( const char *path, char *buf );
72
static char *           path2url( const char *path, char *buf );
73
 
74
 
75
int
76
do_url2path(String_t *str, const char *s)
77
{
78
    char    path[ PATH_MAX ], buf[ PATH_MAX ];
79
    int     len;
80
 
81
    if (expected(s++, ',') > 0 &&
82
            (len = pathargument(s, path, PATH_MAX)) > 0)
83
    {
84
        const char *p;
85
 
86
        p = url2path( path, buf );
87
        verbose( " url2path(%s) = %s\n", path, p );
88
        StringCat(str, p);
89
        return (1 + len);
90
    }
91
    return (-1);
92
}
93
 
94
 
95
int
96
do_path2url(String_t *str, const char *s)
97
{
98
    char    path[ PATH_MAX ], buf[ PATH_MAX ];
99
    int     len;
100
 
101
    if (expected(s++, ',') > 0 &&
102
            (len = pathargument(s, path, PATH_MAX)) > 0)
103
    {
104
        const char *p;
105
 
106
        p = path2url( path, buf );
107
        verbose( " path2url(%s) = %s", path, p );
108
        StringCat(str, p);
109
        return (1 + len);
110
    }
111
    return (-1);
112
}
113
 
114
 
115
static char *
116
url2path( const char *path, char *buf )
117
{
118
    char    *obuf = buf;
119
    char    hex[5];
120
 
121
    while (*path)
122
    {
123
        switch (*path)
124
        {
125
        case '+':               /* Convert all + chars to space chars */
126
            *buf++ = ' ';
127
            path++;
128
            break;
129
 
130
        case '%':               /* Convert all %xy hex codes into ASCII chars */
131
            path++;
132
            hex[0] = *path++;
133
            hex[1] = *path++;
134
            hex[2] = '\0';
135
            *buf++ = (char)strtol(hex, NULL, 16);
136
            break;
137
 
138
        default:                /* Make an exact copy of anything else */
139
            *buf++ = *path++;
140
            break;
141
        }
142
    }
143
    *buf++ = '\0';                              /* terminate */
144
    return (obuf);
145
}
146
 
147
 
148
static char *
149
path2url( const char *path, char *buf )
150
{
151
    char    *obuf = buf;
152
    char    ch;
153
 
154
    while (*path)
155
    {
156
        ch = *path++;                           /* current character */
157
 
158
        if ( ch <= 31 || ch >= 127 ||
159
                strchr( "$&+,;=?@ \"<>{}|^~[]`", ch ) )
160
        {                                       /* convert */
161
            /* URL special characters, excluding
162
             *
163
             *      ":", "/" and "\"
164
             */
165
            *buf++ = '%';
166
            (void) sprintf(buf, "%-2.2X", ch);
167
            buf += 2;
168
        }
169
        else
170
        {                                       /* quote */
171
            *buf++ = ch;
172
        }
173
    }
174
    *buf++ = '\0';                              /* terminate */
175
    return (obuf);
176
}
177
 
178