Clean up RPM spec for newer distributions
[clumanager.git] / librhcm / binhex.c
blobe285e2a171e2d5d0b70c85f50c8e4575ef8aa329
1 /*
2 Copyright 2003 Red Hat, Inc.
4 The Red Hat Cluster Manager API Library is free software; you can
5 redistribute it and/or modify it under the terms of the GNU Lesser
6 General Public License as published by the Free Software Foundation;
7 either version 2.1 of the License, or (at your option) any later
8 version.
10 The Red Hat Cluster Manager API Library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA.
20 /** @file
21 * "fast?" Binary <-> ASCII Hexadecimal conversion functions.
23 #include <string.h>
25 static char tohex[16] = {
26 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
27 'a', 'b', 'c', 'd', 'e', 'f'
30 static char tobin[256] = {
31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0..23 */
32 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 23..47 */
33 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15,0, /* 48..71 */
34 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 72..95 */
35 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 96..119 */
36 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 120..143 */
37 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144..167 */
38 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 168..191 */
39 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 192..215 */
40 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 216..239 */
41 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 239..255 */
45 /**
46 * Convert binary array of bytes to human-readable hexadecimal.
48 * @param in Input buffer
49 * @param inlen Input buffer length
50 * @param out Output buffer
51 * @param outlen Output buffer length (>=2 * inlen)
52 * @return -1 on failure, # of bytes in out on success.
54 int
55 bin2hex(char *in, int inlen, char *out, int outlen)
57 char *inp = in, *outp = out;
58 int x;
60 if (inlen <= 0 || outlen <= 0)
61 return -1;
63 if (outlen < (inlen * 2))
64 return -1;
66 memset(out,0,outlen);
68 for (x=0; x<inlen; x++) {
69 outp[0] = tohex[(inp[0] >> 4 & 0xf)];
70 outp[1] = tohex[(inp[0] & 0xf)];
71 outp+=2;
72 inp++;
75 return (outp - out);
79 /**
80 * Convert human-readable hexadecimal to binary array of bytes
82 * @param in Input buffer
83 * @param inlen Input buffer length
84 * @param out Output buffer
85 * @param outlen Output buffer length (>=inlen / 2)
86 * @return -1 on failure, # of bytes in out on success.
88 int
89 hex2bin(char *in, int inlen, char *out, int outlen)
91 char *inp = in, *outp = out;
92 int x;
94 if (inlen <= 0 || outlen <= 0)
95 return -1;
97 if (inlen > (outlen * 2))
98 return -1;
100 memset(out,0,outlen);
102 for (x=0; x<inlen; x+=2) {
103 outp[0] = (tobin[(int)inp[0]] << 4) | tobin[(int)inp[1]];
104 outp++;
105 inp+=2;
108 return (outp - out);
111 #ifdef STANDALONE
112 #include <stdio.h>
115 main(int argc, char **argv)
117 char buf[256], buf2[128];
118 int o;
120 if (argc < 2) {
121 printf("usage: %s <string>\n", argv[0]);
122 return 1;
125 o = bin2hex(argv[1], strlen(argv[1]), buf, sizeof(buf));
126 printf(" -> hex -> %s (%d)\n", buf, o);
127 o = hex2bin(buf, strlen(buf), buf2, sizeof(buf2));
128 printf(" -> bin -> %s (%d)\n", buf2, o);
130 #endif