conv.c (3358B)
1 /* $OpenBSD: conv.c,v 1.10 2014/04/19 09:28:20 sobrado 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 #include <sys/types.h> 34 35 #include <stdio.h> 36 #include <ctype.h> 37 38 #include "hexdump.h" 39 40 void 41 conv_c(PR *pr, u_char *p) 42 { 43 char buf[10]; 44 char const *str; 45 46 switch(*p) { 47 case '\0': 48 str = "\\0"; 49 goto strpr; 50 /* case '\a': */ 51 case '\007': 52 if (odmode) /* od didn't know about \a */ 53 break; 54 str = "\\a"; 55 goto strpr; 56 case '\b': 57 str = "\\b"; 58 goto strpr; 59 case '\f': 60 str = "\\f"; 61 goto strpr; 62 case '\n': 63 str = "\\n"; 64 goto strpr; 65 case '\r': 66 str = "\\r"; 67 goto strpr; 68 case '\t': 69 str = "\\t"; 70 goto strpr; 71 case '\v': 72 if (odmode) 73 break; 74 str = "\\v"; 75 goto strpr; 76 default: 77 break; 78 } 79 if (isprint(*p)) { 80 *pr->cchar = 'c'; 81 (void)printf(pr->fmt, *p); 82 } else { 83 (void)snprintf(buf, sizeof buf, "%03o", (int)*p); 84 str = buf; 85 strpr: *pr->cchar = 's'; 86 (void)printf(pr->fmt, str); 87 } 88 } 89 90 void 91 conv_u(PR *pr, u_char *p) 92 { 93 static const char *list[] = { 94 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", 95 "bs", "ht", "lf", "vt", "ff", "cr", "so", "si", 96 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", 97 "can", "em", "sub", "esc", "fs", "gs", "rs", "us", 98 }; 99 100 /* od used nl, not lf */ 101 if (*p <= 0x1f) { 102 *pr->cchar = 's'; 103 if (odmode && *p == 0x0a) 104 (void)printf(pr->fmt, "nl"); 105 else 106 (void)printf(pr->fmt, list[*p]); 107 } else if (*p == 0x7f) { 108 *pr->cchar = 's'; 109 (void)printf(pr->fmt, "del"); 110 } else if (odmode && *p == 0x20) { /* od replaced space with sp */ 111 *pr->cchar = 's'; 112 (void)printf(pr->fmt, " sp"); 113 } else if (isprint(*p)) { 114 *pr->cchar = 'c'; 115 (void)printf(pr->fmt, *p); 116 } else { 117 *pr->cchar = 'x'; 118 (void)printf(pr->fmt, (int)*p); 119 } 120 }