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 FAST_FUNC 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 p = strchr(size_conv_str + 12, *fmt);
66 } else if (*fmt == '_') {
68 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
73 bcnt += size_conv_str[p - (size_conv_str + 12)];
76 cur_size += bcnt * fu->reps;
81 static void rewrite(FS * fs)
83 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
84 PR *pr, **nextpr = NULL;
88 const char *byte_count_str;
91 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
93 * break each format unit into print units; each
94 * conversion character gets its own.
96 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
98 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
99 pr = xzalloc(sizeof(PR));
102 /* ignore nextpr -- its unused inside the loop and is
103 * uninitialized 1st time through.
106 /* bb_dump_skip preceding text and up to the next % sign */
107 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
109 /* only text in the string */
117 * get precision for %s -- if have a byte count, don't
122 /* bb_dump_skip to conversion character */
123 for (++p1; strchr(index_str, *p1); ++p1);
125 /* bb_dump_skip any special chars, field width */
126 while (strchr(index_str + 1, *++p1));
127 if (*p1 == '.' && isdigit(*++p1)) {
130 while (isdigit(*++p1));
135 p2 = p1 + 1; /* set end pointer */
138 * figure out the byte count for each conversion;
139 * rewrite the format as necessary, set up blank-
140 * pbb_dump_adding for end of data.
146 byte_count_str = "\001";
150 if (fu->bcnt == *byte_count_str) {
153 } while (*++byte_count_str);
155 /* Unlike the original, output the remainder of the format string. */
156 if (!*byte_count_str) {
157 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
159 pr->bcnt = *byte_count_str;
160 } else if (*p1 == 'l') {
166 e = strchr(lcc, *p1);
168 goto DO_BAD_CONV_CHAR;
174 byte_count_str = "\004\002\001";
178 } else if (strchr(lcc, *p1)) {
180 } else if (strchr("eEfgG", *p1)) {
182 byte_count_str = "\010\004";
184 } else if (*p1 == 's') {
186 if (sokay == USEBCNT) {
188 } else if (sokay == USEPREC) {
190 } else { /* NOTOKAY */
191 bb_error_msg_and_die("%%s requires a precision or a byte count");
193 } else if (*p1 == '_') {
198 fu->flags |= F_IGNORE;
201 pr->flags = F_ADDRESS;
203 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
204 goto DO_BAD_CONV_CHAR;
210 /* *p1 = 'c'; set in conv_c */
211 goto DO_BYTE_COUNT_1;
215 goto DO_BYTE_COUNT_1;
218 /* *p1 = 'c'; set in conv_u */
219 goto DO_BYTE_COUNT_1;
221 goto DO_BAD_CONV_CHAR;
225 bb_error_msg_and_die("bad conversion character %%%s", p1);
229 * copy to PR format string, set conversion character
230 * pointer, update original.
234 pr->fmt = xstrdup(fmtp);
236 pr->cchar = pr->fmt + (p1 - fmtp);
238 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
239 * Skip subsequent text and up to the next % sign and tack the
240 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
241 * we lose the " is a HEX number" part of fmt.
243 for (p3 = p2; *p3 && *p3 != '%'; p3++);
248 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
256 /* only one conversion character if byte count */
257 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
258 bb_error_msg_and_die("byte count with multiple conversion characters");
262 * if format unit byte count not specified, figure it out
263 * so can adjust rep count later.
266 for (pr = fu->nextpr; pr; pr = pr->nextpr)
267 fu->bcnt += pr->bcnt;
270 * if the format string interprets any data at all, and it's
271 * not the same as the bb_dump_blocksize, and its last format unit
272 * interprets any data at all, and has no iteration count,
273 * repeat it as necessary.
275 * if, rep count is greater than 1, no trailing whitespace
276 * gets output from the last iteration of the format unit.
278 for (fu = fs->nextfu;; fu = fu->nextfu) {
279 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
280 !(fu->flags & F_SETREP) && fu->bcnt)
281 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
283 for (pr = fu->nextpr;; pr = pr->nextpr)
286 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
287 p2 = isspace(*p1) ? p1 : NULL;
296 static void do_skip(const char *fname, int statok)
301 if (fstat(STDIN_FILENO, &sbuf)) {
302 bb_simple_perror_msg_and_die(fname);
304 if ((!(S_ISCHR(sbuf.st_mode) ||
305 S_ISBLK(sbuf.st_mode) ||
306 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
307 /* If bb_dump_size valid and bb_dump_skip >= size */
308 bb_dump_skip -= sbuf.st_size;
309 address += sbuf.st_size;
313 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
314 bb_simple_perror_msg_and_die(fname);
316 savaddress = address += bb_dump_skip;
320 static int next(char **argv)
322 static smallint done;
332 if (!(freopen(*_argv, "r", stdin))) {
333 bb_simple_perror_msg(*_argv);
346 do_skip(statok ? *_argv : "stdin", statok);
355 static unsigned char *get(void)
357 static smallint ateof = 1;
358 static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
365 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
366 curp = xmalloc(bb_dump_blocksize);
367 savp = xmalloc(bb_dump_blocksize);
372 address = savaddress += bb_dump_blocksize;
374 for (need = bb_dump_blocksize, nread = 0;;) {
376 * if read the right number of bytes, or at EOF for one file,
377 * and no other files are available, zero-pad the rest of the
378 * block and set the end flag.
380 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
381 if (need == bb_dump_blocksize) {
384 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
385 if (bb_dump_vflag != DUP) {
390 memset((char *) curp + nread, 0, need);
391 eaddress = address + nread;
394 n = fread((char *) curp + nread, sizeof(unsigned char),
395 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
398 bb_simple_perror_msg(_argv[-1]);
404 if (bb_dump_length != -1) {
409 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
410 || memcmp(curp, savp, bb_dump_blocksize)) {
411 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
412 bb_dump_vflag = WAIT;
416 if (bb_dump_vflag == WAIT) {
420 address = savaddress += bb_dump_blocksize;
421 need = bb_dump_blocksize;
429 static void bpad(PR * pr)
434 * remove all conversion flags; '-' is the only one valid
435 * with %s, and it's not useful here.
439 for (p1 = pr->fmt; *p1 != '%'; ++p1);
440 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
441 if (pr->nospace) pr->nospace--;
442 while ((*p2++ = *p1++) != 0);
445 static const char conv_str[] ALIGN1 =
457 static void conv_c(PR * pr, unsigned char * p)
459 const char *str = conv_str;
472 (void) printf(pr->fmt, *p);
474 sprintf(buf, "%03o", (int) *p);
478 printf(pr->fmt, str);
482 static void conv_u(PR * pr, unsigned char * p)
484 static const char list[] ALIGN1 =
485 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
486 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
487 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
488 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
490 /* od used nl, not lf */
493 printf(pr->fmt, list + (4 * (int)*p));
494 } else if (*p == 0x7f) {
496 printf(pr->fmt, "del");
497 } else if (isprint(*p)) {
502 printf(pr->fmt, (int) *p);
506 static void display(void)
508 /* extern FU *endfu; */
516 unsigned char savech = 0, *savebp;
518 while ((bp = get()) != NULL) {
519 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
520 fs = fs->nextfs, bp = savebp, address = saveaddress) {
521 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
522 if (fu->flags & F_IGNORE) {
525 for (cnt = fu->reps; cnt; --cnt) {
526 for (pr = fu->nextpr; pr; address += pr->bcnt,
527 bp += pr->bcnt, pr = pr->nextpr) {
528 if (eaddress && address >= eaddress &&
529 !(pr->flags & (F_TEXT | F_BPAD))) {
532 if (cnt == 1 && pr->nospace) {
533 savech = *pr->nospace;
539 printf(pr->fmt, (unsigned int) address);
548 printf(pr->fmt, *bp);
556 memmove((char *) &fval, (char *) bp,
558 printf(pr->fmt, fval);
561 memmove((char *) &dval, (char *) bp,
563 printf(pr->fmt, dval);
574 printf(pr->fmt, (int) *bp);
577 memmove((char *) &sval, (char *) bp,
579 printf(pr->fmt, (int) sval);
582 memmove((char *) &ival, (char *) bp,
584 printf(pr->fmt, ival);
590 printf(pr->fmt, isprint(*bp) ? *bp : '.');
593 printf(pr->fmt, (char *) bp);
607 printf(pr->fmt, (unsigned int) * bp);
610 memmove((char *) &sval, (char *) bp,
612 printf(pr->fmt, (unsigned int) sval);
615 memmove((char *) &ival, (char *) bp,
617 printf(pr->fmt, ival);
623 if (cnt == 1 && pr->nospace) {
624 *pr->nospace = savech;
633 * if eaddress not set, error or file bb_dump_size was multiple of
634 * bb_dump_blocksize, and no partial block ever found.
642 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
645 (void) printf(pr->fmt, (unsigned int) eaddress);
648 (void) printf(pr->fmt);
655 int FAST_FUNC bb_dump_dump(char **argv)
659 /* figure out the data block bb_dump_size */
660 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
661 tfs->bcnt = bb_dump_size(tfs);
662 if (bb_dump_blocksize < tfs->bcnt) {
663 bb_dump_blocksize = tfs->bcnt;
666 /* rewrite the rules, do syntax checking */
667 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
677 void FAST_FUNC bb_dump_add(const char *fmt)
687 /* start new linked list of format units */
688 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
689 if (!bb_dump_fshead) {
690 bb_dump_fshead = tfs;
694 nextfs = &tfs->nextfs;
695 nextfu = &tfs->nextfu;
697 /* take the format string and break it up into format units */
699 /* bb_dump_skip leading white space */
700 p = skip_whitespace(p);
705 /* allocate a new format unit and link it in */
707 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
708 tfu = xzalloc(sizeof(FU));
710 nextfu = &tfu->nextfu;
713 /* if leading digit, repetition count */
715 for (savep = p; isdigit(*p); ++p);
716 if (!isspace(*p) && *p != '/') {
717 bb_error_msg_and_die("bad format {%s}", fmt);
719 /* may overwrite either white space or slash */
720 tfu->reps = atoi(savep);
721 tfu->flags = F_SETREP;
722 /* bb_dump_skip trailing white space */
723 p = skip_whitespace(++p);
726 /* bb_dump_skip slash and trailing white space */
728 p = skip_whitespace(++p);
733 // TODO: use bb_strtou
735 do p++; while (isdigit(*p));
737 bb_error_msg_and_die("bad format {%s}", fmt);
739 tfu->bcnt = atoi(savep);
740 /* bb_dump_skip trailing white space */
741 p = skip_whitespace(++p);
746 bb_error_msg_and_die("bad format {%s}", fmt);
748 for (savep = ++p; *p != '"';) {
750 bb_error_msg_and_die("bad format {%s}", fmt);
753 tfu->fmt = xmalloc(p - savep + 1);
754 strncpy(tfu->fmt, savep, p - savep);
755 tfu->fmt[p - savep] = '\0';
756 /* escape(tfu->fmt); */
760 /* alphabetic escape sequences have to be done in place */
761 for (p2 = p1;; ++p1, ++p2) {
767 const char *cs = conv_str + 4;
785 * Copyright (c) 1989 The Regents of the University of California.
786 * All rights reserved.
788 * Redistribution and use in source and binary forms, with or without
789 * modification, are permitted provided that the following conditions
791 * 1. Redistributions of source code must retain the above copyright
792 * notice, this list of conditions and the following disclaimer.
793 * 2. Redistributions in binary form must reproduce the above copyright
794 * notice, this list of conditions and the following disclaimer in the
795 * documentation and/or other materials provided with the distribution.
796 * 3. Neither the name of the University nor the names of its contributors
797 * may be used to endorse or promote products derived from this software
798 * without specific prior written permission.
800 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
801 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
802 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
803 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
804 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
805 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
806 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
807 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
808 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
809 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF