1 /* $NetBSD: vis.c,v 1.83 2023/08/12 12:48:52 riastradh 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.83 2023/08/12 12:48:52 riastradh 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 <stdint.h> 75 #include <stdlib.h> 76 #include <wchar.h> 77 #include <wctype.h> 78 79 #ifdef __weak_alias 80 __weak_alias(strvisx,_strvisx) 81 #endif 82 83 #if !HAVE_VIS || !HAVE_SVIS 84 #include <ctype.h> 85 #include <limits.h> 86 #include <stdio.h> 87 #include <string.h> 88 89 /* 90 * The reason for going through the trouble to deal with character encodings 91 * in vis(3), is that we use this to safe encode output of commands. This 92 * safe encoding varies depending on the character set. For example if we 93 * display ps output in French, we don't want to display French characters 94 * as M-foo. 95 */ 96 97 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *); 98 99 #undef BELL 100 #define BELL L'\a' 101 102 #if defined(LC_C_LOCALE) 103 #define iscgraph(c) isgraph_l(c, LC_C_LOCALE) 104 #else 105 /* Keep it simple for now, no locale stuff */ 106 #define iscgraph(c) isgraph(c) 107 #ifdef notyet 108 #include <locale.h> 109 static int 110 iscgraph(int c) { 111 int rv; 112 char *ol; 113 114 ol = setlocale(LC_CTYPE, "C"); 115 rv = isgraph(c); 116 if (ol) 117 setlocale(LC_CTYPE, ol); 118 return rv; 119 } 120 #endif 121 #endif 122 123 #define ISGRAPH(flags, c) \ 124 (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c)) 125 126 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7') 127 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n') 128 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r') 129 #define xtoa(c) L"0123456789abcdef"[c] 130 #define XTOA(c) L"0123456789ABCDEF"[c] 131 132 #define MAXEXTRAS 30 133 134 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~"; 135 static const wchar_t char_glob[] = L"*?[#"; 136 137 #if !HAVE_NBTOOL_CONFIG_H 138 #ifndef __NetBSD__ 139 /* 140 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer 141 * integral type and it is probably wrong, since currently the maximum 142 * number of bytes and character needs is 6. Until this is fixed, the 143 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and 144 * the assertion is commented out. 145 */ 146 #ifdef __FreeBSD__ 147 /* 148 * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel 149 * mode. 150 */ 151 #ifndef CTASSERT 152 #define CTASSERT(x) _CTASSERT(x, __LINE__) 153 #define _CTASSERT(x, y) __CTASSERT(x, y) 154 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1] 155 #endif 156 #endif /* __FreeBSD__ */ 157 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t)); 158 #endif /* !__NetBSD__ */ 159 #endif 160 161 /* 162 * This is do_hvis, for HTTP style (RFC 1808) 163 */ 164 static wchar_t * 165 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) 166 { 167 if (iswalnum(c) 168 /* safe */ 169 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+' 170 /* extra */ 171 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')' 172 || c == L',') 173 dst = do_svis(dst, c, flags, nextc, extra); 174 else { 175 *dst++ = L'%'; 176 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); 177 *dst++ = xtoa((unsigned int)c & 0xf); 178 } 179 180 return dst; 181 } 182 183 /* 184 * This is do_mvis, for Quoted-Printable MIME (RFC 2045) 185 * NB: No handling of long lines or CRLF. 186 */ 187 static wchar_t * 188 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) 189 { 190 if ((c != L'\n') && 191 /* Space at the end of the line */ 192 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) || 193 /* Out of range */ 194 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) || 195 /* Specific char to be escaped */ 196 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) { 197 *dst++ = L'='; 198 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf); 199 *dst++ = XTOA((unsigned int)c & 0xf); 200 } else 201 dst = do_svis(dst, c, flags, nextc, extra); 202 return dst; 203 } 204 205 /* 206 * Output single byte of multibyte character. 207 */ 208 static wchar_t * 209 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra) 210 { 211 if (flags & VIS_CSTYLE) { 212 switch (c) { 213 case L'\n': 214 *dst++ = L'\\'; *dst++ = L'n'; 215 return dst; 216 case L'\r': 217 *dst++ = L'\\'; *dst++ = L'r'; 218 return dst; 219 case L'\b': 220 *dst++ = L'\\'; *dst++ = L'b'; 221 return dst; 222 case BELL: 223 *dst++ = L'\\'; *dst++ = L'a'; 224 return dst; 225 case L'\v': 226 *dst++ = L'\\'; *dst++ = L'v'; 227 return dst; 228 case L'\t': 229 *dst++ = L'\\'; *dst++ = L't'; 230 return dst; 231 case L'\f': 232 *dst++ = L'\\'; *dst++ = L'f'; 233 return dst; 234 case L' ': 235 *dst++ = L'\\'; *dst++ = L's'; 236 return dst; 237 case L'\0': 238 *dst++ = L'\\'; *dst++ = L'0'; 239 if (iswoctal(nextc)) { 240 *dst++ = L'0'; 241 *dst++ = L'0'; 242 } 243 return dst; 244 /* We cannot encode these characters in VIS_CSTYLE 245 * because they special meaning */ 246 case L'n': 247 case L'r': 248 case L'b': 249 case L'a': 250 case L'v': 251 case L't': 252 case L'f': 253 case L's': 254 case L'0': 255 case L'M': 256 case L'^': 257 case L'$': /* vis(1) -l */ 258 break; 259 default: 260 if (ISGRAPH(flags, c) && !iswoctal(c)) { 261 *dst++ = L'\\'; 262 *dst++ = c; 263 return dst; 264 } 265 } 266 } 267 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) { 268 *dst++ = L'\\'; 269 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0'; 270 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0'; 271 *dst++ = (c & 07) + L'0'; 272 } else { 273 if ((flags & VIS_NOSLASH) == 0) 274 *dst++ = L'\\'; 275 276 if (c & 0200) { 277 c &= 0177; 278 *dst++ = L'M'; 279 } 280 281 if (iswcntrl(c)) { 282 *dst++ = L'^'; 283 if (c == 0177) 284 *dst++ = L'?'; 285 else 286 *dst++ = c + L'@'; 287 } else { 288 *dst++ = L'-'; 289 *dst++ = c; 290 } 291 } 292 293 return dst; 294 } 295 296 /* 297 * This is do_vis, the central code of vis. 298 * dst: Pointer to the destination buffer 299 * c: Character to encode 300 * flags: Flags word 301 * nextc: The character following 'c' 302 * extra: Pointer to the list of extra characters to be 303 * backslash-protected. 304 */ 305 static wchar_t * 306 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) 307 { 308 int iswextra, i, shft; 309 uint64_t bmsk, wmsk; 310 311 iswextra = wcschr(extra, c) != NULL; 312 if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) || 313 ((flags & VIS_SAFE) && iswsafe(c)))) { 314 *dst++ = c; 315 return dst; 316 } 317 318 /* See comment in istrsenvisx() output loop, below. */ 319 wmsk = 0; 320 for (i = sizeof(wmsk) - 1; i >= 0; i--) { 321 shft = i * NBBY; 322 bmsk = (uint64_t)0xffLL << shft; 323 wmsk |= bmsk; 324 if ((c & wmsk) || i == 0) 325 dst = do_mbyte(dst, (wint_t)( 326 (uint64_t)(c & bmsk) >> shft), 327 flags, nextc, iswextra); 328 } 329 330 return dst; 331 } 332 333 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *); 334 335 /* 336 * Return the appropriate encoding function depending on the flags given. 337 */ 338 static visfun_t 339 getvisfun(int flags) 340 { 341 if (flags & VIS_HTTPSTYLE) 342 return do_hvis; 343 if (flags & VIS_MIMESTYLE) 344 return do_mvis; 345 return do_svis; 346 } 347 348 /* 349 * Expand list of extra characters to not visually encode. 350 */ 351 static wchar_t * 352 makeextralist(int flags, const char *src) 353 { 354 wchar_t *dst, *d; 355 size_t len; 356 const wchar_t *s; 357 mbstate_t mbstate; 358 359 bzero(&mbstate, sizeof(mbstate)); 360 len = strlen(src); 361 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL) 362 return NULL; 363 364 memset(&mbstate, 0, sizeof(mbstate)); 365 if ((flags & VIS_NOLOCALE) 366 || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) { 367 size_t i; 368 for (i = 0; i < len; i++) 369 dst[i] = (wchar_t)(u_char)src[i]; 370 d = dst + len; 371 } else 372 d = dst + wcslen(dst); 373 374 if (flags & VIS_GLOB) 375 for (s = char_glob; *s; *d++ = *s++) 376 continue; 377 378 if (flags & VIS_SHELL) 379 for (s = char_shell; *s; *d++ = *s++) 380 continue; 381 382 if (flags & VIS_SP) *d++ = L' '; 383 if (flags & VIS_TAB) *d++ = L'\t'; 384 if (flags & VIS_NL) *d++ = L'\n'; 385 if (flags & VIS_DQ) *d++ = L'"'; 386 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\'; 387 *d = L'\0'; 388 389 return dst; 390 } 391 392 /* 393 * istrsenvisx() 394 * The main internal function. 395 * All user-visible functions call this one. 396 */ 397 static int 398 istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength, 399 int flags, const char *mbextra, int *cerr_ptr) 400 { 401 char mbbuf[MB_LEN_MAX]; 402 wchar_t *dst, *src, *pdst, *psrc, *start, *extra; 403 size_t len, olen; 404 uint64_t bmsk, wmsk; 405 wint_t c; 406 visfun_t f; 407 int clen = 0, cerr, error = -1, i, shft; 408 char *mbdst, *mbwrite, *mdst; 409 size_t mbslength; 410 size_t maxolen; 411 mbstate_t mbstate; 412 413 _DIAGASSERT(mbdstp != NULL); 414 _DIAGASSERT(mbsrc != NULL || mblength == 0); 415 _DIAGASSERT(mbextra != NULL); 416 417 mbslength = mblength; 418 /* 419 * When inputing a single character, must also read in the 420 * next character for nextc, the look-ahead character. 421 */ 422 if (mbslength == 1) 423 mbslength++; 424 425 /* 426 * Input (mbsrc) is a char string considered to be multibyte 427 * characters. The input loop will read this string pulling 428 * one character, possibly multiple bytes, from mbsrc and 429 * converting each to wchar_t in src. 430 * 431 * The vis conversion will be done using the wide char 432 * wchar_t string. 433 * 434 * This will then be converted back to a multibyte string to 435 * return to the caller. 436 */ 437 438 /* 439 * Guarantee the arithmetic on input to calloc won't overflow. 440 */ 441 if (mbslength > (SIZE_MAX - 1)/16) { 442 errno = ENOMEM; 443 return -1; 444 } 445 446 /* Allocate space for the wide char strings */ 447 psrc = pdst = extra = NULL; 448 mdst = NULL; 449 if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL) 450 return -1; 451 if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL) 452 goto out; 453 if (*mbdstp == NULL) { 454 if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL) 455 goto out; 456 *mbdstp = mdst; 457 } 458 459 mbdst = *mbdstp; 460 dst = pdst; 461 src = psrc; 462 463 if (flags & VIS_NOLOCALE) { 464 /* Do one byte at a time conversion */ 465 cerr = 1; 466 } else { 467 /* Use caller's multibyte conversion error flag. */ 468 cerr = cerr_ptr ? *cerr_ptr : 0; 469 } 470 471 /* 472 * Input loop. 473 * Handle up to mblength characters (not bytes). We do not 474 * stop at NULs because we may be processing a block of data 475 * that includes NULs. 476 */ 477 memset(&mbstate, 0, sizeof(mbstate)); 478 while (mbslength > 0) { 479 /* Convert one multibyte character to wchar_t. */ 480 if (!cerr) { 481 clen = mbrtowc(src, mbsrc, 482 (mbslength < MB_LEN_MAX 483 ? mbslength 484 : MB_LEN_MAX), 485 &mbstate); 486 assert(clen < 0 || (size_t)clen <= mbslength); 487 assert(clen <= MB_LEN_MAX); 488 } 489 if (cerr || clen < 0) { 490 /* Conversion error, process as a byte instead. */ 491 *src = (wint_t)(u_char)*mbsrc; 492 clen = 1; 493 cerr = 1; 494 } 495 if (clen == 0) { 496 /* 497 * NUL in input gives 0 return value. process 498 * as single NUL byte and keep going. 499 */ 500 clen = 1; 501 } 502 /* 503 * Let n := MIN(mbslength, MB_LEN_MAX). We have: 504 * 505 * mbslength >= 1 506 * mbrtowc(..., n, &mbstate) <= n, 507 * by the contract of mbrtowc 508 * 509 * clen is either 510 * (a) mbrtowc(..., n, &mbstate), in which case 511 * clen <= n <= mbslength; or 512 * (b) 1, in which case clen = 1 <= mbslength. 513 */ 514 assert(clen > 0); 515 assert((size_t)clen <= mbslength); 516 /* Advance buffer character pointer. */ 517 src++; 518 /* Advance input pointer by number of bytes read. */ 519 mbsrc += clen; 520 /* Decrement input byte count. */ 521 mbslength -= clen; 522 } 523 len = src - psrc; 524 src = psrc; 525 526 /* 527 * In the single character input case, we will have actually 528 * processed two characters, c and nextc. Reset len back to 529 * just a single character. 530 */ 531 if (mblength < len) 532 len = mblength; 533 534 /* Convert extra argument to list of characters for this mode. */ 535 extra = makeextralist(flags, mbextra); 536 if (!extra) { 537 if (dlen && *dlen == 0) { 538 errno = ENOSPC; 539 goto out; 540 } 541 *mbdst = '\0'; /* can't create extra, return "" */ 542 error = 0; 543 goto out; 544 } 545 546 /* Look up which processing function to call. */ 547 f = getvisfun(flags); 548 549 /* 550 * Main processing loop. 551 * Call do_Xvis processing function one character at a time 552 * with next character available for look-ahead. 553 */ 554 for (start = dst; len > 0; len--) { 555 c = *src++; 556 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra); 557 if (dst == NULL) { 558 errno = ENOSPC; 559 goto out; 560 } 561 } 562 563 /* Terminate the string in the buffer. */ 564 *dst = L'\0'; 565 566 /* 567 * Output loop. 568 * Convert wchar_t string back to multibyte output string. 569 * If we have hit a multi-byte conversion error on input, 570 * output byte-by-byte here. Else use wctomb(). 571 */ 572 len = wcslen(start); 573 if (dlen) { 574 maxolen = *dlen; 575 if (maxolen == 0) { 576 errno = ENOSPC; 577 goto out; 578 } 579 } else { 580 if (len > (SIZE_MAX - 1)/MB_LEN_MAX) { 581 errno = ENOSPC; 582 goto out; 583 } 584 maxolen = len*MB_LEN_MAX + 1; 585 } 586 olen = 0; 587 memset(&mbstate, 0, sizeof(mbstate)); 588 for (dst = start; len > 0; len--) { 589 if (!cerr) { 590 /* 591 * If we have at least MB_CUR_MAX bytes in the buffer, 592 * we'll just do the conversion in-place into mbdst. We 593 * need to be a little more conservative when we get to 594 * the end of the buffer, as we may not have MB_CUR_MAX 595 * bytes but we may not need it. 596 */ 597 if (maxolen - olen > MB_CUR_MAX) 598 mbwrite = mbdst; 599 else 600 mbwrite = mbbuf; 601 clen = wcrtomb(mbwrite, *dst, &mbstate); 602 if (clen > 0 && mbwrite != mbdst) { 603 /* 604 * Don't break past our output limit, noting 605 * that maxolen includes the nul terminator so 606 * we can't write past maxolen - 1 here. 607 */ 608 if (olen + clen >= maxolen) { 609 errno = ENOSPC; 610 goto out; 611 } 612 613 memcpy(mbdst, mbwrite, clen); 614 } 615 } 616 if (cerr || clen < 0) { 617 /* 618 * Conversion error, process as a byte(s) instead. 619 * Examine each byte and higher-order bytes for 620 * data. E.g., 621 * 0x000000000000a264 -> a2 64 622 * 0x000000001f00a264 -> 1f 00 a2 64 623 */ 624 clen = 0; 625 wmsk = 0; 626 for (i = sizeof(wmsk) - 1; i >= 0; i--) { 627 shft = i * NBBY; 628 bmsk = (uint64_t)0xffLL << shft; 629 wmsk |= bmsk; 630 if ((*dst & wmsk) || i == 0) { 631 if (olen + clen + 1 >= maxolen) { 632 errno = ENOSPC; 633 goto out; 634 } 635 636 mbdst[clen++] = (char)( 637 (uint64_t)(*dst & bmsk) >> 638 shft); 639 } 640 } 641 cerr = 1; 642 } 643 644 /* 645 * We'll be dereferencing mbdst[clen] after this to write the 646 * nul terminator; the above paths should have checked for a 647 * possible overflow already. 648 */ 649 assert(olen + clen < maxolen); 650 651 /* Advance output pointer by number of bytes written. */ 652 mbdst += clen; 653 /* Advance buffer character pointer. */ 654 dst++; 655 /* Incrment output character count. */ 656 olen += clen; 657 } 658 659 /* Terminate the output string. */ 660 assert(olen < maxolen); 661 *mbdst = '\0'; 662 663 if (flags & VIS_NOLOCALE) { 664 /* Pass conversion error flag out. */ 665 if (cerr_ptr) 666 *cerr_ptr = cerr; 667 } 668 669 free(extra); 670 free(pdst); 671 free(psrc); 672 673 return (int)olen; 674 out: 675 free(extra); 676 free(pdst); 677 free(psrc); 678 free(mdst); 679 return error; 680 } 681 682 static int 683 istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc, 684 int flags, const char *mbextra, int *cerr_ptr) 685 { 686 return istrsenvisx(mbdstp, dlen, mbsrc, 687 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr); 688 } 689 690 #endif 691 692 #if !HAVE_SVIS 693 /* 694 * The "svis" variants all take an "extra" arg that is a pointer 695 * to a NUL-terminated list of characters to be encoded, too. 696 * These functions are useful e. g. to encode strings in such a 697 * way so that they are not interpreted by a shell. 698 */ 699 700 char * 701 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra) 702 { 703 char cc[2]; 704 int ret; 705 706 cc[0] = c; 707 cc[1] = nextc; 708 709 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL); 710 if (ret < 0) 711 return NULL; 712 return mbdst + ret; 713 } 714 715 char * 716 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra) 717 { 718 char cc[2]; 719 int ret; 720 721 cc[0] = c; 722 cc[1] = nextc; 723 724 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL); 725 if (ret < 0) 726 return NULL; 727 return mbdst + ret; 728 } 729 730 int 731 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra) 732 { 733 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL); 734 } 735 736 int 737 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra) 738 { 739 return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL); 740 } 741 742 int 743 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra) 744 { 745 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL); 746 } 747 748 int 749 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, 750 const char *mbextra) 751 { 752 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL); 753 } 754 755 int 756 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, 757 const char *mbextra, int *cerr_ptr) 758 { 759 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr); 760 } 761 #endif 762 763 #if !HAVE_VIS 764 /* 765 * vis - visually encode characters 766 */ 767 char * 768 vis(char *mbdst, int c, int flags, int nextc) 769 { 770 char cc[2]; 771 int ret; 772 773 cc[0] = c; 774 cc[1] = nextc; 775 776 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL); 777 if (ret < 0) 778 return NULL; 779 return mbdst + ret; 780 } 781 782 char * 783 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc) 784 { 785 char cc[2]; 786 int ret; 787 788 cc[0] = c; 789 cc[1] = nextc; 790 791 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL); 792 if (ret < 0) 793 return NULL; 794 return mbdst + ret; 795 } 796 797 /* 798 * strvis - visually encode characters from src into dst 799 * 800 * Dst must be 4 times the size of src to account for possible 801 * expansion. The length of dst, not including the trailing NULL, 802 * is returned. 803 */ 804 805 int 806 strvis(char *mbdst, const char *mbsrc, int flags) 807 { 808 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL); 809 } 810 811 int 812 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags) 813 { 814 return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL); 815 } 816 817 int 818 stravis(char **mbdstp, const char *mbsrc, int flags) 819 { 820 *mbdstp = NULL; 821 return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL); 822 } 823 824 /* 825 * strvisx - visually encode characters from src into dst 826 * 827 * Dst must be 4 times the size of src to account for possible 828 * expansion. The length of dst, not including the trailing NULL, 829 * is returned. 830 * 831 * Strvisx encodes exactly len characters from src into dst. 832 * This is useful for encoding a block of data. 833 */ 834 835 int 836 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags) 837 { 838 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL); 839 } 840 841 int 842 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags) 843 { 844 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL); 845 } 846 847 int 848 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, 849 int *cerr_ptr) 850 { 851 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr); 852 } 853 #endif 854