1178825Sdfr/*
2178825Sdfr * Copyright (c) 2004-2005 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
5178825Sdfr *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
9178825Sdfr *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
18178825Sdfr *    may be used to endorse or promote products derived from this software
19178825Sdfr *    without specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178825Sdfr * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr#ifdef HAVE_CONFIG_H
35178825Sdfr#include <config.h>
36178825SdfrRCSID("$Id: hex.c 16504 2006-01-09 17:09:29Z lha $");
37178825Sdfr#endif
38178825Sdfr#include "roken.h"
39178825Sdfr#include <ctype.h>
40178825Sdfr#include "hex.h"
41178825Sdfr
42178825Sdfrconst static char hexchar[] = "0123456789ABCDEF";
43178825Sdfr
44178825Sdfrstatic int
45178825Sdfrpos(char c)
46178825Sdfr{
47178825Sdfr    const char *p;
48178825Sdfr    c = toupper((unsigned char)c);
49178825Sdfr    for (p = hexchar; *p; p++)
50178825Sdfr	if (*p == c)
51178825Sdfr	    return p - hexchar;
52178825Sdfr    return -1;
53178825Sdfr}
54178825Sdfr
55178825Sdfrssize_t ROKEN_LIB_FUNCTION
56178825Sdfrhex_encode(const void *data, size_t size, char **str)
57178825Sdfr{
58178825Sdfr    const unsigned char *q = data;
59178825Sdfr    size_t i;
60178825Sdfr    char *p;
61178825Sdfr
62178825Sdfr    /* check for overflow */
63178825Sdfr    if (size * 2 < size)
64178825Sdfr	return -1;
65178825Sdfr
66178825Sdfr    p = malloc(size * 2 + 1);
67178825Sdfr    if (p == NULL)
68178825Sdfr	return -1;
69178825Sdfr
70178825Sdfr    for (i = 0; i < size; i++) {
71178825Sdfr	p[i * 2] = hexchar[(*q >> 4) & 0xf];
72178825Sdfr	p[i * 2 + 1] = hexchar[*q & 0xf];
73178825Sdfr	q++;
74178825Sdfr    }
75178825Sdfr    p[i * 2] = '\0';
76178825Sdfr    *str = p;
77178825Sdfr
78178825Sdfr    return i * 2;
79178825Sdfr}
80178825Sdfr
81178825Sdfrssize_t ROKEN_LIB_FUNCTION
82178825Sdfrhex_decode(const char *str, void *data, size_t len)
83178825Sdfr{
84178825Sdfr    size_t l;
85178825Sdfr    unsigned char *p = data;
86178825Sdfr    size_t i;
87178825Sdfr
88178825Sdfr    l = strlen(str);
89178825Sdfr
90178825Sdfr    /* check for overflow, same as (l+1)/2 but overflow safe */
91178825Sdfr    if ((l/2) + (l&1) > len)
92178825Sdfr	return -1;
93178825Sdfr
94178825Sdfr    i = 0;
95178825Sdfr    if (l & 1) {
96178825Sdfr	p[0] = pos(str[0]);
97178825Sdfr	str++;
98178825Sdfr	p++;
99178825Sdfr    }
100178825Sdfr    for (i = 0; i < l / 2; i++)
101178825Sdfr	p[i] = pos(str[i * 2]) << 4 | pos(str[(i * 2) + 1]);
102178825Sdfr    return i + (l & 1);
103178825Sdfr}
104