blob: e23b7129409294d5da3d9a3ef535d2d1860d5fa5 [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 */
13
Rob Landleyea224be2006-06-18 20:20:07 +000014#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000016
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010017static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
Glenn L McGrath60281112001-11-02 11:39:46 +000018
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000019static const char size_conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +000020"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
21
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010022static const char int_convs[] ALIGN1 = "diouxX";
Manuel Novoa III cad53642003-03-19 09:13:01 +000023
Denis Vlasenko55f79122008-07-16 11:00:16 +000024
25typedef struct priv_dumper_t {
26 dumper_t pub;
27
28 char **argv;
29 FU *endfu;
30 off_t savaddress; /* saved address/offset in stream */
31 off_t eaddress; /* end address */
32 off_t address; /* address/offset in stream */
33 int blocksize;
34 smallint exitval; /* final exit value */
35
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
43dumper_t* FAST_FUNC alloc_dumper(void)
44{
45 priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
46 dumper->pub.dump_length = -1;
47 dumper->pub.dump_vflag = FIRST;
48 dumper->get__ateof = 1;
49 return &dumper->pub;
50}
51
52
53static NOINLINE int bb_dump_size(FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000054{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000055 FU *fu;
56 int bcnt, cur_size;
57 char *fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000059 int prec;
60
Denys Vlasenko5f7904b2017-07-14 16:03:43 +020061 /* figure out the data block size needed for each format unit */
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +000063 if (fu->bcnt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 cur_size += fu->bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000065 continue;
66 }
67 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
68 if (*fmt != '%')
69 continue;
70 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +000071 * skip any special chars -- save precision in
Glenn L McGrath60281112001-11-02 11:39:46 +000072 * case it's a %s format.
73 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010074 while (strchr(dot_flags_width_chars + 1, *++fmt))
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +010075 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000076 if (*fmt == '.' && isdigit(*++fmt)) {
77 prec = atoi(fmt);
Denis Vlasenko55f79122008-07-16 11:00:16 +000078 while (isdigit(*++fmt))
79 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000080 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000081 p = strchr(size_conv_str + 12, *fmt);
82 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 if (*fmt == 's') {
84 bcnt += prec;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010085 }
86 if (*fmt == '_') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 ++fmt;
88 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
89 bcnt += 1;
90 }
Glenn L McGrath60281112001-11-02 11:39:46 +000091 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 } else {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010093 bcnt += p[-12];
Glenn L McGrath60281112001-11-02 11:39:46 +000094 }
95 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000097 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000098 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000099}
100
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200101static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +0000102{
Rob Landleyea224be2006-06-18 20:20:07 +0000103 FU *fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000104
105 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100106 PR *pr;
107 char *p1, *p2, *p3;
108 char *fmtp;
109 int nconv = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000110 /*
111 * break each format unit into print units; each
112 * conversion character gets its own.
113 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100114 for (fmtp = fu->fmt; *fmtp; ) {
115 unsigned len;
116 const char *prec;
117 const char *byte_count_str;
118
119 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
120 pr = xzalloc(sizeof(*pr));
Glenn L McGrath60281112001-11-02 11:39:46 +0000121 if (!fu->nextpr)
122 fu->nextpr = pr;
Glenn L McGrath60281112001-11-02 11:39:46 +0000123
Denis Vlasenko55f79122008-07-16 11:00:16 +0000124 /* skip preceding text and up to the next % sign */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100125 p1 = strchr(fmtp, '%');
126 if (!p1) { /* only text in the string */
Glenn L McGrath60281112001-11-02 11:39:46 +0000127 pr->fmt = fmtp;
128 pr->flags = F_TEXT;
129 break;
130 }
131
132 /*
133 * get precision for %s -- if have a byte count, don't
134 * need it.
135 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100136 prec = NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000137 if (fu->bcnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000138 /* skip to conversion character */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100139 while (strchr(dot_flags_width_chars, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000140 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000141 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000142 /* skip any special chars, field width */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100143 while (strchr(dot_flags_width_chars + 1, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000144 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000145 if (*p1 == '.' && isdigit(*++p1)) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100146 prec = p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000147 while (isdigit(*++p1))
148 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100149 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000150 }
151
Denis Vlasenko55f79122008-07-16 11:00:16 +0000152 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000153
154 /*
155 * figure out the byte count for each conversion;
156 * rewrite the format as necessary, set up blank-
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200157 * padding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000158 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000160 pr->flags = F_CHAR;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000161 DO_BYTE_COUNT_1:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 byte_count_str = "\001";
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000163 DO_BYTE_COUNT:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 if (fu->bcnt) {
Denys Vlasenko0f436472017-01-25 01:58:00 +0100165 for (;;) {
166 if (fu->bcnt == *byte_count_str)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 break;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100168 if (*++byte_count_str == 0)
169 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
170 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000171 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 /* Unlike the original, output the remainder of the format string. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 pr->bcnt = *byte_count_str;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100174 } else
175 if (*p1 == 'l') { /* %ld etc */
176 const char *e;
177
Glenn L McGrath60281112001-11-02 11:39:46 +0000178 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 ++p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000180 DO_INT_CONV:
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100181 e = strchr(int_convs, *p1); /* "diouxX"? */
182 if (!e)
183 goto DO_BAD_CONV_CHAR;
184 pr->flags = F_INT;
185 if (e > int_convs + 1) /* not d or i? */
186 pr->flags = F_UINT;
187 byte_count_str = "\004\002\001";
188 goto DO_BYTE_COUNT;
189 } else
190 if (strchr(int_convs, *p1)) { /* %d etc */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 goto DO_INT_CONV;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100192 } else
193 if (strchr("eEfgG", *p1)) { /* floating point */
Glenn L McGrath60281112001-11-02 11:39:46 +0000194 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 byte_count_str = "\010\004";
196 goto DO_BYTE_COUNT;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100197 } else
198 if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000199 pr->flags = F_STR;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100200 pr->bcnt = fu->bcnt;
201 if (fu->bcnt == 0) {
202 if (!prec)
203 bb_error_msg_and_die("%%s needs precision or byte count");
204 pr->bcnt = atoi(prec);
Glenn L McGrath60281112001-11-02 11:39:46 +0000205 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100206 } else
207 if (*p1 == '_') {
208 p2++; /* move past a in "%_a" */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000209 switch (p1[1]) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100210 case 'A': /* %_A[dox]: print address and the end */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000211 dumper->endfu = fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000212 fu->flags |= F_IGNORE;
213 /* FALLTHROUGH */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100214 case 'a': /* %_a[dox]: current address */
Glenn L McGrath60281112001-11-02 11:39:46 +0000215 pr->flags = F_ADDRESS;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100216 p2++; /* move past x in "%_ax" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
218 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000219 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000221 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100222 case 'c': /* %_c: chars, \ooo, \n \r \t etc */
Glenn L McGrath60281112001-11-02 11:39:46 +0000223 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000224 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100226 case 'p': /* %_p: chars, dots for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000227 pr->flags = F_P;
228 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100230 case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000231 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000232 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000234 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000236 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 } else {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000238 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000239 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000240 }
241
242 /*
243 * copy to PR format string, set conversion character
244 * pointer, update original.
245 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100246 len = (p1 - fmtp) + 1;
247 pr->fmt = xstrndup(fmtp, len);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000248 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000249 * Skip subsequent text and up to the next % sign and tack the
250 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000251 * we lose the " is a HEX number" part of fmt.
252 */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000253 for (p3 = p2; *p3 && *p3 != '%'; p3++)
254 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100255 if ((p3 - p2) != 0) {
256 char *d;
257 pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
258 d += len;
259 do {
260 *d++ = *p2++;
261 } while (p2 != p3);
262 *d = '\0';
263 /* now p2 = p3 */
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000264 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100265 pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
Glenn L McGrath60281112001-11-02 11:39:46 +0000266 fmtp = p2;
267
268 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000269 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000270 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000271 }
272 }
273 /*
274 * if format unit byte count not specified, figure it out
275 * so can adjust rep count later.
276 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100277 if (fu->bcnt == 0)
Glenn L McGrath60281112001-11-02 11:39:46 +0000278 for (pr = fu->nextpr; pr; pr = pr->nextpr)
279 fu->bcnt += pr->bcnt;
280 }
281 /*
282 * if the format string interprets any data at all, and it's
Denis Vlasenko55f79122008-07-16 11:00:16 +0000283 * not the same as the blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000284 * interprets any data at all, and has no iteration count,
285 * repeat it as necessary.
286 *
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100287 * if rep count is greater than 1, no trailing whitespace
Glenn L McGrath60281112001-11-02 11:39:46 +0000288 * gets output from the last iteration of the format unit.
289 */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000290 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100291 if (!fu->nextfu
292 && fs->bcnt < dumper->blocksize
293 && !(fu->flags & F_SETREP)
294 && fu->bcnt
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000295 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000296 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000297 }
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100298 if (fu->reps > 1 && fu->nextpr) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100299 PR *pr;
300 char *p1, *p2;
301
Glenn L McGrath60281112001-11-02 11:39:46 +0000302 for (pr = fu->nextpr;; pr = pr->nextpr)
303 if (!pr->nextpr)
304 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100305 p2 = NULL;
306 for (p1 = pr->fmt; *p1; ++p1)
Glenn L McGrath60281112001-11-02 11:39:46 +0000307 p2 = isspace(*p1) ? p1 : NULL;
308 if (p2)
309 pr->nospace = p2;
310 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000311 }
312}
313
Denys Vlasenko63214a62017-07-14 16:18:16 +0200314static void do_skip(priv_dumper_t *dumper, const char *fname)
Glenn L McGrath60281112001-11-02 11:39:46 +0000315{
316 struct stat sbuf;
317
Denys Vlasenko63214a62017-07-14 16:18:16 +0200318 xfstat(STDIN_FILENO, &sbuf, fname);
319 if (S_ISREG(sbuf.st_mode)
320 && dumper->pub.dump_skip >= sbuf.st_size
321 ) {
322 /* If st_size is valid and pub.dump_skip >= st_size */
323 dumper->pub.dump_skip -= sbuf.st_size;
324 dumper->address += sbuf.st_size;
325 return;
Glenn L McGrath60281112001-11-02 11:39:46 +0000326 }
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100327 if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000328 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000329 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000330 dumper->address += dumper->pub.dump_skip;
331 dumper->savaddress = dumper->address;
332 dumper->pub.dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000333}
334
Denis Vlasenko55f79122008-07-16 11:00:16 +0000335static NOINLINE int next(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000336{
Glenn L McGrath60281112001-11-02 11:39:46 +0000337 for (;;) {
Denys Vlasenko63214a62017-07-14 16:18:16 +0200338 const char *fname = *dumper->argv;
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200339
340 if (fname) {
341 dumper->argv++;
Denys Vlasenko90678f02017-07-14 16:29:30 +0200342 if (NOT_LONE_DASH(fname)) {
343 if (!freopen(fname, "r", stdin)) {
344 bb_simple_perror_msg(fname);
345 dumper->exitval = 1;
346 continue;
347 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000348 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000349 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000350 if (dumper->next__done)
Denys Vlasenko6aca76d2010-02-06 13:53:21 +0100351 return 0; /* no next file */
Glenn L McGrath60281112001-11-02 11:39:46 +0000352 }
Denys Vlasenko63214a62017-07-14 16:18:16 +0200353 dumper->next__done = 1;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000354 if (dumper->pub.dump_skip)
Denys Vlasenko63214a62017-07-14 16:18:16 +0200355 do_skip(dumper, fname ? fname : "stdin");
356 if (dumper->pub.dump_skip == 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000357 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000358 }
359 /* NOTREACHED */
360}
361
Denis Vlasenko55f79122008-07-16 11:00:16 +0000362static unsigned char *get(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000363{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000364 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000365 int need, nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000366 int blocksize = dumper->blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000367
Denis Vlasenko55f79122008-07-16 11:00:16 +0000368 if (!dumper->get__curp) {
369 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
370 dumper->get__curp = xmalloc(blocksize);
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000371 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
Glenn L McGrath60281112001-11-02 11:39:46 +0000372 } else {
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000373 unsigned char *tmp = dumper->get__curp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000374 dumper->get__curp = dumper->get__savp;
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000375 dumper->get__savp = tmp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000376 dumper->savaddress += blocksize;
377 dumper->address = dumper->savaddress;
Glenn L McGrath60281112001-11-02 11:39:46 +0000378 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000379 need = blocksize;
380 nread = 0;
381 while (1) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000382 /*
383 * if read the right number of bytes, or at EOF for one file,
384 * and no other files are available, zero-pad the rest of the
385 * block and set the end flag.
386 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000387 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
388 if (need == blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000389 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000390 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000391 if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
392 if (dumper->pub.dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000393 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000394 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000395 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000396 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000397 memset(dumper->get__curp + nread, 0, need);
398 dumper->eaddress = dumper->address + nread;
399 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000400 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000401 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
402 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
Glenn L McGrath60281112001-11-02 11:39:46 +0000403 if (!n) {
404 if (ferror(stdin)) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000405 bb_simple_perror_msg(dumper->argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000406 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000407 dumper->get__ateof = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000408 continue;
409 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000410 dumper->get__ateof = 0;
411 if (dumper->pub.dump_length != -1) {
412 dumper->pub.dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000413 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000414 need -= n;
415 if (!need) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000416 if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
417 || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000418 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000419 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
420 dumper->pub.dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000421 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000422 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000423 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000424 if (dumper->pub.dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000425 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000426 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000427 dumper->pub.dump_vflag = DUP;
428 dumper->savaddress += blocksize;
429 dumper->address = dumper->savaddress;
430 need = blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000431 nread = 0;
432 } else {
433 nread += n;
434 }
435 }
436}
437
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000438static void bpad(PR *pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000439{
Rob Landleya6e60372006-06-28 14:36:50 +0000440 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000441
442 /*
443 * remove all conversion flags; '-' is the only one valid
444 * with %s, and it's not useful here.
445 */
446 pr->flags = F_BPAD;
447 *pr->cchar = 's';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000448 for (p1 = pr->fmt; *p1 != '%'; ++p1)
449 continue;
Rob Landleya6e60372006-06-28 14:36:50 +0000450 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000451 if (pr->nospace)
452 pr->nospace--;
453 while ((*p2++ = *p1++) != 0)
454 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000455}
456
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000457static const char conv_str[] ALIGN1 =
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200458 "\0" "\\""0""\0"
459 "\007""\\""a""\0" /* \a */
460 "\b" "\\""b""\0"
461 "\f" "\\""f""\0"
462 "\n" "\\""n""\0"
463 "\r" "\\""r""\0"
464 "\t" "\\""t""\0"
465 "\v" "\\""v""\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000466 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000467
Manuel Novoa III cad53642003-03-19 09:13:01 +0000468
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000469static void conv_c(PR *pr, unsigned char *p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000470{
471 const char *str = conv_str;
472 char buf[10];
473
474 do {
475 if (*p == *str) {
476 ++str;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200477 goto strpr; /* map e.g. '\n' to "\\n" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000478 }
479 str += 4;
480 } while (*str);
481
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100482 if (isprint_asciionly(*p)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000483 *pr->cchar = 'c';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000484 printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000485 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000486 sprintf(buf, "%03o", (int) *p);
487 str = buf;
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100488 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000489 *pr->cchar = 's';
490 printf(pr->fmt, str);
491 }
492}
493
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000494static void conv_u(PR *pr, unsigned char *p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000495{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000496 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000497 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
498 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
499 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
500 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000501
502 /* od used nl, not lf */
503 if (*p <= 0x1f) {
504 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000505 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000506 } else if (*p == 0x7f) {
507 *pr->cchar = 's';
508 printf(pr->fmt, "del");
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100509 } else if (*p < 0x7f) { /* isprint() */
Glenn L McGrath60281112001-11-02 11:39:46 +0000510 *pr->cchar = 'c';
511 printf(pr->fmt, *p);
512 } else {
513 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000514 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000515 }
516}
517
Denis Vlasenko55f79122008-07-16 11:00:16 +0000518static void display(priv_dumper_t* dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000519{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000520 FS *fs;
521 FU *fu;
522 PR *pr;
523 int cnt;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000524 unsigned char *bp, *savebp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000525 off_t saveaddress;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000526 unsigned char savech = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000527
Denis Vlasenko55f79122008-07-16 11:00:16 +0000528 while ((bp = get(dumper)) != NULL) {
529 fs = dumper->pub.fshead;
530 savebp = bp;
531 saveaddress = dumper->address;
532 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000533 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000534 if (fu->flags & F_IGNORE) {
535 break;
536 }
537 for (cnt = fu->reps; cnt; --cnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000538 for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
539 bp += pr->bcnt, pr = pr->nextpr) {
540 if (dumper->eaddress && dumper->address >= dumper->eaddress
541 && !(pr->flags & (F_TEXT | F_BPAD))
542 ) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000543 bpad(pr);
544 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000545 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000546 savech = *pr->nospace;
547 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000548 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000549/* PRINT; */
550 switch (pr->flags) {
551 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000552 printf(pr->fmt, (unsigned) dumper->address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000553 break;
554 case F_BPAD:
555 printf(pr->fmt, "");
556 break;
557 case F_C:
558 conv_c(pr, bp);
559 break;
560 case F_CHAR:
561 printf(pr->fmt, *bp);
562 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000563 case F_DBL: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000564 double dval;
565 float fval;
566
567 switch (pr->bcnt) {
568 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000569 memcpy(&fval, bp, sizeof(fval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000570 printf(pr->fmt, fval);
571 break;
572 case 8:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000573 memcpy(&dval, bp, sizeof(dval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000574 printf(pr->fmt, dval);
575 break;
576 }
577 break;
578 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000579 case F_INT: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000580 int ival;
581 short sval;
582
583 switch (pr->bcnt) {
584 case 1:
585 printf(pr->fmt, (int) *bp);
586 break;
587 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000588 memcpy(&sval, bp, sizeof(sval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000589 printf(pr->fmt, (int) sval);
590 break;
591 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000592 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000593 printf(pr->fmt, ival);
594 break;
595 }
596 break;
597 }
598 case F_P:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100599 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000600 break;
601 case F_STR:
602 printf(pr->fmt, (char *) bp);
603 break;
604 case F_TEXT:
605 printf(pr->fmt);
606 break;
607 case F_U:
608 conv_u(pr, bp);
609 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000610 case F_UINT: {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000611 unsigned ival;
Eric Andersen0f56de62004-01-30 22:52:27 +0000612 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000613
614 switch (pr->bcnt) {
615 case 1:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000616 printf(pr->fmt, (unsigned) *bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000617 break;
618 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000619 memcpy(&sval, bp, sizeof(sval));
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000620 printf(pr->fmt, (unsigned) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000621 break;
622 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000623 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000624 printf(pr->fmt, ival);
625 break;
626 }
627 break;
628 }
629 }
630 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000631 *pr->nospace = savech;
632 }
633 }
634 }
635 }
636 }
637 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000638 if (dumper->endfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000639 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +0000640 * if eaddress not set, error or file size was multiple
641 * of blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000642 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000643 if (!dumper->eaddress) {
644 if (!dumper->address) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000645 return;
646 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000647 dumper->eaddress = dumper->address;
Glenn L McGrath60281112001-11-02 11:39:46 +0000648 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000649 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000650 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000651 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000652 printf(pr->fmt, (unsigned) dumper->eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000653 break;
654 case F_TEXT:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000655 printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000656 break;
657 }
658 }
659 }
660}
661
Denis Vlasenko55f79122008-07-16 11:00:16 +0000662#define dumper ((priv_dumper_t*)pub_dumper)
663int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000664{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000665 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000666 int blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000667
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200668 /* figure out the data block size */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000669 blocksize = 0;
670 tfs = dumper->pub.fshead;
671 while (tfs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000672 tfs->bcnt = bb_dump_size(tfs);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000673 if (blocksize < tfs->bcnt) {
674 blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000675 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000676 tfs = tfs->nextfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000677 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000678 dumper->blocksize = blocksize;
679
Glenn L McGrath60281112001-11-02 11:39:46 +0000680 /* rewrite the rules, do syntax checking */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000681 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
682 rewrite(dumper, tfs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000683 }
684
Denis Vlasenko55f79122008-07-16 11:00:16 +0000685 dumper->argv = argv;
686 display(dumper);
Glenn L McGrath60281112001-11-02 11:39:46 +0000687
Denis Vlasenko55f79122008-07-16 11:00:16 +0000688 return dumper->exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000689}
690
Denis Vlasenko55f79122008-07-16 11:00:16 +0000691void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000692{
Rob Landleyea224be2006-06-18 20:20:07 +0000693 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +0000694 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000695 FU *tfu, **nextfupp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000696 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000697
698 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000699 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000700 if (!dumper->pub.fshead) {
701 dumper->pub.fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000702 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000703 FS *fslast = dumper->pub.fshead;
704 while (fslast->nextfs)
705 fslast = fslast->nextfs;
706 fslast->nextfs = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000707 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000708 nextfupp = &tfs->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000709
710 /* take the format string and break it up into format units */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000711 p = fmt;
712 for (;;) {
Rob Landleyea224be2006-06-18 20:20:07 +0000713 p = skip_whitespace(p);
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100714 if (*p == '\0') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000715 break;
716 }
717
718 /* allocate a new format unit and link it in */
719 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000720 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000721 tfu = xzalloc(sizeof(FU));
Denis Vlasenko55f79122008-07-16 11:00:16 +0000722 *nextfupp = tfu;
723 nextfupp = &tfu->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000724 tfu->reps = 1;
725
726 /* if leading digit, repetition count */
727 if (isdigit(*p)) {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000728 for (savep = p; isdigit(*p); ++p)
729 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000730 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000731 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000732 }
733 /* may overwrite either white space or slash */
734 tfu->reps = atoi(savep);
735 tfu->flags = F_SETREP;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000736 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000737 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000738 }
739
Denis Vlasenko55f79122008-07-16 11:00:16 +0000740 /* skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000741 if (*p == '/') {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100742 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000743 }
744
745 /* byte count */
746 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000747// TODO: use bb_strtou
748 savep = p;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000749 while (isdigit(*++p))
750 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000751 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000752 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000753 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100754// Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
755// Other than this, formats can be pretty much jammed together:
756// "%07_ax:"8/2 "%04x|""\n"
757// but this space is required. The check *can* be removed, but
758// keeping it to stay compat with util-linux hexdump.
Glenn L McGrath60281112001-11-02 11:39:46 +0000759 tfu->bcnt = atoi(savep);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000760 /* skip trailing white space */
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100761 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000762 }
763
764 /* format */
765 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000766 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000767 }
768 for (savep = ++p; *p != '"';) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100769 if (*p++ == '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000770 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000771 }
772 }
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000773 tfu->fmt = xstrndup(savep, p - savep);
Glenn L McGrath60281112001-11-02 11:39:46 +0000774
775 /* alphabetic escape sequences have to be done in place */
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200776 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
777 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
778 /* trailing backslash, if any, is preserved */
779#if 0
780 char *p1;
781 char *p2;
782 p1 = tfu->fmt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000783 for (p2 = p1;; ++p1, ++p2) {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200784 *p2 = *p1;
785 if (*p1 == '\0')
Glenn L McGrath60281112001-11-02 11:39:46 +0000786 break;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200787
Glenn L McGrath60281112001-11-02 11:39:46 +0000788 if (*p1 == '\\') {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200789 const char *cs;
790
791 p1++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000792 *p2 = *p1;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200793 if (*p1 == '\0') {
794 /* "...\" trailing backslash. Eaten. */
795 break;
796 }
797 cs = conv_str + 4; /* skip NUL element */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000798 do {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200799 /* map e.g. "\n" -> '\n' */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000800 if (*p1 == cs[2]) {
801 *p2 = cs[0];
802 break;
803 }
804 cs += 4;
805 } while (*cs);
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200806 /* unknown mappings remove bkslash: "\z" -> 'z' */
Glenn L McGrath60281112001-11-02 11:39:46 +0000807 }
808 }
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200809#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000810
811 p++;
812 }
813}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000814
Glenn L McGrath60281112001-11-02 11:39:46 +0000815/*
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.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000827 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000828 * may be used to endorse or promote products derived from this software
829 * without specific prior written permission.
830 *
Denys Vlasenko95f79532017-08-02 14:26:33 +0200831 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
Glenn L McGrath60281112001-11-02 11:39:46 +0000832 * 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 */