1 /* vi: set sw=4 ts=4: */
3 * Support code for the hexdump and od applets,
4 * based on code from util-linux v 2.11l
7 * The Regents of the University of California. All rights reserved.
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 * Original copyright notice is retained at the end of this file.
17 #include <ctype.h> /* for isdigit() */
20 enum _vflag bb_dump_vflag = FIRST;
21 FS *bb_dump_fshead; /* head of format strings */
24 static off_t savaddress; /* saved address/offset in stream */
25 static off_t eaddress; /* end address */
26 static off_t address; /* address/offset in stream */
27 off_t bb_dump_skip; /* bytes to skip */
28 static int exitval; /* final exit value */
29 int bb_dump_blocksize; /* data block size */
30 int bb_dump_length = -1; /* max bytes to read */
32 static const char index_str[] = ".#-+ 0123456789";
34 static const char size_conv_str[] =
35 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
37 static const char lcc[] = "diouxX";
39 int bb_dump_size(FS * fs)
47 /* figure out the data block bb_dump_size needed for each format unit */
48 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
50 cur_size += fu->bcnt * fu->reps;
53 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
57 * bb_dump_skip any special chars -- save precision in
58 * case it's a %s format.
60 while (strchr(index_str + 1, *++fmt));
61 if (*fmt == '.' && isdigit(*++fmt)) {
63 while (isdigit(*++fmt));
65 if (!(p = strchr(size_conv_str + 12, *fmt))) {
68 } else if (*fmt == '_') {
70 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
75 bcnt += size_conv_str[p - (size_conv_str + 12)];
78 cur_size += bcnt * fu->reps;
83 static void rewrite(FS * fs)
85 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
86 PR *pr, **nextpr = NULL;
90 const char *byte_count_str;
93 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
95 * break each format unit into print units; each
96 * conversion character gets its own.
98 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
100 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
101 pr = xzalloc(sizeof(PR));
104 /* ignore nextpr -- its unused inside the loop and is
105 * uninitialized 1st time thru.
108 /* bb_dump_skip preceding text and up to the next % sign */
109 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
111 /* only text in the string */
119 * get precision for %s -- if have a byte count, don't
124 /* bb_dump_skip to conversion character */
125 for (++p1; strchr(index_str, *p1); ++p1);
127 /* bb_dump_skip any special chars, field width */
128 while (strchr(index_str + 1, *++p1));
129 if (*p1 == '.' && isdigit(*++p1)) {
132 while (isdigit(*++p1));
137 p2 = p1 + 1; /* set end pointer */
140 * figure out the byte count for each conversion;
141 * rewrite the format as necessary, set up blank-
142 * pbb_dump_adding for end of data.
148 byte_count_str = "\001";
152 if (fu->bcnt == *byte_count_str) {
155 } while (*++byte_count_str);
157 /* Unlike the original, output the remainder of the format string. */
158 if (!*byte_count_str) {
159 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
161 pr->bcnt = *byte_count_str;
162 } else if (*p1 == 'l') {
168 if (!(e = strchr(lcc, *p1))) {
169 goto DO_BAD_CONV_CHAR;
175 byte_count_str = "\004\002\001";
179 } else if (strchr(lcc, *p1)) {
181 } else if (strchr("eEfgG", *p1)) {
183 byte_count_str = "\010\004";
185 } else if (*p1 == 's') {
187 if (sokay == USEBCNT) {
189 } else if (sokay == USEPREC) {
191 } else { /* NOTOKAY */
192 bb_error_msg_and_die("%%s requires a precision or a byte count.");
194 } else if (*p1 == '_') {
199 fu->flags |= F_IGNORE;
202 pr->flags = F_ADDRESS;
204 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
205 goto DO_BAD_CONV_CHAR;
211 /* *p1 = 'c'; set in conv_c */
212 goto DO_BYTE_COUNT_1;
216 goto DO_BYTE_COUNT_1;
219 /* *p1 = 'c'; set in conv_u */
220 goto DO_BYTE_COUNT_1;
222 goto DO_BAD_CONV_CHAR;
226 bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
230 * copy to PR format string, set conversion character
231 * pointer, update original.
235 pr->fmt = bb_xstrdup(fmtp);
237 pr->cchar = pr->fmt + (p1 - fmtp);
239 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
240 * Skip subsequent text and up to the next % sign and tack the
241 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
242 * we lose the " is a HEX number" part of fmt.
244 for (p3 = p2; *p3 && *p3 != '%'; p3++);
249 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
257 /* only one conversion character if byte count */
258 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
259 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
263 * if format unit byte count not specified, figure it out
264 * so can adjust rep count later.
267 for (pr = fu->nextpr; pr; pr = pr->nextpr)
268 fu->bcnt += pr->bcnt;
271 * if the format string interprets any data at all, and it's
272 * not the same as the bb_dump_blocksize, and its last format unit
273 * interprets any data at all, and has no iteration count,
274 * repeat it as necessary.
276 * if, rep count is greater than 1, no trailing whitespace
277 * gets output from the last iteration of the format unit.
279 for (fu = fs->nextfu;; fu = fu->nextfu) {
280 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
281 !(fu->flags & F_SETREP) && fu->bcnt)
282 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
284 for (pr = fu->nextpr;; pr = pr->nextpr)
287 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
288 p2 = isspace(*p1) ? p1 : NULL;
297 static void do_skip(char *fname, int statok)
302 if (fstat(STDIN_FILENO, &sbuf)) {
303 bb_perror_msg_and_die("%s", fname);
305 if ((!(S_ISCHR(sbuf.st_mode) ||
306 S_ISBLK(sbuf.st_mode) ||
307 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
308 /* If bb_dump_size valid and bb_dump_skip >= size */
309 bb_dump_skip -= sbuf.st_size;
310 address += sbuf.st_size;
314 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
315 bb_perror_msg_and_die("%s", fname);
317 savaddress = address += bb_dump_skip;
321 static int next(char **argv)
332 if (!(freopen(*_argv, "r", stdin))) {
333 bb_perror_msg("%s", *_argv);
345 do_skip(statok ? *_argv : "stdin", statok);
354 static unsigned char *get(void)
356 static int ateof = 1;
357 static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
363 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
364 curp = (unsigned char *) xmalloc(bb_dump_blocksize);
365 savp = (unsigned char *) xmalloc(bb_dump_blocksize);
370 address = savaddress += bb_dump_blocksize;
372 for (need = bb_dump_blocksize, nread = 0;;) {
374 * if read the right number of bytes, or at EOF for one file,
375 * and no other files are available, zero-pad the rest of the
376 * block and set the end flag.
378 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
379 if (need == bb_dump_blocksize) {
380 return ((unsigned char *) NULL);
382 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
383 if (bb_dump_vflag != DUP) {
386 return ((unsigned char *) NULL);
388 memset((char *) curp + nread, 0, need);
389 eaddress = address + nread;
392 n = fread((char *) curp + nread, sizeof(unsigned char),
393 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
396 bb_perror_msg("%s", _argv[-1]);
402 if (bb_dump_length != -1) {
406 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
407 || memcmp(curp, savp, bb_dump_blocksize)) {
408 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
409 bb_dump_vflag = WAIT;
413 if (bb_dump_vflag == WAIT) {
417 address = savaddress += bb_dump_blocksize;
418 need = bb_dump_blocksize;
426 static void bpad(PR * pr)
431 * remove all conversion flags; '-' is the only one valid
432 * with %s, and it's not useful here.
436 for (p1 = pr->fmt; *p1 != '%'; ++p1);
437 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
438 if (pr->nospace) pr->nospace--;
439 while ((*p2++ = *p1++) != 0);
442 static const char conv_str[] =
454 static void conv_c(PR * pr, unsigned char * p)
456 const char *str = conv_str;
469 (void) printf(pr->fmt, *p);
471 sprintf(buf, "%03o", (int) *p);
475 printf(pr->fmt, str);
479 static void conv_u(PR * pr, unsigned char * p)
481 static const char list[] =
482 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
483 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
484 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
485 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
487 /* od used nl, not lf */
490 printf(pr->fmt, list + (4 * (int)*p));
491 } else if (*p == 0x7f) {
493 printf(pr->fmt, "del");
494 } else if (isprint(*p)) {
499 printf(pr->fmt, (int) *p);
503 static void display(void)
505 /* extern FU *endfu; */
513 unsigned char savech = 0, *savebp;
515 while ((bp = get()) != NULL) {
516 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
517 fs = fs->nextfs, bp = savebp, address = saveaddress) {
518 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
519 if (fu->flags & F_IGNORE) {
522 for (cnt = fu->reps; cnt; --cnt) {
523 for (pr = fu->nextpr; pr; address += pr->bcnt,
524 bp += pr->bcnt, pr = pr->nextpr) {
525 if (eaddress && address >= eaddress &&
526 !(pr->flags & (F_TEXT | F_BPAD))) {
529 if (cnt == 1 && pr->nospace) {
530 savech = *pr->nospace;
536 printf(pr->fmt, (unsigned int) address);
545 printf(pr->fmt, *bp);
553 memmove((char *) &fval, (char *) bp,
555 printf(pr->fmt, fval);
558 memmove((char *) &dval, (char *) bp,
560 printf(pr->fmt, dval);
571 printf(pr->fmt, (int) *bp);
574 memmove((char *) &sval, (char *) bp,
576 printf(pr->fmt, (int) sval);
579 memmove((char *) &ival, (char *) bp,
581 printf(pr->fmt, ival);
587 printf(pr->fmt, isprint(*bp) ? *bp : '.');
590 printf(pr->fmt, (char *) bp);
604 printf(pr->fmt, (unsigned int) * bp);
607 memmove((char *) &sval, (char *) bp,
609 printf(pr->fmt, (unsigned int) sval);
612 memmove((char *) &ival, (char *) bp,
614 printf(pr->fmt, ival);
620 if (cnt == 1 && pr->nospace) {
621 *pr->nospace = savech;
630 * if eaddress not set, error or file bb_dump_size was multiple of
631 * bb_dump_blocksize, and no partial block ever found.
639 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
642 (void) printf(pr->fmt, (unsigned int) eaddress);
645 (void) printf(pr->fmt);
652 int bb_dump_dump(char **argv)
656 /* figure out the data block bb_dump_size */
657 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
658 tfs->bcnt = bb_dump_size(tfs);
659 if (bb_dump_blocksize < tfs->bcnt) {
660 bb_dump_blocksize = tfs->bcnt;
663 /* rewrite the rules, do syntax checking */
664 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
674 void bb_dump_add(const char *fmt)
684 /* start new linked list of format units */
685 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
686 if (!bb_dump_fshead) {
687 bb_dump_fshead = tfs;
691 nextfs = &tfs->nextfs;
692 nextfu = &tfs->nextfu;
694 /* take the format string and break it up into format units */
696 /* bb_dump_skip leading white space */
697 p = skip_whitespace(p);
702 /* allocate a new format unit and link it in */
704 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
705 tfu = xzalloc(sizeof(FU));
707 nextfu = &tfu->nextfu;
710 /* if leading digit, repetition count */
712 for (savep = p; isdigit(*p); ++p);
713 if (!isspace(*p) && *p != '/') {
714 bb_error_msg_and_die("bad format {%s}", fmt);
716 /* may overwrite either white space or slash */
717 tfu->reps = atoi(savep);
718 tfu->flags = F_SETREP;
719 /* bb_dump_skip trailing white space */
720 p = skip_whitespace(++p);
723 /* bb_dump_skip slash and trailing white space */
725 p = skip_whitespace(++p);
730 for (savep = p; isdigit(*p); ++p);
732 bb_error_msg_and_die("bad format {%s}", fmt);
734 tfu->bcnt = atoi(savep);
735 /* bb_dump_skip trailing white space */
736 p = skip_whitespace(++p);
741 bb_error_msg_and_die("bad format {%s}", fmt);
743 for (savep = ++p; *p != '"';) {
745 bb_error_msg_and_die("bad format {%s}", fmt);
748 tfu->fmt = xmalloc(p - savep + 1);
749 strncpy(tfu->fmt, savep, p - savep);
750 tfu->fmt[p - savep] = '\0';
751 /* escape(tfu->fmt); */
755 /* alphabetic escape sequences have to be done in place */
756 for (p2 = p1;; ++p1, ++p2) {
762 const char *cs = conv_str + 4;
780 * Copyright (c) 1989 The Regents of the University of California.
781 * All rights reserved.
783 * Redistribution and use in source and binary forms, with or without
784 * modification, are permitted provided that the following conditions
786 * 1. Redistributions of source code must retain the above copyright
787 * notice, this list of conditions and the following disclaimer.
788 * 2. Redistributions in binary form must reproduce the above copyright
789 * notice, this list of conditions and the following disclaimer in the
790 * documentation and/or other materials provided with the distribution.
791 * 3. Neither the name of the University nor the names of its contributors
792 * may be used to endorse or promote products derived from this software
793 * without specific prior written permission.
795 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
796 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
797 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
798 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
799 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
800 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
801 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
802 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
803 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
804 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF