summaryrefslogtreecommitdiffstats
path: root/src/lib/hex-binary.c
blob: a6d78490effd32ef6f0a7e202dc2af3e7fa7d824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "buffer.h"
#include "hex-binary.h"

static void
binary_to_hex_case(unsigned char *dest, const unsigned char *data,
		   size_t size, bool ucase)
{
	unsigned char *p;
	char base_char;
	size_t i;
	int value;

	/* @UNSAFE */
	base_char = ucase ? 'A' : 'a';

	p = dest;
	for (i = 0; i < size; i++) {
		value = data[i] >> 4;
		*p++ = value < 10 ? value + '0' : value - 10 + base_char;

		value = data[i] & 0x0f;
		*p++ = value < 10 ? value + '0' : value - 10 + base_char;
	}
}

const char *binary_to_hex(const unsigned char *data, size_t size)
{
	unsigned char *dest = t_malloc_no0(MALLOC_MULTIPLY(size, 2) + 1);

	binary_to_hex_case(dest, data, size, FALSE);
	dest[size*2] = '\0';
	return (char *)dest;
}

const char *binary_to_hex_ucase(const unsigned char *data, size_t size)
{
	unsigned char *dest = t_malloc_no0(MALLOC_MULTIPLY(size, 2) + 1);

	binary_to_hex_case(dest, data, size, TRUE);
	dest[size*2] = '\0';
	return (char *)dest;
}

void binary_to_hex_append(string_t *dest, const unsigned char *data,
			  size_t size)
{
	unsigned char *buf;

	buf = buffer_append_space_unsafe(dest, size * 2);
	binary_to_hex_case(buf, data, size, FALSE);
}

int hex_to_binary(const char *data, buffer_t *dest)
{
	int value;

	while (*data != '\0') {
		if (*data >= '0' && *data <= '9')
			value = (*data - '0') << 4;
		else if (*data >= 'a' && *data <= 'f')
			value = (*data - 'a' + 10) << 4;
		else if (*data >= 'A' && *data <= 'F')
			value = (*data - 'A' + 10) << 4;
		else
			return -1;

		data++;
		if (*data >= '0' && *data <= '9')
			value |= *data - '0';
		else if (*data >= 'a' && *data <= 'f')
			value |= *data - 'a' + 10;
		else if (*data >= 'A' && *data <= 'F')
			value |= *data - 'A' + 10;
		else
			return -1;

		buffer_append_c(dest, value);
		data++;
	}

	return 0;
}