chiark / gitweb /
Fix mis-merged trackdb_open().
[disorder] / lib / hex.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
ed422be7 18/** @file lib/hex.c @brief Hexadecimal encoding and decoding */
460b9539 19
05b75f8d 20#include "common.h"
460b9539 21
22#include "hex.h"
23#include "mem.h"
24#include "log.h"
25
ed422be7
RK
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 */
460b9539 31char *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++);
ef582771 36 *p = 0;
460b9539 37 return buf;
38}
39
ed422be7
RK
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 */
460b9539 46int 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
ed422be7
RK
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 */
460b9539 75int unhexdigit(int c) {
76 int d;
77
78 if((d = unhexdigitq(c)) < 0) error(0, "invalid hex digit");
79 return d;
80}
81
ed422be7
RK
82/** @brief Convert a hex string to bytes
83 * @param s Pointer to hex string
84 * @param np Where to store byte string length or NULL
85 * @return Allocated buffer, or 0
86 *
87 * @p s should point to a 0-terminated string containing an even number
88 * of hex digits. They are converted to bytes and returned via the return
89 * value and optionally the length via @p np.
90 *
91 * On any error a message is logged and a null pointer is returned.
92 */
460b9539 93uint8_t *unhex(const char *s, size_t *np) {
94 size_t l;
95 uint8_t *buf, *p;
96 int d1, d2;
97
98 if((l = strlen(s)) & 1) {
99 error(0, "hex string has odd length");
100 return 0;
101 }
102 p = buf = xmalloc_noptr(l / 2);
103 while(*s) {
104 if((d1 = unhexdigit(*s++)) < 0) return 0;
105 if((d2 = unhexdigit(*s++)) < 0) return 0;
106 *p++ = d1 * 16 + d2;
107 }
108 if(np)
109 *np = l / 2;
110 return buf;
111}
112
113/*
114Local Variables:
115c-basic-offset:2
116comment-column:40
117End:
118*/