xref: /freebsd/usr.bin/hexdump/display.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
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 	   double f8;
121 	    float f4;
122 	  int16_t s2;
123 	   int8_t s8;
124 	  int32_t s4;
125 	u_int16_t u2;
126 	u_int32_t u4;
127 	u_int64_t u8;
128 
129 	switch(pr->flags) {
130 	case F_ADDRESS:
131 		(void)printf(pr->fmt, (quad_t)address);
132 		break;
133 	case F_BPAD:
134 		(void)printf(pr->fmt, "");
135 		break;
136 	case F_C:
137 		conv_c(pr, bp);
138 		break;
139 	case F_CHAR:
140 		(void)printf(pr->fmt, *bp);
141 		break;
142 	case F_DBL:
143 		switch(pr->bcnt) {
144 		case 4:
145 			bcopy(bp, &f4, sizeof(f4));
146 			(void)printf(pr->fmt, f4);
147 			break;
148 		case 8:
149 			bcopy(bp, &f8, sizeof(f8));
150 			(void)printf(pr->fmt, f8);
151 			break;
152 		}
153 		break;
154 	case F_INT:
155 		switch(pr->bcnt) {
156 		case 1:
157 			(void)printf(pr->fmt, (quad_t)*bp);
158 			break;
159 		case 2:
160 			bcopy(bp, &s2, sizeof(s2));
161 			(void)printf(pr->fmt, (quad_t)s2);
162 			break;
163 		case 4:
164 			bcopy(bp, &s4, sizeof(s4));
165 			(void)printf(pr->fmt, (quad_t)s4);
166 			break;
167 		case 8:
168 			bcopy(bp, &s8, sizeof(s8));
169 			(void)printf(pr->fmt, s8);
170 			break;
171 		}
172 		break;
173 	case F_P:
174 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
175 		break;
176 	case F_STR:
177 		(void)printf(pr->fmt, (char *)bp);
178 		break;
179 	case F_TEXT:
180 		(void)printf("%s", pr->fmt);
181 		break;
182 	case F_U:
183 		conv_u(pr, bp);
184 		break;
185 	case F_UINT:
186 		switch(pr->bcnt) {
187 		case 1:
188 			(void)printf(pr->fmt, (u_quad_t)*bp);
189 			break;
190 		case 2:
191 			bcopy(bp, &u2, sizeof(u2));
192 			(void)printf(pr->fmt, (u_quad_t)u2);
193 			break;
194 		case 4:
195 			bcopy(bp, &u4, sizeof(u4));
196 			(void)printf(pr->fmt, (u_quad_t)u4);
197 			break;
198 		case 8:
199 			bcopy(bp, &u8, sizeof(u8));
200 			(void)printf(pr->fmt, u8);
201 			break;
202 		}
203 		break;
204 	}
205 }
206 
207 void
208 bpad(pr)
209 	PR *pr;
210 {
211 	static char const *spec = " -0+#";
212 	register char *p1, *p2;
213 
214 	/*
215 	 * Remove all conversion flags; '-' is the only one valid
216 	 * with %s, and it's not useful here.
217 	 */
218 	pr->flags = F_BPAD;
219 	pr->cchar[0] = 's';
220 	pr->cchar[1] = '\0';
221 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
222 	for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
223 	while ((*p2++ = *p1++));
224 }
225 
226 static char **_argv;
227 
228 u_char *
229 get()
230 {
231 	extern int length;
232 	static int ateof = 1;
233 	static u_char *curp, *savp;
234 	register int n;
235 	int need, nread;
236 	int valid_save = 0;
237 	u_char *tmpp;
238 
239 	if (!curp) {
240 		if ((curp = calloc(1, blocksize)) == NULL)
241 			err(1, NULL);
242 		if ((savp = calloc(1, blocksize)) == NULL)
243 			err(1, NULL);
244 	} else {
245 		tmpp = curp;
246 		curp = savp;
247 		savp = tmpp;
248 		address += blocksize;
249 		valid_save = 1;
250 	}
251 	for (need = blocksize, nread = 0;;) {
252 		/*
253 		 * if read the right number of bytes, or at EOF for one file,
254 		 * and no other files are available, zero-pad the rest of the
255 		 * block and set the end flag.
256 		 */
257 		if (!length || (ateof && !next((char **)NULL))) {
258 			if (need == blocksize)
259 				return((u_char *)NULL);
260 			if (vflag != ALL &&
261 			    valid_save &&
262 			    bcmp(curp, savp, nread) == 0) {
263 				if (vflag != DUP)
264 					(void)printf("*\n");
265 				return((u_char *)NULL);
266 			}
267 			bzero((char *)curp + nread, need);
268 			eaddress = address + nread;
269 			return(curp);
270 		}
271 		n = fread((char *)curp + nread, sizeof(u_char),
272 		    length == -1 ? need : MIN(length, need), stdin);
273 		if (!n) {
274 			if (ferror(stdin))
275 				warn("%s", _argv[-1]);
276 			ateof = 1;
277 			continue;
278 		}
279 		ateof = 0;
280 		if (length != -1)
281 			length -= n;
282 		if (!(need -= n)) {
283 			if (vflag == ALL || vflag == FIRST ||
284 			    valid_save == 0 ||
285 			    bcmp(curp, savp, blocksize) != 0) {
286 				if (vflag == DUP || vflag == FIRST)
287 					vflag = WAIT;
288 				return(curp);
289 			}
290 			if (vflag == WAIT)
291 				(void)printf("*\n");
292 			vflag = DUP;
293 			address += blocksize;
294 			need = blocksize;
295 			nread = 0;
296 		}
297 		else
298 			nread += n;
299 	}
300 }
301 
302 extern off_t skip;			/* bytes to skip */
303 
304 int
305 next(argv)
306 	char **argv;
307 {
308 	extern int exitval;
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(fname, statok)
342 	const char *fname;
343 	int statok;
344 {
345 	register int cnt;
346 	struct stat sb;
347 
348 	if (statok) {
349 		if (fstat(fileno(stdin), &sb))
350 			err(1, "%s", fname);
351 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
352 			address += sb.st_size;
353 			skip -= sb.st_size;
354 			return;
355 		}
356 	}
357 	if (S_ISREG(sb.st_mode)) {
358 		if (fseeko(stdin, skip, SEEK_SET))
359 			err(1, "%s", fname);
360 		address += skip;
361 		skip = 0;
362 	} else {
363 		for (cnt = 0; cnt < skip; ++cnt)
364 			if (getchar() == EOF)
365 				break;
366 		address += cnt;
367 		skip -= cnt;
368 	}
369 }
370