about summary refs log tree commit diff stats
path: root/WWW/Library/Implementation/HTParse.c
blob: 7362a59afd04f0c08dec7b805b8ccf081de1aa72 (plain) (blame)
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*		Parse HyperText Document Address		HTParse.c
**		================================
*/

#include <HTUtils.h>
#include <HTParse.h>

#include <LYLeaks.h>

#define HEX_ESCAPE '%'

struct struct_parts {
	char * access;
	char * host;
	char * absolute;
	char * relative;
/*	char * search;		no - treated as part of path */
	char * anchor;
};


/*	Strip white space off a string. 			HTStrip()
**	-------------------------------
**
** On exit,
**	Return value points to first non-white character, or to 0 if none.
**	All trailing white space is OVERWRITTEN with zero.
*/
PUBLIC char * HTStrip ARGS1(
	char *, 	s)
{
#define SPACE(c) ((c == ' ') || (c == '\t') || (c == '\n'))
    char * p = s;
    for (p = s; *p; p++)
	;			/* Find end of string */
    for (p--; p >= s; p--) {
	if (SPACE(*p))
	    *p = '\0';		/* Zap trailing blanks */
	else
	    break;
    }
    while (SPACE(*s))
	s++;			/* Strip leading blanks */
    return s;
}

/*	Scan a filename for its consituents.			scan()
**	------------------------------------
**
** On entry,
**	name	points to a document name which may be incomplete.
** On exit,
**	absolute or relative may be nonzero (but not both).
**	host, anchor and access may be nonzero if they were specified.
**	Any which are nonzero point to zero terminated strings.
*/
PRIVATE void scan ARGS2(
	char *, 		name,
	struct struct_parts *,	parts)
{
    char * after_access;
    char * p;
#ifdef NOTDEFINED
    int length = strlen(name);
#endif /* NOTDEFINED */

    parts->access = NULL;
    parts->host = NULL;
    parts->absolute = NULL;
    parts->relative = NULL;
    parts->anchor = NULL;

    /*
    **	Scan left-to-right for a scheme (access).
    */
    after_access = name;
    for (p = name; *p; p++) {
	if (*p==':') {
	    *p = '\0';
	    parts->access = name;	/* Access name has been specified */
	    after_access = (p + 1);
	    break;
	}
	if (*p == '/' || *p == '#' || *p == ';' || *p == '?')
	    break;
    }

#ifdef NOTDEFINED
    for (p = (name + length-1); p >= name; p--) {}
#endif /* NOTDEFINED */
    /*
    **	Scan left-to-right for a fragment (anchor).
    */
    for (p = after_access; *p; p++) {
	if (*p =='#') {
	    parts->anchor = (p + 1);
	    *p = '\0';			/* terminate the rest */
	    break;		/* leave things after first # alone - kw */
	}
    }

    /*
    **	Scan left-to-right for a host or absolute path.
    */
    p = after_access;
    if (*p == '/') {
	if (p[1] == '/') {
	    parts->host = (p + 2);	  /* host has been specified	*/
	    *p = '\0';			  /* Terminate access		*/
	    p = strchr(parts->host, '/'); /* look for end of host name if any */
	    if (p != NULL) {
		*p = '\0';			/* Terminate host */
		parts->absolute = (p + 1);	/* Root has been found */
	    }
	} else {
	    parts->absolute = (p + 1);		/* Root found but no host */
	}
    } else {
	parts->relative = (*after_access) ?
			     after_access : NULL; /* NULL for "" */
    }

    /*
    **	Check schemes that commonly have unescaped hashes.
    */
    if (parts->access && parts->anchor) {
	if ((!parts->host && strcasecomp(parts->access, "lynxcgi")) ||
	    !strcasecomp(parts->access, "nntp") ||
	    !strcasecomp(parts->access, "snews") ||
	    !strcasecomp(parts->access, "news") ||
	    !strcasecomp(parts->access, "data")) {
	    /*
	     *	Access specified but no host and not a lynxcgi URL, so the
	     *	anchor may not really be one, e.g., news:j462#36487@foo.bar,
	     *	or it's an nntp or snews URL, or news URL with a host.
	     *	Restore the '#' in the address.
	     */
	    /* but only if we have found a path component of which this will
	     * become part. - kw  */
	    if (parts->relative || parts->absolute) {
		*(parts->anchor - 1) = '#';
		parts->anchor = NULL;
	    }
	}
    }

#ifdef NOT_DEFINED	/* search is just treated as part of path */
    {
	char *p = (relative ? relative : absolute);
	if (p != NULL) {
	    char *q = strchr(p, '?');	/* Any search string? */
	    if (q != NULL) {
		*q = '\0';		/* If so, chop that off. */
		parts->search = (q + 1);
	    }
	}
    }
#endif /* NOT_DEFINED */
} /*scan */


