blob: 0d1bb18f2a179ee499f30322f1180435c76169f2 [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 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 if (!(p = strchr(size_conv_str + 12, *fmt))) {
63 if (*fmt == 's') {
64 bcnt += prec;
65 } else if (*fmt == '_') {
66 ++fmt;
67 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
68 bcnt += 1;
69 }
Glenn L McGrath60281112001-11-02 11:39:46 +000070 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 } else {
72 bcnt += size_conv_str[p - (size_conv_str + 12)];
Glenn L McGrath60281112001-11-02 11:39:46 +000073 }
74 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000076 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000077 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000078}
79
Manuel Novoa III cad53642003-03-19 09:13:01 +000080static void rewrite(FS * fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000081{
82 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
Rob Landleyea224be2006-06-18 20:20:07 +000083 PR *pr, **nextpr = NULL;
84 FU *fu;
85 char *p1, *p2, *p3;
Glenn L McGrath60281112001-11-02 11:39:46 +000086 char savech, *fmtp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 const char *byte_count_str;
Glenn L McGrath60281112001-11-02 11:39:46 +000088 int nconv, prec = 0;
89
90 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
91 /*
92 * break each format unit into print units; each
93 * conversion character gets its own.
94 */
95 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
96 /* NOSTRICT */
Eric Andersen4a11e0f2003-04-19 23:18:35 +000097 /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
Rob Landleyea224be2006-06-18 20:20:07 +000098 pr = xzalloc(sizeof(PR));
Glenn L McGrath60281112001-11-02 11:39:46 +000099 if (!fu->nextpr)
100 fu->nextpr = pr;
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000101 /* ignore nextpr -- its unused inside the loop and is
102 * uninitialized 1st time thru.
103 */
Glenn L McGrath60281112001-11-02 11:39:46 +0000104
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 /* bb_dump_skip preceding text and up to the next % sign */
Glenn L McGrath60281112001-11-02 11:39:46 +0000106 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
107
108 /* only text in the string */
109 if (!*p1) {
110 pr->fmt = fmtp;
111 pr->flags = F_TEXT;
112 break;
113 }
114
115 /*
116 * get precision for %s -- if have a byte count, don't
117 * need it.
118 */
119 if (fu->bcnt) {
120 sokay = USEBCNT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 /* bb_dump_skip to conversion character */
122 for (++p1; strchr(index_str, *p1); ++p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000123 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 /* bb_dump_skip any special chars, field width */
125 while (strchr(index_str + 1, *++p1));
Glenn L McGrath60281112001-11-02 11:39:46 +0000126 if (*p1 == '.' && isdigit(*++p1)) {
127 sokay = USEPREC;
128 prec = atoi(p1);
129 while (isdigit(*++p1));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000130 } else
Glenn L McGrath60281112001-11-02 11:39:46 +0000131 sokay = NOTOKAY;
132 }
133
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000134 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000135
136 /*
137 * figure out the byte count for each conversion;
138 * rewrite the format as necessary, set up blank-
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 * pbb_dump_adding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000140 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141
142 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000143 pr->flags = F_CHAR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 DO_BYTE_COUNT_1:
145 byte_count_str = "\001";
146 DO_BYTE_COUNT:
147 if (fu->bcnt) {
148 do {
149 if (fu->bcnt == *byte_count_str) {
150 break;
151 }
152 } while (*++byte_count_str);
Glenn L McGrath60281112001-11-02 11:39:46 +0000153 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 /* Unlike the original, output the remainder of the format string. */
155 if (!*byte_count_str) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000156 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 }
158 pr->bcnt = *byte_count_str;
159 } else if (*p1 == 'l') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000160 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 ++p1;
162 DO_INT_CONV:
163 {
164 const char *e;
165 if (!(e = strchr(lcc, *p1))) {
166 goto DO_BAD_CONV_CHAR;
167 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000168 pr->flags = F_INT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 if (e > lcc + 1) {
170 pr->flags = F_UINT;
171 }
172 byte_count_str = "\004\002\001";
173 goto DO_BYTE_COUNT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000174 }
175 /* NOTREACHED */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 } else if (strchr(lcc, *p1)) {
177 goto DO_INT_CONV;
178 } else if (strchr("eEfgG", *p1)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000179 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 byte_count_str = "\010\004";
181 goto DO_BYTE_COUNT;
182 } else if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000183 pr->flags = F_STR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000184 if (sokay == USEBCNT) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000185 pr->bcnt = fu->bcnt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 } else if (sokay == USEPREC) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000187 pr->bcnt = prec;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 } else { /* NOTOKAY */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000189 bb_error_msg_and_die("%%s requires a precision or a byte count");
Glenn L McGrath60281112001-11-02 11:39:46 +0000190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 } else if (*p1 == '_') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000192 ++p2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000193 switch (p1[1]) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000194 case 'A':
195 endfu = fu;
196 fu->flags |= F_IGNORE;
197 /* FALLTHROUGH */
198 case 'a':
199 pr->flags = F_ADDRESS;
200 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000201 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
202 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000203 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000205 break;
206 case 'c':
207 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000208 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000210 case 'p':
211 pr->flags = F_P;
212 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000214 case 'u':
215 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000216 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000218 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000220 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 } else {
222 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000223 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000224 }
225
226 /*
227 * copy to PR format string, set conversion character
228 * pointer, update original.
229 */
230 savech = *p2;
231 p1[1] = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +0000232 pr->fmt = xstrdup(fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000233 *p2 = savech;
234 pr->cchar = pr->fmt + (p1 - fmtp);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000235
236 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000237 * Skip subsequent text and up to the next % sign and tack the
238 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000239 * we lose the " is a HEX number" part of fmt.
240 */
241 for (p3 = p2; *p3 && *p3 != '%'; p3++);
242 if (p3 > p2)
243 {
244 savech = *p3;
245 *p3 = '\0';
Rob Landleyea224be2006-06-18 20:20:07 +0000246 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000247 strcat(pr->fmt, p2);
248 *p3 = savech;
249 p2 = p3;
250 }
251
Glenn L McGrath60281112001-11-02 11:39:46 +0000252 fmtp = p2;
253
254 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000255 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000256 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000257 }
258 }
259 /*
260 * if format unit byte count not specified, figure it out
261 * so can adjust rep count later.
262 */
263 if (!fu->bcnt)
264 for (pr = fu->nextpr; pr; pr = pr->nextpr)
265 fu->bcnt += pr->bcnt;
266 }
267 /*
268 * if the format string interprets any data at all, and it's
Manuel Novoa III cad53642003-03-19 09:13:01 +0000269 * not the same as the bb_dump_blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000270 * interprets any data at all, and has no iteration count,
271 * repeat it as necessary.
272 *
273 * if, rep count is greater than 1, no trailing whitespace
274 * gets output from the last iteration of the format unit.
275 */
276 for (fu = fs->nextfu;; fu = fu->nextfu) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000277 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000278 !(fu->flags & F_SETREP) && fu->bcnt)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000280 if (fu->reps > 1) {
281 for (pr = fu->nextpr;; pr = pr->nextpr)
282 if (!pr->nextpr)
283 break;
284 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
285 p2 = isspace(*p1) ? p1 : NULL;
286 if (p2)
287 pr->nospace = p2;
288 }
289 if (!fu->nextfu)
290 break;
291 }
292}
293
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000294static void do_skip(const char *fname, int statok)
Glenn L McGrath60281112001-11-02 11:39:46 +0000295{
296 struct stat sbuf;
297
298 if (statok) {
Eric Andersen70060d22004-03-27 10:02:48 +0000299 if (fstat(STDIN_FILENO, &sbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300 bb_perror_msg_and_die("%s", fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000301 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000302 if ((!(S_ISCHR(sbuf.st_mode) ||
303 S_ISBLK(sbuf.st_mode) ||
Manuel Novoa III cad53642003-03-19 09:13:01 +0000304 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
305 /* If bb_dump_size valid and bb_dump_skip >= size */
306 bb_dump_skip -= sbuf.st_size;
Glenn L McGrath60281112001-11-02 11:39:46 +0000307 address += sbuf.st_size;
308 return;
309 }
310 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000311 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
312 bb_perror_msg_and_die("%s", fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000313 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000314 savaddress = address += bb_dump_skip;
315 bb_dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000316}
317
Manuel Novoa III cad53642003-03-19 09:13:01 +0000318static int next(char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000319{
Denis Vlasenko931de892007-06-21 12:43:45 +0000320 static smallint done;
321
Glenn L McGrath60281112001-11-02 11:39:46 +0000322 int statok;
323
324 if (argv) {
325 _argv = argv;
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000326 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000327 }
328 for (;;) {
329 if (*_argv) {
330 if (!(freopen(*_argv, "r", stdin))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000331 bb_perror_msg("%s", *_argv);
Glenn L McGrath60281112001-11-02 11:39:46 +0000332 exitval = 1;
333 ++_argv;
334 continue;
335 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000336 done = statok = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000337 } else {
Denis Vlasenko931de892007-06-21 12:43:45 +0000338 if (done)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000339 return 0;
Denis Vlasenko931de892007-06-21 12:43:45 +0000340 done = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000341 statok = 0;
342 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000343 if (bb_dump_skip)
344 do_skip(statok ? *_argv : "stdin", statok);
Glenn L McGrath60281112001-11-02 11:39:46 +0000345 if (*_argv)
346 ++_argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000347 if (!bb_dump_skip)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000348 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000349 }
350 /* NOTREACHED */
351}
352
Mike Frysinger294254c2006-02-19 22:59:12 +0000353static unsigned char *get(void)
Glenn L McGrath60281112001-11-02 11:39:46 +0000354{
Denis Vlasenko931de892007-06-21 12:43:45 +0000355 static smallint ateof = 1;
356 static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
357
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000358 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000359 int need, nread;
Mike Frysinger294254c2006-02-19 22:59:12 +0000360 unsigned char *tmpp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000361
362 if (!curp) {
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000363 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000364 curp = xmalloc(bb_dump_blocksize);
365 savp = xmalloc(bb_dump_blocksize);
Glenn L McGrath60281112001-11-02 11:39:46 +0000366 } else {
367 tmpp = curp;
368 curp = savp;
369 savp = tmpp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000370 address = savaddress += bb_dump_blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000371 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000372 for (need = bb_dump_blocksize, nread = 0;;) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000373 /*
374 * if read the right number of bytes, or at EOF for one file,
375 * and no other files are available, zero-pad the rest of the
376 * block and set the end flag.
377 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000378 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
379 if (need == bb_dump_blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000380 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000381 }
Mike Frysinger78bd5042006-04-16 05:51:47 +0000382 if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000383 if (bb_dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000384 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000385 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000386 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000387 }
Bernhard Reutner-Fischer30385572006-01-31 17:57:48 +0000388 memset((char *) curp + nread, 0, need);
Glenn L McGrath60281112001-11-02 11:39:46 +0000389 eaddress = address + nread;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000390 return curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000391 }
Mike Frysinger294254c2006-02-19 22:59:12 +0000392 n = fread((char *) curp + nread, sizeof(unsigned char),
Manuel Novoa III cad53642003-03-19 09:13:01 +0000393 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
Glenn L McGrath60281112001-11-02 11:39:46 +0000394 if (!n) {
395 if (ferror(stdin)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000396 bb_perror_msg("%s", _argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000397 }
398 ateof = 1;
399 continue;
400 }
401 ateof = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000402 if (bb_dump_length != -1) {
403 bb_dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000404 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000405 need -= n;
406 if (!need) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000407 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
Mike Frysinger78bd5042006-04-16 05:51:47 +0000408 || memcmp(curp, savp, bb_dump_blocksize)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000409 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
410 bb_dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000411 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000412 return curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000413 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000414 if (bb_dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000415 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000416 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000417 bb_dump_vflag = DUP;
418 address = savaddress += bb_dump_blocksize;
419 need = bb_dump_blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000420 nread = 0;
421 } else {
422 nread += n;
423 }
424 }
425}
426
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000427static void bpad(PR * pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000428{
Rob Landleya6e60372006-06-28 14:36:50 +0000429 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000430
431 /*
432 * remove all conversion flags; '-' is the only one valid
433 * with %s, and it's not useful here.
434 */
435 pr->flags = F_BPAD;
436 *pr->cchar = 's';
437 for (p1 = pr->fmt; *p1 != '%'; ++p1);
Rob Landleya6e60372006-06-28 14:36:50 +0000438 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
439 if (pr->nospace) pr->nospace--;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000440 while ((*p2++ = *p1++) != 0);
Glenn L McGrath60281112001-11-02 11:39:46 +0000441}
442
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000443static const char conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000444 "\0\\0\0"
445 "\007\\a\0" /* \a */
446 "\b\\b\0"
447 "\f\\b\0"
448 "\n\\n\0"
449 "\r\\r\0"
450 "\t\\t\0"
451 "\v\\v\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000452 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000453
Manuel Novoa III cad53642003-03-19 09:13:01 +0000454
Mike Frysinger294254c2006-02-19 22:59:12 +0000455static void conv_c(PR * pr, unsigned char * p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000456{
457 const char *str = conv_str;
458 char buf[10];
459
460 do {
461 if (*p == *str) {
462 ++str;
463 goto strpr;
464 }
465 str += 4;
466 } while (*str);
467
Glenn L McGrath60281112001-11-02 11:39:46 +0000468 if (isprint(*p)) {
469 *pr->cchar = 'c';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000470 (void) printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000471 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000472 sprintf(buf, "%03o", (int) *p);
473 str = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000474 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000475 *pr->cchar = 's';
476 printf(pr->fmt, str);
477 }
478}
479
Mike Frysinger294254c2006-02-19 22:59:12 +0000480static void conv_u(PR * pr, unsigned char * p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000481{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000482 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000483 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
484 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
485 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
486 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000487
488 /* od used nl, not lf */
489 if (*p <= 0x1f) {
490 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000491 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000492 } else if (*p == 0x7f) {
493 *pr->cchar = 's';
494 printf(pr->fmt, "del");
495 } else if (isprint(*p)) {
496 *pr->cchar = 'c';
497 printf(pr->fmt, *p);
498 } else {
499 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000500 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000501 }
502}
503
Manuel Novoa III cad53642003-03-19 09:13:01 +0000504static void display(void)
Glenn L McGrath60281112001-11-02 11:39:46 +0000505{
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000506/* extern FU *endfu; */
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000507 FS *fs;
508 FU *fu;
509 PR *pr;
510 int cnt;
511 unsigned char *bp;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000512
Manuel Novoa III cad53642003-03-19 09:13:01 +0000513 off_t saveaddress;
Mike Frysinger294254c2006-02-19 22:59:12 +0000514 unsigned char savech = 0, *savebp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000515
516 while ((bp = get()) != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000517 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000518 fs = fs->nextfs, bp = savebp, address = saveaddress) {
519 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000520 if (fu->flags & F_IGNORE) {
521 break;
522 }
523 for (cnt = fu->reps; cnt; --cnt) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000524 for (pr = fu->nextpr; pr; address += pr->bcnt,
525 bp += pr->bcnt, pr = pr->nextpr) {
526 if (eaddress && address >= eaddress &&
527 !(pr->flags & (F_TEXT | F_BPAD))) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000528 bpad(pr);
529 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000530 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000531 savech = *pr->nospace;
532 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000533 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000534/* PRINT; */
535 switch (pr->flags) {
536 case F_ADDRESS:
Glenn L McGrathff5309a2004-05-02 08:38:53 +0000537 printf(pr->fmt, (unsigned int) address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000538 break;
539 case F_BPAD:
540 printf(pr->fmt, "");
541 break;
542 case F_C:
543 conv_c(pr, bp);
544 break;
545 case F_CHAR:
546 printf(pr->fmt, *bp);
547 break;
548 case F_DBL:{
549 double dval;
550 float fval;
551
552 switch (pr->bcnt) {
553 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000554 memmove((char *) &fval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000555 sizeof(fval));
556 printf(pr->fmt, fval);
557 break;
558 case 8:
Mike Frysinger1a540302006-04-16 05:58:21 +0000559 memmove((char *) &dval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000560 sizeof(dval));
561 printf(pr->fmt, dval);
562 break;
563 }
564 break;
565 }
566 case F_INT:{
567 int ival;
568 short sval;
569
570 switch (pr->bcnt) {
571 case 1:
572 printf(pr->fmt, (int) *bp);
573 break;
574 case 2:
Mike Frysinger1a540302006-04-16 05:58:21 +0000575 memmove((char *) &sval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000576 sizeof(sval));
577 printf(pr->fmt, (int) sval);
578 break;
579 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000580 memmove((char *) &ival, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000581 sizeof(ival));
582 printf(pr->fmt, ival);
583 break;
584 }
585 break;
586 }
587 case F_P:
588 printf(pr->fmt, isprint(*bp) ? *bp : '.');
589 break;
590 case F_STR:
591 printf(pr->fmt, (char *) bp);
592 break;
593 case F_TEXT:
594 printf(pr->fmt);
595 break;
596 case F_U:
597 conv_u(pr, bp);
598 break;
599 case F_UINT:{
Eric Andersen0f56de62004-01-30 22:52:27 +0000600 unsigned int ival;
601 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000602
603 switch (pr->bcnt) {
604 case 1:
Eric Andersen0f56de62004-01-30 22:52:27 +0000605 printf(pr->fmt, (unsigned int) * bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000606 break;
607 case 2:
Mike Frysinger1a540302006-04-16 05:58:21 +0000608 memmove((char *) &sval, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000609 sizeof(sval));
Eric Andersen0f56de62004-01-30 22:52:27 +0000610 printf(pr->fmt, (unsigned int) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000611 break;
612 case 4:
Mike Frysinger1a540302006-04-16 05:58:21 +0000613 memmove((char *) &ival, (char *) bp,
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000614 sizeof(ival));
615 printf(pr->fmt, ival);
616 break;
617 }
618 break;
619 }
620 }
621 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000622 *pr->nospace = savech;
623 }
624 }
625 }
626 }
627 }
628 }
629 if (endfu) {
630 /*
Manuel Novoa III cad53642003-03-19 09:13:01 +0000631 * if eaddress not set, error or file bb_dump_size was multiple of
632 * bb_dump_blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000633 */
634 if (!eaddress) {
635 if (!address) {
636 return;
637 }
638 eaddress = address;
639 }
640 for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000641 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000642 case F_ADDRESS:
Glenn L McGrathd2b860f2004-03-05 05:47:19 +0000643 (void) printf(pr->fmt, (unsigned int) eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000644 break;
645 case F_TEXT:
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000646 (void) printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000647 break;
648 }
649 }
650 }
651}
652
Manuel Novoa III cad53642003-03-19 09:13:01 +0000653int bb_dump_dump(char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000654{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000655 FS *tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000656
Manuel Novoa III cad53642003-03-19 09:13:01 +0000657 /* figure out the data block bb_dump_size */
658 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
659 tfs->bcnt = bb_dump_size(tfs);
660 if (bb_dump_blocksize < tfs->bcnt) {
661 bb_dump_blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000662 }
663 }
664 /* rewrite the rules, do syntax checking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000665 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000666 rewrite(tfs);
667 }
668
669 next(argv);
670 display();
671
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000672 return exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000673}
674
Manuel Novoa III cad53642003-03-19 09:13:01 +0000675void bb_dump_add(const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000676{
Rob Landleyea224be2006-06-18 20:20:07 +0000677 const char *p;
678 char *p1;
679 char *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000680 static FS **nextfs;
681 FS *tfs;
682 FU *tfu, **nextfu;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000683 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000684
685 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000686 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000687 if (!bb_dump_fshead) {
688 bb_dump_fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000689 } else {
690 *nextfs = tfs;
691 }
692 nextfs = &tfs->nextfs;
693 nextfu = &tfs->nextfu;
694
695 /* take the format string and break it up into format units */
696 for (p = fmt;;) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000697 /* bb_dump_skip leading white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000698 p = skip_whitespace(p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000699 if (!*p) {
700 break;
701 }
702
703 /* allocate a new format unit and link it in */
704 /* NOSTRICT */
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000705 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000706 tfu = xzalloc(sizeof(FU));
Glenn L McGrath60281112001-11-02 11:39:46 +0000707 *nextfu = tfu;
708 nextfu = &tfu->nextfu;
709 tfu->reps = 1;
710
711 /* if leading digit, repetition count */
712 if (isdigit(*p)) {
713 for (savep = p; isdigit(*p); ++p);
714 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000715 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000716 }
717 /* may overwrite either white space or slash */
718 tfu->reps = atoi(savep);
719 tfu->flags = F_SETREP;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000720 /* bb_dump_skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000721 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000722 }
723
Manuel Novoa III cad53642003-03-19 09:13:01 +0000724 /* bb_dump_skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000725 if (*p == '/') {
Rob Landleyea224be2006-06-18 20:20:07 +0000726 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000727 }
728
729 /* byte count */
730 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000731// TODO: use bb_strtou
732 savep = p;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000733 do p++; while (isdigit(*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000734 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000735 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000736 }
737 tfu->bcnt = atoi(savep);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000738 /* bb_dump_skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000739 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000740 }
741
742 /* format */
743 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000744 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000745 }
746 for (savep = ++p; *p != '"';) {
747 if (*p++ == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000748 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000749 }
750 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000751 tfu->fmt = xmalloc(p - savep + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000752 strncpy(tfu->fmt, savep, p - savep);
753 tfu->fmt[p - savep] = '\0';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000754/* escape(tfu->fmt); */
Glenn L McGrath60281112001-11-02 11:39:46 +0000755
756 p1 = tfu->fmt;
757
758 /* alphabetic escape sequences have to be done in place */
759 for (p2 = p1;; ++p1, ++p2) {
760 if (!*p1) {
761 *p2 = *p1;
762 break;
763 }
764 if (*p1 == '\\') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000765 const char *cs = conv_str + 4;
766 ++p1;
767 *p2 = *p1;
768 do {
769 if (*p1 == cs[2]) {
770 *p2 = cs[0];
771 break;
772 }
773 cs += 4;
774 } while (*cs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000775 }
776 }
777
778 p++;
779 }
780}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000781
Glenn L McGrath60281112001-11-02 11:39:46 +0000782/*
783 * Copyright (c) 1989 The Regents of the University of California.
784 * All rights reserved.
785 *
786 * Redistribution and use in source and binary forms, with or without
787 * modification, are permitted provided that the following conditions
788 * are met:
789 * 1. Redistributions of source code must retain the above copyright
790 * notice, this list of conditions and the following disclaimer.
791 * 2. Redistributions in binary form must reproduce the above copyright
792 * notice, this list of conditions and the following disclaimer in the
793 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000794 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000795 * may be used to endorse or promote products derived from this software
796 * without specific prior written permission.
797 *
798 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
799 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
800 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
801 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
802 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
803 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
804 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
805 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
806 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
807 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
808 * SUCH DAMAGE.
809 */