1*0a6a1f1dSLionel Sambuc /* $NetBSD: hex.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2004-2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc
37ebfedea0SLionel Sambuc #include <config.h>
38ebfedea0SLionel Sambuc #include <krb5/roken.h>
39ebfedea0SLionel Sambuc #include <ctype.h>
40ebfedea0SLionel Sambuc #include <krb5/hex.h>
41ebfedea0SLionel Sambuc
42*0a6a1f1dSLionel Sambuc static const char hexchar[16] = "0123456789ABCDEF";
43ebfedea0SLionel Sambuc
44ebfedea0SLionel Sambuc static int
pos(char c)45ebfedea0SLionel Sambuc pos(char c)
46ebfedea0SLionel Sambuc {
47ebfedea0SLionel Sambuc const char *p;
48ebfedea0SLionel Sambuc c = toupper((unsigned char)c);
49ebfedea0SLionel Sambuc for (p = hexchar; *p; p++)
50ebfedea0SLionel Sambuc if (*p == c)
51ebfedea0SLionel Sambuc return p - hexchar;
52ebfedea0SLionel Sambuc return -1;
53ebfedea0SLionel Sambuc }
54ebfedea0SLionel Sambuc
55ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
hex_encode(const void * data,size_t size,char ** str)56ebfedea0SLionel Sambuc hex_encode(const void *data, size_t size, char **str)
57ebfedea0SLionel Sambuc {
58ebfedea0SLionel Sambuc const unsigned char *q = data;
59ebfedea0SLionel Sambuc size_t i;
60ebfedea0SLionel Sambuc char *p;
61ebfedea0SLionel Sambuc
62ebfedea0SLionel Sambuc /* check for overflow */
63ebfedea0SLionel Sambuc if (size * 2 < size) {
64ebfedea0SLionel Sambuc *str = NULL;
65ebfedea0SLionel Sambuc return -1;
66ebfedea0SLionel Sambuc }
67ebfedea0SLionel Sambuc
68ebfedea0SLionel Sambuc p = malloc(size * 2 + 1);
69ebfedea0SLionel Sambuc if (p == NULL) {
70ebfedea0SLionel Sambuc *str = NULL;
71ebfedea0SLionel Sambuc return -1;
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc for (i = 0; i < size; i++) {
75ebfedea0SLionel Sambuc p[i * 2] = hexchar[(*q >> 4) & 0xf];
76ebfedea0SLionel Sambuc p[i * 2 + 1] = hexchar[*q & 0xf];
77ebfedea0SLionel Sambuc q++;
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc p[i * 2] = '\0';
80ebfedea0SLionel Sambuc *str = p;
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc return i * 2;
83ebfedea0SLionel Sambuc }
84ebfedea0SLionel Sambuc
85ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
hex_decode(const char * str,void * data,size_t len)86ebfedea0SLionel Sambuc hex_decode(const char *str, void *data, size_t len)
87ebfedea0SLionel Sambuc {
88ebfedea0SLionel Sambuc size_t l;
89ebfedea0SLionel Sambuc unsigned char *p = data;
90ebfedea0SLionel Sambuc size_t i;
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc l = strlen(str);
93ebfedea0SLionel Sambuc
94ebfedea0SLionel Sambuc /* check for overflow, same as (l+1)/2 but overflow safe */
95ebfedea0SLionel Sambuc if ((l/2) + (l&1) > len)
96ebfedea0SLionel Sambuc return -1;
97ebfedea0SLionel Sambuc
98ebfedea0SLionel Sambuc if (l & 1) {
99ebfedea0SLionel Sambuc p[0] = pos(str[0]);
100ebfedea0SLionel Sambuc str++;
101ebfedea0SLionel Sambuc p++;
102ebfedea0SLionel Sambuc }
103ebfedea0SLionel Sambuc for (i = 0; i < l / 2; i++)
104ebfedea0SLionel Sambuc p[i] = pos(str[i * 2]) << 4 | pos(str[(i * 2) + 1]);
105ebfedea0SLionel Sambuc return i + (l & 1);
106ebfedea0SLionel Sambuc }
107