~q3cpma/misc-tools

ref: 023b3736bfe7bc4caeebda9a5d07d060870f0128 misc-tools/htmldecode.c -rw-r--r-- 3.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "misc.h"
#include "utf8.h"
#include "htab.h"
#include "namedref.h"


/* Like xstrtoul but stops at ';' and increase str to point to after the ';' */
static inline uint32_t xstrtou32_entity(char **str, int base)
{
	char *end;
	const unsigned long ret = strtoul(*str, &end, base);
	if (**str == '\0' || *end != ';' || (ret == ULONG_MAX && errno == ERANGE))
	{
		die("%.*s: invalid base%d number", (int)(end - *str), *str, base);
	}
	if (ret > UINT32_MAX)
	{
		die("%lu: entity value is too big, must be <= %" PRIu32, ret, UINT32_MAX);
	}
	*str = end + 1;
	return ret;
}


bool htmldecode(char *in, char *out, size_t len)
{
	char *old = in;
	for (char *fnd; (fnd = strchr(old, '&')); old = fnd)
	{
		memcpy(out, old, fnd - old);
		out += fnd - old;

		++fnd;
		if (*fnd == '#')
		{
			++fnd;
			if (*fnd == 'x')
			{
				++fnd;
				out += utf8_encode(xstrtou32_entity(&fnd, 16), out);
			}
			else
			{
				out += utf8_encode(xstrtou32_entity(&fnd, 10), out);
			}
		}
		else
		{
			char *sc;
			if ((sc = strchr(fnd, ';')) == NULL)
			{
			    die("Unterminated named character references not supported");
			}
			codep_pair cdp = namedref_to_codep_pair(fnd, sc - fnd);
			if (!cdp.c1 && !cdp.c2)
			{
				fprintf(stderr, "%.*s: unknown named character reference\n",
					(int)(sc - fnd), fnd);
				return false;
			}
			fnd = sc + 1;
			out += utf8_encode(cdp.c1, out);
			if (cdp.c2)
			{
				out += utf8_encode(cdp.c2, out);
			}
		}
	}
	memcpy(out, old, in + len - old);
	return true;
}

NORETURN void usage(int exit_status)
{
	printf(
		"NAME\n"
		"    %s - HTML decode input lines\n"
		"\n"
		"SYNOPSIS\n"
		"    %s [OPTIONS]\n"
		"\n"
		"DESCRIPTION\n"
		"    Copy stdin to stdout while decoding all HTML escape sequences to UTF-8 (for \n"
		"    example, \"&#x26;\", \"&#38;\" and \"&amp;\" all translate to '&').\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;)
		{
			// Decoded string can't be longer than input
			if (buflen < (size_t)read + 1)
			{
				buf = xrealloc(buf, buflen = (size_t)read + 1);
			}
			if (!htmldecode(line, buf, (size_t)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 (!htmldecode(argv[i], buf, arglen))
			{
				free(buf);
				return EXIT_FAILURE;
			}
			puts(buf);
		}
		free(buf);
	}
	return EXIT_SUCCESS;
}