-
Notifications
You must be signed in to change notification settings - Fork 35
/
autolink.c
326 lines (264 loc) · 6.55 KB
/
autolink.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2008, Natacha Porté
* Copyright (c) 2011, Vicent Martí
* Copyright (c) 2014, Xavier Mendez, Devin Torres and the Hoedown authors
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lowdown.h"
#include "extern.h"
#define VALID_URIS_SZ 6
/*
* List of URI prefixes that are considered "valid".
*/
static const char *const valid_uris[VALID_URIS_SZ] = {
"http://",
"https://",
"/",
"#",
"ftp://",
"mailto:"
};
/*
* Verify that a URL has a safe protocol.
*/
static int
halink_is_safe(const char *data, size_t size)
{
size_t i, len;
for (i = 0; i < VALID_URIS_SZ; ++i) {
len = strlen(valid_uris[i]);
if (size > len &&
strncasecmp(data, valid_uris[i], len) == 0 &&
isalnum((unsigned char)data[len]))
return 1;
}
return 0;
}
/*
* Find the end of a hyperlink.
* Returns the position of the end.
*/
static size_t
autolink_delim(char *data,
size_t link_end, size_t max_rewind, size_t size)
{
char cclose, copen = 0;
size_t closing, opening, i, new_end;
for (i = 0; i < link_end; ++i)
if (data[i] == '<') {
link_end = i;
break;
}
while (link_end > 0)
if (strchr("?!.,:", data[link_end - 1]) != NULL)
link_end--;
else if (data[link_end - 1] == ';') {
new_end = link_end - 2;
while (new_end > 0 &&
isalpha((unsigned char)data[new_end]))
new_end--;
if (new_end < link_end - 2 &&
data[new_end] == '&')
link_end = new_end;
else
link_end--;
} else
break;
if (link_end == 0)
return 0;
cclose = data[link_end - 1];
switch (cclose) {
case '"':
copen = '"';
break;
case '\'':
copen = '\'';
break;
case ')':
copen = '(';
break;
case ']':
copen = '[';
break;
case '}':
copen = '{';
break;
}
if (copen != 0) {
closing = opening = i = 0;
/*
* Try to close the final punctuation sign in this same
* line; if we managed to close it outside of the URL,
* that means that it's not part of the URL. If it
* closes inside the URL, that means it is part of the
* URL.
*
* Examples:
*
* foo http://www.pokemon.com/Pikachu_(Electric) bar
* => http://www.pokemon.com/Pikachu_(Electric)
*
* foo (http://www.pokemon.com/Pikachu_(Electric)) bar
* => http://www.pokemon.com/Pikachu_(Electric)
*
* foo http://www.pokemon.com/Pikachu_(Electric)) bar
* => http://www.pokemon.com/Pikachu_(Electric))
*
* (foo http://www.pokemon.com/Pikachu_(Electric)) bar
* => foo http://www.pokemon.com/Pikachu_(Electric)
*/
while (i < link_end) {
if (data[i] == copen)
opening++;
else if (data[i] == cclose)
closing++;
i++;
}
if (closing != opening)
link_end--;
}
return link_end;
}
/*
* To make sure that a domain is well-formed.
* Returns zero on failure, non-zero on success.
* XXX: this function needs to be replaced.
*/
static size_t
check_domain(char *data, size_t size)
{
size_t i, np = 0;
if (!isalnum((unsigned char)data[0]))
return 0;
for (i = 1; i < size - 1; ++i) {
if (strchr(".:", data[i]) != NULL)
np++;
else if (!isalnum((unsigned char)data[i]) &&
data[i] != '-')
break;
}
/* A valid domain needs to have at least a dot. */
return np ? i : 0;
}
/*
* Search for the next www link in data.
*/
ssize_t
halink_www(size_t *rewind_p, struct lowdown_buf *link,
char *data, size_t max_rewind, size_t size)
{
size_t link_end;
if (max_rewind > 0 &&
!ispunct((unsigned char)data[-1]) &&
!isspace((unsigned char)data[-1]))
return 0;
if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
return 0;
link_end = check_domain(data, size);
if (link_end == 0)
return 0;
while (link_end < size &&
!isspace((unsigned char)data[link_end]))
link_end++;
link_end = autolink_delim(data, link_end, max_rewind, size);
if (link_end == 0)
return 0;
if (!hbuf_put(link, data, link_end))
return -1;
*rewind_p = 0;
return link_end;
}
/*
* Search for the next email in data.
*/
ssize_t
halink_email(size_t *rewind_p, struct lowdown_buf *link,
char *data, size_t max_rewind, size_t size)
{
size_t link_end, rewind;
int nb = 0, np = 0;
char c;
for (rewind = 0; rewind < max_rewind; ++rewind) {
c = data[-1 - rewind];
if (isalnum((unsigned char)c))
continue;
if (strchr(".+-_", c) != NULL)
continue;
break;
}
if (rewind == 0)
return 0;
for (link_end = 0; link_end < size; ++link_end) {
c = data[link_end];
if (isalnum(c))
continue;
if (c == '@')
nb++;
else if (c == '.' && link_end < size - 1)
np++;
else if (c != '-' && c != '_')
break;
}
if (link_end < 2 || nb != 1 || np == 0 ||
!isalpha((unsigned char)data[link_end - 1]))
return 0;
link_end = autolink_delim(data, link_end, max_rewind, size);
if (link_end == 0)
return 0;
if (!hbuf_put(link, data - rewind, link_end + rewind))
return -1;
*rewind_p = rewind;
return link_end;
}
/*
* Search for the next URL in data.
*/
ssize_t
halink_url(size_t *rewind_p, struct lowdown_buf *link,
char *data, size_t max_rewind, size_t size)
{
size_t link_end, rewind = 0, domain_len;
if (size < 4 || data[1] != '/' || data[2] != '/')
return 0;
while (rewind < max_rewind &&
isalpha((unsigned char)data[-1 - rewind]))
rewind++;
if (!halink_is_safe(data - rewind, size + rewind))
return 0;
link_end = strlen("://");
domain_len = check_domain(data + link_end, size - link_end);
if (domain_len == 0)
return 0;
link_end += domain_len;
while (link_end < size &&
!isspace((unsigned char)data[link_end]))
link_end++;
link_end = autolink_delim(data, link_end, max_rewind, size);
if (link_end == 0)
return 0;
if (!hbuf_put(link, data - rewind, link_end + rewind))
return -1;
*rewind_p = rewind;
return link_end;
}