GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: display.c,v 1.25 2016/08/24 03:13:45 guenther Exp $ */ |
||
2 |
/* $NetBSD: display.c,v 1.12 2001/12/07 15:14:29 bjh21 Exp $ */ |
||
3 |
|||
4 |
/* |
||
5 |
* Copyright (c) 1989, 1993 |
||
6 |
* The Regents of the University of California. All rights reserved. |
||
7 |
* |
||
8 |
* Redistribution and use in source and binary forms, with or without |
||
9 |
* modification, are permitted provided that the following conditions |
||
10 |
* are met: |
||
11 |
* 1. Redistributions of source code must retain the above copyright |
||
12 |
* notice, this list of conditions and the following disclaimer. |
||
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
14 |
* notice, this list of conditions and the following disclaimer in the |
||
15 |
* documentation and/or other materials provided with the distribution. |
||
16 |
* 3. Neither the name of the University nor the names of its contributors |
||
17 |
* may be used to endorse or promote products derived from this software |
||
18 |
* without specific prior written permission. |
||
19 |
* |
||
20 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
30 |
* SUCH DAMAGE. |
||
31 |
*/ |
||
32 |
|||
33 |
#include <sys/stat.h> |
||
34 |
|||
35 |
#include <ctype.h> |
||
36 |
#include <err.h> |
||
37 |
#include <errno.h> |
||
38 |
#include <stdio.h> |
||
39 |
#include <stdlib.h> |
||
40 |
#include <string.h> |
||
41 |
#include <unistd.h> |
||
42 |
|||
43 |
#include "hexdump.h" |
||
44 |
|||
45 |
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) |
||
46 |
|||
47 |
enum _vflag vflag = FIRST; |
||
48 |
|||
49 |
static off_t address; /* address/offset in stream */ |
||
50 |
static off_t eaddress; /* end address */ |
||
51 |
|||
52 |
static void bpad(PR *); |
||
53 |
static void doskip(const char *, int); |
||
54 |
static u_char *get(void); |
||
55 |
static __inline void print(PR *, u_char *); |
||
56 |
|||
57 |
void |
||
58 |
display(void) |
||
59 |
{ |
||
60 |
FS *fs; |
||
61 |
FU *fu; |
||
62 |
PR *pr; |
||
63 |
int cnt; |
||
64 |
u_char *bp; |
||
65 |
off_t saveaddress; |
||
66 |
u_char savech, *savebp; |
||
67 |
|||
68 |
savech = 0; |
||
69 |
✓✓ | 15133 |
while ((bp = get()) != NULL) |
70 |
✓✓ | 21824 |
for (fs = fshead, savebp = bp, saveaddress = address; fs; |
71 |
7934 |
fs = fs->nextfs, bp = savebp, address = saveaddress) |
|
72 |
✓✓ | 51560 |
for (fu = fs->nextfu; fu; fu = fu->nextfu) { |
73 |
✓✓ | 20324 |
if (fu->flags&F_IGNORE) |
74 |
break; |
||
75 |
✓✓ | 215108 |
for (cnt = fu->reps; cnt; --cnt) |
76 |
✓✓ | 574918 |
for (pr = fu->nextpr; pr; address += pr->bcnt, |
77 |
131834 |
bp += pr->bcnt, pr = pr->nextpr) { |
|
78 |
✓✓✓✓ ✓✓ |
366588 |
if (eaddress && address >= eaddress && |
79 |
103715 |
!(pr->flags & (F_TEXT|F_BPAD))) |
|
80 |
7379 |
bpad(pr); |
|
81 |
✓✓✓✓ |
157114 |
if (cnt == 1 && pr->nospace) { |
82 |
4956 |
savech = *pr->nospace; |
|
83 |
4956 |
*pr->nospace = '\0'; |
|
84 |
4956 |
} |
|
85 |
131834 |
print(pr, bp); |
|
86 |
✓✓✓✓ |
157114 |
if (cnt == 1 && pr->nospace) |
87 |
4956 |
*pr->nospace = savech; |
|
88 |
} |
||
89 |
} |
||
90 |
✓✓ | 3059 |
if (endfu) { |
91 |
/* |
||
92 |
* If eaddress not set, error or file size was multiple of |
||
93 |
* blocksize, and no partial block ever found. |
||
94 |
*/ |
||
95 |
✓✓ | 2559 |
if (!eaddress) { |
96 |
✓✗ | 96 |
if (!address) |
97 |
96 |
return; |
|
98 |
eaddress = address; |
||
99 |
} |
||
100 |
✓✓ | 14778 |
for (pr = endfu->nextpr; pr; pr = pr->nextpr) |
101 |
✓✓✓ | 9852 |
switch(pr->flags) { |
102 |
case F_ADDRESS: |
||
103 |
2463 |
(void)printf(pr->fmt, (int64_t)eaddress); |
|
104 |
2463 |
break; |
|
105 |
case F_TEXT: |
||
106 |
2463 |
(void)printf("%s", pr->fmt); |
|
107 |
2463 |
break; |
|
108 |
} |
||
109 |
} |
||
110 |
6022 |
} |
|
111 |
|||
112 |
static __inline void |
||
113 |
print(PR *pr, u_char *bp) |
||
114 |
{ |
||
115 |
double f8; |
||
116 |
float f4; |
||
117 |
int16_t s2; |
||
118 |
int32_t s4; |
||
119 |
int64_t s8; |
||
120 |
u_int16_t u2; |
||
121 |
u_int32_t u4; |
||
122 |
u_int64_t u8; |
||
123 |
|||
124 |
✓✓✗✗ ✗✓✓✗ ✓✗✓✓ |
387466 |
switch(pr->flags) { |
125 |
case F_ADDRESS: |
||
126 |
2478 |
(void)printf(pr->fmt, (int64_t)address); |
|
127 |
2478 |
break; |
|
128 |
case F_BPAD: |
||
129 |
64224 |
(void)printf(pr->fmt, ""); |
|
130 |
64224 |
break; |
|
131 |
case F_C: |
||
132 |
conv_c(pr, bp); |
||
133 |
break; |
||
134 |
case F_CHAR: |
||
135 |
(void)printf(pr->fmt, *bp); |
||
136 |
break; |
||
137 |
case F_DBL: |
||
138 |
switch(pr->bcnt) { |
||
139 |
case 4: |
||
140 |
memmove(&f4, bp, sizeof(f4)); |
||
141 |
(void)printf(pr->fmt, f4); |
||
142 |
break; |
||
143 |
case 8: |
||
144 |
memmove(&f8, bp, sizeof(f8)); |
||
145 |
(void)printf(pr->fmt, f8); |
||
146 |
break; |
||
147 |
} |
||
148 |
break; |
||
149 |
case F_INT: |
||
150 |
✗✗✓✗ ✓ |
1000 |
switch(pr->bcnt) { |
151 |
case 1: |
||
152 |
(void)printf(pr->fmt, (int64_t)*bp); |
||
153 |
break; |
||
154 |
case 2: |
||
155 |
memmove(&s2, bp, sizeof(s2)); |
||
156 |
(void)printf(pr->fmt, (int64_t)s2); |
||
157 |
break; |
||
158 |
case 4: |
||
159 |
500 |
memmove(&s4, bp, sizeof(s4)); |
|
160 |
500 |
(void)printf(pr->fmt, (int64_t)s4); |
|
161 |
500 |
break; |
|
162 |
case 8: |
||
163 |
memmove(&s8, bp, sizeof(s8)); |
||
164 |
(void)printf(pr->fmt, s8); |
||
165 |
break; |
||
166 |
} |
||
167 |
break; |
||
168 |
case F_P: |
||
169 |
✓✓ | 21311 |
(void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); |
170 |
7536 |
break; |
|
171 |
case F_STR: |
||
172 |
(void)printf(pr->fmt, (char *)bp); |
||
173 |
break; |
||
174 |
case F_TEXT: |
||
175 |
49560 |
(void)printf("%s", pr->fmt); |
|
176 |
49560 |
break; |
|
177 |
case F_U: |
||
178 |
conv_u(pr, bp); |
||
179 |
break; |
||
180 |
case F_UINT: |
||
181 |
✓✗✗✗ ✓ |
15072 |
switch(pr->bcnt) { |
182 |
case 1: |
||
183 |
7536 |
(void)printf(pr->fmt, (uint64_t)*bp); |
|
184 |
7536 |
break; |
|
185 |
case 2: |
||
186 |
memmove(&u2, bp, sizeof(u2)); |
||
187 |
(void)printf(pr->fmt, (uint64_t)u2); |
||
188 |
break; |
||
189 |
case 4: |
||
190 |
memmove(&u4, bp, sizeof(u4)); |
||
191 |
(void)printf(pr->fmt, (uint64_t)u4); |
||
192 |
break; |
||
193 |
case 8: |
||
194 |
memmove(&u8, bp, sizeof(u8)); |
||
195 |
(void)printf(pr->fmt, u8); |
||
196 |
break; |
||
197 |
} |
||
198 |
break; |
||
199 |
} |
||
200 |
131834 |
} |
|
201 |
|||
202 |
static void |
||
203 |
bpad(PR *pr) |
||
204 |
{ |
||
205 |
static const char *spec = " -0+#"; |
||
206 |
char *p1, *p2; |
||
207 |
|||
208 |
/* |
||
209 |
* Remove all conversion flags; '-' is the only one valid |
||
210 |
* with %s, and it's not useful here. |
||
211 |
*/ |
||
212 |
14758 |
pr->flags = F_BPAD; |
|
213 |
7379 |
pr->cchar[0] = 's'; |
|
214 |
7379 |
pr->cchar[1] = '\0'; |
|
215 |
✗✓ | 14758 |
for (p1 = pr->fmt; *p1 != '%'; ++p1); |
216 |
✓✗✓✓ |
36885 |
for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); |
217 |
✓✓ | 39348 |
while ((*p2++ = *p1++) != '\0'); |
218 |
7379 |
} |
|
219 |
|||
220 |
static char **_argv; |
||
221 |
|||
222 |
static u_char * |
||
223 |
get(void) |
||
224 |
{ |
||
225 |
static int ateof = 1; |
||
226 |
static u_char *curp, *savp; |
||
227 |
int n; |
||
228 |
int need, nread; |
||
229 |
u_char *tmpp; |
||
230 |
|||
231 |
✓✓ | 12074 |
if (!curp) { |
232 |
✓✗✗✓ |
6118 |
if ((curp = calloc(1, blocksize)) == NULL || |
233 |
3059 |
(savp = calloc(1, blocksize)) == NULL) |
|
234 |
err(1, NULL); |
||
235 |
} else { |
||
236 |
tmpp = curp; |
||
237 |
2978 |
curp = savp; |
|
238 |
2978 |
savp = tmpp; |
|
239 |
2978 |
address += blocksize; |
|
240 |
} |
||
241 |
9000 |
for (need = blocksize, nread = 0;;) { |
|
242 |
/* |
||
243 |
* if read the right number of bytes, or at EOF for one file, |
||
244 |
* and no other files are available, zero-pad the rest of the |
||
245 |
* block and set the end flag. |
||
246 |
*/ |
||
247 |
✓✗✓✓ ✓✓ |
33199 |
if (!length || (ateof && !next(NULL))) { |
248 |
✓✓ | 6022 |
if (need == blocksize) |
249 |
3059 |
return(NULL); |
|
250 |
✗✓✗✗ |
2963 |
if (!need && vflag != ALL && |
251 |
!memcmp(curp, savp, nread)) { |
||
252 |
if (vflag != DUP) |
||
253 |
(void)printf("*\n"); |
||
254 |
return(NULL); |
||
255 |
} |
||
256 |
2963 |
memset((char *)curp + nread, 0, need); |
|
257 |
2963 |
eaddress = address + nread; |
|
258 |
2963 |
return(curp); |
|
259 |
} |
||
260 |
12074 |
n = fread((char *)curp + nread, sizeof(u_char), |
|
261 |
✓✗ | 18111 |
length == -1 ? need : MINIMUM(length, need), stdin); |
262 |
✓✓ | 6037 |
if (!n) { |
263 |
✓✗✗✓ ✗✗ |
6118 |
if (ferror(stdin)) |
264 |
warn("%s", _argv[-1]); |
||
265 |
3059 |
ateof = 1; |
|
266 |
3059 |
continue; |
|
267 |
} |
||
268 |
2978 |
ateof = 0; |
|
269 |
✗✓ | 2978 |
if (length != -1) |
270 |
length -= n; |
||
271 |
✓✓ | 2978 |
if (!(need -= n)) { |
272 |
✓✓✓✗ |
25 |
if (vflag == ALL || vflag == FIRST || |
273 |
10 |
memcmp(curp, savp, blocksize)) { |
|
274 |
✓✓ | 15 |
if (vflag == DUP || vflag == FIRST) |
275 |
5 |
vflag = WAIT; |
|
276 |
15 |
return(curp); |
|
277 |
} |
||
278 |
if (vflag == WAIT) |
||
279 |
(void)printf("*\n"); |
||
280 |
vflag = DUP; |
||
281 |
address += blocksize; |
||
282 |
need = blocksize; |
||
283 |
nread = 0; |
||
284 |
} |
||
285 |
else |
||
286 |
2963 |
nread += n; |
|
287 |
} |
||
288 |
6037 |
} |
|
289 |
|||
290 |
int |
||
291 |
next(char **argv) |
||
292 |
{ |
||
293 |
static int done; |
||
294 |
int statok; |
||
295 |
|||
296 |
✓✓ | 24280 |
if (argv) { |
297 |
3059 |
_argv = argv; |
|
298 |
3059 |
return(1); |
|
299 |
} |
||
300 |
9081 |
for (;;) { |
|
301 |
✗✓ | 9081 |
if (*_argv) { |
302 |
if (!(freopen(*_argv, "r", stdin))) { |
||
303 |
warn("%s", *_argv); |
||
304 |
exitval = done = 1; |
||
305 |
++_argv; |
||
306 |
continue; |
||
307 |
} |
||
308 |
statok = done = 1; |
||
309 |
} else { |
||
310 |
✓✓ | 9081 |
if (done++) |
311 |
6022 |
return(0); |
|
312 |
statok = 0; |
||
313 |
} |
||
314 |
✗✓ | 3059 |
if (iobuf != NULL) |
315 |
setvbuf(stdin, iobuf, _IOFBF, iobufsiz); |
||
316 |
✗✓ | 3059 |
if (skip) |
317 |
doskip(statok ? *_argv : "stdin", statok); |
||
318 |
✗✓ | 3059 |
if (*_argv) |
319 |
++_argv; |
||
320 |
✓✗ | 3059 |
if (!skip) |
321 |
3059 |
return(1); |
|
322 |
} |
||
323 |
/* NOTREACHED */ |
||
324 |
12140 |
} |
|
325 |
|||
326 |
static void |
||
327 |
doskip(const char *fname, int statok) |
||
328 |
{ |
||
329 |
off_t cnt; |
||
330 |
struct stat sb; |
||
331 |
|||
332 |
if (statok) { |
||
333 |
if (fstat(fileno(stdin), &sb)) |
||
334 |
err(1, "fstat %s", fname); |
||
335 |
if (S_ISREG(sb.st_mode)) { |
||
336 |
if (skip > sb.st_size) { |
||
337 |
address += sb.st_size; |
||
338 |
skip -= sb.st_size; |
||
339 |
} else { |
||
340 |
if (fseeko(stdin, skip, SEEK_SET)) |
||
341 |
err(1, "fseeko %s", fname); |
||
342 |
address += skip; |
||
343 |
skip = 0; |
||
344 |
} |
||
345 |
return; |
||
346 |
} |
||
347 |
} |
||
348 |
|||
349 |
for (cnt = 0; cnt < skip; ++cnt) |
||
350 |
if (getchar() == EOF) |
||
351 |
break; |
||
352 |
address += cnt; |
||
353 |
skip -= cnt; |
||
354 |
} |
Generated by: GCOVR (Version 3.3) |