/*	Parse a Name relative to another name.			HTParse()
**	--------------------------------------
**
**	This returns those parts of a name which are given (and requested)
**	substituting bits from the related name where necessary.
**
** On entry,
**	aName		A filename given
**	relatedName	A name relative to which aName is to be parsed
**	wanted		A mask for the bits which are wanted.
**
** On exit,
**	returns 	A pointer to a calloc'd string which MUST BE FREED
*/
PUBLIC char * HTParse ARGS3(
	CONST char *,	aName,
	CONST char *,	relatedName,
	int,		wanted)
{
    char * result = NULL;
    char * return_value = NULL;
    int len;
    char * name = NULL;
    char * rel = NULL;
    char * p;
    char * acc_method;
    struct struct_parts given, related;

    CTRACE(tfp, "HTParse: aName:%s   relatedName:%s\n", aName, relatedName);

    /*
    **	Allocate the output string.
    */
    len = strlen(aName) + strlen(relatedName) + 10;
    result = (char *)calloc(1, len);	/* Lots of space: more than enough */
    if (result == NULL) {
	outofmem(__FILE__, "HTParse");
    }

    /*
    **	Make working copies of the input strings to cut up.
    */
    StrAllocCopy(name, aName);
    StrAllocCopy(rel, relatedName);

    /*
    **	Cut up the strings into URL fields.
    */
    scan(name, &given);
    scan(rel,  &related);

    /*
    **	Handle the scheme (access) field.
    */
    if (given.access && given.host && !given.relative && !given.absolute) {
	if (!strcmp(given.access, "http") ||
	    !strcmp(given.access, "https") ||
	    !strcmp(given.access, "ftp"))
	    /*
	    **	Assume root.
	    */
	    given.absolute = "";
    }
    acc_method = given.access ? given.access : related.access;
    if (wanted & PARSE_ACCESS) {
	if (acc_method) {
	    strcat(result, acc_method);
	    if (wanted & PARSE_PUNCTUATION)
		strcat(result, ":");
	}
    }

    /*
    **	If different schemes, inherit nothing.
    **
    **	We'll try complying with RFC 1808 and
    **	the Fielding draft, and inherit nothing
    **	if both schemes are given, rather than
    **	only when they differ, except for
    **	file URLs - FM
    **
    **	After trying it for a while, it's still
    **	premature, IHMO, to go along with it, so
    **	this is back to inheriting for identical
    **	schemes whether or not they are "file".
    **	If you want to try it again yourself,
    **	uncomment the strcasecomp() below. - FM
    */
    if ((given.access && related.access) &&
	(/* strcasecomp(given.access, "file") || */
	 strcmp(given.access, related.access))) {
	related.host = NULL;
	related.absolute = NULL;
	related.relative = NULL;
	related.anchor = NULL;
    }

    /*
    **	Handle the host field.
    */
    if (wanted & PARSE_HOST)
	if (given.host || related.host) {
	    char *tail = result + strlen(result);
	    if (wanted & PARSE_PUNCTUATION)
		strcat(result, "//");
	    strcat(result, given.host ? given.host : related.host);
#define CLEAN_URLS
#ifdef CLEAN_URLS
	    /*
	    **	Ignore default port numbers, and trailing dots on FQDNs,
	    **	which will only cause identical addresses to look different.
	    */
	    {
		char *p2, *h;
		if ((p2 = strchr(result, '@')) != NULL)
		   tail = (p2 + 1);
		p2 = strchr(tail, ':');
		if (p2 != NULL && !isdigit((unsigned char)p2[1]))
		    /*
		    **	Colon not followed by a port number.
		    */
		    *p2 = '\0';
		if (p2 != NULL && *p2 != '\0' && acc_method != NULL) {
		    /*
		    **	Port specified.
		    */
		    if ((!strcmp(acc_method, "http"	 ) && !strcmp(p2, ":80" )) ||
			(!strcmp(acc_method, "https"	 ) && !strcmp(p2, ":443")) ||
			(!strcmp(acc_method, "gopher"	 ) && !strcmp(p2, ":70" )) ||
			(!strcmp(acc_method, "ftp"	 ) && !strcmp(p2, ":21" )) ||
			(!strcmp(acc_method, "wais"	 ) && !strcmp(p2, ":210")) ||
			(!strcmp(acc_method, "nntp"	 ) && !strcmp(p2, ":119")) ||
			(!strcmp(acc_method, "news"	 ) && !strcmp(p2, ":119")) ||
			(!strcmp(acc_method, "newspost"  ) && !strcmp(p2, ":119")) ||
			(!strcmp(acc_method, "newsreply" ) && !strcmp(p2, ":119")) ||
			(!strcmp(acc_method, "snews"	 ) && !strcmp(p2, ":563")) ||
			(!strcmp(acc_method, "snewspost" ) && !strcmp(p2, ":563")) ||
			(!strcmp(acc_method, "snewsreply") && !strcmp(p2, ":563")) ||
			(!strcmp(acc_method, "finger"	 ) && !strcmp(p2, ":79" )) ||
			(!strcmp(acc_method, "telnet"	 ) && !strcmp(p2, ":23" )) ||
			(!strcmp(acc_method, "tn3270"	 ) && !strcmp(p2, ":23" )) ||
			(!strcmp(acc_method, "rlogin"	 ) && !strcmp(p2, ":513")) ||
			(!strcmp(acc_method, "cso"	 ) && !strcmp(p2, ":105")))
		    *p2 = '\0'; /* It is the default: ignore it */
		}
		if (p2 == NULL) {
		    int len2 = strlen(tail);

		    if (len2 > 0) {
			h = tail + len2 - 1;	/* last char of hostname */
			if (*h == '.')
			    *h = '\0';		/* chop final . */
		    }
		} else {
		    h = p2;
		    h--;		/* End of hostname */
		    if (*h == '.') {
			/*
			**  Slide p2 over h.
			*/
			while (*p2 != '\0')
			    *h++ = *p2++;
			*h = '\0';	/* terminate */
		    }
		}
	    }
#endif /* CLEAN_URLS */
	}

    /*
    **	If different hosts, inherit no path.
    */
    if (given.host && related.host)
	if (strcmp(given.host, related.host) != 0) {
	    related.absolute = NULL;
	    related.relative = NULL;
	    related.anchor = NULL;
	}

    /*
    **	Handle the path.
    */
    if (wanted & PARSE_PATH) {
	if (acc_method && !given.absolute && given.relative) {
	    if (!strcasecomp(acc_method, "nntp") ||
		!strcasecomp(acc_method, "snews") ||
		(!strcasecomp(acc_method, "news") &&
		 !strncasecomp(result, "news://", 7))) {
		/*
		 *  Treat all given nntp or snews paths,
		 *  or given paths for news URLs with a host,
		 *  as absolute.
		 */
		given.absolute = given.relative;
		given.relative = NULL;
	    }
	}
	if (given.absolute) {			/* All is given */
	    if (wanted & PARSE_PUNCTUATION)
		strcat(result, "/");
	    strcat(result, given.absolute);
	    CTRACE(tfp, "1\n");
	} else if (related.absolute) {		/* Adopt path not name */
	    strcat(result, "/");
	    strcat(result, related.absolute);
	    if (given.relative) {
		p = strchr(result, '?');	/* Search part? */
		if (p == NULL)
		    p = (result + strlen(result) - 1);
		for (; *p != '/'; p--)
		    ;				/* last / */
		p[1] = '\0';			/* Remove filename */
		strcat(result, given.relative); /* Add given one */
		HTSimplify (result);
	    }
	    CTRACE(tfp, "2\n");
	} else if (given.relative) {
	    strcat(result, given.relative);		/* what we've got */
	    CTRACE(tfp, "3\n");
	} else if (related.relative) {
	    strcat(result, related.relative);
	    CTRACE(tfp, "4\n");
	} else {  /* No inheritance */
	    if (strncasecomp(aName, "lynxcgi:", 8) &&
		strncasecomp(aName, "lynxexec:", 9) &&
		strncasecomp(aName, "lynxprog:", 9)) {
		strcat(result, "/");
	    }
	    if (!strcmp(result, "news:/"))
		result[5] = '*';
	    CTRACE(tfp, "5\n");
	}
    }

    /*
    **	Handle the fragment (anchor).
    */
    if (wanted & PARSE_ANCHOR)
	if ((given.anchor && *given.anchor) ||
	    (!given.anchor && related.anchor)) {
	    if (wanted & PARSE_PUNCTUATION)
		strcat(result, "#");
	    strcat(result, (given.anchor) ?
			     given.anchor : related.anchor);
	}
    CTRACE(tfp, "HTParse: result:%s\n", result);
    FREE(rel);
    FREE(name);

    StrAllocCopy(return_value, result);
    FREE(result);

    return return_value;		/* exactly the right length */
}

