blob: fcdee8343309523811871970fc01c42fbdcbeaa3 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath60281112001-11-02 11:39:46 +00002/*
3 * Support code for the hexdump and od applets,
4 * based on code from util-linux v 2.11l
5 *
6 * Copyright (c) 1989
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02007 * The Regents of the University of California. All rights reserved.
Glenn L McGrath60281112001-11-02 11:39:46 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath60281112001-11-02 11:39:46 +000010 *
11 * Original copyright notice is retained at the end of this file.
12 */
Rob Landleyea224be2006-06-18 20:20:07 +000013#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000014#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000015
Denys Vlasenkodac5b832020-10-20 18:54:36 +020016#define F_IGNORE 0x01 /* %_A */
17#define F_SETREP 0x02 /* rep count set, not default */
18#define F_ADDRESS 0x001 /* print offset */
19#define F_BPAD 0x002 /* blank pad */
20#define F_C 0x004 /* %_c */
21#define F_CHAR 0x008 /* %c */
22#define F_DBL 0x010 /* %[EefGf] */
23#define F_INT 0x020 /* %[di] */
24#define F_P 0x040 /* %_p */
25#define F_STR 0x080 /* %s */
26#define F_U 0x100 /* %_u */
27#define F_UINT 0x200 /* %[ouXx] */
28#define F_TEXT 0x400 /* no conversions */
Glenn L McGrath60281112001-11-02 11:39:46 +000029
Denis Vlasenko55f79122008-07-16 11:00:16 +000030typedef struct priv_dumper_t {
31 dumper_t pub;
32
33 char **argv;
34 FU *endfu;
35 off_t savaddress; /* saved address/offset in stream */
36 off_t eaddress; /* end address */
Denis Vlasenko55f79122008-07-16 11:00:16 +000037 int blocksize;
38 smallint exitval; /* final exit value */
39
40 /* former statics */
41 smallint next__done;
42 smallint get__ateof; // = 1;
43 unsigned char *get__curp;
44 unsigned char *get__savp;
45} priv_dumper_t;
46
Denys Vlasenkodac5b832020-10-20 18:54:36 +020047static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
48
49static const char size_conv_str[] ALIGN1 =
50"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
51
52static const char int_convs[] ALIGN1 = "diouxX";
53
Denis Vlasenko55f79122008-07-16 11:00:16 +000054dumper_t* FAST_FUNC alloc_dumper(void)
55{
56 priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
57 dumper->pub.dump_length = -1;
58 dumper->pub.dump_vflag = FIRST;
59 dumper->get__ateof = 1;
60 return &dumper->pub;
61}
62
Denis Vlasenko55f79122008-07-16 11:00:16 +000063static NOINLINE int bb_dump_size(FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000064{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000065 FU *fu;
66 int bcnt, cur_size;
67 char *fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000069 int prec;
70
Denys Vlasenko5f7904b2017-07-14 16:03:43 +020071 /* figure out the data block size needed for each format unit */
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +000073 if (fu->bcnt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 cur_size += fu->bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000075 continue;
76 }
77 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
78 if (*fmt != '%')
79 continue;
80 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +000081 * skip any special chars -- save precision in
Glenn L McGrath60281112001-11-02 11:39:46 +000082 * case it's a %s format.
83 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010084 while (strchr(dot_flags_width_chars + 1, *++fmt))
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +010085 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000086 if (*fmt == '.' && isdigit(*++fmt)) {
87 prec = atoi(fmt);
Denis Vlasenko55f79122008-07-16 11:00:16 +000088 while (isdigit(*++fmt))
89 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000090 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000091 p = strchr(size_conv_str + 12, *fmt);
92 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 if (*fmt == 's') {
94 bcnt += prec;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010095 }
96 if (*fmt == '_') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 ++fmt;
98 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
99 bcnt += 1;
100 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000101 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 } else {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100103 bcnt += p[-12];
Glenn L McGrath60281112001-11-02 11:39:46 +0000104 }
105 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +0000107 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000108 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +0000109}
110
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200111static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +0000112{
Rob Landleyea224be2006-06-18 20:20:07 +0000113 FU *fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000114
115 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100116 PR *pr;
117 char *p1, *p2, *p3;
118 char *fmtp;
119 int nconv = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000120 /*
121 * break each format unit into print units; each
122 * conversion character gets its own.
123 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100124 for (fmtp = fu->fmt; *fmtp; ) {
125 unsigned len;
126 const char *prec;
127 const char *byte_count_str;
128
129 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
130 pr = xzalloc(sizeof(*pr));
Glenn L McGrath60281112001-11-02 11:39:46 +0000131 if (!fu->nextpr)
132 fu->nextpr = pr;
Glenn L McGrath60281112001-11-02 11:39:46 +0000133
Denis Vlasenko55f79122008-07-16 11:00:16 +0000134 /* skip preceding text and up to the next % sign */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100135 p1 = strchr(fmtp, '%');
136 if (!p1) { /* only text in the string */
Glenn L McGrath60281112001-11-02 11:39:46 +0000137 pr->fmt = fmtp;
138 pr->flags = F_TEXT;
139 break;
140 }
141
142 /*
143 * get precision for %s -- if have a byte count, don't
144 * need it.
145 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100146 prec = NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000147 if (fu->bcnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000148 /* skip to conversion character */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100149 while (strchr(dot_flags_width_chars, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000150 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000151 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000152 /* skip any special chars, field width */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100153 while (strchr(dot_flags_width_chars + 1, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000154 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000155 if (*p1 == '.' && isdigit(*++p1)) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100156 prec = p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000157 while (isdigit(*++p1))
158 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100159 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000160 }
161
Denis Vlasenko55f79122008-07-16 11:00:16 +0000162 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000163
164 /*
165 * figure out the byte count for each conversion;
166 * rewrite the format as necessary, set up blank-
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200167 * padding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000168 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000170 pr->flags = F_CHAR;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000171 DO_BYTE_COUNT_1:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 byte_count_str = "\001";
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000173 DO_BYTE_COUNT:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 if (fu->bcnt) {
Denys Vlasenko0f436472017-01-25 01:58:00 +0100175 for (;;) {
176 if (fu->bcnt == *byte_count_str)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 break;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100178 if (*++byte_count_str == 0)
179 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
180 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000181 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 /* Unlike the original, output the remainder of the format string. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 pr->bcnt = *byte_count_str;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100184 } else
185 if (*p1 == 'l') { /* %ld etc */
186 const char *e;
187
Glenn L McGrath60281112001-11-02 11:39:46 +0000188 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 ++p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000190 DO_INT_CONV:
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100191 e = strchr(int_convs, *p1); /* "diouxX"? */
192 if (!e)
193 goto DO_BAD_CONV_CHAR;
194 pr->flags = F_INT;
195 if (e > int_convs + 1) /* not d or i? */
196 pr->flags = F_UINT;
197 byte_count_str = "\004\002\001";
198 goto DO_BYTE_COUNT;
199 } else
200 if (strchr(int_convs, *p1)) { /* %d etc */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000201 goto DO_INT_CONV;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100202 } else
203 if (strchr("eEfgG", *p1)) { /* floating point */
Glenn L McGrath60281112001-11-02 11:39:46 +0000204 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000205 byte_count_str = "\010\004";
206 goto DO_BYTE_COUNT;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100207 } else
208 if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000209 pr->flags = F_STR;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100210 pr->bcnt = fu->bcnt;
211 if (fu->bcnt == 0) {
212 if (!prec)
Denys Vlasenkoa0f80762021-06-17 23:23:26 +0200213 bb_simple_error_msg_and_die("%s needs precision or byte count");
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100214 pr->bcnt = atoi(prec);
Glenn L McGrath60281112001-11-02 11:39:46 +0000215 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100216 } else
217 if (*p1 == '_') {
218 p2++; /* move past a in "%_a" */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000219 switch (p1[1]) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100220 case 'A': /* %_A[dox]: print address and the end */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000221 dumper->endfu = fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000222 fu->flags |= F_IGNORE;
223 /* FALLTHROUGH */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100224 case 'a': /* %_a[dox]: current address */
Glenn L McGrath60281112001-11-02 11:39:46 +0000225 pr->flags = F_ADDRESS;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100226 p2++; /* move past x in "%_ax" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
228 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000229 }
Denys Vlasenko4d161612021-06-17 23:53:30 +0200230 *p1++ = 'l';
231 *p1++ = 'l';
Glenn L McGrath60281112001-11-02 11:39:46 +0000232 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100233 case 'c': /* %_c: chars, \ooo, \n \r \t etc */
Glenn L McGrath60281112001-11-02 11:39:46 +0000234 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000235 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100237 case 'p': /* %_p: chars, dots for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000238 pr->flags = F_P;
239 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100241 case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000242 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000243 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000245 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000246 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000247 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 } else {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000249 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000250 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000251 }
252
253 /*
254 * copy to PR format string, set conversion character
255 * pointer, update original.
256 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100257 len = (p1 - fmtp) + 1;
258 pr->fmt = xstrndup(fmtp, len);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000259 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000260 * Skip subsequent text and up to the next % sign and tack the
261 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000262 * we lose the " is a HEX number" part of fmt.
263 */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000264 for (p3 = p2; *p3 && *p3 != '%'; p3++)
265 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100266 if ((p3 - p2) != 0) {
267 char *d;
268 pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
269 d += len;
270 do {
271 *d++ = *p2++;
272 } while (p2 != p3);
273 *d = '\0';
274 /* now p2 = p3 */
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000275 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100276 pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
Glenn L McGrath60281112001-11-02 11:39:46 +0000277 fmtp = p2;
278
279 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000280 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
James Byrne69374872019-07-02 11:35:03 +0200281 bb_simple_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000282 }
283 }
284 /*
285 * if format unit byte count not specified, figure it out
286 * so can adjust rep count later.
287 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100288 if (fu->bcnt == 0)
Glenn L McGrath60281112001-11-02 11:39:46 +0000289 for (pr = fu->nextpr; pr; pr = pr->nextpr)
290 fu->bcnt += pr->bcnt;
291 }
292 /*
293 * if the format string interprets any data at all, and it's
Denis Vlasenko55f79122008-07-16 11:00:16 +0000294 * not the same as the blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000295 * interprets any data at all, and has no iteration count,
296 * repeat it as necessary.
297 *
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100298 * if rep count is greater than 1, no trailing whitespace
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200299 * gets output from the last iteration of the format unit:
300 * 2/1 "%02x " prints "XX XX", not "XX XX "
301 * 2/1 "%02x\n" prints "XX\nXX", not "XX\nXX\n"
Glenn L McGrath60281112001-11-02 11:39:46 +0000302 */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000303 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100304 if (!fu->nextfu
305 && fs->bcnt < dumper->blocksize
306 && !(fu->flags & F_SETREP)
307 && fu->bcnt
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000308 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000309 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000310 }
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100311 if (fu->reps > 1 && fu->nextpr) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100312 PR *pr;
313 char *p1, *p2;
314
Glenn L McGrath60281112001-11-02 11:39:46 +0000315 for (pr = fu->nextpr;; pr = pr->nextpr)
316 if (!pr->nextpr)
317 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100318 p2 = NULL;
319 for (p1 = pr->fmt; *p1; ++p1)
Glenn L McGrath60281112001-11-02 11:39:46 +0000320 p2 = isspace(*p1) ? p1 : NULL;
321 if (p2)
322 pr->nospace = p2;
323 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000324 }
325}
326
Denys Vlasenko63214a62017-07-14 16:18:16 +0200327static void do_skip(priv_dumper_t *dumper, const char *fname)
Glenn L McGrath60281112001-11-02 11:39:46 +0000328{
329 struct stat sbuf;
330
Denys Vlasenko63214a62017-07-14 16:18:16 +0200331 xfstat(STDIN_FILENO, &sbuf, fname);
332 if (S_ISREG(sbuf.st_mode)
333 && dumper->pub.dump_skip >= sbuf.st_size
334 ) {
335 /* If st_size is valid and pub.dump_skip >= st_size */
336 dumper->pub.dump_skip -= sbuf.st_size;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200337 dumper->pub.address += sbuf.st_size;
Denys Vlasenko63214a62017-07-14 16:18:16 +0200338 return;
Glenn L McGrath60281112001-11-02 11:39:46 +0000339 }
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100340 if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000341 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000342 }
Denys Vlasenko2c436672021-06-18 00:59:17 +0200343 dumper->pub.address += dumper->pub.dump_skip;
344 dumper->savaddress = dumper->pub.address;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000345 dumper->pub.dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000346}
347
Denis Vlasenko55f79122008-07-16 11:00:16 +0000348static NOINLINE int next(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000349{
Glenn L McGrath60281112001-11-02 11:39:46 +0000350 for (;;) {
Denys Vlasenko63214a62017-07-14 16:18:16 +0200351 const char *fname = *dumper->argv;
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200352
353 if (fname) {
354 dumper->argv++;
Denys Vlasenko90678f02017-07-14 16:29:30 +0200355 if (NOT_LONE_DASH(fname)) {
356 if (!freopen(fname, "r", stdin)) {
357 bb_simple_perror_msg(fname);
358 dumper->exitval = 1;
Denys Vlasenko25fe2d52021-06-18 00:32:29 +0200359 dumper->next__done = 1;
Denys Vlasenko90678f02017-07-14 16:29:30 +0200360 continue;
361 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000362 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000363 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000364 if (dumper->next__done)
Denys Vlasenko6aca76d2010-02-06 13:53:21 +0100365 return 0; /* no next file */
Glenn L McGrath60281112001-11-02 11:39:46 +0000366 }
Denys Vlasenko63214a62017-07-14 16:18:16 +0200367 dumper->next__done = 1;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000368 if (dumper->pub.dump_skip)
Denys Vlasenko63214a62017-07-14 16:18:16 +0200369 do_skip(dumper, fname ? fname : "stdin");
370 if (dumper->pub.dump_skip == 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000371 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000372 }
373 /* NOTREACHED */
374}
375
Denis Vlasenko55f79122008-07-16 11:00:16 +0000376static unsigned char *get(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000377{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000378 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000379 int need, nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000380 int blocksize = dumper->blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000381
Denis Vlasenko55f79122008-07-16 11:00:16 +0000382 if (!dumper->get__curp) {
Denys Vlasenko2c436672021-06-18 00:59:17 +0200383 dumper->pub.address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
Denis Vlasenko55f79122008-07-16 11:00:16 +0000384 dumper->get__curp = xmalloc(blocksize);
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000385 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
Glenn L McGrath60281112001-11-02 11:39:46 +0000386 } else {
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000387 unsigned char *tmp = dumper->get__curp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000388 dumper->get__curp = dumper->get__savp;
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000389 dumper->get__savp = tmp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000390 dumper->savaddress += blocksize;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200391 dumper->pub.address = dumper->savaddress;
Glenn L McGrath60281112001-11-02 11:39:46 +0000392 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000393 need = blocksize;
394 nread = 0;
395 while (1) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000396 /*
397 * if read the right number of bytes, or at EOF for one file,
398 * and no other files are available, zero-pad the rest of the
399 * block and set the end flag.
400 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000401 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
402 if (need == blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000403 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000404 }
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200405 if (dumper->pub.dump_vflag != ALL /* not "show all"? */
406 && dumper->pub.dump_vflag != FIRST /* not first line? */
407 && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */
408 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000409 if (dumper->pub.dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000410 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000411 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000412 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000413 memset(dumper->get__curp + nread, 0, need);
Denys Vlasenko2c436672021-06-18 00:59:17 +0200414 dumper->eaddress = dumper->pub.address + nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000415 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000416 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000417 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
418 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200419 if (n == 0) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000420 if (ferror(stdin)) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000421 bb_simple_perror_msg(dumper->argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000422 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000423 dumper->get__ateof = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000424 continue;
425 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000426 dumper->get__ateof = 0;
427 if (dumper->pub.dump_length != -1) {
428 dumper->pub.dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000429 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000430 need -= n;
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200431 if (need == 0) {
432 if (dumper->pub.dump_vflag == ALL /* "show all"? */
433 || dumper->pub.dump_vflag == FIRST /* first line? */
434 || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000435 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000436 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
437 dumper->pub.dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000438 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000439 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000440 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000441 if (dumper->pub.dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000442 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000443 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000444 dumper->pub.dump_vflag = DUP;
445 dumper->savaddress += blocksize;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200446 dumper->pub.address = dumper->savaddress;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000447 need = blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000448 nread = 0;
449 } else {
450 nread += n;
451 }
452 }
453}
454
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000455static void bpad(PR *pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000456{
Rob Landleya6e60372006-06-28 14:36:50 +0000457 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000458
459 /*
460 * remove all conversion flags; '-' is the only one valid
461 * with %s, and it's not useful here.
462 */
463 pr->flags = F_BPAD;
464 *pr->cchar = 's';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000465 for (p1 = pr->fmt; *p1 != '%'; ++p1)
466 continue;
Rob Landleya6e60372006-06-28 14:36:50 +0000467 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000468 if (pr->nospace)
469 pr->nospace--;
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200470 while ((*p2++ = *p1++) != '\0')
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000471 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000472}
473
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000474static const char conv_str[] ALIGN1 =
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200475 "\0" "\\""0""\0"
476 "\007""\\""a""\0" /* \a */
477 "\b" "\\""b""\0"
478 "\f" "\\""f""\0"
479 "\n" "\\""n""\0"
480 "\r" "\\""r""\0"
481 "\t" "\\""t""\0"
482 "\v" "\\""v""\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000483 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000484
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000485static void conv_c(PR *pr, unsigned char *p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000486{
487 const char *str = conv_str;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000488
489 do {
490 if (*p == *str) {
491 ++str;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200492 goto strpr; /* map e.g. '\n' to "\\n" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000493 }
494 str += 4;
495 } while (*str);
496
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100497 if (isprint_asciionly(*p)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000498 *pr->cchar = 'c';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000499 printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000500 } else {
Denys Vlasenko2ab994f2018-04-06 18:26:33 +0200501 char buf[4];
502 /* gcc-8.0.1 needs lots of casts to shut up */
503 sprintf(buf, "%03o", (unsigned)(uint8_t)*p);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000504 str = buf;
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100505 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000506 *pr->cchar = 's';
507 printf(pr->fmt, str);
508 }
509}
510
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000511static void conv_u(PR *pr, unsigned char *p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000512{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000513 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000514 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
515 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
516 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
517 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000518
519 /* od used nl, not lf */
520 if (*p <= 0x1f) {
521 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000522 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000523 } else if (*p == 0x7f) {
524 *pr->cchar = 's';
525 printf(pr->fmt, "del");
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100526 } else if (*p < 0x7f) { /* isprint() */
Glenn L McGrath60281112001-11-02 11:39:46 +0000527 *pr->cchar = 'c';
528 printf(pr->fmt, *p);
529 } else {
530 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000531 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000532 }
533}
534
Denys Vlasenko53b2fdc2021-10-10 13:50:53 +0200535static NOINLINE void display(priv_dumper_t* dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000536{
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200537 unsigned char *bp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000538 unsigned char savech = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000539
Denis Vlasenko55f79122008-07-16 11:00:16 +0000540 while ((bp = get(dumper)) != NULL) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200541 FS *fs;
542 unsigned char *savebp;
543 off_t saveaddress;
544
Denis Vlasenko55f79122008-07-16 11:00:16 +0000545 fs = dumper->pub.fshead;
546 savebp = bp;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200547 saveaddress = dumper->pub.address;
548 for (; fs; fs = fs->nextfs, bp = savebp, dumper->pub.address = saveaddress) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200549 FU *fu;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000550 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200551 int cnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000552 if (fu->flags & F_IGNORE) {
553 break;
554 }
555 for (cnt = fu->reps; cnt; --cnt) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200556 PR *pr;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200557 for (pr = fu->nextpr; pr; dumper->pub.address += pr->bcnt,
Denis Vlasenko55f79122008-07-16 11:00:16 +0000558 bp += pr->bcnt, pr = pr->nextpr) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200559 if (dumper->eaddress
Denys Vlasenko2c436672021-06-18 00:59:17 +0200560 && dumper->pub.address >= dumper->eaddress
Denis Vlasenko55f79122008-07-16 11:00:16 +0000561 ) {
Denys Vlasenko4d161612021-06-17 23:53:30 +0200562 if (dumper->pub.xxd_eofstring) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200563 /* xxd support: requested to not pad incomplete blocks */
Denys Vlasenko4d161612021-06-17 23:53:30 +0200564 fputs_stdout(dumper->pub.xxd_eofstring);
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200565 return;
566 }
567 if (!(pr->flags & (F_TEXT | F_BPAD)))
568 bpad(pr);
Glenn L McGrath60281112001-11-02 11:39:46 +0000569 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000570 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000571 savech = *pr->nospace;
572 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000573 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000574 switch (pr->flags) {
575 case F_ADDRESS:
Denys Vlasenko2c436672021-06-18 00:59:17 +0200576 printf(pr->fmt, (unsigned long long) dumper->pub.address + dumper->pub.xxd_displayoff);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000577 break;
578 case F_BPAD:
579 printf(pr->fmt, "");
580 break;
581 case F_C:
582 conv_c(pr, bp);
583 break;
584 case F_CHAR:
585 printf(pr->fmt, *bp);
586 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000587 case F_DBL: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000588 double dval;
589 float fval;
590
591 switch (pr->bcnt) {
592 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000593 memcpy(&fval, bp, sizeof(fval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000594 printf(pr->fmt, fval);
595 break;
596 case 8:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000597 memcpy(&dval, bp, sizeof(dval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000598 printf(pr->fmt, dval);
599 break;
600 }
601 break;
602 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000603 case F_INT: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000604 int ival;
605 short sval;
606
607 switch (pr->bcnt) {
608 case 1:
609 printf(pr->fmt, (int) *bp);
610 break;
611 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000612 memcpy(&sval, bp, sizeof(sval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000613 printf(pr->fmt, (int) sval);
614 break;
615 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000616 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000617 printf(pr->fmt, ival);
618 break;
619 }
620 break;
621 }
622 case F_P:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100623 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000624 break;
625 case F_STR:
626 printf(pr->fmt, (char *) bp);
627 break;
628 case F_TEXT:
629 printf(pr->fmt);
630 break;
631 case F_U:
632 conv_u(pr, bp);
633 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000634 case F_UINT: {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000635 unsigned ival;
Eric Andersen0f56de62004-01-30 22:52:27 +0000636 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000637
638 switch (pr->bcnt) {
639 case 1:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000640 printf(pr->fmt, (unsigned) *bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000641 break;
642 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000643 memcpy(&sval, bp, sizeof(sval));
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000644 printf(pr->fmt, (unsigned) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000645 break;
646 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000647 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000648 printf(pr->fmt, ival);
649 break;
650 }
651 break;
652 }
653 }
654 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000655 *pr->nospace = savech;
656 }
657 }
658 }
659 }
660 }
661 }
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200662
Denis Vlasenko55f79122008-07-16 11:00:16 +0000663 if (dumper->endfu) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200664 PR *pr;
Glenn L McGrath60281112001-11-02 11:39:46 +0000665 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +0000666 * if eaddress not set, error or file size was multiple
667 * of blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000668 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000669 if (!dumper->eaddress) {
Denys Vlasenko2c436672021-06-18 00:59:17 +0200670 if (!dumper->pub.address) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000671 return;
672 }
Denys Vlasenko2c436672021-06-18 00:59:17 +0200673 dumper->eaddress = dumper->pub.address;
Glenn L McGrath60281112001-11-02 11:39:46 +0000674 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000675 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000676 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000677 case F_ADDRESS:
Denys Vlasenko4d161612021-06-17 23:53:30 +0200678 printf(pr->fmt, (unsigned long long) dumper->eaddress + dumper->pub.xxd_displayoff);
Glenn L McGrath60281112001-11-02 11:39:46 +0000679 break;
680 case F_TEXT:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000681 printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000682 break;
683 }
684 }
685 }
686}
687
Denis Vlasenko55f79122008-07-16 11:00:16 +0000688#define dumper ((priv_dumper_t*)pub_dumper)
689int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000690{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000691 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000692 int blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000693
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200694 /* figure out the data block size */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000695 blocksize = 0;
696 tfs = dumper->pub.fshead;
697 while (tfs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000698 tfs->bcnt = bb_dump_size(tfs);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000699 if (blocksize < tfs->bcnt) {
700 blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000701 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000702 tfs = tfs->nextfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000703 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000704 dumper->blocksize = blocksize;
705
Glenn L McGrath60281112001-11-02 11:39:46 +0000706 /* rewrite the rules, do syntax checking */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000707 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
708 rewrite(dumper, tfs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000709 }
710
Denis Vlasenko55f79122008-07-16 11:00:16 +0000711 dumper->argv = argv;
712 display(dumper);
Glenn L McGrath60281112001-11-02 11:39:46 +0000713
Denis Vlasenko55f79122008-07-16 11:00:16 +0000714 return dumper->exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000715}
716
Denis Vlasenko55f79122008-07-16 11:00:16 +0000717void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000718{
Rob Landleyea224be2006-06-18 20:20:07 +0000719 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +0000720 FS *tfs;
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200721 FU **nextfupp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000722
723 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000724 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000725 if (!dumper->pub.fshead) {
726 dumper->pub.fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000727 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000728 FS *fslast = dumper->pub.fshead;
729 while (fslast->nextfs)
730 fslast = fslast->nextfs;
731 fslast->nextfs = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000732 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000733 nextfupp = &tfs->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000734
735 /* take the format string and break it up into format units */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000736 p = fmt;
737 for (;;) {
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200738 FU *tfu;
739 const char *savep;
740
Rob Landleyea224be2006-06-18 20:20:07 +0000741 p = skip_whitespace(p);
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100742 if (*p == '\0') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000743 break;
744 }
745
746 /* allocate a new format unit and link it in */
747 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000748 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000749 tfu = xzalloc(sizeof(FU));
Denis Vlasenko55f79122008-07-16 11:00:16 +0000750 *nextfupp = tfu;
751 nextfupp = &tfu->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000752 tfu->reps = 1;
753
754 /* if leading digit, repetition count */
755 if (isdigit(*p)) {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000756 for (savep = p; isdigit(*p); ++p)
757 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000758 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000759 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000760 }
761 /* may overwrite either white space or slash */
762 tfu->reps = atoi(savep);
763 tfu->flags = F_SETREP;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000764 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000765 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000766 }
767
Denis Vlasenko55f79122008-07-16 11:00:16 +0000768 /* skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000769 if (*p == '/') {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100770 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000771 }
772
773 /* byte count */
774 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000775// TODO: use bb_strtou
776 savep = p;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000777 while (isdigit(*++p))
778 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000779 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000780 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000781 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100782// Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
783// Other than this, formats can be pretty much jammed together:
784// "%07_ax:"8/2 "%04x|""\n"
785// but this space is required. The check *can* be removed, but
786// keeping it to stay compat with util-linux hexdump.
Glenn L McGrath60281112001-11-02 11:39:46 +0000787 tfu->bcnt = atoi(savep);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000788 /* skip trailing white space */
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100789 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000790 }
791
792 /* format */
793 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000794 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000795 }
796 for (savep = ++p; *p != '"';) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100797 if (*p++ == '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000798 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000799 }
800 }
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000801 tfu->fmt = xstrndup(savep, p - savep);
Glenn L McGrath60281112001-11-02 11:39:46 +0000802
803 /* alphabetic escape sequences have to be done in place */
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200804 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
805 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
806 /* trailing backslash, if any, is preserved */
807#if 0
808 char *p1;
809 char *p2;
810 p1 = tfu->fmt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000811 for (p2 = p1;; ++p1, ++p2) {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200812 *p2 = *p1;
813 if (*p1 == '\0')
Glenn L McGrath60281112001-11-02 11:39:46 +0000814 break;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200815
Glenn L McGrath60281112001-11-02 11:39:46 +0000816 if (*p1 == '\\') {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200817 const char *cs;
818
819 p1++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000820 *p2 = *p1;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200821 if (*p1 == '\0') {
822 /* "...\" trailing backslash. Eaten. */
823 break;
824 }
825 cs = conv_str + 4; /* skip NUL element */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000826 do {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200827 /* map e.g. "\n" -> '\n' */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000828 if (*p1 == cs[2]) {
829 *p2 = cs[0];
830 break;
831 }
832 cs += 4;
833 } while (*cs);
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200834 /* unknown mappings remove bkslash: "\z" -> 'z' */
Glenn L McGrath60281112001-11-02 11:39:46 +0000835 }
836 }
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200837#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000838
839 p++;
840 }
841}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000842
Glenn L McGrath60281112001-11-02 11:39:46 +0000843/*
844 * Copyright (c) 1989 The Regents of the University of California.
845 * All rights reserved.
846 *
847 * Redistribution and use in source and binary forms, with or without
848 * modification, are permitted provided that the following conditions
849 * are met:
850 * 1. Redistributions of source code must retain the above copyright
851 * notice, this list of conditions and the following disclaimer.
852 * 2. Redistributions in binary form must reproduce the above copyright
853 * notice, this list of conditions and the following disclaimer in the
854 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000855 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000856 * may be used to endorse or promote products derived from this software
857 * without specific prior written permission.
858 *
Denys Vlasenko95f79532017-08-02 14:26:33 +0200859 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
Glenn L McGrath60281112001-11-02 11:39:46 +0000860 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
861 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
862 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
863 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
864 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
865 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
866 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
867 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
868 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
869 * SUCH DAMAGE.
870 */