dump.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 bb_dump_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, int statok)
  291. {
  292. struct stat sbuf;
  293. if (statok) {
  294. xfstat(STDIN_FILENO, &sbuf, fname);
  295. if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
  296. && dumper->pub.dump_skip >= sbuf.st_size
  297. ) {
  298. /* If bb_dump_size valid and pub.dump_skip >= size */
  299. dumper->pub.dump_skip -= sbuf.st_size;
  300. dumper->address += sbuf.st_size;
  301. return;
  302. }
  303. }
  304. if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
  305. bb_simple_perror_msg_and_die(fname);
  306. }
  307. dumper->address += dumper->pub.dump_skip;
  308. dumper->savaddress = dumper->address;
  309. dumper->pub.dump_skip = 0;
  310. }
  311. static NOINLINE int next(priv_dumper_t *dumper)
  312. {
  313. int statok;
  314. for (;;) {
  315. if (*dumper->argv) {
  316. dumper->next__done = statok = 1;
  317. if (!(freopen(*dumper->argv, "r", stdin))) {
  318. bb_simple_perror_msg(*dumper->argv);
  319. dumper->exitval = 1;
  320. ++dumper->argv;
  321. continue;
  322. }
  323. } else {
  324. if (dumper->next__done)
  325. return 0; /* no next file */
  326. dumper->next__done = 1;
  327. //why stat of stdin is specially prohibited?
  328. statok = 0;
  329. }
  330. if (dumper->pub.dump_skip)
  331. do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
  332. if (*dumper->argv)
  333. ++dumper->argv;
  334. if (!dumper->pub.dump_skip)
  335. return 1;
  336. }
  337. /* NOTREACHED */
  338. }
  339. static unsigned char *get(priv_dumper_t *dumper)
  340. {
  341. int n;
  342. int need, nread;
  343. int blocksize = dumper->blocksize;
  344. if (!dumper->get__curp) {
  345. dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
  346. dumper->get__curp = xmalloc(blocksize);
  347. dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
  348. } else {
  349. unsigned char *tmp = dumper->get__curp;
  350. dumper->get__curp = dumper->get__savp;
  351. dumper->get__savp = tmp;
  352. dumper->savaddress += blocksize;
  353. dumper->address = dumper->savaddress;
  354. }
  355. need = blocksize;
  356. nread = 0;
  357. while (1) {
  358. /*
  359. * if read the right number of bytes, or at EOF for one file,
  360. * and no other files are available, zero-pad the rest of the
  361. * block and set the end flag.
  362. */
  363. if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
  364. if (need == blocksize) {
  365. return NULL;
  366. }
  367. if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
  368. if (dumper->pub.dump_vflag != DUP) {
  369. puts("*");
  370. }
  371. return NULL;
  372. }
  373. memset(dumper->get__curp + nread, 0, need);
  374. dumper->eaddress = dumper->address + nread;
  375. return dumper->get__curp;
  376. }
  377. n = fread(dumper->get__curp + nread, sizeof(unsigned char),
  378. dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
  379. if (!n) {
  380. if (ferror(stdin)) {
  381. bb_simple_perror_msg(dumper->argv[-1]);
  382. }
  383. dumper->get__ateof = 1;
  384. continue;
  385. }
  386. dumper->get__ateof = 0;
  387. if (dumper->pub.dump_length != -1) {
  388. dumper->pub.dump_length -= n;
  389. }
  390. need -= n;
  391. if (!need) {
  392. if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
  393. || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
  394. ) {
  395. if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
  396. dumper->pub.dump_vflag = WAIT;
  397. }
  398. return dumper->get__curp;
  399. }
  400. if (dumper->pub.dump_vflag == WAIT) {
  401. puts("*");
  402. }
  403. dumper->pub.dump_vflag = DUP;
  404. dumper->savaddress += blocksize;
  405. dumper->address = dumper->savaddress;
  406. need = blocksize;
  407. nread = 0;
  408. } else {
  409. nread += n;
  410. }
  411. }
  412. }
  413. static void bpad(PR *pr)
  414. {
  415. char *p1, *p2;
  416. /*
  417. * remove all conversion flags; '-' is the only one valid
  418. * with %s, and it's not useful here.
  419. */
  420. pr->flags = F_BPAD;
  421. *pr->cchar = 's';
  422. for (p1 = pr->fmt; *p1 != '%'; ++p1)
  423. continue;
  424. for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
  425. if (pr->nospace)
  426. pr->nospace--;
  427. while ((*p2++ = *p1++) != 0)
  428. continue;
  429. }
  430. static const char conv_str[] ALIGN1 =
  431. "\0" "\\""0""\0"
  432. "\007""\\""a""\0" /* \a */
  433. "\b" "\\""b""\0"
  434. "\f" "\\""f""\0"
  435. "\n" "\\""n""\0"
  436. "\r" "\\""r""\0"
  437. "\t" "\\""t""\0"
  438. "\v" "\\""v""\0"
  439. ;
  440. static void conv_c(PR *pr, unsigned char *p)
  441. {
  442. const char *str = conv_str;
  443. char buf[10];
  444. do {
  445. if (*p == *str) {
  446. ++str;
  447. goto strpr; /* map e.g. '\n' to "\\n" */
  448. }
  449. str += 4;
  450. } while (*str);
  451. if (isprint_asciionly(*p)) {
  452. *pr->cchar = 'c';
  453. printf(pr->fmt, *p);
  454. } else {
  455. sprintf(buf, "%03o", (int) *p);
  456. str = buf;
  457. strpr:
  458. *pr->cchar = 's';
  459. printf(pr->fmt, str);
  460. }
  461. }
  462. static void conv_u(PR *pr, unsigned char *p)
  463. {
  464. static const char list[] ALIGN1 =
  465. "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
  466. "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
  467. "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
  468. "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
  469. /* od used nl, not lf */
  470. if (*p <= 0x1f) {
  471. *pr->cchar = 's';
  472. printf(pr->fmt, list + (4 * (int)*p));
  473. } else if (*p == 0x7f) {
  474. *pr->cchar = 's';
  475. printf(pr->fmt, "del");
  476. } else if (*p < 0x7f) { /* isprint() */
  477. *pr->cchar = 'c';
  478. printf(pr->fmt, *p);
  479. } else {
  480. *pr->cchar = 'x';
  481. printf(pr->fmt, (int) *p);
  482. }
  483. }
  484. static void display(priv_dumper_t* dumper)
  485. {
  486. FS *fs;
  487. FU *fu;
  488. PR *pr;
  489. int cnt;
  490. unsigned char *bp, *savebp;
  491. off_t saveaddress;
  492. unsigned char savech = '\0';
  493. while ((bp = get(dumper)) != NULL) {
  494. fs = dumper->pub.fshead;
  495. savebp = bp;
  496. saveaddress = dumper->address;
  497. for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
  498. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  499. if (fu->flags & F_IGNORE) {
  500. break;
  501. }
  502. for (cnt = fu->reps; cnt; --cnt) {
  503. for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
  504. bp += pr->bcnt, pr = pr->nextpr) {
  505. if (dumper->eaddress && dumper->address >= dumper->eaddress
  506. && !(pr->flags & (F_TEXT | F_BPAD))
  507. ) {
  508. bpad(pr);
  509. }
  510. if (cnt == 1 && pr->nospace) {
  511. savech = *pr->nospace;
  512. *pr->nospace = '\0';
  513. }
  514. /* PRINT; */
  515. switch (pr->flags) {
  516. case F_ADDRESS:
  517. printf(pr->fmt, (unsigned) dumper->address);
  518. break;
  519. case F_BPAD:
  520. printf(pr->fmt, "");
  521. break;
  522. case F_C:
  523. conv_c(pr, bp);
  524. break;
  525. case F_CHAR:
  526. printf(pr->fmt, *bp);
  527. break;
  528. case F_DBL: {
  529. double dval;
  530. float fval;
  531. switch (pr->bcnt) {
  532. case 4:
  533. memcpy(&fval, bp, sizeof(fval));
  534. printf(pr->fmt, fval);
  535. break;
  536. case 8:
  537. memcpy(&dval, bp, sizeof(dval));
  538. printf(pr->fmt, dval);
  539. break;
  540. }
  541. break;
  542. }
  543. case F_INT: {
  544. int ival;
  545. short sval;
  546. switch (pr->bcnt) {
  547. case 1:
  548. printf(pr->fmt, (int) *bp);
  549. break;
  550. case 2:
  551. memcpy(&sval, bp, sizeof(sval));
  552. printf(pr->fmt, (int) sval);
  553. break;
  554. case 4:
  555. memcpy(&ival, bp, sizeof(ival));
  556. printf(pr->fmt, ival);
  557. break;
  558. }
  559. break;
  560. }
  561. case F_P:
  562. printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
  563. break;
  564. case F_STR:
  565. printf(pr->fmt, (char *) bp);
  566. break;
  567. case F_TEXT:
  568. printf(pr->fmt);
  569. break;
  570. case F_U:
  571. conv_u(pr, bp);
  572. break;
  573. case F_UINT: {
  574. unsigned ival;
  575. unsigned short sval;
  576. switch (pr->bcnt) {
  577. case 1:
  578. printf(pr->fmt, (unsigned) *bp);
  579. break;
  580. case 2:
  581. memcpy(&sval, bp, sizeof(sval));
  582. printf(pr->fmt, (unsigned) sval);
  583. break;
  584. case 4:
  585. memcpy(&ival, bp, sizeof(ival));
  586. printf(pr->fmt, ival);
  587. break;
  588. }
  589. break;
  590. }
  591. }
  592. if (cnt == 1 && pr->nospace) {
  593. *pr->nospace = savech;
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. if (dumper->endfu) {
  601. /*
  602. * if eaddress not set, error or file size was multiple
  603. * of blocksize, and no partial block ever found.
  604. */
  605. if (!dumper->eaddress) {
  606. if (!dumper->address) {
  607. return;
  608. }
  609. dumper->eaddress = dumper->address;
  610. }
  611. for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
  612. switch (pr->flags) {
  613. case F_ADDRESS:
  614. printf(pr->fmt, (unsigned) dumper->eaddress);
  615. break;
  616. case F_TEXT:
  617. printf(pr->fmt);
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. #define dumper ((priv_dumper_t*)pub_dumper)
  624. int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
  625. {
  626. FS *tfs;
  627. int blocksize;
  628. /* figure out the data block bb_dump_size */
  629. blocksize = 0;
  630. tfs = dumper->pub.fshead;
  631. while (tfs) {
  632. tfs->bcnt = bb_dump_size(tfs);
  633. if (blocksize < tfs->bcnt) {
  634. blocksize = tfs->bcnt;
  635. }
  636. tfs = tfs->nextfs;
  637. }
  638. dumper->blocksize = blocksize;
  639. /* rewrite the rules, do syntax checking */
  640. for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
  641. rewrite(dumper, tfs);
  642. }
  643. dumper->argv = argv;
  644. display(dumper);
  645. return dumper->exitval;
  646. }
  647. void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
  648. {
  649. const char *p;
  650. FS *tfs;
  651. FU *tfu, **nextfupp;
  652. const char *savep;
  653. /* start new linked list of format units */
  654. tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
  655. if (!dumper->pub.fshead) {
  656. dumper->pub.fshead = tfs;
  657. } else {
  658. FS *fslast = dumper->pub.fshead;
  659. while (fslast->nextfs)
  660. fslast = fslast->nextfs;
  661. fslast->nextfs = tfs;
  662. }
  663. nextfupp = &tfs->nextfu;
  664. /* take the format string and break it up into format units */
  665. p = fmt;
  666. for (;;) {
  667. p = skip_whitespace(p);
  668. if (*p == '\0') {
  669. break;
  670. }
  671. /* allocate a new format unit and link it in */
  672. /* NOSTRICT */
  673. /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
  674. tfu = xzalloc(sizeof(FU));
  675. *nextfupp = tfu;
  676. nextfupp = &tfu->nextfu;
  677. tfu->reps = 1;
  678. /* if leading digit, repetition count */
  679. if (isdigit(*p)) {
  680. for (savep = p; isdigit(*p); ++p)
  681. continue;
  682. if (!isspace(*p) && *p != '/') {
  683. bb_error_msg_and_die("bad format {%s}", fmt);
  684. }
  685. /* may overwrite either white space or slash */
  686. tfu->reps = atoi(savep);
  687. tfu->flags = F_SETREP;
  688. /* skip trailing white space */
  689. p = skip_whitespace(++p);
  690. }
  691. /* skip slash and trailing white space */
  692. if (*p == '/') {
  693. p = skip_whitespace(p + 1);
  694. }
  695. /* byte count */
  696. if (isdigit(*p)) {
  697. // TODO: use bb_strtou
  698. savep = p;
  699. while (isdigit(*++p))
  700. continue;
  701. if (!isspace(*p)) {
  702. bb_error_msg_and_die("bad format {%s}", fmt);
  703. }
  704. // Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
  705. // Other than this, formats can be pretty much jammed together:
  706. // "%07_ax:"8/2 "%04x|""\n"
  707. // but this space is required. The check *can* be removed, but
  708. // keeping it to stay compat with util-linux hexdump.
  709. tfu->bcnt = atoi(savep);
  710. /* skip trailing white space */
  711. p = skip_whitespace(p + 1);
  712. }
  713. /* format */
  714. if (*p != '"') {
  715. bb_error_msg_and_die("bad format {%s}", fmt);
  716. }
  717. for (savep = ++p; *p != '"';) {
  718. if (*p++ == '\0') {
  719. bb_error_msg_and_die("bad format {%s}", fmt);
  720. }
  721. }
  722. tfu->fmt = xstrndup(savep, p - savep);
  723. /* alphabetic escape sequences have to be done in place */
  724. strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
  725. /* unknown mappings are not changed: "\z" -> '\\' 'z' */
  726. /* trailing backslash, if any, is preserved */
  727. #if 0
  728. char *p1;
  729. char *p2;
  730. p1 = tfu->fmt;
  731. for (p2 = p1;; ++p1, ++p2) {
  732. *p2 = *p1;
  733. if (*p1 == '\0')
  734. break;
  735. if (*p1 == '\\') {
  736. const char *cs;
  737. p1++;
  738. *p2 = *p1;
  739. if (*p1 == '\0') {
  740. /* "...\" trailing backslash. Eaten. */
  741. break;
  742. }
  743. cs = conv_str + 4; /* skip NUL element */
  744. do {
  745. /* map e.g. "\n" -> '\n' */
  746. if (*p1 == cs[2]) {
  747. *p2 = cs[0];
  748. break;
  749. }
  750. cs += 4;
  751. } while (*cs);
  752. /* unknown mappings remove bkslash: "\z" -> 'z' */
  753. }
  754. }
  755. #endif
  756. p++;
  757. }
  758. }
  759. /*
  760. * Copyright (c) 1989 The Regents of the University of California.
  761. * All rights reserved.
  762. *
  763. * Redistribution and use in source and binary forms, with or without
  764. * modification, are permitted provided that the following conditions
  765. * are met:
  766. * 1. Redistributions of source code must retain the above copyright
  767. * notice, this list of conditions and the following disclaimer.
  768. * 2. Redistributions in binary form must reproduce the above copyright
  769. * notice, this list of conditions and the following disclaimer in the
  770. * documentation and/or other materials provided with the distribution.
  771. * 3. Neither the name of the University nor the names of its contributors
  772. * may be used to endorse or promote products derived from this software
  773. * without specific prior written permission.
  774. *
  775. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  776. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  777. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  778. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  779. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  780. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  781. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  782. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  783. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  784. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  785. * SUCH DAMAGE.
  786. */