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() */
19 enum _vflag bb_dump_vflag = FIRST;
20 FS *bb_dump_fshead; /* head of format strings */
23 static off_t savaddress; /* saved address/offset in stream */
24 static off_t eaddress; /* end address */
25 static off_t address; /* address/offset in stream */
26 off_t bb_dump_skip; /* bytes to skip */
27 static int exitval; /* final exit value */
28 int bb_dump_blocksize; /* data block size */
29 int bb_dump_length = -1; /* max bytes to read */
31 static const char index_str[] = ".#-+ 0123456789";
33 static const char size_conv_str[] =
34 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
36 static const char lcc[] = "diouxX";
38 int bb_dump_size(FS * fs)
46 /* figure out the data block bb_dump_size needed for each format unit */
47 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
49 cur_size += fu->bcnt * fu->reps;
52 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
56 * bb_dump_skip any special chars -- save precision in
57 * case it's a %s format.
59 while (strchr(index_str + 1, *++fmt));
60 if (*fmt == '.' && isdigit(*++fmt)) {
62 while (isdigit(*++fmt));
64 if (!(p = strchr(size_conv_str + 12, *fmt))) {
67 } else if (*fmt == '_') {
69 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
74 bcnt += size_conv_str[p - (size_conv_str + 12)];
77 cur_size += bcnt * fu->reps;
82 static void rewrite(FS * fs)
84 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
85 PR *pr, **nextpr = NULL;
89 const char *byte_count_str;
92 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
94 * break each format unit into print units; each
95 * conversion character gets its own.
97 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
99 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
100 pr = xzalloc(sizeof(PR));
103 /* ignore nextpr -- its unused inside the loop and is
104 * uninitialized 1st time thru.
107 /* bb_dump_skip preceding text and up to the next % sign */
108 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
110 /* only text in the string */
118 * get precision for %s -- if have a byte count, don't
123 /* bb_dump_skip to conversion character */
124 for (++p1; strchr(index_str, *p1); ++p1);
126 /* bb_dump_skip any special chars, field width */
127 while (strchr(index_str + 1, *++p1));
128 if (*p1 == '.' && isdigit(*++p1)) {
131 while (isdigit(*++p1));
136 p2 = p1 + 1; /* set end pointer */
139 * figure out the byte count for each conversion;
140 * rewrite the format as necessary, set up blank-
141 * pbb_dump_adding for end of data.
147 byte_count_str = "\001";
151 if (fu->bcnt == *byte_count_str) {
154 } while (*++byte_count_str);
156 /* Unlike the original, output the remainder of the format string. */
157 if (!*byte_count_str) {
158 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
160 pr->bcnt = *byte_count_str;
161 } else if (*p1 == 'l') {
167 if (!(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.\n", p1);
229 * copy to PR format string, set conversion character
230 * pointer, update original.
234 pr->fmt = bb_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.\n");
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(char *fname, int statok)
301 if (fstat(STDIN_FILENO, &sbuf)) {
302 bb_perror_msg_and_die("%s", 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_perror_msg_and_die("%s", fname);
316 savaddress = address += bb_dump_skip;
320 static int next(char **argv)
331 if (!(freopen(*_argv, "r", stdin))) {
332 bb_perror_msg("%s", *_argv);
344 do_skip(statok ? *_argv : "stdin", statok);
353 static unsigned char *get(void)
355 static int ateof = 1;
356 static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
362 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
363 curp = (unsigned char *) xmalloc(bb_dump_blocksize);
364 savp = (unsigned char *) xmalloc(bb_dump_blocksize);
369 address = savaddress += bb_dump_blocksize;
371 for (need = bb_dump_blocksize, nread = 0;;) {
373 * if read the right number of bytes, or at EOF for one file,
374 * and no other files are available, zero-pad the rest of the
375 * block and set the end flag.
377 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
378 if (need == bb_dump_blocksize) {
379 return ((unsigned char *) NULL);
381 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
382 if (bb_dump_vflag != DUP) {
385 return ((unsigned char *) NULL);
387 memset((char *) curp + nread, 0, need);
388 eaddress = address + nread;
391 n = fread((char *) curp + nread, sizeof(unsigned char),
392 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
395 bb_perror_msg("%s", _argv[-1]);
401 if (bb_dump_length != -1) {
405 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
406 || memcmp(curp, savp, bb_dump_blocksize)) {
407 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
408 bb_dump_vflag = WAIT;
412 if (bb_dump_vflag == WAIT) {
416 address = savaddress += bb_dump_blocksize;
417 need = bb_dump_blocksize;
425 static void bpad(PR * pr)
430 * remove all conversion flags; '-' is the only one valid
431 * with %s, and it's not useful here.
435 for (p1 = pr->fmt; *p1 != '%'; ++p1);
436 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
437 if (pr->nospace) pr->nospace--;
438 while ((*p2++ = *p1++) != 0);
441 static const char conv_str[] =
453 static void conv_c(PR * pr, unsigned char * p)
455 const char *str = conv_str;
468 (void) printf(pr->fmt, *p);
470 sprintf(buf, "%03o", (int) *p);
474 printf(pr->fmt, str);
478 static void conv_u(PR * pr, unsigned char * p)
480 static const char list[] =
481 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
482 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
483 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
484 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
486 /* od used nl, not lf */
489 printf(pr->fmt, list + (4 * (int)*p));
490 } else if (*p == 0x7f) {
492 printf(pr->fmt, "del");
493 } else if (isprint(*p)) {
498 printf(pr->fmt, (int) *p);
502 static void display(void)
504 /* extern FU *endfu; */
512 unsigned char savech = 0, *savebp;
514 while ((bp = get()) != NULL) {
515 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
516 fs = fs->nextfs, bp = savebp, address = saveaddress) {
517 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
518 if (fu->flags & F_IGNORE) {
521 for (cnt = fu->reps; cnt; --cnt) {
522 for (pr = fu->nextpr; pr; address += pr->bcnt,
523 bp += pr->bcnt, pr = pr->nextpr) {
524 if (eaddress && address >= eaddress &&
525 !(pr->flags & (F_TEXT | F_BPAD))) {
528 if (cnt == 1 && pr->nospace) {
529 savech = *pr->nospace;
535 printf(pr->fmt, (unsigned int) address);
544 printf(pr->fmt, *bp);
552 memmove((char *) &fval, (char *) bp,
554 printf(pr->fmt, fval);
557 memmove((char *) &dval, (char *) bp,
559 printf(pr->fmt, dval);
570 printf(pr->fmt, (int) *bp);
573 memmove((char *) &sval, (char *) bp,
575 printf(pr->fmt, (int) sval);
578 memmove((char *) &ival, (char *) bp,
580 printf(pr->fmt, ival);
586 printf(pr->fmt, isprint(*bp) ? *bp : '.');
589 printf(pr->fmt, (char *) bp);
603 printf(pr->fmt, (unsigned int) * bp);
606 memmove((char *) &sval, (char *) bp,
608 printf(pr->fmt, (unsigned int) sval);
611 memmove((char *) &ival, (char *) bp,
613 printf(pr->fmt, ival);
619 if (cnt == 1 && pr->nospace) {
620 *pr->nospace = savech;
629 * if eaddress not set, error or file bb_dump_size was multiple of
630 * bb_dump_blocksize, and no partial block ever found.
638 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
641 (void) printf(pr->fmt, (unsigned int) eaddress);
644 (void) printf(pr->fmt);
651 int bb_dump_dump(char **argv)
655 /* figure out the data block bb_dump_size */
656 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
657 tfs->bcnt = bb_dump_size(tfs);
658 if (bb_dump_blocksize < tfs->bcnt) {
659 bb_dump_blocksize = tfs->bcnt;
662 /* rewrite the rules, do syntax checking */
663 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
673 void bb_dump_add(const char *fmt)
683 /* start new linked list of format units */
684 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
685 if (!bb_dump_fshead) {
686 bb_dump_fshead = tfs;
690 nextfs = &tfs->nextfs;
691 nextfu = &tfs->nextfu;
693 /* take the format string and break it up into format units */
695 /* bb_dump_skip leading white space */
696 p = skip_whitespace(p);
701 /* allocate a new format unit and link it in */
703 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
704 tfu = xzalloc(sizeof(FU));
706 nextfu = &tfu->nextfu;
709 /* if leading digit, repetition count */
711 for (savep = p; isdigit(*p); ++p);
712 if (!isspace(*p) && *p != '/') {
713 bb_error_msg_and_die("bad format {%s}", fmt);
715 /* may overwrite either white space or slash */
716 tfu->reps = atoi(savep);
717 tfu->flags = F_SETREP;
718 /* bb_dump_skip trailing white space */
719 p = skip_whitespace(++p);
722 /* bb_dump_skip slash and trailing white space */
724 p = skip_whitespace(++p);
729 for (savep = p; isdigit(*p); ++p);
731 bb_error_msg_and_die("bad format {%s}", fmt);
733 tfu->bcnt = atoi(savep);
734 /* bb_dump_skip trailing white space */
735 p = skip_whitespace(++p);
740 bb_error_msg_and_die("bad format {%s}", fmt);
742 for (savep = ++p; *p != '"';) {
744 bb_error_msg_and_die("bad format {%s}", fmt);
747 tfu->fmt = xmalloc(p - savep + 1);
748 strncpy(tfu->fmt, savep, p - savep);
749 tfu->fmt[p - savep] = '\0';
750 /* escape(tfu->fmt); */
754 /* alphabetic escape sequences have to be done in place */
755 for (p2 = p1;; ++p1, ++p2) {
761 const char *cs = conv_str + 4;
779 * Copyright (c) 1989 The Regents of the University of California.
780 * All rights reserved.
782 * Redistribution and use in source and binary forms, with or without
783 * modification, are permitted provided that the following conditions
785 * 1. Redistributions of source code must retain the above copyright
786 * notice, this list of conditions and the following disclaimer.
787 * 2. Redistributions in binary form must reproduce the above copyright
788 * notice, this list of conditions and the following disclaimer in the
789 * documentation and/or other materials provided with the distribution.
790 * 3. Neither the name of the University nor the names of its contributors
791 * may be used to endorse or promote products derived from this software
792 * without specific prior written permission.
794 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
795 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
796 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
797 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
798 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
799 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
800 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
801 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
802 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
803 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF