Bug Summary

File:/home/joel/rtems-4.11-work/build/rtems/c/src/../../cpukit/libmisc/shell/vis.c
Location:line 361, column 2
Description:Subtraction of two pointers that do not point to the same memory chunk may cause incorrect result

Annotated Source Code

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_H1
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 <vis.h>
81#include <stdlib.h>
82
83#if !HAVE_VIS || !HAVE_SVIS
84#include <ctype.h>
85#include <limits.h>
86#include <stdio.h>
87#include <string.h>
88
89#undef BELL'\a'
90#define BELL'\a' '\a'
91
92#define isoctal(c)(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
93#define iswhite(c)(c == ' ' || c == '\t' || c == '\n') (c == ' ' || c == '\t' || c == '\n')
94#define issafe(c)(c == '\b' || c == '\a' || c == '\r') (c == '\b' || c == BELL'\a' || c == '\r')
95#define xtoa(c)"0123456789abcdef"[c] "0123456789abcdef"[c]
96
97#define MAXEXTRAS5 5
98
99
100#define MAKEEXTRALIST(flag, extra, orig)do { const char *o = orig; char *e; while (*o++) continue; extra
= malloc((size_t)((o - orig) + 5)); if (!extra) break; for (
o = orig, e = extra; (*e++ = *o++) != '\0';) continue; e--; if
(flag & 0x04) *e++ = ' '; if (flag & 0x08) *e++ = '\t'
; if (flag & 0x10) *e++ = '\n'; if ((flag & 0x40) == 0
) *e++ = '\\'; *e = '\0'; } while ( 0)
\
101do { \
102 const char *o = orig; \
103 char *e; \
104 while (*o++) \
105 continue; \
106 extra = malloc((size_t)((o - orig) + MAXEXTRAS5)); \
107 if (!extra) break; \
108 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
109 continue; \
110 e--; \
111 if (flag & VIS_SP0x04) *e++ = ' '; \
112 if (flag & VIS_TAB0x08) *e++ = '\t'; \
113 if (flag & VIS_NL0x10) *e++ = '\n'; \
114 if ((flag & VIS_NOSLASH0x40) == 0) *e++ = '\\'; \
115 *e = '\0'; \
116} while (/*CONSTCOND*/0)
117
118
119/*
120 * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
121 */
122#define HVIS(dst, c, flag, nextc, extra)do if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISalnum) || strchr("$-_.+!*'(),"
, c) != ((void*)0)) { *dst++ = '%'; *dst++ = "0123456789abcdef"
[((unsigned int)c >> 4) & 0xf]; *dst++ = "0123456789abcdef"
[(unsigned int)c & 0xf]; } else { do { int isextra; isextra
= strchr(extra, c) != ((void*)0); if (!isextra && ((
(c) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int)
((c))] & (unsigned short int) _ISgraph) || (c == ' ' || c
== '\t' || c == '\n') || ((flag & 0x20) && (c ==
'\b' || c == '\a' || c == '\r')))) { *dst++ = c; break; } if
(flag & 0x02) { switch (c) { case '\n': *dst++ = '\\'; *
dst++ = 'n'; continue; case '\r': *dst++ = '\\'; *dst++ = 'r'
; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'; continue;
case '\a': *dst++ = '\\'; *dst++ = 'a'; continue; case '\v':
*dst++ = '\\'; *dst++ = 'v'; continue; case '\t': *dst++ = '\\'
; *dst++ = 't'; continue; case '\f': *dst++ = '\\'; *dst++ = 'f'
; continue; case ' ': *dst++ = '\\'; *dst++ = 's'; continue; case
'\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char)(nextc)) >=
'0' && ((u_char)(nextc)) <= '7')) { *dst++ = '0';
*dst++ = '0'; } continue; default: if (((*__ctype_b_loc ())[
(int) ((c))] & (unsigned short int) _ISgraph)) { *dst++ =
'\\'; *dst++ = c; continue; } } } if (isextra || ((c & 0177
) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ = (u_char
)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; *
dst++ = (c & 07) + '0'; } else { if ((flag & 0x40) ==
0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst++ =
'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0); } while ( 0)
\
123do \
124 if (!isascii(c)(((c) & ~0x7f) == 0) || !isalnum(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalnum
)
|| strchr("$-_.+!*'(),", c) != NULL((void*)0)) { \
125 *dst++ = '%'; \
126 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf)"0123456789abcdef"[((unsigned int)c >> 4) & 0xf]; \
127 *dst++ = xtoa((unsigned int)c & 0xf)"0123456789abcdef"[(unsigned int)c & 0xf]; \
128 } else { \
129 SVIS(dst, c, flag, nextc, extra)do { int isextra; isextra = strchr(extra, c) != ((void*)0); if
(!isextra && (((c) & ~0x7f) == 0) && (((
*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
) || (c == ' ' || c == '\t' || c == '\n') || ((flag & 0x20
) && (c == '\b' || c == '\a' || c == '\r')))) { *dst++
= c; break; } if (flag & 0x02) { switch (c) { case '\n':
*dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(nextc)) >= '0' && ((u_char)(nextc)) <= '7')) {
*dst++ = '0'; *dst++ = '0'; } continue; default: if (((*__ctype_b_loc
())[(int) ((c))] & (unsigned short int) _ISgraph)) { *dst
++ = '\\'; *dst++ = c; continue; } } } if (isextra || ((c &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *
dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) +
'0'; *dst++ = (c & 07) + '0'; } else { if ((flag & 0x40
) == 0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst
++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0)
; \
130 } \
131while (/*CONSTCOND*/0)
132
133/*
134 * This is SVIS, the central macro of vis.
135 * dst: Pointer to the destination buffer
136 * c: Character to encode
137 * flag: Flag word
138 * nextc: The character following 'c'
139 * extra: Pointer to the list of extra characters to be
140 * backslash-protected.
141 */
142#define SVIS(dst, c, flag, nextc, extra)do { int isextra; isextra = strchr(extra, c) != ((void*)0); if
(!isextra && (((c) & ~0x7f) == 0) && (((
*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
) || (c == ' ' || c == '\t' || c == '\n') || ((flag & 0x20
) && (c == '\b' || c == '\a' || c == '\r')))) { *dst++
= c; break; } if (flag & 0x02) { switch (c) { case '\n':
*dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(nextc)) >= '0' && ((u_char)(nextc)) <= '7')) {
*dst++ = '0'; *dst++ = '0'; } continue; default: if (((*__ctype_b_loc
())[(int) ((c))] & (unsigned short int) _ISgraph)) { *dst
++ = '\\'; *dst++ = c; continue; } } } if (isextra || ((c &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *
dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) +
'0'; *dst++ = (c & 07) + '0'; } else { if ((flag & 0x40
) == 0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst
++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0)
\
143do { \
144 int isextra; \
145 isextra = strchr(extra, c) != NULL((void*)0); \
146 if (!isextra && isascii(c)(((c) & ~0x7f) == 0) && (isgraph(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
)
|| iswhite(c)(c == ' ' || c == '\t' || c == '\n') || \
147 ((flag & VIS_SAFE0x20) && issafe(c)(c == '\b' || c == '\a' || c == '\r')))) { \
148 *dst++ = c; \
149 break; \
150 } \
151 if (flag & VIS_CSTYLE0x02) { \
152 switch (c) { \
153 case '\n': \
154 *dst++ = '\\'; *dst++ = 'n'; \
155 continue; \
156 case '\r': \
157 *dst++ = '\\'; *dst++ = 'r'; \
158 continue; \
159 case '\b': \
160 *dst++ = '\\'; *dst++ = 'b'; \
161 continue; \
162 case BELL'\a': \
163 *dst++ = '\\'; *dst++ = 'a'; \
164 continue; \
165 case '\v': \
166 *dst++ = '\\'; *dst++ = 'v'; \
167 continue; \
168 case '\t': \
169 *dst++ = '\\'; *dst++ = 't'; \
170 continue; \
171 case '\f': \
172 *dst++ = '\\'; *dst++ = 'f'; \
173 continue; \
174 case ' ': \
175 *dst++ = '\\'; *dst++ = 's'; \
176 continue; \
177 case '\0': \
178 *dst++ = '\\'; *dst++ = '0'; \
179 if (isoctal(nextc)(((u_char)(nextc)) >= '0' && ((u_char)(nextc)) <=
'7')
) { \
180 *dst++ = '0'; \
181 *dst++ = '0'; \
182 } \
183 continue; \
184 default: \
185 if (isgraph(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
)
) { \
186 *dst++ = '\\'; *dst++ = c; \
187 continue; \
188 } \
189 } \
190 } \
191 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL0x01)) { \
192 *dst++ = '\\'; \
193 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
194 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
195 *dst++ = (c & 07) + '0'; \
196 } else { \
197 if ((flag & VIS_NOSLASH0x40) == 0) *dst++ = '\\'; \
198 if (c & 0200) { \
199 c &= 0177; *dst++ = 'M'; \
200 } \
201 if (iscntrl(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _IScntrl
)
) { \
202 *dst++ = '^'; \
203 if (c == 0177) \
204 *dst++ = '?'; \
205 else \
206 *dst++ = c + '@'; \
207 } else { \
208 *dst++ = '-'; *dst++ = c; \
209 } \
210 } \
211} while (/*CONSTCOND*/0)
212
213
214/*
215 * svis - visually encode characters, also encoding the characters
216 * pointed to by `extra'
217 */
218char *
219svis(char *dst, int c, int flag, int nextc, const char *extra)
220{
221 char *nextra = NULL((void*)0);
222
223 _DIAGASSERT(dst != NULL);
224 _DIAGASSERT(extra != NULL);
225 MAKEEXTRALIST(flag, nextra, extra)do { const char *o = extra; char *e; while (*o++) continue; nextra
= malloc((size_t)((o - extra) + 5)); if (!nextra) break; for
(o = extra, e = nextra; (*e++ = *o++) != '\0';) continue; e--
; if (flag & 0x04) *e++ = ' '; if (flag & 0x08) *e++ =
'\t'; if (flag & 0x10) *e++ = '\n'; if ((flag & 0x40
) == 0) *e++ = '\\'; *e = '\0'; } while ( 0)
;
226 if (!nextra) {
227 *dst = '\0'; /* can't create nextra, return "" */
228 return dst;
229 }
230 if (flag & VIS_HTTPSTYLE0x80)
231 HVIS(dst, c, flag, nextc, nextra)do if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISalnum) || strchr("$-_.+!*'(),"
, c) != ((void*)0)) { *dst++ = '%'; *dst++ = "0123456789abcdef"
[((unsigned int)c >> 4) & 0xf]; *dst++ = "0123456789abcdef"
[(unsigned int)c & 0xf]; } else { do { int isextra; isextra
= strchr(nextra, c) != ((void*)0); if (!isextra && (
((c) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISgraph) || (c == ' ' ||
c == '\t' || c == '\n') || ((flag & 0x20) && (c ==
'\b' || c == '\a' || c == '\r')))) { *dst++ = c; break; } if
(flag & 0x02) { switch (c) { case '\n': *dst++ = '\\'; *
dst++ = 'n'; continue; case '\r': *dst++ = '\\'; *dst++ = 'r'
; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'; continue;
case '\a': *dst++ = '\\'; *dst++ = 'a'; continue; case '\v':
*dst++ = '\\'; *dst++ = 'v'; continue; case '\t': *dst++ = '\\'
; *dst++ = 't'; continue; case '\f': *dst++ = '\\'; *dst++ = 'f'
; continue; case ' ': *dst++ = '\\'; *dst++ = 's'; continue; case
'\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char)(nextc)) >=
'0' && ((u_char)(nextc)) <= '7')) { *dst++ = '0';
*dst++ = '0'; } continue; default: if (((*__ctype_b_loc ())[
(int) ((c))] & (unsigned short int) _ISgraph)) { *dst++ =
'\\'; *dst++ = c; continue; } } } if (isextra || ((c & 0177
) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ = (u_char
)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; *
dst++ = (c & 07) + '0'; } else { if ((flag & 0x40) ==
0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst++ =
'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0); } while ( 0)
;
232 else
233 SVIS(dst, c, flag, nextc, nextra)do { int isextra; isextra = strchr(nextra, c) != ((void*)0); if
(!isextra && (((c) & ~0x7f) == 0) && (((
*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
) || (c == ' ' || c == '\t' || c == '\n') || ((flag & 0x20
) && (c == '\b' || c == '\a' || c == '\r')))) { *dst++
= c; break; } if (flag & 0x02) { switch (c) { case '\n':
*dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(nextc)) >= '0' && ((u_char)(nextc)) <= '7')) {
*dst++ = '0'; *dst++ = '0'; } continue; default: if (((*__ctype_b_loc
())[(int) ((c))] & (unsigned short int) _ISgraph)) { *dst
++ = '\\'; *dst++ = c; continue; } } } if (isextra || ((c &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *
dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) +
'0'; *dst++ = (c & 07) + '0'; } else { if ((flag & 0x40
) == 0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst
++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0)
;
234 free(nextra);
235 *dst = '\0';
236 return dst;
237}
238
239
240/*
241 * strsvis, strsvisx - visually encode characters from src into dst
242 *
243 * Extra is a pointer to a \0-terminated list of characters to
244 * be encoded, too. These functions are useful e. g. to
245 * encode strings in such a way so that they are not interpreted
246 * by a shell.
247 *
248 * Dst must be 4 times the size of src to account for possible
249 * expansion. The length of dst, not including the trailing NULL,
250 * is returned.
251 *
252 * Strsvisx encodes exactly len bytes from src into dst.
253 * This is useful for encoding a block of data.
254 */
255int
256strsvis(char *dst, const char *csrc, int flag, const char *extra)
257{
258 int c;
259 char *start;
260 char *nextra = NULL((void*)0);
261 const unsigned char *src = (const unsigned char *)csrc;
262
263 _DIAGASSERT(dst != NULL);
264 _DIAGASSERT(src != NULL);
265 _DIAGASSERT(extra != NULL);
266 MAKEEXTRALIST(flag, nextra, extra)do { const char *o = extra; char *e; while (*o++) continue; nextra
= malloc((size_t)((o - extra) + 5)); if (!nextra) break; for
(o = extra, e = nextra; (*e++ = *o++) != '\0';) continue; e--
; if (flag & 0x04) *e++ = ' '; if (flag & 0x08) *e++ =
'\t'; if (flag & 0x10) *e++ = '\n'; if ((flag & 0x40
) == 0) *e++ = '\\'; *e = '\0'; } while ( 0)
;
267 if (!nextra) {
268 *dst = '\0'; /* can't create nextra, return "" */
269 return 0;
270 }
271 if (flag & VIS_HTTPSTYLE0x80) {
272 for (start = dst; (c = *src++) != '\0'; /* empty */)
273 HVIS(dst, c, flag, *src, nextra)do if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISalnum) || strchr("$-_.+!*'(),"
, c) != ((void*)0)) { *dst++ = '%'; *dst++ = "0123456789abcdef"
[((unsigned int)c >> 4) & 0xf]; *dst++ = "0123456789abcdef"
[(unsigned int)c & 0xf]; } else { do { int isextra; isextra
= strchr(nextra, c) != ((void*)0); if (!isextra && (
((c) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISgraph) || (c == ' ' ||
c == '\t' || c == '\n') || ((flag & 0x20) && (c ==
'\b' || c == '\a' || c == '\r')))) { *dst++ = c; break; } if
(flag & 0x02) { switch (c) { case '\n': *dst++ = '\\'; *
dst++ = 'n'; continue; case '\r': *dst++ = '\\'; *dst++ = 'r'
; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'; continue;
case '\a': *dst++ = '\\'; *dst++ = 'a'; continue; case '\v':
*dst++ = '\\'; *dst++ = 'v'; continue; case '\t': *dst++ = '\\'
; *dst++ = 't'; continue; case '\f': *dst++ = '\\'; *dst++ = 'f'
; continue; case ' ': *dst++ = '\\'; *dst++ = 's'; continue; case
'\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char)(*src)) >=
'0' && ((u_char)(*src)) <= '7')) { *dst++ = '0'; *
dst++ = '0'; } continue; default: if (((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISgraph)) { *dst++ = '\\'
; *dst++ = c; continue; } } } if (isextra || ((c & 0177) ==
' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ = (u_char
)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; *
dst++ = (c & 07) + '0'; } else { if ((flag & 0x40) ==
0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst++ =
'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0); } while ( 0)
;
274 } else {
275 for (start = dst; (c = *src++) != '\0'; /* empty */)
276 SVIS(dst, c, flag, *src, nextra)do { int isextra; isextra = strchr(nextra, c) != ((void*)0); if
(!isextra && (((c) & ~0x7f) == 0) && (((
*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
) || (c == ' ' || c == '\t' || c == '\n') || ((flag & 0x20
) && (c == '\b' || c == '\a' || c == '\r')))) { *dst++
= c; break; } if (flag & 0x02) { switch (c) { case '\n':
*dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(*src)) >= '0' && ((u_char)(*src)) <= '7')) { *
dst++ = '0'; *dst++ = '0'; } continue; default: if (((*__ctype_b_loc
())[(int) ((c))] & (unsigned short int) _ISgraph)) { *dst
++ = '\\'; *dst++ = c; continue; } } } if (isextra || ((c &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; *
dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) +
'0'; *dst++ = (c & 07) + '0'; } else { if ((flag & 0x40
) == 0) *dst++ = '\\'; if (c & 0200) { c &= 0177; *dst
++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _IScntrl)) { *dst++ = '^'; if (c == 0177) *dst++ =
'?'; else *dst++ = c + '@'; } else { *dst++ = '-'; *dst++ = c
; } } } while ( 0)
;
277 }
278 free(nextra);
279 *dst = '\0';
280 return (dst - start);
281}
282
283
284int
285strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
286{
287 unsigned char c;
288 char *start;
289 char *nextra = NULL((void*)0);
290 const unsigned char *src = (const unsigned char *)csrc;
291
292 _DIAGASSERT(dst != NULL);
293 _DIAGASSERT(src != NULL);
294 _DIAGASSERT(extra != NULL);
295 MAKEEXTRALIST(flag, nextra, extra)do { const char *o = extra; char *e; while (*o++) continue; nextra
= malloc((size_t)((o - extra) + 5)); if (!nextra) break; for
(o = extra, e = nextra; (*e++ = *o++) != '\0';) continue; e--
; if (flag & 0x04) *e++ = ' '; if (flag & 0x08) *e++ =
'\t'; if (flag & 0x10) *e++ = '\n'; if ((flag & 0x40
) == 0) *e++ = '\\'; *e = '\0'; } while ( 0)
;
296 if (! nextra) {
297 *dst = '\0'; /* can't create nextra, return "" */
298 return 0;
299 }
300
301 if (flag & VIS_HTTPSTYLE0x80) {
302 for (start = dst; len > 0; len--) {
303 c = *src++;
304 HVIS(dst, c, flag, len ? *src : '\0', nextra)do if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISalnum) || strchr("$-_.+!*'(),"
, c) != ((void*)0)) { *dst++ = '%'; *dst++ = "0123456789abcdef"
[((unsigned int)c >> 4) & 0xf]; *dst++ = "0123456789abcdef"
[(unsigned int)c & 0xf]; } else { do { int isextra; isextra
= strchr(nextra, c) != ((void*)0); if (!isextra && (
((c) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int
) ((c))] & (unsigned short int) _ISgraph) || (c == ' ' ||
c == '\t' || c == '\n') || ((flag & 0x20) && (c ==
'\b' || c == '\a' || c == '\r')))) { *dst++ = c; break; } if
(flag & 0x02) { switch (c) { case '\n': *dst++ = '\\'; *
dst++ = 'n'; continue; case '\r': *dst++ = '\\'; *dst++ = 'r'
; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'; continue;
case '\a': *dst++ = '\\'; *dst++ = 'a'; continue; case '\v':
*dst++ = '\\'; *dst++ = 'v'; continue; case '\t': *dst++ = '\\'
; *dst++ = 't'; continue; case '\f': *dst++ = '\\'; *dst++ = 'f'
; continue; case ' ': *dst++ = '\\'; *dst++ = 's'; continue; case
'\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char)(len ? *src
: '\0')) >= '0' && ((u_char)(len ? *src : '\0')) <=
'7')) { *dst++ = '0'; *dst++ = '0'; } continue; default: if (
((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
)) { *dst++ = '\\'; *dst++ = c; continue; } } } if (isextra ||
((c & 0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'
; *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03
) + '0'; *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) &
07) + '0'; *dst++ = (c & 07) + '0'; } else { if ((flag &
0x40) == 0) *dst++ = '\\'; if (c & 0200) { c &= 0177
; *dst++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((c))] &
(unsigned short int) _IScntrl)) { *dst++ = '^'; if (c == 0177
) *dst++ = '?'; else *dst++ = c + '@'; } else { *dst++ = '-';
*dst++ = c; } } } while ( 0); } while ( 0)
;
305 }
306 } else {
307 for (start = dst; len > 0; len--) {
308 c = *src++;
309 SVIS(dst, c, flag, len ? *src : '\0', nextra)do { int isextra; isextra = strchr(nextra, c) != ((void*)0); if
(!isextra && (((c) & ~0x7f) == 0) && (((
*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISgraph
) || (c == ' ' || c == '\t' || c == '\n') || ((flag & 0x20
) && (c == '\b' || c == '\a' || c == '\r')))) { *dst++
= c; break; } if (flag & 0x02) { switch (c) { case '\n':
*dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(len ? *src : '\0')) >= '0' && ((u_char)(len ? *src
: '\0')) <= '7')) { *dst++ = '0'; *dst++ = '0'; } continue
; default: if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned
short int) _ISgraph)) { *dst++ = '\\'; *dst++ = c; continue;
} } } if (isextra || ((c & 0177) == ' ') || (flag & 0x01
)) { *dst++ = '\\'; *dst++ = (u_char)(((u_int32_t)(u_char)c >>
6) & 03) + '0'; *dst++ = (u_char)(((u_int32_t)(u_char)c >>
3) & 07) + '0'; *dst++ = (c & 07) + '0'; } else { if
((flag & 0x40) == 0) *dst++ = '\\'; if (c & 0200) { c
&= 0177; *dst++ = 'M'; } if (((*__ctype_b_loc ())[(int) (
(c))] & (unsigned short int) _IScntrl)) { *dst++ = '^'; if
(c == 0177) *dst++ = '?'; else *dst++ = c + '@'; } else { *dst
++ = '-'; *dst++ = c; } } } while ( 0)
;
310 }
311 }
312 free(nextra);
313 *dst = '\0';
314 return (dst - start);
315}
316#endif
317
318#if !HAVE_VIS
319/*
320 * vis - visually encode characters
321 */
322char *
323vis(char *dst, int c, int flag, int nextc)
324{
325 char *extra = NULL((void*)0);
326 unsigned char uc = (unsigned char)c;
327
328 _DIAGASSERT(dst != NULL);
329
330 MAKEEXTRALIST(flag, extra, "")do { const char *o = ""; char *e; while (*o++) continue; extra
= malloc((size_t)((o - "") + 5)); if (!extra) break; for (o =
"", e = extra; (*e++ = *o++) != '\0';) continue; e--; if (flag
& 0x04) *e++ = ' '; if (flag & 0x08) *e++ = '\t'; if
(flag & 0x10) *e++ = '\n'; if ((flag & 0x40) == 0) *
e++ = '\\'; *e = '\0'; } while ( 0)
;
331 if (! extra) {
332 *dst = '\0'; /* can't create extra, return "" */
333 return dst;
334 }
335 if (flag & VIS_HTTPSTYLE0x80)
336 HVIS(dst, uc, flag, nextc, extra)do if (!(((uc) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int
) ((uc))] & (unsigned short int) _ISalnum) || strchr("$-_.+!*'(),"
, uc) != ((void*)0)) { *dst++ = '%'; *dst++ = "0123456789abcdef"
[((unsigned int)uc >> 4) & 0xf]; *dst++ = "0123456789abcdef"
[(unsigned int)uc & 0xf]; } else { do { int isextra; isextra
= strchr(extra, uc) != ((void*)0); if (!isextra && (
((uc) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int
) ((uc))] & (unsigned short int) _ISgraph) || (uc == ' ' ||
uc == '\t' || uc == '\n') || ((flag & 0x20) && (
uc == '\b' || uc == '\a' || uc == '\r')))) { *dst++ = uc; break
; } if (flag & 0x02) { switch (uc) { case '\n': *dst++ = '\\'
; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'; *dst++ = 'r'
; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'; continue;
case '\a': *dst++ = '\\'; *dst++ = 'a'; continue; case '\v':
*dst++ = '\\'; *dst++ = 'v'; continue; case '\t': *dst++ = '\\'
; *dst++ = 't'; continue; case '\f': *dst++ = '\\'; *dst++ = 'f'
; continue; case ' ': *dst++ = '\\'; *dst++ = 's'; continue; case
'\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char)(nextc)) >=
'0' && ((u_char)(nextc)) <= '7')) { *dst++ = '0';
*dst++ = '0'; } continue; default: if (((*__ctype_b_loc ())[
(int) ((uc))] & (unsigned short int) _ISgraph)) { *dst++ =
'\\'; *dst++ = uc; continue; } } } if (isextra || ((uc &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)uc >> 6) & 03) + '0';
*dst++ = (u_char)(((u_int32_t)(u_char)uc >> 3) & 07
) + '0'; *dst++ = (uc & 07) + '0'; } else { if ((flag &
0x40) == 0) *dst++ = '\\'; if (uc & 0200) { uc &= 0177
; *dst++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((uc))] &
(unsigned short int) _IScntrl)) { *dst++ = '^'; if (uc == 0177
) *dst++ = '?'; else *dst++ = uc + '@'; } else { *dst++ = '-'
; *dst++ = uc; } } } while ( 0); } while ( 0)
;
337 else
338 SVIS(dst, uc, flag, nextc, extra)do { int isextra; isextra = strchr(extra, uc) != ((void*)0); if
(!isextra && (((uc) & ~0x7f) == 0) && ((
(*__ctype_b_loc ())[(int) ((uc))] & (unsigned short int) _ISgraph
) || (uc == ' ' || uc == '\t' || uc == '\n') || ((flag & 0x20
) && (uc == '\b' || uc == '\a' || uc == '\r')))) { *dst
++ = uc; break; } if (flag & 0x02) { switch (uc) { case '\n'
: *dst++ = '\\'; *dst++ = 'n'; continue; case '\r': *dst++ = '\\'
; *dst++ = 'r'; continue; case '\b': *dst++ = '\\'; *dst++ = 'b'
; continue; case '\a': *dst++ = '\\'; *dst++ = 'a'; continue;
case '\v': *dst++ = '\\'; *dst++ = 'v'; continue; case '\t':
*dst++ = '\\'; *dst++ = 't'; continue; case '\f': *dst++ = '\\'
; *dst++ = 'f'; continue; case ' ': *dst++ = '\\'; *dst++ = 's'
; continue; case '\0': *dst++ = '\\'; *dst++ = '0'; if ((((u_char
)(nextc)) >= '0' && ((u_char)(nextc)) <= '7')) {
*dst++ = '0'; *dst++ = '0'; } continue; default: if (((*__ctype_b_loc
())[(int) ((uc))] & (unsigned short int) _ISgraph)) { *dst
++ = '\\'; *dst++ = uc; continue; } } } if (isextra || ((uc &
0177) == ' ') || (flag & 0x01)) { *dst++ = '\\'; *dst++ =
(u_char)(((u_int32_t)(u_char)uc >> 6) & 03) + '0';
*dst++ = (u_char)(((u_int32_t)(u_char)uc >> 3) & 07
) + '0'; *dst++ = (uc & 07) + '0'; } else { if ((flag &
0x40) == 0) *dst++ = '\\'; if (uc & 0200) { uc &= 0177
; *dst++ = 'M'; } if (((*__ctype_b_loc ())[(int) ((uc))] &
(unsigned short int) _IScntrl)) { *dst++ = '^'; if (uc == 0177
) *dst++ = '?'; else *dst++ = uc + '@'; } else { *dst++ = '-'
; *dst++ = uc; } } } while ( 0)
;
339 free(extra);
340 *dst = '\0';
341 return dst;
342}
343
344
345/*
346 * strvis, strvisx - visually encode characters from src into dst
347 *
348 * Dst must be 4 times the size of src to account for possible
349 * expansion. The length of dst, not including the trailing NULL,
350 * is returned.
351 *
352 * Strvisx encodes exactly len bytes from src into dst.
353 * This is useful for encoding a block of data.
354 */
355int
356strvis(char *dst, const char *src, int flag)
357{
358 char *extra = NULL((void*)0);
359 int rv;
360
361 MAKEEXTRALIST(flag, extra, "")do { const char *o = ""; char *e; while (*o++) continue; extra
= malloc((size_t)((o - "") + 5)); if (!extra) break; for (o =
"", e = extra; (*e++ = *o++) != '\0';) continue; e--; if (flag
& 0x04) *e++ = ' '; if (flag & 0x08) *e++ = '\t'; if
(flag & 0x10) *e++ = '\n'; if ((flag & 0x40) == 0) *
e++ = '\\'; *e = '\0'; } while ( 0)
;
Within the expansion of the macro 'MAKEEXTRALIST':
a
Subtraction of two pointers that do not point to the same memory chunk may cause incorrect result
362 if (!extra) {
363 *dst = '\0'; /* can't create extra, return "" */
364 return 0;
365 }
366 rv = strsvis(dst, src, flag, extra);
367 free(extra);
368 return rv;
369}
370
371
372int
373strvisx(char *dst, const char *src, size_t len, int flag)
374{
375 char *extra = NULL((void*)0);
376 int rv;
377
378 MAKEEXTRALIST(flag, extra, "")do { const char *o = ""; char *e; while (*o++) continue; extra
= malloc((size_t)((o - "") + 5)); if (!extra) break; for (o =
"", e = extra; (*e++ = *o++) != '\0';) continue; e--; if (flag
& 0x04) *e++ = ' '; if (flag & 0x08) *e++ = '\t'; if
(flag & 0x10) *e++ = '\n'; if ((flag & 0x40) == 0) *
e++ = '\\'; *e = '\0'; } while ( 0)
;
379 if (!extra) {
380 *dst = '\0'; /* can't create extra, return "" */
381 return 0;
382 }
383 rv = strsvisx(dst, src, len, flag, extra);
384 free(extra);
385 return rv;
386}
387#endif