chiark / gitweb /
Use libsamplerate in disorder-normalize, if available. If it's not
[disorder] / lib / hex.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/hex.c @brief Hexadecimal encoding and decoding */
19
20 #include "common.h"
21
22 #include "hex.h"
23 #include "mem.h"
24 #include "log.h"
25
26 /** @brief Convert a byte sequence to hex
27  * @param ptr Pointer to first byte
28  * @param n Number of bytes
29  * @return Allocated string containing hexdump
30  */
31 char *hex(const uint8_t *ptr, size_t n) {
32   char *buf = xmalloc_noptr(n * 2 + 1), *p = buf;
33
34   while(n-- > 0)
35     p += sprintf(p, "%02x", (unsigned)*ptr++);
36   *p = 0;
37   return buf;
38 }
39
40 /** @brief Convert a character to its value as a hex digit
41  * @param c Character code
42  * @return Value has hex digit or -1
43  *
44  * The 'q' stands for 'quiet' - this function does not report errors.
45  */
46 int unhexdigitq(int c) {
47   switch(c) {
48   case '0': return 0;
49   case '1': return 1;
50   case '2': return 2;
51   case '3': return 3;
52   case '4': return 4;
53   case '5': return 5;
54   case '6': return 6;
55   case '7': return 7;
56   case '8': return 8;
57   case '9': return 9;
58   case 'a': case 'A': return 10;
59   case 'b': case 'B': return 11;
60   case 'c': case 'C': return 12;
61   case 'd': case 'D': return 13;
62   case 'e': case 'E': return 14;
63   case 'f': case 'F': return 15;
64   default: return -1;
65   }
66 }
67
68 /** @brief Convert a character to its value as a hex digit
69  * @param c Character code
70  * @return Value has hex digit or -1
71  *
72  * If the character is not a valid hex digit then an error is logged.
73  * See @ref unhexdigitq() if that is a problem.
74  */
75 int unhexdigit(int c) {
76   int d;
77
78   if((d = unhexdigitq(c)) < 0)
79     disorder_error(0, "invalid hex digit");
80   return d;
81 }
82
83 /** @brief Convert a hex string to bytes
84  * @param s Pointer to hex string
85  * @param np Where to store byte string length or NULL
86  * @return Allocated buffer, or 0
87  *
88  * @p s should point to a 0-terminated string containing an even number
89  * of hex digits.  They are converted to bytes and returned via the return
90  * value and optionally the length via @p np.
91  *
92  * On any error a message is logged and a null pointer is returned.
93  */
94 uint8_t *unhex(const char *s, size_t *np) {
95   size_t l;
96   uint8_t *buf, *p;
97   int d1, d2;
98
99   if((l = strlen(s)) & 1) {
100     disorder_error(0, "hex string has odd length");
101     return 0;
102   }
103   p = buf = xmalloc_noptr(l / 2);
104   while(*s) {
105     if((d1 = unhexdigit(*s++)) < 0) return 0;
106     if((d2 = unhexdigit(*s++)) < 0) return 0;
107     *p++ = d1 * 16 + d2;
108   }
109   if(np)
110     *np = l / 2;
111   return buf;
112 }
113
114 /*
115 Local Variables:
116 c-basic-offset:2
117 comment-column:40
118 End:
119 */