xref: /openbsd/usr.bin/hexdump/conv.c (revision db3296cf)
1 /*	$OpenBSD: conv.c,v 1.7 2003/06/12 20:58:09 deraadt Exp $	*/
2 /*	$NetBSD: conv.c,v 1.7 2001/12/07 15:14:29 bjh21 Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 /*static char sccsid[] = "from: @(#)conv.c	5.4 (Berkeley) 6/1/90";*/
35 static char rcsid[] = "$OpenBSD: conv.c,v 1.7 2003/06/12 20:58:09 deraadt Exp $";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 
40 #include <stdio.h>
41 #include <ctype.h>
42 
43 #include "hexdump.h"
44 
45 void
46 conv_c(PR *pr, u_char *p)
47 {
48 	char buf[10];
49 	char const *str;
50 
51 	switch(*p) {
52 	case '\0':
53 		str = "\\0";
54 		goto strpr;
55 	/* case '\a': */
56 	case '\007':
57 		if (deprecated)		/* od didn't know about \a */
58 			break;
59 		str = "\\a";
60 		goto strpr;
61 	case '\b':
62 		str = "\\b";
63 		goto strpr;
64 	case '\f':
65 		str = "\\f";
66 		goto strpr;
67 	case '\n':
68 		str = "\\n";
69 		goto strpr;
70 	case '\r':
71 		str = "\\r";
72 		goto strpr;
73 	case '\t':
74 		str = "\\t";
75 		goto strpr;
76 	case '\v':
77 		if (deprecated)
78 			break;
79 		str = "\\v";
80 		goto strpr;
81 	default:
82 		break;
83 	}
84 	if (isprint(*p)) {
85 		*pr->cchar = 'c';
86 		(void)printf(pr->fmt, *p);
87 	} else {
88 		(void)snprintf(buf, sizeof buf, "%03o", (int)*p);
89 		str = buf;
90 strpr:		*pr->cchar = 's';
91 		(void)printf(pr->fmt, str);
92 	}
93 }
94 
95 void
96 conv_u(PR *pr, u_char *p)
97 {
98 	static const char *list[] = {
99 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
100 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
101 		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
102 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
103 	};
104 
105 						/* od used nl, not lf */
106 	if (*p <= 0x1f) {
107 		*pr->cchar = 's';
108 		if (deprecated && *p == 0x0a)
109 			(void)printf(pr->fmt, "nl");
110 		else
111 			(void)printf(pr->fmt, list[*p]);
112 	} else if (*p == 0x7f) {
113 		*pr->cchar = 's';
114 		(void)printf(pr->fmt, "del");
115 	} else if (deprecated && *p == 0x20) {	/* od replaced space with sp */
116 		*pr->cchar = 's';
117 		(void)printf(pr->fmt, " sp");
118 	} else if (isprint(*p)) {
119 		*pr->cchar = 'c';
120 		(void)printf(pr->fmt, *p);
121 	} else {
122 		*pr->cchar = 'x';
123 		(void)printf(pr->fmt, (int)*p);
124 	}
125 }
126