Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / hex_quote.c
blobbdfaddde80d42a0ea7da4b11348a2788f3f61ee5
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* hex_quote 3
6 /* SUMMARY
7 /* quote/unquote text, HTTP style.
8 /* SYNOPSIS
9 /* #include <hex_quote.h>
11 /* VSTRING *hex_quote(hex, raw)
12 /* VSTRING *hex;
13 /* const char *raw;
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.
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 /*--*/
42 /* System library. */
44 #include "sys_defs.h"
45 #include <ctype.h>
47 /* Utility library. */
49 #include "msg.h"
50 #include "vstring.h"
51 #include "hex_quote.h"
53 /* Application-specific. */
55 #define STR(x) vstring_str(x)
56 #define LEN(x) VSTRING_LEN(x)
58 /* hex_quote - raw data to quoted */
60 VSTRING *hex_quote(VSTRING *hex, const char *raw)
62 const char *cp;
63 int ch;
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);
73 VSTRING_TERMINATE(hex);
74 return (hex);
77 /* hex_unquote - quoted data to raw */
79 VSTRING *hex_unquote(VSTRING *raw, const char *hex)
81 const char *cp;
82 int ch;
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;
105 VSTRING_ADDCH(raw, ch);
107 VSTRING_TERMINATE(raw);
108 return (raw);
111 #ifdef TEST
114 * Proof-of-concept test program: convert to hex and back.
116 #include <vstream.h>
118 #define BUFLEN 1024
120 static ssize_t read_buf(VSTREAM *fp, VSTRING *buf)
122 ssize_t len;
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);
131 int main(int unused_argc, char **unused_argv)
133 VSTRING *raw = vstring_alloc(BUFLEN);
134 VSTRING *hex = vstring_alloc(100);
135 ssize_t len;
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");
146 vstream_fflush(VSTREAM_OUT);
147 vstring_free(raw);
148 vstring_free(hex);
149 return (0);
152 #endif