chiark / gitweb /
Merge branch 'protogen'
[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
2e9ba080
RK
78 if((d = unhexdigitq(c)) < 0)
79 disorder_error(0, "invalid hex digit");
460b9539 80 return d;
81}
82
ed422be7
RK
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 */
460b9539 94uint8_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) {
2e9ba080 100 disorder_error(0, "hex string has odd length");
460b9539 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/*
115Local Variables:
116c-basic-offset:2
117comment-column:40
118End:
119*/