Deleted Added
full compact
vis.c (244401) vis.c (248302)
1/* $NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 christos Exp $ */
1/* $NetBSD: vis.c,v 1.60 2013/02/21 16:21:20 joerg 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:

--- 42 unchanged lines hidden (view full) ---

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)
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:

--- 42 unchanged lines hidden (view full) ---

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