/*	Simplify a filename.				HTSimplify()
**	--------------------
**
**  A unix-style file is allowed to contain the seqeunce xxx/../ which may
**  be replaced by "" , and the seqeunce "/./" which may be replaced by "/".
**  Simplification helps us recognize duplicate filenames.
**
**	Thus,	/etc/junk/../fred	becomes /etc/fred
**		/etc/junk/./fred	becomes /etc/junk/fred
**
**	but we should NOT change
**		http://fred.xxx.edu/../..
**
**	or	../../albert.html
*/
PUBLIC void HTSimplify ARGS1(
	char *, 	filename)
{
    char *p;
    char *q, *q1;

    if (filename == NULL)
	return;

    if (!(filename[0] && filename[1]) ||
	filename[0] == '?' || filename[1] == '?' || filename[2] == '?')
	return;

    if (strchr(filename, '/') != NULL) {
	for (p = (filename + 2); *p; p++) {
	    if (*p == '?') {
		/*
		**  We're still treating a ?searchpart as part of
		**  the path in HTParse() and scan(), but if we
		**  encounter a '?' here, assume it's the delimiter
		**  and break.	We also could check for a parameter
		**  delimiter (';') here, but the current Fielding
		**  draft (wisely or ill-advisedly :) says that it
		**  should be ignored and collapsing be allowed in
		**  it's value).  The only defined parameter at
		**  present is ;type=[A, I, or D] for ftp URLs, so
		**  if there's a "/..", "/../", "/./", or terminal
		**  '.' following the ';', it must be due to the
		**  ';' being an unescaped path character and not
		**  actually a parameter delimiter. - FM
		*/
		break;
	    }
	    if (*p == '/') {
		if ((p[1] == '.') && (p[2] == '.') &&
		    (p[3] == '/' || p[3] == '?' || p[3] == '\0')) {
		    /*
		    **	Handle "../", "..?" or "..".
		    */
		    for (q = (p - 1); (q >= filename) && (*q != '/'); q--)
			/*
			**  Back up to previous slash or beginning of string.
			*/
			;
		    if ((q[0] == '/') &&
			(strncmp(q, "/../", 4) &&
			 strncmp(q, "/..?", 4)) &&
			!((q - 1) > filename && q[-1] == '/')) {
			/*
			**  Not at beginning of string or in a
			**  host field, so remove the "/xxx/..".
			*/
			q1 = (p + 3);
			p = q;
			while (*q1 != '\0')
			    *p++ = *q1++;
			*p = '\0';		/* terminate */
#ifdef NOTDEFINED
			/*
			**  Make sure filename has at least one slash.
			*/
			if (*filename == '\0') {
			    *filename = '/';
			    *(filename + 1) = '\0';
			}
#endif /* NOTDEFINED */
			/*
			**  Start again with previous slash.
			*/
			p = (q - 1);
		    }
		} else if (p[1] == '.' && p[2] == '/') {
		    /*
		    **	Handle "./" by removing both characters.
		    */
		    q = p;
		    q1 = (p + 2);
		    while (*q1 != '\0')
		       *q++ = *q1++;
		    *q = '\0';		/* terminate */
		    p--;
		} else if (p[1] == '.' && p[2] == '?') {
		    /*
		    **	Handle ".?" by removing the dot.
		    */
		    q = (p + 1);
		    q1 = (p + 2);
		    while (*q1 != '\0')
		       *q++ = *q1++;
		    *q = '\0';		/* terminate */
		    p--;
		} else if (p[1] == '.' && p[2] == '\0') {
		    /*
		    **	Handle terminal "." by removing the character.
		    */
		    p[1] = '\0';
		}
	    }
	}
	if (p >= filename + 2 && *p == '?' && *(p-1)  == '.') {
	    if (*(p-2) == '/') {
		/*
		**  Handle "/.?" by removing the dot.
		*/
		q = p - 1;
		q1 = p;
		while (*q1 != '\0')
		    *q++ = *q1++;
		*q = '\0';
	    } else if (*(p-2) == '.' &&
		       p >= filename + 4 && *(p-3) == '/' &&
		       (*(p-4) != '/' ||
			(p > filename + 4 && *(p-5) != ':'))) {
		    /*
		    **	Handle "xxx/..?"
		    */
		for (q = (p - 4); (q > filename) && (*q != '/'); q--)
			/*
			**  Back up to previous slash or beginning of string.
			*/
		    ;
		if (*q == '/') {
		    if (q > filename && *(q-1) == '/' &&
			!(q > filename + 1 && *(q-1) != ':'))
			return;
		    q++;
		}
		if (strncmp(q, "../", 3) && strncmp(q, "./", 2)) {
			/*
			**  Not after "//" at beginning of string or
			**  after "://", and xxx is not ".." or ".",
			**  so remove the "xxx/..".
			*/
		    q1 = p;
		    p = q;
		    while (*q1 != '\0')
			*p++ = *q1++;
		    *p = '\0';		/* terminate */
		}
	    }
	}
    }
}

