2 * Helper functions for test programs.
3 * Copyright © 2012 Nick Bowler
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.
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.
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/>.
24 * Decode a hexadecimal string into a sequence of bytes. If there are an
25 * odd number of nibbles, treat the first character as the least significant
26 * nibble of the first byte. The result is written to the buffer specified by
27 * buf. At most n bytes are written to the buffer.
29 * Returns the number of bytes that would be written provided that n was large
30 * enough, or (size_t)-1 if the input is not valid.
32 size_t test_decode_hex(const char *hex, unsigned char *buf, size_t n)
34 size_t len, count = 0;
37 for (len = 0; hex[len]; len++) {
38 if (!isxdigit(hex[len]))
47 case 0: tmp[0] = *hex++; len--;
48 case 1: tmp[1] = *hex++; len--;
51 buf[count] = strtoul(tmp, NULL, 16);