OpenCoverage

vis.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/openbsd-compat/vis.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */-
2/*--
3 * Copyright (c) 1989, 1993-
4 * The Regents of the University of California. All rights reserved.-
5 *-
6 * Redistribution and use in source and binary forms, with or without-
7 * modification, are permitted provided that the following conditions-
8 * are met:-
9 * 1. Redistributions of source code must retain the above copyright-
10 * notice, this list of conditions and the following disclaimer.-
11 * 2. Redistributions in binary form must reproduce the above copyright-
12 * notice, this list of conditions and the following disclaimer in the-
13 * documentation and/or other materials provided with the distribution.-
14 * 3. Neither the name of the University nor the names of its contributors-
15 * may be used to endorse or promote products derived from this software-
16 * without specific prior written permission.-
17 *-
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND-
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE-
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE-
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE-
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL-
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS-
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)-
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT-
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY-
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF-
28 * SUCH DAMAGE.-
29 */-
30-
31/* OPENBSD ORIGINAL: lib/libc/gen/vis.c */-
32-
33#include "includes.h"-
34#if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS)-
35-
36#include <sys/types.h>-
37#include <errno.h>-
38#include <ctype.h>-
39#include <limits.h>-
40#include <string.h>-
41#include <stdlib.h>-
42-
43#include "vis.h"-
44-
45#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')-
46#define isvisible(c,flag) \-
47 (((c) == '\\' || (flag & VIS_ALL) == 0) && \-
48 (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \-
49 (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \-
50 (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \-
51 ((flag & VIS_SP) == 0 && (c) == ' ') || \-
52 ((flag & VIS_TAB) == 0 && (c) == '\t') || \-
53 ((flag & VIS_NL) == 0 && (c) == '\n') || \-
54 ((flag & VIS_SAFE) && ((c) == '\b' || \-
55 (c) == '\007' || (c) == '\r' || \-
56 isgraph((u_char)(c))))))-
57-
58/*-
59 * vis - visually encode characters-
60 */-
61char *-
62vis(char *dst, int c, int flag, int nextc)-
63{-
64 if (isvisible(c, flag)) {
(c) == '\\'Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_utf8
(flag & 0x400) == 0Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_utf8
(u_int)(c) <= (0x7f * 2 + 1)Description
TRUEnever evaluated
FALSEnever evaluated
((( (u_char)(c...& ~0x7f) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
(c) != '*'Description
TRUEnever evaluated
FALSEnever evaluated
(c) != '?'Description
TRUEnever evaluated
FALSEnever evaluated
(c) != '['Description
TRUEnever evaluated
FALSEnever evaluated
(c) != '#'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x100) == 0Description
TRUEnever evaluated
FALSEnever evaluated
((*__ctype_b_l...int) _ISgraph)Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x04) == 0Description
TRUEnever evaluated
FALSEnever evaluated
(c) == ' 'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x08) == 0Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\t'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x10) == 0Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\n'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x20)Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\b'Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\007'Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\r'Description
TRUEnever evaluated
FALSEnever evaluated
((*__ctype_b_l...int) _ISgraph)Description
TRUEnever evaluated
FALSEnever evaluated
0-12
65 if ((c == '"' && (flag & VIS_DQ) != 0) ||
c == '"'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x200) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
66 (c == '\\' && (flag & VIS_NOSLASH) == 0))
c == '\\'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x40) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
67 *dst++ = '\\';
never executed: *dst++ = '\\';
0
68 *dst++ = c;-
69 *dst = '\0';-
70 return (dst);
never executed: return (dst);
0
71 }-
72-
73 if (flag & VIS_CSTYLE) {
flag & 0x02Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_utf8
0-12
74 switch(c) {-
75 case '\n':
never executed: case '\n':
0
76 *dst++ = '\\';-
77 *dst++ = 'n';-
78 goto done;
never executed: goto done;
0
79 case '\r':
never executed: case '\r':
0
80 *dst++ = '\\';-
81 *dst++ = 'r';-
82 goto done;
never executed: goto done;
0
83 case '\b':
never executed: case '\b':
0
84 *dst++ = '\\';-
85 *dst++ = 'b';-
86 goto done;
never executed: goto done;
0
87 case '\a':
never executed: case '\a':
0
88 *dst++ = '\\';-
89 *dst++ = 'a';-
90 goto done;
never executed: goto done;
0
91 case '\v':
never executed: case '\v':
0
92 *dst++ = '\\';-
93 *dst++ = 'v';-
94 goto done;
never executed: goto done;
0
95 case '\t':
never executed: case '\t':
0
96 *dst++ = '\\';-
97 *dst++ = 't';-
98 goto done;
never executed: goto done;
0
99 case '\f':
never executed: case '\f':
0
100 *dst++ = '\\';-
101 *dst++ = 'f';-
102 goto done;
never executed: goto done;
0
103 case ' ':
never executed: case ' ':
0
104 *dst++ = '\\';-
105 *dst++ = 's';-
106 goto done;
never executed: goto done;
0
107 case '\0':
never executed: case '\0':
0
108 *dst++ = '\\';-
109 *dst++ = '0';-
110 if (isoctal(nextc)) {
((u_char)(nextc)) >= '0'Description
TRUEnever evaluated
FALSEnever evaluated
((u_char)(nextc)) <= '7'Description
TRUEnever evaluated
FALSEnever evaluated
0
111 *dst++ = '0';-
112 *dst++ = '0';-
113 }
never executed: end of block
0
114 goto done;
never executed: goto done;
0
115 }-
116 }
never executed: end of block
0
117 if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
((c & 0177) == ' ')Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_utf8
(flag & 0x01)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • test_utf8
FALSEnever evaluated
0-12
118 ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
(flag & 0x100)Description
TRUEnever evaluated
FALSEnever evaluated
c == '*'Description
TRUEnever evaluated
FALSEnever evaluated
c == '?'Description
TRUEnever evaluated
FALSEnever evaluated
c == '['Description
TRUEnever evaluated
FALSEnever evaluated
c == '#'Description
TRUEnever evaluated
FALSEnever evaluated
0
119 *dst++ = '\\';-
120 *dst++ = ((u_char)c >> 6 & 07) + '0';-
121 *dst++ = ((u_char)c >> 3 & 07) + '0';-
122 *dst++ = ((u_char)c & 07) + '0';-
123 goto done;
executed 12 times by 1 test: goto done;
Executed by:
  • test_utf8