/*	Make Relative Name.					HTRelative()
**	-------------------
**
** This function creates and returns a string which gives an expression of
** one address as related to another. Where there is no relation, an absolute
** address is retured.
**
**  On entry,
**	Both names must be absolute, fully qualified names of nodes
**	(no anchor bits)
**
**  On exit,
**	The return result points to a newly allocated name which, if
**	parsed by HTParse relative to relatedName, will yield aName.
**	The caller is responsible for freeing the resulting name later.
**
*/
PUBLIC char * HTRelative ARGS2(
	CONST char *,	aName,
	CONST char *,	relatedName)
{
    char * result = NULL;
    CONST char *p = aName;
    CONST char *q = relatedName;
    CONST char * after_access = NULL;
    CONST char * path = NULL;
    CONST char * last_slash = NULL;
    int slashes = 0;

    for (; *p; p++, q++) {	/* Find extent of match */
	if (*p != *q)
	    break;
	if (*p == ':')
	    after_access = p+1;
	if (*p == '/') {
	    last_slash = p;
	    slashes++;
	    if (slashes == 3)
		path=p;
	}
    }

    /* q, p point to the first non-matching character or zero */

    if (!after_access) {			/* Different access */
	StrAllocCopy(result, aName);
    } else if (slashes < 3){			/* Different nodes */
	StrAllocCopy(result, after_access);
    } else if (slashes == 3){			/* Same node, different path */
	StrAllocCopy(result, path);
    } else {					/* Some path in common */
	int levels = 0;
	for (; *q && (*q != '#'); q++)
	    if (*q == '/')
		levels++;
	result = (char *)calloc(1, (3*levels + strlen(last_slash) + 1));
	if (result == NULL)
	    outofmem(__FILE__, "HTRelative");
	result[0] = '\0';
	for (; levels; levels--)
	    strcat(result, "../");
	strcat(result, last_slash+1);
    }
    CTRACE(tfp, "HT: `%s' expressed relative to\n    `%s' is\n   `%s'.",
		aName, relatedName, result);
    return result;
}

