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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Original copyright notice is retained at the end of this file.
27 #include <ctype.h> /* for isdigit() */
31 enum _vflag bb_dump_vflag = FIRST;
32 FS *bb_dump_fshead; /* head of format strings */
35 static off_t savaddress; /* saved address/offset in stream */
36 static off_t eaddress; /* end address */
37 static off_t address; /* address/offset in stream */
38 off_t bb_dump_skip; /* bytes to skip */
39 static int exitval; /* final exit value */
40 int bb_dump_blocksize; /* data block size */
41 int bb_dump_length = -1; /* max bytes to read */
43 static const char index_str[] = ".#-+ 0123456789";
45 static const char size_conv_str[] =
46 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
48 static const char lcc[] = "diouxX";
50 int bb_dump_size(FS * fs)
53 register int bcnt, cur_size;
58 /* figure out the data block bb_dump_size needed for each format unit */
59 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
61 cur_size += fu->bcnt * fu->reps;
64 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
68 * bb_dump_skip any special chars -- save precision in
69 * case it's a %s format.
71 while (strchr(index_str + 1, *++fmt));
72 if (*fmt == '.' && isdigit(*++fmt)) {
74 while (isdigit(*++fmt));
76 if (!(p = strchr(size_conv_str + 12, *fmt))) {
79 } else if (*fmt == '_') {
81 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
86 bcnt += size_conv_str[p - (size_conv_str + 12)];
89 cur_size += bcnt * fu->reps;
94 static void rewrite(FS * fs)
96 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
97 register PR *pr, **nextpr = NULL;
99 register char *p1, *p2, *p3;
101 const char *byte_count_str;
104 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
106 * break each format unit into print units; each
107 * conversion character gets its own.
109 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
111 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
112 pr = (PR *) xcalloc(1,sizeof(PR));
115 /* ignore nextpr -- its unused inside the loop and is
116 * uninitialized 1st time thru.
119 /* bb_dump_skip preceding text and up to the next % sign */
120 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
122 /* only text in the string */
130 * get precision for %s -- if have a byte count, don't
135 /* bb_dump_skip to conversion character */
136 for (++p1; strchr(index_str, *p1); ++p1);
138 /* bb_dump_skip any special chars, field width */
139 while (strchr(index_str + 1, *++p1));
140 if (*p1 == '.' && isdigit(*++p1)) {
143 while (isdigit(*++p1));
148 p2 = p1 + 1; /* set end pointer */
151 * figure out the byte count for each conversion;
152 * rewrite the format as necessary, set up blank-
153 * pbb_dump_adding for end of data.
159 byte_count_str = "\001";
163 if (fu->bcnt == *byte_count_str) {
166 } while (*++byte_count_str);
168 /* Unlike the original, output the remainder of the format string. */
169 if (!*byte_count_str) {
170 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
172 pr->bcnt = *byte_count_str;
173 } else if (*p1 == 'l') {
179 if (!(e = strchr(lcc, *p1))) {
180 goto DO_BAD_CONV_CHAR;
186 byte_count_str = "\004\002\001";
190 } else if (strchr(lcc, *p1)) {
192 } else if (strchr("eEfgG", *p1)) {
194 byte_count_str = "\010\004";
196 } else if (*p1 == 's') {
198 if (sokay == USEBCNT) {
200 } else if (sokay == USEPREC) {
202 } else { /* NOTOKAY */
203 bb_error_msg_and_die("%%s requires a precision or a byte count.");
205 } else if (*p1 == '_') {
210 fu->flags |= F_IGNORE;
213 pr->flags = F_ADDRESS;
215 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
216 goto DO_BAD_CONV_CHAR;
222 /* *p1 = 'c'; set in conv_c */
223 goto DO_BYTE_COUNT_1;
227 goto DO_BYTE_COUNT_1;
230 /* *p1 = 'c'; set in conv_u */
231 goto DO_BYTE_COUNT_1;
233 goto DO_BAD_CONV_CHAR;
237 bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
241 * copy to PR format string, set conversion character
242 * pointer, update original.
246 pr->fmt = bb_xstrdup(fmtp);
248 pr->cchar = pr->fmt + (p1 - fmtp);
250 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
251 * Skip subsequent text and up to the next % sign and tack the
252 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
253 * we lose the " is a HEX number" part of fmt.
255 for (p3 = p2; *p3 && *p3 != '%'; p3++);
260 if (!(pr->fmt = realloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1)))
261 perror_msg_and_die("hexdump");
269 /* only one conversion character if byte count */
270 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
271 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
275 * if format unit byte count not specified, figure it out
276 * so can adjust rep count later.
279 for (pr = fu->nextpr; pr; pr = pr->nextpr)
280 fu->bcnt += pr->bcnt;
283 * if the format string interprets any data at all, and it's
284 * not the same as the bb_dump_blocksize, and its last format unit
285 * interprets any data at all, and has no iteration count,
286 * repeat it as necessary.
288 * if, rep count is greater than 1, no trailing whitespace
289 * gets output from the last iteration of the format unit.
291 for (fu = fs->nextfu;; fu = fu->nextfu) {
292 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
293 !(fu->flags & F_SETREP) && fu->bcnt)
294 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
296 for (pr = fu->nextpr;; pr = pr->nextpr)
299 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
300 p2 = isspace(*p1) ? p1 : NULL;
309 static void do_skip(char *fname, int statok)
314 if (fstat(fileno(stdin), &sbuf)) {
315 bb_perror_msg_and_die("%s", fname);
317 if ((!(S_ISCHR(sbuf.st_mode) ||
318 S_ISBLK(sbuf.st_mode) ||
319 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
320 /* If bb_dump_size valid and bb_dump_skip >= size */
321 bb_dump_skip -= sbuf.st_size;
322 address += sbuf.st_size;
326 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
327 bb_perror_msg_and_die("%s", fname);
329 savaddress = address += bb_dump_skip;
333 static int next(char **argv)
344 if (!(freopen(*_argv, "r", stdin))) {
345 bb_perror_msg("%s", *_argv);
357 do_skip(statok ? *_argv : "stdin", statok);
366 static u_char *get(void)
368 static int ateof = 1;
369 static u_char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
375 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
376 curp = (u_char *) xmalloc(bb_dump_blocksize);
377 savp = (u_char *) xmalloc(bb_dump_blocksize);
382 address = savaddress += bb_dump_blocksize;
384 for (need = bb_dump_blocksize, nread = 0;;) {
386 * if read the right number of bytes, or at EOF for one file,
387 * and no other files are available, zero-pad the rest of the
388 * block and set the end flag.
390 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
391 if (need == bb_dump_blocksize) {
392 return ((u_char *) NULL);
394 if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
395 if (bb_dump_vflag != DUP) {
398 return ((u_char *) NULL);
400 bzero((char *) curp + nread, need);
401 eaddress = address + nread;
404 n = fread((char *) curp + nread, sizeof(u_char),
405 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
408 bb_perror_msg("%s", _argv[-1]);
414 if (bb_dump_length != -1) {
418 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
419 || bcmp(curp, savp, bb_dump_blocksize)) {
420 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
421 bb_dump_vflag = WAIT;
425 if (bb_dump_vflag == WAIT) {
429 address = savaddress += bb_dump_blocksize;
430 need = bb_dump_blocksize;
438 static void bpad(PR * pr)
440 register char *p1, *p2;
443 * remove all conversion flags; '-' is the only one valid
444 * with %s, and it's not useful here.
448 for (p1 = pr->fmt; *p1 != '%'; ++p1);
449 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
450 while ((*p2++ = *p1++) != 0);
453 static const char conv_str[] =
465 static void conv_c(PR * pr, u_char * p)
467 const char *str = conv_str;
480 (void) printf(pr->fmt, *p);
482 sprintf(buf, "%03o", (int) *p);
486 printf(pr->fmt, str);
490 static void conv_u(PR * pr, u_char * p)
492 static const char list[] =
493 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
494 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
495 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
496 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
498 /* od used nl, not lf */
501 printf(pr->fmt, list[4 * (int)(*p)]);
502 } else if (*p == 0x7f) {
504 printf(pr->fmt, "del");
505 } else if (isprint(*p)) {
510 printf(pr->fmt, (int) *p);
514 static void display(void)
516 /* extern FU *endfu; */
524 u_char savech = 0, *savebp;
526 while ((bp = get()) != NULL) {
527 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
528 fs = fs->nextfs, bp = savebp, address = saveaddress) {
529 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
530 if (fu->flags & F_IGNORE) {
533 for (cnt = fu->reps; cnt; --cnt) {
534 for (pr = fu->nextpr; pr; address += pr->bcnt,
535 bp += pr->bcnt, pr = pr->nextpr) {
536 if (eaddress && address >= eaddress &&
537 !(pr->flags & (F_TEXT | F_BPAD))) {
540 if (cnt == 1 && pr->nospace) {
541 savech = *pr->nospace;
547 printf(pr->fmt, address);
556 printf(pr->fmt, *bp);
564 bcopy((char *) bp, (char *) &fval,
566 printf(pr->fmt, fval);
569 bcopy((char *) bp, (char *) &dval,
571 printf(pr->fmt, dval);
582 printf(pr->fmt, (int) *bp);
585 bcopy((char *) bp, (char *) &sval,
587 printf(pr->fmt, (int) sval);
590 bcopy((char *) bp, (char *) &ival,
592 printf(pr->fmt, ival);
598 printf(pr->fmt, isprint(*bp) ? *bp : '.');
601 printf(pr->fmt, (char *) bp);
615 printf(pr->fmt, (u_int) * bp);
618 bcopy((char *) bp, (char *) &sval,
620 printf(pr->fmt, (u_int) sval);
623 bcopy((char *) bp, (char *) &ival,
625 printf(pr->fmt, ival);
631 if (cnt == 1 && pr->nospace) {
632 *pr->nospace = savech;
641 * if eaddress not set, error or file bb_dump_size was multiple of
642 * bb_dump_blocksize, and no partial block ever found.
650 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
653 (void) printf(pr->fmt, eaddress);
656 (void) printf(pr->fmt);
663 int bb_dump_dump(char **argv)
667 /* figure out the data block bb_dump_size */
668 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
669 tfs->bcnt = bb_dump_size(tfs);
670 if (bb_dump_blocksize < tfs->bcnt) {
671 bb_dump_blocksize = tfs->bcnt;
674 /* rewrite the rules, do syntax checking */
675 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
685 void bb_dump_add(const char *fmt)
687 register const char *p;
695 /* start new linked list of format units */
697 tfs = (FS *) xcalloc(1,sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
698 if (!bb_dump_fshead) {
699 bb_dump_fshead = tfs;
703 nextfs = &tfs->nextfs;
704 nextfu = &tfs->nextfu;
706 /* take the format string and break it up into format units */
708 /* bb_dump_skip leading white space */
709 p = bb_skip_whitespace(p);
714 /* allocate a new format unit and link it in */
716 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
717 tfu = (FU *) xcalloc(1,sizeof(FU));
719 nextfu = &tfu->nextfu;
722 /* if leading digit, repetition count */
724 for (savep = p; isdigit(*p); ++p);
725 if (!isspace(*p) && *p != '/') {
726 bb_error_msg_and_die("bad format {%s}", fmt);
728 /* may overwrite either white space or slash */
729 tfu->reps = atoi(savep);
730 tfu->flags = F_SETREP;
731 /* bb_dump_skip trailing white space */
732 p = bb_skip_whitespace(++p);
735 /* bb_dump_skip slash and trailing white space */
737 p = bb_skip_whitespace(++p);
742 for (savep = p; isdigit(*p); ++p);
744 bb_error_msg_and_die("bad format {%s}", fmt);
746 tfu->bcnt = atoi(savep);
747 /* bb_dump_skip trailing white space */
748 p = bb_skip_whitespace(++p);
753 bb_error_msg_and_die("bad format {%s}", fmt);
755 for (savep = ++p; *p != '"';) {
757 bb_error_msg_and_die("bad format {%s}", fmt);
760 tfu->fmt = xmalloc(p - savep + 1);
761 strncpy(tfu->fmt, savep, p - savep);
762 tfu->fmt[p - savep] = '\0';
763 /* escape(tfu->fmt); */
767 /* alphabetic escape sequences have to be done in place */
768 for (p2 = p1;; ++p1, ++p2) {
774 const char *cs = conv_str + 4;
792 * Copyright (c) 1989 The Regents of the University of California.
793 * All rights reserved.
795 * Redistribution and use in source and binary forms, with or without
796 * modification, are permitted provided that the following conditions
798 * 1. Redistributions of source code must retain the above copyright
799 * notice, this list of conditions and the following disclaimer.
800 * 2. Redistributions in binary form must reproduce the above copyright
801 * notice, this list of conditions and the following disclaimer in the
802 * documentation and/or other materials provided with the distribution.
803 * 3. Neither the name of the University nor the names of its contributors
804 * may be used to endorse or promote products derived from this software
805 * without specific prior written permission.
807 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
808 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
809 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
810 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
811 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
812 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
813 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
814 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
815 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
816 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF