vis.c revision 1.44 1 1.44 christos /* $NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 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.44 christos __RCSID("$NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 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.44 christos #include <errno.h>
69 1.22 christos #include <stdlib.h>
70 1.8 jtc
71 1.8 jtc #ifdef __weak_alias
72 1.18 mycroft __weak_alias(strvisx,_strvisx)
73 1.20 tv #endif
74 1.20 tv
75 1.24 pooka #if !HAVE_VIS || !HAVE_SVIS
76 1.20 tv #include <ctype.h>
77 1.20 tv #include <limits.h>
78 1.20 tv #include <stdio.h>
79 1.20 tv #include <string.h>
80 1.1 cgd
81 1.42 christos static char *do_svis(char *, size_t *, int, int, int, const char *);
82 1.37 dsl
83 1.15 wennmach #undef BELL
84 1.15 wennmach #define BELL '\a'
85 1.15 wennmach
86 1.16 wennmach #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
87 1.16 wennmach #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
88 1.16 wennmach #define issafe(c) (c == '\b' || c == BELL || c == '\r')
89 1.22 christos #define xtoa(c) "0123456789abcdef"[c]
90 1.39 christos #define XTOA(c) "0123456789ABCDEF"[c]
91 1.16 wennmach
92 1.27 enami #define MAXEXTRAS 5
93 1.15 wennmach
94 1.34 martin #define MAKEEXTRALIST(flag, extra, orig_str) \
95 1.16 wennmach do { \
96 1.34 martin const char *orig = orig_str; \
97 1.22 christos const char *o = orig; \
98 1.27 enami char *e; \
99 1.22 christos while (*o++) \
100 1.22 christos continue; \
101 1.31 lukem extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
102 1.31 lukem if (!extra) break; \
103 1.22 christos for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
104 1.22 christos continue; \
105 1.22 christos e--; \
106 1.27 enami if (flag & VIS_SP) *e++ = ' '; \
107 1.22 christos if (flag & VIS_TAB) *e++ = '\t'; \
108 1.27 enami if (flag & VIS_NL) *e++ = '\n'; \
109 1.27 enami if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
110 1.22 christos *e = '\0'; \
111 1.19 mycroft } while (/*CONSTCOND*/0)
112 1.15 wennmach
113 1.22 christos /*
114 1.37 dsl * This is do_hvis, for HTTP style (RFC 1808)
115 1.22 christos */
116 1.37 dsl static char *
117 1.44 christos do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
118 1.37 dsl {
119 1.41 plunky
120 1.41 plunky if ((isascii(c) && isalnum(c))
121 1.41 plunky /* safe */
122 1.41 plunky || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
123 1.41 plunky /* extra */
124 1.41 plunky || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
125 1.41 plunky || c == ',') {
126 1.44 christos dst = do_svis(dst, dlen, c, flag, nextc, extra);
127 1.41 plunky } else {
128 1.44 christos if (dlen) {
129 1.44 christos if (*dlen < 3)
130 1.42 christos return NULL;
131 1.44 christos *dlen -= 3;
132 1.42 christos }
133 1.37 dsl *dst++ = '%';
134 1.37 dsl *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
135 1.37 dsl *dst++ = xtoa((unsigned int)c & 0xf);
136 1.37 dsl }
137 1.41 plunky
138 1.37 dsl return dst;
139 1.37 dsl }
140 1.27 enami
141 1.15 wennmach /*
142 1.39 christos * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
143 1.39 christos * NB: No handling of long lines or CRLF.
144 1.39 christos */
145 1.39 christos static char *
146 1.44 christos do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
147 1.39 christos {
148 1.39 christos if ((c != '\n') &&
149 1.39 christos /* Space at the end of the line */
150 1.39 christos ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
151 1.39 christos /* Out of range */
152 1.39 christos (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
153 1.39 christos /* Specific char to be escaped */
154 1.39 christos strchr("#$@[\\]^`{|}~", c) != NULL)) {
155 1.44 christos if (dlen) {
156 1.44 christos if (*dlen < 3)
157 1.42 christos return NULL;
158 1.44 christos *dlen -= 3;
159 1.42 christos }
160 1.39 christos *dst++ = '=';
161 1.39 christos *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
162 1.39 christos *dst++ = XTOA((unsigned int)c & 0xf);
163 1.39 christos } else {
164 1.44 christos dst = do_svis(dst, dlen, c, flag, nextc, extra);
165 1.39 christos }
166 1.39 christos return dst;
167 1.39 christos }
168 1.39 christos
169 1.39 christos /*
170 1.37 dsl * This is do_vis, the central code of vis.
171 1.16 wennmach * dst: Pointer to the destination buffer
172 1.16 wennmach * c: Character to encode
173 1.15 wennmach * flag: Flag word
174 1.15 wennmach * nextc: The character following 'c'
175 1.15 wennmach * extra: Pointer to the list of extra characters to be
176 1.16 wennmach * backslash-protected.
177 1.15 wennmach */
178 1.37 dsl static char *
179 1.44 christos do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
180 1.37 dsl {
181 1.37 dsl int isextra;
182 1.44 christos size_t odlen = dlen ? *dlen : 0;
183 1.43 christos
184 1.37 dsl isextra = strchr(extra, c) != NULL;
185 1.42 christos #define HAVE(x) \
186 1.42 christos do { \
187 1.44 christos if (dlen) { \
188 1.44 christos if (*dlen < (x)) \
189 1.42 christos goto out; \
190 1.44 christos *dlen -= (x); \
191 1.42 christos } \
192 1.42 christos } while (/*CONSTCOND*/0)
193 1.37 dsl if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
194 1.37 dsl ((flag & VIS_SAFE) && issafe(c)))) {
195 1.42 christos HAVE(1);
196 1.37 dsl *dst++ = c;
197 1.37 dsl return dst;
198 1.37 dsl }
199 1.37 dsl if (flag & VIS_CSTYLE) {
200 1.42 christos HAVE(2);
201 1.37 dsl switch (c) {
202 1.37 dsl case '\n':
203 1.37 dsl *dst++ = '\\'; *dst++ = 'n';
204 1.37 dsl return dst;
205 1.37 dsl case '\r':
206 1.37 dsl *dst++ = '\\'; *dst++ = 'r';
207 1.37 dsl return dst;
208 1.37 dsl case '\b':
209 1.37 dsl *dst++ = '\\'; *dst++ = 'b';
210 1.37 dsl return dst;
211 1.37 dsl case BELL:
212 1.37 dsl *dst++ = '\\'; *dst++ = 'a';
213 1.37 dsl return dst;
214 1.37 dsl case '\v':
215 1.37 dsl *dst++ = '\\'; *dst++ = 'v';
216 1.37 dsl return dst;
217 1.37 dsl case '\t':
218 1.37 dsl *dst++ = '\\'; *dst++ = 't';
219 1.37 dsl return dst;
220 1.37 dsl case '\f':
221 1.37 dsl *dst++ = '\\'; *dst++ = 'f';
222 1.37 dsl return dst;
223 1.37 dsl case ' ':
224 1.37 dsl *dst++ = '\\'; *dst++ = 's';
225 1.37 dsl return dst;
226 1.37 dsl case '\0':
227 1.37 dsl *dst++ = '\\'; *dst++ = '0';
228 1.37 dsl if (isoctal(nextc)) {
229 1.42 christos HAVE(2);
230 1.37 dsl *dst++ = '0';
231 1.37 dsl *dst++ = '0';
232 1.37 dsl }
233 1.37 dsl return dst;
234 1.37 dsl default:
235 1.37 dsl if (isgraph(c)) {
236 1.37 dsl *dst++ = '\\'; *dst++ = c;
237 1.37 dsl return dst;
238 1.37 dsl }
239 1.44 christos if (dlen)
240 1.44 christos *dlen = odlen;
241 1.37 dsl }
242 1.37 dsl }
243 1.37 dsl if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
244 1.42 christos HAVE(4);
245 1.37 dsl *dst++ = '\\';
246 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
247 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
248 1.37 dsl *dst++ = (c & 07) + '0';
249 1.37 dsl } else {
250 1.42 christos if ((flag & VIS_NOSLASH) == 0) {
251 1.42 christos HAVE(1);
252 1.42 christos *dst++ = '\\';
253 1.42 christos }
254 1.42 christos
255 1.37 dsl if (c & 0200) {
256 1.42 christos HAVE(1);
257 1.37 dsl c &= 0177; *dst++ = 'M';
258 1.37 dsl }
259 1.42 christos
260 1.37 dsl if (iscntrl(c)) {
261 1.42 christos HAVE(2);
262 1.37 dsl *dst++ = '^';
263 1.37 dsl if (c == 0177)
264 1.37 dsl *dst++ = '?';
265 1.37 dsl else
266 1.37 dsl *dst++ = c + '@';
267 1.37 dsl } else {
268 1.42 christos HAVE(2);
269 1.37 dsl *dst++ = '-'; *dst++ = c;
270 1.37 dsl }
271 1.37 dsl }
272 1.37 dsl return dst;
273 1.42 christos out:
274 1.44 christos *dlen = odlen;
275 1.42 christos return NULL;
276 1.37 dsl }
277 1.15 wennmach
278 1.42 christos typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
279 1.39 christos
280 1.39 christos /*
281 1.39 christos * Return the appropriate encoding function depending on the flags given.
282 1.39 christos */
283 1.39 christos static visfun_t
284 1.39 christos getvisfun(int flag)
285 1.39 christos {
286 1.39 christos if (flag & VIS_HTTPSTYLE)
287 1.39 christos return do_hvis;
288 1.40 christos if (flag & VIS_MIMESTYLE)
289 1.39 christos return do_mvis;
290 1.39 christos return do_svis;
291 1.39 christos }
292 1.15 wennmach
293 1.15 wennmach /*
294 1.44 christos * isnvis - visually encode characters, also encoding the characters
295 1.33 lukem * pointed to by `extra'
296 1.15 wennmach */
297 1.44 christos static char *
298 1.44 christos isnvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
299 1.15 wennmach {
300 1.31 lukem char *nextra = NULL;
301 1.39 christos visfun_t f;
302 1.31 lukem
303 1.16 wennmach _DIAGASSERT(dst != NULL);
304 1.16 wennmach _DIAGASSERT(extra != NULL);
305 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
306 1.31 lukem if (!nextra) {
307 1.44 christos if (dlen && *dlen == 0) {
308 1.44 christos errno = ENOSPC;
309 1.44 christos return NULL;
310 1.44 christos }
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.44 christos dst = (*f)(dst, dlen, c, flag, nextc, nextra);
316 1.31 lukem free(nextra);
317 1.44 christos if (dst == NULL || (dlen && *dlen == 0)) {
318 1.44 christos errno = ENOSPC;
319 1.44 christos return NULL;
320 1.44 christos }
321 1.16 wennmach *dst = '\0';
322 1.33 lukem return dst;
323 1.15 wennmach }
324 1.15 wennmach
325 1.44 christos char *
326 1.44 christos svis(char *dst, int c, int flag, int nextc, const char *extra)
327 1.44 christos {
328 1.44 christos return isnvis(dst, NULL, c, flag, nextc, extra);
329 1.44 christos }
330 1.44 christos
331 1.44 christos char *
332 1.44 christos snvis(char *dst, size_t dlen, int c, int flag, int nextc, const char *extra)
333 1.44 christos {
334 1.44 christos return isnvis(dst, &dlen, c, flag, nextc, extra);
335 1.44 christos }
336 1.44 christos
337 1.15 wennmach
338 1.15 wennmach /*
339 1.15 wennmach * strsvis, strsvisx - visually encode characters from src into dst
340 1.15 wennmach *
341 1.16 wennmach * Extra is a pointer to a \0-terminated list of characters to
342 1.17 wennmach * be encoded, too. These functions are useful e. g. to
343 1.17 wennmach * encode strings in such a way so that they are not interpreted
344 1.16 wennmach * by a shell.
345 1.27 enami *
346 1.16 wennmach * Dst must be 4 times the size of src to account for possible
347 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
348 1.27 enami * is returned.
349 1.15 wennmach *
350 1.16 wennmach * Strsvisx encodes exactly len bytes from src into dst.
351 1.16 wennmach * This is useful for encoding a block of data.
352 1.15 wennmach */
353 1.44 christos static int
354 1.44 christos istrsnvis(char *dst, size_t *dlen, const char *csrc, int flag, const char *extra)
355 1.15 wennmach {
356 1.25 dsl int c;
357 1.16 wennmach char *start;
358 1.31 lukem char *nextra = NULL;
359 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
360 1.39 christos visfun_t f;
361 1.15 wennmach
362 1.16 wennmach _DIAGASSERT(dst != NULL);
363 1.16 wennmach _DIAGASSERT(src != NULL);
364 1.16 wennmach _DIAGASSERT(extra != NULL);
365 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
366 1.31 lukem if (!nextra) {
367 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
368 1.31 lukem return 0;
369 1.31 lukem }
370 1.39 christos f = getvisfun(flag);
371 1.44 christos for (start = dst; (c = *src++) != '\0'; /* empty */) {
372 1.44 christos dst = (*f)(dst, dlen, c, flag, *src, nextra);
373 1.44 christos if (dst == NULL) {
374 1.44 christos errno = ENOSPC;
375 1.44 christos return -1;
376 1.44 christos }
377 1.44 christos }
378 1.31 lukem free(nextra);
379 1.44 christos if (dlen && *dlen == 0) {
380 1.44 christos errno = ENOSPC;
381 1.44 christos return -1;
382 1.44 christos }
383 1.16 wennmach *dst = '\0';
384 1.39 christos return (int)(dst - start);
385 1.15 wennmach }
386 1.15 wennmach
387 1.44 christos int
388 1.44 christos strsvis(char *dst, const char *csrc, int flag, const char *extra)
389 1.44 christos {
390 1.44 christos return istrsnvis(dst, NULL, csrc, flag, extra);
391 1.44 christos }
392 1.15 wennmach
393 1.15 wennmach int
394 1.44 christos strsnvis(char *dst, size_t dlen, const char *csrc, int flag, const char *extra)
395 1.44 christos {
396 1.44 christos return istrsnvis(dst, &dlen, csrc, flag, extra);
397 1.44 christos }
398 1.44 christos
399 1.44 christos static int
400 1.44 christos istrsnvisx(char *dst, size_t *dlen, const char *csrc, size_t len, int flag,
401 1.44 christos const char *extra)
402 1.15 wennmach {
403 1.28 christos unsigned char c;
404 1.16 wennmach char *start;
405 1.31 lukem char *nextra = NULL;
406 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
407 1.39 christos visfun_t f;
408 1.15 wennmach
409 1.16 wennmach _DIAGASSERT(dst != NULL);
410 1.16 wennmach _DIAGASSERT(src != NULL);
411 1.16 wennmach _DIAGASSERT(extra != NULL);
412 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
413 1.31 lukem if (! nextra) {
414 1.44 christos if (dlen && *dlen == 0) {
415 1.44 christos errno = ENOSPC;
416 1.44 christos return -1;
417 1.44 christos }
418 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
419 1.31 lukem return 0;
420 1.31 lukem }
421 1.16 wennmach
422 1.39 christos f = getvisfun(flag);
423 1.39 christos for (start = dst; len > 0; len--) {
424 1.39 christos c = *src++;
425 1.44 christos dst = (*f)(dst, dlen, c, flag, len > 1 ? *src : '\0', nextra);
426 1.44 christos if (dst == NULL) {
427 1.44 christos errno = ENOSPC;
428 1.44 christos return -1;
429 1.44 christos }
430 1.16 wennmach }
431 1.31 lukem free(nextra);
432 1.44 christos if (dlen && *dlen == 0) {
433 1.44 christos errno = ENOSPC;
434 1.44 christos return -1;
435 1.44 christos }
436 1.16 wennmach *dst = '\0';
437 1.39 christos return (int)(dst - start);
438 1.15 wennmach }
439 1.44 christos
440 1.44 christos int
441 1.44 christos strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
442 1.44 christos {
443 1.44 christos return istrsnvisx(dst, NULL, csrc, len, flag, extra);
444 1.44 christos }
445 1.44 christos
446 1.44 christos int
447 1.44 christos strsnvisx(char *dst, size_t dlen, const char *csrc, size_t len, int flag,
448 1.44 christos const char *extra)
449 1.44 christos {
450 1.44 christos return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
451 1.44 christos }
452 1.24 pooka #endif
453 1.15 wennmach
454 1.24 pooka #if !HAVE_VIS
455 1.1 cgd /*
456 1.1 cgd * vis - visually encode characters
457 1.1 cgd */
458 1.44 christos static char *
459 1.44 christos invis(char *dst, size_t *dlen, int c, int flag, int nextc)
460 1.15 wennmach {
461 1.31 lukem char *extra = NULL;
462 1.28 christos unsigned char uc = (unsigned char)c;
463 1.39 christos visfun_t f;
464 1.15 wennmach
465 1.16 wennmach _DIAGASSERT(dst != NULL);
466 1.15 wennmach
467 1.22 christos MAKEEXTRALIST(flag, extra, "");
468 1.31 lukem if (! extra) {
469 1.44 christos if (dlen && *dlen == 0) {
470 1.44 christos errno = ENOSPC;
471 1.44 christos return NULL;
472 1.44 christos }
473 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
474 1.33 lukem return dst;
475 1.31 lukem }
476 1.39 christos f = getvisfun(flag);
477 1.44 christos dst = (*f)(dst, dlen, uc, flag, nextc, extra);
478 1.32 lukem free(extra);
479 1.44 christos if (dst == NULL || (dlen && *dlen == 0)) {
480 1.44 christos errno = ENOSPC;
481 1.44 christos return NULL;
482 1.44 christos }
483 1.16 wennmach *dst = '\0';
484 1.33 lukem return dst;
485 1.1 cgd }
486 1.1 cgd
487 1.44 christos char *
488 1.44 christos vis(char *dst, int c, int flag, int nextc)
489 1.44 christos {
490 1.44 christos return invis(dst, NULL, c, flag, nextc);
491 1.44 christos }
492 1.44 christos
493 1.44 christos char *
494 1.44 christos nvis(char *dst, size_t dlen, int c, int flag, int nextc)
495 1.44 christos {
496 1.44 christos return invis(dst, &dlen, c, flag, nextc);
497 1.44 christos }
498 1.44 christos
499 1.15 wennmach
500 1.1 cgd /*
501 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
502 1.27 enami *
503 1.16 wennmach * Dst must be 4 times the size of src to account for possible
504 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
505 1.27 enami * is returned.
506 1.1 cgd *
507 1.16 wennmach * Strvisx encodes exactly len bytes from src into dst.
508 1.16 wennmach * This is useful for encoding a block of data.
509 1.1 cgd */
510 1.44 christos static int
511 1.44 christos istrnvis(char *dst, size_t *dlen, const char *src, int flag)
512 1.15 wennmach {
513 1.31 lukem char *extra = NULL;
514 1.31 lukem int rv;
515 1.15 wennmach
516 1.22 christos MAKEEXTRALIST(flag, extra, "");
517 1.31 lukem if (!extra) {
518 1.44 christos if (dlen && *dlen == 0) {
519 1.44 christos errno = ENOSPC;
520 1.44 christos return -1;
521 1.44 christos }
522 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
523 1.31 lukem return 0;
524 1.31 lukem }
525 1.44 christos rv = istrsnvis(dst, dlen, src, flag, extra);
526 1.31 lukem free(extra);
527 1.33 lukem return rv;
528 1.1 cgd }
529 1.1 cgd
530 1.44 christos int
531 1.44 christos strvis(char *dst, const char *src, int flag)
532 1.44 christos {
533 1.44 christos return istrnvis(dst, NULL, src, flag);
534 1.44 christos }
535 1.15 wennmach
536 1.1 cgd int
537 1.44 christos strnvis(char *dst, size_t dlen, const char *src, int flag)
538 1.44 christos {
539 1.44 christos return istrnvis(dst, &dlen, src, flag);
540 1.44 christos }
541 1.44 christos
542 1.44 christos static int
543 1.44 christos istrnvisx(char *dst, size_t *dlen, const char *src, size_t len, int flag)
544 1.15 wennmach {
545 1.31 lukem char *extra = NULL;
546 1.31 lukem int rv;
547 1.1 cgd
548 1.22 christos MAKEEXTRALIST(flag, extra, "");
549 1.31 lukem if (!extra) {
550 1.44 christos if (dlen && *dlen == 0) {
551 1.44 christos errno = ENOSPC;
552 1.44 christos return -1;
553 1.44 christos }
554 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
555 1.31 lukem return 0;
556 1.31 lukem }
557 1.44 christos rv = istrsnvisx(dst, dlen, src, len, flag, extra);
558 1.31 lukem free(extra);
559 1.33 lukem return rv;
560 1.1 cgd }
561 1.44 christos
562 1.44 christos int
563 1.44 christos strvisx(char *dst, const char *src, size_t len, int flag)
564 1.44 christos {
565 1.44 christos return istrnvisx(dst, NULL, src, len, flag);
566 1.44 christos }
567 1.44 christos
568 1.44 christos int
569 1.44 christos strnvisx(char *dst, size_t dlen, const char *src, size_t len, int flag)
570 1.44 christos {
571 1.44 christos return istrnvisx(dst, &dlen, src, len, flag);
572 1.44 christos }
573 1.44 christos
574 1.20 tv #endif
575