/*		Escape undesirable characters using %		HTEscape()
**		-------------------------------------
**
**	This function takes a pointer to a string in which
**	some characters may be unacceptable unescaped.
**	It returns a string which has these characters
**	represented by a '%' character followed by two hex digits.
**
**	Unlike HTUnEscape(), this routine returns a calloced string.
*/
PRIVATE CONST unsigned char isAcceptable[96] =

/*	Bit 0		xalpha		-- see HTFile.h
**	Bit 1		xpalpha 	-- as xalpha but with plus.
**	Bit 3 ...	path		-- as xpalphas but with /
*/
    /*	 0 1 2 3 4 5 6 7 8 9 A B C D E F */
    {	 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,	/* 2x	!"#$%&'()*+,-./  */
	 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,	/* 3x  0123456789:;<=>?  */
	 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	/* 4x  @ABCDEFGHIJKLMNO  */
	 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,	/* 5X  PQRSTUVWXYZ[\]^_  */
	 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	/* 6x  `abcdefghijklmno  */
	 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };	/* 7X  pqrstuvwxyz{|}~	DEL */

PRIVATE char *hex = "0123456789ABCDEF";
#define ACCEPTABLE(a)	( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))

PUBLIC char * HTEscape ARGS2(
	CONST char *,	str,
	unsigned char,	mask)
{
    CONST char * p;
    char * q;
    char * result;
    int unacceptable = 0;
    for (p = str; *p; p++)
	if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
	    unacceptable++;
    result = (char *)calloc(1, (p-str + unacceptable + unacceptable + 1));
    if (result == NULL)
	outofmem(__FILE__, "HTEscape");
    for (q = result, p = str; *p; p++) {
	unsigned char a = TOASCII(*p);
	if (!ACCEPTABLE(a)) {
	    *q++ = HEX_ESCAPE;	/* Means hex commming */
	    *q++ = hex[a >> 4];
	    *q++ = hex[a & 15];
	}
	else *q++ = *p;
    }
    *q++ = '\0';			/* Terminate */
    return result;
}

