~q3cpma/misc-tools

ref: 023b3736bfe7bc4caeebda9a5d07d060870f0128 misc-tools/urldecode.c -rw-r--r-- 2.5 KiB
023b3736q3cpma Fix gen_unicode_tolower_lut.sh typo 7 months ago
                                                                                
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "misc.h"


bool urldecode(char *in, char *out, size_t len)
{
	char *fnd, *old = in;
	for (; (fnd = strpbrk(old, "%+")); old = fnd, ++out)
	{
		memcpy(out, old, (size_t)(fnd - old));
		out += fnd - old;

		if (*fnd == '+')
		{
			*out = ' ';
			++fnd;
		}
		else
		{
			if (fnd > in + len - 2)
			{
				fprintf(stderr, "Unfinished percent-encoded sequence\n");
				return false;
			}
			if (!isxdigit(fnd[1]) || !isxdigit(fnd[2]))
			{
				fprintf(stderr, "%c%c: not an hexadecimal value\n", fnd[1],
					fnd[2]);
				return false;
			}
			unsigned char c;
			if (sscanf(fnd + 1, "%2hhx", &c) == EOF)
			{
				fprintf(stderr, "%s", strerror(errno));
				return false;
			}
			memcpy(out, &c, 1);
			fnd += 3;
		}
	}
	memcpy(out, old, in + len - old + 1);
	return true;
}

NORETURN void usage(int exit_status)
{
	printf(
		"NAME\n"
		"    %s - URL decode strings\n"
		"\n"
		"SYNOPSIS\n"
		"    %s [OPTIONS] URL...\n"
		"\n"
		"DESCRIPTION\n"
		"    URL decode the arguments and print them to stdout separated by newlines.\n"
		"    If there is no argument or the only argument is '-', stdin is read as a\n"
		"    newline separated argument list.\n"
		"\n"
		"OPTIONS\n"
		"    -h\n"
		"        Print this help message and exit.\n"
		"\n"
		"    -v\n"
		"        Print the version and exit.\n"
		"\n",
		PROG_NAME, PROG_NAME);
	exit(exit_status);
}


int main(int argc, char **argv)
{
	signals_nointerrupt();

	for (int opt; (opt = getopt(argc, argv, "hv")) != -1;)
	{
		switch (opt)
		{
			case 'h':
				usage(EXIT_SUCCESS);
				break;

			case 'v':
			    puts(PROG_VERSION);
				exit(EXIT_SUCCESS);

			default:
				exit(EXIT_FAILURE);
		}
	}
	argc -= optind;
	argv += optind;

	char *buf = NULL;
	size_t buflen = 0;

	if (argc == 0 || (argc == 1 && !strcmp(argv[0], "-")))
	{
		char *line = NULL;
		size_t linelen = 0;

		for (ssize_t read; (read = xgetline(&line, &linelen, stdin)) != -1; )
		{
			if (buflen < (size_t)read)
			{
			    buf = xrealloc(buf, buflen = read + 1);
			}
			if (!urldecode(line, buf, read))
			{
				free(buf);
				free(line);
				return EXIT_FAILURE;
			}
			puts(buf);
		}
		free(buf);
		free(line);
	}
	else
	{
		for (int i = 0; i < argc; ++i)
		{
			size_t arglen = strlen(argv[i]) + 1;
			if (buflen < arglen)
			{
			    buf = xrealloc(buf, buflen = arglen + 1);
			}
			if (!urldecode(argv[i], buf, arglen))
			{
				free(buf);
				return EXIT_FAILURE;
			}
			puts(buf);
		}
		free(buf);
	}
	return EXIT_SUCCESS;
}