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