vis.c revision 1.45 1 1.45 christos /* $NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 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.45 christos __RCSID("$NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 christos Exp $");
61 1.21 tv #endif /* LIBC_SCCS and not lint */
62 1.1 cgd
63 1.8 jtc #include "namespace.h"
64 1.1 cgd #include <sys/types.h>
65 1.12 lukem
66 1.12 lukem #include <assert.h>
67 1.1 cgd #include <vis.h>
68 1.44 christos #include <errno.h>
69 1.22 christos #include <stdlib.h>
70 1.8 jtc
71 1.8 jtc #ifdef __weak_alias
72 1.18 mycroft __weak_alias(strvisx,_strvisx)
73 1.20 tv #endif
74 1.20 tv
75 1.24 pooka #if !HAVE_VIS || !HAVE_SVIS
76 1.20 tv #include <ctype.h>
77 1.20 tv #include <limits.h>
78 1.20 tv #include <stdio.h>
79 1.20 tv #include <string.h>
80 1.1 cgd
81 1.42 christos static char *do_svis(char *, size_t *, int, int, int, const char *);
82 1.37 dsl
83 1.15 wennmach #undef BELL
84 1.15 wennmach #define BELL '\a'
85 1.15 wennmach
86 1.16 wennmach #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
87 1.16 wennmach #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
88 1.16 wennmach #define issafe(c) (c == '\b' || c == BELL || c == '\r')
89 1.22 christos #define xtoa(c) "0123456789abcdef"[c]
90 1.39 christos #define XTOA(c) "0123456789ABCDEF"[c]
91 1.16 wennmach
92 1.45 christos #define MAXEXTRAS 9
93 1.15 wennmach
94 1.34 martin #define MAKEEXTRALIST(flag, extra, orig_str) \
95 1.16 wennmach do { \
96 1.34 martin const char *orig = orig_str; \
97 1.22 christos const char *o = orig; \
98 1.27 enami char *e; \
99 1.22 christos while (*o++) \
100 1.22 christos continue; \
101 1.31 lukem extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
102 1.31 lukem if (!extra) break; \
103 1.22 christos for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
104 1.22 christos continue; \
105 1.22 christos e--; \
106 1.45 christos if (flag & VIS_GLOB) { \
107 1.45 christos *e++ = '*'; \
108 1.45 christos *e++ = '?'; \
109 1.45 christos *e++ = '['; \
110 1.45 christos *e++ = '#'; \
111 1.45 christos } \
112 1.27 enami if (flag & VIS_SP) *e++ = ' '; \
113 1.22 christos if (flag & VIS_TAB) *e++ = '\t'; \
114 1.27 enami if (flag & VIS_NL) *e++ = '\n'; \
115 1.27 enami if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
116 1.22 christos *e = '\0'; \
117 1.19 mycroft } while (/*CONSTCOND*/0)
118 1.15 wennmach
119 1.22 christos /*
120 1.37 dsl * This is do_hvis, for HTTP style (RFC 1808)
121 1.22 christos */
122 1.37 dsl static char *
123 1.44 christos do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
124 1.37 dsl {
125 1.41 plunky
126 1.41 plunky if ((isascii(c) && isalnum(c))
127 1.41 plunky /* safe */
128 1.41 plunky || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
129 1.41 plunky /* extra */
130 1.41 plunky || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
131 1.41 plunky || c == ',') {
132 1.44 christos dst = do_svis(dst, dlen, c, flag, nextc, extra);
133 1.41 plunky } else {
134 1.44 christos if (dlen) {
135 1.44 christos if (*dlen < 3)
136 1.42 christos return NULL;
137 1.44 christos *dlen -= 3;
138 1.42 christos }
139 1.37 dsl *dst++ = '%';
140 1.37 dsl *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
141 1.37 dsl *dst++ = xtoa((unsigned int)c & 0xf);
142 1.37 dsl }
143 1.41 plunky
144 1.37 dsl return dst;
145 1.37 dsl }
146 1.27 enami
147 1.15 wennmach /*
148 1.39 christos * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
149 1.39 christos * NB: No handling of long lines or CRLF.
150 1.39 christos */
151 1.39 christos static char *
152 1.44 christos do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
153 1.39 christos {
154 1.39 christos if ((c != '\n') &&
155 1.39 christos /* Space at the end of the line */
156 1.39 christos ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
157 1.39 christos /* Out of range */
158 1.39 christos (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
159 1.39 christos /* Specific char to be escaped */
160 1.39 christos strchr("#$@[\\]^`{|}~", c) != NULL)) {
161 1.44 christos if (dlen) {
162 1.44 christos if (*dlen < 3)
163 1.42 christos return NULL;
164 1.44 christos *dlen -= 3;
165 1.42 christos }
166 1.39 christos *dst++ = '=';
167 1.39 christos *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
168 1.39 christos *dst++ = XTOA((unsigned int)c & 0xf);
169 1.39 christos } else {
170 1.44 christos dst = do_svis(dst, dlen, c, flag, nextc, extra);
171 1.39 christos }
172 1.39 christos return dst;
173 1.39 christos }
174 1.39 christos
175 1.39 christos /*
176 1.37 dsl * This is do_vis, the central code of vis.
177 1.16 wennmach * dst: Pointer to the destination buffer
178 1.16 wennmach * c: Character to encode
179 1.15 wennmach * flag: Flag word
180 1.15 wennmach * nextc: The character following 'c'
181 1.15 wennmach * extra: Pointer to the list of extra characters to be
182 1.16 wennmach * backslash-protected.
183 1.15 wennmach */
184 1.37 dsl static char *
185 1.44 christos do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
186 1.37 dsl {
187 1.37 dsl int isextra;
188 1.44 christos size_t odlen = dlen ? *dlen : 0;
189 1.43 christos
190 1.37 dsl isextra = strchr(extra, c) != NULL;
191 1.42 christos #define HAVE(x) \
192 1.42 christos do { \
193 1.44 christos if (dlen) { \
194 1.44 christos if (*dlen < (x)) \
195 1.42 christos goto out; \
196 1.44 christos *dlen -= (x); \
197 1.42 christos } \
198 1.42 christos } while (/*CONSTCOND*/0)
199 1.37 dsl if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
200 1.37 dsl ((flag & VIS_SAFE) && issafe(c)))) {
201 1.42 christos HAVE(1);
202 1.37 dsl *dst++ = c;
203 1.37 dsl return dst;
204 1.37 dsl }
205 1.37 dsl if (flag & VIS_CSTYLE) {
206 1.42 christos HAVE(2);
207 1.37 dsl switch (c) {
208 1.37 dsl case '\n':
209 1.37 dsl *dst++ = '\\'; *dst++ = 'n';
210 1.37 dsl return dst;
211 1.37 dsl case '\r':
212 1.37 dsl *dst++ = '\\'; *dst++ = 'r';
213 1.37 dsl return dst;
214 1.37 dsl case '\b':
215 1.37 dsl *dst++ = '\\'; *dst++ = 'b';
216 1.37 dsl return dst;
217 1.37 dsl case BELL:
218 1.37 dsl *dst++ = '\\'; *dst++ = 'a';
219 1.37 dsl return dst;
220 1.37 dsl case '\v':
221 1.37 dsl *dst++ = '\\'; *dst++ = 'v';
222 1.37 dsl return dst;
223 1.37 dsl case '\t':
224 1.37 dsl *dst++ = '\\'; *dst++ = 't';
225 1.37 dsl return dst;
226 1.37 dsl case '\f':
227 1.37 dsl *dst++ = '\\'; *dst++ = 'f';
228 1.37 dsl return dst;
229 1.37 dsl case ' ':
230 1.37 dsl *dst++ = '\\'; *dst++ = 's';
231 1.37 dsl return dst;
232 1.37 dsl case '\0':
233 1.37 dsl *dst++ = '\\'; *dst++ = '0';
234 1.37 dsl if (isoctal(nextc)) {
235 1.42 christos HAVE(2);
236 1.37 dsl *dst++ = '0';
237 1.37 dsl *dst++ = '0';
238 1.37 dsl }
239 1.37 dsl return dst;
240 1.37 dsl default:
241 1.37 dsl if (isgraph(c)) {
242 1.37 dsl *dst++ = '\\'; *dst++ = c;
243 1.37 dsl return dst;
244 1.37 dsl }
245 1.44 christos if (dlen)
246 1.44 christos *dlen = odlen;
247 1.37 dsl }
248 1.37 dsl }
249 1.37 dsl if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
250 1.42 christos HAVE(4);
251 1.37 dsl *dst++ = '\\';
252 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
253 1.37 dsl *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
254 1.37 dsl *dst++ = (c & 07) + '0';
255 1.37 dsl } else {
256 1.42 christos if ((flag & VIS_NOSLASH) == 0) {
257 1.42 christos HAVE(1);
258 1.42 christos *dst++ = '\\';
259 1.42 christos }
260 1.42 christos
261 1.37 dsl if (c & 0200) {
262 1.42 christos HAVE(1);
263 1.37 dsl c &= 0177; *dst++ = 'M';
264 1.37 dsl }
265 1.42 christos
266 1.37 dsl if (iscntrl(c)) {
267 1.42 christos HAVE(2);
268 1.37 dsl *dst++ = '^';
269 1.37 dsl if (c == 0177)
270 1.37 dsl *dst++ = '?';
271 1.37 dsl else
272 1.37 dsl *dst++ = c + '@';
273 1.37 dsl } else {
274 1.42 christos HAVE(2);
275 1.37 dsl *dst++ = '-'; *dst++ = c;
276 1.37 dsl }
277 1.37 dsl }
278 1.37 dsl return dst;
279 1.42 christos out:
280 1.44 christos *dlen = odlen;
281 1.42 christos return NULL;
282 1.37 dsl }
283 1.15 wennmach
284 1.42 christos typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
285 1.39 christos
286 1.39 christos /*
287 1.39 christos * Return the appropriate encoding function depending on the flags given.
288 1.39 christos */
289 1.39 christos static visfun_t
290 1.39 christos getvisfun(int flag)
291 1.39 christos {
292 1.39 christos if (flag & VIS_HTTPSTYLE)
293 1.39 christos return do_hvis;
294 1.40 christos if (flag & VIS_MIMESTYLE)
295 1.39 christos return do_mvis;
296 1.39 christos return do_svis;
297 1.39 christos }
298 1.15 wennmach
299 1.15 wennmach /*
300 1.44 christos * isnvis - visually encode characters, also encoding the characters
301 1.33 lukem * pointed to by `extra'
302 1.15 wennmach */
303 1.44 christos static char *
304 1.44 christos isnvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
305 1.15 wennmach {
306 1.31 lukem char *nextra = NULL;
307 1.39 christos visfun_t f;
308 1.31 lukem
309 1.16 wennmach _DIAGASSERT(dst != NULL);
310 1.16 wennmach _DIAGASSERT(extra != NULL);
311 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
312 1.31 lukem if (!nextra) {
313 1.44 christos if (dlen && *dlen == 0) {
314 1.44 christos errno = ENOSPC;
315 1.44 christos return NULL;
316 1.44 christos }
317 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
318 1.33 lukem return dst;
319 1.31 lukem }
320 1.39 christos f = getvisfun(flag);
321 1.44 christos dst = (*f)(dst, dlen, c, flag, nextc, nextra);
322 1.31 lukem free(nextra);
323 1.44 christos if (dst == NULL || (dlen && *dlen == 0)) {
324 1.44 christos errno = ENOSPC;
325 1.44 christos return NULL;
326 1.44 christos }
327 1.16 wennmach *dst = '\0';
328 1.33 lukem return dst;
329 1.15 wennmach }
330 1.15 wennmach
331 1.44 christos char *
332 1.44 christos svis(char *dst, int c, int flag, int nextc, const char *extra)
333 1.44 christos {
334 1.44 christos return isnvis(dst, NULL, c, flag, nextc, extra);
335 1.44 christos }
336 1.44 christos
337 1.44 christos char *
338 1.44 christos snvis(char *dst, size_t dlen, int c, int flag, int nextc, const char *extra)
339 1.44 christos {
340 1.44 christos return isnvis(dst, &dlen, c, flag, nextc, extra);
341 1.44 christos }
342 1.44 christos
343 1.15 wennmach
344 1.15 wennmach /*
345 1.15 wennmach * strsvis, strsvisx - visually encode characters from src into dst
346 1.15 wennmach *
347 1.16 wennmach * Extra is a pointer to a \0-terminated list of characters to
348 1.17 wennmach * be encoded, too. These functions are useful e. g. to
349 1.17 wennmach * encode strings in such a way so that they are not interpreted
350 1.16 wennmach * by a shell.
351 1.27 enami *
352 1.16 wennmach * Dst must be 4 times the size of src to account for possible
353 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
354 1.27 enami * is returned.
355 1.15 wennmach *
356 1.16 wennmach * Strsvisx encodes exactly len bytes from src into dst.
357 1.16 wennmach * This is useful for encoding a block of data.
358 1.15 wennmach */
359 1.44 christos static int
360 1.44 christos istrsnvis(char *dst, size_t *dlen, const char *csrc, int flag, const char *extra)
361 1.15 wennmach {
362 1.25 dsl int c;
363 1.16 wennmach char *start;
364 1.31 lukem char *nextra = NULL;
365 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
366 1.39 christos visfun_t f;
367 1.15 wennmach
368 1.16 wennmach _DIAGASSERT(dst != NULL);
369 1.16 wennmach _DIAGASSERT(src != NULL);
370 1.16 wennmach _DIAGASSERT(extra != NULL);
371 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
372 1.31 lukem if (!nextra) {
373 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
374 1.31 lukem return 0;
375 1.31 lukem }
376 1.39 christos f = getvisfun(flag);
377 1.44 christos for (start = dst; (c = *src++) != '\0'; /* empty */) {
378 1.44 christos dst = (*f)(dst, dlen, c, flag, *src, nextra);
379 1.44 christos if (dst == NULL) {
380 1.44 christos errno = ENOSPC;
381 1.44 christos return -1;
382 1.44 christos }
383 1.44 christos }
384 1.31 lukem free(nextra);
385 1.44 christos if (dlen && *dlen == 0) {
386 1.44 christos errno = ENOSPC;
387 1.44 christos return -1;
388 1.44 christos }
389 1.16 wennmach *dst = '\0';
390 1.39 christos return (int)(dst - start);
391 1.15 wennmach }
392 1.15 wennmach
393 1.44 christos int
394 1.44 christos strsvis(char *dst, const char *csrc, int flag, const char *extra)
395 1.44 christos {
396 1.44 christos return istrsnvis(dst, NULL, csrc, flag, extra);
397 1.44 christos }
398 1.15 wennmach
399 1.15 wennmach int
400 1.44 christos strsnvis(char *dst, size_t dlen, const char *csrc, int flag, const char *extra)
401 1.44 christos {
402 1.44 christos return istrsnvis(dst, &dlen, csrc, flag, extra);
403 1.44 christos }
404 1.44 christos
405 1.44 christos static int
406 1.44 christos istrsnvisx(char *dst, size_t *dlen, const char *csrc, size_t len, int flag,
407 1.44 christos const char *extra)
408 1.15 wennmach {
409 1.28 christos unsigned char c;
410 1.16 wennmach char *start;
411 1.31 lukem char *nextra = NULL;
412 1.25 dsl const unsigned char *src = (const unsigned char *)csrc;
413 1.39 christos visfun_t f;
414 1.15 wennmach
415 1.16 wennmach _DIAGASSERT(dst != NULL);
416 1.16 wennmach _DIAGASSERT(src != NULL);
417 1.16 wennmach _DIAGASSERT(extra != NULL);
418 1.22 christos MAKEEXTRALIST(flag, nextra, extra);
419 1.31 lukem if (! nextra) {
420 1.44 christos if (dlen && *dlen == 0) {
421 1.44 christos errno = ENOSPC;
422 1.44 christos return -1;
423 1.44 christos }
424 1.31 lukem *dst = '\0'; /* can't create nextra, return "" */
425 1.31 lukem return 0;
426 1.31 lukem }
427 1.16 wennmach
428 1.39 christos f = getvisfun(flag);
429 1.39 christos for (start = dst; len > 0; len--) {
430 1.39 christos c = *src++;
431 1.44 christos dst = (*f)(dst, dlen, c, flag, len > 1 ? *src : '\0', nextra);
432 1.44 christos if (dst == NULL) {
433 1.44 christos errno = ENOSPC;
434 1.44 christos return -1;
435 1.44 christos }
436 1.16 wennmach }
437 1.31 lukem free(nextra);
438 1.44 christos if (dlen && *dlen == 0) {
439 1.44 christos errno = ENOSPC;
440 1.44 christos return -1;
441 1.44 christos }
442 1.16 wennmach *dst = '\0';
443 1.39 christos return (int)(dst - start);
444 1.15 wennmach }
445 1.44 christos
446 1.44 christos int
447 1.44 christos strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
448 1.44 christos {
449 1.44 christos return istrsnvisx(dst, NULL, csrc, len, flag, extra);
450 1.44 christos }
451 1.44 christos
452 1.44 christos int
453 1.44 christos strsnvisx(char *dst, size_t dlen, const char *csrc, size_t len, int flag,
454 1.44 christos const char *extra)
455 1.44 christos {
456 1.44 christos return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
457 1.44 christos }
458 1.24 pooka #endif
459 1.15 wennmach
460 1.24 pooka #if !HAVE_VIS
461 1.1 cgd /*
462 1.1 cgd * vis - visually encode characters
463 1.1 cgd */
464 1.44 christos static char *
465 1.44 christos invis(char *dst, size_t *dlen, int c, int flag, int nextc)
466 1.15 wennmach {
467 1.31 lukem char *extra = NULL;
468 1.28 christos unsigned char uc = (unsigned char)c;
469 1.39 christos visfun_t f;
470 1.15 wennmach
471 1.16 wennmach _DIAGASSERT(dst != NULL);
472 1.15 wennmach
473 1.22 christos MAKEEXTRALIST(flag, extra, "");
474 1.31 lukem if (! extra) {
475 1.44 christos if (dlen && *dlen == 0) {
476 1.44 christos errno = ENOSPC;
477 1.44 christos return NULL;
478 1.44 christos }
479 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
480 1.33 lukem return dst;
481 1.31 lukem }
482 1.39 christos f = getvisfun(flag);
483 1.44 christos dst = (*f)(dst, dlen, uc, flag, nextc, extra);
484 1.32 lukem free(extra);
485 1.44 christos if (dst == NULL || (dlen && *dlen == 0)) {
486 1.44 christos errno = ENOSPC;
487 1.44 christos return NULL;
488 1.44 christos }
489 1.16 wennmach *dst = '\0';
490 1.33 lukem return dst;
491 1.1 cgd }
492 1.1 cgd
493 1.44 christos char *
494 1.44 christos vis(char *dst, int c, int flag, int nextc)
495 1.44 christos {
496 1.44 christos return invis(dst, NULL, c, flag, nextc);
497 1.44 christos }
498 1.44 christos
499 1.44 christos char *
500 1.44 christos nvis(char *dst, size_t dlen, int c, int flag, int nextc)
501 1.44 christos {
502 1.44 christos return invis(dst, &dlen, c, flag, nextc);
503 1.44 christos }
504 1.44 christos
505 1.15 wennmach
506 1.1 cgd /*
507 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
508 1.27 enami *
509 1.16 wennmach * Dst must be 4 times the size of src to account for possible
510 1.16 wennmach * expansion. The length of dst, not including the trailing NULL,
511 1.27 enami * is returned.
512 1.1 cgd *
513 1.16 wennmach * Strvisx encodes exactly len bytes from src into dst.
514 1.16 wennmach * This is useful for encoding a block of data.
515 1.1 cgd */
516 1.44 christos static int
517 1.44 christos istrnvis(char *dst, size_t *dlen, const char *src, int flag)
518 1.15 wennmach {
519 1.31 lukem char *extra = NULL;
520 1.31 lukem int rv;
521 1.15 wennmach
522 1.22 christos MAKEEXTRALIST(flag, extra, "");
523 1.31 lukem if (!extra) {
524 1.44 christos if (dlen && *dlen == 0) {
525 1.44 christos errno = ENOSPC;
526 1.44 christos return -1;
527 1.44 christos }
528 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
529 1.31 lukem return 0;
530 1.31 lukem }
531 1.44 christos rv = istrsnvis(dst, dlen, src, flag, extra);
532 1.31 lukem free(extra);
533 1.33 lukem return rv;
534 1.1 cgd }
535 1.1 cgd
536 1.44 christos int
537 1.44 christos strvis(char *dst, const char *src, int flag)
538 1.44 christos {
539 1.44 christos return istrnvis(dst, NULL, src, flag);
540 1.44 christos }
541 1.15 wennmach
542 1.1 cgd int
543 1.44 christos strnvis(char *dst, size_t dlen, const char *src, int flag)
544 1.44 christos {
545 1.44 christos return istrnvis(dst, &dlen, src, flag);
546 1.44 christos }
547 1.44 christos
548 1.44 christos static int
549 1.44 christos istrnvisx(char *dst, size_t *dlen, const char *src, size_t len, int flag)
550 1.15 wennmach {
551 1.31 lukem char *extra = NULL;
552 1.31 lukem int rv;
553 1.1 cgd
554 1.22 christos MAKEEXTRALIST(flag, extra, "");
555 1.31 lukem if (!extra) {
556 1.44 christos if (dlen && *dlen == 0) {
557 1.44 christos errno = ENOSPC;
558 1.44 christos return -1;
559 1.44 christos }
560 1.31 lukem *dst = '\0'; /* can't create extra, return "" */
561 1.31 lukem return 0;
562 1.31 lukem }
563 1.44 christos rv = istrsnvisx(dst, dlen, src, len, flag, extra);
564 1.31 lukem free(extra);
565 1.33 lukem return rv;
566 1.1 cgd }
567 1.44 christos
568 1.44 christos int
569 1.44 christos strvisx(char *dst, const char *src, size_t len, int flag)
570 1.44 christos {
571 1.44 christos return istrnvisx(dst, NULL, src, len, flag);
572 1.44 christos }
573 1.44 christos
574 1.44 christos int
575 1.44 christos strnvisx(char *dst, size_t dlen, const char *src, size_t len, int flag)
576 1.44 christos {
577 1.44 christos return istrnvisx(dst, &dlen, src, len, flag);
578 1.44 christos }
579 1.44 christos
580 1.20 tv #endif
581