busybox/libbb/dump.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Support code for the hexdump and od applets,
   4 * based on code from util-linux v 2.11l
   5 *
   6 * Copyright (c) 1989
   7 * The Regents of the University of California.  All rights reserved.
   8 *
   9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10 *
  11 * Original copyright notice is retained at the end of this file.
  12 */
  13#include "libbb.h"
  14#include "dump.h"
  15
  16static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
  17
  18static const char size_conv_str[] ALIGN1 =
  19"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
  20
  21static const char int_convs[] ALIGN1 = "diouxX";
  22
  23
  24typedef struct priv_dumper_t {
  25        dumper_t pub;
  26
  27        char **argv;
  28        FU *endfu;
  29        off_t savaddress;        /* saved address/offset in stream */
  30        off_t eaddress;          /* end address */
  31        off_t address;           /* address/offset in stream */
  32        int blocksize;
  33        smallint exitval;        /* final exit value */
  34
  35        /* former statics */
  36        smallint next__done;
  37        smallint get__ateof; // = 1;
  38        unsigned char *get__curp;
  39        unsigned char *get__savp;
  40} priv_dumper_t;
  41
  42dumper_t* FAST_FUNC alloc_dumper(void)
  43{
  44        priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
  45        dumper->pub.dump_length = -1;
  46        dumper->pub.dump_vflag = FIRST;
  47        dumper->get__ateof = 1;
  48        return &dumper->pub;
  49}
  50
  51
  52static NOINLINE int bb_dump_size(FS *fs)
  53{
  54        FU *fu;
  55        int bcnt, cur_size;
  56        char *fmt;
  57        const char *p;
  58        int prec;
  59
  60        /* figure out the data block size needed for each format unit */
  61        for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
  62                if (fu->bcnt) {
  63                        cur_size += fu->bcnt * fu->reps;
  64                        continue;
  65                }
  66                for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
  67                        if (*fmt != '%')
  68                                continue;
  69                        /*
  70                         * skip any special chars -- save precision in
  71                         * case it's a %s format.
  72                         */
  73                        while (strchr(dot_flags_width_chars + 1, *++fmt))
  74                                continue;
  75                        if (*fmt == '.' && isdigit(*++fmt)) {
  76                                prec = atoi(fmt);
  77                                while (isdigit(*++fmt))
  78                                        continue;
  79                        }
  80                        p = strchr(size_conv_str + 12, *fmt);
  81                        if (!p) {
  82                                if (*fmt == 's') {
  83                                        bcnt += prec;
  84                                }
  85                                if (*fmt == '_') {
  86                                        ++fmt;
  87                                        if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
  88                                                bcnt += 1;
  89                                        }
  90                                }
  91                        } else {
  92                                bcnt += p[-12];
  93                        }
  94                }
  95                cur_size += bcnt * fu->reps;
  96        }
  97        return cur_size;
  98}
  99
 100static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
 101{
 102        FU *fu;
 103
 104        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
 105                PR *pr;
 106                char *p1, *p2, *p3;
 107                char *fmtp;
 108                int nconv = 0;
 109                /*
 110                 * break each format unit into print units; each
 111                 * conversion character gets its own.
 112                 */
 113                for (fmtp = fu->fmt; *fmtp; ) {
 114                        unsigned len;
 115                        const char *prec;
 116                        const char *byte_count_str;
 117
 118                        /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
 119                        pr = xzalloc(sizeof(*pr));
 120                        if (!fu->nextpr)
 121                                fu->nextpr = pr;
 122
 123                        /* skip preceding text and up to the next % sign */
 124                        p1 = strchr(fmtp, '%');
 125                        if (!p1) { /* only text in the string */
 126                                pr->fmt = fmtp;
 127                                pr->flags = F_TEXT;
 128                                break;
 129                        }
 130
 131                        /*
 132                         * get precision for %s -- if have a byte count, don't
 133                         * need it.
 134                         */
 135                        prec = NULL;
 136                        if (fu->bcnt) {
 137                                /* skip to conversion character */
 138                                while (strchr(dot_flags_width_chars, *++p1))
 139                                        continue;
 140                        } else {
 141                                /* skip any special chars, field width */
 142                                while (strchr(dot_flags_width_chars + 1, *++p1))
 143                                        continue;
 144                                if (*p1 == '.' && isdigit(*++p1)) {
 145                                        prec = p1;
 146                                        while (isdigit(*++p1))
 147                                                continue;
 148                                }
 149                        }
 150
 151                        p2 = p1 + 1; /* set end pointer */
 152
 153                        /*
 154                         * figure out the byte count for each conversion;
 155                         * rewrite the format as necessary, set up blank-
 156                         * padding for end of data.
 157                         */
 158                        if (*p1 == 'c') {
 159                                pr->flags = F_CHAR;
 160 DO_BYTE_COUNT_1:
 161                                byte_count_str = "\001";
 162 DO_BYTE_COUNT:
 163                                if (fu->bcnt) {
 164                                        for (;;) {
 165                                                if (fu->bcnt == *byte_count_str)
 166                                                        break;
 167                                                if (*++byte_count_str == 0)
 168                                                        bb_error_msg_and_die("bad byte count for conversion character %s", p1);
 169                                        }
 170                                }
 171                                /* Unlike the original, output the remainder of the format string. */
 172                                pr->bcnt = *byte_count_str;
 173                        } else
 174                        if (*p1 == 'l') { /* %ld etc */
 175                                const char *e;
 176
 177                                ++p2;
 178                                ++p1;
 179 DO_INT_CONV:
 180                                e = strchr(int_convs, *p1); /* "diouxX"? */
 181                                if (!e)
 182                                        goto DO_BAD_CONV_CHAR;
 183                                pr->flags = F_INT;
 184                                if (e > int_convs + 1) /* not d or i? */
 185                                        pr->flags = F_UINT;
 186                                byte_count_str = "\004\002\001";
 187                                goto DO_BYTE_COUNT;
 188                        } else
 189                        if (strchr(int_convs, *p1)) { /* %d etc */
 190                                goto DO_INT_CONV;
 191                        } else
 192                        if (strchr("eEfgG", *p1)) { /* floating point */
 193                                pr->flags = F_DBL;
 194                                byte_count_str = "\010\004";
 195                                goto DO_BYTE_COUNT;
 196                        } else
 197                        if (*p1 == 's') {
 198                                pr->flags = F_STR;
 199                                pr->bcnt = fu->bcnt;
 200                                if (fu->bcnt == 0) {
 201                                        if (!prec)
 202                                                bb_error_msg_and_die("%%s needs precision or byte count");
 203                                        pr->bcnt = atoi(prec);
 204                                }
 205                        } else
 206                        if (*p1 == '_') {
 207                                p2++;  /* move past a in "%_a" */
 208                                switch (p1[1]) {
 209                                case 'A':       /* %_A[dox]: print address and the end */
 210                                        dumper->endfu = fu;
 211                                        fu->flags |= F_IGNORE;
 212                                        /* FALLTHROUGH */
 213                                case 'a':       /* %_a[dox]: current address */
 214                                        pr->flags = F_ADDRESS;
 215                                        p2++;  /* move past x in "%_ax" */
 216                                        if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
 217                                                goto DO_BAD_CONV_CHAR;
 218                                        }
 219                                        *p1 = p1[2];
 220                                        break;
 221                                case 'c':       /* %_c: chars, \ooo, \n \r \t etc */
 222                                        pr->flags = F_C;
 223                                        /* *p1 = 'c';   set in conv_c */
 224                                        goto DO_BYTE_COUNT_1;
 225                                case 'p':       /* %_p: chars, dots for nonprintable */
 226                                        pr->flags = F_P;
 227                                        *p1 = 'c';
 228                                        goto DO_BYTE_COUNT_1;
 229                                case 'u':       /* %_p: chars, 'nul', 'esc' etc for nonprintable */
 230                                        pr->flags = F_U;
 231                                        /* *p1 = 'c';   set in conv_u */
 232                                        goto DO_BYTE_COUNT_1;
 233                                default:
 234                                        goto DO_BAD_CONV_CHAR;
 235                                }
 236                        } else {
 237 DO_BAD_CONV_CHAR:
 238                                bb_error_msg_and_die("bad conversion character %%%s", p1);
 239                        }
 240
 241                        /*
 242                         * copy to PR format string, set conversion character
 243                         * pointer, update original.
 244                         */
 245                        len = (p1 - fmtp) + 1;
 246                        pr->fmt = xstrndup(fmtp, len);
 247                        /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
 248                         * Skip subsequent text and up to the next % sign and tack the
 249                         * additional text onto fmt: eg. if fmt is "%x is a HEX number",
 250                         * we lose the " is a HEX number" part of fmt.
 251                         */
 252                        for (p3 = p2; *p3 && *p3 != '%'; p3++)
 253                                continue;
 254                        if ((p3 - p2) != 0) {
 255                                char *d;
 256                                pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
 257                                d += len;
 258                                do {
 259                                        *d++ = *p2++;
 260                                } while (p2 != p3);
 261                                *d = '\0';
 262                                /* now p2 = p3 */
 263                        }
 264                        pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
 265                        fmtp = p2;
 266
 267                        /* only one conversion character if byte count */
 268                        if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
 269                                bb_error_msg_and_die("byte count with multiple conversion characters");
 270                        }
 271                }
 272                /*
 273                 * if format unit byte count not specified, figure it out
 274                 * so can adjust rep count later.
 275                 */
 276                if (fu->bcnt == 0)
 277                        for (pr = fu->nextpr; pr; pr = pr->nextpr)
 278                                fu->bcnt += pr->bcnt;
 279        }
 280        /*
 281         * if the format string interprets any data at all, and it's
 282         * not the same as the blocksize, and its last format unit
 283         * interprets any data at all, and has no iteration count,
 284         * repeat it as necessary.
 285         *
 286         * if rep count is greater than 1, no trailing whitespace
 287         * gets output from the last iteration of the format unit.
 288         */
 289        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
 290                if (!fu->nextfu
 291                 && fs->bcnt < dumper->blocksize
 292                 && !(fu->flags & F_SETREP)
 293                 && fu->bcnt
 294                ) {
 295                        fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
 296                }
 297                if (fu->reps > 1 && fu->nextpr) {
 298                        PR *pr;
 299                        char *p1, *p2;
 300
 301                        for (pr = fu->nextpr;; pr = pr->nextpr)
 302                                if (!pr->nextpr)
 303                                        break;
 304                        p2 = NULL;
 305                        for (p1 = pr->fmt; *p1; ++p1)
 306                                p2 = isspace(*p1) ? p1 : NULL;
 307                        if (p2)
 308                                pr->nospace = p2;
 309                }
 310        }
 311}
 312
 313static void do_skip(priv_dumper_t *dumper, const char *fname)
 314{
 315        struct stat sbuf;
 316
 317        xfstat(STDIN_FILENO, &sbuf, fname);
 318        if (S_ISREG(sbuf.st_mode)
 319         && dumper->pub.dump_skip >= sbuf.st_size
 320        ) {
 321                /* If st_size is valid and pub.dump_skip >= st_size */
 322                dumper->pub.dump_skip -= sbuf.st_size;
 323                dumper->address += sbuf.st_size;
 324                return;
 325        }
 326        if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
 327                bb_simple_perror_msg_and_die(fname);
 328        }
 329        dumper->address += dumper->pub.dump_skip;
 330        dumper->savaddress = dumper->address;
 331        dumper->pub.dump_skip = 0;
 332}
 333
 334static NOINLINE int next(priv_dumper_t *dumper)
 335{
 336        for (;;) {
 337                const char *fname = *dumper->argv;
 338
 339                if (fname) {
 340                        dumper->argv++;
 341                        if (NOT_LONE_DASH(fname)) {
 342                                if (!freopen(fname, "r", stdin)) {
 343                                        bb_simple_perror_msg(fname);
 344                                        dumper->exitval = 1;
 345                                        continue;
 346                                }
 347                        }
 348                } else {
 349                        if (dumper->next__done)
 350                                return 0; /* no next file */
 351                }
 352                dumper->next__done = 1;
 353                if (dumper->pub.dump_skip)
 354                        do_skip(dumper, fname ? fname : "stdin");
 355                if (dumper->pub.dump_skip == 0)
 356                        return 1;
 357        }
 358        /* NOTREACHED */
 359}
 360
 361static unsigned char *get(priv_dumper_t *dumper)
 362{
 363        int n;
 364        int need, nread;
 365        int blocksize = dumper->blocksize;
 366
 367        if (!dumper->get__curp) {
 368                dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
 369                dumper->get__curp = xmalloc(blocksize);
 370                dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
 371        } else {
 372                unsigned char *tmp = dumper->get__curp;
 373                dumper->get__curp = dumper->get__savp;
 374                dumper->get__savp = tmp;
 375                dumper->savaddress += blocksize;
 376                dumper->address = dumper->savaddress;
 377        }
 378        need = blocksize;
 379        nread = 0;
 380        while (1) {
 381                /*
 382                 * if read the right number of bytes, or at EOF for one file,
 383                 * and no other files are available, zero-pad the rest of the
 384                 * block and set the end flag.
 385                 */
 386                if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
 387                        if (need == blocksize) {
 388                                return NULL;
 389                        }
 390                        if (dumper->pub.dump_vflag != ALL   /* not "show all"? */
 391                         && dumper->pub.dump_vflag != FIRST /* not first line? */
 392                         && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */
 393                        ) {
 394                                if (dumper->pub.dump_vflag != DUP) {
 395                                        puts("*");
 396                                }
 397                                return NULL;
 398                        }
 399                        memset(dumper->get__curp + nread, 0, need);
 400                        dumper->eaddress = dumper->address + nread;
 401                        return dumper->get__curp;
 402                }
 403                n = fread(dumper->get__curp + nread, sizeof(unsigned char),
 404                                dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
 405                if (n == 0) {
 406                        if (ferror(stdin)) {
 407                                bb_simple_perror_msg(dumper->argv[-1]);
 408                        }
 409                        dumper->get__ateof = 1;
 410                        continue;
 411                }
 412                dumper->get__ateof = 0;
 413                if (dumper->pub.dump_length != -1) {
 414                        dumper->pub.dump_length -= n;
 415                }
 416                need -= n;
 417                if (need == 0) {
 418                        if (dumper->pub.dump_vflag == ALL   /* "show all"? */
 419                         || dumper->pub.dump_vflag == FIRST /* first line? */
 420                         || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */
 421                        ) {
 422                                if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
 423                                        dumper->pub.dump_vflag = WAIT;
 424                                }
 425                                return dumper->get__curp;
 426                        }
 427                        if (dumper->pub.dump_vflag == WAIT) {
 428                                puts("*");
 429                        }
 430                        dumper->pub.dump_vflag = DUP;
 431                        dumper->savaddress += blocksize;
 432                        dumper->address = dumper->savaddress;
 433                        need = blocksize;
 434                        nread = 0;
 435                } else {
 436                        nread += n;
 437                }
 438        }
 439}
 440
 441static void bpad(PR *pr)
 442{
 443        char *p1, *p2;
 444
 445        /*
 446         * remove all conversion flags; '-' is the only one valid
 447         * with %s, and it's not useful here.
 448         */
 449        pr->flags = F_BPAD;
 450        *pr->cchar = 's';
 451        for (p1 = pr->fmt; *p1 != '%'; ++p1)
 452                continue;
 453        for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
 454                if (pr->nospace)
 455                        pr->nospace--;
 456        while ((*p2++ = *p1++) != 0)
 457                continue;
 458}
 459
 460static const char conv_str[] ALIGN1 =
 461        "\0"  "\\""0""\0"
 462        "\007""\\""a""\0"  /* \a */
 463        "\b"  "\\""b""\0"
 464        "\f"  "\\""f""\0"
 465        "\n"  "\\""n""\0"
 466        "\r"  "\\""r""\0"
 467        "\t"  "\\""t""\0"
 468        "\v"  "\\""v""\0"
 469        ;
 470
 471static void conv_c(PR *pr, unsigned char *p)
 472{
 473        const char *str = conv_str;
 474
 475        do {
 476                if (*p == *str) {
 477                        ++str;
 478                        goto strpr; /* map e.g. '\n' to "\\n" */
 479                }
 480                str += 4;
 481        } while (*str);
 482
 483        if (isprint_asciionly(*p)) {
 484                *pr->cchar = 'c';
 485                printf(pr->fmt, *p);
 486        } else {
 487                char buf[4];
 488                /* gcc-8.0.1 needs lots of casts to shut up */
 489                sprintf(buf, "%03o", (unsigned)(uint8_t)*p);
 490                str = buf;
 491 strpr:
 492                *pr->cchar = 's';
 493                printf(pr->fmt, str);
 494        }
 495}
 496
 497static void conv_u(PR *pr, unsigned char *p)
 498{
 499        static const char list[] ALIGN1 =
 500                "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
 501                "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
 502                "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
 503                "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
 504
 505        /* od used nl, not lf */
 506        if (*p <= 0x1f) {
 507                *pr->cchar = 's';
 508                printf(pr->fmt, list + (4 * (int)*p));
 509        } else if (*p == 0x7f) {
 510                *pr->cchar = 's';
 511                printf(pr->fmt, "del");
 512        } else if (*p < 0x7f) { /* isprint() */
 513                *pr->cchar = 'c';
 514                printf(pr->fmt, *p);
 515        } else {
 516                *pr->cchar = 'x';
 517                printf(pr->fmt, (int) *p);
 518        }
 519}
 520
 521static void display(priv_dumper_t* dumper)
 522{
 523        FS *fs;
 524        FU *fu;
 525        PR *pr;
 526        int cnt;
 527        unsigned char *bp, *savebp;
 528        off_t saveaddress;
 529        unsigned char savech = '\0';
 530
 531        while ((bp = get(dumper)) != NULL) {
 532                fs = dumper->pub.fshead;
 533                savebp = bp;
 534                saveaddress = dumper->address;
 535                for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
 536                        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
 537                                if (fu->flags & F_IGNORE) {
 538                                        break;
 539                                }
 540                                for (cnt = fu->reps; cnt; --cnt) {
 541                                        for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
 542                                                                bp += pr->bcnt, pr = pr->nextpr) {
 543                                                if (dumper->eaddress && dumper->address >= dumper->eaddress
 544                                                 && !(pr->flags & (F_TEXT | F_BPAD))
 545                                                ) {
 546                                                        bpad(pr);
 547                                                }
 548                                                if (cnt == 1 && pr->nospace) {
 549                                                        savech = *pr->nospace;
 550                                                        *pr->nospace = '\0';
 551                                                }
 552/*                      PRINT; */
 553                                                switch (pr->flags) {
 554                                                case F_ADDRESS:
 555                                                        printf(pr->fmt, (unsigned) dumper->address);
 556                                                        break;
 557                                                case F_BPAD:
 558                                                        printf(pr->fmt, "");
 559                                                        break;
 560                                                case F_C:
 561                                                        conv_c(pr, bp);
 562                                                        break;
 563                                                case F_CHAR:
 564                                                        printf(pr->fmt, *bp);
 565                                                        break;
 566                                                case F_DBL: {
 567                                                        double dval;
 568                                                        float fval;
 569
 570                                                        switch (pr->bcnt) {
 571                                                        case 4:
 572                                                                memcpy(&fval, bp, sizeof(fval));
 573                                                                printf(pr->fmt, fval);
 574                                                                break;
 575                                                        case 8:
 576                                                                memcpy(&dval, bp, sizeof(dval));
 577                                                                printf(pr->fmt, dval);
 578                                                                break;
 579                                                        }
 580                                                        break;
 581                                                }
 582                                                case F_INT: {
 583                                                        int ival;
 584                                                        short sval;
 585
 586                                                        switch (pr->bcnt) {
 587                                                        case 1:
 588                                                                printf(pr->fmt, (int) *bp);
 589                                                                break;
 590                                                        case 2:
 591                                                                memcpy(&sval, bp, sizeof(sval));
 592                                                                printf(pr->fmt, (int) sval);
 593                                                                break;
 594                                                        case 4:
 595                                                                memcpy(&ival, bp, sizeof(ival));
 596                                                                printf(pr->fmt, ival);
 597                                                                break;
 598                                                        }
 599                                                        break;
 600                                                }
 601                                                case F_P:
 602                                                        printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
 603                                                        break;
 604                                                case F_STR:
 605                                                        printf(pr->fmt, (char *) bp);
 606                                                        break;
 607                                                case F_TEXT:
 608                                                        printf(pr->fmt);
 609                                                        break;
 610                                                case F_U:
 611                                                        conv_u(pr, bp);
 612                                                        break;
 613                                                case F_UINT: {
 614                                                        unsigned ival;
 615                                                        unsigned short sval;
 616
 617                                                        switch (pr->bcnt) {
 618                                                        case 1:
 619                                                                printf(pr->fmt, (unsigned) *bp);
 620                                                                break;
 621                                                        case 2:
 622                                                                memcpy(&sval, bp, sizeof(sval));
 623                                                                printf(pr->fmt, (unsigned) sval);
 624                                                                break;
 625                                                        case 4:
 626                                                                memcpy(&ival, bp, sizeof(ival));
 627                                                                printf(pr->fmt, ival);
 628                                                                break;
 629                                                        }
 630                                                        break;
 631                                                }
 632                                                }
 633                                                if (cnt == 1 && pr->nospace) {
 634                                                        *pr->nospace = savech;
 635                                                }
 636                                        }
 637                                }
 638                        }
 639                }
 640        }
 641        if (dumper->endfu) {
 642                /*
 643                 * if eaddress not set, error or file size was multiple
 644                 * of blocksize, and no partial block ever found.
 645                 */
 646                if (!dumper->eaddress) {
 647                        if (!dumper->address) {
 648                                return;
 649                        }
 650                        dumper->eaddress = dumper->address;
 651                }
 652                for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
 653                        switch (pr->flags) {
 654                        case F_ADDRESS:
 655                                printf(pr->fmt, (unsigned) dumper->eaddress);
 656                                break;
 657                        case F_TEXT:
 658                                printf(pr->fmt);
 659                                break;
 660                        }
 661                }
 662        }
 663}
 664
 665#define dumper ((priv_dumper_t*)pub_dumper)
 666int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
 667{
 668        FS *tfs;
 669        int blocksize;
 670
 671        /* figure out the data block size */
 672        blocksize = 0;
 673        tfs = dumper->pub.fshead;
 674        while (tfs) {
 675                tfs->bcnt = bb_dump_size(tfs);
 676                if (blocksize < tfs->bcnt) {
 677                        blocksize = tfs->bcnt;
 678                }
 679                tfs = tfs->nextfs;
 680        }
 681        dumper->blocksize = blocksize;
 682
 683        /* rewrite the rules, do syntax checking */
 684        for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
 685                rewrite(dumper, tfs);
 686        }
 687
 688        dumper->argv = argv;
 689        display(dumper);
 690
 691        return dumper->exitval;
 692}
 693
 694void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
 695{
 696        const char *p;
 697        FS *tfs;
 698        FU *tfu, **nextfupp;
 699        const char *savep;
 700
 701        /* start new linked list of format units */
 702        tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
 703        if (!dumper->pub.fshead) {
 704                dumper->pub.fshead = tfs;
 705        } else {
 706                FS *fslast = dumper->pub.fshead;
 707                while (fslast->nextfs)
 708                        fslast = fslast->nextfs;
 709                fslast->nextfs = tfs;
 710        }
 711        nextfupp = &tfs->nextfu;
 712
 713        /* take the format string and break it up into format units */
 714        p = fmt;
 715        for (;;) {
 716                p = skip_whitespace(p);
 717                if (*p == '\0') {
 718                        break;
 719                }
 720
 721                /* allocate a new format unit and link it in */
 722                /* NOSTRICT */
 723                /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
 724                tfu = xzalloc(sizeof(FU));
 725                *nextfupp = tfu;
 726                nextfupp = &tfu->nextfu;
 727                tfu->reps = 1;
 728
 729                /* if leading digit, repetition count */
 730                if (isdigit(*p)) {
 731                        for (savep = p; isdigit(*p); ++p)
 732                                continue;
 733                        if (!isspace(*p) && *p != '/') {
 734                                bb_error_msg_and_die("bad format {%s}", fmt);
 735                        }
 736                        /* may overwrite either white space or slash */
 737                        tfu->reps = atoi(savep);
 738                        tfu->flags = F_SETREP;
 739                        /* skip trailing white space */
 740                        p = skip_whitespace(++p);
 741                }
 742
 743                /* skip slash and trailing white space */
 744                if (*p == '/') {
 745                        p = skip_whitespace(p + 1);
 746                }
 747
 748                /* byte count */
 749                if (isdigit(*p)) {
 750// TODO: use bb_strtou
 751                        savep = p;
 752                        while (isdigit(*++p))
 753                                continue;
 754                        if (!isspace(*p)) {
 755                                bb_error_msg_and_die("bad format {%s}", fmt);
 756                        }
 757// Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
 758// Other than this, formats can be pretty much jammed together:
 759// "%07_ax:"8/2 "%04x|""\n"
 760// but this space is required. The check *can* be removed, but
 761// keeping it to stay compat with util-linux hexdump.
 762                        tfu->bcnt = atoi(savep);
 763                        /* skip trailing white space */
 764                        p = skip_whitespace(p + 1);
 765                }
 766
 767                /* format */
 768                if (*p != '"') {
 769                        bb_error_msg_and_die("bad format {%s}", fmt);
 770                }
 771                for (savep = ++p; *p != '"';) {
 772                        if (*p++ == '\0') {
 773                                bb_error_msg_and_die("bad format {%s}", fmt);
 774                        }
 775                }
 776                tfu->fmt = xstrndup(savep, p - savep);
 777
 778                /* alphabetic escape sequences have to be done in place */
 779                strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
 780                /* unknown mappings are not changed: "\z" -> '\\' 'z' */
 781                /* trailing backslash, if any, is preserved */
 782#if 0
 783                char *p1;
 784                char *p2;
 785                p1 = tfu->fmt;
 786                for (p2 = p1;; ++p1, ++p2) {
 787                        *p2 = *p1;
 788                        if (*p1 == '\0')
 789                                break;
 790
 791                        if (*p1 == '\\') {
 792                                const char *cs;
 793
 794                                p1++;
 795                                *p2 = *p1;
 796                                if (*p1 == '\0') {
 797                                        /* "...\" trailing backslash. Eaten. */
 798                                        break;
 799                                }
 800                                cs = conv_str + 4; /* skip NUL element */
 801                                do {
 802                                        /* map e.g. "\n" -> '\n' */
 803                                        if (*p1 == cs[2]) {
 804                                                *p2 = cs[0];
 805                                                break;
 806                                        }
 807                                        cs += 4;
 808                                } while (*cs);
 809                                /* unknown mappings remove bkslash: "\z" -> 'z' */
 810                        }
 811                }
 812#endif
 813
 814                p++;
 815        }
 816}
 817
 818/*
 819 * Copyright (c) 1989 The Regents of the University of California.
 820 * All rights reserved.
 821 *
 822 * Redistribution and use in source and binary forms, with or without
 823 * modification, are permitted provided that the following conditions
 824 * are met:
 825 * 1. Redistributions of source code must retain the above copyright
 826 *    notice, this list of conditions and the following disclaimer.
 827 * 2. Redistributions in binary form must reproduce the above copyright
 828 *    notice, this list of conditions and the following disclaimer in the
 829 *    documentation and/or other materials provided with the distribution.
 830 * 3. Neither the name of the University nor the names of its contributors
 831 *    may be used to endorse or promote products derived from this software
 832 *    without specific prior written permission.
 833 *
 834 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
 835 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 836 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 837 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 838 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 839 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 840 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 841 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 842 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 843 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 844 * SUCH DAMAGE.
 845 */
 846