From aa8aae0ff1e737da9203f967b367e5c42863ee55 Mon Sep 17 00:00:00 2001 From: Antonio Huete Jimenez Date: Sat, 12 Jan 2013 21:45:20 +0100 Subject: [PATCH] libkern - Add new function hexncpy. --- sys/conf/files | 1 + sys/libkern/hexncpy.c | 65 +++++++++++++++++++++++++++++++++++++++++++ sys/sys/libkern.h | 7 +++++ 3 files changed, 73 insertions(+) create mode 100644 sys/libkern/hexncpy.c diff --git a/sys/conf/files b/sys/conf/files index 63323ca525..29f693bbcf 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1845,6 +1845,7 @@ libkern/strlcpy.c standard libkern/strlen.c standard libkern/strncmp.c standard libkern/strncpy.c standard +libkern/hexncpy.c standard libkern/strsep.c standard libkern/strtol.c standard libkern/strtoq.c standard diff --git a/sys/libkern/hexncpy.c b/sys/libkern/hexncpy.c new file mode 100644 index 0000000000..187c7fd3de --- /dev/null +++ b/sys/libkern/hexncpy.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2013 The DragonFly Project. All rights reserved. + * + * 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. + * 3. Neither the name of The DragonFly Project 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDERS 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. + */ +#include + +#define MIN_OUTBLEN 3 + +/* + * Take an array of u_char of size inlen and stores its hexadecimal representation + * into outb which is max size outlen. Use sep as separator. + * + * Returns the same outb buffer for ease of use when printing. + */ +char * +hexncpy(const u_char *inb, int inlen, char *outb, int outlen, const char *sep) +{ + char hexdigit[] = "0123456789abcdef"; + char *addr; + + /* + * In the case we pass invalid buffers or the size + * of the outb buffer isn't enough to print at least + * a single u_char in hex format we assert. + */ + KKASSERT((outb != NULL && inb != NULL && outlen >= MIN_OUTBLEN)); + + /* Save up our base address */ + addr = outb; + for (; inlen > 0 && outlen >= MIN_OUTBLEN; --inlen, outlen -= 3) { + *outb++ = hexdigit[*inb >> 4]; + *outb++ = hexdigit[*inb++ & 0xf]; + if (sep) + *outb++ = *sep; + } + *--outb = 0; + + return addr; +} diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index a44d0f2486..2b74a6d4be 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -65,6 +65,12 @@ extern char const hex2ascii_data[]; #define bin2bcd(bin) (bin2bcd_data[bin]) #define hex2ascii(hex) (hex2ascii_data[hex]) +/* + * Number of bytes needed for hexadecimal representation + * plus trailing \0 + */ +#define HEX_NCPYLEN(s) (s * 3) + static __inline int imax(int a, int b) { return (a > b ? a : b); } static __inline int imin(int a, int b) { return (a < b ? a : b); } static __inline long lmax(long a, long b) { return (a > b ? a : b); } @@ -125,6 +131,7 @@ char *strncpy (char * __restrict, const char * __restrict, size_t); char *strsep(char **, const char *); int _kfnmatch(const char *, const char *, int, int); void * memcchr(const void *, int, size_t); +char * hexncpy(const u_char *, int, char *, int, const char *); /* * memset can't be an inline, it is used by gcc-4.x directly. -- 2.41.0