1 | /* $NetBSD: vis.c,v 1.33 2005/05/28 13:11:14 lukem 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: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. Neither the name of the University nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | /*- |
---|
33 | * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc. |
---|
34 | * All rights reserved. |
---|
35 | * |
---|
36 | * Redistribution and use in source and binary forms, with or without |
---|
37 | * modification, are permitted provided that the following conditions |
---|
38 | * are met: |
---|
39 | * 1. Redistributions of source code must retain the above copyright |
---|
40 | * notice, this list of conditions and the following disclaimer. |
---|
41 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
42 | * notice, this list of conditions and the following disclaimer in the |
---|
43 | * documentation and/or other materials provided with the distribution. |
---|
44 | * 3. All advertising materials mentioning features or use of this software |
---|
45 | * must display the following acknowledgement: |
---|
46 | * This product includes software developed by the NetBSD |
---|
47 | * Foundation, Inc. and its contributors. |
---|
48 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
---|
49 | * contributors may be used to endorse or promote products derived |
---|
50 | * from this software without specific prior written permission. |
---|
51 | * |
---|
52 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
---|
53 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
---|
54 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
55 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
---|
56 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
57 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
58 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
59 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
60 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
61 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
62 | * POSSIBILITY OF SUCH DAMAGE. |
---|
63 | */ |
---|
64 | |
---|
65 | #ifdef HAVE_CONFIG_H |
---|
66 | #include "config.h" |
---|
67 | #endif |
---|
68 | |
---|
69 | #define _DIAGASSERT(a) |
---|
70 | |
---|
71 | #if 0 |
---|
72 | #include <sys/cdefs.h> |
---|
73 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
74 | __RCSID("$NetBSD: vis.c,v 1.33 2005/05/28 13:11:14 lukem Exp $"); |
---|
75 | #endif /* LIBC_SCCS and not lint */ |
---|
76 | #endif |
---|
77 | |
---|
78 | #include <sys/types.h> |
---|
79 | |
---|
80 | #include <assert.h> |
---|
81 | #include <vis.h> |
---|
82 | #include <stdlib.h> |
---|
83 | |
---|
84 | #if !HAVE_VIS || !HAVE_SVIS |
---|
85 | #include <ctype.h> |
---|
86 | #include <limits.h> |
---|
87 | #include <stdio.h> |
---|
88 | #include <string.h> |
---|
89 | |
---|
90 | #undef BELL |
---|
91 | #define BELL '\a' |
---|
92 | |
---|
93 | #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') |
---|
94 | #define iswhite(c) (c == ' ' || c == '\t' || c == '\n') |
---|
95 | #define issafe(c) (c == '\b' || c == BELL || c == '\r') |
---|
96 | #define xtoa(c) "0123456789abcdef"[c] |
---|
97 | |
---|
98 | #define MAXEXTRAS 5 |
---|
99 | |
---|
100 | |
---|
101 | #define MAKEEXTRALIST(flag, extra, orig) \ |
---|
102 | do { \ |
---|
103 | const char *o = orig; \ |
---|
104 | char *e; \ |
---|
105 | while (*o++) \ |
---|
106 | continue; \ |
---|
107 | extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \ |
---|
108 | if (!extra) break; \ |
---|
109 | for (o = orig, e = extra; (*e++ = *o++) != '\0';) \ |
---|
110 | continue; \ |
---|
111 | e--; \ |
---|
112 | if (flag & VIS_SP) *e++ = ' '; \ |
---|
113 | if (flag & VIS_TAB) *e++ = '\t'; \ |
---|
114 | if (flag & VIS_NL) *e++ = '\n'; \ |
---|
115 | if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \ |
---|
116 | *e = '\0'; \ |
---|
117 | } while (/*CONSTCOND*/0) |
---|
118 | |
---|
119 | |
---|
120 | /* |
---|
121 | * This is HVIS, the macro of vis used to HTTP style (RFC 1808) |
---|
122 | */ |
---|
123 | #define HVIS(dst, c, flag, nextc, extra) \ |
---|
124 | do \ |
---|
125 | if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \ |
---|
126 | *dst++ = '%'; \ |
---|
127 | *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); \ |
---|
128 | *dst++ = xtoa((unsigned int)c & 0xf); \ |
---|
129 | } else { \ |
---|
130 | SVIS(dst, c, flag, nextc, extra); \ |
---|
131 | } \ |
---|
132 | while (/*CONSTCOND*/0) |
---|
133 | |
---|
134 | /* |
---|
135 | * This is SVIS, the central macro of vis. |
---|
136 | * dst: Pointer to the destination buffer |
---|
137 | * c: Character to encode |
---|
138 | * flag: Flag word |
---|
139 | * nextc: The character following 'c' |
---|
140 | * extra: Pointer to the list of extra characters to be |
---|
141 | * backslash-protected. |
---|
142 | */ |
---|
143 | #define SVIS(dst, c, flag, nextc, extra) \ |
---|
144 | do { \ |
---|
145 | int isextra; \ |
---|
146 | isextra = strchr(extra, c) != NULL; \ |
---|
147 | if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \ |
---|
148 | ((flag & VIS_SAFE) && issafe(c)))) { \ |
---|
149 | *dst++ = c; \ |
---|
150 | break; \ |
---|
151 | } \ |
---|
152 | if (flag & VIS_CSTYLE) { \ |
---|
153 | switch (c) { \ |
---|
154 | case '\n': \ |
---|
155 | *dst++ = '\\'; *dst++ = 'n'; \ |
---|
156 | continue; \ |
---|
157 | case '\r': \ |
---|
158 | *dst++ = '\\'; *dst++ = 'r'; \ |
---|
159 | continue; \ |
---|
160 | case '\b': \ |
---|
161 | *dst++ = '\\'; *dst++ = 'b'; \ |
---|
162 | continue; \ |
---|
163 | case BELL: \ |
---|
164 | *dst++ = '\\'; *dst++ = 'a'; \ |
---|
165 | continue; \ |
---|
166 | case '\v': \ |
---|
167 | *dst++ = '\\'; *dst++ = 'v'; \ |
---|
168 | continue; \ |
---|
169 | case '\t': \ |
---|
170 | *dst++ = '\\'; *dst++ = 't'; \ |
---|
171 | continue; \ |
---|
172 | case '\f': \ |
---|
173 | *dst++ = '\\'; *dst++ = 'f'; \ |
---|
174 | continue; \ |
---|
175 | case ' ': \ |
---|
176 | *dst++ = '\\'; *dst++ = 's'; \ |
---|
177 | continue; \ |
---|
178 | case '\0': \ |
---|
179 | *dst++ = '\\'; *dst++ = '0'; \ |
---|
180 | if (isoctal(nextc)) { \ |
---|
181 | *dst++ = '0'; \ |
---|
182 | *dst++ = '0'; \ |
---|
183 | } \ |
---|
184 | continue; \ |
---|
185 | default: \ |
---|
186 | if (isgraph(c)) { \ |
---|
187 | *dst++ = '\\'; *dst++ = c; \ |
---|
188 | continue; \ |
---|
189 | } \ |
---|
190 | } \ |
---|
191 | } \ |
---|
192 | if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \ |
---|
193 | *dst++ = '\\'; \ |
---|
194 | *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \ |
---|
195 | *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \ |
---|
196 | *dst++ = (c & 07) + '0'; \ |
---|
197 | } else { \ |
---|
198 | if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \ |
---|
199 | if (c & 0200) { \ |
---|
200 | c &= 0177; *dst++ = 'M'; \ |
---|
201 | } \ |
---|
202 | if (iscntrl(c)) { \ |
---|
203 | *dst++ = '^'; \ |
---|
204 | if (c == 0177) \ |
---|
205 | *dst++ = '?'; \ |
---|
206 | else \ |
---|
207 | *dst++ = c + '@'; \ |
---|
208 | } else { \ |
---|
209 | *dst++ = '-'; *dst++ = c; \ |
---|
210 | } \ |
---|
211 | } \ |
---|
212 | } while (/*CONSTCOND*/0) |
---|
213 | |
---|
214 | |
---|
215 | /* |
---|
216 | * svis - visually encode characters, also encoding the characters |
---|
217 | * pointed to by `extra' |
---|
218 | */ |
---|
219 | char * |
---|
220 | svis(char *dst, int c, int flag, int nextc, const char *extra) |
---|
221 | { |
---|
222 | char *nextra = NULL; |
---|
223 | |
---|
224 | _DIAGASSERT(dst != NULL); |
---|
225 | _DIAGASSERT(extra != NULL); |
---|
226 | MAKEEXTRALIST(flag, nextra, extra); |
---|
227 | if (!nextra) { |
---|
228 | *dst = '\0'; /* can't create nextra, return "" */ |
---|
229 | return dst; |
---|
230 | } |
---|
231 | if (flag & VIS_HTTPSTYLE) |
---|
232 | HVIS(dst, c, flag, nextc, nextra); |
---|
233 | else |
---|
234 | SVIS(dst, c, flag, nextc, nextra); |
---|
235 | free(nextra); |
---|
236 | *dst = '\0'; |
---|
237 | return dst; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /* |
---|
242 | * strsvis, strsvisx - visually encode characters from src into dst |
---|
243 | * |
---|
244 | * Extra is a pointer to a \0-terminated list of characters to |
---|
245 | * be encoded, too. These functions are useful e. g. to |
---|
246 | * encode strings in such a way so that they are not interpreted |
---|
247 | * by a shell. |
---|
248 | * |
---|
249 | * Dst must be 4 times the size of src to account for possible |
---|
250 | * expansion. The length of dst, not including the trailing NULL, |
---|
251 | * is returned. |
---|
252 | * |
---|
253 | * Strsvisx encodes exactly len bytes from src into dst. |
---|
254 | * This is useful for encoding a block of data. |
---|
255 | */ |
---|
256 | int |
---|
257 | strsvis(char *dst, const char *csrc, int flag, const char *extra) |
---|
258 | { |
---|
259 | int c; |
---|
260 | char *start; |
---|
261 | char *nextra = NULL; |
---|
262 | const unsigned char *src = (const unsigned char *)csrc; |
---|
263 | |
---|
264 | _DIAGASSERT(dst != NULL); |
---|
265 | _DIAGASSERT(src != NULL); |
---|
266 | _DIAGASSERT(extra != NULL); |
---|
267 | MAKEEXTRALIST(flag, nextra, extra); |
---|
268 | if (!nextra) { |
---|
269 | *dst = '\0'; /* can't create nextra, return "" */ |
---|
270 | return 0; |
---|
271 | } |
---|
272 | if (flag & VIS_HTTPSTYLE) { |
---|
273 | for (start = dst; (c = *src++) != '\0'; /* empty */) |
---|
274 | HVIS(dst, c, flag, *src, nextra); |
---|
275 | } else { |
---|
276 | for (start = dst; (c = *src++) != '\0'; /* empty */) |
---|
277 | SVIS(dst, c, flag, *src, nextra); |
---|
278 | } |
---|
279 | free(nextra); |
---|
280 | *dst = '\0'; |
---|
281 | return (dst - start); |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | int |
---|
286 | strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) |
---|
287 | { |
---|
288 | unsigned char c; |
---|
289 | char *start; |
---|
290 | char *nextra = NULL; |
---|
291 | const unsigned char *src = (const unsigned char *)csrc; |
---|
292 | |
---|
293 | _DIAGASSERT(dst != NULL); |
---|
294 | _DIAGASSERT(src != NULL); |
---|
295 | _DIAGASSERT(extra != NULL); |
---|
296 | MAKEEXTRALIST(flag, nextra, extra); |
---|
297 | if (! nextra) { |
---|
298 | *dst = '\0'; /* can't create nextra, return "" */ |
---|
299 | return 0; |
---|
300 | } |
---|
301 | |
---|
302 | if (flag & VIS_HTTPSTYLE) { |
---|
303 | for (start = dst; len > 0; len--) { |
---|
304 | c = *src++; |
---|
305 | HVIS(dst, c, flag, len ? *src : '\0', nextra); |
---|
306 | } |
---|
307 | } else { |
---|
308 | for (start = dst; len > 0; len--) { |
---|
309 | c = *src++; |
---|
310 | SVIS(dst, c, flag, len ? *src : '\0', nextra); |
---|
311 | } |
---|
312 | } |
---|
313 | free(nextra); |
---|
314 | *dst = '\0'; |
---|
315 | return (dst - start); |
---|
316 | } |
---|
317 | #endif |
---|
318 | |
---|
319 | #if !HAVE_VIS |
---|
320 | /* |
---|
321 | * vis - visually encode characters |
---|
322 | */ |
---|
323 | char * |
---|
324 | vis(char *dst, int c, int flag, int nextc) |
---|
325 | { |
---|
326 | char *extra = NULL; |
---|
327 | unsigned char uc = (unsigned char)c; |
---|
328 | |
---|
329 | _DIAGASSERT(dst != NULL); |
---|
330 | |
---|
331 | MAKEEXTRALIST(flag, extra, ""); |
---|
332 | if (! extra) { |
---|
333 | *dst = '\0'; /* can't create extra, return "" */ |
---|
334 | return dst; |
---|
335 | } |
---|
336 | if (flag & VIS_HTTPSTYLE) |
---|
337 | HVIS(dst, uc, flag, nextc, extra); |
---|
338 | else |
---|
339 | SVIS(dst, uc, flag, nextc, extra); |
---|
340 | free(extra); |
---|
341 | *dst = '\0'; |
---|
342 | return dst; |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | /* |
---|
347 | * strvis, strvisx - visually encode characters from src into dst |
---|
348 | * |
---|
349 | * Dst must be 4 times the size of src to account for possible |
---|
350 | * expansion. The length of dst, not including the trailing NULL, |
---|
351 | * is returned. |
---|
352 | * |
---|
353 | * Strvisx encodes exactly len bytes from src into dst. |
---|
354 | * This is useful for encoding a block of data. |
---|
355 | */ |
---|
356 | int |
---|
357 | strvis(char *dst, const char *src, int flag) |
---|
358 | { |
---|
359 | char *extra = NULL; |
---|
360 | int rv; |
---|
361 | |
---|
362 | MAKEEXTRALIST(flag, extra, ""); |
---|
363 | if (!extra) { |
---|
364 | *dst = '\0'; /* can't create extra, return "" */ |
---|
365 | return 0; |
---|
366 | } |
---|
367 | rv = strsvis(dst, src, flag, extra); |
---|
368 | free(extra); |
---|
369 | return rv; |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | int |
---|
374 | strvisx(char *dst, const char *src, size_t len, int flag) |
---|
375 | { |
---|
376 | char *extra = NULL; |
---|
377 | int rv; |
---|
378 | |
---|
379 | MAKEEXTRALIST(flag, extra, ""); |
---|
380 | if (!extra) { |
---|
381 | *dst = '\0'; /* can't create extra, return "" */ |
---|
382 | return 0; |
---|
383 | } |
---|
384 | rv = strsvisx(dst, src, len, flag, extra); |
---|
385 | free(extra); |
---|
386 | return rv; |
---|
387 | } |
---|
388 | #endif |
---|