blob: 4d6472e37a68de50d3f4dd893d4a1f127503b89f [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
7 * The Regents of the University of California. All rights reserved.
8 *
Bernhard Reutner-Fischer30385572006-01-31 17:57:48 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Manuel Novoa III cad53642003-03-19 09:13:01 +000017enum _vflag bb_dump_vflag = FIRST;
18FS *bb_dump_fshead; /* head of format strings */
Glenn L McGrath60281112001-11-02 11:39:46 +000019static FU *endfu;
20static char **_argv;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000021static off_t savaddress; /* saved address/offset in stream */
22static off_t eaddress; /* end address */
23static off_t address; /* address/offset in stream */
Manuel Novoa III cad53642003-03-19 09:13:01 +000024off_t bb_dump_skip; /* bytes to skip */
25static int exitval; /* final exit value */
26int bb_dump_blocksize; /* data block size */
27int bb_dump_length = -1; /* max bytes to read */
Glenn L McGrath60281112001-11-02 11:39:46 +000028
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000029static const char index_str[] ALIGN1 = ".#-+ 0123456789";
Glenn L McGrath60281112001-11-02 11:39:46 +000030
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000031static const char size_conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +000032"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
33
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000034static const char lcc[] ALIGN1 = "diouxX";
Manuel Novoa III cad53642003-03-19 09:13:01 +000035
36int bb_dump_size(FS * fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000037{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000038 FU *fu;
39 int bcnt, cur_size;
40 char *fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000042 int prec;
43
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 /* figure out the data block bb_dump_size needed for each format unit */
45 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +000046 if (fu->bcnt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 cur_size += fu->bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000048 continue;
49 }
50 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
51 if (*fmt != '%')
52 continue;
53 /*
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 * bb_dump_skip any special chars -- save precision in
Glenn L McGrath60281112001-11-02 11:39:46 +000055 * case it's a %s format.
56 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 while (strchr(index_str + 1, *++fmt));
Glenn L McGrath60281112001-11-02 11:39:46 +000058 if (*fmt == '.' && isdigit(*++fmt)) {
59 prec = atoi(fmt);
60 while (isdigit(*++fmt));
61 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000062 p = strchr(size_conv_str + 12, *fmt);
63 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 if (*fmt == 's') {
65 bcnt += prec;
66 } else if (*fmt == '_') {
67 ++fmt;
68 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
69 bcnt += 1;
70 }
Glenn L McGrath60281112001-11-02 11:39:46 +000071 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 } else {
73 bcnt += size_conv_str[p - (size_conv_str + 12)];
Glenn L McGrath60281112001-11-02 11:39:46 +000074 }
75 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000077 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000078 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000079}
80
Manuel Novoa III cad53642003-03-19 09:13:01 +000081static void rewrite(FS * fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000082{
83 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
Rob Landleyea224be2006-06-18 20:20:07 +000084 PR *pr, **nextpr = NULL;
85 FU *fu;
86 char *p1, *p2, *p3;
Glenn L McGrath60281112001-11-02 11:39:46 +000087 char savech, *fmtp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 const char *byte_count_str;
Glenn L McGrath60281112001-11-02 11:39:46 +000089 int nconv, prec = 0;
90
91 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
92 /*
93 * break each format unit into print units; each
94 * conversion character gets its own.
95 */
96 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
97 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +000098 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
Rob Landleyea224be2006-06-18 20:20:07 +000099 pr = xzalloc(sizeof(PR));
Glenn L McGrath60281112001-11-02 11:39:46 +0000100 if (!fu->nextpr)
101 fu->nextpr = pr;
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000102 /* ignore nextpr -- its unused inside the loop and is
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +0000103 * uninitialized 1st time through.
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000104 */
Glenn L McGrath60281112001-11-02 11:39:46 +0000105
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 /* bb_dump_skip preceding text and up to the next % sign */
Glenn L McGrath60281112001-11-02 11:39:46 +0000107 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
108
109 /* only text in the string */
110 if (!*p1) {
111 pr->fmt = fmtp;
112 pr->flags = F_TEXT;
113 break;
114 }
115
116 /*
117 * get precision for %s -- if have a byte count, don't
118 * need it.
119 */
120 if (fu->bcnt) {
121 sokay = USEBCNT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 /* bb_dump_skip to conversion character */
123 for (++p1; strchr(index_str, *p1); ++p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000124 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 /* bb_dump_skip any special chars, field width */
126 while (strchr(index_str + 1, *++p1));
Glenn L McGrath60281112001-11-02 11:39:46 +0000127 if (*p1 == '.' && isdigit(*++p1)) {
128 sokay = USEPREC;
129 prec = atoi(p1);
130 while (isdigit(*++p1));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000131 } else
Glenn L McGrath60281112001-11-02 11:39:46 +0000132 sokay = NOTOKAY;
133 }
134
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000135 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000136
137 /*
138 * figure out the byte count for each conversion;
139 * rewrite the format as necessary, set up blank-
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 * pbb_dump_adding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000141 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142
143 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000144 pr->flags = F_CHAR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 DO_BYTE_COUNT_1:
146 byte_count_str = "\001";
147 DO_BYTE_COUNT:
148 if (fu->bcnt) {
149 do {
150 if (fu->bcnt == *byte_count_str) {
151 break;
152 }
153 } while (*++byte_count_str);
Glenn L McGrath60281112001-11-02 11:39:46 +0000154 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 /* Unlike the original, output the remainder of the format string. */
156 if (!*byte_count_str) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000157 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 }
159 pr->bcnt = *byte_count_str;
160 } else if (*p1 == 'l') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000161 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 ++p1;
163 DO_INT_CONV:
164 {
165 const char *e;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000166 e = strchr(lcc, *p1);
167 if (!e) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 goto DO_BAD_CONV_CHAR;
169 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000170 pr->flags = F_INT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 if (e > lcc + 1) {
172 pr->flags = F_UINT;
173 }
174 byte_count_str = "\004\002\001";
175 goto DO_BYTE_COUNT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000176 }
177 /* NOTREACHED */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 } else if (strchr(lcc, *p1)) {
179 goto DO_INT_CONV;
180 } else if (strchr("eEfgG", *p1)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000181 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 byte_count_str = "\010\004";
183 goto DO_BYTE_COUNT;
184 } else if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000185 pr->flags = F_STR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 if (sokay == USEBCNT) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000187 pr->bcnt = fu->bcnt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 } else if (sokay == USEPREC) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000189 pr->bcnt = prec;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190 } else { /* NOTOKAY */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000191 bb_error_msg_and_die("%%s requires a precision or a byte count");
Glenn L McGrath60281112001-11-02 11:39:46 +0000192 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 } else if (*p1 == '_') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000194 ++p2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000195 switch (p1[1]) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000196 case 'A':
197 endfu = fu;
198 fu->flags |= F_IGNORE;
199 /* FALLTHROUGH */
200 case 'a':
201 pr->flags = F_ADDRESS;
202 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
204 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000205 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000207 break;
208 case 'c':
209 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000210 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000212 case 'p':
213 pr->flags = F_P;
214 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000215 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000216 case 'u':
217 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000218 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000220 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000222 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 } else {
224 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000225 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000226 }
227
228 /*
229 * copy to PR format string, set conversion character
230 * pointer, update original.
231 */
232 savech = *p2;
233 p1[1] = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +0000234 pr->fmt = xstrdup(fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000235 *p2 = savech;
236 pr->cchar = pr->fmt + (p1 - fmtp);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000237
238 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000239 * Skip subsequent text and up to the next % sign and tack the
240 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000241 * we lose the " is a HEX number" part of fmt.
242 */
243 for (p3 = p2; *p3 && *p3 != '%'; p3++);
244 if (p3 > p2)
245 {
246 savech = *p3;
247 *p3 = '\0';
Rob Landleyea224be2006-06-18 20:20:07 +0000248 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000249 strcat(pr->fmt, p2);
250 *p3 = savech;
251 p2 = p3;
252 }
253
Glenn L McGrath60281112001-11-02 11:39:46 +0000254 fmtp = p2;
255
256 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000257 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000258 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000259 }
260 }
261 /*
262 * if format unit byte count not specified, figure it out
263 * so can adjust rep count later.
264 */
265 if (!fu->bcnt)
266 for (pr = fu->nextpr; pr; pr = pr->nextpr)
267 fu->bcnt += pr->bcnt;
268 }
269 /*
270 * if the format string interprets any data at all, and it's
Manuel Novoa III cad53642003-03-19 09:13:01 +0000271 * not the same as the bb_dump_blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000272 * interprets any data at all, and has no iteration count,
273 * repeat it as necessary.
274 *
275 * if, rep count is greater than 1, no trailing whitespace
276 * gets output from the last iteration of the format unit.
277 */
278 for (fu = fs->nextfu;; fu = fu->nextfu) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000280 !(fu->flags & F_SETREP) && fu->bcnt)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000281 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000282 if (fu->reps > 1) {
283 for (pr = fu->nextpr;; pr = pr->nextpr)
284 if (!pr->nextpr)
285 break;
286 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
287 p2 = isspace(*p1) ? p1 : NULL;
288 if (p2)
289 pr->nospace = p2;
290 }
291 if (!fu->nextfu)
292 break;
293 }
294}
295
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000296static void do_skip(const char *fname, int statok)
Glenn L McGrath60281112001-11-02 11:39:46 +0000297{
298 struct stat sbuf;
299
300 if (statok) {
Eric Andersen70060d22004-03-27 10:02:48 +0000301 if (fstat(STDIN_FILENO, &sbuf)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000302 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000303 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000304 if ((!(S_ISCHR(sbuf.st_mode) ||
305 S_ISBLK(sbuf.st_mode) ||
Manuel Novoa III cad53642003-03-19 09:13:01 +0000306 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
307 /* If bb_dump_size valid and bb_dump_skip >= size */
308 bb_dump_skip -= sbuf.st_size;
Glenn L McGrath60281112001-11-02 11:39:46 +0000309 address += sbuf.st_size;
310 return;
311 }
312 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000313 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000314 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000315 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000316 savaddress = address += bb_dump_skip;
317 bb_dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000318}
319
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320static int next(char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000321{
Denis Vlasenko931de892007-06-21 12:43:45 +0000322 static smallint done;
323
Glenn L McGrath60281112001-11-02 11:39:46 +0000324 int statok;
325
326 if (argv) {
327 _argv = argv;
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000328 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000329 }
330 for (;;) {
331 if (*_argv) {
332 if (!(freopen(*_argv, "r", stdin))) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000333 bb_simple_perror_msg(*_argv);
Glenn L McGrath60281112001-11-02 11:39:46 +0000334 exitval = 1;
335 ++_argv;
336 continue;
337 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000338 done = statok = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000339 } else {
Denis Vlasenko931de892007-06-21 12:43:45 +0000340 if (done)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000341 return 0;
Denis Vlasenko931de892007-06-21 12:43:45 +0000342 done = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000343 statok = 0;
344 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000345 if (bb_dump_skip)
346 do_skip(statok ? *_argv : "stdin", statok);
Glenn L McGrath60281112001-11-02 11:39:46 +0000347 if (*_argv)
348 ++_argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 if (!bb_dump_skip)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000350 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000351 }
352 /* NOTREACHED */
353}
354
Mike Frysinger294254c2006-02-19 22:59:12 +0000355static unsigned char *get(void)
Glenn L McGrath60281112001-11-02 11:39:46 +0000356{
Denis Vlasenko931de892007-06-21 12:43:45 +0000357 static smallint ateof = 1;
358 static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
359
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000360 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000361 int need, nread;
Mike Frysinger294254c2006-02-19 22:59:12 +0000362 unsigned char *tmpp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000363
364 if (!curp) {
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000365 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000366 curp = xmalloc(bb_dump_blocksize);
367 savp = xmalloc(bb_dump_blocksize);
Glenn L McGrath60281112001-11-02 11:39:46 +0000368 } else {
369 tmpp = curp;
370 curp = savp;
371 savp = tmpp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000372 address = savaddress += bb_dump_blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000373 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000374 for (need = bb_dump_blocksize, nread = 0;;) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000375 /*
376 * if read the right number of bytes, or at EOF for one file,
377 * and no other files are available, zero-pad the rest of the
378 * block and set the end flag.
379 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000380 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
381 if (need == bb_dump_blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000382 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000383 }
Mike Frysinger78bd5042006-04-16 05:51:47 +0000384 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000385 if (bb_dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000386 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000387 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000388 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000389 }
Bernhard Reutner-Fischer30385572006-01-31 17:57:48 +0000390 memset((char *) curp + nread, 0, need);
Glenn L McGrath60281112001-11-02 11:39:46 +0000391 eaddress = address + nread;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000392 return curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000393 }
Mike Frysinger294254c2006-02-19 22:59:12 +0000394 n = fread((char *) curp + nread, sizeof(unsigned char),
Manuel Novoa III cad53642003-03-19 09:13:01 +0000395 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
Glenn L McGrath60281112001-11-02 11:39:46 +0000396 if (!n) {
397 if (ferror(stdin)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000398 bb_simple_perror_msg(_argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000399 }
400 ateof = 1;
401 continue;
402 }
403 ateof = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000404 if (bb_dump_length != -1) {
405 bb_dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000406 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000407 need -= n;
408 if (!need) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000409 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
Mike Frysinger78bd5042006-04-16 05:51:47 +0000410 || memcmp(curp, savp, bb_dump_blocksize)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000411 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
412 bb_dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000413 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000414 return curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000415 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000416 if (bb_dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000417 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000418 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000419 bb_dump_vflag = DUP;
420 address = savaddress += bb_dump_blocksize;
421 need = bb_dump_blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000422 nread = 0;
423 } else {
424 nread += n;
425 }
426 }
427}
428
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000429static void bpad(PR * pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000430{
Rob Landleya6e60372006-06-28 14:36:50 +0000431 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000432
433 /*
434 * remove all conversion flags; '-' is the only one valid
435 * with %s, and it's not useful here.
436 */
437 pr->flags = F_BPAD;
438 *pr->cchar = 's';
439 for (p1 = pr->fmt; *p1 != '%'; ++p1);
Rob Landleya6e60372006-06-28 14:36:50 +0000440 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
441 if (pr->nospace) pr->nospace--;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000442 while ((*p2++ = *p1++) != 0);
Glenn L McGrath60281112001-11-02 11:39:46 +0000443}
444
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000445static const char conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000446 "\0\\0\0"
447 "\007\\a\0" /* \a */
448 "\b\\b\0"
449 "\f\\b\0"
450 "\n\\n\0"
451 "\r\\r\0"
452 "\t\\t\0"
453 "\v\\v\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000454 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000455
Manuel Novoa III cad53642003-03-19 09:13:01 +0000456
Mike Frysinger294254c2006-02-19 22:59:12 +0000457static void conv_c(PR * pr, unsigned char * p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000458{
459 const char *str = conv_str;
460 char buf[10];
461
462 do {
463 if (*p == *str) {
464 ++str;
465 goto strpr;
466 }
467 str += 4;
468 } while (*str);
469
Glenn L McGrath60281112001-11-02 11:39:46 +0000470 if (isprint(*p)) {
471 *pr->cchar = 'c';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000472 (void) printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000473 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000474 sprintf(buf, "%03o", (int) *p);
475 str = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000476 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000477 *pr->cchar = 's';
478 printf(pr->fmt, str);
479 }
480}
481
Mike Frysinger294254c2006-02-19 22:59:12 +0000482static void conv_u(PR * pr, unsigned char * p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000483{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000484 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000485 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
486 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
487 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
488 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000489
490 /* od used nl, not lf */
491 if (*p <= 0x1f) {
492 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000493 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000494 } else if (*p == 0x7f) {
495 *pr->cchar = 's';
496 printf(pr->fmt, "del");
497 } else if (isprint(*p)) {
498 *pr->cchar = 'c';
499 printf(pr->fmt, *p);
500 } else {
501 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000502 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000503 }
504}
505
Manuel Novoa III cad53642003-03-19 09:13:01 +0000506static void display(void)
Glenn L McGrath60281112001-11-02 11:39:46 +0000507{
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000508/* extern FU *endfu; */
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000509 FS *fs;
510 FU *fu;
511 PR *pr;
512 int cnt;
513 unsigned char *bp;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000514
Manuel Novoa III cad53642003-03-19 09:13:01 +0000515 off_t saveaddress;
Mike Frysinger294254c2006-02-19 22:59:12 +0000516 unsigned char savech = 0, *savebp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000517
518 while ((bp = get()) != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000519 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000520 fs = fs->nextfs, bp = savebp, address = saveaddress) {
521 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000522 if (fu->flags & F_IGNORE) {
523 break;
524 }
525 for (cnt = fu->reps; cnt; --cnt) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000526 for (pr = fu->nextpr; pr; address += pr->bcnt,
527 bp += pr->bcnt, pr = pr->nextpr) {
528 if (eaddress && address >= eaddress &&
529 !(pr->flags & (F_TEXT | F_BPAD))) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000530 bpad(pr);
531 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000532 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000533 savech = *pr->nospace;
534 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000535 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000536/* PRINT; */
537 switch (pr->flags) {
538 case F_ADDRESS:
Glenn L McGrathff5309a2004-05-02 08:38:53 +0000539 printf(pr->fmt, (unsigned int) address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000540 break;
541 case F_BPAD:
542 printf(pr->fmt, "");
543 break;
544 case F_C:
545 conv_c(pr, bp);
546 break;
547 case F_CHAR:
548 printf(pr->fmt, *bp);
549 break;
550 case F_DBL:{
551 double dval;
552 float fval;
553
554 switch (pr->bcnt) {
555 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000556 memmove((char *) &fval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000557 sizeof(fval));
558 printf(pr->fmt, fval);
559 break;
560 case 8:
Mike Frysinger1a540302006-04-16 05:58:21 +0000561 memmove((char *) &dval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000562 sizeof(dval));
563 printf(pr->fmt, dval);
564 break;
565 }
566 break;
567 }
568 case F_INT:{
569 int ival;
570 short sval;
571
572 switch (pr->bcnt) {
573 case 1:
574 printf(pr->fmt, (int) *bp);
575 break;
576 case 2:
Mike Frysinger1a540302006-04-16 05:58:21 +0000577 memmove((char *) &sval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000578 sizeof(sval));
579 printf(pr->fmt, (int) sval);
580 break;
581 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000582 memmove((char *) &ival, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000583 sizeof(ival));
584 printf(pr->fmt, ival);
585 break;
586 }
587 break;
588 }
589 case F_P:
590 printf(pr->fmt, isprint(*bp) ? *bp : '.');
591 break;
592 case F_STR:
593 printf(pr->fmt, (char *) bp);
594 break;
595 case F_TEXT:
596 printf(pr->fmt);
597 break;
598 case F_U:
599 conv_u(pr, bp);
600 break;
601 case F_UINT:{
Eric Andersen0f56de62004-01-30 22:52:27 +0000602 unsigned int ival;
603 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000604
605 switch (pr->bcnt) {
606 case 1:
Eric Andersen0f56de62004-01-30 22:52:27 +0000607 printf(pr->fmt, (unsigned int) * bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000608 break;
609 case 2:
Mike Frysinger1a540302006-04-16 05:58:21 +0000610 memmove((char *) &sval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000611 sizeof(sval));
Eric Andersen0f56de62004-01-30 22:52:27 +0000612 printf(pr->fmt, (unsigned int) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000613 break;
614 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000615 memmove((char *) &ival, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000616 sizeof(ival));
617 printf(pr->fmt, ival);
618 break;
619 }
620 break;
621 }
622 }
623 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000624 *pr->nospace = savech;
625 }
626 }
627 }
628 }
629 }
630 }
631 if (endfu) {
632 /*
Manuel Novoa III cad53642003-03-19 09:13:01 +0000633 * if eaddress not set, error or file bb_dump_size was multiple of
634 * bb_dump_blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000635 */
636 if (!eaddress) {
637 if (!address) {
638 return;
639 }
640 eaddress = address;
641 }
642 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000643 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000644 case F_ADDRESS:
Glenn L McGrathd2b860f2004-03-05 05:47:19 +0000645 (void) printf(pr->fmt, (unsigned int) eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000646 break;
647 case F_TEXT:
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000648 (void) printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000649 break;
650 }
651 }
652 }
653}
654
Manuel Novoa III cad53642003-03-19 09:13:01 +0000655int bb_dump_dump(char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000656{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000657 FS *tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000658
Manuel Novoa III cad53642003-03-19 09:13:01 +0000659 /* figure out the data block bb_dump_size */
660 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
661 tfs->bcnt = bb_dump_size(tfs);
662 if (bb_dump_blocksize < tfs->bcnt) {
663 bb_dump_blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000664 }
665 }
666 /* rewrite the rules, do syntax checking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000667 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000668 rewrite(tfs);
669 }
670
671 next(argv);
672 display();
673
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000674 return exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000675}
676
Manuel Novoa III cad53642003-03-19 09:13:01 +0000677void bb_dump_add(const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000678{
Rob Landleyea224be2006-06-18 20:20:07 +0000679 const char *p;
680 char *p1;
681 char *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000682 static FS **nextfs;
683 FS *tfs;
684 FU *tfu, **nextfu;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000685 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000686
687 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000688 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000689 if (!bb_dump_fshead) {
690 bb_dump_fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000691 } else {
692 *nextfs = tfs;
693 }
694 nextfs = &tfs->nextfs;
695 nextfu = &tfs->nextfu;
696
697 /* take the format string and break it up into format units */
698 for (p = fmt;;) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000699 /* bb_dump_skip leading white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000700 p = skip_whitespace(p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000701 if (!*p) {
702 break;
703 }
704
705 /* allocate a new format unit and link it in */
706 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000707 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000708 tfu = xzalloc(sizeof(FU));
Glenn L McGrath60281112001-11-02 11:39:46 +0000709 *nextfu = tfu;
710 nextfu = &tfu->nextfu;
711 tfu->reps = 1;
712
713 /* if leading digit, repetition count */
714 if (isdigit(*p)) {
715 for (savep = p; isdigit(*p); ++p);
716 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000717 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000718 }
719 /* may overwrite either white space or slash */
720 tfu->reps = atoi(savep);
721 tfu->flags = F_SETREP;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000722 /* bb_dump_skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000723 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000724 }
725
Manuel Novoa III cad53642003-03-19 09:13:01 +0000726 /* bb_dump_skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000727 if (*p == '/') {
Rob Landleyea224be2006-06-18 20:20:07 +0000728 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000729 }
730
731 /* byte count */
732 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000733// TODO: use bb_strtou
734 savep = p;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000735 do p++; while (isdigit(*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000736 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000737 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000738 }
739 tfu->bcnt = atoi(savep);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000740 /* bb_dump_skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000741 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000742 }
743
744 /* format */
745 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000746 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000747 }
748 for (savep = ++p; *p != '"';) {
749 if (*p++ == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000751 }
752 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000753 tfu->fmt = xmalloc(p - savep + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000754 strncpy(tfu->fmt, savep, p - savep);
755 tfu->fmt[p - savep] = '\0';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000756/* escape(tfu->fmt); */
Glenn L McGrath60281112001-11-02 11:39:46 +0000757
758 p1 = tfu->fmt;
759
760 /* alphabetic escape sequences have to be done in place */
761 for (p2 = p1;; ++p1, ++p2) {
762 if (!*p1) {
763 *p2 = *p1;
764 break;
765 }
766 if (*p1 == '\\') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000767 const char *cs = conv_str + 4;
768 ++p1;
769 *p2 = *p1;
770 do {
771 if (*p1 == cs[2]) {
772 *p2 = cs[0];
773 break;
774 }
775 cs += 4;
776 } while (*cs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000777 }
778 }
779
780 p++;
781 }
782}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000783
Glenn L McGrath60281112001-11-02 11:39:46 +0000784/*
785 * Copyright (c) 1989 The Regents of the University of California.
786 * All rights reserved.
787 *
788 * Redistribution and use in source and binary forms, with or without
789 * modification, are permitted provided that the following conditions
790 * are met:
791 * 1. Redistributions of source code must retain the above copyright
792 * notice, this list of conditions and the following disclaimer.
793 * 2. Redistributions in binary form must reproduce the above copyright
794 * notice, this list of conditions and the following disclaimer in the
795 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000796 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000797 * may be used to endorse or promote products derived from this software
798 * without specific prior written permission.
799 *
800 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
801 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
802 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
803 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
804 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
805 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
806 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
807 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
808 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
809 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
810 * SUCH DAMAGE.
811 */