xref: /freebsd/usr.bin/hexdump/conv.c (revision 3df058ffaf72b8715c9a5a6a4cbaf1eac1910e43)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char sccsid[] = "@(#)conv.c	8.1 (Berkeley) 6/6/93";
32 #endif /* not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 
38 #include <assert.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <wchar.h>
45 #include <wctype.h>
46 #include "hexdump.h"
47 
48 void
49 conv_c(PR *pr, u_char *p, size_t bufsize)
50 {
51 	char buf[10];
52 	char const *str;
53 	wchar_t wc;
54 	size_t clen, oclen;
55 	int converr, pad, width;
56 	u_char peekbuf[MB_LEN_MAX];
57 
58 	if (pr->mbleft > 0) {
59 		str = "**";
60 		pr->mbleft--;
61 		goto strpr;
62 	}
63 
64 	switch(*p) {
65 	case '\0':
66 		str = "\\0";
67 		goto strpr;
68 	/* case '\a': */
69 	case '\007':
70 		str = "\\a";
71 		goto strpr;
72 	case '\b':
73 		str = "\\b";
74 		goto strpr;
75 	case '\f':
76 		str = "\\f";
77 		goto strpr;
78 	case '\n':
79 		str = "\\n";
80 		goto strpr;
81 	case '\r':
82 		str = "\\r";
83 		goto strpr;
84 	case '\t':
85 		str = "\\t";
86 		goto strpr;
87 	case '\v':
88 		str = "\\v";
89 		goto strpr;
90 	default:
91 		break;
92 	}
93 	/*
94 	 * Multibyte characters are disabled for hexdump(1) for backwards
95 	 * compatibility and consistency (none of its other output formats
96 	 * recognize them correctly).
97 	 */
98 	converr = 0;
99 	if (odmode && MB_CUR_MAX > 1) {
100 		oclen = 0;
101 retry:
102 		clen = mbrtowc(&wc, p, bufsize, &pr->mbstate);
103 		if (clen == 0)
104 			clen = 1;
105 		else if (clen == (size_t)-1 || (clen == (size_t)-2 &&
106 		    p == peekbuf)) {
107 			memset(&pr->mbstate, 0, sizeof(pr->mbstate));
108 			wc = *p;
109 			clen = 1;
110 			converr = 1;
111 		} else if (clen == (size_t)-2) {
112 			/*
113 			 * Incomplete character; peek ahead and see if we
114 			 * can complete it.
115 			 */
116 			oclen = bufsize;
117 			bufsize = peek(p = peekbuf, MB_CUR_MAX);
118 			goto retry;
119 		}
120 		clen += oclen;
121 	} else {
122 		wc = *p;
123 		clen = 1;
124 	}
125 	if (!converr && iswprint(wc)) {
126 		if (!odmode) {
127 			*pr->cchar = 'c';
128 			(void)printf(pr->fmt, (int)wc);
129 		} else {
130 			*pr->cchar = 'C';
131 			assert(strcmp(pr->fmt, "%3C") == 0);
132 			width = wcwidth(wc);
133 			assert(width >= 0);
134 			pad = 3 - width;
135 			if (pad < 0)
136 				pad = 0;
137 			(void)printf("%*s%C", pad, "", wc);
138 			pr->mbleft = clen - 1;
139 		}
140 	} else {
141 		(void)sprintf(buf, "%03o", (int)*p);
142 		str = buf;
143 strpr:		*pr->cchar = 's';
144 		(void)printf(pr->fmt, str);
145 	}
146 }
147 
148 void
149 conv_u(PR *pr, u_char *p)
150 {
151 	static char const * list[] = {
152 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
153 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
154 		"dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
155 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
156 	};
157 
158 						/* od used nl, not lf */
159 	if (*p <= 0x1f) {
160 		*pr->cchar = 's';
161 		if (odmode && *p == 0x0a)
162 			(void)printf(pr->fmt, "nl");
163 		else
164 			(void)printf(pr->fmt, list[*p]);
165 	} else if (*p == 0x7f) {
166 		*pr->cchar = 's';
167 		(void)printf(pr->fmt, "del");
168 	} else if (odmode && *p == 0x20) {	/* od replaced space with sp */
169 		*pr->cchar = 's';
170 		(void)printf(pr->fmt, " sp");
171 	} else if (isprint(*p)) {
172 		*pr->cchar = 'c';
173 		(void)printf(pr->fmt, *p);
174 	} else {
175 		*pr->cchar = 'x';
176 		(void)printf(pr->fmt, (int)*p);
177 	}
178 }
179