1*8cfaf194Ssobrado /* $OpenBSD: conv.c,v 1.10 2014/04/19 09:28:20 sobrado Exp $ */
27baf058cSpvalchev /* $NetBSD: conv.c,v 1.7 2001/12/07 15:14:29 bjh21 Exp $ */
31258a77dSderaadt
4df930be7Sderaadt /*
57baf058cSpvalchev * Copyright (c) 1989, 1993
67baf058cSpvalchev * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt * modification, are permitted provided that the following conditions
10df930be7Sderaadt * are met:
11df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt * documentation and/or other materials provided with the distribution.
16f75387cbSmillert * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt * may be used to endorse or promote products derived from this software
18df930be7Sderaadt * without specific prior written permission.
19df930be7Sderaadt *
20df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt * SUCH DAMAGE.
31df930be7Sderaadt */
32df930be7Sderaadt
33df930be7Sderaadt #include <sys/types.h>
347baf058cSpvalchev
35c3e1e82eSderaadt #include <stdio.h>
367baf058cSpvalchev #include <ctype.h>
377baf058cSpvalchev
38df930be7Sderaadt #include "hexdump.h"
39df930be7Sderaadt
40c3e1e82eSderaadt void
conv_c(PR * pr,u_char * p)413921aef3Sderaadt conv_c(PR *pr, u_char *p)
42df930be7Sderaadt {
437baf058cSpvalchev char buf[10];
447baf058cSpvalchev char const *str;
45df930be7Sderaadt
46df930be7Sderaadt switch(*p) {
47df930be7Sderaadt case '\0':
48df930be7Sderaadt str = "\\0";
49df930be7Sderaadt goto strpr;
50df930be7Sderaadt /* case '\a': */
51df930be7Sderaadt case '\007':
52*8cfaf194Ssobrado if (odmode) /* od didn't know about \a */
53df930be7Sderaadt break;
54df930be7Sderaadt str = "\\a";
55df930be7Sderaadt goto strpr;
56df930be7Sderaadt case '\b':
57df930be7Sderaadt str = "\\b";
58df930be7Sderaadt goto strpr;
59df930be7Sderaadt case '\f':
60df930be7Sderaadt str = "\\f";
61df930be7Sderaadt goto strpr;
62df930be7Sderaadt case '\n':
63df930be7Sderaadt str = "\\n";
64df930be7Sderaadt goto strpr;
65df930be7Sderaadt case '\r':
66df930be7Sderaadt str = "\\r";
67df930be7Sderaadt goto strpr;
68df930be7Sderaadt case '\t':
69df930be7Sderaadt str = "\\t";
70df930be7Sderaadt goto strpr;
71df930be7Sderaadt case '\v':
72*8cfaf194Ssobrado if (odmode)
73df930be7Sderaadt break;
74df930be7Sderaadt str = "\\v";
75df930be7Sderaadt goto strpr;
76df930be7Sderaadt default:
77df930be7Sderaadt break;
78df930be7Sderaadt }
79df930be7Sderaadt if (isprint(*p)) {
80df930be7Sderaadt *pr->cchar = 'c';
81df930be7Sderaadt (void)printf(pr->fmt, *p);
82df930be7Sderaadt } else {
83334e018bSderaadt (void)snprintf(buf, sizeof buf, "%03o", (int)*p);
847baf058cSpvalchev str = buf;
85df930be7Sderaadt strpr: *pr->cchar = 's';
86df930be7Sderaadt (void)printf(pr->fmt, str);
87df930be7Sderaadt }
88df930be7Sderaadt }
89df930be7Sderaadt
90c3e1e82eSderaadt void
conv_u(PR * pr,u_char * p)913921aef3Sderaadt conv_u(PR *pr, u_char *p)
92df930be7Sderaadt {
937baf058cSpvalchev static const char *list[] = {
94df930be7Sderaadt "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
95df930be7Sderaadt "bs", "ht", "lf", "vt", "ff", "cr", "so", "si",
96ac6012cdSkrw "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
97df930be7Sderaadt "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
98df930be7Sderaadt };
99df930be7Sderaadt
100df930be7Sderaadt /* od used nl, not lf */
101df930be7Sderaadt if (*p <= 0x1f) {
102df930be7Sderaadt *pr->cchar = 's';
103*8cfaf194Ssobrado if (odmode && *p == 0x0a)
104df930be7Sderaadt (void)printf(pr->fmt, "nl");
105df930be7Sderaadt else
106df930be7Sderaadt (void)printf(pr->fmt, list[*p]);
107df930be7Sderaadt } else if (*p == 0x7f) {
108df930be7Sderaadt *pr->cchar = 's';
109df930be7Sderaadt (void)printf(pr->fmt, "del");
110*8cfaf194Ssobrado } else if (odmode && *p == 0x20) { /* od replaced space with sp */
111df930be7Sderaadt *pr->cchar = 's';
112df930be7Sderaadt (void)printf(pr->fmt, " sp");
113df930be7Sderaadt } else if (isprint(*p)) {
114df930be7Sderaadt *pr->cchar = 'c';
115df930be7Sderaadt (void)printf(pr->fmt, *p);
116df930be7Sderaadt } else {
117df930be7Sderaadt *pr->cchar = 'x';
118df930be7Sderaadt (void)printf(pr->fmt, (int)*p);
119df930be7Sderaadt }
120df930be7Sderaadt }
121