summaryrefslogtreecommitdiff
path: root/lib/tar/base64.c
blob: 0ccf3c4141032c3d492c81828c89d2a7f72092f5 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * base64.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "internal.h"

static sqfs_u8 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(sqfs_u8 *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);
	}
}