xref: /freebsd/usr.bin/hexdump/display.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 __P((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(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(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 *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 enum _vflag vflag;
232 	extern int length;
233 	static int ateof = 1;
234 	static u_char *curp, *savp;
235 	register int n;
236 	int need, nread;
237 	int valid_save = 0;
238 	u_char *tmpp;
239 
240 	if (!curp) {
241 		curp = emalloc(blocksize);
242 		savp = emalloc(blocksize);
243 	} else {
244 		tmpp = curp;
245 		curp = savp;
246 		savp = tmpp;
247 		address += blocksize;
248 		valid_save = 1;
249 	}
250 	for (need = blocksize, nread = 0;;) {
251 		/*
252 		 * if read the right number of bytes, or at EOF for one file,
253 		 * and no other files are available, zero-pad the rest of the
254 		 * block and set the end flag.
255 		 */
256 		if (!length || ateof && !next((char **)NULL)) {
257 			if (need == blocksize)
258 				return((u_char *)NULL);
259 			if (vflag != ALL &&
260 			    valid_save &&
261 			    bcmp(curp, savp, nread) == 0) {
262 				if (vflag != DUP)
263 					(void)printf("*\n");
264 				return((u_char *)NULL);
265 			}
266 			bzero((char *)curp + nread, need);
267 			eaddress = address + nread;
268 			return(curp);
269 		}
270 		n = fread((char *)curp + nread, sizeof(u_char),
271 		    length == -1 ? need : MIN(length, need), stdin);
272 		if (!n) {
273 			if (ferror(stdin))
274 				warn("%s", _argv[-1]);
275 			ateof = 1;
276 			continue;
277 		}
278 		ateof = 0;
279 		if (length != -1)
280 			length -= n;
281 		if (!(need -= n)) {
282 			if (vflag == ALL || vflag == FIRST ||
283 			    valid_save == 0 ||
284 			    bcmp(curp, savp, blocksize) != 0) {
285 				if (vflag == DUP || vflag == FIRST)
286 					vflag = WAIT;
287 				return(curp);
288 			}
289 			if (vflag == WAIT)
290 				(void)printf("*\n");
291 			vflag = DUP;
292 			address += blocksize;
293 			need = blocksize;
294 			nread = 0;
295 		}
296 		else
297 			nread += n;
298 	}
299 }
300 
301 extern off_t skip;			/* bytes to skip */
302 
303 int
304 next(argv)
305 	char **argv;
306 {
307 	extern int exitval;
308 	static int done;
309 	int statok;
310 
311 	if (argv) {
312 		_argv = argv;
313 		return(1);
314 	}
315 	for (;;) {
316 		if (*_argv) {
317 			if (!(freopen(*_argv, "r", stdin))) {
318 				warn("%s", *_argv);
319 				exitval = 1;
320 				++_argv;
321 				continue;
322 			}
323 			statok = done = 1;
324 		} else {
325 			if (done++)
326 				return(0);
327 			statok = 0;
328 		}
329 		if (skip)
330 			doskip(statok ? *_argv : "stdin", statok);
331 		if (*_argv)
332 			++_argv;
333 		if (!skip)
334 			return(1);
335 	}
336 	/* NOTREACHED */
337 }
338 
339 void
340 doskip(fname, statok)
341 	char *fname;
342 	int statok;
343 {
344 	register int cnt;
345 	struct stat sb;
346 
347 	if (statok) {
348 		if (fstat(fileno(stdin), &sb))
349 			err(1, "%s", fname);
350 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
351 			address += sb.st_size;
352 			skip -= sb.st_size;
353 			return;
354 		}
355 	}
356 	if (S_ISREG(sb.st_mode)) {
357 		if (fseek(stdin, skip, SEEK_SET))
358 			err(1, "%s", fname);
359 		address += skip;
360 		skip = 0;
361 	} else {
362 		for (cnt = 0; cnt < skip; ++cnt)
363 			if (getchar() == EOF)
364 				break;
365 		address += cnt;
366 		skip -= cnt;
367 	}
368 }
369 
370 void *
371 emalloc(size)
372 	int size;
373 {
374 	void *p;
375 
376 	if ((p = malloc((u_int)size)) == NULL)
377 		nomem();
378 	bzero(p, size);
379 	return(p);
380 }
381 
382 void
383 nomem()
384 {
385 	err(1, NULL);
386 }
387