/*	Escape undesirable characters using % but space to +.	HTEscapeSP()
**	-----------------------------------------------------
**
**	This function takes a pointer to a string in which
**	some characters may be unacceptable unescaped.
**	It returns a string which has these characters
**	represented by a '%' character followed by two hex digits,
**	except that spaces are converted to '+' instead of %2B.
**
**	Unlike HTUnEscape(), this routine returns a calloced string.
*/
PUBLIC char * HTEscapeSP ARGS2(
	CONST char *,	str,
	unsigned char,	mask)
{
    CONST char * p;
    char * q;
    char * result;
    int unacceptable = 0;
    for (p = str; *p; p++)
	if (!(*p == ' ' || ACCEPTABLE((unsigned char)TOASCII(*p))))
	    unacceptable++;
    result = (char *)calloc(1, (p-str + unacceptable + unacceptable + 1));
    if (result == NULL)
	outofmem(__FILE__, "HTEscape");
    for (q = result, p = str; *p; p++) {
	unsigned char a = TOASCII(*p);
	if (a == 32) {
	    *q++ = '+';
	} else if (!ACCEPTABLE(a)) {
	    *q++ = HEX_ESCAPE;	/* Means hex commming */
	    *q++ = hex[a >> 4];
	    *q++ = hex[a & 15];
	} else {
	    *q++ = *p;
	}
    }
    *q++ = '\0';			/* Terminate */
    return result;
}

/*	Decode %xx escaped characters.				HTUnEscape()
**	------------------------------
**
**	This function takes a pointer to a string in which some
**	characters may have been encoded in %xy form, where xy is
**	the acsii hex code for character 16x+y.
**	The string is converted in place, as it will never grow.
*/
PRIVATE char from_hex ARGS1(
	char,		c)
{
    return  c >= '0' && c <= '9' ?  c - '0'
	    : c >= 'A' && c <= 'F'? c - 'A' + 10
	    : c - 'a' + 10;	/* accept small letters just in case */
}

