1*aa8aae0fSAntonio Huete Jimenez /*
2*aa8aae0fSAntonio Huete Jimenez * Copyright (c) 2013 The DragonFly Project. All rights reserved.
3*aa8aae0fSAntonio Huete Jimenez *
4*aa8aae0fSAntonio Huete Jimenez * Redistribution and use in source and binary forms, with or without
5*aa8aae0fSAntonio Huete Jimenez * modification, are permitted provided that the following conditions
6*aa8aae0fSAntonio Huete Jimenez * are met:
7*aa8aae0fSAntonio Huete Jimenez *
8*aa8aae0fSAntonio Huete Jimenez * 1. Redistributions of source code must retain the above copyright
9*aa8aae0fSAntonio Huete Jimenez * notice, this list of conditions and the following disclaimer.
10*aa8aae0fSAntonio Huete Jimenez * 2. Redistributions in binary form must reproduce the above copyright
11*aa8aae0fSAntonio Huete Jimenez * notice, this list of conditions and the following disclaimer in
12*aa8aae0fSAntonio Huete Jimenez * the documentation and/or other materials provided with the
13*aa8aae0fSAntonio Huete Jimenez * distribution.
14*aa8aae0fSAntonio Huete Jimenez * 3. Neither the name of The DragonFly Project nor the names of its
15*aa8aae0fSAntonio Huete Jimenez * contributors may be used to endorse or promote products derived
16*aa8aae0fSAntonio Huete Jimenez * from this software without specific, prior written permission.
17*aa8aae0fSAntonio Huete Jimenez *
18*aa8aae0fSAntonio Huete Jimenez * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*aa8aae0fSAntonio Huete Jimenez * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*aa8aae0fSAntonio Huete Jimenez * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*aa8aae0fSAntonio Huete Jimenez * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*aa8aae0fSAntonio Huete Jimenez * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*aa8aae0fSAntonio Huete Jimenez * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*aa8aae0fSAntonio Huete Jimenez * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*aa8aae0fSAntonio Huete Jimenez * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*aa8aae0fSAntonio Huete Jimenez * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*aa8aae0fSAntonio Huete Jimenez * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28*aa8aae0fSAntonio Huete Jimenez * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*aa8aae0fSAntonio Huete Jimenez * SUCH DAMAGE.
30*aa8aae0fSAntonio Huete Jimenez */
31*aa8aae0fSAntonio Huete Jimenez #include <sys/systm.h>
32*aa8aae0fSAntonio Huete Jimenez
33*aa8aae0fSAntonio Huete Jimenez #define MIN_OUTBLEN 3
34*aa8aae0fSAntonio Huete Jimenez
35*aa8aae0fSAntonio Huete Jimenez /*
36*aa8aae0fSAntonio Huete Jimenez * Take an array of u_char of size inlen and stores its hexadecimal representation
37*aa8aae0fSAntonio Huete Jimenez * into outb which is max size outlen. Use sep as separator.
38*aa8aae0fSAntonio Huete Jimenez *
39*aa8aae0fSAntonio Huete Jimenez * Returns the same outb buffer for ease of use when printing.
40*aa8aae0fSAntonio Huete Jimenez */
41*aa8aae0fSAntonio Huete Jimenez char *
hexncpy(const u_char * inb,int inlen,char * outb,int outlen,const char * sep)42*aa8aae0fSAntonio Huete Jimenez hexncpy(const u_char *inb, int inlen, char *outb, int outlen, const char *sep)
43*aa8aae0fSAntonio Huete Jimenez {
44*aa8aae0fSAntonio Huete Jimenez char hexdigit[] = "0123456789abcdef";
45*aa8aae0fSAntonio Huete Jimenez char *addr;
46*aa8aae0fSAntonio Huete Jimenez
47*aa8aae0fSAntonio Huete Jimenez /*
48*aa8aae0fSAntonio Huete Jimenez * In the case we pass invalid buffers or the size
49*aa8aae0fSAntonio Huete Jimenez * of the outb buffer isn't enough to print at least
50*aa8aae0fSAntonio Huete Jimenez * a single u_char in hex format we assert.
51*aa8aae0fSAntonio Huete Jimenez */
52*aa8aae0fSAntonio Huete Jimenez KKASSERT((outb != NULL && inb != NULL && outlen >= MIN_OUTBLEN));
53*aa8aae0fSAntonio Huete Jimenez
54*aa8aae0fSAntonio Huete Jimenez /* Save up our base address */
55*aa8aae0fSAntonio Huete Jimenez addr = outb;
56*aa8aae0fSAntonio Huete Jimenez for (; inlen > 0 && outlen >= MIN_OUTBLEN; --inlen, outlen -= 3) {
57*aa8aae0fSAntonio Huete Jimenez *outb++ = hexdigit[*inb >> 4];
58*aa8aae0fSAntonio Huete Jimenez *outb++ = hexdigit[*inb++ & 0xf];
59*aa8aae0fSAntonio Huete Jimenez if (sep)
60*aa8aae0fSAntonio Huete Jimenez *outb++ = *sep;
61*aa8aae0fSAntonio Huete Jimenez }
62*aa8aae0fSAntonio Huete Jimenez *--outb = 0;
63*aa8aae0fSAntonio Huete Jimenez
64*aa8aae0fSAntonio Huete Jimenez return addr;
65*aa8aae0fSAntonio Huete Jimenez }
66