3
0

dump.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. if (e > int_convs + 1) /* not d or i? */
  185. pr->flags = F_UINT;
  186. byte_count_str = "\010\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(float_convs, *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_simple_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++ = 'l';
  220. *p1++ = 'l';
  221. break;
  222. case 'c': /* %_c: chars, \ooo, \n \r \t etc */
  223. pr->flags = F_C;
  224. /* *p1 = 'c'; set in conv_c */
  225. goto DO_BYTE_COUNT_1;
  226. case 'p': /* %_p: chars, dots for nonprintable */
  227. pr->flags = F_P;
  228. *p1 = 'c';
  229. goto DO_BYTE_COUNT_1;
  230. case 'u': /* %_u: chars, 'nul', 'esc' etc for nonprintable */
  231. pr->flags = F_U;
  232. /* *p1 = 'c'; set in conv_u */
  233. goto DO_BYTE_COUNT_1;
  234. default:
  235. goto DO_BAD_CONV_CHAR;
  236. }
  237. } else {
  238. DO_BAD_CONV_CHAR:
  239. bb_error_msg_and_die("bad conversion character %%%s", p1);
  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. /* only one conversion character if byte count */
  267. if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
  268. bb_simple_error_msg_and_die("byte count with multiple conversion characters");
  269. }
  270. }
  271. /*
  272. * if format unit byte count not specified, figure it out
  273. * so can adjust rep count later.
  274. */
  275. if (fu->bcnt == 0)
  276. for (pr = fu->nextpr; pr; pr = pr->nextpr)
  277. fu->bcnt += pr->bcnt;
  278. }
  279. /*
  280. * if the format string interprets any data at all, and it's
  281. * not the same as the blocksize, and its last format unit
  282. * interprets any data at all, and has no iteration count,
  283. * repeat it as necessary.
  284. *
  285. * if rep count is greater than 1, no trailing whitespace
  286. * gets output from the last iteration of the format unit:
  287. * 2/1 "%02x " prints "XX XX", not "XX XX "
  288. * 2/1 "%02x\n" prints "XX\nXX", not "XX\nXX\n"
  289. */
  290. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  291. if (!fu->nextfu
  292. && fs->bcnt < dumper->blocksize
  293. && !(fu->flags & F_SETREP)
  294. && fu->bcnt
  295. ) {
  296. fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
  297. }
  298. if (fu->reps > 1 && fu->nextpr) {
  299. PR *pr;
  300. char *p1, *p2;
  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. pr->nospace = p2;
  308. }
  309. }
  310. }
  311. static void do_skip(priv_dumper_t *dumper, const char *fname)
  312. {
  313. struct stat sbuf;
  314. xfstat(STDIN_FILENO, &sbuf, fname);
  315. if (S_ISREG(sbuf.st_mode)
  316. && dumper->pub.dump_skip >= sbuf.st_size
  317. ) {
  318. /* If st_size is valid and pub.dump_skip >= st_size */
  319. dumper->pub.dump_skip -= sbuf.st_size;
  320. dumper->pub.address += sbuf.st_size;
  321. return;
  322. }
  323. if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
  324. bb_simple_perror_msg_and_die(fname);
  325. }
  326. dumper->pub.address += dumper->pub.dump_skip;
  327. dumper->savaddress = dumper->pub.address;
  328. dumper->pub.dump_skip = 0;
  329. }
  330. static NOINLINE int next(priv_dumper_t *dumper)
  331. {
  332. for (;;) {
  333. const char *fname = *dumper->argv;
  334. if (fname) {
  335. dumper->argv++;
  336. if (NOT_LONE_DASH(fname)) {
  337. if (!freopen(fname, "r", stdin)) {
  338. bb_simple_perror_msg(fname);
  339. dumper->exitval = 1;
  340. dumper->next__done = 1;
  341. continue;
  342. }
  343. }
  344. } else {
  345. if (dumper->next__done)
  346. return 0; /* no next file */
  347. }
  348. dumper->next__done = 1;
  349. if (dumper->pub.dump_skip)
  350. do_skip(dumper, fname ? fname : "stdin");
  351. if (dumper->pub.dump_skip == 0)
  352. return 1;
  353. }
  354. /* NOTREACHED */
  355. }
  356. static unsigned char *get(priv_dumper_t *dumper)
  357. {
  358. int n;
  359. int need, nread;
  360. int blocksize = dumper->blocksize;
  361. if (!dumper->get__curp) {
  362. dumper->pub.address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
  363. dumper->get__curp = xmalloc(blocksize);
  364. dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
  365. } else {
  366. unsigned char *tmp = dumper->get__curp;
  367. dumper->get__curp = dumper->get__savp;
  368. dumper->get__savp = tmp;
  369. dumper->savaddress += blocksize;
  370. dumper->pub.address = dumper->savaddress;
  371. }
  372. need = blocksize;
  373. nread = 0;
  374. while (1) {
  375. /*
  376. * if read the right number of bytes, or at EOF for one file,
  377. * and no other files are available, zero-pad the rest of the
  378. * block and set the end flag.
  379. */
  380. if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
  381. if (need == blocksize) {
  382. return NULL;
  383. }
  384. if (dumper->pub.dump_vflag != ALL /* not "show all"? */
  385. && dumper->pub.dump_vflag != FIRST /* not first line? */
  386. && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */
  387. ) {
  388. if (dumper->pub.dump_vflag != DUP) {
  389. puts("*");
  390. }
  391. }
  392. memset(dumper->get__curp + nread, 0, need);
  393. dumper->eaddress = dumper->pub.address + nread;
  394. return dumper->get__curp;
  395. }
  396. n = fread(dumper->get__curp + nread, sizeof(unsigned char),
  397. dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
  398. if (n == 0) {
  399. if (ferror(stdin)) {
  400. bb_simple_perror_msg(dumper->argv[-1]);
  401. }
  402. dumper->get__ateof = 1;
  403. continue;
  404. }
  405. dumper->get__ateof = 0;
  406. if (dumper->pub.dump_length != -1) {
  407. dumper->pub.dump_length -= n;
  408. }
  409. need -= n;
  410. if (need == 0) {
  411. if (dumper->pub.dump_vflag == ALL /* "show all"? */
  412. || dumper->pub.dump_vflag == FIRST /* first line? */
  413. || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */
  414. ) {
  415. if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
  416. dumper->pub.dump_vflag = WAIT;
  417. }
  418. return dumper->get__curp;
  419. }
  420. if (dumper->pub.dump_vflag == WAIT) {
  421. puts("*");
  422. }
  423. dumper->pub.dump_vflag = DUP;
  424. dumper->savaddress += blocksize;
  425. dumper->pub.address = dumper->savaddress;
  426. need = blocksize;
  427. nread = 0;
  428. } else {
  429. nread += n;
  430. }
  431. }
  432. }
  433. static void bpad(PR *pr)
  434. {
  435. char *p1, *p2;
  436. /*
  437. * remove all conversion flags; '-' is the only one valid
  438. * with %s, and it's not useful here.
  439. */
  440. pr->flags = F_BPAD;
  441. *pr->cchar = 's';
  442. for (p1 = pr->fmt; *p1 != '%'; ++p1)
  443. continue;
  444. for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
  445. if (pr->nospace)
  446. pr->nospace--;
  447. while ((*p2++ = *p1++) != '\0')
  448. continue;
  449. }
  450. static const char conv_str[] ALIGN1 =
  451. "\0" "\\""0""\0"
  452. "\007""\\""a""\0"
  453. "\b" "\\""b""\0"
  454. "\f" "\\""f""\0"
  455. "\n" "\\""n""\0"
  456. "\r" "\\""r""\0"
  457. "\t" "\\""t""\0"
  458. "\v" "\\""v""\0"
  459. ;
  460. static void conv_c(PR *pr, unsigned char *p)
  461. {
  462. const char *str = conv_str;
  463. do {
  464. if (*p == *str) {
  465. ++str;
  466. goto strpr; /* map e.g. '\n' to "\\n" */
  467. }
  468. str += 4;
  469. } while (*str);
  470. if (isprint_asciionly(*p)) {
  471. *pr->cchar = 'c';
  472. printf(pr->fmt, *p);
  473. } else {
  474. char buf[4];
  475. /* gcc-8.0.1 needs lots of casts to shut up */
  476. sprintf(buf, "%03o", (unsigned)(uint8_t)*p);
  477. str = buf;
  478. strpr:
  479. *pr->cchar = 's';
  480. printf(pr->fmt, str);
  481. }
  482. }
  483. static void conv_u(PR *pr, unsigned char *p)
  484. {
  485. static const char list[] ALIGN1 =
  486. "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
  487. "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
  488. "dle\0dc1\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
  489. "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
  490. /* NB: bug: od uses %_u to implement -a,
  491. * but it should use "nl", not "lf", for char #10.
  492. */
  493. if (*p <= 0x1f) {
  494. *pr->cchar = 's';
  495. printf(pr->fmt, list + (4 * (int)*p));
  496. } else if (*p == 0x7f) {
  497. *pr->cchar = 's';
  498. printf(pr->fmt, "del");
  499. } else if (*p < 0x7f) { /* isprint() */
  500. *pr->cchar = 'c';
  501. printf(pr->fmt, *p);
  502. } else {
  503. *pr->cchar = 'x';
  504. printf(pr->fmt, (int) *p);
  505. }
  506. }
  507. static NOINLINE void display(priv_dumper_t* dumper)
  508. {
  509. unsigned char *bp;
  510. while ((bp = get(dumper)) != NULL) {
  511. FS *fs;
  512. unsigned char *savebp;
  513. off_t saveaddress;
  514. fs = dumper->pub.fshead;
  515. savebp = bp;
  516. saveaddress = dumper->pub.address;
  517. for (; fs; fs = fs->nextfs, bp = savebp, dumper->pub.address = saveaddress) {
  518. FU *fu;
  519. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  520. int cnt;
  521. if (fu->flags & F_IGNORE) {
  522. break;
  523. }
  524. for (cnt = fu->reps; cnt; --cnt) {
  525. PR *pr;
  526. for (pr = fu->nextpr; pr; dumper->pub.address += pr->bcnt,
  527. bp += pr->bcnt, pr = pr->nextpr) {
  528. unsigned char savech;
  529. if (dumper->eaddress
  530. && dumper->pub.address >= dumper->eaddress
  531. ) {
  532. #if ENABLE_XXD
  533. if (dumper->pub.xxd_eofstring) {
  534. /* xxd support: requested to not pad incomplete blocks */
  535. fputs_stdout(dumper->pub.xxd_eofstring);
  536. return;
  537. }
  538. #endif
  539. #if ENABLE_OD
  540. if (dumper->pub.od_eofstring) {
  541. /* od support: requested to not pad incomplete blocks */
  542. /* ... but do print final offset */
  543. fputs_stdout(dumper->pub.od_eofstring);
  544. goto endfu;
  545. }
  546. #endif
  547. if (!(pr->flags & (F_TEXT | F_BPAD)))
  548. bpad(pr);
  549. }
  550. savech = '\0';
  551. if (cnt == 1 && pr->nospace) {
  552. savech = *pr->nospace;
  553. *pr->nospace = '\0';
  554. }
  555. switch (pr->flags) {
  556. case F_ADDRESS:
  557. printf(pr->fmt, (unsigned long long) dumper->pub.address
  558. #if ENABLE_XXD
  559. + dumper->pub.xxd_displayoff
  560. #endif
  561. );
  562. break;
  563. case F_BPAD:
  564. printf(pr->fmt, "");
  565. break;
  566. case F_C:
  567. conv_c(pr, bp);
  568. break;
  569. case F_CHAR:
  570. printf(pr->fmt, *bp);
  571. break;
  572. case F_DBL: {
  573. double dval;
  574. float fval;
  575. switch (pr->bcnt) {
  576. case 4:
  577. memcpy(&fval, bp, sizeof(fval));
  578. printf(pr->fmt, fval);
  579. break;
  580. case 8:
  581. memcpy(&dval, bp, sizeof(dval));
  582. printf(pr->fmt, dval);
  583. break;
  584. }
  585. break;
  586. }
  587. case F_INT: {
  588. union {
  589. int16_t ival16;
  590. int32_t ival32;
  591. int64_t ival64;
  592. } u;
  593. int value = (signed char)*bp;
  594. switch (pr->bcnt) {
  595. case 1:
  596. break;
  597. case 2:
  598. move_from_unaligned16(u.ival16, bp);
  599. value = u.ival16;
  600. break;
  601. case 4:
  602. move_from_unaligned32(u.ival32, bp);
  603. value = u.ival32;
  604. break;
  605. case 8:
  606. move_from_unaligned64(u.ival64, bp);
  607. //A hack. Users _must_ use %llX formats to not truncate high bits
  608. printf(pr->fmt, (long long)u.ival64);
  609. goto skip;
  610. }
  611. printf(pr->fmt, value);
  612. skip:
  613. break;
  614. }
  615. case F_P:
  616. printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
  617. break;
  618. case F_STR:
  619. printf(pr->fmt, (char *) bp);
  620. break;
  621. case F_TEXT:
  622. fputs_stdout(pr->fmt);
  623. break;
  624. case F_U:
  625. conv_u(pr, bp);
  626. break;
  627. case F_UINT: {
  628. unsigned value = (unsigned char)*bp;
  629. switch (pr->bcnt) {
  630. case 1:
  631. break;
  632. case 2:
  633. move_from_unaligned16(value, bp);
  634. break;
  635. case 4:
  636. move_from_unaligned32(value, bp);
  637. break;
  638. /* case 8: no users yet */
  639. }
  640. printf(pr->fmt, value);
  641. break;
  642. }
  643. }
  644. if (savech) {
  645. *pr->nospace = savech;
  646. }
  647. }
  648. }
  649. }
  650. }
  651. }
  652. IF_OD(endfu:)
  653. if (dumper->endfu) {
  654. PR *pr;
  655. /*
  656. * if eaddress not set, error or file size was multiple
  657. * of blocksize, and no partial block ever found.
  658. */
  659. if (!dumper->eaddress) {
  660. if (!dumper->pub.address) {
  661. return;
  662. }
  663. dumper->eaddress = dumper->pub.address;
  664. }
  665. for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
  666. switch (pr->flags) {
  667. case F_ADDRESS:
  668. printf(pr->fmt, (unsigned long long) dumper->eaddress
  669. #if ENABLE_XXD
  670. + dumper->pub.xxd_displayoff
  671. #endif
  672. );
  673. break;
  674. case F_TEXT:
  675. fputs_stdout(pr->fmt);
  676. break;
  677. }
  678. }
  679. }
  680. }
  681. #define dumper ((priv_dumper_t*)pub_dumper)
  682. int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
  683. {
  684. FS *tfs;
  685. int blocksize;
  686. /* figure out the data block size */
  687. blocksize = 0;
  688. tfs = dumper->pub.fshead;
  689. while (tfs) {
  690. tfs->bcnt = bb_dump_size(tfs);
  691. if (blocksize < tfs->bcnt) {
  692. blocksize = tfs->bcnt;
  693. }
  694. tfs = tfs->nextfs;
  695. }
  696. dumper->blocksize = blocksize;
  697. /* rewrite the rules, do syntax checking */
  698. for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
  699. rewrite(dumper, tfs);
  700. }
  701. dumper->argv = argv;
  702. display(dumper);
  703. return dumper->exitval;
  704. }
  705. void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
  706. {
  707. const char *p;
  708. FS *tfs;
  709. FU **nextfupp;
  710. /* start new linked list of format units */
  711. tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
  712. if (!dumper->pub.fshead) {
  713. dumper->pub.fshead = tfs;
  714. } else {
  715. FS *fslast = dumper->pub.fshead;
  716. while (fslast->nextfs)
  717. fslast = fslast->nextfs;
  718. fslast->nextfs = tfs;
  719. }
  720. nextfupp = &tfs->nextfu;
  721. /* take the format string and break it up into format units */
  722. p = fmt;
  723. for (;;) {
  724. FU *tfu;
  725. const char *savep;
  726. p = skip_whitespace(p);
  727. if (*p == '\0') {
  728. break;
  729. }
  730. /* allocate a new format unit and link it in */
  731. /* NOSTRICT */
  732. /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
  733. tfu = xzalloc(sizeof(FU));
  734. *nextfupp = tfu;
  735. nextfupp = &tfu->nextfu;
  736. tfu->reps = 1;
  737. /* if leading digit, repetition count */
  738. if (isdigit(*p)) {
  739. for (savep = p; isdigit(*p); ++p)
  740. continue;
  741. if (!isspace(*p) && *p != '/') {
  742. bb_error_msg_and_die("bad format {%s}", fmt);
  743. }
  744. /* may overwrite either white space or slash */
  745. tfu->reps = atoi(savep);
  746. tfu->flags = F_SETREP;
  747. /* skip trailing white space */
  748. p = skip_whitespace(++p);
  749. }
  750. /* skip slash and trailing white space */
  751. if (*p == '/') {
  752. p = skip_whitespace(p + 1);
  753. }
  754. /* byte count */
  755. if (isdigit(*p)) {
  756. // TODO: use bb_strtou
  757. savep = p;
  758. while (isdigit(*++p))
  759. continue;
  760. if (!isspace(*p)) {
  761. bb_error_msg_and_die("bad format {%s}", fmt);
  762. }
  763. // Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
  764. // Other than this, formats can be pretty much jammed together:
  765. // "%07_ax:"8/2 "%04x|""\n"
  766. // but this space is required. The check *can* be removed, but
  767. // keeping it to stay compat with util-linux hexdump.
  768. tfu->bcnt = atoi(savep);
  769. /* skip trailing white space */
  770. p = skip_whitespace(p + 1);
  771. }
  772. /* format */
  773. if (*p != '"') {
  774. bb_error_msg_and_die("bad format {%s}", fmt);
  775. }
  776. for (savep = ++p; *p != '"';) {
  777. if (*p++ == '\0') {
  778. bb_error_msg_and_die("bad format {%s}", fmt);
  779. }
  780. }
  781. tfu->fmt = xstrndup(savep, p - savep);
  782. /* alphabetic escape sequences have to be done in place */
  783. strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
  784. /* unknown mappings are not changed: "\z" -> '\\' 'z' */
  785. /* trailing backslash, if any, is preserved */
  786. #if 0
  787. char *p1;
  788. char *p2;
  789. p1 = tfu->fmt;
  790. for (p2 = p1;; ++p1, ++p2) {
  791. *p2 = *p1;
  792. if (*p1 == '\0')
  793. break;
  794. if (*p1 == '\\') {
  795. const char *cs;
  796. p1++;
  797. *p2 = *p1;
  798. if (*p1 == '\0') {
  799. /* "...\" trailing backslash. Eaten. */
  800. break;
  801. }
  802. cs = conv_str + 4; /* skip NUL element */
  803. do {
  804. /* map e.g. "\n" -> '\n' */
  805. if (*p1 == cs[2]) {
  806. *p2 = cs[0];
  807. break;
  808. }
  809. cs += 4;
  810. } while (*cs);
  811. /* unknown mappings remove bkslash: "\z" -> 'z' */
  812. }
  813. }
  814. #endif
  815. p++;
  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. */