PUBLIC char * HTUnEscape ARGS1(
	char *, 	str)
{
    char * p = str;
    char * q = str;

    if (!(p && *p))
	return str;

    while (*p != '\0') {
	if (*p == HEX_ESCAPE &&
	    /*
	     *	Tests shouldn't be needed, but better safe than sorry.
	     */
	    p[1] && p[2] &&
	    isxdigit((unsigned char)p[1]) &&
	    isxdigit((unsigned char)p[2])) {
	    p++;
	    if (*p)
	        *q = from_hex(*p++) * 16;
	    if (*p)
	        *q = FROMASCII(*q + from_hex(*p++));
	    q++;
	} else {
	    *q++ = *p++;
	}
    }

    *q++ = '\0';
    return str;

} /* HTUnEscape */

/*	Decode some %xx escaped characters.		      HTUnEscapeSome()
**	-----------------------------------			Klaus Weide
**							    (kweide@tezcat.com)
**	This function takes a pointer to a string in which some
**	characters may have been encoded in %xy form, where xy is
**	the acsii hex code for character 16x+y, and a pointer to
**	a second string containing one or more characters which
**	should be unescaped if escaped in the first string.
**	The first string is converted in place, as it will never grow.
*/
PUBLIC char * HTUnEscapeSome ARGS2(
	char *, 	str,
	CONST char *,	do_trans)
{
    char * p = str;
    char * q = str;
    char testcode;

    if (p == NULL || *p == '\0' || do_trans == NULL || *do_trans == '\0')
	return str;

    while (*p != '\0') {
	if (*p == HEX_ESCAPE &&
	    p[1] && p[2] &&	/* tests shouldn't be needed, but.. */
	    isxdigit((unsigned char)p[1]) &&
	    isxdigit((unsigned char)p[2]) &&
	    (testcode = from_hex(p[1])*16 + from_hex(p[2])) && /* %00 no good*/
	    strchr(do_trans, testcode)) { /* it's one of the ones we want */
	    *q++ = FROMASCII(testcode);
	    p += 3;
	} else {
	    *q++ = *p++;
	}
    }

    *q++ = '\0';
    return str;

} /* HTUnEscapeSome */

PRIVATE CONST unsigned char crfc[96] =

/*	Bit 0		xalpha		-- need "quoting"
**	Bit 1		xpalpha 	-- need \escape if quoted
*/
    /*	 0 1 2 3 4 5 6 7 8 9 A B C D E F */
    {	 1,0,3,0,0,0,0,0,1,1,0,0,1,0,1,0,	/* 2x	!"#$%&'()*+,-./  */
	 0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,	/* 3x  0123456789:;<=>?  */
	 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	/* 4x  @ABCDEFGHIJKLMNO  */
	 0,0,0,0,0,0,0,0,0,0,0,1,2,1,0,0,	/* 5X  PQRSTUVWXYZ[\]^_  */
	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	/* 6x  `abcdefghijklmno  */
	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 };	/* 7X  pqrstuvwxyz{|}~	DEL */

/*
**  Turn a string which is not a RFC 822 token into a quoted-string. - KW
*/
PUBLIC void HTMake822Word ARGS1(
	char **,	str)
{
    CONST char * p;
    char * q;
    char * result;
    unsigned char a;
    int added = 0;
    if (!(*str) || !(**str)) {
	StrAllocCopy(*str, "\"\"");
	return;
    }
    for (p = *str; *p; p++) {
	a = *p;
	if (a < 32 || a >= 128 ||
	    ((crfc[a-32]) & 1)) {
	    if (!added)
		added = 2;
	    if (a >= 160 || a == '\t')
		continue;
	    if (a == '\r' || a == '\n')
		added += 2;
	    else if ((a & 127) < 32 || ((crfc[a-32]) & 2))
		added++;
	}
    }
    if (!added)
	return;
    result = (char *)calloc(1, (p-(*str) + added + 1));
    if (result == NULL)
	outofmem(__FILE__, "HTMake822Word");
    result[0] = '"';
    for (q = result + 1, p = *str; *p; p++) {
	a = TOASCII(*p);
	if ((a != '\t') && ((a & 127) < 32 ||
			    ( a < 128 && ((crfc[a-32]) & 2))))
	    *q++ = '\\';
	*q++ = *p;
	if (a == '\n' || (a == '\r' && (TOASCII(*(p+1)) != '\n')))
	    *q++ = ' ';
    }
    *q++ = '"';
    *q++ = '\0';			/* Terminate */
    FREE(*str);
    *str = result;
}