vis.c revision 1.43 1 1.43 christos /* $NetBSD: vis.c,v 1.43 2011/03/12 05:23:41 christos 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 *
45 1.30 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 1.30 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 1.30 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 1.30 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 1.30 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 1.30 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 1.30 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 1.30 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 1.30 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 1.30 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 1.30 lukem * POSSIBILITY OF SUCH DAMAGE.
56 1.26 agc */
57 1.26 agc
58 1.7 christos #include <sys/cdefs.h>
59 1.21 tv #if defined(LIBC_SCCS) && !defined(lint)
60 1.43 christos __RCSID("$NetBSD: vis.c,v 1.43 2011/03/12 05:23:41 christos Exp $");
61 1.21 tv #endif /* LIBC_SCCS and not lint */
62 1.1 cgd
63 1.8 jtc #include "namespace.h"
64 1.1 cgd #include <sys/types.h>
65 1.12 lukem
66 1.12 lukem #include <assert.h>
67 1.1 cgd #include <vis.h>
68 1.22 christos #include <stdlib.h>
69 1.8 jtc
70 1.8 jtc #ifdef __weak_alias
71 1.18 mycroft __weak_alias(strsvis,_strsvis)
72 1.18 mycroft __weak_alias(strsvisx,_strsvisx)
73 1.18 mycroft __weak_alias(strvis,_strvis)
74 1.18 mycroft __weak_alias(strvisx,_strvisx)
75 1.18 mycroft __weak_alias(svis,_svis)
76 1.18 mycroft __weak_alias(vis,_vis)
77 1.20 tv #endif
78 1.20 tv
79 1.24 pooka #if !HAVE_VIS || !HAVE_SVIS
80 1.20 tv #include <ctype.h>
81 1.20 tv #include <limits.h>
82 1.20 tv #include <stdio.h>
83 1.20 tv #include <string.h>
84 1.1 cgd
85 1.42 christos static char *do_svis(char *, size_t *, int, int, int, const char *);
86 1.37 dsl
87 1.15 wennmach #undef BELL
88 1.15 wennmach #define BELL '\a'
89 1.15 wennmach
90 1.16 wennmach #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
91 1.16 wennmach #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
92 1.16 wennmach #define issafe(c) (c == '\b' || c == BELL || c == '\r')
93 1.22 christos #define xtoa(c) "0123456789abcdef"[c]
94 1.39 christos #define XTOA(c) "0123456789ABCDEF"[c]
95 1.16 wennmach
96 1.27 enami #define MAXEXTRAS 5
97 1.15 wennmach
98 1.34 martin #define MAKEEXTRALIST(flag, extra, orig_str) \
99 1.16 wennmach do { \
100 1.34 martin const char *orig = orig_str; \
101 1.22 christos const char *o = orig; \
102 1.27 enami char *e; \
103 1.22 christos while (*o++) \
104 1.22 christos continue; \
105 1.31 lukem extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
106 1.31 lukem if (!extra) break; \
107 1.22 christos for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
108 1.22 christos continue; \
109 1.22 christos e--; \
110 1.27 enami if (flag & VIS_SP) *e++ = ' '; \
111 1.22 christos if (flag & VIS_TAB) *e++ = '\t'; \
112 1.27 enami if (flag & VIS_NL) *e++ = '\n'; \
113 1.27 enami if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
114 1.22 christos *e = '\0'; \
115 1.19 mycroft } while (/*CONSTCOND*/0)
116 1.15 wennmach
117 1.22 christos /*
118 1.37 dsl * This is do_hvis, for HTTP style (RFC 1808)
119 1.22 christos */
120 1.37 dsl static char *
121 1.42 christos do_hvis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
122 1.37 dsl {
123 1.41 plunky
124 1.41 plunky if ((isascii(c) && isalnum(c))
125 1.41 plunky /* safe */
126 1.41 plunky || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
127 1.41 plunky /* extra */
128 1.41 plunky || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
129 1.41 plunky || c == ',') {
130 1.42 christos dst = do_svis(dst, len, c, flag, nextc, extra);
131 1.41 plunky } else {
132 1.42 christos if (len) {
133 1.42 christos if (*len < 3)
134 1.42 christos return NULL;
135 1.42 christos *len -= 3;
136 1.42 christos }
137 1.37 dsl *dst++ = '%';
138 1.37 dsl *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
139 1.37 dsl *dst++ = xtoa((unsigned int)c & 0xf);
140 1.37 dsl }
141 1.41 plunky
142 1.37 dsl return dst;
143 1.37 dsl }
144 1.27 enami
145 1.15 wennmach /*
146 1.39 christos * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
147 1.39 christos * NB: No handling of long lines or CRLF.
148 1.39 christos */
149 1.39 christos static char *
150 1.42 christos do_mvis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
151 1.39 christos {
152 1.39 christos if ((c != '\n') &&
153 1.39 christos /* Space at the end of the line */
154 1.39 christos ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
155 1.39 christos /* Out of range */
156 1.39 christos (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
157 1.39 christos /* Specific char to be escaped */
158 1.39 christos strchr("#$@[\\]^`{|}~", c) != NULL)) {
159 1.42 christos if (len) {
160 1.42 christos if (*len < 3)
161 1.42 christos return NULL;
162 1.42 christos *len -= 3;
163 1.42 christos }
164 1.39 christos *dst++ = '=';
165 1.39 christos *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
166 1.39 christos *dst++ = XTOA((unsigned int)c & 0xf);
167 1.39 christos } else {
168 1.42 christos dst = do_svis(dst, len, c, flag, nextc, extra);
169 1.39 christos }
170 1.39 christos return dst;
171 1.39 christos }
172 1.39 christos
173 1.39 christos /*
174 1.37 dsl * This is do_vis, the central code of vis.
175 1.16 wennmach * dst: Pointer to the destination buffer
176 1.16 wennmach * c: Character to encode
177 1.15 wennmach * flag: Flag word
178 1.15 wennmach * nextc: The character following 'c'
179 1.15 wennmach * extra: Pointer to the list of extra characters to be
180 1.16 wennmach * backslash-protected.
181 1.15 wennmach */
182 1.37 dsl static char *
183 1.42 christos do_svis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
184 1.37 dsl {
185 1.37 dsl int isextra;
186 1.43 christos size_t olen = len ? *len : 0;
187 1.43 christos
188 1.37 dsl isextra = strchr(extra, c) != NULL;
189 1.42 christos #define HAVE(x) \
190 1.42 christos do { \
191 1.42 christos if (len) { \
192 1.42 christos if (*len < (x)) \
193 1.42 christos goto out; \
194 1.42 christos *len -= (x); \
195 1.42 christos } \
196 1.42 christos } while (/*CONSTCOND*/0)
197 1.37 dsl if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
198 1.37 dsl ((flag & VIS_SAFE) && issafe(c)))) {
199 1.42 christos HAVE(1);
200 1.37 dsl *dst++ = c;
201 1.37 dsl return dst;
202 1.37 dsl }
203 1.37 dsl if (flag & VIS_CSTYLE) {
204 1.42 christos HAVE(2);
205 1.37 dsl switch (c) {
206 1.37 dsl case '\n':
207 1.37 dsl *dst++ = '\\'; *dst++ = 'n';
208 1.37 dsl return dst;
209 1.37 dsl case '\r':
210 1.37 dsl *dst++ = '\\'; *dst++ = 'r';
211 1.37 dsl return dst;
212 1.37 dsl case '\b':
213 1.37 dsl *dst++ = '\\'; *dst++ = 'b';
214 1.37 dsl return dst;
215 1.37 dsl case BELL:
216 1.37 dsl *dst++ = '\\'; *dst++ = 'a';
217 1.37 dsl return dst;
218 1.37 dsl case '\v':
219 1.37 dsl *dst++ = '\\'; *dst++ = 'v';
220 1.37 dsl return dst;
221 1.37 dsl case '\t':
222 1.37 dsl *dst++ = '\\'; *dst++ = 't';
223 1.37 dsl return dst;
224 1.37 dsl case '\f':
225 1.37 dsl *dst++ = '\\'; *dst++ = 'f';
226 1.37 dsl return dst;
227 1.37 dsl case ' ':
228 1.37 dsl *dst++ = '\\'; *dst++ = 's';
229 1.37 dsl return dst;
230 1.37 dsl case '\0':
231 1.37 dsl *dst++ = '\\'; *dst++ = '0';
232 1.37 dsl if (isoctal(nextc)) {
233 1.42 christos HAVE(2);
234 1.37 dsl *dst++ = '0';
235 1.37 dsl *dst++ = '0';
236 1.37 dsl }
237 1.37 dsl return dst;
238 1.37 dsl default:
239 1.37 dsl if (isgraph(c)) {
240 1.37 dsl *dst++ = '\\'; *dst++ = c;
241 1.37 dsl return dst;
242 1.37 dsl }
243 1.42 christos if (len)
244 1.42 christos *len = olen;
245 1.37 dsl }
246 1.37 dsl }
247 1.37 dsl if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
248 1.42 christos HAVE(4);
249 1.37 dsl *dst++ = '\\';
250 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
251 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
252 1.37 dsl *dst++ = (c & 07) + '0';
253 1.37 dsl } else {
254 1.42 christos if ((flag & VIS_NOSLASH) == 0) {
255 1.42 christos HAVE(1);
256 1.42 christos *dst++ = '\\';
257 1.42 christos }
258 1.42 christos
259 1.37 dsl if (c & 0200) {
260 1.42 christos HAVE(1);
261 1.37 dsl c &= 0177; *dst++ = 'M';
262 1.37 dsl }
263 1.42 christos
264 1.37 dsl if (iscntrl(c)) {
265 1.42 christos HAVE(2);
266 1.37 dsl *dst++ = '^';
267 1.37 dsl if (c == 0177)
268 1.37 dsl *dst++ = '?';
269 1.37 dsl else
270 1.37 dsl *dst++ = c + '@';
271 1.37 dsl } else {
272 1.42 christos HAVE(2);
273 1.37 dsl *dst++ = '-'; *dst++ = c;
274 1.37 dsl }
275 1.37 dsl }
276 1.37 dsl return dst;
277 1.42 christos out:
278 1.42 christos *len = olen;
279 1.42 christos return NULL;
280 1.37 dsl }
281 1.15 wennmach
282 1.42 christos typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
283 1.39 christos
284 1.39 christos /*
285 1.39 christos * Return the appropriate encoding function depending on the flags given.
286 1.39 christos */
287 1.39 christos static visfun_t
288 1.39 christos getvisfun(int flag)
289 1.39 christos {
290 1.39 christos if (flag & VIS_HTTPSTYLE)
291 1.39 christos return do_hvis;
292 1.40 christos if (flag & VIS_MIMESTYLE)
293 1.39 christos return do_mvis;
294 1.39 christos return do_svis;
295 1.39 christos }
296 1.15 wennmach
297 1.15 wennmach /*
298 1.17 wennmach * svis - visually encode characters, also encoding the characters
299 1.33 lukem * pointed to by `extra'
300 1.15 wennmach */
301 1.15 wennmach char *
302 1.33 lukem svis(char *dst, int c, int flag, int nextc, const char *extra)
303 1.15 wennmach {
304 1.31 lukem char *nextra = NULL;
305 1.39 christos visfun_t f;
306 1.31 lukem
307 1.16 wennmach _DIAGASSERT(dst != NULL);
308 1.16 wennmach _DIAGASSERT(extra != NULL);
309 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
310 1.31 lukem if (!nextra) {
311 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
312 1.33 lukem return dst;
313 1.31 lukem }
314 1.39 christos f = getvisfun(flag);
315 1.42 christos dst = (*f)(dst, NULL, c, flag, nextc, nextra);
316 1.31 lukem free(nextra);
317 1.16 wennmach *dst = '\0';
318 1.33 lukem return dst;
319 1.15 wennmach }
320 1.15 wennmach
321 1.15 wennmach
322 1.15 wennmach /*
323 1.15 wennmach * strsvis, strsvisx - visually encode characters from src into dst
324 1.15 wennmach *
325 1.16 wennmach * Extra is a pointer to a \0-terminated list of characters to
326 1.17 wennmach * be encoded, too. These functions are useful e. g. to
327 1.17 wennmach * encode strings in such a way so that they are not interpreted
328 1.16 wennmach * by a shell.
329 1.27 enami *
330 1.16 wennmach * Dst must be 4 times the size of src to account for possible
331 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
332 1.27 enami * is returned.
333 1.15 wennmach *
334 1.16 wennmach * Strsvisx encodes exactly len bytes from src into dst.
335 1.16 wennmach * This is useful for encoding a block of data.
336 1.15 wennmach */
337 1.15 wennmach int
338 1.33 lukem strsvis(char *dst, const char *csrc, int flag, const char *extra)
339 1.15 wennmach {
340 1.25 dsl int c;
341 1.16 wennmach char *start;
342 1.31 lukem char *nextra = NULL;
343 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
344 1.39 christos visfun_t f;
345 1.15 wennmach
346 1.16 wennmach _DIAGASSERT(dst != NULL);
347 1.16 wennmach _DIAGASSERT(src != NULL);
348 1.16 wennmach _DIAGASSERT(extra != NULL);
349 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
350 1.31 lukem if (!nextra) {
351 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
352 1.31 lukem return 0;
353 1.31 lukem }
354 1.39 christos f = getvisfun(flag);
355 1.39 christos for (start = dst; (c = *src++) != '\0'; /* empty */)
356 1.42 christos dst = (*f)(dst, NULL, c, flag, *src, nextra);
357 1.31 lukem free(nextra);
358 1.16 wennmach *dst = '\0';
359 1.39 christos return (int)(dst - start);
360 1.15 wennmach }
361 1.15 wennmach
362 1.15 wennmach
363 1.15 wennmach int
364 1.33 lukem strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
365 1.15 wennmach {
366 1.28 christos unsigned char c;
367 1.16 wennmach char *start;
368 1.31 lukem char *nextra = NULL;
369 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
370 1.39 christos visfun_t f;
371 1.15 wennmach
372 1.16 wennmach _DIAGASSERT(dst != NULL);
373 1.16 wennmach _DIAGASSERT(src != NULL);
374 1.16 wennmach _DIAGASSERT(extra != NULL);
375 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
376 1.31 lukem if (! nextra) {
377 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
378 1.31 lukem return 0;
379 1.31 lukem }
380 1.16 wennmach
381 1.39 christos f = getvisfun(flag);
382 1.39 christos for (start = dst; len > 0; len--) {
383 1.39 christos c = *src++;
384 1.42 christos dst = (*f)(dst, NULL, c, flag, len > 1 ? *src : '\0', nextra);
385 1.16 wennmach }
386 1.31 lukem free(nextra);
387 1.16 wennmach *dst = '\0';
388 1.39 christos return (int)(dst - start);
389 1.15 wennmach }
390 1.24 pooka #endif
391 1.15 wennmach
392 1.24 pooka #if !HAVE_VIS
393 1.1 cgd /*
394 1.1 cgd * vis - visually encode characters
395 1.1 cgd */
396 1.1 cgd char *
397 1.33 lukem vis(char *dst, int c, int flag, int nextc)
398 1.15 wennmach {
399 1.31 lukem char *extra = NULL;
400 1.28 christos unsigned char uc = (unsigned char)c;
401 1.39 christos visfun_t f;
402 1.15 wennmach
403 1.16 wennmach _DIAGASSERT(dst != NULL);
404 1.15 wennmach
405 1.22 christos MAKEEXTRALIST(flag, extra, "");
406 1.31 lukem if (! extra) {
407 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
408 1.33 lukem return dst;
409 1.31 lukem }
410 1.39 christos f = getvisfun(flag);
411 1.42 christos dst = (*f)(dst, NULL, uc, flag, nextc, extra);
412 1.32 lukem free(extra);
413 1.16 wennmach *dst = '\0';
414 1.33 lukem return dst;
415 1.1 cgd }
416 1.1 cgd
417 1.15 wennmach
418 1.1 cgd /*
419 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
420 1.27 enami *
421 1.16 wennmach * Dst must be 4 times the size of src to account for possible
422 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
423 1.27 enami * is returned.
424 1.1 cgd *
425 1.16 wennmach * Strvisx encodes exactly len bytes from src into dst.
426 1.16 wennmach * This is useful for encoding a block of data.
427 1.1 cgd */
428 1.1 cgd int
429 1.33 lukem strvis(char *dst, const char *src, int flag)
430 1.15 wennmach {
431 1.31 lukem char *extra = NULL;
432 1.31 lukem int rv;
433 1.15 wennmach
434 1.22 christos MAKEEXTRALIST(flag, extra, "");
435 1.31 lukem if (!extra) {
436 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
437 1.31 lukem return 0;
438 1.31 lukem }
439 1.31 lukem rv = strsvis(dst, src, flag, extra);
440 1.31 lukem free(extra);
441 1.33 lukem return rv;
442 1.1 cgd }
443 1.1 cgd
444 1.15 wennmach
445 1.1 cgd int
446 1.33 lukem strvisx(char *dst, const char *src, size_t len, int flag)
447 1.15 wennmach {
448 1.31 lukem char *extra = NULL;
449 1.31 lukem int rv;
450 1.1 cgd
451 1.22 christos MAKEEXTRALIST(flag, extra, "");
452 1.31 lukem if (!extra) {
453 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
454 1.31 lukem return 0;
455 1.31 lukem }
456 1.31 lukem rv = strsvisx(dst, src, len, flag, extra);
457 1.31 lukem free(extra);
458 1.33 lukem return rv;
459 1.1 cgd }
460 1.20 tv #endif
461