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.
28 #include <ctype.h> /* for isdigit() */
32 enum _vflag bb_dump_vflag = FIRST;
33 FS *bb_dump_fshead; /* head of format strings */
36 static off_t savaddress; /* saved address/offset in stream */
37 static off_t eaddress; /* end address */
38 static off_t address; /* address/offset in stream */
39 off_t bb_dump_skip; /* bytes to skip */
40 static int exitval; /* final exit value */
41 int bb_dump_blocksize; /* data block size */
42 int bb_dump_length = -1; /* max bytes to read */
44 static const char index_str[] = ".#-+ 0123456789";
46 static const char size_conv_str[] =
47 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
49 static const char lcc[] = "diouxX";
51 int bb_dump_size(FS * fs)
54 register int bcnt, cur_size;
59 /* figure out the data block bb_dump_size needed for each format unit */
60 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
62 cur_size += fu->bcnt * fu->reps;
65 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
69 * bb_dump_skip any special chars -- save precision in
70 * case it's a %s format.
72 while (strchr(index_str + 1, *++fmt));
73 if (*fmt == '.' && isdigit(*++fmt)) {
75 while (isdigit(*++fmt));
77 if (!(p = strchr(size_conv_str + 12, *fmt))) {
80 } else if (*fmt == '_') {
82 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
87 bcnt += size_conv_str[p - (size_conv_str + 12)];
90 cur_size += bcnt * fu->reps;
95 static void rewrite(FS * fs)
97 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
98 register PR *pr, **nextpr = NULL;
100 register char *p1, *p2, *p3;
102 const char *byte_count_str;
105 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
107 * break each format unit into print units; each
108 * conversion character gets its own.
110 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
112 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
113 pr = (PR *) xcalloc(1,sizeof(PR));
116 /* ignore nextpr -- its unused inside the loop and is
117 * uninitialized 1st time thru.
120 /* bb_dump_skip preceding text and up to the next % sign */
121 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
123 /* only text in the string */
131 * get precision for %s -- if have a byte count, don't
136 /* bb_dump_skip to conversion character */
137 for (++p1; strchr(index_str, *p1); ++p1);
139 /* bb_dump_skip any special chars, field width */
140 while (strchr(index_str + 1, *++p1));
141 if (*p1 == '.' && isdigit(*++p1)) {
144 while (isdigit(*++p1));
149 p2 = p1 + 1; /* set end pointer */
152 * figure out the byte count for each conversion;
153 * rewrite the format as necessary, set up blank-
154 * pbb_dump_adding for end of data.
160 byte_count_str = "\001";
164 if (fu->bcnt == *byte_count_str) {
167 } while (*++byte_count_str);
169 /* Unlike the original, output the remainder of the format string. */
170 if (!*byte_count_str) {
171 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
173 pr->bcnt = *byte_count_str;
174 } else if (*p1 == 'l') {
180 if (!(e = strchr(lcc, *p1))) {
181 goto DO_BAD_CONV_CHAR;
187 byte_count_str = "\004\002\001";
191 } else if (strchr(lcc, *p1)) {
193 } else if (strchr("eEfgG", *p1)) {
195 byte_count_str = "\010\004";
197 } else if (*p1 == 's') {
199 if (sokay == USEBCNT) {
201 } else if (sokay == USEPREC) {
203 } else { /* NOTOKAY */
204 bb_error_msg_and_die("%%s requires a precision or a byte count.");
206 } else if (*p1 == '_') {
211 fu->flags |= F_IGNORE;
214 pr->flags = F_ADDRESS;
216 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
217 goto DO_BAD_CONV_CHAR;
223 /* *p1 = 'c'; set in conv_c */
224 goto DO_BYTE_COUNT_1;
228 goto DO_BYTE_COUNT_1;
231 /* *p1 = 'c'; set in conv_u */
232 goto DO_BYTE_COUNT_1;
234 goto DO_BAD_CONV_CHAR;
238 bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
242 * copy to PR format string, set conversion character
243 * pointer, update original.
247 pr->fmt = bb_xstrdup(fmtp);
249 pr->cchar = pr->fmt + (p1 - fmtp);
251 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
252 * Skip subsequent text and up to the next % sign and tack the
253 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
254 * we lose the " is a HEX number" part of fmt.
256 for (p3 = p2; *p3 && *p3 != '%'; p3++);
261 if (!(pr->fmt = realloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1)))
262 bb_perror_msg_and_die("hexdump");
270 /* only one conversion character if byte count */
271 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
272 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
276 * if format unit byte count not specified, figure it out
277 * so can adjust rep count later.
280 for (pr = fu->nextpr; pr; pr = pr->nextpr)
281 fu->bcnt += pr->bcnt;
284 * if the format string interprets any data at all, and it's
285 * not the same as the bb_dump_blocksize, and its last format unit
286 * interprets any data at all, and has no iteration count,
287 * repeat it as necessary.
289 * if, rep count is greater than 1, no trailing whitespace
290 * gets output from the last iteration of the format unit.
292 for (fu = fs->nextfu;; fu = fu->nextfu) {
293 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
294 !(fu->flags & F_SETREP) && fu->bcnt)
295 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
297 for (pr = fu->nextpr;; pr = pr->nextpr)
300 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
301 p2 = isspace(*p1) ? p1 : NULL;
310 static void do_skip(char *fname, int statok)
315 if (fstat(STDIN_FILENO, &sbuf)) {
316 bb_perror_msg_and_die("%s", fname);
318 if ((!(S_ISCHR(sbuf.st_mode) ||
319 S_ISBLK(sbuf.st_mode) ||
320 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
321 /* If bb_dump_size valid and bb_dump_skip >= size */
322 bb_dump_skip -= sbuf.st_size;
323 address += sbuf.st_size;
327 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
328 bb_perror_msg_and_die("%s", fname);
330 savaddress = address += bb_dump_skip;
334 static int next(char **argv)
345 if (!(freopen(*_argv, "r", stdin))) {
346 bb_perror_msg("%s", *_argv);
358 do_skip(statok ? *_argv : "stdin", statok);
367 static u_char *get(void)
369 static int ateof = 1;
370 static u_char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
376 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
377 curp = (u_char *) xmalloc(bb_dump_blocksize);
378 savp = (u_char *) xmalloc(bb_dump_blocksize);
383 address = savaddress += bb_dump_blocksize;
385 for (need = bb_dump_blocksize, nread = 0;;) {
387 * if read the right number of bytes, or at EOF for one file,
388 * and no other files are available, zero-pad the rest of the
389 * block and set the end flag.
391 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
392 if (need == bb_dump_blocksize) {
393 return ((u_char *) NULL);
395 if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
396 if (bb_dump_vflag != DUP) {
399 return ((u_char *) NULL);
401 bzero((char *) curp + nread, need);
402 eaddress = address + nread;
405 n = fread((char *) curp + nread, sizeof(u_char),
406 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
409 bb_perror_msg("%s", _argv[-1]);
415 if (bb_dump_length != -1) {
419 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
420 || bcmp(curp, savp, bb_dump_blocksize)) {
421 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
422 bb_dump_vflag = WAIT;
426 if (bb_dump_vflag == WAIT) {
430 address = savaddress += bb_dump_blocksize;
431 need = bb_dump_blocksize;
439 static void bpad(PR * pr)
441 register char *p1, *p2;
444 * remove all conversion flags; '-' is the only one valid
445 * with %s, and it's not useful here.
449 for (p1 = pr->fmt; *p1 != '%'; ++p1);
450 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
451 while ((*p2++ = *p1++) != 0);
454 static const char conv_str[] =
466 static void conv_c(PR * pr, u_char * p)
468 const char *str = conv_str;
481 (void) printf(pr->fmt, *p);
483 sprintf(buf, "%03o", (int) *p);
487 printf(pr->fmt, str);
491 static void conv_u(PR * pr, u_char * p)
493 static const char list[] =
494 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
495 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
496 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
497 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
499 /* od used nl, not lf */
502 printf(pr->fmt, list[4 * (int)(*p)]);
503 } else if (*p == 0x7f) {
505 printf(pr->fmt, "del");
506 } else if (isprint(*p)) {
511 printf(pr->fmt, (int) *p);
515 static void display(void)
517 /* extern FU *endfu; */
525 u_char savech = 0, *savebp;
527 while ((bp = get()) != NULL) {
528 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
529 fs = fs->nextfs, bp = savebp, address = saveaddress) {
530 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
531 if (fu->flags & F_IGNORE) {
534 for (cnt = fu->reps; cnt; --cnt) {
535 for (pr = fu->nextpr; pr; address += pr->bcnt,
536 bp += pr->bcnt, pr = pr->nextpr) {
537 if (eaddress && address >= eaddress &&
538 !(pr->flags & (F_TEXT | F_BPAD))) {
541 if (cnt == 1 && pr->nospace) {
542 savech = *pr->nospace;
548 printf(pr->fmt, (unsigned int) address);
557 printf(pr->fmt, *bp);
565 bcopy((char *) bp, (char *) &fval,
567 printf(pr->fmt, fval);
570 bcopy((char *) bp, (char *) &dval,
572 printf(pr->fmt, dval);
583 printf(pr->fmt, (int) *bp);
586 bcopy((char *) bp, (char *) &sval,
588 printf(pr->fmt, (int) sval);
591 bcopy((char *) bp, (char *) &ival,
593 printf(pr->fmt, ival);
599 printf(pr->fmt, isprint(*bp) ? *bp : '.');
602 printf(pr->fmt, (char *) bp);
616 printf(pr->fmt, (unsigned int) * bp);
619 bcopy((char *) bp, (char *) &sval,
621 printf(pr->fmt, (unsigned int) sval);
624 bcopy((char *) bp, (char *) &ival,
626 printf(pr->fmt, ival);
632 if (cnt == 1 && pr->nospace) {
633 *pr->nospace = savech;
642 * if eaddress not set, error or file bb_dump_size was multiple of
643 * bb_dump_blocksize, and no partial block ever found.
651 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
654 (void) printf(pr->fmt, (unsigned int) eaddress);
657 (void) printf(pr->fmt);
664 int bb_dump_dump(char **argv)
668 /* figure out the data block bb_dump_size */
669 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
670 tfs->bcnt = bb_dump_size(tfs);
671 if (bb_dump_blocksize < tfs->bcnt) {
672 bb_dump_blocksize = tfs->bcnt;
675 /* rewrite the rules, do syntax checking */
676 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
686 void bb_dump_add(const char *fmt)
688 register const char *p;
696 /* start new linked list of format units */
698 tfs = (FS *) xcalloc(1,sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
699 if (!bb_dump_fshead) {
700 bb_dump_fshead = tfs;
704 nextfs = &tfs->nextfs;
705 nextfu = &tfs->nextfu;
707 /* take the format string and break it up into format units */
709 /* bb_dump_skip leading white space */
710 p = bb_skip_whitespace(p);
715 /* allocate a new format unit and link it in */
717 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
718 tfu = (FU *) xcalloc(1,sizeof(FU));
720 nextfu = &tfu->nextfu;
723 /* if leading digit, repetition count */
725 for (savep = p; isdigit(*p); ++p);
726 if (!isspace(*p) && *p != '/') {
727 bb_error_msg_and_die("bad format {%s}", fmt);
729 /* may overwrite either white space or slash */
730 tfu->reps = atoi(savep);
731 tfu->flags = F_SETREP;
732 /* bb_dump_skip trailing white space */
733 p = bb_skip_whitespace(++p);
736 /* bb_dump_skip slash and trailing white space */
738 p = bb_skip_whitespace(++p);
743 for (savep = p; isdigit(*p); ++p);
745 bb_error_msg_and_die("bad format {%s}", fmt);
747 tfu->bcnt = atoi(savep);
748 /* bb_dump_skip trailing white space */
749 p = bb_skip_whitespace(++p);
754 bb_error_msg_and_die("bad format {%s}", fmt);
756 for (savep = ++p; *p != '"';) {
758 bb_error_msg_and_die("bad format {%s}", fmt);
761 tfu->fmt = xmalloc(p - savep + 1);
762 strncpy(tfu->fmt, savep, p - savep);
763 tfu->fmt[p - savep] = '\0';
764 /* escape(tfu->fmt); */
768 /* alphabetic escape sequences have to be done in place */
769 for (p2 = p1;; ++p1, ++p2) {
775 const char *cs = conv_str + 4;
793 * Copyright (c) 1989 The Regents of the University of California.
794 * All rights reserved.
796 * Redistribution and use in source and binary forms, with or without
797 * modification, are permitted provided that the following conditions
799 * 1. Redistributions of source code must retain the above copyright
800 * notice, this list of conditions and the following disclaimer.
801 * 2. Redistributions in binary form must reproduce the above copyright
802 * notice, this list of conditions and the following disclaimer in the
803 * documentation and/or other materials provided with the distribution.
804 * 3. Neither the name of the University nor the names of its contributors
805 * may be used to endorse or promote products derived from this software
806 * without specific prior written permission.
808 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
809 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
810 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
811 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
812 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
813 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
814 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
815 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
816 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
817 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF