summaryrefslogtreecommitdiff
path: root/network/elinks/patches/0008-gopher-html-entities.patch
blob: 750d1a49deb42379d65d2b3a82c83e995eb722a2 (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
41
42
43
44
45
46
47
###################################################################
# Encode < > to HTML.
###################################################################
diff -Naur '--exclude=.git' bb/src/protocol/gopher/gopher.c cc/src/protocol/gopher/gopher.c
--- bb/src/protocol/gopher/gopher.c	2020-01-14 03:26:41.791778387 +0000
+++ cc/src/protocol/gopher/gopher.c	2020-01-14 06:19:56.964025440 +0000
@@ -620,6 +620,25 @@
 	return line[0] == '.' && !line[1] ? NULL : line;
 }
 
+/* substring replacement */
+static void
+str_replace(char * in_string, char * s_string, char * r_string)
+{
+	/* max == replace all characters */
+	char temp[strlen(in_string) * (strlen(r_string) - strlen(s_string)) + 1];
+	char * pos;
+
+	if (!(pos = strstr(in_string, s_string)))
+			return;
+
+	strncpy(temp, in_string, pos - in_string);
+	temp[pos - in_string] = 0;
+	sprintf(temp + (pos - in_string), "%s%s", r_string, pos + 1);
+	in_string[0] = 0;
+	strcpy(in_string, temp);
+	return str_replace(in_string, s_string, r_string);
+}
+
 /* Parse a Gopher Menu document */
 static struct connection_state
 read_gopher_directory_data(struct connection *conn, struct read_buffer *rb)
@@ -639,6 +658,14 @@
 		return connection_state(S_OUT_OF_MEM);
 	}
 
+	/* Lines with characters that need to be converted to html */
+	if (strstr(rb->data, "<")) {
+		str_replace(rb->data, "<", "&lt;");
+	}
+	if (strstr(rb->data, ">")) {
+		str_replace(rb->data, ">", "&gt;");
+	}
+
 	while ((end = get_gopher_line_end(rb->data, rb->length))) {
 		unsigned char *line = check_gopher_last_line(rb->data, end);