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