1 /* ykhex.c --- Implementation of hex encoding/decoding
2 *
3 * Written by Simon Josefsson <simon@josefsson.org>.
4 * Copyright (c) 2006-2014 Yubico AB
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33 #include "yubikey.h"
34
35 static const char hex_trans[] = "0123456789abcdef";
36 static const char modhex_trans[] = YUBIKEY_MODHEX_MAP;
37
38 static void
_yubikey_encode(char * dst,const char * src,size_t srcSize,const char * trans)39 _yubikey_encode (char *dst, const char *src, size_t srcSize,
40 const char *trans)
41 {
42 while (srcSize--)
43 {
44 *dst++ = trans[(*src >> 4) & 0xf];
45 *dst++ = trans[*src++ & 0xf];
46 }
47
48 *dst = '\0';
49 }
50
51 static void
_yubikey_decode(char * dst,const char * src,size_t dstSize,const char * trans)52 _yubikey_decode (char *dst, const char *src, size_t dstSize,
53 const char *trans)
54 {
55 char b;
56 int flag = 0;
57 const char *p1;
58
59 if (strlen (src) % 2 == 1)
60 {
61 flag = !flag;
62 }
63
64 for (; *src && dstSize > 0; src++)
65 {
66 if ((p1 = strchr (trans, *src)) == NULL)
67 b = 0;
68 else
69 b = (char) (p1 - trans);
70
71 if ((flag = !flag))
72 *dst = b;
73 else
74 {
75 *dst = (*dst << 4) | b;
76 dst++;
77 dstSize--;
78 }
79 }
80 while (dstSize--)
81 *dst++ = 0;
82 }
83
84 static int
_yubikey_p(const char * str,const char * trans)85 _yubikey_p (const char *str, const char *trans)
86 {
87 for (; *str; str++)
88 if (strchr (trans, *str) == NULL)
89 return 0;
90
91 return 1;
92 }
93
94 void
yubikey_hex_encode(char * dst,const char * src,size_t srcSize)95 yubikey_hex_encode (char *dst, const char *src, size_t srcSize)
96 {
97 _yubikey_encode (dst, src, srcSize, hex_trans);
98 }
99
100 void
yubikey_hex_decode(char * dst,const char * src,size_t dstSize)101 yubikey_hex_decode (char *dst, const char *src, size_t dstSize)
102 {
103 _yubikey_decode (dst, src, dstSize, hex_trans);
104 }
105
106 int
yubikey_hex_p(const char * str)107 yubikey_hex_p (const char *str)
108 {
109 return _yubikey_p (str, hex_trans);
110 }
111
112
113 void
yubikey_modhex_encode(char * dst,const char * src,size_t srcSize)114 yubikey_modhex_encode (char *dst, const char *src, size_t srcSize)
115 {
116 _yubikey_encode (dst, src, srcSize, modhex_trans);
117 }
118
119 void
yubikey_modhex_decode(char * dst,const char * src,size_t dstSize)120 yubikey_modhex_decode (char *dst, const char *src, size_t dstSize)
121 {
122 _yubikey_decode (dst, src, dstSize, modhex_trans);
123 }
124
125 int
yubikey_modhex_p(const char * str)126 yubikey_modhex_p (const char *str)
127 {
128 return _yubikey_p (str, modhex_trans);
129 }
130