File: hex.c

package info (click to toggle)
p11-kit 0.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,088 kB
  • sloc: ansic: 73,585; sh: 7,776; xml: 1,953; makefile: 1,200; python: 675; sed: 39
file content (128 lines) | stat: -rw-r--r-- 3,531 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2011, Collabora Ltd.
 * Copyright (C) 2023 Red Hat Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * Authors: Stef Walter <stefw@collabora.co.uk>,
 *          Daiki Ueno,
 *          Zoltan Fridrich <zfridric@redhat.com>
 */

#include "config.h"

#include "debug.h"
#include "hex.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static const char HEXC_LOWER[] = "0123456789abcdef";

char *
hex_encode (const unsigned char *data,
            size_t n_data)
{
	char *result;
	size_t i;
	size_t o;

	return_val_if_fail (data != NULL, NULL);

	if ((SIZE_MAX - 1) / 3 < n_data)
		return NULL;
	result = malloc (n_data * 3 + 1);
	if (result == NULL)
		return NULL;

	for (i = 0, o = 0; i < n_data; i++) {
		if (i > 0)
			result[o++] = ':';
		result[o++] = HEXC_LOWER[data[i] >> 4 & 0xf];
		result[o++] = HEXC_LOWER[data[i] & 0xf];
	}

	result[o] = 0;
	return result;
}

unsigned char *
hex_decode (const char *hex,
            size_t *bin_len)
{
	int i, j;
	size_t bin_len_, hex_len;
	unsigned char *bin, c;
	bool with_separator;

	return_val_if_fail (hex != NULL, NULL);
	return_val_if_fail (bin_len != NULL, NULL);

	hex_len = strlen (hex);
	if (hex_len == 0)
		return NULL;

	with_separator = hex_len > 2 && hex[2] == ':';
	if (with_separator)
		for (i = 5; i < hex_len; i += 3)
			if (hex[i] != ':')
				return NULL;

	if (SIZE_MAX - 1 < hex_len ||
	    (with_separator && (hex_len + 1) % 3 != 0) ||
	    (!with_separator && hex_len % 2 != 0))
		return NULL;

	bin_len_ = with_separator ? (hex_len + 1) / 3 : hex_len / 2;
	bin = calloc (bin_len_, 1);
	if (bin == NULL)
		return NULL;

	for (i = 0; i < bin_len_; ++i) {
		for (j = 0; j < 2; ++j) {
			c = with_separator ? hex[i * 3 + j] : hex[i * 2 + j];
			if ('0' <= c && c <= '9')
				bin[i] |= c - '0';
			else if ('a' <= c && c <= 'f')
				bin[i] |= c - 'a' + 10;
			else if ('A' <= c && c <= 'F')
				bin[i] |= c - 'A' + 10;
			else {
				free (bin);
				return NULL;
			}
			if (j == 0)
				bin[i] <<= 4;
		}
	}

	*bin_len = bin_len_;
	return bin;
}