blob: 0d15886f05354d216ebc35aa288f155d19a003ab [file] [log] [blame]
Damien Millerb93addb2003-01-07 17:04:18 +11001/*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33#include "config.h"
Damien Miller9715bb12003-01-17 10:31:38 +110034#if !defined(HAVE_STRNVIS)
Damien Millerb93addb2003-01-07 17:04:18 +110035
36#if defined(LIBC_SCCS) && !defined(lint)
Damien Millere323df62003-05-18 22:24:09 +100037static char rcsid[] = "$OpenBSD: vis.c,v 1.11 2003/05/14 05:16:43 pjanzen Exp $";
Damien Millerb93addb2003-01-07 17:04:18 +110038#endif /* LIBC_SCCS and not lint */
39
40#include <ctype.h>
41
42#include "vis.h"
43
44#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
45#define isvisible(c) (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
46 isgraph((u_char)(c))) || \
47 ((flag & VIS_SP) == 0 && (c) == ' ') || \
48 ((flag & VIS_TAB) == 0 && (c) == '\t') || \
49 ((flag & VIS_NL) == 0 && (c) == '\n') || \
Damien Millere323df62003-05-18 22:24:09 +100050 ((flag & VIS_SAFE) && ((c) == '\b' || \
51 (c) == '\007' || (c) == '\r' || \
52 isgraph((u_char)(c)))))
Damien Millerb93addb2003-01-07 17:04:18 +110053
54/*
55 * vis - visually encode characters
56 */
57char *
58vis(dst, c, flag, nextc)
59 register char *dst;
60 int c, nextc;
61 register int flag;
62{
63 if (isvisible(c)) {
64 *dst++ = c;
65 if (c == '\\' && (flag & VIS_NOSLASH) == 0)
66 *dst++ = '\\';
67 *dst = '\0';
68 return (dst);
69 }
70
71 if (flag & VIS_CSTYLE) {
72 switch(c) {
73 case '\n':
74 *dst++ = '\\';
75 *dst++ = 'n';
76 goto done;
77 case '\r':
78 *dst++ = '\\';
79 *dst++ = 'r';
80 goto done;
81 case '\b':
82 *dst++ = '\\';
83 *dst++ = 'b';
84 goto done;
85 case '\a':
86 *dst++ = '\\';
87 *dst++ = 'a';
88 goto done;
89 case '\v':
90 *dst++ = '\\';
91 *dst++ = 'v';
92 goto done;
93 case '\t':
94 *dst++ = '\\';
95 *dst++ = 't';
96 goto done;
97 case '\f':
98 *dst++ = '\\';
99 *dst++ = 'f';
100 goto done;
101 case ' ':
102 *dst++ = '\\';
103 *dst++ = 's';
104 goto done;
105 case '\0':
106 *dst++ = '\\';
107 *dst++ = '0';
108 if (isoctal(nextc)) {
109 *dst++ = '0';
110 *dst++ = '0';
111 }
112 goto done;
113 }
114 }
115 if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
116 *dst++ = '\\';
117 *dst++ = ((u_char)c >> 6 & 07) + '0';
118 *dst++ = ((u_char)c >> 3 & 07) + '0';
119 *dst++ = ((u_char)c & 07) + '0';
120 goto done;
121 }
122 if ((flag & VIS_NOSLASH) == 0)
123 *dst++ = '\\';
124 if (c & 0200) {
125 c &= 0177;
126 *dst++ = 'M';
127 }
128 if (iscntrl(c)) {
129 *dst++ = '^';
130 if (c == 0177)
131 *dst++ = '?';
132 else
133 *dst++ = c + '@';
134 } else {
135 *dst++ = '-';
136 *dst++ = c;
137 }
138done:
139 *dst = '\0';
140 return (dst);
141}
142
143/*
144 * strvis, strnvis, strvisx - visually encode characters from src into dst
145 *
146 * Dst must be 4 times the size of src to account for possible
147 * expansion. The length of dst, not including the trailing NULL,
148 * is returned.
149 *
150 * Strnvis will write no more than siz-1 bytes (and will NULL terminate).
151 * The number of bytes needed to fully encode the string is returned.
152 *
153 * Strvisx encodes exactly len bytes from src into dst.
154 * This is useful for encoding a block of data.
155 */
156int
157strvis(dst, src, flag)
158 register char *dst;
159 register const char *src;
160 int flag;
161{
162 register char c;
163 char *start;
164
165 for (start = dst; (c = *src);)
166 dst = vis(dst, c, flag, *++src);
167 *dst = '\0';
168 return (dst - start);
169}
170
171int
172strnvis(dst, src, siz, flag)
Damien Millere323df62003-05-18 22:24:09 +1000173 char *dst;
174 const char *src;
Damien Millerb93addb2003-01-07 17:04:18 +1100175 size_t siz;
176 int flag;
177{
Damien Millere323df62003-05-18 22:24:09 +1000178 char c;
Damien Millerb93addb2003-01-07 17:04:18 +1100179 char *start, *end;
Damien Millere323df62003-05-18 22:24:09 +1000180 char tbuf[5];
181 int i;
Damien Millerb93addb2003-01-07 17:04:18 +1100182
Damien Millere323df62003-05-18 22:24:09 +1000183 i = 0;
Damien Millerb93addb2003-01-07 17:04:18 +1100184 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
185 if (isvisible(c)) {
Damien Millere323df62003-05-18 22:24:09 +1000186 i = 1;
Damien Millerb93addb2003-01-07 17:04:18 +1100187 *dst++ = c;
188 if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
189 /* need space for the extra '\\' */
190 if (dst < end)
191 *dst++ = '\\';
192 else {
193 dst--;
Damien Millere323df62003-05-18 22:24:09 +1000194 i = 2;
Damien Millerb93addb2003-01-07 17:04:18 +1100195 break;
196 }
197 }
198 src++;
199 } else {
Damien Millere323df62003-05-18 22:24:09 +1000200 i = vis(tbuf, c, flag, *++src) - tbuf;
201 if (dst + i <= end) {
202 memcpy(dst, tbuf, i);
203 dst += i;
204 } else {
205 src--;
Damien Millerb93addb2003-01-07 17:04:18 +1100206 break;
Damien Millere323df62003-05-18 22:24:09 +1000207 }
Damien Millerb93addb2003-01-07 17:04:18 +1100208 }
209 }
Damien Millere323df62003-05-18 22:24:09 +1000210 if (siz > 0)
211 *dst = '\0';
212 if (dst + i > end) {
Damien Millerb93addb2003-01-07 17:04:18 +1100213 /* adjust return value for truncation */
214 while ((c = *src))
215 dst += vis(tbuf, c, flag, *++src) - tbuf;
216 }
217 return (dst - start);
218}
219
220int
221strvisx(dst, src, len, flag)
222 register char *dst;
223 register const char *src;
224 register size_t len;
225 int flag;
226{
227 register char c;
228 char *start;
229
230 for (start = dst; len > 1; len--) {
231 c = *src;
232 dst = vis(dst, c, flag, *++src);
233 }
234 if (len)
235 dst = vis(dst, *src, flag, '\0');
236 *dst = '\0';
237 return (dst - start);
238}
239
240#endif