]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/hexdump-display.c
2 * Copyright (c) 1989 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
36 #include <sys/types.h>
49 static void doskip(const char *, int, struct hexdump
*);
50 static u_char
*get(struct hexdump
*);
52 enum _vflag vflag
= FIRST
;
54 static off_t address
; /* address/offset in stream */
55 static off_t eaddress
; /* end address */
57 static const char *color_cond(struct hexdump_pr
*pr
, unsigned char *bp
, int bcnt
)
59 register struct list_head
*p
;
60 register struct hexdump_clr
*clr
;
64 list_for_each(p
, pr
->colorlist
) {
65 clr
= list_entry(p
, struct hexdump_clr
, colorlist
);
69 /* no offset or offset outside this print unit */
72 if (offt
< address
|| offt
+ clr
->range
> address
+ bcnt
)
77 if (pr
->flags
== F_ADDRESS
) {
80 else if (!strncmp(clr
->str
, (char *)bp
+ offt
81 - address
, clr
->range
))
84 } else if (clr
->val
!= -1) {
86 /* addresses are not part of the input, so we can't
87 * compare with the contents of bp */
88 if (pr
->flags
== F_ADDRESS
) {
89 if (clr
->val
== address
)
92 memcpy(&val
, bp
+ offt
- address
, clr
->range
);
96 /* no conditions, only a color was specified */
100 /* return the format string or check for another */
101 if (match
^ clr
->invert
)
111 print(struct hexdump_pr
*pr
, unsigned char *bp
) {
113 const char *color
= NULL
;
115 if (pr
->colorlist
&& (color
= color_cond(pr
, bp
, pr
->bcnt
)))
120 printf(pr
->fmt
, address
);
129 printf(pr
->fmt
, *bp
);
137 memmove(&fval
, bp
, sizeof(fval
));
138 printf(pr
->fmt
, fval
);
141 memmove(&dval
, bp
, sizeof(dval
));
142 printf(pr
->fmt
, dval
);
149 short sval
; /* int16_t */
150 int ival
; /* int32_t */
151 long long Lval
; /* int64_t, int64_t */
155 printf(pr
->fmt
, (unsigned long long) *bp
);
158 memmove(&sval
, bp
, sizeof(sval
));
159 printf(pr
->fmt
, (unsigned long long) sval
);
162 memmove(&ival
, bp
, sizeof(ival
));
163 printf(pr
->fmt
, (unsigned long long) ival
);
166 memmove(&Lval
, bp
, sizeof(Lval
));
167 printf(pr
->fmt
, Lval
);
173 printf(pr
->fmt
, isprint(*bp
) ? *bp
: '.');
176 printf(pr
->fmt
, (char *)bp
);
179 printf("%s", pr
->fmt
);
186 unsigned short sval
; /* u_int16_t */
187 unsigned int ival
; /* u_int32_t */
188 unsigned long long Lval
;/* u_int64_t, u_int64_t */
192 printf(pr
->fmt
, (unsigned long long) *bp
);
195 memmove(&sval
, bp
, sizeof(sval
));
196 printf(pr
->fmt
, (unsigned long long) sval
);
199 memmove(&ival
, bp
, sizeof(ival
));
200 printf(pr
->fmt
, (unsigned long long) ival
);
203 memmove(&Lval
, bp
, sizeof(Lval
));
204 printf(pr
->fmt
, Lval
);
210 if (color
) /* did we colorize something? */
214 static void bpad(struct hexdump_pr
*pr
)
216 static const char *spec
= " -0+#";
220 * remove all conversion flags; '-' is the only one valid
221 * with %s, and it's not useful here.
232 while (*p1
&& strchr(spec
, *p1
))
235 while ((*p2
++ = *p1
++))
239 void display(struct hexdump
*hex
)
241 register struct list_head
*fs
;
242 register struct hexdump_fs
*fss
;
243 register struct hexdump_fu
*fu
;
244 register struct hexdump_pr
*pr
;
246 register unsigned char *bp
;
248 unsigned char savech
= 0, *savebp
;
249 struct list_head
*p
, *q
, *r
;
251 while ((bp
= get(hex
)) != NULL
) {
252 fs
= &hex
->fshead
; savebp
= bp
; saveaddress
= address
;
254 list_for_each(p
, fs
) {
255 fss
= list_entry(p
, struct hexdump_fs
, fslist
);
257 list_for_each(q
, &fss
->fulist
) {
258 fu
= list_entry(q
, struct hexdump_fu
, fulist
);
260 if (fu
->flags
&F_IGNORE
)
266 list_for_each(r
, &fu
->prlist
) {
267 pr
= list_entry(r
, struct hexdump_pr
, prlist
);
269 if (eaddress
&& address
>= eaddress
270 && !(pr
->flags
&(F_TEXT
|F_BPAD
)))
273 if (cnt
== 1 && pr
->nospace
) {
274 savech
= *pr
->nospace
;
277 *pr
->nospace
= savech
;
288 address
= saveaddress
;
293 * if eaddress not set, error or file size was multiple of
294 * blocksize, and no partial block ever found.
301 list_for_each (p
, &endfu
->prlist
) {
302 const char *color
= NULL
;
304 pr
= list_entry(p
, struct hexdump_pr
, prlist
);
305 if (colors_wanted() && pr
->colorlist
306 && (color
= color_cond(pr
, bp
, pr
->bcnt
))) {
312 printf(pr
->fmt
, eaddress
);
315 printf("%s", pr
->fmt
);
318 if (color
) /* did we highlight something? */
327 get(struct hexdump
*hex
)
329 static int ateof
= 1;
330 static u_char
*curp
, *savp
;
331 ssize_t n
, need
, nread
;
335 curp
= xcalloc(1, hex
->blocksize
);
336 savp
= xcalloc(1, hex
->blocksize
);
341 address
+= hex
->blocksize
;
343 need
= hex
->blocksize
, nread
= 0;
346 * if read the right number of bytes, or at EOF for one file,
347 * and no other files are available, zero-pad the rest of the
348 * block and set the end flag.
350 if (!hex
->length
|| (ateof
&& !next(NULL
, hex
))) {
351 if (need
== hex
->blocksize
)
353 if (!need
&& vflag
!= ALL
&&
354 !memcmp(curp
, savp
, nread
)) {
360 memset((char *)curp
+ nread
, 0, need
);
361 eaddress
= address
+ nread
;
364 if (fileno(stdin
) == -1) {
365 warnx(_("all input file arguments failed"));
368 n
= fread((char *)curp
+ nread
, sizeof(unsigned char),
369 hex
->length
== -1 ? need
: min(hex
->length
, need
), stdin
);
372 warn("%s", _argv
[-1]);
377 if (hex
->length
!= -1)
380 if (vflag
== ALL
|| vflag
== FIRST
||
381 memcmp(curp
, savp
, hex
->blocksize
)) {
382 if (vflag
== DUP
|| vflag
== FIRST
)
389 address
+= hex
->blocksize
;
390 need
= hex
->blocksize
;
402 int next(char **argv
, struct hexdump
*hex
)
413 if (!(freopen(*_argv
, "r", stdin
))) {
415 hex
->exitval
= EXIT_FAILURE
;
426 doskip(statok
? *_argv
: "stdin", statok
, hex
);
436 doskip(const char *fname
, int statok
, struct hexdump
*hex
)
441 if (fstat(fileno(stdin
), &sbuf
))
442 err(EXIT_FAILURE
, "%s", fname
);
443 if (S_ISREG(sbuf
.st_mode
) && hex
->skip
> sbuf
.st_size
) {
444 /* If size valid and skip >= size */
445 hex
->skip
-= sbuf
.st_size
;
446 address
+= sbuf
.st_size
;
450 /* sbuf may be undefined here - do not test it */
451 if (fseek(stdin
, hex
->skip
, SEEK_SET
))
452 err(EXIT_FAILURE
, "%s", fname
);
453 address
+= hex
->skip
;