1 /* $NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 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.62 2014/09/08 17:35:01 christos Exp $");
61 #endif /* LIBC_SCCS and not lint */
62 #ifdef __FBSDID
63 __FBSDID("$FreeBSD$");
64 #define _DIAGASSERT(x) assert(x)
65 #endif
66
67 #include "namespace.h"
68 #include <sys/types.h>
69 #include <sys/param.h>
70
71 #include <assert.h>
72 #include <vis.h>
73 #include <errno.h>
74 #include <stdlib.h>
75 #include <wchar.h>
76 #include <wctype.h>
77
78 #ifdef __weak_alias
79 __weak_alias(strvisx,_strvisx)
80 #endif
81
82 #if !HAVE_VIS || !HAVE_SVIS
83 #include <ctype.h>
84 #include <limits.h>
85 #include <stdio.h>
86 #include <string.h>
87
88 /*
89 * The reason for going through the trouble to deal with character encodings
90 * in vis(3), is that we use this to safe encode output of commands. This
91 * safe encoding varies depending on the character set. For example if we
92 * display ps output in French, we don't want to display French characters
93 * as M-foo.
94 */
95
96 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
97
98 #undef BELL
99 #define BELL L'\a'
100
101 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
102 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
103 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
104 #define xtoa(c) L"0123456789abcdef"[c]
105 #define XTOA(c) L"0123456789ABCDEF"[c]
106
107 #define MAXEXTRAS 10
108
109 #if !HAVE_NBTOOL_CONFIG_H
110 #ifndef __NetBSD__
111 /*
112 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
113 * integral type and it is probably wrong, since currently the maximum
114 * number of bytes and character needs is 6. Until this is fixed, the
115 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
116 * the assertion is commented out.
117 */
118 #ifdef __FreeBSD__
119 /*
120 * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
121 * mode.
122 */
123 #ifndef CTASSERT
124 #define CTASSERT(x) _CTASSERT(x, __LINE__)
125 #define _CTASSERT(x, y) __CTASSERT(x, y)
126 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
127 #endif
128 #endif /* __FreeBSD__ */
129 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
130 #endif /* !__NetBSD__ */
131 #endif
132
133 /*
134 * This is do_hvis, for HTTP style (RFC 1808)
135 */
136 static wchar_t *
do_hvis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)137 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
138 {
139 if (iswalnum(c)
140 /* safe */
141 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
142 /* extra */
143 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
144 || c == L',')
145 dst = do_svis(dst, c, flags, nextc, extra);
146 else {
147 *dst++ = L'%';
148 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
149 *dst++ = xtoa((unsigned int)c & 0xf);
150 }
151
152 return dst;
153 }
154
155 /*
156 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
157 * NB: No handling of long lines or CRLF.
158 */
159 static wchar_t *
do_mvis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)160 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
161 {
162 if ((c != L'\n') &&
163 /* Space at the end of the line */
164 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
165 /* Out of range */
166 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
167 /* Specific char to be escaped */
168 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
169 *dst++ = L'=';
170 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
171 *dst++ = XTOA((unsigned int)c & 0xf);
172 } else
173 dst = do_svis(dst, c, flags, nextc, extra);
174 return dst;
175 }
176
177 /*
178 * Output single byte of multibyte character.
179 */
180 static wchar_t *
do_mbyte(wchar_t * dst,wint_t c,int flags,wint_t nextc,int iswextra)181 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
182 {
183 if (flags & VIS_CSTYLE) {
184 switch (c) {
185 case L'\n':
186 *dst++ = L'\\'; *dst++ = L'n';
187 return dst;
188 case L'\r':
189 *dst++ = L'\\'; *dst++ = L'r';
190 return dst;
191 case L'\b':
192 *dst++ = L'\\'; *dst++ = L'b';
193 return dst;
194 case BELL:
195 *dst++ = L'\\'; *dst++ = L'a';
196 return dst;
197 case L'\v':
198 *dst++ = L'\\'; *dst++ = L'v';
199 return dst;
200 case L'\t':
201 *dst++ = L'\\'; *dst++ = L't';
202 return dst;
203 case L'\f':
204 *dst++ = L'\\'; *dst++ = L'f';
205 return dst;
206 case L' ':
207 *dst++ = L'\\'; *dst++ = L's';
208 return dst;
209 case L'\0':
210 *dst++ = L'\\'; *dst++ = L'0';
211 if (iswoctal(nextc)) {
212 *dst++ = L'0';
213 *dst++ = L'0';
214 }
215 return dst;
216 default:
217 if (iswgraph(c)) {
218 *dst++ = L'\\';
219 *dst++ = c;
220 return dst;
221 }
222 }
223 }
224 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
225 *dst++ = L'\\';
226 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
227 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
228 *dst++ = (c & 07) + L'0';
229 } else {
230 if ((flags & VIS_NOSLASH) == 0)
231 *dst++ = L'\\';
232
233 if (c & 0200) {
234 c &= 0177;
235 *dst++ = L'M';
236 }
237
238 if (iswcntrl(c)) {
239 *dst++ = L'^';
240 if (c == 0177)
241 *dst++ = L'?';
242 else
243 *dst++ = c + L'@';
244 } else {
245 *dst++ = L'-';
246 *dst++ = c;
247 }
248 }
249
250 return dst;
251 }
252
253 /*
254 * This is do_vis, the central code of vis.
255 * dst: Pointer to the destination buffer
256 * c: Character to encode
257 * flags: Flags word
258 * nextc: The character following 'c'
259 * extra: Pointer to the list of extra characters to be
260 * backslash-protected.
261 */
262 static wchar_t *
do_svis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)263 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
264 {
265 int iswextra, i, shft;
266 uint64_t bmsk, wmsk;
267
268 iswextra = wcschr(extra, c) != NULL;
269 if (!iswextra && (iswgraph(c) || iswwhite(c) ||
270 ((flags & VIS_SAFE) && iswsafe(c)))) {
271 *dst++ = c;
272 return dst;
273 }
274
275 /* See comment in istrsenvisx() output loop, below. */
276 wmsk = 0;
277 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
278 shft = i * NBBY;
279 bmsk = (uint64_t)0xffLL << shft;
280 wmsk |= bmsk;
281 if ((c & wmsk) || i == 0)
282 dst = do_mbyte(dst, (wint_t)(
283 (uint64_t)(c & bmsk) >> shft),
284 flags, nextc, iswextra);
285 }
286
287 return dst;
288 }
289
290 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
291
292 /*
293 * Return the appropriate encoding function depending on the flags given.
294 */
295 static visfun_t
getvisfun(int flags)296 getvisfun(int flags)
297 {
298 if (flags & VIS_HTTPSTYLE)
299 return do_hvis;
300 if (flags & VIS_MIMESTYLE)
301 return do_mvis;
302 return do_svis;
303 }
304
305 /*
306 * Expand list of extra characters to not visually encode.
307 */
308 static wchar_t *
makeextralist(int flags,const char * src)309 makeextralist(int flags, const char *src)
310 {
311 wchar_t *dst, *d;
312 size_t len;
313
314 len = strlen(src);
315 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
316 return NULL;
317
318 if (mbstowcs(dst, src, len) == (size_t)-1) {
319 size_t i;
320 for (i = 0; i < len; i++)
321 dst[i] = (wint_t)(u_char)src[i];
322 d = dst + len;
323 } else
324 d = dst + wcslen(dst);
325
326 if (flags & VIS_GLOB) {
327 *d++ = L'*';
328 *d++ = L'?';
329 *d++ = L'[';
330 *d++ = L'#';
331 }
332
333 if (flags & VIS_SP) *d++ = L' ';
334 if (flags & VIS_TAB) *d++ = L'\t';
335 if (flags & VIS_NL) *d++ = L'\n';
336 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
337 *d = L'\0';
338
339 return dst;
340 }
341
342 /*
343 * istrsenvisx()
344 * The main internal function.
345 * All user-visible functions call this one.
346 */
347 static int
istrsenvisx(char * mbdst,size_t * dlen,const char * mbsrc,size_t mblength,int flags,const char * mbextra,int * cerr_ptr)348 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
349 int flags, const char *mbextra, int *cerr_ptr)
350 {
351 wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
352 size_t len, olen;
353 uint64_t bmsk, wmsk;
354 wint_t c;
355 visfun_t f;
356 int clen = 0, cerr = 0, error = -1, i, shft;
357 ssize_t mbslength, maxolen;
358
359 _DIAGASSERT(mbdst != NULL);
360 _DIAGASSERT(mbsrc != NULL || mblength == 0);
361 _DIAGASSERT(mbextra != NULL);
362
363 /*
364 * Input (mbsrc) is a char string considered to be multibyte
365 * characters. The input loop will read this string pulling
366 * one character, possibly multiple bytes, from mbsrc and
367 * converting each to wchar_t in src.
368 *
369 * The vis conversion will be done using the wide char
370 * wchar_t string.
371 *
372 * This will then be converted back to a multibyte string to
373 * return to the caller.
374 */
375
376 /* Allocate space for the wide char strings */
377 psrc = pdst = extra = NULL;
378 if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
379 return -1;
380 if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
381 goto out;
382 dst = pdst;
383 src = psrc;
384
385 /* Use caller's multibyte conversion error flag. */
386 if (cerr_ptr)
387 cerr = *cerr_ptr;
388
389 /*
390 * Input loop.
391 * Handle up to mblength characters (not bytes). We do not
392 * stop at NULs because we may be processing a block of data
393 * that includes NULs.
394 */
395 mbslength = (ssize_t)mblength;
396 /*
397 * When inputing a single character, must also read in the
398 * next character for nextc, the look-ahead character.
399 */
400 if (mbslength == 1)
401 mbslength++;
402 while (mbslength > 0) {
403 /* Convert one multibyte character to wchar_t. */
404 if (!cerr)
405 clen = mbtowc(src, mbsrc, MB_LEN_MAX);
406 if (cerr || clen < 0) {
407 /* Conversion error, process as a byte instead. */
408 *src = (wint_t)(u_char)*mbsrc;
409 clen = 1;
410 cerr = 1;
411 }
412 if (clen == 0)
413 /*
414 * NUL in input gives 0 return value. process
415 * as single NUL byte and keep going.
416 */
417 clen = 1;
418 /* Advance buffer character pointer. */
419 src++;
420 /* Advance input pointer by number of bytes read. */
421 mbsrc += clen;
422 /* Decrement input byte count. */
423 mbslength -= clen;
424 }
425 len = src - psrc;
426 src = psrc;
427 /*
428 * In the single character input case, we will have actually
429 * processed two characters, c and nextc. Reset len back to
430 * just a single character.
431 */
432 if (mblength < len)
433 len = mblength;
434
435 /* Convert extra argument to list of characters for this mode. */
436 extra = makeextralist(flags, mbextra);
437 if (!extra) {
438 if (dlen && *dlen == 0) {
439 errno = ENOSPC;
440 goto out;
441 }
442 *mbdst = '\0'; /* can't create extra, return "" */
443 error = 0;
444 goto out;
445 }
446
447 /* Look up which processing function to call. */
448 f = getvisfun(flags);
449
450 /*
451 * Main processing loop.
452 * Call do_Xvis processing function one character at a time
453 * with next character available for look-ahead.
454 */
455 for (start = dst; len > 0; len--) {
456 c = *src++;
457 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
458 if (dst == NULL) {
459 errno = ENOSPC;
460 goto out;
461 }
462 }
463
464 /* Terminate the string in the buffer. */
465 *dst = L'\0';
466
467 /*
468 * Output loop.
469 * Convert wchar_t string back to multibyte output string.
470 * If we have hit a multi-byte conversion error on input,
471 * output byte-by-byte here. Else use wctomb().
472 */
473 len = wcslen(start);
474 maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
475 olen = 0;
476 for (dst = start; len > 0; len--) {
477 if (!cerr)
478 clen = wctomb(mbdst, *dst);
479 if (cerr || clen < 0) {
480 /*
481 * Conversion error, process as a byte(s) instead.
482 * Examine each byte and higher-order bytes for
483 * data. E.g.,
484 * 0x000000000000a264 -> a2 64
485 * 0x000000001f00a264 -> 1f 00 a2 64
486 */
487 clen = 0;
488 wmsk = 0;
489 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
490 shft = i * NBBY;
491 bmsk = (uint64_t)0xffLL << shft;
492 wmsk |= bmsk;
493 if ((*dst & wmsk) || i == 0)
494 mbdst[clen++] = (char)(
495 (uint64_t)(*dst & bmsk) >>
496 shft);
497 }
498 cerr = 1;
499 }
500 /* If this character would exceed our output limit, stop. */
501 if (olen + clen > (size_t)maxolen)
502 break;
503 /* Advance output pointer by number of bytes written. */
504 mbdst += clen;
505 /* Advance buffer character pointer. */
506 dst++;
507 /* Incrment output character count. */
508 olen += clen;
509 }
510
511 /* Terminate the output string. */
512 *mbdst = '\0';
513
514 /* Pass conversion error flag out. */
515 if (cerr_ptr)
516 *cerr_ptr = cerr;
517
518 free(extra);
519 free(pdst);
520 free(psrc);
521
522 return (int)olen;
523 out:
524 free(extra);
525 free(pdst);
526 free(psrc);
527 return error;
528 }
529
530 static int
istrsenvisxl(char * mbdst,size_t * dlen,const char * mbsrc,int flags,const char * mbextra,int * cerr_ptr)531 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
532 int flags, const char *mbextra, int *cerr_ptr)
533 {
534 return istrsenvisx(mbdst, dlen, mbsrc,
535 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
536 }
537
538 #endif
539
540 #if !HAVE_SVIS
541 /*
542 * The "svis" variants all take an "extra" arg that is a pointer
543 * to a NUL-terminated list of characters to be encoded, too.
544 * These functions are useful e. g. to encode strings in such a
545 * way so that they are not interpreted by a shell.
546 */
547
548 char *
svis(char * mbdst,int c,int flags,int nextc,const char * mbextra)549 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
550 {
551 char cc[2];
552 int ret;
553
554 cc[0] = c;
555 cc[1] = nextc;
556
557 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
558 if (ret < 0)
559 return NULL;
560 return mbdst + ret;
561 }
562
563 char *
snvis(char * mbdst,size_t dlen,int c,int flags,int nextc,const char * mbextra)564 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
565 {
566 char cc[2];
567 int ret;
568
569 cc[0] = c;
570 cc[1] = nextc;
571
572 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
573 if (ret < 0)
574 return NULL;
575 return mbdst + ret;
576 }
577
578 int
strsvis(char * mbdst,const char * mbsrc,int flags,const char * mbextra)579 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
580 {
581 return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
582 }
583
584 int
strsnvis(char * mbdst,size_t dlen,const char * mbsrc,int flags,const char * mbextra)585 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
586 {
587 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
588 }
589
590 int
strsvisx(char * mbdst,const char * mbsrc,size_t len,int flags,const char * mbextra)591 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
592 {
593 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
594 }
595
596 int
strsnvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,const char * mbextra)597 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
598 const char *mbextra)
599 {
600 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
601 }
602
603 int
strsenvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,const char * mbextra,int * cerr_ptr)604 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
605 const char *mbextra, int *cerr_ptr)
606 {
607 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
608 }
609 #endif
610
611 #if !HAVE_VIS
612 /*
613 * vis - visually encode characters
614 */
615 char *
vis(char * mbdst,int c,int flags,int nextc)616 vis(char *mbdst, int c, int flags, int nextc)
617 {
618 char cc[2];
619 int ret;
620
621 cc[0] = c;
622 cc[1] = nextc;
623
624 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
625 if (ret < 0)
626 return NULL;
627 return mbdst + ret;
628 }
629
630 char *
nvis(char * mbdst,size_t dlen,int c,int flags,int nextc)631 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
632 {
633 char cc[2];
634 int ret;
635
636 cc[0] = c;
637 cc[1] = nextc;
638
639 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
640 if (ret < 0)
641 return NULL;
642 return mbdst + ret;
643 }
644
645 /*
646 * strvis - visually encode characters from src into dst
647 *
648 * Dst must be 4 times the size of src to account for possible
649 * expansion. The length of dst, not including the trailing NULL,
650 * is returned.
651 */
652
653 int
strvis(char * mbdst,const char * mbsrc,int flags)654 strvis(char *mbdst, const char *mbsrc, int flags)
655 {
656 return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
657 }
658
659 int
strnvis(char * mbdst,size_t dlen,const char * mbsrc,int flags)660 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
661 {
662 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
663 }
664
665 /*
666 * strvisx - visually encode characters from src into dst
667 *
668 * Dst must be 4 times the size of src to account for possible
669 * expansion. The length of dst, not including the trailing NULL,
670 * is returned.
671 *
672 * Strvisx encodes exactly len characters from src into dst.
673 * This is useful for encoding a block of data.
674 */
675
676 int
strvisx(char * mbdst,const char * mbsrc,size_t len,int flags)677 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
678 {
679 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
680 }
681
682 int
strnvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags)683 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
684 {
685 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
686 }
687
688 int
strenvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,int * cerr_ptr)689 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
690 int *cerr_ptr)
691 {
692 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
693 }
694 #endif
695