1 /* vi: set sw=4 ts=4: */
3 * Support code for the hexdump and od applets,
4 * based on code from util-linux v 2.11l
7 * The Regents of the University of California. All rights reserved.
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 * Original copyright notice is retained at the end of this file.
17 static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
19 static const char size_conv_str[] ALIGN1 =
20 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
22 static const char int_convs[] ALIGN1 = "diouxX";
25 typedef struct priv_dumper_t {
30 off_t savaddress; /* saved address/offset in stream */
31 off_t eaddress; /* end address */
32 off_t address; /* address/offset in stream */
34 smallint exitval; /* final exit value */
38 smallint get__ateof; // = 1;
39 unsigned char *get__curp;
40 unsigned char *get__savp;
43 dumper_t* FAST_FUNC alloc_dumper(void)
45 priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
46 dumper->pub.dump_length = -1;
47 dumper->pub.dump_vflag = FIRST;
48 dumper->get__ateof = 1;
53 static NOINLINE int bb_dump_size(FS *fs)
61 /* figure out the data block bb_dump_size needed for each format unit */
62 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
64 cur_size += fu->bcnt * fu->reps;
67 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
71 * skip any special chars -- save precision in
72 * case it's a %s format.
74 while (strchr(dot_flags_width_chars + 1, *++fmt))
76 if (*fmt == '.' && isdigit(*++fmt)) {
78 while (isdigit(*++fmt))
81 p = strchr(size_conv_str + 12, *fmt);
88 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
96 cur_size += bcnt * fu->reps;
101 static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
105 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
111 * break each format unit into print units; each
112 * conversion character gets its own.
114 for (fmtp = fu->fmt; *fmtp; ) {
117 const char *byte_count_str;
119 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
120 pr = xzalloc(sizeof(*pr));
124 /* skip preceding text and up to the next % sign */
125 p1 = strchr(fmtp, '%');
126 if (!p1) { /* only text in the string */
133 * get precision for %s -- if have a byte count, don't
138 /* skip to conversion character */
139 while (strchr(dot_flags_width_chars, *++p1))
142 /* skip any special chars, field width */
143 while (strchr(dot_flags_width_chars + 1, *++p1))
145 if (*p1 == '.' && isdigit(*++p1)) {
147 while (isdigit(*++p1))
152 p2 = p1 + 1; /* set end pointer */
155 * figure out the byte count for each conversion;
156 * rewrite the format as necessary, set up blank-
157 * padding for end of data.
162 byte_count_str = "\001";
166 if (fu->bcnt == *byte_count_str)
168 if (*++byte_count_str == 0)
169 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
172 /* Unlike the original, output the remainder of the format string. */
173 pr->bcnt = *byte_count_str;
175 if (*p1 == 'l') { /* %ld etc */
181 e = strchr(int_convs, *p1); /* "diouxX"? */
183 goto DO_BAD_CONV_CHAR;
185 if (e > int_convs + 1) /* not d or i? */
187 byte_count_str = "\004\002\001";
190 if (strchr(int_convs, *p1)) { /* %d etc */
193 if (strchr("eEfgG", *p1)) { /* floating point */
195 byte_count_str = "\010\004";
203 bb_error_msg_and_die("%%s needs precision or byte count");
204 pr->bcnt = atoi(prec);
208 p2++; /* move past a in "%_a" */
210 case 'A': /* %_A[dox]: print address and the end */
212 fu->flags |= F_IGNORE;
214 case 'a': /* %_a[dox]: current address */
215 pr->flags = F_ADDRESS;
216 p2++; /* move past x in "%_ax" */
217 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
218 goto DO_BAD_CONV_CHAR;
222 case 'c': /* %_c: chars, \ooo, \n \r \t etc */
224 /* *p1 = 'c'; set in conv_c */
225 goto DO_BYTE_COUNT_1;
226 case 'p': /* %_p: chars, dots for nonprintable */
229 goto DO_BYTE_COUNT_1;
230 case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */
232 /* *p1 = 'c'; set in conv_u */
233 goto DO_BYTE_COUNT_1;
235 goto DO_BAD_CONV_CHAR;
239 bb_error_msg_and_die("bad conversion character %%%s", p1);
243 * copy to PR format string, set conversion character
244 * pointer, update original.
246 len = (p1 - fmtp) + 1;
247 pr->fmt = xstrndup(fmtp, len);
248 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
249 * Skip subsequent text and up to the next % sign and tack the
250 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
251 * we lose the " is a HEX number" part of fmt.
253 for (p3 = p2; *p3 && *p3 != '%'; p3++)
255 if ((p3 - p2) != 0) {
257 pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
265 pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
268 /* only one conversion character if byte count */
269 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
270 bb_error_msg_and_die("byte count with multiple conversion characters");
274 * if format unit byte count not specified, figure it out
275 * so can adjust rep count later.
278 for (pr = fu->nextpr; pr; pr = pr->nextpr)
279 fu->bcnt += pr->bcnt;
282 * if the format string interprets any data at all, and it's
283 * not the same as the blocksize, and its last format unit
284 * interprets any data at all, and has no iteration count,
285 * repeat it as necessary.
287 * if rep count is greater than 1, no trailing whitespace
288 * gets output from the last iteration of the format unit.
290 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
292 && fs->bcnt < dumper->blocksize
293 && !(fu->flags & F_SETREP)
296 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
298 if (fu->reps > 1 && fu->nextpr) {
302 for (pr = fu->nextpr;; pr = pr->nextpr)
306 for (p1 = pr->fmt; *p1; ++p1)
307 p2 = isspace(*p1) ? p1 : NULL;
314 static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
319 xfstat(STDIN_FILENO, &sbuf, fname);
320 if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
321 && dumper->pub.dump_skip >= sbuf.st_size
323 /* If bb_dump_size valid and pub.dump_skip >= size */
324 dumper->pub.dump_skip -= sbuf.st_size;
325 dumper->address += sbuf.st_size;
329 if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
330 bb_simple_perror_msg_and_die(fname);
332 dumper->address += dumper->pub.dump_skip;
333 dumper->savaddress = dumper->address;
334 dumper->pub.dump_skip = 0;
337 static NOINLINE int next(priv_dumper_t *dumper)
343 dumper->next__done = statok = 1;
344 if (!(freopen(*dumper->argv, "r", stdin))) {
345 bb_simple_perror_msg(*dumper->argv);
351 if (dumper->next__done)
352 return 0; /* no next file */
353 dumper->next__done = 1;
354 //why stat of stdin is specially prohibited?
357 if (dumper->pub.dump_skip)
358 do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
361 if (!dumper->pub.dump_skip)
367 static unsigned char *get(priv_dumper_t *dumper)
371 int blocksize = dumper->blocksize;
373 if (!dumper->get__curp) {
374 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
375 dumper->get__curp = xmalloc(blocksize);
376 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
378 unsigned char *tmp = dumper->get__curp;
379 dumper->get__curp = dumper->get__savp;
380 dumper->get__savp = tmp;
381 dumper->savaddress += blocksize;
382 dumper->address = dumper->savaddress;
388 * if read the right number of bytes, or at EOF for one file,
389 * and no other files are available, zero-pad the rest of the
390 * block and set the end flag.
392 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
393 if (need == blocksize) {
396 if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
397 if (dumper->pub.dump_vflag != DUP) {
402 memset(dumper->get__curp + nread, 0, need);
403 dumper->eaddress = dumper->address + nread;
404 return dumper->get__curp;
406 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
407 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
410 bb_simple_perror_msg(dumper->argv[-1]);
412 dumper->get__ateof = 1;
415 dumper->get__ateof = 0;
416 if (dumper->pub.dump_length != -1) {
417 dumper->pub.dump_length -= n;
421 if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
422 || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
424 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
425 dumper->pub.dump_vflag = WAIT;
427 return dumper->get__curp;
429 if (dumper->pub.dump_vflag == WAIT) {
432 dumper->pub.dump_vflag = DUP;
433 dumper->savaddress += blocksize;
434 dumper->address = dumper->savaddress;
443 static void bpad(PR *pr)
448 * remove all conversion flags; '-' is the only one valid
449 * with %s, and it's not useful here.
453 for (p1 = pr->fmt; *p1 != '%'; ++p1)
455 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
458 while ((*p2++ = *p1++) != 0)
462 static const char conv_str[] ALIGN1 =
464 "\007""\\""a""\0" /* \a */
474 static void conv_c(PR *pr, unsigned char *p)
476 const char *str = conv_str;
482 goto strpr; /* map e.g. '\n' to "\\n" */
487 if (isprint_asciionly(*p)) {
491 sprintf(buf, "%03o", (int) *p);
495 printf(pr->fmt, str);
499 static void conv_u(PR *pr, unsigned char *p)
501 static const char list[] ALIGN1 =
502 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
503 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
504 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
505 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
507 /* od used nl, not lf */
510 printf(pr->fmt, list + (4 * (int)*p));
511 } else if (*p == 0x7f) {
513 printf(pr->fmt, "del");
514 } else if (*p < 0x7f) { /* isprint() */
519 printf(pr->fmt, (int) *p);
523 static void display(priv_dumper_t* dumper)
529 unsigned char *bp, *savebp;
531 unsigned char savech = '\0';
533 while ((bp = get(dumper)) != NULL) {
534 fs = dumper->pub.fshead;
536 saveaddress = dumper->address;
537 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
538 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
539 if (fu->flags & F_IGNORE) {
542 for (cnt = fu->reps; cnt; --cnt) {
543 for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
544 bp += pr->bcnt, pr = pr->nextpr) {
545 if (dumper->eaddress && dumper->address >= dumper->eaddress
546 && !(pr->flags & (F_TEXT | F_BPAD))
550 if (cnt == 1 && pr->nospace) {
551 savech = *pr->nospace;
557 printf(pr->fmt, (unsigned) dumper->address);
566 printf(pr->fmt, *bp);
574 memcpy(&fval, bp, sizeof(fval));
575 printf(pr->fmt, fval);
578 memcpy(&dval, bp, sizeof(dval));
579 printf(pr->fmt, dval);
590 printf(pr->fmt, (int) *bp);
593 memcpy(&sval, bp, sizeof(sval));
594 printf(pr->fmt, (int) sval);
597 memcpy(&ival, bp, sizeof(ival));
598 printf(pr->fmt, ival);
604 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
607 printf(pr->fmt, (char *) bp);
621 printf(pr->fmt, (unsigned) *bp);
624 memcpy(&sval, bp, sizeof(sval));
625 printf(pr->fmt, (unsigned) sval);
628 memcpy(&ival, bp, sizeof(ival));
629 printf(pr->fmt, ival);
635 if (cnt == 1 && pr->nospace) {
636 *pr->nospace = savech;
645 * if eaddress not set, error or file size was multiple
646 * of blocksize, and no partial block ever found.
648 if (!dumper->eaddress) {
649 if (!dumper->address) {
652 dumper->eaddress = dumper->address;
654 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
657 printf(pr->fmt, (unsigned) dumper->eaddress);
667 #define dumper ((priv_dumper_t*)pub_dumper)
668 int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
673 /* figure out the data block bb_dump_size */
675 tfs = dumper->pub.fshead;
677 tfs->bcnt = bb_dump_size(tfs);
678 if (blocksize < tfs->bcnt) {
679 blocksize = tfs->bcnt;
683 dumper->blocksize = blocksize;
685 /* rewrite the rules, do syntax checking */
686 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
687 rewrite(dumper, tfs);
693 return dumper->exitval;
696 void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
703 /* start new linked list of format units */
704 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
705 if (!dumper->pub.fshead) {
706 dumper->pub.fshead = tfs;
708 FS *fslast = dumper->pub.fshead;
709 while (fslast->nextfs)
710 fslast = fslast->nextfs;
711 fslast->nextfs = tfs;
713 nextfupp = &tfs->nextfu;
715 /* take the format string and break it up into format units */
718 p = skip_whitespace(p);
723 /* allocate a new format unit and link it in */
725 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
726 tfu = xzalloc(sizeof(FU));
728 nextfupp = &tfu->nextfu;
731 /* if leading digit, repetition count */
733 for (savep = p; isdigit(*p); ++p)
735 if (!isspace(*p) && *p != '/') {
736 bb_error_msg_and_die("bad format {%s}", fmt);
738 /* may overwrite either white space or slash */
739 tfu->reps = atoi(savep);
740 tfu->flags = F_SETREP;
741 /* skip trailing white space */
742 p = skip_whitespace(++p);
745 /* skip slash and trailing white space */
747 p = skip_whitespace(p + 1);
752 // TODO: use bb_strtou
754 while (isdigit(*++p))
757 bb_error_msg_and_die("bad format {%s}", fmt);
759 // Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
760 // Other than this, formats can be pretty much jammed together:
761 // "%07_ax:"8/2 "%04x|""\n"
762 // but this space is required. The check *can* be removed, but
763 // keeping it to stay compat with util-linux hexdump.
764 tfu->bcnt = atoi(savep);
765 /* skip trailing white space */
766 p = skip_whitespace(p + 1);
771 bb_error_msg_and_die("bad format {%s}", fmt);
773 for (savep = ++p; *p != '"';) {
775 bb_error_msg_and_die("bad format {%s}", fmt);
778 tfu->fmt = xstrndup(savep, p - savep);
780 /* alphabetic escape sequences have to be done in place */
781 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
782 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
783 /* trailing backslash, if any, is preserved */
788 for (p2 = p1;; ++p1, ++p2) {
799 /* "...\" trailing backslash. Eaten. */
802 cs = conv_str + 4; /* skip NUL element */
804 /* map e.g. "\n" -> '\n' */
811 /* unknown mappings remove bkslash: "\z" -> 'z' */
821 * Copyright (c) 1989 The Regents of the University of California.
822 * All rights reserved.
824 * Redistribution and use in source and binary forms, with or without
825 * modification, are permitted provided that the following conditions
827 * 1. Redistributions of source code must retain the above copyright
828 * notice, this list of conditions and the following disclaimer.
829 * 2. Redistributions in binary form must reproduce the above copyright
830 * notice, this list of conditions and the following disclaimer in the
831 * documentation and/or other materials provided with the distribution.
832 * 3. Neither the name of the University nor the names of its contributors
833 * may be used to endorse or promote products derived from this software
834 * without specific prior written permission.
836 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
837 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
838 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
839 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
840 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
841 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
842 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
843 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
844 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
845 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF