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