From: John Marino Date: Sat, 3 Nov 2012 15:32:51 +0000 (+0100) Subject: libutil: Implement hexdump(3) X-Git-Tag: v3.4.0rc~911 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/6d29a5802dc3385563ba87437bff1618489f15a6 libutil: Implement hexdump(3) Similar to hexdump(9) but available to userland. Taken-from: FreeBSD SVN 180161 (01 JUL 2008) FreeBSD SVN 185729 (06 DEC 2008) --- diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index 97d5370834..3d6dcdb3be 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -12,7 +12,7 @@ SRCS= flopen.c login.c login_tty.c logout.c logwtmp.c logwtmpx.c pty.c \ realhostname.c fparseln.c stub.c pidfile.c trimdomain.c \ dehumanize_number.c humanize_number.c humanize_unsigned.c pw_util.c \ efun.c getmntopts.c -SRCS+= gr_util.c +SRCS+= gr_util.c hexdump.c INCS= libutil.h login_cap.h INCSLINKS=libutil.h ${INCLUDEDIR}/util.h @@ -28,6 +28,7 @@ MAN+= flopen.3 login.3 loginx.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 \ humanize_number.3 humanize_unsigned.3 \ efun.3 getmntopts.3 MAN+= login.conf.5 auth.conf.5 +MAN+= hexdump.3 MLINKS+=auth.3 auth_getval.3 MLINKS+=efun.3 ecalloc.3 efun.3 emalloc.3 efun.3 erealloc.3 efun.3 esetfunc.3 \ diff --git a/lib/libutil/hexdump.3 b/lib/libutil/hexdump.3 new file mode 100644 index 0000000000..8e997ac24b --- /dev/null +++ b/lib/libutil/hexdump.3 @@ -0,0 +1,94 @@ +.\" -*- nroff -*- +.\" +.\" Copyright (c) 2003 Scott Long +.\" +.\" All rights reserved. +.\" +.\" This program is free software. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd July 1, 2008 +.Dt HEXDUMP 3 +.Os +.Sh NAME +.Nm hexdump +.Nd "dump a block of bytes to standard out in hexadecimal form" +.Sh SYNOPSIS +.In libutil.h +.Ft void +.Fn hexdump "void *ptr" "int length" "const char *hdr" "int flags" +.Sh DESCRIPTION +The +.Fn hexdump +function prints an array of bytes to standard out in hexadecimal form, +along with the +.Tn ASCII +representation of the bytes, if possible. +By default, each line of +output will start with an offset count, followed by 16 hexadecimal values, +followed by 16 +.Tn ASCII +characters. +.Bl -tag -width indent +.It Fa ptr +Pointer to the array of bytes to print. +It does not need to be +.Dv NUL Ns +-terminated. +.It Fa length +Number of bytes to print. +.It Fa hdr +Pointer to a +.Dv NUL Ns +-terminated character string that will be prepended to each +line of output. +A value of +.Dv NULL +implies that no header will be printed. +.It Fa flags +Flags for controlling the formatting of the output. +.Bl -tag -width ".Dv HD_OMIT_COUNT" +.It Bits 0-7 +Integer value of the number of bytes to display on each line. +A value of 0 implies that the default value of 16 will be used. +.It Bits 8-15 +Character +.Tn ASCII +value to use as the separator for the hexadecimal output. +A value of 0 implies that the default value of 32 +.Tn ( ASCII +space) will be used. +.It Dv HD_OMIT_COUNT +Do not print the offset column at the beginning of each line. +.It Dv HD_OMIT_HEX +Do not print the hexadecimal values on each line. +.It Dv HD_OMIT_CHARS +Do not print the character values on each line. +.El +.El +.Sh SEE ALSO +.Xr ascii 7 +.Sh AUTHORS +This manual page was written by +.An Scott Long . diff --git a/lib/libutil/hexdump.c b/lib/libutil/hexdump.c new file mode 100644 index 0000000000..6eb559b1af --- /dev/null +++ b/lib/libutil/hexdump.c @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 1986, 1988, 1991, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 + */ + +#include +#include +#include + +void +hexdump(const void *ptr, int length, const char *hdr, int flags) +{ + int i, j, k; + int cols; + const unsigned char *cp; + char delim; + + if ((flags & HD_DELIM_MASK) != 0) + delim = (flags & HD_DELIM_MASK) >> 8; + else + delim = ' '; + + if ((flags & HD_COLUMN_MASK) != 0) + cols = flags & HD_COLUMN_MASK; + else + cols = 16; + + cp = ptr; + for (i = 0; i < length; i+= cols) { + if (hdr != NULL) + printf("%s", hdr); + + if ((flags & HD_OMIT_COUNT) == 0) + printf("%04x ", i); + + if ((flags & HD_OMIT_HEX) == 0) { + for (j = 0; j < cols; j++) { + k = i + j; + if (k < length) + printf("%c%02x", delim, cp[k]); + else + printf(" "); + } + } + + if ((flags & HD_OMIT_CHARS) == 0) { + printf(" |"); + for (j = 0; j < cols; j++) { + k = i + j; + if (k >= length) + printf(" "); + else if (cp[k] >= ' ' && cp[k] <= '~') + printf("%c", cp[k]); + else + printf("."); + } + printf("|"); + } + printf("\n"); + } +} + diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index 1688ea4f99..ad74a26440 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -77,6 +77,7 @@ void trimdomain(char *, int); int openpty(int *, int *, char *, struct termios *, struct winsize *); int forkpty(int *, char *, struct termios *, struct winsize *); int dehumanize_number(const char *, int64_t *); +void hexdump(const void *_ptr, int _length, const char *_hdr, int _flags); int humanize_number(char *, size_t, int64_t, const char *, int, int); int humanize_unsigned(char *buf, size_t len, uint64_t bytes, const char *suffix, int divisor); @@ -153,6 +154,13 @@ __END_DECLS #define FPARSELN_UNESCREST 0x08 #define FPARSELN_UNESCALL 0x0f +/* Flags for hexdump(3). */ +#define HD_COLUMN_MASK 0xff +#define HD_DELIM_MASK 0xff00 +#define HD_OMIT_COUNT (1 << 16) +#define HD_OMIT_HEX (1 << 17) +#define HD_OMIT_CHARS (1 << 18) + /* Values for humanize_number(3)'s flags parameter. */ #define HN_DECIMAL 0x01 #define HN_NOSPACE 0x02