dump.c 20 KB

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