2 * Support code for the hexdump and od applets,
3 * based on code from util-linux v 2.11l
6 * The Regents of the University of California. All rights reserved.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * Original copyright notice is retained at the end of this file.
16 #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)
42 register int bcnt, cur_size;
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 register PR *pr, **nextpr = NULL;
88 register char *p1, *p2, *p3;
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 = (PR *) xcalloc(1,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 if (!(pr->fmt = realloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1)))
250 bb_perror_msg_and_die("hexdump");
258 /* only one conversion character if byte count */
259 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
260 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
264 * if format unit byte count not specified, figure it out
265 * so can adjust rep count later.
268 for (pr = fu->nextpr; pr; pr = pr->nextpr)
269 fu->bcnt += pr->bcnt;
272 * if the format string interprets any data at all, and it's
273 * not the same as the bb_dump_blocksize, and its last format unit
274 * interprets any data at all, and has no iteration count,
275 * repeat it as necessary.
277 * if, rep count is greater than 1, no trailing whitespace
278 * gets output from the last iteration of the format unit.
280 for (fu = fs->nextfu;; fu = fu->nextfu) {
281 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
282 !(fu->flags & F_SETREP) && fu->bcnt)
283 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
285 for (pr = fu->nextpr;; pr = pr->nextpr)
288 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
289 p2 = isspace(*p1) ? p1 : NULL;
298 static void do_skip(char *fname, int statok)
303 if (fstat(STDIN_FILENO, &sbuf)) {
304 bb_perror_msg_and_die("%s", fname);
306 if ((!(S_ISCHR(sbuf.st_mode) ||
307 S_ISBLK(sbuf.st_mode) ||
308 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
309 /* If bb_dump_size valid and bb_dump_skip >= size */
310 bb_dump_skip -= sbuf.st_size;
311 address += sbuf.st_size;
315 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
316 bb_perror_msg_and_die("%s", fname);
318 savaddress = address += bb_dump_skip;
322 static int next(char **argv)
333 if (!(freopen(*_argv, "r", stdin))) {
334 bb_perror_msg("%s", *_argv);
346 do_skip(statok ? *_argv : "stdin", statok);
355 static u_char *get(void)
357 static int ateof = 1;
358 static u_char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
364 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
365 curp = (u_char *) xmalloc(bb_dump_blocksize);
366 savp = (u_char *) xmalloc(bb_dump_blocksize);
371 address = savaddress += bb_dump_blocksize;
373 for (need = bb_dump_blocksize, nread = 0;;) {
375 * if read the right number of bytes, or at EOF for one file,
376 * and no other files are available, zero-pad the rest of the
377 * block and set the end flag.
379 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
380 if (need == bb_dump_blocksize) {
381 return ((u_char *) NULL);
383 if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
384 if (bb_dump_vflag != DUP) {
387 return ((u_char *) NULL);
389 memset((char *) curp + nread, 0, need);
390 eaddress = address + nread;
393 n = fread((char *) curp + nread, sizeof(u_char),
394 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
397 bb_perror_msg("%s", _argv[-1]);
403 if (bb_dump_length != -1) {
407 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
408 || bcmp(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)
429 register char *p1, *p2;
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 while ((*p2++ = *p1++) != 0);
442 static const char conv_str[] =
454 static void conv_c(PR * pr, u_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, u_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 u_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 bcopy((char *) bp, (char *) &fval,
555 printf(pr->fmt, fval);
558 bcopy((char *) bp, (char *) &dval,
560 printf(pr->fmt, dval);
571 printf(pr->fmt, (int) *bp);
574 bcopy((char *) bp, (char *) &sval,
576 printf(pr->fmt, (int) sval);
579 bcopy((char *) bp, (char *) &ival,
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 bcopy((char *) bp, (char *) &sval,
609 printf(pr->fmt, (unsigned int) sval);
612 bcopy((char *) bp, (char *) &ival,
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)
676 register const char *p;
684 /* start new linked list of format units */
686 tfs = (FS *) xcalloc(1,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 = bb_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 = (FU *) xcalloc(1,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 = bb_skip_whitespace(++p);
724 /* bb_dump_skip slash and trailing white space */
726 p = bb_skip_whitespace(++p);
731 for (savep = p; isdigit(*p); ++p);
733 bb_error_msg_and_die("bad format {%s}", fmt);
735 tfu->bcnt = atoi(savep);
736 /* bb_dump_skip trailing white space */
737 p = bb_skip_whitespace(++p);
742 bb_error_msg_and_die("bad format {%s}", fmt);
744 for (savep = ++p; *p != '"';) {
746 bb_error_msg_and_die("bad format {%s}", fmt);
749 tfu->fmt = xmalloc(p - savep + 1);
750 strncpy(tfu->fmt, savep, p - savep);
751 tfu->fmt[p - savep] = '\0';
752 /* escape(tfu->fmt); */
756 /* alphabetic escape sequences have to be done in place */
757 for (p2 = p1;; ++p1, ++p2) {
763 const char *cs = conv_str + 4;
781 * Copyright (c) 1989 The Regents of the University of California.
782 * All rights reserved.
784 * Redistribution and use in source and binary forms, with or without
785 * modification, are permitted provided that the following conditions
787 * 1. Redistributions of source code must retain the above copyright
788 * notice, this list of conditions and the following disclaimer.
789 * 2. Redistributions in binary form must reproduce the above copyright
790 * notice, this list of conditions and the following disclaimer in the
791 * documentation and/or other materials provided with the distribution.
792 * 3. Neither the name of the University nor the names of its contributors
793 * may be used to endorse or promote products derived from this software
794 * without specific prior written permission.
796 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
797 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
798 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
799 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
800 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
801 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
802 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
803 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
804 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
805 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF