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

#include "internal.h"

static int xdigit(int x)
{
	if (isupper(x))
		return x - 'A' + 0x0A;
	if (islower(x))
		return x - 'a' + 0x0A;
	return x - '0';
}

void urldecode(char *str)
{
	unsigned char *out = (unsigned char *)str;
	char *in = str;
	int x;

	while (*in != '\0') {
		x = *(in++);

		if (x == '%' && isxdigit(in[0]) && isxdigit(in[1])) {
			x = xdigit(*(in++)) << 4;
			x |= xdigit(*(in++));
		}

		*(out++) = x;
	}

	*out = '\0';
}