~q3cpma/misc-tools

ref: 313cd570e4b81c5f6749356698ce617c65181b61 misc-tools/urlencode.c -rw-r--r-- 3.5 KiB
313cd570q3cpma README nit 10 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
146
147
148
149
150
151
152
153
154
155
156
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "misc.h"

#define ESCAPED_REPL_LEN 3


/* Unreserved URL characters. NUL is also set to 1, in order to be copied
   verbatim from the source string */
static unsigned char unreserved[256] =
{
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
	0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
	0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

void urlencode(const char *restrict in, char *restrict out, size_t len)
{
	for (size_t i = 0; i < len; ++i)
	{
		if (unreserved[(unsigned char)in[i]])
		{
			*out++ = in[i];
		}
		else
		{
			sprintf(out, "%%%02X", in[i]);
			out += 3;
		}
	}
	*out = '\0';
}

NORETURN void usage(int exit_status)
{
	printf(
		"NAME\n"
		"    %s - URL encode strings\n"
		"\n"
		"SYNOPSIS\n"
		"    %s [OPTIONS] [URL...]\n"
		"\n"
		"DESCRIPTION\n"
		"    URL encode 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"
		"    -p\n"
		"        Don't encode slashes, as arguments are to be used as paths.\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, "hvp")) != -1;)
	{
		switch (opt)
		{
			case 'h':
				usage(EXIT_SUCCESS);
				break;

			case 'p':
				unreserved['/'] = 1;
				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;)
		{
			//Use three time the input size (no need to check size when
			//sprinting, this way)
			if ((size_t)read > (SIZE_MAX - 1) / ESCAPED_REPL_LEN)
			{
				die("Input string is too big (lim: %zu)",
					(SIZE_MAX - 1) / ESCAPED_REPL_LEN);
			}
			if (buflen < (size_t)read * ESCAPED_REPL_LEN + 1)
			{
				buflen = read * ESCAPED_REPL_LEN + 1;
				buf = xrealloc(buf, buflen);
			}
			urlencode(line, buf, read);
			puts(buf);
		}
		free(buf);
		free(line);
	}
	else
	{
		for (int i = 0; i < argc; ++i)
		{
			size_t arglen = strlen(argv[i]);
			if (arglen > (SIZE_MAX - 1) / ESCAPED_REPL_LEN)
			{
				die("Input string is too big (lim: %zu)",
					(SIZE_MAX - 1) / ESCAPED_REPL_LEN);
			}
			if (buflen < arglen * ESCAPED_REPL_LEN + 1)
			{
				buflen = arglen * ESCAPED_REPL_LEN + 1;
				buf = xrealloc(buf, buflen);
			}
			urlencode(argv[i], buf, arglen);
			puts(buf);
		}
		free(buf);
	}
	return EXIT_SUCCESS;
}