xref: /freebsd/usr.bin/hexdump/display.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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()
62 {
63 	extern FU *endfu;
64 	register FS *fs;
65 	register FU *fu;
66 	register PR *pr;
67 	register int cnt;
68 	register u_char *bp;
69 	off_t saveaddress;
70 	u_char savech, *savebp;
71 
72 	while ((bp = get()))
73 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
74 		fs = fs->nextfs, bp = savebp, address = saveaddress)
75 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
76 			if (fu->flags&F_IGNORE)
77 				break;
78 			for (cnt = fu->reps; cnt; --cnt)
79 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
80 				bp += pr->bcnt, pr = pr->nextpr) {
81 				    if (eaddress && address >= eaddress &&
82 					!(pr->flags & (F_TEXT|F_BPAD)))
83 					    bpad(pr);
84 				    if (cnt == 1 && pr->nospace) {
85 					savech = *pr->nospace;
86 					*pr->nospace = '\0';
87 				    }
88 				    print(pr, bp);
89 				    if (cnt == 1 && pr->nospace)
90 					*pr->nospace = savech;
91 			    }
92 		    }
93 	if (endfu) {
94 		/*
95 		 * If eaddress not set, error or file size was multiple of
96 		 * blocksize, and no partial block ever found.
97 		 */
98 		if (!eaddress) {
99 			if (!address)
100 				return;
101 			eaddress = address;
102 		}
103 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
104 			switch(pr->flags) {
105 			case F_ADDRESS:
106 				(void)printf(pr->fmt, (quad_t)eaddress);
107 				break;
108 			case F_TEXT:
109 				(void)printf("%s", pr->fmt);
110 				break;
111 			}
112 	}
113 }
114 
115 static __inline void
116 print(pr, bp)
117 	PR *pr;
118 	u_char *bp;
119 {
120 	long double ldbl;
121 	   double f8;
122 	    float f4;
123 	  int16_t s2;
124 	   int8_t s8;
125 	  int32_t s4;
126 	u_int16_t u2;
127 	u_int32_t u4;
128 	u_int64_t u8;
129 
130 	switch(pr->flags) {
131 	case F_ADDRESS:
132 		(void)printf(pr->fmt, (quad_t)address);
133 		break;
134 	case F_BPAD:
135 		(void)printf(pr->fmt, "");
136 		break;
137 	case F_C:
138 		conv_c(pr, bp);
139 		break;
140 	case F_CHAR:
141 		(void)printf(pr->fmt, *bp);
142 		break;
143 	case F_DBL:
144 		switch(pr->bcnt) {
145 		case 4:
146 			bcopy(bp, &f4, sizeof(f4));
147 			(void)printf(pr->fmt, f4);
148 			break;
149 		case 8:
150 			bcopy(bp, &f8, sizeof(f8));
151 			(void)printf(pr->fmt, f8);
152 			break;
153 		default:
154 			if (pr->bcnt == sizeof(long double)) {
155 				bcopy(bp, &ldbl, sizeof(ldbl));
156 				(void)printf(pr->fmt, ldbl);
157 			}
158 			break;
159 		}
160 		break;
161 	case F_INT:
162 		switch(pr->bcnt) {
163 		case 1:
164 			(void)printf(pr->fmt, (quad_t)(signed char)*bp);
165 			break;
166 		case 2:
167 			bcopy(bp, &s2, sizeof(s2));
168 			(void)printf(pr->fmt, (quad_t)s2);
169 			break;
170 		case 4:
171 			bcopy(bp, &s4, sizeof(s4));
172 			(void)printf(pr->fmt, (quad_t)s4);
173 			break;
174 		case 8:
175 			bcopy(bp, &s8, sizeof(s8));
176 			(void)printf(pr->fmt, s8);
177 			break;
178 		}
179 		break;
180 	case F_P:
181 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
182 		break;
183 	case F_STR:
184 		(void)printf(pr->fmt, (char *)bp);
185 		break;
186 	case F_TEXT:
187 		(void)printf("%s", pr->fmt);
188 		break;
189 	case F_U:
190 		conv_u(pr, bp);
191 		break;
192 	case F_UINT:
193 		switch(pr->bcnt) {
194 		case 1:
195 			(void)printf(pr->fmt, (u_quad_t)*bp);
196 			break;
197 		case 2:
198 			bcopy(bp, &u2, sizeof(u2));
199 			(void)printf(pr->fmt, (u_quad_t)u2);
200 			break;
201 		case 4:
202 			bcopy(bp, &u4, sizeof(u4));
203 			(void)printf(pr->fmt, (u_quad_t)u4);
204 			break;
205 		case 8:
206 			bcopy(bp, &u8, sizeof(u8));
207 			(void)printf(pr->fmt, u8);
208 			break;
209 		}
210 		break;
211 	}
212 }
213 
214 void
215 bpad(pr)
216 	PR *pr;
217 {
218 	static char const *spec = " -0+#";
219 	register char *p1, *p2;
220 
221 	/*
222 	 * Remove all conversion flags; '-' is the only one valid
223 	 * with %s, and it's not useful here.
224 	 */
225 	pr->flags = F_BPAD;
226 	pr->cchar[0] = 's';
227 	pr->cchar[1] = '\0';
228 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
229 	for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
230 	while ((*p2++ = *p1++));
231 }
232 
233 static char **_argv;
234 
235 u_char *
236 get()
237 {
238 	static int ateof = 1;
239 	static u_char *curp, *savp;
240 	register int n;
241 	int need, nread;
242 	int valid_save = 0;
243 	u_char *tmpp;
244 
245 	if (!curp) {
246 		if ((curp = calloc(1, blocksize)) == NULL)
247 			err(1, NULL);
248 		if ((savp = calloc(1, blocksize)) == NULL)
249 			err(1, NULL);
250 	} else {
251 		tmpp = curp;
252 		curp = savp;
253 		savp = tmpp;
254 		address += blocksize;
255 		valid_save = 1;
256 	}
257 	for (need = blocksize, nread = 0;;) {
258 		/*
259 		 * if read the right number of bytes, or at EOF for one file,
260 		 * and no other files are available, zero-pad the rest of the
261 		 * block and set the end flag.
262 		 */
263 		if (!length || (ateof && !next((char **)NULL))) {
264 			if (odmode && address < skip)
265 				errx(1, "cannot skip past end of input");
266 			if (need == blocksize)
267 				return((u_char *)NULL);
268 			if (vflag != ALL &&
269 			    valid_save &&
270 			    bcmp(curp, savp, nread) == 0) {
271 				if (vflag != DUP)
272 					(void)printf("*\n");
273 				return((u_char *)NULL);
274 			}
275 			bzero((char *)curp + nread, need);
276 			eaddress = address + nread;
277 			return(curp);
278 		}
279 		n = fread((char *)curp + nread, sizeof(u_char),
280 		    length == -1 ? need : MIN(length, need), stdin);
281 		if (!n) {
282 			if (ferror(stdin))
283 				warn("%s", _argv[-1]);
284 			ateof = 1;
285 			continue;
286 		}
287 		ateof = 0;
288 		if (length != -1)
289 			length -= n;
290 		if (!(need -= n)) {
291 			if (vflag == ALL || vflag == FIRST ||
292 			    valid_save == 0 ||
293 			    bcmp(curp, savp, blocksize) != 0) {
294 				if (vflag == DUP || vflag == FIRST)
295 					vflag = WAIT;
296 				return(curp);
297 			}
298 			if (vflag == WAIT)
299 				(void)printf("*\n");
300 			vflag = DUP;
301 			address += blocksize;
302 			need = blocksize;
303 			nread = 0;
304 		}
305 		else
306 			nread += n;
307 	}
308 }
309 
310 int
311 next(argv)
312 	char **argv;
313 {
314 	extern int exitval;
315 	static int done;
316 	int statok;
317 
318 	if (argv) {
319 		_argv = argv;
320 		return(1);
321 	}
322 	for (;;) {
323 		if (*_argv) {
324 			if (!(freopen(*_argv, "r", stdin))) {
325 				warn("%s", *_argv);
326 				exitval = 1;
327 				++_argv;
328 				continue;
329 			}
330 			statok = done = 1;
331 		} else {
332 			if (done++)
333 				return(0);
334 			statok = 0;
335 		}
336 		if (skip)
337 			doskip(statok ? *_argv : "stdin", statok);
338 		if (*_argv)
339 			++_argv;
340 		if (!skip)
341 			return(1);
342 	}
343 	/* NOTREACHED */
344 }
345 
346 void
347 doskip(fname, statok)
348 	const char *fname;
349 	int statok;
350 {
351 	register int cnt;
352 	struct stat sb;
353 
354 	if (statok) {
355 		if (fstat(fileno(stdin), &sb))
356 			err(1, "%s", fname);
357 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
358 			address += sb.st_size;
359 			skip -= sb.st_size;
360 			return;
361 		}
362 	}
363 	if (S_ISREG(sb.st_mode)) {
364 		if (fseeko(stdin, skip, SEEK_SET))
365 			err(1, "%s", fname);
366 		address += skip;
367 		skip = 0;
368 	} else {
369 		for (cnt = 0; cnt < skip; ++cnt)
370 			if (getchar() == EOF)
371 				break;
372 		address += cnt;
373 		skip -= cnt;
374 	}
375 }
376