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