xref: /freebsd/usr.bin/hexdump/display.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "hexdump.h"
52 
53 enum _vflag vflag = FIRST;
54 
55 static off_t address;			/* address/offset in stream */
56 static off_t eaddress;			/* end address */
57 
58 static __inline void print(PR *, u_char *);
59 
60 void
61 display(void)
62 {
63 	FS *fs;
64 	FU *fu;
65 	PR *pr;
66 	int cnt;
67 	u_char *bp;
68 	off_t saveaddress;
69 	u_char savech, *savebp;
70 
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 __inline void
115 print(PR *pr, u_char *bp)
116 {
117 	long double ldbl;
118 	   double f8;
119 	    float f4;
120 	  int16_t s2;
121 	   int8_t s8;
122 	  int32_t s4;
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);
136 		break;
137 	case F_CHAR:
138 		(void)printf(pr->fmt, *bp);
139 		break;
140 	case F_DBL:
141 		switch(pr->bcnt) {
142 		case 4:
143 			bcopy(bp, &f4, sizeof(f4));
144 			(void)printf(pr->fmt, f4);
145 			break;
146 		case 8:
147 			bcopy(bp, &f8, sizeof(f8));
148 			(void)printf(pr->fmt, f8);
149 			break;
150 		default:
151 			if (pr->bcnt == sizeof(long double)) {
152 				bcopy(bp, &ldbl, sizeof(ldbl));
153 				(void)printf(pr->fmt, ldbl);
154 			}
155 			break;
156 		}
157 		break;
158 	case F_INT:
159 		switch(pr->bcnt) {
160 		case 1:
161 			(void)printf(pr->fmt, (quad_t)(signed char)*bp);
162 			break;
163 		case 2:
164 			bcopy(bp, &s2, sizeof(s2));
165 			(void)printf(pr->fmt, (quad_t)s2);
166 			break;
167 		case 4:
168 			bcopy(bp, &s4, sizeof(s4));
169 			(void)printf(pr->fmt, (quad_t)s4);
170 			break;
171 		case 8:
172 			bcopy(bp, &s8, sizeof(s8));
173 			(void)printf(pr->fmt, s8);
174 			break;
175 		}
176 		break;
177 	case F_P:
178 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
179 		break;
180 	case F_STR:
181 		(void)printf(pr->fmt, (char *)bp);
182 		break;
183 	case F_TEXT:
184 		(void)printf("%s", pr->fmt);
185 		break;
186 	case F_U:
187 		conv_u(pr, bp);
188 		break;
189 	case F_UINT:
190 		switch(pr->bcnt) {
191 		case 1:
192 			(void)printf(pr->fmt, (u_quad_t)*bp);
193 			break;
194 		case 2:
195 			bcopy(bp, &u2, sizeof(u2));
196 			(void)printf(pr->fmt, (u_quad_t)u2);
197 			break;
198 		case 4:
199 			bcopy(bp, &u4, sizeof(u4));
200 			(void)printf(pr->fmt, (u_quad_t)u4);
201 			break;
202 		case 8:
203 			bcopy(bp, &u8, sizeof(u8));
204 			(void)printf(pr->fmt, u8);
205 			break;
206 		}
207 		break;
208 	}
209 }
210 
211 void
212 bpad(PR *pr)
213 {
214 	static char const *spec = " -0+#";
215 	char *p1, *p2;
216 
217 	/*
218 	 * Remove all conversion flags; '-' is the only one valid
219 	 * with %s, and it's not useful here.
220 	 */
221 	pr->flags = F_BPAD;
222 	pr->cchar[0] = 's';
223 	pr->cchar[1] = '\0';
224 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
225 	for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
226 	while ((*p2++ = *p1++));
227 }
228 
229 static char **_argv;
230 
231 u_char *
232 get(void)
233 {
234 	static int ateof = 1;
235 	static u_char *curp, *savp;
236 	int n;
237 	int need, nread;
238 	int valid_save = 0;
239 	u_char *tmpp;
240 
241 	if (!curp) {
242 		if ((curp = calloc(1, blocksize)) == NULL)
243 			err(1, NULL);
244 		if ((savp = calloc(1, blocksize)) == NULL)
245 			err(1, NULL);
246 	} else {
247 		tmpp = curp;
248 		curp = savp;
249 		savp = tmpp;
250 		address += blocksize;
251 		valid_save = 1;
252 	}
253 	for (need = blocksize, nread = 0;;) {
254 		/*
255 		 * if read the right number of bytes, or at EOF for one file,
256 		 * and no other files are available, zero-pad the rest of the
257 		 * block and set the end flag.
258 		 */
259 		if (!length || (ateof && !next((char **)NULL))) {
260 			if (odmode && address < skip)
261 				errx(1, "cannot skip past end of input");
262 			if (need == blocksize)
263 				return((u_char *)NULL);
264 			if (vflag != ALL &&
265 			    valid_save &&
266 			    bcmp(curp, savp, nread) == 0) {
267 				if (vflag != DUP)
268 					(void)printf("*\n");
269 				return((u_char *)NULL);
270 			}
271 			bzero((char *)curp + nread, need);
272 			eaddress = address + nread;
273 			return(curp);
274 		}
275 		n = fread((char *)curp + nread, sizeof(u_char),
276 		    length == -1 ? need : MIN(length, need), stdin);
277 		if (!n) {
278 			if (ferror(stdin))
279 				warn("%s", _argv[-1]);
280 			ateof = 1;
281 			continue;
282 		}
283 		ateof = 0;
284 		if (length != -1)
285 			length -= n;
286 		if (!(need -= n)) {
287 			if (vflag == ALL || vflag == FIRST ||
288 			    valid_save == 0 ||
289 			    bcmp(curp, savp, blocksize) != 0) {
290 				if (vflag == DUP || vflag == FIRST)
291 					vflag = WAIT;
292 				return(curp);
293 			}
294 			if (vflag == WAIT)
295 				(void)printf("*\n");
296 			vflag = DUP;
297 			address += blocksize;
298 			need = blocksize;
299 			nread = 0;
300 		}
301 		else
302 			nread += n;
303 	}
304 }
305 
306 int
307 next(char **argv)
308 {
309 	static int done;
310 	int statok;
311 
312 	if (argv) {
313 		_argv = argv;
314 		return(1);
315 	}
316 	for (;;) {
317 		if (*_argv) {
318 			if (!(freopen(*_argv, "r", stdin))) {
319 				warn("%s", *_argv);
320 				exitval = 1;
321 				++_argv;
322 				continue;
323 			}
324 			statok = done = 1;
325 		} else {
326 			if (done++)
327 				return(0);
328 			statok = 0;
329 		}
330 		if (skip)
331 			doskip(statok ? *_argv : "stdin", statok);
332 		if (*_argv)
333 			++_argv;
334 		if (!skip)
335 			return(1);
336 	}
337 	/* NOTREACHED */
338 }
339 
340 void
341 doskip(const char *fname, int statok)
342 {
343 	int cnt;
344 	struct stat sb;
345 
346 	if (statok) {
347 		if (fstat(fileno(stdin), &sb))
348 			err(1, "%s", fname);
349 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
350 			address += sb.st_size;
351 			skip -= sb.st_size;
352 			return;
353 		}
354 	}
355 	if (S_ISREG(sb.st_mode)) {
356 		if (fseeko(stdin, skip, SEEK_SET))
357 			err(1, "%s", fname);
358 		address += skip;
359 		skip = 0;
360 	} else {
361 		for (cnt = 0; cnt < skip; ++cnt)
362 			if (getchar() == EOF)
363 				break;
364 		address += cnt;
365 		skip -= cnt;
366 	}
367 }
368