12
124 }-
125 if ((flag & VIS_NOSLASH) == 0)
(flag & 0x40) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
126 *dst++ = '\\';
never executed: *dst++ = '\\';
0
127 if (c & 0200) {
c & 0200Description
TRUEnever evaluated
FALSEnever evaluated
0
128 c &= 0177;-
129 *dst++ = 'M';-
130 }
never executed: end of block
0
131 if (iscntrl((u_char)c)) {
((*__ctype_b_l...int) _IScntrl)Description
TRUEnever evaluated
FALSEnever evaluated
0
132 *dst++ = '^';-
133 if (c == 0177)
c == 0177Description
TRUEnever evaluated
FALSEnever evaluated
0
134 *dst++ = '?';
never executed: *dst++ = '?';
0
135 else-
136 *dst++ = c + '@';
never executed: *dst++ = c + '@';
0
137 } else {-
138 *dst++ = '-';-
139 *dst++ = c;-
140 }
never executed: end of block
0
141done:
code before this statement never executed: done:
0
142 *dst = '\0';-
143 return (dst);
executed 12 times by 1 test: return (dst);
Executed by:
  • test_utf8
12
144}-
145DEF_WEAK(vis);-
146-
147/*-
148 * strvis, strnvis, strvisx - visually encode characters from src into dst-
149 * -
150 * Dst must be 4 times the size of src to account for possible-
151 * expansion. The length of dst, not including the trailing NULL,-
152 * is returned. -
153 *-
154 * Strnvis will write no more than siz-1 bytes (and will NULL terminate).-
155 * The number of bytes needed to fully encode the string is returned.-
156 *-
157 * Strvisx encodes exactly len bytes from src into dst.-
158 * This is useful for encoding a block of data.-
159 */-
160int-
161strvis(char *dst, const char *src, int flag)-
162{-
163 char c;-
164 char *start;-
165-
166 for (start = dst; (c = *src);)
(c = *src)Description
TRUEnever evaluated
FALSEnever evaluated
0
167 dst = vis(dst, c, flag, *++src);
never executed: dst = vis(dst, c, flag, *++src);
0
168 *dst = '\0';-
169 return (dst - start);
never executed: return (dst - start);
0
170}-
171DEF_WEAK(strvis);-
172-
173int-
174strnvis(char *dst, const char *src, size_t siz, int flag)-
175{-
176 char *start, *end;-
177 char tbuf[5];-
178 int c, i;-
179-
180 i = 0;-
181 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
(c = *src)Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
dst < endDescription
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
0-4200
182 if (isvisible(c, flag)) {
(c) == '\\'Description
TRUEnever evaluated
FALSEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
(flag & 0x400) == 0Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(u_int)(c) <= (0x7f * 2 + 1)Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
((( (u_char)(c...& ~0x7f) == 0)Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(c) != '*'Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(c) != '?'Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(c) != '['Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(c) != '#'Description
TRUEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(flag & 0x100) == 0Description
TRUEnever evaluated
FALSEnever evaluated
((*__ctype_b_l...int) _ISgraph)Description
TRUEevaluated 3720 times by 1 test
Evaluated by:
  • test_kex
FALSEevaluated 480 times by 1 test
Evaluated by:
  • test_kex
(flag & 0x04) == 0Description
TRUEevaluated 480 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(c) == ' 'Description
TRUEevaluated 480 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
(flag & 0x08) == 0Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\t'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x10) == 0Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\n'Description
TRUEnever evaluated
FALSEnever evaluated
(flag & 0x20)Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\b'Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\007'Description
TRUEnever evaluated
FALSEnever evaluated
(c) == '\r'Description
TRUEnever evaluated
FALSEnever evaluated
((*__ctype_b_l...int) _ISgraph)Description
TRUEnever evaluated
FALSEnever evaluated
0-4200
183 if ((c == '"' && (flag & VIS_DQ) != 0) ||
c == '"'Description
TRUEnever evaluated
FALSEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
(flag & 0x200) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0-4200
184 (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
c == '\\'Description
TRUEnever evaluated
FALSEevaluated 4200 times by 1 test
Evaluated by:
  • test_kex
(flag & 0x40) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-4200
185 /* need space for the extra '\\' */-
186 if (dst + 1 >= end) {
dst + 1 >= endDescription
TRUEnever evaluated
FALSEnever evaluated
0
187 i = 2;-
188 break;
never executed: break;
0
189 }-
190 *dst++ = '\\';-
191 }
never executed: end of block
0
192 i = 1;-
193 *dst++ = c;-
194 src++;-
195 } else {
executed 4200 times by 1 test: end of block
Executed by:
  • test_kex
4200
196 i = vis(tbuf, c, flag, *++src) - tbuf;-
197 if (dst + i <= end) {
dst + i <= endDescription
TRUEnever evaluated
FALSEnever evaluated
0
198 memcpy(dst, tbuf, i);-
199 dst += i;-
200 } else {
never executed: end of block
0
201 src--;-
202 break;
never executed: break;
0
203 }-
204 }-
205 }-
206 if (siz > 0)
siz > 0Description
TRUEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
0-40
207 *dst = '\0';
executed 40 times by 1 test: *dst = '\0';
Executed by:
  • test_kex
40
208 if (dst + i > end) {
dst + i > endDescription
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
209 /* adjust return value for truncation */-
210 while ((c = *src))
(c = *src)Description
TRUEnever evaluated
FALSEnever evaluated
0
211 dst += vis(tbuf, c, flag, *++src) - tbuf;
never executed: dst += vis(tbuf, c, flag, *++src) - tbuf;
0
212 }
never executed: end of block
0
213 return (dst - start);
executed 40 times by 1 test: return (dst - start);
Executed by:
  • test_kex
40
214}-
215-
216int-
217stravis(char **outp, const char *src, int flag)-
218{-
219 char *buf;-
220 int len, serrno;-
221-
222 buf = reallocarray(NULL, 4, strlen(src) + 1);-
223 if (buf == NULL)
buf == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
224 return -1;
never executed: return -1;
0
225 len = strvis(buf, src, flag);-
226 serrno = errno;-
227 *outp = realloc(buf, len + 1);-
228 if (*outp == NULL) {
*outp == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
229 *outp = buf;-
230 errno = serrno;-
231 }
never executed: end of block
0
232 return (len);
never executed: return (len);
0
233}-
234-
235int-
236strvisx(char *dst, const char *src, size_t len, int flag)-
237{-
238 char c;-
239 char *start;-
240-
241 for (start = dst; len > 1; len--) {
len > 1Description
TRUEnever evaluated
FALSEnever evaluated
0
242 c = *src;-
243 dst = vis(dst, c, flag, *++src);-
244 }
never executed: end of block
0
245 if (len)
lenDescription
TRUEnever evaluated
FALSEnever evaluated
0
246 dst = vis(dst, *src, flag, '\0');
never executed: dst = vis(dst, *src, flag, '\0');
0
247 *dst = '\0';-
248 return (dst - start);
never executed: return (dst - start);
0
249}-
250-
251#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2