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 enum _vflag bb_dump_vflag = FIRST;
18 FS *bb_dump_fshead; /* head of format strings */
21 static off_t savaddress; /* saved address/offset in stream */
22 static off_t eaddress; /* end address */
23 static off_t address; /* address/offset in stream */
24 off_t bb_dump_skip; /* bytes to skip */
25 static int exitval; /* final exit value */
26 int bb_dump_blocksize; /* data block size */
27 int bb_dump_length = -1; /* max bytes to read */
29 static const char index_str[] ALIGN1 = ".#-+ 0123456789";
31 static const char size_conv_str[] ALIGN1 =
32 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
34 static const char lcc[] ALIGN1 = "diouxX";
36 int bb_dump_size(FS * fs)
44 /* figure out the data block bb_dump_size needed for each format unit */
45 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
47 cur_size += fu->bcnt * fu->reps;
50 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
54 * bb_dump_skip any special chars -- save precision in
55 * case it's a %s format.
57 while (strchr(index_str + 1, *++fmt));
58 if (*fmt == '.' && isdigit(*++fmt)) {
60 while (isdigit(*++fmt));
62 if (!(p = strchr(size_conv_str + 12, *fmt))) {
65 } else if (*fmt == '_') {
67 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
72 bcnt += size_conv_str[p - (size_conv_str + 12)];
75 cur_size += bcnt * fu->reps;
80 static void rewrite(FS * fs)
82 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
83 PR *pr, **nextpr = NULL;
87 const char *byte_count_str;
90 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
92 * break each format unit into print units; each
93 * conversion character gets its own.
95 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
97 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
98 pr = xzalloc(sizeof(PR));
101 /* ignore nextpr -- its unused inside the loop and is
102 * uninitialized 1st time thru.
105 /* bb_dump_skip preceding text and up to the next % sign */
106 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
108 /* only text in the string */
116 * get precision for %s -- if have a byte count, don't
121 /* bb_dump_skip to conversion character */
122 for (++p1; strchr(index_str, *p1); ++p1);
124 /* bb_dump_skip any special chars, field width */
125 while (strchr(index_str + 1, *++p1));
126 if (*p1 == '.' && isdigit(*++p1)) {
129 while (isdigit(*++p1));
134 p2 = p1 + 1; /* set end pointer */
137 * figure out the byte count for each conversion;
138 * rewrite the format as necessary, set up blank-
139 * pbb_dump_adding for end of data.
145 byte_count_str = "\001";
149 if (fu->bcnt == *byte_count_str) {
152 } while (*++byte_count_str);
154 /* Unlike the original, output the remainder of the format string. */
155 if (!*byte_count_str) {
156 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
158 pr->bcnt = *byte_count_str;
159 } else if (*p1 == 'l') {
165 if (!(e = strchr(lcc, *p1))) {
166 goto DO_BAD_CONV_CHAR;
172 byte_count_str = "\004\002\001";
176 } else if (strchr(lcc, *p1)) {
178 } else if (strchr("eEfgG", *p1)) {
180 byte_count_str = "\010\004";
182 } else if (*p1 == 's') {
184 if (sokay == USEBCNT) {
186 } else if (sokay == USEPREC) {
188 } else { /* NOTOKAY */
189 bb_error_msg_and_die("%%s requires a precision or a byte count");
191 } else if (*p1 == '_') {
196 fu->flags |= F_IGNORE;
199 pr->flags = F_ADDRESS;
201 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
202 goto DO_BAD_CONV_CHAR;
208 /* *p1 = 'c'; set in conv_c */
209 goto DO_BYTE_COUNT_1;
213 goto DO_BYTE_COUNT_1;
216 /* *p1 = 'c'; set in conv_u */
217 goto DO_BYTE_COUNT_1;
219 goto DO_BAD_CONV_CHAR;
223 bb_error_msg_and_die("bad conversion character %%%s", p1);
227 * copy to PR format string, set conversion character
228 * pointer, update original.
232 pr->fmt = xstrdup(fmtp);
234 pr->cchar = pr->fmt + (p1 - fmtp);
236 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
237 * Skip subsequent text and up to the next % sign and tack the
238 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
239 * we lose the " is a HEX number" part of fmt.
241 for (p3 = p2; *p3 && *p3 != '%'; p3++);
246 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
254 /* only one conversion character if byte count */
255 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
256 bb_error_msg_and_die("byte count with multiple conversion characters");
260 * if format unit byte count not specified, figure it out
261 * so can adjust rep count later.
264 for (pr = fu->nextpr; pr; pr = pr->nextpr)
265 fu->bcnt += pr->bcnt;
268 * if the format string interprets any data at all, and it's
269 * not the same as the bb_dump_blocksize, and its last format unit
270 * interprets any data at all, and has no iteration count,
271 * repeat it as necessary.
273 * if, rep count is greater than 1, no trailing whitespace
274 * gets output from the last iteration of the format unit.
276 for (fu = fs->nextfu;; fu = fu->nextfu) {
277 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
278 !(fu->flags & F_SETREP) && fu->bcnt)
279 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
281 for (pr = fu->nextpr;; pr = pr->nextpr)
284 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
285 p2 = isspace(*p1) ? p1 : NULL;
294 static void do_skip(const char *fname, int statok)
299 if (fstat(STDIN_FILENO, &sbuf)) {
300 bb_perror_msg_and_die("%s", fname);
302 if ((!(S_ISCHR(sbuf.st_mode) ||
303 S_ISBLK(sbuf.st_mode) ||
304 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
305 /* If bb_dump_size valid and bb_dump_skip >= size */
306 bb_dump_skip -= sbuf.st_size;
307 address += sbuf.st_size;
311 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
312 bb_perror_msg_and_die("%s", fname);
314 savaddress = address += bb_dump_skip;
318 static int next(char **argv)
320 static smallint done;
330 if (!(freopen(*_argv, "r", stdin))) {
331 bb_perror_msg("%s", *_argv);
344 do_skip(statok ? *_argv : "stdin", statok);
353 static unsigned char *get(void)
355 static smallint ateof = 1;
356 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 = xmalloc(bb_dump_blocksize);
365 savp = 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) {
382 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
383 if (bb_dump_vflag != DUP) {
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) {
407 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
408 || memcmp(curp, savp, bb_dump_blocksize)) {
409 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
410 bb_dump_vflag = WAIT;
414 if (bb_dump_vflag == WAIT) {
418 address = savaddress += bb_dump_blocksize;
419 need = bb_dump_blocksize;
427 static void bpad(PR * pr)
432 * remove all conversion flags; '-' is the only one valid
433 * with %s, and it's not useful here.
437 for (p1 = pr->fmt; *p1 != '%'; ++p1);
438 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
439 if (pr->nospace) pr->nospace--;
440 while ((*p2++ = *p1++) != 0);
443 static const char conv_str[] ALIGN1 =
455 static void conv_c(PR * pr, unsigned char * p)
457 const char *str = conv_str;
470 (void) printf(pr->fmt, *p);
472 sprintf(buf, "%03o", (int) *p);
476 printf(pr->fmt, str);
480 static void conv_u(PR * pr, unsigned char * p)
482 static const char list[] ALIGN1 =
483 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
484 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
485 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
486 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
488 /* od used nl, not lf */
491 printf(pr->fmt, list + (4 * (int)*p));
492 } else if (*p == 0x7f) {
494 printf(pr->fmt, "del");
495 } else if (isprint(*p)) {
500 printf(pr->fmt, (int) *p);
504 static void display(void)
506 /* extern FU *endfu; */
514 unsigned char savech = 0, *savebp;
516 while ((bp = get()) != NULL) {
517 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
518 fs = fs->nextfs, bp = savebp, address = saveaddress) {
519 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
520 if (fu->flags & F_IGNORE) {
523 for (cnt = fu->reps; cnt; --cnt) {
524 for (pr = fu->nextpr; pr; address += pr->bcnt,
525 bp += pr->bcnt, pr = pr->nextpr) {
526 if (eaddress && address >= eaddress &&
527 !(pr->flags & (F_TEXT | F_BPAD))) {
530 if (cnt == 1 && pr->nospace) {
531 savech = *pr->nospace;
537 printf(pr->fmt, (unsigned int) address);
546 printf(pr->fmt, *bp);
554 memmove((char *) &fval, (char *) bp,
556 printf(pr->fmt, fval);
559 memmove((char *) &dval, (char *) bp,
561 printf(pr->fmt, dval);
572 printf(pr->fmt, (int) *bp);
575 memmove((char *) &sval, (char *) bp,
577 printf(pr->fmt, (int) sval);
580 memmove((char *) &ival, (char *) bp,
582 printf(pr->fmt, ival);
588 printf(pr->fmt, isprint(*bp) ? *bp : '.');
591 printf(pr->fmt, (char *) bp);
605 printf(pr->fmt, (unsigned int) * bp);
608 memmove((char *) &sval, (char *) bp,
610 printf(pr->fmt, (unsigned int) sval);
613 memmove((char *) &ival, (char *) bp,
615 printf(pr->fmt, ival);
621 if (cnt == 1 && pr->nospace) {
622 *pr->nospace = savech;
631 * if eaddress not set, error or file bb_dump_size was multiple of
632 * bb_dump_blocksize, and no partial block ever found.
640 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
643 (void) printf(pr->fmt, (unsigned int) eaddress);
646 (void) printf(pr->fmt);
653 int bb_dump_dump(char **argv)
657 /* figure out the data block bb_dump_size */
658 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
659 tfs->bcnt = bb_dump_size(tfs);
660 if (bb_dump_blocksize < tfs->bcnt) {
661 bb_dump_blocksize = tfs->bcnt;
664 /* rewrite the rules, do syntax checking */
665 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
675 void bb_dump_add(const char *fmt)
685 /* start new linked list of format units */
686 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
687 if (!bb_dump_fshead) {
688 bb_dump_fshead = tfs;
692 nextfs = &tfs->nextfs;
693 nextfu = &tfs->nextfu;
695 /* take the format string and break it up into format units */
697 /* bb_dump_skip leading white space */
698 p = skip_whitespace(p);
703 /* allocate a new format unit and link it in */
705 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
706 tfu = xzalloc(sizeof(FU));
708 nextfu = &tfu->nextfu;
711 /* if leading digit, repetition count */
713 for (savep = p; isdigit(*p); ++p);
714 if (!isspace(*p) && *p != '/') {
715 bb_error_msg_and_die("bad format {%s}", fmt);
717 /* may overwrite either white space or slash */
718 tfu->reps = atoi(savep);
719 tfu->flags = F_SETREP;
720 /* bb_dump_skip trailing white space */
721 p = skip_whitespace(++p);
724 /* bb_dump_skip slash and trailing white space */
726 p = skip_whitespace(++p);
731 // TODO: use bb_strtou
733 do p++; while (isdigit(*p));
735 bb_error_msg_and_die("bad format {%s}", fmt);
737 tfu->bcnt = atoi(savep);
738 /* bb_dump_skip trailing white space */
739 p = skip_whitespace(++p);
744 bb_error_msg_and_die("bad format {%s}", fmt);
746 for (savep = ++p; *p != '"';) {
748 bb_error_msg_and_die("bad format {%s}", fmt);
751 tfu->fmt = xmalloc(p - savep + 1);
752 strncpy(tfu->fmt, savep, p - savep);
753 tfu->fmt[p - savep] = '\0';
754 /* escape(tfu->fmt); */
758 /* alphabetic escape sequences have to be done in place */
759 for (p2 = p1;; ++p1, ++p2) {
765 const char *cs = conv_str + 4;
783 * Copyright (c) 1989 The Regents of the University of California.
784 * All rights reserved.
786 * Redistribution and use in source and binary forms, with or without
787 * modification, are permitted provided that the following conditions
789 * 1. Redistributions of source code must retain the above copyright
790 * notice, this list of conditions and the following disclaimer.
791 * 2. Redistributions in binary form must reproduce the above copyright
792 * notice, this list of conditions and the following disclaimer in the
793 * documentation and/or other materials provided with the distribution.
794 * 3. Neither the name of the University nor the names of its contributors
795 * may be used to endorse or promote products derived from this software
796 * without specific prior written permission.
798 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
799 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
800 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
801 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
802 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
803 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
804 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
805 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
806 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
807 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF