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