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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: url.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2006-2007 University of Washington
* Copyright 2013-2015 Eduardo Chappa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
#include "../pith/headers.h"
#include "../pith/url.h"
#include "../pith/mailview.h"
#include "../pith/string.h"
/*
* Internal prototypes
*/
char *rfc1738_scheme_part(char *);
int rfc1738uchar(char *);
int rfc1738xchar(char *);
/*
* * * * * * * * * RFC 1738 support routines * * * * * * * *
*/
/*
* Various helpful definitions
*/
#define RFC1738_SAFE "$-_.+" /* "safe" */
#define RFC1738_EXTRA "!*'()," /* "extra" */
#define RFC1738_RSVP ";/?:@&=" /* "reserved" */
#define RFC1738_NEWS "-.+_" /* valid for "news:" URL */
#define RFC1738_FUDGE "#{}|\\^~[]" /* Unsafe, but popular */
#define RFC1738_ESC(S) (*(S) == '%' && isxpair((S) + 1))
/*
* rfc1738_scan -- Scan the given line for possible URLs as defined
* in RFC1738
*/
char *
rfc1738_scan(char *line, int *len)
{
char *colon, *start, *end;
int n;
/* process each : in the line */
for(; (colon = strindex(line, ':')) != NULL; line = end){
end = colon + 1;
if(colon == line) /* zero length scheme? */
continue;
/*
* Valid URL (ala RFC1738 BNF)? First, first look to the
* left to make sure there are valid "scheme" chars...
*/
start = colon - 1;
while(1)
if(!(isdigit((unsigned char) *start)
|| isalpha((unsigned char) *start)
|| strchr("+-.", *start))){
start++; /* advance over bogus char */
break;
}
else if(start > line)
start--;
else
break;
/*
* Make sure everyhing up to the colon is a known scheme...
*/
if(start && (n = colon - start) && !isdigit((unsigned char) *start)
&& (((n == 2
&& (*start == 'w' || *start == 'W')
&& (*(start+1) == 's' || *(start+1) == 'S'))
|| (n == 3
&& (((*start == 'F' || *start == 'f')
&& !struncmp(start+1, "tp", 2))
||
((*start == 'w' || *start == 'W')
&& !struncmp(start+1, "ss", 2))))
|| (n == 4
&& (((*start == 'H' || *start == 'h')
&& !struncmp(start + 1, "ttp", 3))
|| ((*start == 'N' || *start == 'n')
&& !struncmp(start + 1, "ews", 3))
|| ((*start == 'N' || *start == 'n')
&& !struncmp(start + 1, "ntp", 3))
|| ((*start == 'W' || *start == 'w')
&& !struncmp(start + 1, "ais", 3))
#ifdef ENABLE_LDAP
|| ((*start == 'L' || *start == 'l')
&& !struncmp(start + 1, "dap", 3))
#endif
|| ((*start == 'I' || *start == 'i')
&& !struncmp(start + 1, "map", 3))
|| ((*start == 'F' || *start == 'f')
&& !struncmp(start + 1, "ile", 3))))
|| (n == 5
&& (*start == 'H' || *start == 'h')
&& !struncmp(start+1, "ttps", 4))
|| (n == 6
&& (((*start == 'G' || *start == 'g')
&& !struncmp(start+1, "opher", 5))
|| ((*start == 'M' || *start == 'm')
&& !struncmp(start + 1, "ailto", 5))
|| ((*start == 'T' || *start == 't')
&& !struncmp(start + 1, "elnet", 5))))
|| (n == 8
&& (*start == 'P' || *start == 'p')
&& !struncmp(start + 1, "rospero", 7))
|| (n == 11
&& (*start == 'x' || *start == 'X')
&& !struncmp(start + 1, "-pine-help", 10))
|| (n == 13
&& (*start == 'x' || *start == 'X')
&& !struncmp(start + 1, "-alpine-help", 12)))
|| url_external_specific_handler(start, n))){
/*
* Second, make sure that everything to the right of the
* colon is valid for a "schemepart"...
*/
if((end = rfc1738_scheme_part(colon + 1)) - colon > 1){
int i, j;
/* make sure something useful follows colon */
for(i = 0, j = end - colon; i < j; i++)
if(!strchr(RFC1738_RSVP, colon[i]))
break;
if(i != j){
*len = end - start;
/*
* Special case handling for comma.
* See the problem is comma's valid, but if it's the
* last character in the url, it's likely intended
* as a delimiter in the text rather part of the URL.
* In most cases any way, that's why we have the
* exception.
*/
if(*(end - 1) == ','
|| (*(end - 1) == '.' && (!*end || *end == ' ')))
(*len)--;
if(*len - (colon - start) > 0)
return(start);
}
}
}
}
return(NULL);
}
/*
* rfc1738_scheme_part - make sure what's to the right of the
* colon is valid
*
* NOTE: we have a problem matching closing parens when users
* bracket the url in parens. So, lets try terminating our
* match on any closing paren that doesn't have a coresponding
* open-paren.
*/
char *
rfc1738_scheme_part(char *s)
{
int n, paren = 0, bracket = 0;
while(1)
switch(*s){
default :
if((n = rfc1738xchar(s)) != 0){
s += n;
break;
}
case '\0' :
return(s);
case '[' :
bracket++;
s++;
break;
case ']' :
if(bracket--){
s++;
break;
}
return(s);
case '(' :
paren++;
s++;
break;
case ')' :
if(paren--){
s++;
break;
}
return(s);
}
}
/*
* rfc1738_str - convert rfc1738 escaped octets in place
*/
char *
rfc1738_str(char *s)
{
register char *p = s, *q = s;
while(1)
switch(*q = *p++){
case '%' :
if(isxpair(p)){
*q = X2C(p);
p += 2;
}
default :
q++;
break;
case '\0':
return(s);
}
}
/*
* rfc1738uchar - returns TRUE if the given char fits RFC 1738 "uchar" BNF
*/
int
rfc1738uchar(char *s)
{
int valid = (RFC1738_ESC(s)) /* "escape" */
? 2
: (isalnum((unsigned char) *s) /* alphanumeric */
|| strchr(RFC1738_SAFE, *s) /* other special stuff */
|| strchr(RFC1738_EXTRA, *s));
if(!valid){
char *t;
UCS ucs;
CBUF_S cbuf;
cbuf.cbuf[0] = '\0';
cbuf.cbufp = cbuf.cbuf;
cbuf.cbufend = cbuf.cbuf;
for(t = s; t && *t; t++){
if(utf8_to_ucs4_oneatatime((unsigned char) *t & 0xff, &cbuf, &ucs, NULL)){
if ((ucs >= 0x00A0 && ucs <= 0xD7FF)
|| (ucs >= 0xE000 && ucs <= 0xFDCF)
|| (ucs >= 0xFDF0 && ucs <= 0xFFEF)
|| (ucs >= 0x10000 && ucs <= 0x1FFFD)
|| (ucs >= 0x20000 && ucs <= 0x2FFFD)
|| (ucs >= 0x30000 && ucs <= 0x3FFFD)
|| (ucs >= 0x40000 && ucs <= 0x4FFFD)
|| (ucs >= 0x50000 && ucs <= 0x5FFFD)
|| (ucs >= 0x60000 && ucs <= 0x6FFFD)
|| (ucs >= 0x70000 && ucs <= 0x7FFFD)
|| (ucs >= 0x80000 && ucs <= 0x8FFFD)
|| (ucs >= 0x90000 && ucs <= 0x9FFFD)
|| (ucs >= 0xA0000 && ucs <= 0xAFFFD)
|| (ucs >= 0xB0000 && ucs <= 0xBFFFD)
|| (ucs >= 0xC0000 && ucs <= 0xCFFFD)
|| (ucs >= 0xD0000 && ucs <= 0xDFFFD)
|| (ucs >= 0xE0000 && ucs <= 0xEFFFD)
|| (ucs >= 0xF0000 && ucs <= 0xFFFFD)
|| (ucs >= 0x100000 && ucs <= 0x10FFFD))
valid = t-s+1;
break;
}
}
}
return valid;
}
/*
* rfc1738xchar - returns TRUE if the given char fits RFC 1738 "xchar" BNF
*/
int
rfc1738xchar(char *s)
{
int n;
return((n = rfc1738uchar(s))
? n
: (strchr(RFC1738_RSVP, *s) != NULL
|| strchr(RFC1738_FUDGE, *s)));
}
/*
* rfc1738_num - return long value of a string of digits, possibly escaped
*/
unsigned long
rfc1738_num(char **s)
{
register char *p = *s;
unsigned long n = 0L;
for(; *p; p++)
if(*p == '%' && isxpair(p+1)){
int c = X2C(p+1);
if(isdigit((unsigned char) c)){
n = (c - '0') + (n * 10);
p += 2;
}
else
break;
}
else if(isdigit((unsigned char) *p))
n = (*p - '0') + (n * 10);
else
break;
*s = p;
return(n);
}
int
rfc1738_group(char *s)
{
return(isalnum((unsigned char) *s)
|| RFC1738_ESC(s)
|| strchr(RFC1738_NEWS, *s));
}
/*
* Encode (hexify) a mailto url.
*
* Args s -- src url
*
* Returns An allocated string which is suitably encoded.
* Result should be freed by caller.
*
* Since we don't know here which characters are reserved characters (? and &)
* for use in delimiting the pieces of the url and which are just those
* characters contained in the data that should be encoded, we always encode
* them. That's because we know we don't use those as reserved characters.
* If you do use those as reserved characters you have to encode each part
* separately.
*/
char *
rfc1738_encode_mailto(char *s)
{
char *d, *ret = NULL;
if(s){
/* Worst case, encode every character */
ret = d = (char *)fs_get((3*strlen(s) + 1) * sizeof(char));
while(*s){
if(isalnum((unsigned char)*s)
|| strchr(RFC1738_SAFE, *s)
|| strchr(RFC1738_EXTRA, *s))
*d++ = *s++;
else{
*d++ = '%';
C2XPAIR(*s, d);
s++;
}
}
*d = '\0';
}
return(ret);
}
/*
* * * * * * * * * RFC 1808 support routines * * * * * * * *
*/
int
rfc1808_tokens(char *url, char **scheme, char **net_loc, char **path,
char **parms, char **query, char **frag)
{
char *p, *q, *start, *tmp = cpystr(url);
start = tmp;
if((p = strchr(start, '#')) != NULL){ /* fragment spec? */
*p++ = '\0';
if(*p)
*frag = cpystr(p);
}
if((p = strchr(start, ':')) && p != start){ /* scheme part? */
for(q = start; q < p; q++)
if(!(isdigit((unsigned char) *q)
|| isalpha((unsigned char) *q)
|| strchr("+-.", *q)))
break;
if(p == q){
*p++ = '\0';
*scheme = cpystr(start);
start = p;
}
}
if(*start == '/' && *(start+1) == '/'){ /* net_loc */
if((p = strchr(start+2, '/')) != NULL)
*p++ = '\0';
*net_loc = cpystr(start+2);
if(p)
start = p;
else *start = '\0'; /* End of parse */
}
if((p = strchr(start, '?')) != NULL){
*p++ = '\0';
*query = cpystr(p);
}
if((p = strchr(start, ';')) != NULL){
*p++ = '\0';
*parms = cpystr(p);
}
if(*start)
*path = cpystr(start);
fs_give((void **) &tmp);
return(1);
}
/*
* web_host_scan -- Scan the given line for possible web host names
*
* NOTE: scan below is limited to DNS names ala RFC1034
*/
char *
web_host_scan(char *line, int *len)
{
char *end, last = '\0';
for(; *line; last = *line++)
if((*line == 'w' || *line == 'W')
&& (!last || !(isalnum((unsigned char) last)
|| last == '.' || last == '-' || last == '/'))
&& (((*(line + 1) == 'w' || *(line + 1) == 'W') /* "www." */
&& (*(line + 2) == 'w' || *(line + 2) == 'W'))
|| ((*(line + 1) == 'e' || *(line + 1) == 'E') /* "web." */
&& (*(line + 2) == 'b' || *(line + 2) == 'B')))
&& (*(line + 3) == '.')){
end = rfc1738_scheme_part(line + 3);
if((*len = end - line) > ((*(line+3) == '.') ? 4 : 3)){
/* Dread comma exception, see note in rfc1738_scan */
if(strchr(",:", *(line + (*len) - 1))
|| (*(line + (*len) - 1) == '.'
&& (!*(line + (*len)) || *(line + (*len)) == ' ')))
(*len)--;
return(line);
}
else
line += 3;
}
return(NULL);
}
/*
* mail_addr_scan -- Scan the given line for possible RFC822 addr-spec's
*
* NOTE: Well, OK, not strictly addr-specs since there's alot of junk
* we're tying to sift thru and we'd like to minimize false-pos
* matches.
*/
char *
mail_addr_scan(char *line, int *len)
{
char *amp, *start, *end;
/*
* This list is not the whole standards-based list, this is just a list
* of likely email address characters. We don't want to include everything
* because punctuation in the text might get mixed in with the address.
*/
#define NONALPHANUMOK ".-_+%/="
/* process each : in the line */
for(; (amp = strindex(line, '@')) != NULL; line = end){
end = amp + 1;
/* zero length addr? */
if(amp == line || !(isalnum((unsigned char) *(start = amp - 1))
|| strchr(NONALPHANUMOK, *start)))
continue;
/*
* Valid address (ala RFC822 BNF)? First, first look to the
* left to make sure there are valid "scheme" chars...
*/
while(1)
/* NOTE: we're not doing quoted-strings */
if(!(isalnum((unsigned char) *start) || strchr(NONALPHANUMOK, *start))){
/* advance over bogus char, and erase leading punctuation */
for(start++; *start && strchr(NONALPHANUMOK, *start); start++)
;
break;
}
else if(start > line)
start--;
else
break;
/*
* Make sure everyhing up to the colon is a known scheme...
*/
if(start && (amp - start) > 0){
/*
* Second, make sure that everything to the right of
* amp is valid for a "domain"...
*/
if(*(end = amp + 1) == '['){ /* domain literal */
int dots = 3;
for(++end; *end ; end++)
if(*end == ']'){
if(!dots){
*len = end - start + 1;
return(start);
}
else
break; /* bogus */
}
else if(*end == '.'){
if(--dots < 0)
break; /* bogus */
}
else if(!isdigit((unsigned char) *end))
break; /* bogus */
}
else if(isalnum((unsigned char) *end)){ /* domain name? */
for(++end; ; end++)
if(!(*end && (isalnum((unsigned char) *end)
|| *end == '-'
|| *end == '.'
|| *end == '_'))){
/* can't end with dash, dot or underscore */
while(!isalnum((unsigned char) *(end - 1)))
end--;
*len = end - start;
return(start);
}
}
}
}
return(NULL);
}
|