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;
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 pr = (PR *) xmalloc(sizeof(PR));
117 /* bb_dump_skip preceding text and up to the next % sign */
118 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
120 /* only text in the string */
128 * get precision for %s -- if have a byte count, don't
133 /* bb_dump_skip to conversion character */
134 for (++p1; strchr(index_str, *p1); ++p1);
136 /* bb_dump_skip any special chars, field width */
137 while (strchr(index_str + 1, *++p1));
138 if (*p1 == '.' && isdigit(*++p1)) {
141 while (isdigit(*++p1));
146 p2 = p1 + 1; /* set end pointer */
149 * figure out the byte count for each conversion;
150 * rewrite the format as necessary, set up blank-
151 * pbb_dump_adding for end of data.
157 byte_count_str = "\001";
161 if (fu->bcnt == *byte_count_str) {
164 } while (*++byte_count_str);
166 /* Unlike the original, output the remainder of the format string. */
167 if (!*byte_count_str) {
168 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
170 pr->bcnt = *byte_count_str;
171 } else if (*p1 == 'l') {
177 if (!(e = strchr(lcc, *p1))) {
178 goto DO_BAD_CONV_CHAR;
184 byte_count_str = "\004\002\001";
188 } else if (strchr(lcc, *p1)) {
190 } else if (strchr("eEfgG", *p1)) {
192 byte_count_str = "\010\004";
194 } else if (*p1 == 's') {
196 if (sokay == USEBCNT) {
198 } else if (sokay == USEPREC) {
200 } else { /* NOTOKAY */
201 bb_error_msg_and_die("%%s requires a precision or a byte count.");
203 } else if (*p1 == '_') {
208 fu->flags |= F_IGNORE;
211 pr->flags = F_ADDRESS;
213 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
214 goto DO_BAD_CONV_CHAR;
220 /* *p1 = 'c'; set in conv_c */
221 goto DO_BYTE_COUNT_1;
225 goto DO_BYTE_COUNT_1;
228 /* *p1 = 'c'; set in conv_u */
229 goto DO_BYTE_COUNT_1;
231 goto DO_BAD_CONV_CHAR;
235 bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
239 * copy to PR format string, set conversion character
240 * pointer, update original.
244 pr->fmt = bb_xstrdup(fmtp);
246 pr->cchar = pr->fmt + (p1 - fmtp);
249 /* only one conversion character if byte count */
250 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
251 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
255 * if format unit byte count not specified, figure it out
256 * so can adjust rep count later.
259 for (pr = fu->nextpr; pr; pr = pr->nextpr)
260 fu->bcnt += pr->bcnt;
263 * if the format string interprets any data at all, and it's
264 * not the same as the bb_dump_blocksize, and its last format unit
265 * interprets any data at all, and has no iteration count,
266 * repeat it as necessary.
268 * if, rep count is greater than 1, no trailing whitespace
269 * gets output from the last iteration of the format unit.
271 for (fu = fs->nextfu;; fu = fu->nextfu) {
272 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
273 !(fu->flags & F_SETREP) && fu->bcnt)
274 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
276 for (pr = fu->nextpr;; pr = pr->nextpr)
279 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
280 p2 = isspace(*p1) ? p1 : NULL;
289 static void do_skip(char *fname, int statok)
294 if (fstat(fileno(stdin), &sbuf)) {
295 bb_perror_msg_and_die("%s", fname);
297 if ((!(S_ISCHR(sbuf.st_mode) ||
298 S_ISBLK(sbuf.st_mode) ||
299 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
300 /* If bb_dump_size valid and bb_dump_skip >= size */
301 bb_dump_skip -= sbuf.st_size;
302 address += sbuf.st_size;
306 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
307 bb_perror_msg_and_die("%s", fname);
309 savaddress = address += bb_dump_skip;
313 static int next(char **argv)
324 if (!(freopen(*_argv, "r", stdin))) {
325 bb_perror_msg("%s", *_argv);
337 do_skip(statok ? *_argv : "stdin", statok);
346 static u_char *get(void)
348 static int ateof = 1;
349 static u_char *curp, *savp;
355 curp = (u_char *) xmalloc(bb_dump_blocksize);
356 savp = (u_char *) xmalloc(bb_dump_blocksize);
361 address = savaddress += bb_dump_blocksize;
363 for (need = bb_dump_blocksize, nread = 0;;) {
365 * if read the right number of bytes, or at EOF for one file,
366 * and no other files are available, zero-pad the rest of the
367 * block and set the end flag.
369 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
370 if (need == bb_dump_blocksize) {
371 return ((u_char *) NULL);
373 if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
374 if (bb_dump_vflag != DUP) {
377 return ((u_char *) NULL);
379 bzero((char *) curp + nread, need);
380 eaddress = address + nread;
383 n = fread((char *) curp + nread, sizeof(u_char),
384 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
387 bb_perror_msg("%s", _argv[-1]);
393 if (bb_dump_length != -1) {
397 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
398 || bcmp(curp, savp, bb_dump_blocksize)) {
399 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
400 bb_dump_vflag = WAIT;
404 if (bb_dump_vflag == WAIT) {
408 address = savaddress += bb_dump_blocksize;
409 need = bb_dump_blocksize;
417 static void bpad(PR * pr)
419 register char *p1, *p2;
422 * remove all conversion flags; '-' is the only one valid
423 * with %s, and it's not useful here.
427 for (p1 = pr->fmt; *p1 != '%'; ++p1);
428 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
429 while ((*p2++ = *p1++) != 0);
432 static const char conv_str[] =
444 static void conv_c(PR * pr, u_char * p)
446 const char *str = conv_str;
459 (void) printf(pr->fmt, *p);
461 sprintf(buf, "%03o", (int) *p);
465 printf(pr->fmt, str);
469 static void conv_u(PR * pr, u_char * p)
471 static const char list[] =
472 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
473 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
474 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
475 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
477 /* od used nl, not lf */
480 printf(pr->fmt, list[4 * (int)(*p)]);
481 } else if (*p == 0x7f) {
483 printf(pr->fmt, "del");
484 } else if (isprint(*p)) {
489 printf(pr->fmt, (int) *p);
493 static void display(void)
495 /* extern FU *endfu; */
503 u_char savech = 0, *savebp;
505 while ((bp = get()) != NULL) {
506 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
507 fs = fs->nextfs, bp = savebp, address = saveaddress) {
508 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
509 if (fu->flags & F_IGNORE) {
512 for (cnt = fu->reps; cnt; --cnt) {
513 for (pr = fu->nextpr; pr; address += pr->bcnt,
514 bp += pr->bcnt, pr = pr->nextpr) {
515 if (eaddress && address >= eaddress &&
516 !(pr->flags & (F_TEXT | F_BPAD))) {
519 if (cnt == 1 && pr->nospace) {
520 savech = *pr->nospace;
526 printf(pr->fmt, address);
535 printf(pr->fmt, *bp);
543 bcopy((char *) bp, (char *) &fval,
545 printf(pr->fmt, fval);
548 bcopy((char *) bp, (char *) &dval,
550 printf(pr->fmt, dval);
561 printf(pr->fmt, (int) *bp);
564 bcopy((char *) bp, (char *) &sval,
566 printf(pr->fmt, (int) sval);
569 bcopy((char *) bp, (char *) &ival,
571 printf(pr->fmt, ival);
577 printf(pr->fmt, isprint(*bp) ? *bp : '.');
580 printf(pr->fmt, (char *) bp);
594 printf(pr->fmt, (u_int) * bp);
597 bcopy((char *) bp, (char *) &sval,
599 printf(pr->fmt, (u_int) sval);
602 bcopy((char *) bp, (char *) &ival,
604 printf(pr->fmt, ival);
610 if (cnt == 1 && pr->nospace) {
611 *pr->nospace = savech;
620 * if eaddress not set, error or file bb_dump_size was multiple of
621 * bb_dump_blocksize, and no partial block ever found.
629 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
632 (void) printf(pr->fmt, eaddress);
635 (void) printf(pr->fmt);
642 int bb_dump_dump(char **argv)
646 /* figure out the data block bb_dump_size */
647 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
648 tfs->bcnt = bb_dump_size(tfs);
649 if (bb_dump_blocksize < tfs->bcnt) {
650 bb_dump_blocksize = tfs->bcnt;
653 /* rewrite the rules, do syntax checking */
654 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
664 void bb_dump_add(const char *fmt)
666 register const char *p;
674 /* start new linked list of format units */
676 tfs = (FS *) xmalloc(sizeof(FS));
677 if (!bb_dump_fshead) {
678 bb_dump_fshead = tfs;
682 nextfs = &tfs->nextfs;
683 nextfu = &tfs->nextfu;
685 /* take the format string and break it up into format units */
687 /* bb_dump_skip leading white space */
688 p = bb_skip_whitespace(p);
693 /* allocate a new format unit and link it in */
695 tfu = (FU *) xmalloc(sizeof(FU));
697 nextfu = &tfu->nextfu;
700 /* if leading digit, repetition count */
702 for (savep = p; isdigit(*p); ++p);
703 if (!isspace(*p) && *p != '/') {
704 bb_error_msg_and_die("bad format {%s}", fmt);
706 /* may overwrite either white space or slash */
707 tfu->reps = atoi(savep);
708 tfu->flags = F_SETREP;
709 /* bb_dump_skip trailing white space */
710 p = bb_skip_whitespace(++p);
713 /* bb_dump_skip slash and trailing white space */
715 p = bb_skip_whitespace(++p);
720 for (savep = p; isdigit(*p); ++p);
722 bb_error_msg_and_die("bad format {%s}", fmt);
724 tfu->bcnt = atoi(savep);
725 /* bb_dump_skip trailing white space */
726 p = bb_skip_whitespace(++p);
731 bb_error_msg_and_die("bad format {%s}", fmt);
733 for (savep = ++p; *p != '"';) {
735 bb_error_msg_and_die("bad format {%s}", fmt);
738 tfu->fmt = xmalloc(p - savep + 1);
739 strncpy(tfu->fmt, savep, p - savep);
740 tfu->fmt[p - savep] = '\0';
741 /* escape(tfu->fmt); */
745 /* alphabetic escape sequences have to be done in place */
746 for (p2 = p1;; ++p1, ++p2) {
752 const char *cs = conv_str + 4;
770 * Copyright (c) 1989 The Regents of the University of California.
771 * All rights reserved.
773 * Redistribution and use in source and binary forms, with or without
774 * modification, are permitted provided that the following conditions
776 * 1. Redistributions of source code must retain the above copyright
777 * notice, this list of conditions and the following disclaimer.
778 * 2. Redistributions in binary form must reproduce the above copyright
779 * notice, this list of conditions and the following disclaimer in the
780 * documentation and/or other materials provided with the distribution.
781 * 3. Neither the name of the University nor the names of its contributors
782 * may be used to endorse or promote products derived from this software
783 * without specific prior written permission.
785 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
786 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
787 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
788 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
789 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
790 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
791 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
792 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
793 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
794 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF