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/cdefs.h> 33 #include <sys/types.h> 34 35 #include <assert.h> 36 #include <ctype.h> 37 #include <limits.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <wchar.h> 42 #include <wctype.h> 43 #include "hexdump.h" 44 45 void 46 conv_c(PR *pr, u_char *p, size_t bufsize) 47 { 48 char buf[10]; 49 char const *str; 50 wchar_t wc; 51 size_t clen, oclen; 52 int converr, pad, width; 53 u_char peekbuf[MB_LEN_MAX]; 54 u_char *op; 55 56 op = NULL; 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 if (p == peekbuf) { 109 /* 110 * We peeked ahead, but that didn't help -- 111 * we either got an illegal sequence or still 112 * can't complete; restore original character. 113 */ 114 oclen = 0; 115 p = op; 116 } 117 wc = *p; 118 clen = 1; 119 converr = 1; 120 } else if (clen == (size_t)-2) { 121 /* 122 * Incomplete character; peek ahead and see if we 123 * can complete it. 124 */ 125 oclen = bufsize; 126 op = p; 127 bufsize = peek(p = peekbuf, MB_CUR_MAX); 128 goto retry; 129 } 130 clen += oclen; 131 } else { 132 wc = *p; 133 clen = 1; 134 } 135 if (!converr && iswprint(wc)) { 136 if (!odmode) { 137 *pr->cchar = 'c'; 138 (void)printf(pr->fmt, (int)wc); 139 } else { 140 *pr->cchar = 'C'; 141 assert(strcmp(pr->fmt, "%3C") == 0); 142 width = wcwidth(wc); 143 assert(width >= 0); 144 pad = 3 - width; 145 if (pad < 0) 146 pad = 0; 147 (void)printf("%*s%C", pad, "", wc); 148 pr->mbleft = clen - 1; 149 } 150 } else { 151 (void)sprintf(buf, "%03o", (int)*p); 152 str = buf; 153 strpr: *pr->cchar = 's'; 154 (void)printf(pr->fmt, str); 155 } 156 } 157 158 void 159 conv_u(PR *pr, u_char *p) 160 { 161 static char const * list[] = { 162 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", 163 "bs", "ht", "lf", "vt", "ff", "cr", "so", "si", 164 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", 165 "can", "em", "sub", "esc", "fs", "gs", "rs", "us", 166 }; 167 168 /* od used nl, not lf */ 169 if (*p <= 0x1f) { 170 *pr->cchar = 's'; 171 if (odmode && *p == 0x0a) 172 (void)printf(pr->fmt, "nl"); 173 else 174 (void)printf(pr->fmt, list[*p]); 175 } else if (*p == 0x7f) { 176 *pr->cchar = 's'; 177 (void)printf(pr->fmt, "del"); 178 } else if (odmode && *p == 0x20) { /* od replaced space with sp */ 179 *pr->cchar = 's'; 180 (void)printf(pr->fmt, " sp"); 181 } else if (isprint(*p)) { 182 *pr->cchar = 'c'; 183 (void)printf(pr->fmt, *p); 184 } else { 185 *pr->cchar = 'x'; 186 (void)printf(pr->fmt, (int)*p); 187 } 188 } 189