blob: b4b49d709e77492c45fe17662a021c1ef9a65c55 [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 Vlasenkocb8e84e2017-01-25 16:21:00 +010016static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
Glenn L McGrath60281112001-11-02 11:39:46 +000017
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000018static const char size_conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +000019"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
20
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010021static const char int_convs[] ALIGN1 = "diouxX";
Manuel Novoa III cad53642003-03-19 09:13:01 +000022
Denis Vlasenko55f79122008-07-16 11:00:16 +000023
24typedef struct priv_dumper_t {
25 dumper_t pub;
26
27 char **argv;
28 FU *endfu;
29 off_t savaddress; /* saved address/offset in stream */
30 off_t eaddress; /* end address */
31 off_t address; /* address/offset in stream */
32 int blocksize;
33 smallint exitval; /* final exit value */
34
35 /* former statics */
36 smallint next__done;
37 smallint get__ateof; // = 1;
38 unsigned char *get__curp;
39 unsigned char *get__savp;
40} priv_dumper_t;
41
42dumper_t* FAST_FUNC alloc_dumper(void)
43{
44 priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
45 dumper->pub.dump_length = -1;
46 dumper->pub.dump_vflag = FIRST;
47 dumper->get__ateof = 1;
48 return &dumper->pub;
49}
50
51
52static NOINLINE int bb_dump_size(FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000053{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000054 FU *fu;
55 int bcnt, cur_size;
56 char *fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000058 int prec;
59
Denys Vlasenko5f7904b2017-07-14 16:03:43 +020060 /* figure out the data block size needed for each format unit */
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +000062 if (fu->bcnt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 cur_size += fu->bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000064 continue;
65 }
66 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
67 if (*fmt != '%')
68 continue;
69 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +000070 * skip any special chars -- save precision in
Glenn L McGrath60281112001-11-02 11:39:46 +000071 * case it's a %s format.
72 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010073 while (strchr(dot_flags_width_chars + 1, *++fmt))
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +010074 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000075 if (*fmt == '.' && isdigit(*++fmt)) {
76 prec = atoi(fmt);
Denis Vlasenko55f79122008-07-16 11:00:16 +000077 while (isdigit(*++fmt))
78 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000079 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000080 p = strchr(size_conv_str + 12, *fmt);
81 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 if (*fmt == 's') {
83 bcnt += prec;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010084 }
85 if (*fmt == '_') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 ++fmt;
87 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
88 bcnt += 1;
89 }
Glenn L McGrath60281112001-11-02 11:39:46 +000090 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 } else {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +010092 bcnt += p[-12];
Glenn L McGrath60281112001-11-02 11:39:46 +000093 }
94 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000096 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000097 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000098}
99
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200100static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +0000101{
Rob Landleyea224be2006-06-18 20:20:07 +0000102 FU *fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000103
104 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100105 PR *pr;
106 char *p1, *p2, *p3;
107 char *fmtp;
108 int nconv = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000109 /*
110 * break each format unit into print units; each
111 * conversion character gets its own.
112 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100113 for (fmtp = fu->fmt; *fmtp; ) {
114 unsigned len;
115 const char *prec;
116 const char *byte_count_str;
117
118 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
119 pr = xzalloc(sizeof(*pr));
Glenn L McGrath60281112001-11-02 11:39:46 +0000120 if (!fu->nextpr)
121 fu->nextpr = pr;
Glenn L McGrath60281112001-11-02 11:39:46 +0000122
Denis Vlasenko55f79122008-07-16 11:00:16 +0000123 /* skip preceding text and up to the next % sign */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100124 p1 = strchr(fmtp, '%');
125 if (!p1) { /* only text in the string */
Glenn L McGrath60281112001-11-02 11:39:46 +0000126 pr->fmt = fmtp;
127 pr->flags = F_TEXT;
128 break;
129 }
130
131 /*
132 * get precision for %s -- if have a byte count, don't
133 * need it.
134 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100135 prec = NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000136 if (fu->bcnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000137 /* skip to conversion character */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100138 while (strchr(dot_flags_width_chars, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000139 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000140 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000141 /* skip any special chars, field width */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100142 while (strchr(dot_flags_width_chars + 1, *++p1))
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000143 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000144 if (*p1 == '.' && isdigit(*++p1)) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100145 prec = p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000146 while (isdigit(*++p1))
147 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100148 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000149 }
150
Denis Vlasenko55f79122008-07-16 11:00:16 +0000151 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000152
153 /*
154 * figure out the byte count for each conversion;
155 * rewrite the format as necessary, set up blank-
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200156 * padding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000157 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000159 pr->flags = F_CHAR;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000160 DO_BYTE_COUNT_1:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 byte_count_str = "\001";
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000162 DO_BYTE_COUNT:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 if (fu->bcnt) {
Denys Vlasenko0f436472017-01-25 01:58:00 +0100164 for (;;) {
165 if (fu->bcnt == *byte_count_str)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 break;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100167 if (*++byte_count_str == 0)
168 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
169 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000170 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 /* Unlike the original, output the remainder of the format string. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 pr->bcnt = *byte_count_str;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100173 } else
174 if (*p1 == 'l') { /* %ld etc */
175 const char *e;
176
Glenn L McGrath60281112001-11-02 11:39:46 +0000177 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 ++p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000179 DO_INT_CONV:
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100180 e = strchr(int_convs, *p1); /* "diouxX"? */
181 if (!e)
182 goto DO_BAD_CONV_CHAR;
183 pr->flags = F_INT;
184 if (e > int_convs + 1) /* not d or i? */
185 pr->flags = F_UINT;
186 byte_count_str = "\004\002\001";
187 goto DO_BYTE_COUNT;
188 } else
189 if (strchr(int_convs, *p1)) { /* %d etc */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190 goto DO_INT_CONV;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100191 } else
192 if (strchr("eEfgG", *p1)) { /* floating point */
Glenn L McGrath60281112001-11-02 11:39:46 +0000193 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000194 byte_count_str = "\010\004";
195 goto DO_BYTE_COUNT;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100196 } else
197 if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000198 pr->flags = F_STR;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100199 pr->bcnt = fu->bcnt;
200 if (fu->bcnt == 0) {
201 if (!prec)
202 bb_error_msg_and_die("%%s needs precision or byte count");
203 pr->bcnt = atoi(prec);
Glenn L McGrath60281112001-11-02 11:39:46 +0000204 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100205 } else
206 if (*p1 == '_') {
207 p2++; /* move past a in "%_a" */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000208 switch (p1[1]) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100209 case 'A': /* %_A[dox]: print address and the end */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000210 dumper->endfu = fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000211 fu->flags |= F_IGNORE;
212 /* FALLTHROUGH */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100213 case 'a': /* %_a[dox]: current address */
Glenn L McGrath60281112001-11-02 11:39:46 +0000214 pr->flags = F_ADDRESS;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100215 p2++; /* move past x in "%_ax" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000216 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
217 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000218 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000220 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100221 case 'c': /* %_c: chars, \ooo, \n \r \t etc */
Glenn L McGrath60281112001-11-02 11:39:46 +0000222 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000223 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100225 case 'p': /* %_p: chars, dots for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000226 pr->flags = F_P;
227 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 goto DO_BYTE_COUNT_1;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100229 case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */
Glenn L McGrath60281112001-11-02 11:39:46 +0000230 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000231 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000233 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000235 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 } else {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000237 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000238 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000239 }
240
241 /*
242 * copy to PR format string, set conversion character
243 * pointer, update original.
244 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100245 len = (p1 - fmtp) + 1;
246 pr->fmt = xstrndup(fmtp, len);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000247 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000248 * Skip subsequent text and up to the next % sign and tack the
249 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000250 * we lose the " is a HEX number" part of fmt.
251 */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000252 for (p3 = p2; *p3 && *p3 != '%'; p3++)
253 continue;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100254 if ((p3 - p2) != 0) {
255 char *d;
256 pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
257 d += len;
258 do {
259 *d++ = *p2++;
260 } while (p2 != p3);
261 *d = '\0';
262 /* now p2 = p3 */
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000263 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100264 pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
Glenn L McGrath60281112001-11-02 11:39:46 +0000265 fmtp = p2;
266
267 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000268 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000269 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000270 }
271 }
272 /*
273 * if format unit byte count not specified, figure it out
274 * so can adjust rep count later.
275 */
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100276 if (fu->bcnt == 0)
Glenn L McGrath60281112001-11-02 11:39:46 +0000277 for (pr = fu->nextpr; pr; pr = pr->nextpr)
278 fu->bcnt += pr->bcnt;
279 }
280 /*
281 * if the format string interprets any data at all, and it's
Denis Vlasenko55f79122008-07-16 11:00:16 +0000282 * not the same as the blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000283 * interprets any data at all, and has no iteration count,
284 * repeat it as necessary.
285 *
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100286 * if rep count is greater than 1, no trailing whitespace
Glenn L McGrath60281112001-11-02 11:39:46 +0000287 * gets output from the last iteration of the format unit.
288 */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000289 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100290 if (!fu->nextfu
291 && fs->bcnt < dumper->blocksize
292 && !(fu->flags & F_SETREP)
293 && fu->bcnt
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000294 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000295 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000296 }
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100297 if (fu->reps > 1 && fu->nextpr) {
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100298 PR *pr;
299 char *p1, *p2;
300
Glenn L McGrath60281112001-11-02 11:39:46 +0000301 for (pr = fu->nextpr;; pr = pr->nextpr)
302 if (!pr->nextpr)
303 break;
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100304 p2 = NULL;
305 for (p1 = pr->fmt; *p1; ++p1)
Glenn L McGrath60281112001-11-02 11:39:46 +0000306 p2 = isspace(*p1) ? p1 : NULL;
307 if (p2)
308 pr->nospace = p2;
309 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000310 }
311}
312
Denys Vlasenko63214a62017-07-14 16:18:16 +0200313static void do_skip(priv_dumper_t *dumper, const char *fname)
Glenn L McGrath60281112001-11-02 11:39:46 +0000314{
315 struct stat sbuf;
316
Denys Vlasenko63214a62017-07-14 16:18:16 +0200317 xfstat(STDIN_FILENO, &sbuf, fname);
318 if (S_ISREG(sbuf.st_mode)
319 && dumper->pub.dump_skip >= sbuf.st_size
320 ) {
321 /* If st_size is valid and pub.dump_skip >= st_size */
322 dumper->pub.dump_skip -= sbuf.st_size;
323 dumper->address += sbuf.st_size;
324 return;
Glenn L McGrath60281112001-11-02 11:39:46 +0000325 }
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100326 if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000327 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000328 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000329 dumper->address += dumper->pub.dump_skip;
330 dumper->savaddress = dumper->address;
331 dumper->pub.dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000332}
333
Denis Vlasenko55f79122008-07-16 11:00:16 +0000334static NOINLINE int next(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000335{
Glenn L McGrath60281112001-11-02 11:39:46 +0000336 for (;;) {
Denys Vlasenko63214a62017-07-14 16:18:16 +0200337 const char *fname = *dumper->argv;
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200338
339 if (fname) {
340 dumper->argv++;
Denys Vlasenko90678f02017-07-14 16:29:30 +0200341 if (NOT_LONE_DASH(fname)) {
342 if (!freopen(fname, "r", stdin)) {
343 bb_simple_perror_msg(fname);
344 dumper->exitval = 1;
345 continue;
346 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000347 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000348 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000349 if (dumper->next__done)
Denys Vlasenko6aca76d2010-02-06 13:53:21 +0100350 return 0; /* no next file */
Glenn L McGrath60281112001-11-02 11:39:46 +0000351 }
Denys Vlasenko63214a62017-07-14 16:18:16 +0200352 dumper->next__done = 1;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000353 if (dumper->pub.dump_skip)
Denys Vlasenko63214a62017-07-14 16:18:16 +0200354 do_skip(dumper, fname ? fname : "stdin");
355 if (dumper->pub.dump_skip == 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000356 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000357 }
358 /* NOTREACHED */
359}
360
Denis Vlasenko55f79122008-07-16 11:00:16 +0000361static unsigned char *get(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000362{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000363 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000364 int need, nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000365 int blocksize = dumper->blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000366
Denis Vlasenko55f79122008-07-16 11:00:16 +0000367 if (!dumper->get__curp) {
368 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
369 dumper->get__curp = xmalloc(blocksize);
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000370 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
Glenn L McGrath60281112001-11-02 11:39:46 +0000371 } else {
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000372 unsigned char *tmp = dumper->get__curp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000373 dumper->get__curp = dumper->get__savp;
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000374 dumper->get__savp = tmp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000375 dumper->savaddress += blocksize;
376 dumper->address = dumper->savaddress;
Glenn L McGrath60281112001-11-02 11:39:46 +0000377 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000378 need = blocksize;
379 nread = 0;
380 while (1) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000381 /*
382 * if read the right number of bytes, or at EOF for one file,
383 * and no other files are available, zero-pad the rest of the
384 * block and set the end flag.
385 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000386 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
387 if (need == blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000388 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000389 }
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200390 if (dumper->pub.dump_vflag != ALL /* not "show all"? */
391 && dumper->pub.dump_vflag != FIRST /* not first line? */
392 && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */
393 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000394 if (dumper->pub.dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000395 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000396 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000397 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000398 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000399 memset(dumper->get__curp + nread, 0, need);
400 dumper->eaddress = dumper->address + nread;
401 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000402 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000403 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
404 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200405 if (n == 0) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000406 if (ferror(stdin)) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000407 bb_simple_perror_msg(dumper->argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000408 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000409 dumper->get__ateof = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000410 continue;
411 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000412 dumper->get__ateof = 0;
413 if (dumper->pub.dump_length != -1) {
414 dumper->pub.dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000415 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000416 need -= n;
Denys Vlasenkoe5d5f5b2018-07-03 16:27:54 +0200417 if (need == 0) {
418 if (dumper->pub.dump_vflag == ALL /* "show all"? */
419 || dumper->pub.dump_vflag == FIRST /* first line? */
420 || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000421 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000422 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
423 dumper->pub.dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000424 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000425 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000426 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000427 if (dumper->pub.dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000428 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000429 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000430 dumper->pub.dump_vflag = DUP;
431 dumper->savaddress += blocksize;
432 dumper->address = dumper->savaddress;
433 need = blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000434 nread = 0;
435 } else {
436 nread += n;
437 }
438 }
439}
440
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000441static void bpad(PR *pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000442{
Rob Landleya6e60372006-06-28 14:36:50 +0000443 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000444
445 /*
446 * remove all conversion flags; '-' is the only one valid
447 * with %s, and it's not useful here.
448 */
449 pr->flags = F_BPAD;
450 *pr->cchar = 's';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000451 for (p1 = pr->fmt; *p1 != '%'; ++p1)
452 continue;
Rob Landleya6e60372006-06-28 14:36:50 +0000453 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000454 if (pr->nospace)
455 pr->nospace--;
456 while ((*p2++ = *p1++) != 0)
457 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000458}
459
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000460static const char conv_str[] ALIGN1 =
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200461 "\0" "\\""0""\0"
462 "\007""\\""a""\0" /* \a */
463 "\b" "\\""b""\0"
464 "\f" "\\""f""\0"
465 "\n" "\\""n""\0"
466 "\r" "\\""r""\0"
467 "\t" "\\""t""\0"
468 "\v" "\\""v""\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000469 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000470
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000471static void conv_c(PR *pr, unsigned char *p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000472{
473 const char *str = conv_str;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000474
475 do {
476 if (*p == *str) {
477 ++str;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200478 goto strpr; /* map e.g. '\n' to "\\n" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000479 }
480 str += 4;
481 } while (*str);
482
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100483 if (isprint_asciionly(*p)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000484 *pr->cchar = 'c';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000485 printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000486 } else {
Denys Vlasenko2ab994f2018-04-06 18:26:33 +0200487 char buf[4];
488 /* gcc-8.0.1 needs lots of casts to shut up */
489 sprintf(buf, "%03o", (unsigned)(uint8_t)*p);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000490 str = buf;
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100491 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000492 *pr->cchar = 's';
493 printf(pr->fmt, str);
494 }
495}
496
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000497static void conv_u(PR *pr, unsigned char *p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000498{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000499 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000500 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
501 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
502 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
503 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000504
505 /* od used nl, not lf */
506 if (*p <= 0x1f) {
507 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000508 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000509 } else if (*p == 0x7f) {
510 *pr->cchar = 's';
511 printf(pr->fmt, "del");
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100512 } else if (*p < 0x7f) { /* isprint() */
Glenn L McGrath60281112001-11-02 11:39:46 +0000513 *pr->cchar = 'c';
514 printf(pr->fmt, *p);
515 } else {
516 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000517 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000518 }
519}
520
Denis Vlasenko55f79122008-07-16 11:00:16 +0000521static void display(priv_dumper_t* dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000522{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000523 FS *fs;
524 FU *fu;
525 PR *pr;
526 int cnt;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000527 unsigned char *bp, *savebp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000528 off_t saveaddress;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000529 unsigned char savech = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000530
Denis Vlasenko55f79122008-07-16 11:00:16 +0000531 while ((bp = get(dumper)) != NULL) {
532 fs = dumper->pub.fshead;
533 savebp = bp;
534 saveaddress = dumper->address;
535 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000536 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000537 if (fu->flags & F_IGNORE) {
538 break;
539 }
540 for (cnt = fu->reps; cnt; --cnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000541 for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
542 bp += pr->bcnt, pr = pr->nextpr) {
543 if (dumper->eaddress && dumper->address >= dumper->eaddress
544 && !(pr->flags & (F_TEXT | F_BPAD))
545 ) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000546 bpad(pr);
547 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000548 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000549 savech = *pr->nospace;
550 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000551 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000552/* PRINT; */
553 switch (pr->flags) {
554 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000555 printf(pr->fmt, (unsigned) dumper->address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000556 break;
557 case F_BPAD:
558 printf(pr->fmt, "");
559 break;
560 case F_C:
561 conv_c(pr, bp);
562 break;
563 case F_CHAR:
564 printf(pr->fmt, *bp);
565 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000566 case F_DBL: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000567 double dval;
568 float fval;
569
570 switch (pr->bcnt) {
571 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000572 memcpy(&fval, bp, sizeof(fval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000573 printf(pr->fmt, fval);
574 break;
575 case 8:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000576 memcpy(&dval, bp, sizeof(dval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000577 printf(pr->fmt, dval);
578 break;
579 }
580 break;
581 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000582 case F_INT: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000583 int ival;
584 short sval;
585
586 switch (pr->bcnt) {
587 case 1:
588 printf(pr->fmt, (int) *bp);
589 break;
590 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000591 memcpy(&sval, bp, sizeof(sval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000592 printf(pr->fmt, (int) sval);
593 break;
594 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000595 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000596 printf(pr->fmt, ival);
597 break;
598 }
599 break;
600 }
601 case F_P:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100602 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000603 break;
604 case F_STR:
605 printf(pr->fmt, (char *) bp);
606 break;
607 case F_TEXT:
608 printf(pr->fmt);
609 break;
610 case F_U:
611 conv_u(pr, bp);
612 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000613 case F_UINT: {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000614 unsigned ival;
Eric Andersen0f56de62004-01-30 22:52:27 +0000615 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000616
617 switch (pr->bcnt) {
618 case 1:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000619 printf(pr->fmt, (unsigned) *bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000620 break;
621 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000622 memcpy(&sval, bp, sizeof(sval));
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000623 printf(pr->fmt, (unsigned) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000624 break;
625 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000626 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000627 printf(pr->fmt, ival);
628 break;
629 }
630 break;
631 }
632 }
633 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000634 *pr->nospace = savech;
635 }
636 }
637 }
638 }
639 }
640 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000641 if (dumper->endfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000642 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +0000643 * if eaddress not set, error or file size was multiple
644 * of blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000645 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000646 if (!dumper->eaddress) {
647 if (!dumper->address) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000648 return;
649 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000650 dumper->eaddress = dumper->address;
Glenn L McGrath60281112001-11-02 11:39:46 +0000651 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000652 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000653 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000654 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000655 printf(pr->fmt, (unsigned) dumper->eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000656 break;
657 case F_TEXT:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000658 printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000659 break;
660 }
661 }
662 }
663}
664
Denis Vlasenko55f79122008-07-16 11:00:16 +0000665#define dumper ((priv_dumper_t*)pub_dumper)
666int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000667{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000668 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000669 int blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000670
Denys Vlasenko5f7904b2017-07-14 16:03:43 +0200671 /* figure out the data block size */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000672 blocksize = 0;
673 tfs = dumper->pub.fshead;
674 while (tfs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000675 tfs->bcnt = bb_dump_size(tfs);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000676 if (blocksize < tfs->bcnt) {
677 blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000678 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000679 tfs = tfs->nextfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000680 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000681 dumper->blocksize = blocksize;
682
Glenn L McGrath60281112001-11-02 11:39:46 +0000683 /* rewrite the rules, do syntax checking */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000684 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
685 rewrite(dumper, tfs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000686 }
687
Denis Vlasenko55f79122008-07-16 11:00:16 +0000688 dumper->argv = argv;
689 display(dumper);
Glenn L McGrath60281112001-11-02 11:39:46 +0000690
Denis Vlasenko55f79122008-07-16 11:00:16 +0000691 return dumper->exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000692}
693
Denis Vlasenko55f79122008-07-16 11:00:16 +0000694void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000695{
Rob Landleyea224be2006-06-18 20:20:07 +0000696 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +0000697 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000698 FU *tfu, **nextfupp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000699 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000700
701 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000702 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000703 if (!dumper->pub.fshead) {
704 dumper->pub.fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000705 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000706 FS *fslast = dumper->pub.fshead;
707 while (fslast->nextfs)
708 fslast = fslast->nextfs;
709 fslast->nextfs = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000710 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000711 nextfupp = &tfs->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000712
713 /* take the format string and break it up into format units */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000714 p = fmt;
715 for (;;) {
Rob Landleyea224be2006-06-18 20:20:07 +0000716 p = skip_whitespace(p);
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100717 if (*p == '\0') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000718 break;
719 }
720
721 /* allocate a new format unit and link it in */
722 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000723 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000724 tfu = xzalloc(sizeof(FU));
Denis Vlasenko55f79122008-07-16 11:00:16 +0000725 *nextfupp = tfu;
726 nextfupp = &tfu->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000727 tfu->reps = 1;
728
729 /* if leading digit, repetition count */
730 if (isdigit(*p)) {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000731 for (savep = p; isdigit(*p); ++p)
732 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000733 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000734 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000735 }
736 /* may overwrite either white space or slash */
737 tfu->reps = atoi(savep);
738 tfu->flags = F_SETREP;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000739 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000740 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000741 }
742
Denis Vlasenko55f79122008-07-16 11:00:16 +0000743 /* skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000744 if (*p == '/') {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100745 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000746 }
747
748 /* byte count */
749 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000750// TODO: use bb_strtou
751 savep = p;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000752 while (isdigit(*++p))
753 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000754 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000755 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000756 }
Denys Vlasenkocb8e84e2017-01-25 16:21:00 +0100757// Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
758// Other than this, formats can be pretty much jammed together:
759// "%07_ax:"8/2 "%04x|""\n"
760// but this space is required. The check *can* be removed, but
761// keeping it to stay compat with util-linux hexdump.
Glenn L McGrath60281112001-11-02 11:39:46 +0000762 tfu->bcnt = atoi(savep);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000763 /* skip trailing white space */
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100764 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000765 }
766
767 /* format */
768 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000769 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000770 }
771 for (savep = ++p; *p != '"';) {
Denys Vlasenkoa0bef7c2011-11-18 02:47:35 +0100772 if (*p++ == '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000773 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000774 }
775 }
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000776 tfu->fmt = xstrndup(savep, p - savep);
Glenn L McGrath60281112001-11-02 11:39:46 +0000777
778 /* alphabetic escape sequences have to be done in place */
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200779 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
780 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
781 /* trailing backslash, if any, is preserved */
782#if 0
783 char *p1;
784 char *p2;
785 p1 = tfu->fmt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000786 for (p2 = p1;; ++p1, ++p2) {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200787 *p2 = *p1;
788 if (*p1 == '\0')
Glenn L McGrath60281112001-11-02 11:39:46 +0000789 break;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200790
Glenn L McGrath60281112001-11-02 11:39:46 +0000791 if (*p1 == '\\') {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200792 const char *cs;
793
794 p1++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000795 *p2 = *p1;
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200796 if (*p1 == '\0') {
797 /* "...\" trailing backslash. Eaten. */
798 break;
799 }
800 cs = conv_str + 4; /* skip NUL element */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000801 do {
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200802 /* map e.g. "\n" -> '\n' */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000803 if (*p1 == cs[2]) {
804 *p2 = cs[0];
805 break;
806 }
807 cs += 4;
808 } while (*cs);
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200809 /* unknown mappings remove bkslash: "\z" -> 'z' */
Glenn L McGrath60281112001-11-02 11:39:46 +0000810 }
811 }
Denys Vlasenkod3d7f082016-08-26 20:14:31 +0200812#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000813
814 p++;
815 }
816}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000817
Glenn L McGrath60281112001-11-02 11:39:46 +0000818/*
819 * Copyright (c) 1989 The Regents of the University of California.
820 * All rights reserved.
821 *
822 * Redistribution and use in source and binary forms, with or without
823 * modification, are permitted provided that the following conditions
824 * are met:
825 * 1. Redistributions of source code must retain the above copyright
826 * notice, this list of conditions and the following disclaimer.
827 * 2. Redistributions in binary form must reproduce the above copyright
828 * notice, this list of conditions and the following disclaimer in the
829 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000830 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000831 * may be used to endorse or promote products derived from this software
832 * without specific prior written permission.
833 *
Denys Vlasenko95f79532017-08-02 14:26:33 +0200834 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
Glenn L McGrath60281112001-11-02 11:39:46 +0000835 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
836 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
837 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
838 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
839 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
840 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
841 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
842 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
843 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
844 * SUCH DAMAGE.
845 */