summaryrefslogtreecommitdiff
path: root/lib/tar/base64.c
blob: 313e9f422a5a3d2d3b1fe6a8c24e3415d7a36141 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "internal.h"

static uint8_t convert(char in)
{
	if (isupper(in))
		return in - 'A';
	if (islower(in))
		return in - 'a' + 26;
	if (isdigit(in))
		return in - '0' + 52;
	if (in == '+')
		return 62;
	if (in == '/' || in == '-')
		return 63;
	return 0;
}

void base64_decode(uint8_t *out, const char *in)
{
	char temp[4];

	while (*in != '\0' && *in != '=') {
		temp[0] = *in == '\0' ? 0 : convert(*(in++));
		temp[1] = *in == '\0' ? 0 : convert(*(in++));
		temp[2] = *in == '\0' ? 0 : convert(*(in++));
		temp[3] = *in == '\0' ? 0 : convert(*(in++));

		*(out++) = ((temp[0] << 2) & 0xFC) | ((temp[1] >> 4) & 0x03);
		*(out++) = ((temp[1] << 4) & 0xF0) | ((temp[2] >> 2) & 0x0F);
		*(out++) = ((temp[2] << 6) & 0xC0) | ( temp[3]       & 0x3F);
	}
}