dump.c 22 KB

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