File: url.c

package info (click to toggle)
cftp 0.12-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 968 kB
  • ctags: 853
  • sloc: ansic: 9,541; sh: 327; makefile: 92
file content (221 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
  $NiH: url.c,v 1.6 2002/09/16 12:42:45 dillo Exp $

  url.c -- functions to parse and create URLs
  Copyright (C) 1996-2002 Dieter Baron

  This file is part of cftp, a fullscreen ftp client
  The author can be contacted at <dillo@giga.or.at>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/



#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "url.h"

#define R URL_UCHAR
#define B (URL_XCHAR|URL_UCHAR)

static char _url_spec[256] = {
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0x00 - 0x0f */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0x10 - 0x1f */
    B, 0, B, B, 0, B, R, 0,  0, 0, 0, 0, 0, 0, 0, R,  /* 0x20 - 0x2f */
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, R, R, B, R, B, R,  /* 0x30 - 0x3f */
    R, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,  /* 0x40 - 0x4f */
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, B, B, B, B, 0,  /* 0x50 - 0x5f */
    B, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,  /* 0x60 - 0x6f */
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, B, B, B, B, B,  /* 0x70 - 0x7f */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0x80 - 0x8f */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0x90 - 0x9f */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xa0 - 0xaf */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xb0 - 0xbf */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xc0 - 0xcf */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xd0 - 0xdf */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xe0 - 0xef */
    B, B, B, B, B, B, B, B,  B, B, B, B, B, B, B, B,  /* 0xf0 - 0xff */
};

#undef R
#undef B



int
url_special(int c, int which)
{
    if (c < 0 || c >= 256)
	return 1;
    else
	return _url_spec[c] & which;
}



int
url_enclen(char *s, int which)
{
    int len;

    len=0;
    while (*s)
	len += url_special(*(s++), which) ? 3 : 1;

    return len;
}



char *
url_encode(char *d, const char *s, int which)
{
    static char hex[] = "0123456789ABCDEF";

    char *p;

    for (p=d; *s; s++) {
	if (url_special(*s, which)) {
	    p[0] = '%';
	    p[1] = hex[((*s)>>4)&0xf];
	    p[2] = hex[(*s)&0xf];
	    p += 3;
	}
	else
	    *(p++) = *s;
    }
    *p = '\0';

    return d;
}



int
url_declen(char *s)
{
    int len;

    for (len=0; *s; len++) {
	if (s[0] == '%' && s[1] && isxdigit(s[1]) && s[2] && isxdigit(s[2]))
	    s += 3;
	else
	    s++;
    }

    return len;
}



char *
url_decode(char *d, const char *s)
{
    unsigned char *p, x[3];

    x[2] = 0;
    p = (unsigned char *)d;
    while (*s) {
	if (s[0] == '%' && s[1] && isxdigit(s[1]) && s[2] && isxdigit(s[2])) {
	    x[0] = s[1];
	    x[1] = s[2];
	    *p = strtoul((char *)x, NULL, 16);
	    s += 3;
	}
	else
	    *(p++) = *(s++);
    }
    *p = '\0';

    return d;
}



int
parse_url(char *url, int *proto, char **user, char **pass,
	  char **host, char **port, char **dir)
{
    char *p, *q, *h, *r;

    if (strncmp(url, "ftp://", 6) == 0) {
	url += 6;
	*proto = 0;
    }
    else if (strncmp(url, "sftp://", 7) == 0) {
	url += 7;
	*proto = 1;
    }
    else
	return -1;

    if ((p=strchr(url, '/')) != NULL)
	*(p++) = '\0';
    else
	p = url+strlen(url);
    
    if ((q=strrchr(url, '@')) != NULL) {
	*q = '\0';
	if ((r=strchr(url, ':')) != NULL) {
	    *(r++) = '\0';
	    *pass = malloc(url_declen(r)+1);
	    url_decode(*pass, r);
	}
	*user = malloc(url_declen(url)+1);
	url_decode(*user, url);
	url = q+1;
    }

    if (url[0] == '[' && (h=strchr(url, ']')) != NULL
	&& (h[1] == ':' || h[1] == '\0')) {
	url++;
	*(h++) = '\0';
    }
    else
	h = url;

    if ((q=strchr(h, ':')) != NULL) {
	*q = '\0';
	if (*(q+1) != '\0') {
	    *port = malloc(url_declen(q+1)+1);
	    url_decode(*port, q+1);
	}
    }

    *host = malloc(url_declen(url)+1);
    url_decode(*host, url);

    if (p && *p != '\0') {
	*dir = malloc(url_declen(p)+1);
	url_decode(*dir, p);
    }

    return 0;
}



int
is_url(char *str)
{
    return (strncmp(str, "ftp://", 6) == 0
#ifdef USE_SFTP
	    || strncmp(str, "sftp://", 7) == 0
#endif
	);
}