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