]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/hexdump/display.c
zfs: merge openzfs/zfs@2e2a46e0a
[FreeBSD/FreeBSD.git] / usr.bin / hexdump / display.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)display.c   8.1 (Berkeley) 6/6/93";
35 #endif
36 #endif /* not lint */
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43
44 #include <capsicum_helpers.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "hexdump.h"
53
54 enum _vflag vflag = FIRST;
55
56 static off_t address;                   /* address/offset in stream */
57 static off_t eaddress;                  /* end address */
58
59 static void print(PR *, u_char *);
60 static void noseek(void);
61
62 void
63 display(void)
64 {
65         FS *fs;
66         FU *fu;
67         PR *pr;
68         int cnt;
69         u_char *bp;
70         off_t saveaddress;
71         u_char savech, *savebp;
72
73         savech = 0;
74         while ((bp = get()))
75             for (fs = fshead, savebp = bp, saveaddress = address; fs;
76                 fs = fs->nextfs, bp = savebp, address = saveaddress)
77                     for (fu = fs->nextfu; fu; fu = fu->nextfu) {
78                         if (fu->flags&F_IGNORE)
79                                 break;
80                         for (cnt = fu->reps; cnt; --cnt)
81                             for (pr = fu->nextpr; pr; address += pr->bcnt,
82                                 bp += pr->bcnt, pr = pr->nextpr) {
83                                     if (eaddress && address >= eaddress &&
84                                         !(pr->flags & (F_TEXT|F_BPAD)))
85                                             bpad(pr);
86                                     if (cnt == 1 && pr->nospace) {
87                                         savech = *pr->nospace;
88                                         *pr->nospace = '\0';
89                                     }
90                                     print(pr, bp);
91                                     if (cnt == 1 && pr->nospace)
92                                         *pr->nospace = savech;
93                             }
94                     }
95         if (endfu) {
96                 /*
97                  * If eaddress not set, error or file size was multiple of
98                  * blocksize, and no partial block ever found.
99                  */
100                 if (!eaddress) {
101                         if (!address)
102                                 return;
103                         eaddress = address;
104                 }
105                 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
106                         switch(pr->flags) {
107                         case F_ADDRESS:
108                                 (void)printf(pr->fmt, (quad_t)eaddress);
109                                 break;
110                         case F_TEXT:
111                                 (void)printf("%s", pr->fmt);
112                                 break;
113                         }
114         }
115 }
116
117 static void
118 print(PR *pr, u_char *bp)
119 {
120         long double ldbl;
121            double f8;
122             float f4;
123           int16_t s2;
124           int32_t s4;
125           int64_t s8;
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, eaddress ? eaddress - address :
139                     blocksize - address % blocksize);
140                 break;
141         case F_CHAR:
142                 (void)printf(pr->fmt, *bp);
143                 break;
144         case F_DBL:
145                 switch(pr->bcnt) {
146                 case 4:
147                         bcopy(bp, &f4, sizeof(f4));
148                         (void)printf(pr->fmt, f4);
149                         break;
150                 case 8:
151                         bcopy(bp, &f8, sizeof(f8));
152                         (void)printf(pr->fmt, f8);
153                         break;
154                 default:
155                         if (pr->bcnt == sizeof(long double)) {
156                                 bcopy(bp, &ldbl, sizeof(ldbl));
157                                 (void)printf(pr->fmt, ldbl);
158                         }
159                         break;
160                 }
161                 break;
162         case F_INT:
163                 switch(pr->bcnt) {
164                 case 1:
165                         (void)printf(pr->fmt, (quad_t)(signed char)*bp);
166                         break;
167                 case 2:
168                         bcopy(bp, &s2, sizeof(s2));
169                         (void)printf(pr->fmt, (quad_t)s2);
170                         break;
171                 case 4:
172                         bcopy(bp, &s4, sizeof(s4));
173                         (void)printf(pr->fmt, (quad_t)s4);
174                         break;
175                 case 8:
176                         bcopy(bp, &s8, sizeof(s8));
177                         (void)printf(pr->fmt, s8);
178                         break;
179                 }
180                 break;
181         case F_P:
182                 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
183                 break;
184         case F_STR:
185                 (void)printf(pr->fmt, (char *)bp);
186                 break;
187         case F_TEXT:
188                 (void)printf("%s", pr->fmt);
189                 break;
190         case F_U:
191                 conv_u(pr, bp);
192                 break;
193         case F_UINT:
194                 switch(pr->bcnt) {
195                 case 1:
196                         (void)printf(pr->fmt, (u_quad_t)*bp);
197                         break;
198                 case 2:
199                         bcopy(bp, &u2, sizeof(u2));
200                         (void)printf(pr->fmt, (u_quad_t)u2);
201                         break;
202                 case 4:
203                         bcopy(bp, &u4, sizeof(u4));
204                         (void)printf(pr->fmt, (u_quad_t)u4);
205                         break;
206                 case 8:
207                         bcopy(bp, &u8, sizeof(u8));
208                         (void)printf(pr->fmt, u8);
209                         break;
210                 }
211                 break;
212         }
213 }
214
215 void
216 bpad(PR *pr)
217 {
218         static char const *spec = " -0+#";
219         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 && strchr(spec, *p1); ++p1);
230         while ((*p2++ = *p1++));
231 }
232
233 static char **_argv;
234
235 u_char *
236 get(void)
237 {
238         static int ateof = 1;
239         static u_char *curp, *savp;
240         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 && skip > 0)
265                                 errx(1, "cannot skip past end of input");
266                         if (need == blocksize)
267                                 return((u_char *)NULL);
268                         /*
269                          * XXX bcmp() is not quite right in the presence
270                          * of multibyte characters.
271                          */
272                         if (need == 0 && vflag != ALL &&
273                             valid_save && 
274                             bcmp(curp, savp, nread) == 0) {
275                                 if (vflag != DUP) {
276                                         (void)printf("*\n");
277                                         (void)fflush(stdout);
278                                 }
279                                 return((u_char *)NULL);
280                         }
281                         bzero((char *)curp + nread, need);
282                         eaddress = address + nread;
283                         return(curp);
284                 }
285                 n = fread((char *)curp + nread, sizeof(u_char),
286                     length == -1 ? need : MIN(length, need), stdin);
287                 if (!n) {
288                         if (ferror(stdin))
289                                 warn("%s", _argv[-1]);
290                         ateof = 1;
291                         continue;
292                 }
293                 ateof = 0;
294                 if (length != -1)
295                         length -= n;
296                 if (!(need -= n)) {
297                         /*
298                          * XXX bcmp() is not quite right in the presence
299                          * of multibyte characters.
300                          */
301                         if (vflag == ALL || vflag == FIRST ||
302                             valid_save == 0 ||
303                             bcmp(curp, savp, blocksize) != 0) {
304                                 if (vflag == DUP || vflag == FIRST)
305                                         vflag = WAIT;
306                                 return(curp);
307                         }
308                         if (vflag == WAIT) {
309                                 (void)printf("*\n");
310                                 (void)fflush(stdout);
311                         }
312                         vflag = DUP;
313                         address += blocksize;
314                         need = blocksize;
315                         nread = 0;
316                 }
317                 else
318                         nread += n;
319         }
320 }
321
322 size_t
323 peek(u_char *buf, size_t nbytes)
324 {
325         size_t n, nread;
326         int c;
327
328         if (length != -1 && nbytes > (unsigned int)length)
329                 nbytes = length;
330         nread = 0;
331         while (nread < nbytes && (c = getchar()) != EOF) {
332                 *buf++ = c;
333                 nread++;
334         }
335         n = nread;
336         while (n-- > 0) {
337                 c = *--buf;
338                 ungetc(c, stdin);
339         }
340         return (nread);
341 }
342
343 int
344 next(char **argv)
345 {
346         static int done;
347         int statok;
348
349         if (argv) {
350                 _argv = argv;
351                 return(1);
352         }
353         for (;;) {
354                 if (*_argv) {
355                         done = 1;
356                         if (!(freopen(*_argv, "r", stdin))) {
357                                 warn("%s", *_argv);
358                                 exitval = 1;
359                                 ++_argv;
360                                 continue;
361                         }
362                         statok = 1;
363                 } else {
364                         if (done++)
365                                 return(0);
366                         statok = 0;
367                 }
368
369                 if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
370                         err(1, "unable to restrict %s",
371                             statok ? *_argv : "stdin");
372
373                 /*
374                  * We've opened our last input file; enter capsicum sandbox.
375                  */
376                 if (statok == 0 || *(_argv + 1) == NULL) {
377                         if (caph_enter() < 0)
378                                 err(1, "unable to enter capability mode");
379                 }
380
381                 if (skip)
382                         doskip(statok ? *_argv : "stdin", statok);
383                 if (*_argv)
384                         ++_argv;
385                 if (!skip)
386                         return(1);
387         }
388         /* NOTREACHED */
389 }
390
391 void
392 doskip(const char *fname, int statok)
393 {
394         int type;
395         struct stat sb;
396
397         if (statok) {
398                 if (fstat(fileno(stdin), &sb))
399                         err(1, "%s", fname);
400                 if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
401                         address += sb.st_size;
402                         skip -= sb.st_size;
403                         return;
404                 }
405         }
406         if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
407                 noseek();
408                 return;
409         }
410         if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
411                 if (ioctl(fileno(stdin), FIODTYPE, &type))
412                         err(1, "%s", fname);
413                 /*
414                  * Most tape drives don't support seeking,
415                  * yet fseek() would succeed.
416                  */
417                 if (type & D_TAPE) {
418                         noseek();
419                         return;
420                 }
421         }
422         if (fseeko(stdin, skip, SEEK_SET)) {
423                 noseek();
424                 return;
425         }
426         address += skip;
427         skip = 0;
428 }
429
430 static void
431 noseek(void)
432 {
433         int count;
434         for (count = 0; count < skip; ++count)
435                 if (getchar() == EOF)
436                         break;
437         address += count;
438         skip -= count;
439 }