1/*	$NetBSD$	*/
2
3/*++
4/* NAME
5/*	hex_quote 3
6/* SUMMARY
7/*	quote/unquote text, HTTP style.
8/* SYNOPSIS
9/*	#include <hex_quote.h>
10/*
11/*	VSTRING	*hex_quote(hex, raw)
12/*	VSTRING	*hex;
13/*	const char *raw;
14/*
15/*	VSTRING	*hex_unquote(raw, hex)
16/*	VSTRING	*raw;
17/*	const char *hex;
18/* DESCRIPTION
19/*	hex_quote() takes a null-terminated string and replaces non-printable
20/*	and whitespace characters and the % by %XX, XX being the two-digit
21/*	hexadecimal equivalent.
22/*	The hexadecimal codes are produced as upper-case characters. The result
23/*	value is the hex argument.
24/*
25/*	hex_unquote() performs the opposite transformation. This function
26/*	understands lowercase, uppercase, and mixed case %XX sequences. The
27/*	result value is the raw argument in case of success, a null pointer
28/*	otherwise.
29/* BUGS
30/*	hex_quote() cannot process null characters in data.
31/* LICENSE
32/* .ad
33/* .fi
34/*	The Secure Mailer license must be distributed with this software.
35/* AUTHOR(S)
36/*	Wietse Venema
37/*	IBM T.J. Watson Research
38/*	P.O. Box 704
39/*	Yorktown Heights, NY 10598, USA
40/*--*/
41
42/* System library. */
43
44#include "sys_defs.h"
45#include <ctype.h>
46
47/* Utility library. */
48
49#include "msg.h"
50#include "vstring.h"
51#include "hex_quote.h"
52
53/* Application-specific. */
54
55#define STR(x)	vstring_str(x)
56#define LEN(x)	VSTRING_LEN(x)
57
58/* hex_quote - raw data to quoted */
59
60VSTRING *hex_quote(VSTRING *hex, const char *raw)
61{
62    const char *cp;
63    int     ch;
64
65    VSTRING_RESET(hex);
66    for (cp = raw; (ch = *(unsigned const char *) cp) != 0; cp++) {
67	if (ch != '%' && !ISSPACE(ch) && ISPRINT(ch)) {
68	    VSTRING_ADDCH(hex, ch);
69	} else {
70	    vstring_sprintf_append(hex, "%%%02X", ch);
71	}
72    }
73    VSTRING_TERMINATE(hex);
74    return (hex);
75}
76
77/* hex_unquote - quoted data to raw */
78
79VSTRING *hex_unquote(VSTRING *raw, const char *hex)
80{
81    const char *cp;
82    int     ch;
83
84    VSTRING_RESET(raw);
85    for (cp = hex; (ch = *cp) != 0; cp++) {
86	if (ch == '%') {
87	    if (ISDIGIT(cp[1]))
88		ch = (cp[1] - '0') << 4;
89	    else if (cp[1] >= 'a' && cp[1] <= 'f')
90		ch = (cp[1] - 'a' + 10) << 4;
91	    else if (cp[1] >= 'A' && cp[1] <= 'F')
92		ch = (cp[1] - 'A' + 10) << 4;
93	    else
94		return (0);
95	    if (ISDIGIT(cp[2]))
96		ch |= (cp[2] - '0');
97	    else if (cp[2] >= 'a' && cp[2] <= 'f')
98		ch |= (cp[2] - 'a' + 10);
99	    else if (cp[2] >= 'A' && cp[2] <= 'F')
100		ch |= (cp[2] - 'A' + 10);
101	    else
102		return (0);
103	    cp += 2;
104	}
105	VSTRING_ADDCH(raw, ch);
106    }
107    VSTRING_TERMINATE(raw);
108    return (raw);
109}
110
111#ifdef TEST
112
113 /*
114  * Proof-of-concept test program: convert to hex and back.
115  */
116#include <vstream.h>
117
118#define BUFLEN 1024
119
120static ssize_t read_buf(VSTREAM *fp, VSTRING *buf)
121{
122    ssize_t len;
123
124    VSTRING_RESET(buf);
125    len = vstream_fread(fp, STR(buf), vstring_avail(buf));
126    VSTRING_AT_OFFSET(buf, len);		/* XXX */
127    VSTRING_TERMINATE(buf);
128    return (len);
129}
130
131int     main(int unused_argc, char **unused_argv)
132{
133    VSTRING *raw = vstring_alloc(BUFLEN);
134    VSTRING *hex = vstring_alloc(100);
135    ssize_t len;
136
137    while ((len = read_buf(VSTREAM_IN, raw)) > 0) {
138	hex_quote(hex, STR(raw));
139	if (hex_unquote(raw, STR(hex)) == 0)
140	    msg_fatal("bad input: %.100s", STR(hex));
141	if (LEN(raw) != len)
142	    msg_fatal("len %ld != raw len %ld", (long) len, (long) LEN(raw));
143	if (vstream_fwrite(VSTREAM_OUT, STR(raw), LEN(raw)) != LEN(raw))
144	    msg_fatal("write error: %m");
145    }
146    vstream_fflush(VSTREAM_OUT);
147    vstring_free(raw);
148    vstring_free(hex);
149    return (0);
150}
151
152#endif
153