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