xref: /freebsd/usr.bin/hexdump/parse.c (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 
34 #include <err.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include "hexdump.h"
41 
42 FU *endfu;					/* format at end-of-data */
43 
44 void
45 addfile(const char *name)
46 {
47 	unsigned char *p;
48 	FILE *fp;
49 	int ch;
50 	char buf[2048 + 1];
51 
52 	if ((fp = fopen(name, "r")) == NULL)
53 		err(1, "%s", name);
54 	while (fgets(buf, sizeof(buf), fp)) {
55 		if (!(p = strchr(buf, '\n'))) {
56 			warnx("line too long");
57 			while ((ch = getchar()) != '\n' && ch != EOF);
58 			continue;
59 		}
60 		*p = '\0';
61 		for (p = buf; *p && isspace(*p); ++p);
62 		if (!*p || *p == '#')
63 			continue;
64 		add(p);
65 	}
66 	(void)fclose(fp);
67 }
68 
69 void
70 add(const char *fmt)
71 {
72 	unsigned const char *p, *savep;
73 	static FS **nextfs;
74 	FS *tfs;
75 	FU *tfu, **nextfu;
76 
77 	/* start new linked list of format units */
78 	if ((tfs = calloc(1, sizeof(FS))) == NULL)
79 		err(1, NULL);
80 	if (!fshead)
81 		fshead = tfs;
82 	else
83 		*nextfs = tfs;
84 	nextfs = &tfs->nextfs;
85 	nextfu = &tfs->nextfu;
86 
87 	/* take the format string and break it up into format units */
88 	for (p = fmt;;) {
89 		/* skip leading white space */
90 		for (; isspace(*p); ++p);
91 		if (!*p)
92 			break;
93 
94 		/* allocate a new format unit and link it in */
95 		if ((tfu = calloc(1, sizeof(FU))) == NULL)
96 			err(1, NULL);
97 		*nextfu = tfu;
98 		nextfu = &tfu->nextfu;
99 		tfu->reps = 1;
100 
101 		/* if leading digit, repetition count */
102 		if (isdigit(*p)) {
103 			for (savep = p; isdigit(*p); ++p);
104 			if (!isspace(*p) && *p != '/')
105 				badfmt(fmt);
106 			/* may overwrite either white space or slash */
107 			tfu->reps = atoi(savep);
108 			tfu->flags = F_SETREP;
109 			/* skip trailing white space */
110 			for (++p; isspace(*p); ++p);
111 		}
112 
113 		/* skip slash and trailing white space */
114 		if (*p == '/')
115 			while (isspace(*++p));
116 
117 		/* byte count */
118 		if (isdigit(*p)) {
119 			for (savep = p; isdigit(*p); ++p);
120 			if (!isspace(*p))
121 				badfmt(fmt);
122 			tfu->bcnt = atoi(savep);
123 			/* skip trailing white space */
124 			for (++p; isspace(*p); ++p);
125 		}
126 
127 		/* format */
128 		if (*p != '"')
129 			badfmt(fmt);
130 		for (savep = ++p; *p != '"';)
131 			if (*p++ == 0)
132 				badfmt(fmt);
133 		if (!(tfu->fmt = malloc(p - savep + 1)))
134 			err(1, NULL);
135 		(void) strlcpy(tfu->fmt, savep, p - savep + 1);
136 		escape(tfu->fmt);
137 		p++;
138 	}
139 }
140 
141 static const char *spec = ".#-+ 0123456789";
142 
143 int
144 size(FS *fs)
145 {
146 	FU *fu;
147 	int bcnt, cursize;
148 	unsigned char *fmt;
149 	int prec;
150 
151 	/* figure out the data block size needed for each format unit */
152 	for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
153 		if (fu->bcnt) {
154 			cursize += fu->bcnt * fu->reps;
155 			continue;
156 		}
157 		for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
158 			if (*fmt != '%')
159 				continue;
160 			/*
161 			 * skip any special chars -- save precision in
162 			 * case it's a %s format.
163 			 */
164 			while (*++fmt != 0 && strchr(spec + 1, *fmt) != NULL)
165 				;
166 			if (*fmt == 0)
167 				badnoconv();
168 			if (*fmt == '.' && isdigit(*++fmt)) {
169 				prec = atoi(fmt);
170 				while (isdigit(*++fmt));
171 			}
172 			switch(*fmt) {
173 			case 'c':
174 				bcnt += 1;
175 				break;
176 			case 'd': case 'i': case 'o': case 'u':
177 			case 'x': case 'X':
178 				bcnt += 4;
179 				break;
180 			case 'e': case 'E': case 'f': case 'g': case 'G':
181 				bcnt += 8;
182 				break;
183 			case 's':
184 				bcnt += prec;
185 				break;
186 			case '_':
187 				switch(*++fmt) {
188 				case 'c': case 'p': case 'u':
189 					bcnt += 1;
190 					break;
191 				}
192 			}
193 		}
194 		cursize += bcnt * fu->reps;
195 	}
196 	return (cursize);
197 }
198 
199 void
200 rewrite(FS *fs)
201 {
202 	enum { NOTOKAY, USEBCNT, USEPREC } sokay;
203 	PR *pr, **nextpr;
204 	FU *fu;
205 	unsigned char *p1, *p2, *fmtp;
206 	char savech, cs[3];
207 	int nconv, prec;
208 
209 	prec = 0;
210 
211 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
212 		/*
213 		 * Break each format unit into print units; each conversion
214 		 * character gets its own.
215 		 */
216 		nextpr = &fu->nextpr;
217 		for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
218 			if ((pr = calloc(1, sizeof(PR))) == NULL)
219 				err(1, NULL);
220 			*nextpr = pr;
221 
222 			/* Skip preceding text and up to the next % sign. */
223 			for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
224 
225 			/* Only text in the string. */
226 			if (!*p1) {
227 				pr->fmt = fmtp;
228 				pr->flags = F_TEXT;
229 				break;
230 			}
231 
232 			/*
233 			 * Get precision for %s -- if have a byte count, don't
234 			 * need it.
235 			 */
236 			if (fu->bcnt) {
237 				sokay = USEBCNT;
238 				/* Skip to conversion character. */
239 				while (*++p1 != 0 && strchr(spec, *p1) != NULL)
240 					;
241 				if (*p1 == 0)
242 					badnoconv();
243 			} else {
244 				/* Skip any special chars, field width. */
245 				while (*++p1 != 0 && strchr(spec + 1, *p1) != NULL)
246 					;
247 				if (*p1 == 0)
248 					badnoconv();
249 				if (*p1 == '.' && isdigit(*++p1)) {
250 					sokay = USEPREC;
251 					prec = atoi(p1);
252 					while (isdigit(*++p1));
253 				} else
254 					sokay = NOTOKAY;
255 			}
256 
257 			p2 = *p1 ? p1 + 1 : p1;	/* Set end pointer -- make sure
258 						 * that it's non-NUL/-NULL first
259 						 * though. */
260 			cs[0] = *p1;		/* Set conversion string. */
261 			cs[1] = '\0';
262 
263 			/*
264 			 * Figure out the byte count for each conversion;
265 			 * rewrite the format as necessary, set up blank-
266 			 * padding for end of data.
267 			 */
268 			switch(cs[0]) {
269 			case 'c':
270 				pr->flags = F_CHAR;
271 				switch(fu->bcnt) {
272 				case 0: case 1:
273 					pr->bcnt = 1;
274 					break;
275 				default:
276 					p1[1] = '\0';
277 					badcnt(p1);
278 				}
279 				break;
280 			case 'd': case 'i':
281 				pr->flags = F_INT;
282 				goto isint;
283 			case 'o': case 'u': case 'x': case 'X':
284 				pr->flags = F_UINT;
285 isint:				cs[2] = '\0';
286 				cs[1] = cs[0];
287 				cs[0] = 'q';
288 				switch(fu->bcnt) {
289 				case 0: case 4:
290 					pr->bcnt = 4;
291 					break;
292 				case 1:
293 					pr->bcnt = 1;
294 					break;
295 				case 2:
296 					pr->bcnt = 2;
297 					break;
298 				case 8:
299 					pr->bcnt = 8;
300 					break;
301 				default:
302 					p1[1] = '\0';
303 					badcnt(p1);
304 				}
305 				break;
306 			case 'e': case 'E': case 'f': case 'g': case 'G':
307 				pr->flags = F_DBL;
308 				switch(fu->bcnt) {
309 				case 0: case 8:
310 					pr->bcnt = 8;
311 					break;
312 				case 4:
313 					pr->bcnt = 4;
314 					break;
315 				default:
316 					if (fu->bcnt == sizeof(long double)) {
317 						cs[2] = '\0';
318 						cs[1] = cs[0];
319 						cs[0] = 'L';
320 						pr->bcnt = sizeof(long double);
321 					} else {
322 						p1[1] = '\0';
323 						badcnt(p1);
324 					}
325 				}
326 				break;
327 			case 's':
328 				pr->flags = F_STR;
329 				switch(sokay) {
330 				case NOTOKAY:
331 					badsfmt();
332 				case USEBCNT:
333 					pr->bcnt = fu->bcnt;
334 					break;
335 				case USEPREC:
336 					pr->bcnt = prec;
337 					break;
338 				}
339 				break;
340 			case '_':
341 				++p2;
342 				switch(p1[1]) {
343 				case 'A':
344 					endfu = fu;
345 					fu->flags |= F_IGNORE;
346 					/* FALLTHROUGH */
347 				case 'a':
348 					pr->flags = F_ADDRESS;
349 					++p2;
350 					switch(p1[2]) {
351 					case 'd': case 'o': case'x':
352 						cs[0] = 'q';
353 						cs[1] = p1[2];
354 						cs[2] = '\0';
355 						break;
356 					default:
357 						p1[3] = '\0';
358 						badconv(p1);
359 					}
360 					break;
361 				case 'c':
362 					pr->flags = F_C;
363 					/* cs[0] = 'c';	set in conv_c */
364 					goto isint2;
365 				case 'p':
366 					pr->flags = F_P;
367 					cs[0] = 'c';
368 					goto isint2;
369 				case 'u':
370 					pr->flags = F_U;
371 					/* cs[0] = 'c';	set in conv_u */
372 isint2:					switch(fu->bcnt) {
373 					case 0: case 1:
374 						pr->bcnt = 1;
375 						break;
376 					default:
377 						p1[2] = '\0';
378 						badcnt(p1);
379 					}
380 					break;
381 				default:
382 					p1[2] = '\0';
383 					badconv(p1);
384 				}
385 				break;
386 			default:
387 				p1[1] = '\0';
388 				badconv(p1);
389 			}
390 
391 			/*
392 			 * Copy to PR format string, set conversion character
393 			 * pointer, update original.
394 			 */
395 			savech = *p2;
396 			p1[0] = '\0';
397 			if (asprintf(&pr->fmt, "%s%s", fmtp, cs) == -1)
398 				err(1, NULL);
399 			*p2 = savech;
400 			pr->cchar = pr->fmt + (p1 - fmtp);
401 			fmtp = p2;
402 
403 			/* Only one conversion character if byte count. */
404 			if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
405 	    errx(1, "byte count with multiple conversion characters");
406 		}
407 		/*
408 		 * If format unit byte count not specified, figure it out
409 		 * so can adjust rep count later.
410 		 */
411 		if (!fu->bcnt)
412 			for (pr = fu->nextpr; pr; pr = pr->nextpr)
413 				fu->bcnt += pr->bcnt;
414 	}
415 	/*
416 	 * If the format string interprets any data at all, and it's
417 	 * not the same as the blocksize, and its last format unit
418 	 * interprets any data at all, and has no iteration count,
419 	 * repeat it as necessary.
420 	 *
421 	 * If, rep count is greater than 1, no trailing whitespace
422 	 * gets output from the last iteration of the format unit.
423 	 */
424 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
425 		if (!fu->nextfu && fs->bcnt < blocksize &&
426 		    !(fu->flags&F_SETREP) && fu->bcnt)
427 			fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
428 		if (fu->reps > 1) {
429 			for (pr = fu->nextpr;; pr = pr->nextpr)
430 				if (!pr->nextpr)
431 					break;
432 			for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
433 				p2 = isspace(*p1) ? p1 : NULL;
434 			if (p2)
435 				pr->nospace = p2;
436 		}
437 	}
438 #ifdef DEBUG
439 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
440 		(void)printf("fmt:");
441 		for (pr = fu->nextpr; pr; pr = pr->nextpr)
442 			(void)printf(" {%s}", pr->fmt);
443 		(void)printf("\n");
444 	}
445 #endif
446 }
447 
448 void
449 escape(char *p1)
450 {
451 	char *p2;
452 
453 	/* alphabetic escape sequences have to be done in place */
454 	for (p2 = p1;; p1++, p2++) {
455 		if (*p1 == '\\') {
456 			p1++;
457 			switch(*p1) {
458 			case '\0':
459 				*p2 = '\\';
460 				*++p2 = '\0';
461 				return;
462 			case 'a':
463 			     /* *p2 = '\a'; */
464 				*p2 = '\007';
465 				break;
466 			case 'b':
467 				*p2 = '\b';
468 				break;
469 			case 'f':
470 				*p2 = '\f';
471 				break;
472 			case 'n':
473 				*p2 = '\n';
474 				break;
475 			case 'r':
476 				*p2 = '\r';
477 				break;
478 			case 't':
479 				*p2 = '\t';
480 				break;
481 			case 'v':
482 				*p2 = '\v';
483 				break;
484 			default:
485 				*p2 = *p1;
486 				break;
487 			}
488 		} else {
489 			*p2 = *p1;
490 			if (*p1 == '\0')
491 				return;
492 		}
493 	}
494 }
495 
496 void
497 badcnt(const char *s)
498 {
499 	errx(1, "%s: bad byte count", s);
500 }
501 
502 void
503 badsfmt(void)
504 {
505 	errx(1, "%%s: requires a precision or a byte count");
506 }
507 
508 void
509 badfmt(const char *fmt)
510 {
511 	errx(1, "\"%s\": bad format", fmt);
512 }
513 
514 void
515 badconv(const char *ch)
516 {
517 	errx(1, "%%%s: bad conversion character", ch);
518 }
519 
520 void
521 badnoconv(void)
522 {
523 	errx(1, "missing conversion character");
524 }
525