xref: /freebsd/usr.bin/hexdump/conv.c (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
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 <assert.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
41 #include <wctype.h>
42 #include "hexdump.h"
43 
44 void
45 conv_c(PR *pr, u_char *p, size_t bufsize)
46 {
47 	char buf[10];
48 	char const *str;
49 	wchar_t wc;
50 	size_t clen, oclen;
51 	int converr, pad, width;
52 	u_char peekbuf[MB_LEN_MAX];
53 	u_char *op;
54 
55 	op = NULL;
56 
57 	if (pr->mbleft > 0) {
58 		str = "**";
59 		pr->mbleft--;
60 		goto strpr;
61 	}
62 
63 	switch(*p) {
64 	case '\0':
65 		str = "\\0";
66 		goto strpr;
67 	/* case '\a': */
68 	case '\007':
69 		str = "\\a";
70 		goto strpr;
71 	case '\b':
72 		str = "\\b";
73 		goto strpr;
74 	case '\f':
75 		str = "\\f";
76 		goto strpr;
77 	case '\n':
78 		str = "\\n";
79 		goto strpr;
80 	case '\r':
81 		str = "\\r";
82 		goto strpr;
83 	case '\t':
84 		str = "\\t";
85 		goto strpr;
86 	case '\v':
87 		str = "\\v";
88 		goto strpr;
89 	default:
90 		break;
91 	}
92 	/*
93 	 * Multibyte characters are disabled for hexdump(1) for backwards
94 	 * compatibility and consistency (none of its other output formats
95 	 * recognize them correctly).
96 	 */
97 	converr = 0;
98 	if (odmode && MB_CUR_MAX > 1) {
99 		oclen = 0;
100 retry:
101 		clen = mbrtowc(&wc, p, bufsize, &pr->mbstate);
102 		if (clen == 0)
103 			clen = 1;
104 		else if (clen == (size_t)-1 || (clen == (size_t)-2 &&
105 		    p == peekbuf)) {
106 			memset(&pr->mbstate, 0, sizeof(pr->mbstate));
107 			if (p == peekbuf) {
108 				/*
109 				 * We peeked ahead, but that didn't help --
110 				 * we either got an illegal sequence or still
111 				 * can't complete; restore original character.
112 				 */
113 				oclen = 0;
114 				p = op;
115 			}
116 			wc = *p;
117 			clen = 1;
118 			converr = 1;
119 		} else if (clen == (size_t)-2) {
120 			/*
121 			 * Incomplete character; peek ahead and see if we
122 			 * can complete it.
123 			 */
124 			oclen = bufsize;
125 			op = p;
126 			bufsize = peek(p = peekbuf, MB_CUR_MAX);
127 			goto retry;
128 		}
129 		clen += oclen;
130 	} else {
131 		wc = *p;
132 		clen = 1;
133 	}
134 	if (!converr && iswprint(wc)) {
135 		if (!odmode) {
136 			*pr->cchar = 'c';
137 			(void)printf(pr->fmt, (int)wc);
138 		} else {
139 			*pr->cchar = 'C';
140 			assert(strcmp(pr->fmt, "%3C") == 0);
141 			width = wcwidth(wc);
142 			assert(width >= 0);
143 			pad = 3 - width;
144 			if (pad < 0)
145 				pad = 0;
146 			(void)printf("%*s%C", pad, "", wc);
147 			pr->mbleft = clen - 1;
148 		}
149 	} else {
150 		(void)sprintf(buf, "%03o", (int)*p);
151 		str = buf;
152 strpr:		*pr->cchar = 's';
153 		(void)printf(pr->fmt, str);
154 	}
155 }
156 
157 void
158 conv_u(PR *pr, u_char *p)
159 {
160 	static char const * list[] = {
161 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
162 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
163 		"dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
164 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
165 	};
166 
167 						/* od used nl, not lf */
168 	if (*p <= 0x1f) {
169 		*pr->cchar = 's';
170 		if (odmode && *p == 0x0a)
171 			(void)printf(pr->fmt, "nl");
172 		else
173 			(void)printf(pr->fmt, list[*p]);
174 	} else if (*p == 0x7f) {
175 		*pr->cchar = 's';
176 		(void)printf(pr->fmt, "del");
177 	} else if (odmode && *p == 0x20) {	/* od replaced space with sp */
178 		*pr->cchar = 's';
179 		(void)printf(pr->fmt, " sp");
180 	} else if (isprint(*p)) {
181 		*pr->cchar = 'c';
182 		(void)printf(pr->fmt, *p);
183 	} else {
184 		*pr->cchar = 'x';
185 		(void)printf(pr->fmt, (int)*p);
186 	}
187 }
188