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
|
/*++
/* NAME
/* hex_quote 3
/* SUMMARY
/* quote/unquote text, HTTP style.
/* SYNOPSIS
/* #include <hex_quote.h>
/*
/* VSTRING *hex_quote(hex, raw)
/* VSTRING *hex;
/* const char *raw;
/*
/* VSTRING *hex_unquote(raw, hex)
/* VSTRING *raw;
/* const char *hex;
/* DESCRIPTION
/* hex_quote() takes a null-terminated string and replaces non-printable
/* and whitespace characters and the % by %XX, XX being the two-digit
/* hexadecimal equivalent.
/* The hexadecimal codes are produced as upper-case characters. The result
/* value is the hex argument.
/*
/* hex_unquote() performs the opposite transformation. This function
/* understands lowercase, uppercase, and mixed case %XX sequences. The
/* result value is the raw argument in case of success, a null pointer
/* otherwise.
/* BUGS
/* hex_quote() cannot process null characters in data.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*
/* Wietse Venema
/* Google, Inc.
/* 111 8th Avenue
/* New York, NY 10011, USA
/*--*/
/* System library. */
#include "sys_defs.h"
#include <ctype.h>
/* Utility library. */
#include "msg.h"
#include "vstring.h"
#include "hex_quote.h"
/* Application-specific. */
#define STR(x) vstring_str(x)
#define LEN(x) VSTRING_LEN(x)
/* hex_quote - raw data to quoted */
VSTRING *hex_quote(VSTRING *hex, const char *raw)
{
const char *cp;
int ch;
VSTRING_RESET(hex);
for (cp = raw; (ch = *(unsigned const char *) cp) != 0; cp++) {
if (ch != '%' && !ISSPACE(ch) && ISPRINT(ch)) {
VSTRING_ADDCH(hex, ch);
} else {
vstring_sprintf_append(hex, "%%%02X", ch);
}
}
VSTRING_TERMINATE(hex);
return (hex);
}
/* hex_unquote - quoted data to raw */
VSTRING *hex_unquote(VSTRING *raw, const char *hex)
{
const char *cp;
int ch;
VSTRING_RESET(raw);
for (cp = hex; (ch = *cp) != 0; cp++) {
if (ch == '%') {
if (ISDIGIT(cp[1]))
ch = (cp[1] - '0') << 4;
else if (cp[1] >= 'a' && cp[1] <= 'f')
ch = (cp[1] - 'a' + 10) << 4;
else if (cp[1] >= 'A' && cp[1] <= 'F')
ch = (cp[1] - 'A' + 10) << 4;
else
return (0);
if (ISDIGIT(cp[2]))
ch |= (cp[2] - '0');
else if (cp[2] >= 'a' && cp[2] <= 'f')
ch |= (cp[2] - 'a' + 10);
else if (cp[2] >= 'A' && cp[2] <= 'F')
ch |= (cp[2] - 'A' + 10);
else
return (0);
cp += 2;
}
VSTRING_ADDCH(raw, ch);
}
VSTRING_TERMINATE(raw);
return (raw);
}
#ifdef TEST
/*
* Proof-of-concept test program: convert to hex and back.
*/
#include <vstream.h>
#define BUFLEN 1024
static ssize_t read_buf(VSTREAM *fp, VSTRING *buf)
{
ssize_t len;
len = vstream_fread_buf(fp, buf, BUFLEN);
VSTRING_TERMINATE(buf);
return (len);
}
int main(int unused_argc, char **unused_argv)
{
VSTRING *raw = vstring_alloc(BUFLEN);
VSTRING *hex = vstring_alloc(100);
ssize_t len;
while ((len = read_buf(VSTREAM_IN, raw)) > 0) {
hex_quote(hex, STR(raw));
if (hex_unquote(raw, STR(hex)) == 0)
msg_fatal("bad input: %.100s", STR(hex));
if (LEN(raw) != len)
msg_fatal("len %ld != raw len %ld", (long) len, (long) LEN(raw));
if (vstream_fwrite(VSTREAM_OUT, STR(raw), LEN(raw)) != LEN(raw))
msg_fatal("write error: %m");
}
vstream_fflush(VSTREAM_OUT);
vstring_free(raw);
vstring_free(hex);
return (0);
}
#endif
|