xref: /freebsd/usr.bin/hexdump/display.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 #endif /* not lint */
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/conf.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 
41 #include <capsicum_helpers.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include "hexdump.h"
50 
51 enum _vflag vflag = FIRST;
52 
53 static off_t address;			/* address/offset in stream */
54 static off_t eaddress;			/* end address */
55 
56 static void print(PR *, u_char *);
57 static void noseek(void);
58 
59 void
60 display(void)
61 {
62 	FS *fs;
63 	FU *fu;
64 	PR *pr;
65 	int cnt;
66 	u_char *bp;
67 	off_t saveaddress;
68 	u_char savech, *savebp;
69 
70 	savech = 0;
71 	while ((bp = get()))
72 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
73 		fs = fs->nextfs, bp = savebp, address = saveaddress)
74 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
75 			if (fu->flags&F_IGNORE)
76 				break;
77 			for (cnt = fu->reps; cnt; --cnt)
78 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
79 				bp += pr->bcnt, pr = pr->nextpr) {
80 				    if (eaddress && address >= eaddress &&
81 					!(pr->flags & (F_TEXT|F_BPAD)))
82 					    bpad(pr);
83 				    if (cnt == 1 && pr->nospace) {
84 					savech = *pr->nospace;
85 					*pr->nospace = '\0';
86 				    }
87 				    print(pr, bp);
88 				    if (cnt == 1 && pr->nospace)
89 					*pr->nospace = savech;
90 			    }
91 		    }
92 	if (endfu) {
93 		/*
94 		 * If eaddress not set, error or file size was multiple of
95 		 * blocksize, and no partial block ever found.
96 		 */
97 		if (!eaddress) {
98 			if (!address)
99 				return;
100 			eaddress = address;
101 		}
102 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
103 			switch(pr->flags) {
104 			case F_ADDRESS:
105 				(void)printf(pr->fmt, (quad_t)eaddress);
106 				break;
107 			case F_TEXT:
108 				(void)printf("%s", pr->fmt);
109 				break;
110 			}
111 	}
112 }
113 
114 static void
115 print(PR *pr, u_char *bp)
116 {
117 	long double ldbl;
118 	   double f8;
119 	    float f4;
120 	  int16_t s2;
121 	  int32_t s4;
122 	  int64_t s8;
123 	u_int16_t u2;
124 	u_int32_t u4;
125 	u_int64_t u8;
126 
127 	switch(pr->flags) {
128 	case F_ADDRESS:
129 		(void)printf(pr->fmt, (quad_t)address);
130 		break;
131 	case F_BPAD:
132 		(void)printf(pr->fmt, "");
133 		break;
134 	case F_C:
135 		conv_c(pr, bp, eaddress ? eaddress - address :
136 		    blocksize - address % blocksize);
137 		break;
138 	case F_CHAR:
139 		(void)printf(pr->fmt, *bp);
140 		break;
141 	case F_DBL:
142 		switch(pr->bcnt) {
143 		case 4:
144 			bcopy(bp, &f4, sizeof(f4));
145 			(void)printf(pr->fmt, f4);
146 			break;
147 		case 8:
148 			bcopy(bp, &f8, sizeof(f8));
149 			(void)printf(pr->fmt, f8);
150 			break;
151 		default:
152 			if (pr->bcnt == sizeof(long double)) {
153 				bcopy(bp, &ldbl, sizeof(ldbl));
154 				(void)printf(pr->fmt, ldbl);
155 			}
156 			break;
157 		}
158 		break;
159 	case F_INT:
160 		switch(pr->bcnt) {
161 		case 1:
162 			(void)printf(pr->fmt, (quad_t)(signed char)*bp);
163 			break;
164 		case 2:
165 			bcopy(bp, &s2, sizeof(s2));
166 			(void)printf(pr->fmt, (quad_t)s2);
167 			break;
168 		case 4:
169 			bcopy(bp, &s4, sizeof(s4));
170 			(void)printf(pr->fmt, (quad_t)s4);
171 			break;
172 		case 8:
173 			bcopy(bp, &s8, sizeof(s8));
174 			(void)printf(pr->fmt, s8);
175 			break;
176 		}
177 		break;
178 	case F_P:
179 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
180 		break;
181 	case F_STR:
182 		(void)printf(pr->fmt, (char *)bp);
183 		break;
184 	case F_TEXT:
185 		(void)printf("%s", pr->fmt);
186 		break;
187 	case F_U:
188 		conv_u(pr, bp);
189 		break;
190 	case F_UINT:
191 		switch(pr->bcnt) {
192 		case 1:
193 			(void)printf(pr->fmt, (u_quad_t)*bp);
194 			break;
195 		case 2:
196 			bcopy(bp, &u2, sizeof(u2));
197 			(void)printf(pr->fmt, (u_quad_t)u2);
198 			break;
199 		case 4:
200 			bcopy(bp, &u4, sizeof(u4));
201 			(void)printf(pr->fmt, (u_quad_t)u4);
202 			break;
203 		case 8:
204 			bcopy(bp, &u8, sizeof(u8));
205 			(void)printf(pr->fmt, u8);
206 			break;
207 		}
208 		break;
209 	}
210 }
211 
212 void
213 bpad(PR *pr)
214 {
215 	static char const *spec = " -0+#";
216 	char *p1, *p2;
217 
218 	/*
219 	 * Remove all conversion flags; '-' is the only one valid
220 	 * with %s, and it's not useful here.
221 	 */
222 	pr->flags = F_BPAD;
223 	pr->cchar[0] = 's';
224 	pr->cchar[1] = '\0';
225 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
226 	for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
227 	while ((*p2++ = *p1++));
228 }
229 
230 static char **_argv;
231 
232 u_char *
233 get(void)
234 {
235 	static int ateof = 1;
236 	static u_char *curp, *savp;
237 	int n;
238 	int need, nread;
239 	int valid_save = 0;
240 	u_char *tmpp;
241 
242 	if (!curp) {
243 		if ((curp = calloc(1, blocksize)) == NULL)
244 			err(1, NULL);
245 		if ((savp = calloc(1, blocksize)) == NULL)
246 			err(1, NULL);
247 	} else {
248 		tmpp = curp;
249 		curp = savp;
250 		savp = tmpp;
251 		address += blocksize;
252 		valid_save = 1;
253 	}
254 	for (need = blocksize, nread = 0;;) {
255 		/*
256 		 * if read the right number of bytes, or at EOF for one file,
257 		 * and no other files are available, zero-pad the rest of the
258 		 * block and set the end flag.
259 		 */
260 		if (!length || (ateof && !next((char **)NULL))) {
261 			if (odmode && skip > 0)
262 				errx(1, "cannot skip past end of input");
263 			if (need == blocksize)
264 				return((u_char *)NULL);
265 			/*
266 			 * XXX bcmp() is not quite right in the presence
267 			 * of multibyte characters.
268 			 */
269 			if (need == 0 && vflag != ALL &&
270 			    valid_save &&
271 			    bcmp(curp, savp, nread) == 0) {
272 				if (vflag != DUP) {
273 					(void)printf("*\n");
274 					(void)fflush(stdout);
275 				}
276 				return((u_char *)NULL);
277 			}
278 			bzero((char *)curp + nread, need);
279 			eaddress = address + nread;
280 			return(curp);
281 		}
282 		n = fread((char *)curp + nread, sizeof(u_char),
283 		    length == -1 ? need : MIN(length, need), stdin);
284 		if (!n) {
285 			if (ferror(stdin))
286 				warn("%s", _argv[-1]);
287 			ateof = 1;
288 			continue;
289 		}
290 		ateof = 0;
291 		if (length != -1)
292 			length -= n;
293 		if (!(need -= n)) {
294 			/*
295 			 * XXX bcmp() is not quite right in the presence
296 			 * of multibyte characters.
297 			 */
298 			if (vflag == ALL || vflag == FIRST ||
299 			    valid_save == 0 ||
300 			    bcmp(curp, savp, blocksize) != 0) {
301 				if (vflag == DUP || vflag == FIRST)
302 					vflag = WAIT;
303 				return(curp);
304 			}
305 			if (vflag == WAIT) {
306 				(void)printf("*\n");
307 				(void)fflush(stdout);
308 			}
309 			vflag = DUP;
310 			address += blocksize;
311 			need = blocksize;
312 			nread = 0;
313 		}
314 		else
315 			nread += n;
316 	}
317 }
318 
319 size_t
320 peek(u_char *buf, size_t nbytes)
321 {
322 	size_t n, nread;
323 	int c;
324 
325 	if (length != -1 && nbytes > (unsigned int)length)
326 		nbytes = length;
327 	nread = 0;
328 	while (nread < nbytes && (c = getchar()) != EOF) {
329 		*buf++ = c;
330 		nread++;
331 	}
332 	n = nread;
333 	while (n-- > 0) {
334 		c = *--buf;
335 		ungetc(c, stdin);
336 	}
337 	return (nread);
338 }
339 
340 int
341 next(char **argv)
342 {
343 	static int done;
344 	int statok;
345 
346 	if (argv) {
347 		_argv = argv;
348 		return(1);
349 	}
350 	for (;;) {
351 		if (*_argv) {
352 			done = 1;
353 			if (!(freopen(*_argv, "r", stdin))) {
354 				warn("%s", *_argv);
355 				exitval = 1;
356 				++_argv;
357 				continue;
358 			}
359 			statok = 1;
360 		} else {
361 			if (done++)
362 				return(0);
363 			statok = 0;
364 		}
365 
366 		if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
367 			err(1, "unable to restrict %s",
368 			    statok ? *_argv : "stdin");
369 
370 		/*
371 		 * We've opened our last input file; enter capsicum sandbox.
372 		 */
373 		if (statok == 0 || *(_argv + 1) == NULL) {
374 			if (caph_enter() < 0)
375 				err(1, "unable to enter capability mode");
376 		}
377 
378 		if (skip)
379 			doskip(statok ? *_argv : "stdin", statok);
380 		if (*_argv)
381 			++_argv;
382 		if (!skip)
383 			return(1);
384 	}
385 	/* NOTREACHED */
386 }
387 
388 void
389 doskip(const char *fname, int statok)
390 {
391 	int type;
392 	struct stat sb;
393 
394 	if (statok) {
395 		if (fstat(fileno(stdin), &sb))
396 			err(1, "%s", fname);
397 		if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
398 			address += sb.st_size;
399 			skip -= sb.st_size;
400 			return;
401 		}
402 	}
403 	if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
404 		noseek();
405 		return;
406 	}
407 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
408 		if (ioctl(fileno(stdin), FIODTYPE, &type))
409 			err(1, "%s", fname);
410 		/*
411 		 * Most tape drives don't support seeking,
412 		 * yet fseek() would succeed.
413 		 */
414 		if (type & D_TAPE) {
415 			noseek();
416 			return;
417 		}
418 	}
419 	if (fseeko(stdin, skip, SEEK_SET)) {
420 		noseek();
421 		return;
422 	}
423 	address += skip;
424 	skip = 0;
425 }
426 
427 static void
428 noseek(void)
429 {
430 	int count;
431 	for (count = 0; count < skip; ++count)
432 		if (getchar() == EOF)
433 			break;
434 	address += count;
435 	skip -= count;
436 }
437