vis.c revision 1.25 1 1.25 dsl /* $NetBSD: vis.c,v 1.25 2003/07/16 22:34:34 dsl Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.15 wennmach * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.6 cgd * Copyright (c) 1989, 1993
6 1.16 wennmach * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.16 wennmach * This product includes software developed by the University of
19 1.16 wennmach * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.7 christos #include <sys/cdefs.h>
38 1.21 tv #if defined(LIBC_SCCS) && !defined(lint)
39 1.25 dsl __RCSID("$NetBSD: vis.c,v 1.25 2003/07/16 22:34:34 dsl Exp $");
40 1.21 tv #endif /* LIBC_SCCS and not lint */
41 1.1 cgd
42 1.8 jtc #include "namespace.h"
43 1.1 cgd #include <sys/types.h>
44 1.12 lukem
45 1.12 lukem #include <assert.h>
46 1.1 cgd #include <vis.h>
47 1.22 christos #include <stdlib.h>
48 1.8 jtc
49 1.8 jtc #ifdef __weak_alias
50 1.18 mycroft __weak_alias(strsvis,_strsvis)
51 1.18 mycroft __weak_alias(strsvisx,_strsvisx)
52 1.18 mycroft __weak_alias(strvis,_strvis)
53 1.18 mycroft __weak_alias(strvisx,_strvisx)
54 1.18 mycroft __weak_alias(svis,_svis)
55 1.18 mycroft __weak_alias(vis,_vis)
56 1.20 tv #endif
57 1.20 tv
58 1.24 pooka #if !HAVE_VIS || !HAVE_SVIS
59 1.20 tv #include <ctype.h>
60 1.20 tv #include <limits.h>
61 1.20 tv #include <stdio.h>
62 1.20 tv #include <string.h>
63 1.1 cgd
64 1.15 wennmach #undef BELL
65 1.15 wennmach #define BELL '\a'
66 1.15 wennmach
67 1.16 wennmach #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
68 1.16 wennmach #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
69 1.16 wennmach #define issafe(c) (c == '\b' || c == BELL || c == '\r')
70 1.22 christos #define xtoa(c) "0123456789abcdef"[c]
71 1.16 wennmach
72 1.16 wennmach #define MAXEXTRAS 5
73 1.15 wennmach
74 1.15 wennmach
75 1.22 christos #define MAKEEXTRALIST(flag, extra, orig) \
76 1.16 wennmach do { \
77 1.22 christos const char *o = orig; \
78 1.22 christos char *e; \
79 1.22 christos while (*o++) \
80 1.22 christos continue; \
81 1.22 christos extra = alloca((size_t)((o - orig) + MAXEXTRAS)); \
82 1.22 christos for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
83 1.22 christos continue; \
84 1.22 christos e--; \
85 1.22 christos if (flag & VIS_SP) *e++ = ' '; \
86 1.22 christos if (flag & VIS_TAB) *e++ = '\t'; \
87 1.22 christos if (flag & VIS_NL) *e++ = '\n'; \
88 1.22 christos if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
89 1.22 christos *e = '\0'; \
90 1.19 mycroft } while (/*CONSTCOND*/0)
91 1.15 wennmach
92 1.22 christos
93 1.22 christos /*
94 1.22 christos * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
95 1.22 christos */
96 1.22 christos #define HVIS(dst, c, flag, nextc, extra) \
97 1.22 christos do \
98 1.22 christos if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
99 1.22 christos *dst++ = '%'; \
100 1.22 christos *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); \
101 1.22 christos *dst++ = xtoa((unsigned int)c & 0xf); \
102 1.22 christos } else { \
103 1.22 christos SVIS(dst, c, flag, nextc, extra); \
104 1.22 christos } \
105 1.22 christos while (/*CONSTCOND*/0)
106 1.22 christos
107 1.15 wennmach /*
108 1.15 wennmach * This is SVIS, the central macro of vis.
109 1.16 wennmach * dst: Pointer to the destination buffer
110 1.16 wennmach * c: Character to encode
111 1.15 wennmach * flag: Flag word
112 1.15 wennmach * nextc: The character following 'c'
113 1.15 wennmach * extra: Pointer to the list of extra characters to be
114 1.16 wennmach * backslash-protected.
115 1.15 wennmach */
116 1.16 wennmach #define SVIS(dst, c, flag, nextc, extra) \
117 1.16 wennmach do { \
118 1.25 dsl int isextra; \
119 1.16 wennmach isextra = strchr(extra, c) != NULL; \
120 1.17 wennmach if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \
121 1.17 wennmach ((flag & VIS_SAFE) && issafe(c)))) { \
122 1.16 wennmach *dst++ = c; \
123 1.16 wennmach break; \
124 1.16 wennmach } \
125 1.16 wennmach if (flag & VIS_CSTYLE) { \
126 1.16 wennmach switch (c) { \
127 1.16 wennmach case '\n': \
128 1.25 dsl *dst++ = '\\'; *dst++ = 'n'; \
129 1.25 dsl continue; \
130 1.16 wennmach case '\r': \
131 1.25 dsl *dst++ = '\\'; *dst++ = 'r'; \
132 1.25 dsl continue; \
133 1.16 wennmach case '\b': \
134 1.25 dsl *dst++ = '\\'; *dst++ = 'b'; \
135 1.25 dsl continue; \
136 1.16 wennmach case BELL: \
137 1.25 dsl *dst++ = '\\'; *dst++ = 'a'; \
138 1.25 dsl continue; \
139 1.16 wennmach case '\v': \
140 1.25 dsl *dst++ = '\\'; *dst++ = 'v'; \
141 1.25 dsl continue; \
142 1.16 wennmach case '\t': \
143 1.25 dsl *dst++ = '\\'; *dst++ = 't'; \
144 1.25 dsl continue; \
145 1.16 wennmach case '\f': \
146 1.25 dsl *dst++ = '\\'; *dst++ = 'f'; \
147 1.25 dsl continue; \
148 1.16 wennmach case ' ': \
149 1.25 dsl *dst++ = '\\'; *dst++ = 's'; \
150 1.25 dsl continue; \
151 1.16 wennmach case '\0': \
152 1.25 dsl *dst++ = '\\'; *dst++ = '0'; \
153 1.16 wennmach if (isoctal(nextc)) { \
154 1.16 wennmach *dst++ = '0'; \
155 1.16 wennmach *dst++ = '0'; \
156 1.16 wennmach } \
157 1.25 dsl continue; \
158 1.25 dsl default: \
159 1.25 dsl if (isgraph(c)) { \
160 1.25 dsl *dst++ = '\\'; *dst++ = c; \
161 1.25 dsl continue; \
162 1.25 dsl } \
163 1.16 wennmach } \
164 1.16 wennmach } \
165 1.17 wennmach if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
166 1.16 wennmach *dst++ = '\\'; \
167 1.16 wennmach *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
168 1.16 wennmach *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
169 1.16 wennmach *dst++ = (c & 07) + '0'; \
170 1.16 wennmach } else { \
171 1.16 wennmach if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
172 1.16 wennmach if (c & 0200) { \
173 1.16 wennmach c &= 0177; *dst++ = 'M'; \
174 1.16 wennmach } \
175 1.16 wennmach if (iscntrl(c)) { \
176 1.16 wennmach *dst++ = '^'; \
177 1.16 wennmach if (c == 0177) \
178 1.16 wennmach *dst++ = '?'; \
179 1.16 wennmach else \
180 1.16 wennmach *dst++ = c + '@'; \
181 1.16 wennmach } else { \
182 1.16 wennmach *dst++ = '-'; *dst++ = c; \
183 1.16 wennmach } \
184 1.16 wennmach } \
185 1.19 mycroft } while (/*CONSTCOND*/0)
186 1.15 wennmach
187 1.15 wennmach
188 1.15 wennmach /*
189 1.17 wennmach * svis - visually encode characters, also encoding the characters
190 1.17 wennmach * pointed to by `extra'
191 1.15 wennmach */
192 1.15 wennmach char *
193 1.15 wennmach svis(dst, c, flag, nextc, extra)
194 1.16 wennmach char *dst;
195 1.16 wennmach int c, flag, nextc;
196 1.16 wennmach const char *extra;
197 1.15 wennmach {
198 1.22 christos char *nextra;
199 1.16 wennmach _DIAGASSERT(dst != NULL);
200 1.16 wennmach _DIAGASSERT(extra != NULL);
201 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
202 1.22 christos if (flag & VIS_HTTPSTYLE)
203 1.22 christos HVIS(dst, c, flag, nextc, nextra);
204 1.22 christos else
205 1.22 christos SVIS(dst, c, flag, nextc, nextra);
206 1.16 wennmach *dst = '\0';
207 1.16 wennmach return(dst);
208 1.15 wennmach }
209 1.15 wennmach
210 1.15 wennmach
211 1.15 wennmach /*
212 1.15 wennmach * strsvis, strsvisx - visually encode characters from src into dst
213 1.15 wennmach *
214 1.16 wennmach * Extra is a pointer to a \0-terminated list of characters to
215 1.17 wennmach * be encoded, too. These functions are useful e. g. to
216 1.17 wennmach * encode strings in such a way so that they are not interpreted
217 1.16 wennmach * by a shell.
218 1.16 wennmach *
219 1.16 wennmach * Dst must be 4 times the size of src to account for possible
220 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
221 1.16 wennmach * is returned.
222 1.15 wennmach *
223 1.16 wennmach * Strsvisx encodes exactly len bytes from src into dst.
224 1.16 wennmach * This is useful for encoding a block of data.
225 1.15 wennmach */
226 1.15 wennmach int
227 1.25 dsl strsvis(dst, csrc, flag, extra)
228 1.16 wennmach char *dst;
229 1.25 dsl const char *csrc;
230 1.16 wennmach int flag;
231 1.16 wennmach const char *extra;
232 1.15 wennmach {
233 1.25 dsl int c;
234 1.16 wennmach char *start;
235 1.22 christos char *nextra;
236 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
237 1.15 wennmach
238 1.16 wennmach _DIAGASSERT(dst != NULL);
239 1.16 wennmach _DIAGASSERT(src != NULL);
240 1.16 wennmach _DIAGASSERT(extra != NULL);
241 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
242 1.22 christos if (flag & VIS_HTTPSTYLE) {
243 1.22 christos for (start = dst; (c = *src++) != '\0'; /* empty */)
244 1.22 christos HVIS(dst, c, flag, *src, nextra);
245 1.22 christos } else {
246 1.22 christos for (start = dst; (c = *src++) != '\0'; /* empty */)
247 1.22 christos SVIS(dst, c, flag, *src, nextra);
248 1.22 christos }
249 1.16 wennmach *dst = '\0';
250 1.16 wennmach return (dst - start);
251 1.15 wennmach }
252 1.15 wennmach
253 1.15 wennmach
254 1.15 wennmach int
255 1.25 dsl strsvisx(dst, csrc, len, flag, extra)
256 1.16 wennmach char *dst;
257 1.25 dsl const char *csrc;
258 1.16 wennmach size_t len;
259 1.16 wennmach int flag;
260 1.16 wennmach const char *extra;
261 1.15 wennmach {
262 1.25 dsl int c;
263 1.16 wennmach char *start;
264 1.22 christos char *nextra;
265 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
266 1.15 wennmach
267 1.16 wennmach _DIAGASSERT(dst != NULL);
268 1.16 wennmach _DIAGASSERT(src != NULL);
269 1.16 wennmach _DIAGASSERT(extra != NULL);
270 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
271 1.16 wennmach
272 1.22 christos if (flag & VIS_HTTPSTYLE) {
273 1.22 christos for (start = dst; len > 0; len--) {
274 1.22 christos c = *src++;
275 1.22 christos HVIS(dst, c, flag, len ? *src : '\0', nextra);
276 1.22 christos }
277 1.22 christos } else {
278 1.22 christos for (start = dst; len > 0; len--) {
279 1.22 christos c = *src++;
280 1.22 christos SVIS(dst, c, flag, len ? *src : '\0', nextra);
281 1.22 christos }
282 1.16 wennmach }
283 1.16 wennmach *dst = '\0';
284 1.16 wennmach return (dst - start);
285 1.15 wennmach }
286 1.24 pooka #endif
287 1.15 wennmach
288 1.24 pooka #if !HAVE_VIS
289 1.1 cgd /*
290 1.1 cgd * vis - visually encode characters
291 1.1 cgd */
292 1.1 cgd char *
293 1.1 cgd vis(dst, c, flag, nextc)
294 1.16 wennmach char *dst;
295 1.16 wennmach int c, flag, nextc;
296 1.16 wennmach
297 1.15 wennmach {
298 1.22 christos char *extra;
299 1.15 wennmach
300 1.16 wennmach _DIAGASSERT(dst != NULL);
301 1.15 wennmach
302 1.22 christos MAKEEXTRALIST(flag, extra, "");
303 1.22 christos if (flag & VIS_HTTPSTYLE)
304 1.22 christos HVIS(dst, c, flag, nextc, extra);
305 1.22 christos else
306 1.22 christos SVIS(dst, c, flag, nextc, extra);
307 1.16 wennmach *dst = '\0';
308 1.16 wennmach return (dst);
309 1.1 cgd }
310 1.1 cgd
311 1.15 wennmach
312 1.1 cgd /*
313 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
314 1.16 wennmach *
315 1.16 wennmach * Dst must be 4 times the size of src to account for possible
316 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
317 1.16 wennmach * is returned.
318 1.1 cgd *
319 1.16 wennmach * Strvisx encodes exactly len bytes from src into dst.
320 1.16 wennmach * This is useful for encoding a block of data.
321 1.1 cgd */
322 1.1 cgd int
323 1.1 cgd strvis(dst, src, flag)
324 1.16 wennmach char *dst;
325 1.16 wennmach const char *src;
326 1.16 wennmach int flag;
327 1.15 wennmach {
328 1.22 christos char *extra;
329 1.15 wennmach
330 1.22 christos MAKEEXTRALIST(flag, extra, "");
331 1.16 wennmach return (strsvis(dst, src, flag, extra));
332 1.1 cgd }
333 1.1 cgd
334 1.15 wennmach
335 1.1 cgd int
336 1.1 cgd strvisx(dst, src, len, flag)
337 1.16 wennmach char *dst;
338 1.16 wennmach const char *src;
339 1.16 wennmach size_t len;
340 1.16 wennmach int flag;
341 1.15 wennmach {
342 1.22 christos char *extra;
343 1.1 cgd
344 1.22 christos MAKEEXTRALIST(flag, extra, "");
345 1.16 wennmach return (strsvisx(dst, src, len, flag, extra));
346 1.1 cgd }
347 1.20 tv #endif
348