1 /* modhex.c --- Simple ModHex conversion command line tool.
2 *
3 * Written by Simon Josefsson <simon@josefsson.org>.
4 * Copyright (c) 2006-2012 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 #include <stdio.h>
36 #include <stdlib.h>
37
38 int
main(int argc,char * argv[])39 main (int argc, char *argv[])
40 {
41 int argi;
42 int decode_p = 0;
43 int hex_p = 0;
44 char *data;
45 size_t inlen;
46
47 /* Usage. */
48 if (argc < 2)
49 {
50 printf ("Usage: %s [-dh] <data>\n", argv[0]);
51 printf ("\n");
52 printf
53 ("Convert input DATA as specified and print output to STDOUT.\n");
54 printf ("\n");
55 printf (" -d: Decode data (the default is to encode data).\n");
56 printf (" -h: Use hex encoding for all non-modhex data.\n");
57 printf (" DATA: string with data to encode\n");
58 printf ("\n");
59 printf ("Examples:\n");
60 printf ("\n");
61 printf (" ModHex encode ASCII-string \"test\":\n");
62 printf (" %s test\n", argv[0]);
63 printf ("\n");
64 printf (" Decode ModHex data \"ifhgieif\" into ASCII string:\n");
65 printf (" %s -d ifhgieif\n", argv[0]);
66 printf ("\n");
67 printf (" ModHex encode hex-encoded data \"b565716f\":\n");
68 printf (" %s -h b565716f\n", argv[0]);
69 printf ("\n");
70 printf
71 (" Decode ModHex data \"nghgibhv\" and print hex-encode data:\n");
72 printf (" %s -d -h nghgibhv\n", argv[0]);
73 return 1;
74 }
75
76 /* Command line handling. */
77
78 argi = 1;
79 while (argc > argi && (!decode_p || !hex_p))
80 {
81 if (strcmp (argv[argi], "-d") == 0)
82 {
83 decode_p = 1;
84 argi++;
85 }
86 else if (strcmp (argv[argi], "-h") == 0)
87 {
88 hex_p = 1;
89 argi++;
90 }
91 else
92 break;
93 }
94
95 if (argc < argi + 1)
96 {
97 printf ("error: missing argument\n");
98 return 1;
99 }
100
101 data = argv[argi];
102 inlen = strlen (data);
103
104 if (!decode_p && hex_p)
105 {
106 unsigned long i;
107 char *tmp;
108
109 if (inlen & 1)
110 {
111 printf ("error: size of hex encoded input not even\n");
112 return 1;
113 }
114
115 tmp = malloc (inlen / 2 + 1);
116 if (!tmp)
117 {
118 perror ("malloc");
119 return 1;
120 }
121
122 for (i = 0; i < inlen; i++)
123 {
124 const char *hex = "0123456789abcdef";
125 char *p;
126
127 if ((p = strchr (hex, data[i])) == NULL)
128 {
129 printf ("error: input not hex encoded at position %lu\n", i);
130 free (tmp);
131 return 1;
132 }
133
134 if (i & 1)
135 tmp[i / 2] |= p - hex;
136 else
137 tmp[i / 2] = (p - hex) << 4;
138 }
139 tmp[i / 2] = '\0';
140
141 inlen = inlen / 2;
142
143 data = tmp;
144 }
145
146 /* Doit. */
147
148 {
149 size_t i;
150 char *buf = malloc (2 * inlen + 1);
151
152 if (!buf)
153 {
154 perror ("malloc");
155 return 1;
156 }
157
158 if (decode_p)
159 yubikey_modhex_decode (buf, data, inlen / 2);
160 else
161 yubikey_modhex_encode (buf, data, inlen);
162
163 if (decode_p && hex_p)
164 {
165 for (i = 0; i < inlen / 2; i++)
166 printf ("%02x", buf[i] & 0xFF);
167 printf ("\n");
168 }
169 else if (decode_p)
170 fwrite (buf, inlen / 2, 1, stdout);
171 else
172 printf ("%s\n", buf);
173
174 free (buf);
175 }
176
177 if (!decode_p && hex_p)
178 free (data);
179
180 return 0;
181 }
182