dump.c 22 KB

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