xref: /dragonfly/usr.bin/hexdump/display.c (revision 7485684f)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)display.c	8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.bin/hexdump/display.c,v 1.4.2.2 2002/07/23 14:27:06 tjr Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "hexdump.h"
43 
44 enum _vflag vflag = FIRST;
45 
46 static off_t address;			/* address/offset in stream */
47 static off_t eaddress;			/* end address */
48 
49 static __inline void print(PR *, u_char *);
50 
51 static __inline void
52 print(PR *pr, u_char *bp)
53 {
54 	long double ldbl;
55 	   double f8;
56 	    float f4;
57 	  int16_t s2;
58 	  int32_t s4;
59 	  int64_t s8;
60 	u_int16_t u2;
61 	u_int32_t u4;
62 	u_int64_t u8;
63 
64 	switch(pr->flags) {
65 	case F_ADDRESS:
66 		(void)printf(pr->fmt, (quad_t)address);
67 		break;
68 	case F_BPAD:
69 		(void)printf(pr->fmt, "");
70 		break;
71 	case F_C:
72 		conv_c(pr, bp);
73 		break;
74 	case F_CHAR:
75 		(void)printf(pr->fmt, *bp);
76 		break;
77 	case F_DBL:
78 		switch(pr->bcnt) {
79 		case 4:
80 			bcopy(bp, &f4, sizeof(f4));
81 			(void)printf(pr->fmt, f4);
82 			break;
83 		case 8:
84 			bcopy(bp, &f8, sizeof(f8));
85 			(void)printf(pr->fmt, f8);
86 			break;
87 		default:
88 			if (pr->bcnt == sizeof(long double)) {
89 				bcopy(bp, &ldbl, sizeof(ldbl));
90 				(void)printf(pr->fmt, ldbl);
91 			}
92 			break;
93 		}
94 		break;
95 	case F_INT:
96 		switch(pr->bcnt) {
97 		case 1:
98 			(void)printf(pr->fmt, (quad_t)(signed char)*bp);
99 			break;
100 		case 2:
101 			bcopy(bp, &s2, sizeof(s2));
102 			(void)printf(pr->fmt, (quad_t)s2);
103 			break;
104 		case 4:
105 			bcopy(bp, &s4, sizeof(s4));
106 			(void)printf(pr->fmt, (quad_t)s4);
107 			break;
108 		case 8:
109 			bcopy(bp, &s8, sizeof(s8));
110 			(void)printf(pr->fmt, s8);
111 			break;
112 		}
113 		break;
114 	case F_P:
115 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
116 		break;
117 	case F_STR:
118 		(void)printf(pr->fmt, (char *)bp);
119 		break;
120 	case F_TEXT:
121 		(void)printf("%s", pr->fmt);
122 		break;
123 	case F_U:
124 		conv_u(pr, bp);
125 		break;
126 	case F_UINT:
127 		switch(pr->bcnt) {
128 		case 1:
129 			(void)printf(pr->fmt, (u_quad_t)*bp);
130 			break;
131 		case 2:
132 			bcopy(bp, &u2, sizeof(u2));
133 			(void)printf(pr->fmt, (u_quad_t)u2);
134 			break;
135 		case 4:
136 			bcopy(bp, &u4, sizeof(u4));
137 			(void)printf(pr->fmt, (u_quad_t)u4);
138 			break;
139 		case 8:
140 			bcopy(bp, &u8, sizeof(u8));
141 			(void)printf(pr->fmt, u8);
142 			break;
143 		}
144 		break;
145 	}
146 }
147 
148 void
149 display(void)
150 {
151 
152 	FS *fs;
153 	FU *fu;
154 	PR *pr;
155 	int cnt;
156 	u_char *bp;
157 	off_t saveaddress;
158 	u_char savech = '\0', *savebp;
159 
160 	while ((bp = get()))
161 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
162 		fs = fs->nextfs, bp = savebp, address = saveaddress)
163 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
164 			if (fu->flags&F_IGNORE)
165 				break;
166 			for (cnt = fu->reps; cnt; --cnt)
167 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
168 				bp += pr->bcnt, pr = pr->nextpr) {
169 				    if (eaddress && address >= eaddress &&
170 					!(pr->flags & (F_TEXT|F_BPAD)))
171 					    bpad(pr);
172 				    if (cnt == 1 && pr->nospace) {
173 					savech = *pr->nospace;
174 					*pr->nospace = '\0';
175 				    }
176 				    print(pr, bp);
177 				    if (cnt == 1 && pr->nospace)
178 					*pr->nospace = savech;
179 			    }
180 		    }
181 	if (endfu) {
182 		/*
183 		 * If eaddress not set, error or file size was multiple of
184 		 * blocksize, and no partial block ever found.
185 		 */
186 		if (!eaddress) {
187 			if (!address)
188 				return;
189 			eaddress = address;
190 		}
191 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
192 			switch(pr->flags) {
193 			case F_ADDRESS:
194 				(void)printf(pr->fmt, (quad_t)eaddress);
195 				break;
196 			case F_TEXT:
197 				(void)printf("%s", pr->fmt);
198 				break;
199 			}
200 	}
201 }
202 
203 void
204 bpad(PR *pr)
205 {
206 	static char const *spec = " -0+#";
207 	char *p1, *p2;
208 
209 	/*
210 	 * Remove all conversion flags; '-' is the only one valid
211 	 * with %s, and it's not useful here.
212 	 */
213 	pr->flags = F_BPAD;
214 	pr->cchar[0] = 's';
215 	pr->cchar[1] = '\0';
216 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
217 	for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
218 	while ((*p2++ = *p1++));
219 }
220 
221 static char **_argv;
222 
223 u_char *
224 get(void)
225 {
226 	static int ateof = 1;
227 	static u_char *curp, *savp;
228 	int n;
229 	int need, nread;
230 	int valid_save = 0;
231 	u_char *tmpp;
232 
233 	if (!curp) {
234 		if ((curp = calloc(1, blocksize)) == NULL)
235 			err(1, NULL);
236 		if ((savp = calloc(1, blocksize)) == NULL)
237 			err(1, NULL);
238 	} else {
239 		tmpp = curp;
240 		curp = savp;
241 		savp = tmpp;
242 		address += blocksize;
243 		valid_save = 1;
244 	}
245 	for (need = blocksize, nread = 0;;) {
246 		/*
247 		 * if read the right number of bytes, or at EOF for one file,
248 		 * and no other files are available, zero-pad the rest of the
249 		 * block and set the end flag.
250 		 */
251 		if (!length || (ateof && !next(NULL))) {
252 			if (odmode && address < skip)
253 				errx(1, "cannot skip past end of input");
254 			if (need == blocksize)
255 				return(NULL);
256 			if (vflag != ALL &&
257 			    valid_save &&
258 			    bcmp(curp, savp, nread) == 0) {
259 				if (vflag != DUP)
260 					(void)printf("*\n");
261 				return(NULL);
262 			}
263 			bzero((char *)curp + nread, need);
264 			eaddress = address + nread;
265 			return(curp);
266 		}
267 		n = fread((char *)curp + nread, sizeof(u_char),
268 		    length == -1 ? need : MIN(length, need), stdin);
269 		if (!n) {
270 			if (ferror(stdin))
271 				warn("%s", _argv[-1]);
272 			ateof = 1;
273 			continue;
274 		}
275 		ateof = 0;
276 		if (length != -1)
277 			length -= n;
278 		if (!(need -= n)) {
279 			if (vflag == ALL || vflag == FIRST ||
280 			    valid_save == 0 ||
281 			    bcmp(curp, savp, blocksize) != 0) {
282 				if (vflag == DUP || vflag == FIRST)
283 					vflag = WAIT;
284 				return(curp);
285 			}
286 			if (vflag == WAIT)
287 				(void)printf("*\n");
288 			vflag = DUP;
289 			address += blocksize;
290 			need = blocksize;
291 			nread = 0;
292 		}
293 		else
294 			nread += n;
295 	}
296 }
297 
298 int
299 next(char **argv)
300 {
301 	static int done;
302 
303 	if (argv) {
304 		_argv = argv;
305 		return(1);
306 	}
307 	for (;;) {
308 		if (*_argv) {
309 			if (!(freopen(*_argv, "r", stdin))) {
310 				warn("%s", *_argv);
311 				exitval = 1;
312 				++_argv;
313 				continue;
314 			}
315 			done = 1;
316 		} else {
317 			if (done++)
318 				return(0);
319 		}
320 		if (skip)
321 			doskip(*_argv ? *_argv : "stdin");
322 		if (*_argv)
323 			++_argv;
324 		if (!skip)
325 			return(1);
326 	}
327 	/* NOTREACHED */
328 }
329 
330 void
331 doskip(const char *fname)
332 {
333 	int cnt;
334 	struct stat sb;
335 
336 	if (fstat(fileno(stdin), &sb))
337 		err(1, "%s", fname);
338 
339 	/* Pseudo-filesystems advertize a zero size. */
340 	if (S_ISREG(sb.st_mode) && sb.st_size > 0) {
341 		if (skip >= sb.st_size) {
342 			address += sb.st_size;
343 			skip -= sb.st_size;
344 		} else {
345 			if (fseeko(stdin, skip, SEEK_SET))
346 				err(1, "%s", fname);
347 			address += skip;
348 			skip = 0;
349 		}
350 	} else {
351 		for (cnt = 0; cnt < skip; ++cnt)
352 			if (getchar() == EOF)
353 				break;
354 		address += cnt;
355 		skip -= cnt;
356 	}
357 }
358