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