redcarpet 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +312 -20
- data/Rakefile +31 -36
- data/bin/redcarpet +1 -1
- data/ext/redcarpet/autolink.c +260 -0
- data/ext/redcarpet/autolink.h +36 -0
- data/ext/redcarpet/buffer.c +229 -0
- data/ext/redcarpet/buffer.h +88 -0
- data/ext/redcarpet/houdini.h +29 -0
- data/ext/redcarpet/houdini_href_e.c +108 -0
- data/ext/redcarpet/houdini_html_e.c +84 -0
- data/ext/redcarpet/html.c +603 -0
- data/ext/redcarpet/html.h +67 -0
- data/ext/redcarpet/html_blocks.h +206 -0
- data/ext/redcarpet/html_smartypants.c +385 -0
- data/ext/redcarpet/markdown.c +2454 -0
- data/ext/redcarpet/markdown.h +130 -0
- data/ext/redcarpet/rc_markdown.c +137 -0
- data/ext/redcarpet/rc_render.c +434 -0
- data/ext/redcarpet/redcarpet.h +33 -0
- data/ext/redcarpet/stack.c +81 -0
- data/ext/redcarpet/stack.h +21 -0
- data/lib/redcarpet/render_man.rb +65 -0
- data/lib/redcarpet.rb +60 -59
- data/redcarpet.gemspec +29 -19
- data/test/redcarpet_test.rb +273 -51
- metadata +43 -26
- data/ext/array.c +0 -300
- data/ext/array.h +0 -148
- data/ext/buffer.c +0 -318
- data/ext/buffer.h +0 -147
- data/ext/markdown.c +0 -1590
- data/ext/markdown.h +0 -124
- data/ext/redcarpet.c +0 -86
- data/ext/render.c +0 -499
- data/lib/markdown.rb +0 -1
- data/test/benchmark.rb +0 -56
- data/test/benchmark.txt +0 -306
- data/test/markdown_test.rb +0 -176
- /data/ext/{extconf.rb → redcarpet/extconf.rb} +0 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2011, Vicent Marti
|
|
3
|
+
*
|
|
4
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
|
7
|
+
*
|
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#include "buffer.h"
|
|
18
|
+
|
|
19
|
+
#include <string.h>
|
|
20
|
+
#include <stdlib.h>
|
|
21
|
+
#include <stdio.h>
|
|
22
|
+
#include <ctype.h>
|
|
23
|
+
|
|
24
|
+
int
|
|
25
|
+
sd_autolink_issafe(const uint8_t *link, size_t link_len)
|
|
26
|
+
{
|
|
27
|
+
static const size_t valid_uris_count = 5;
|
|
28
|
+
static const char *valid_uris[] = {
|
|
29
|
+
"/", "http://", "https://", "ftp://", "mailto:"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
size_t i;
|
|
33
|
+
|
|
34
|
+
for (i = 0; i < valid_uris_count; ++i) {
|
|
35
|
+
size_t len = strlen(valid_uris[i]);
|
|
36
|
+
|
|
37
|
+
if (link_len > len &&
|
|
38
|
+
strncasecmp((char *)link, valid_uris[i], len) == 0 &&
|
|
39
|
+
isalnum(link[len]))
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static size_t
|
|
47
|
+
autolink_delim(uint8_t *data, size_t link_end, size_t offset, size_t size)
|
|
48
|
+
{
|
|
49
|
+
uint8_t cclose, copen = 0;
|
|
50
|
+
size_t i;
|
|
51
|
+
|
|
52
|
+
for (i = 0; i < link_end; ++i)
|
|
53
|
+
if (data[i] == '<') {
|
|
54
|
+
link_end = i;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
while (link_end > 0) {
|
|
59
|
+
if (strchr("?!.,", data[link_end - 1]) != NULL)
|
|
60
|
+
link_end--;
|
|
61
|
+
|
|
62
|
+
else if (data[link_end - 1] == ';') {
|
|
63
|
+
size_t new_end = link_end - 2;
|
|
64
|
+
|
|
65
|
+
while (new_end > 0 && isalpha(data[new_end]))
|
|
66
|
+
new_end--;
|
|
67
|
+
|
|
68
|
+
if (new_end < link_end - 2 && data[new_end] == '&')
|
|
69
|
+
link_end = new_end;
|
|
70
|
+
else
|
|
71
|
+
link_end--;
|
|
72
|
+
}
|
|
73
|
+
else break;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (link_end == 0)
|
|
77
|
+
return 0;
|
|
78
|
+
|
|
79
|
+
cclose = data[link_end - 1];
|
|
80
|
+
|
|
81
|
+
switch (cclose) {
|
|
82
|
+
case '"': copen = '"'; break;
|
|
83
|
+
case '\'': copen = '\''; break;
|
|
84
|
+
case ')': copen = '('; break;
|
|
85
|
+
case ']': copen = '['; break;
|
|
86
|
+
case '}': copen = '{'; break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (copen != 0) {
|
|
90
|
+
size_t closing = 0;
|
|
91
|
+
size_t opening = 0;
|
|
92
|
+
size_t i = 0;
|
|
93
|
+
|
|
94
|
+
/* Try to close the final punctuation sign in this same line;
|
|
95
|
+
* if we managed to close it outside of the URL, that means that it's
|
|
96
|
+
* not part of the URL. If it closes inside the URL, that means it
|
|
97
|
+
* is part of the URL.
|
|
98
|
+
*
|
|
99
|
+
* Examples:
|
|
100
|
+
*
|
|
101
|
+
* foo http://www.pokemon.com/Pikachu_(Electric) bar
|
|
102
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
|
103
|
+
*
|
|
104
|
+
* foo (http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
105
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
|
106
|
+
*
|
|
107
|
+
* foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
108
|
+
* => http://www.pokemon.com/Pikachu_(Electric))
|
|
109
|
+
*
|
|
110
|
+
* (foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
111
|
+
* => foo http://www.pokemon.com/Pikachu_(Electric)
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
while (i < link_end) {
|
|
115
|
+
if (data[i] == copen)
|
|
116
|
+
opening++;
|
|
117
|
+
else if (data[i] == cclose)
|
|
118
|
+
closing++;
|
|
119
|
+
|
|
120
|
+
i++;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (closing != opening)
|
|
124
|
+
link_end--;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return link_end;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static size_t
|
|
131
|
+
check_domain(uint8_t *data, size_t size)
|
|
132
|
+
{
|
|
133
|
+
size_t i, np = 0;
|
|
134
|
+
|
|
135
|
+
if (!isalnum(data[0]))
|
|
136
|
+
return 0;
|
|
137
|
+
|
|
138
|
+
for (i = 1; i < size - 1; ++i) {
|
|
139
|
+
if (data[i] == '.') np++;
|
|
140
|
+
else if (!isalnum(data[i]) && data[i] != '-') break;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* a valid domain needs to have at least a dot.
|
|
144
|
+
* that's as far as we get */
|
|
145
|
+
return np ? i : 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
size_t
|
|
149
|
+
sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
|
|
150
|
+
{
|
|
151
|
+
size_t link_end;
|
|
152
|
+
|
|
153
|
+
if (offset > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
|
|
154
|
+
return 0;
|
|
155
|
+
|
|
156
|
+
if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
|
|
157
|
+
return 0;
|
|
158
|
+
|
|
159
|
+
link_end = check_domain(data, size);
|
|
160
|
+
|
|
161
|
+
if (link_end == 0)
|
|
162
|
+
return 0;
|
|
163
|
+
|
|
164
|
+
while (link_end < size && !isspace(data[link_end]))
|
|
165
|
+
link_end++;
|
|
166
|
+
|
|
167
|
+
link_end = autolink_delim(data, link_end, offset, size);
|
|
168
|
+
|
|
169
|
+
if (link_end == 0)
|
|
170
|
+
return 0;
|
|
171
|
+
|
|
172
|
+
bufput(link, data, link_end);
|
|
173
|
+
*rewind_p = 0;
|
|
174
|
+
|
|
175
|
+
return (int)link_end;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
size_t
|
|
179
|
+
sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
|
|
180
|
+
{
|
|
181
|
+
size_t link_end, rewind;
|
|
182
|
+
int nb = 0, np = 0;
|
|
183
|
+
|
|
184
|
+
for (rewind = 0; rewind < offset; ++rewind) {
|
|
185
|
+
uint8_t c = data[-rewind - 1];
|
|
186
|
+
|
|
187
|
+
if (isalnum(c))
|
|
188
|
+
continue;
|
|
189
|
+
|
|
190
|
+
if (strchr(".+-_", c) != NULL)
|
|
191
|
+
continue;
|
|
192
|
+
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (rewind == 0)
|
|
197
|
+
return 0;
|
|
198
|
+
|
|
199
|
+
for (link_end = 0; link_end < size; ++link_end) {
|
|
200
|
+
uint8_t c = data[link_end];
|
|
201
|
+
|
|
202
|
+
if (isalnum(c))
|
|
203
|
+
continue;
|
|
204
|
+
|
|
205
|
+
if (c == '@')
|
|
206
|
+
nb++;
|
|
207
|
+
else if (c == '.' && link_end < size - 1)
|
|
208
|
+
np++;
|
|
209
|
+
else if (c != '-' && c != '_')
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (link_end < 2 || nb != 1 || np == 0)
|
|
214
|
+
return 0;
|
|
215
|
+
|
|
216
|
+
link_end = autolink_delim(data, link_end, offset, size);
|
|
217
|
+
|
|
218
|
+
if (link_end == 0)
|
|
219
|
+
return 0;
|
|
220
|
+
|
|
221
|
+
bufput(link, data - rewind, link_end + rewind);
|
|
222
|
+
*rewind_p = rewind;
|
|
223
|
+
|
|
224
|
+
return link_end;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
size_t
|
|
228
|
+
sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
|
|
229
|
+
{
|
|
230
|
+
size_t link_end, rewind = 0, domain_len;
|
|
231
|
+
|
|
232
|
+
if (size < 4 || data[1] != '/' || data[2] != '/')
|
|
233
|
+
return 0;
|
|
234
|
+
|
|
235
|
+
while (rewind < offset && isalpha(data[-rewind - 1]))
|
|
236
|
+
rewind++;
|
|
237
|
+
|
|
238
|
+
if (!sd_autolink_issafe(data - rewind, size + rewind))
|
|
239
|
+
return 0;
|
|
240
|
+
link_end = strlen("://");
|
|
241
|
+
|
|
242
|
+
domain_len = check_domain(data + link_end, size - link_end);
|
|
243
|
+
if (domain_len == 0)
|
|
244
|
+
return 0;
|
|
245
|
+
|
|
246
|
+
link_end += domain_len;
|
|
247
|
+
while (link_end < size && !isspace(data[link_end]))
|
|
248
|
+
link_end++;
|
|
249
|
+
|
|
250
|
+
link_end = autolink_delim(data, link_end, offset, size);
|
|
251
|
+
|
|
252
|
+
if (link_end == 0)
|
|
253
|
+
return 0;
|
|
254
|
+
|
|
255
|
+
bufput(link, data - rewind, link_end + rewind);
|
|
256
|
+
*rewind_p = rewind;
|
|
257
|
+
|
|
258
|
+
return link_end;
|
|
259
|
+
}
|
|
260
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2011, Vicent Marti
|
|
3
|
+
*
|
|
4
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
|
7
|
+
*
|
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#ifndef UPSKIRT_AUTOLINK_H
|
|
18
|
+
#define UPSKIRT_AUTOLINK_H
|
|
19
|
+
|
|
20
|
+
#include "buffer.h"
|
|
21
|
+
|
|
22
|
+
extern int
|
|
23
|
+
sd_autolink_issafe(const uint8_t *link, size_t link_len);
|
|
24
|
+
|
|
25
|
+
extern size_t
|
|
26
|
+
sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
|
|
27
|
+
|
|
28
|
+
extern size_t
|
|
29
|
+
sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
|
|
30
|
+
|
|
31
|
+
extern size_t
|
|
32
|
+
sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
|
|
33
|
+
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
/* vim: set filetype=c: */
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2008, Natacha Porté
|
|
3
|
+
* Copyright (c) 2011, Vicent Martí
|
|
4
|
+
*
|
|
5
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
6
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
* copyright notice and this permission notice appear in all copies.
|
|
8
|
+
*
|
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
#define BUFFER_MAX_ALLOC_SIZE (1024 * 1024 * 16) //16mb
|
|
19
|
+
|
|
20
|
+
#include "buffer.h"
|
|
21
|
+
|
|
22
|
+
#include <stdio.h>
|
|
23
|
+
#include <stdlib.h>
|
|
24
|
+
#include <string.h>
|
|
25
|
+
|
|
26
|
+
/* MSVC compat */
|
|
27
|
+
#if defined(_MSC_VER)
|
|
28
|
+
# define _buf_vsnprintf _vsnprintf
|
|
29
|
+
#else
|
|
30
|
+
# define _buf_vsnprintf vsnprintf
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
int
|
|
34
|
+
bufprefix(const struct buf *buf, const char *prefix)
|
|
35
|
+
{
|
|
36
|
+
size_t i;
|
|
37
|
+
|
|
38
|
+
for (i = 0; i < buf->size; ++i) {
|
|
39
|
+
if (prefix[i] == 0)
|
|
40
|
+
return 0;
|
|
41
|
+
|
|
42
|
+
if (buf->data[i] != prefix[i])
|
|
43
|
+
return buf->data[i] - prefix[i];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/* bufgrow: increasing the allocated size to the given value */
|
|
50
|
+
int
|
|
51
|
+
bufgrow(struct buf *buf, size_t neosz)
|
|
52
|
+
{
|
|
53
|
+
size_t neoasz;
|
|
54
|
+
void *neodata;
|
|
55
|
+
if (!buf || !buf->unit || neosz > BUFFER_MAX_ALLOC_SIZE)
|
|
56
|
+
return BUF_ENOMEM;
|
|
57
|
+
|
|
58
|
+
if (buf->asize >= neosz)
|
|
59
|
+
return BUF_OK;
|
|
60
|
+
|
|
61
|
+
neoasz = buf->asize + buf->unit;
|
|
62
|
+
while (neoasz < neosz)
|
|
63
|
+
neoasz += buf->unit;
|
|
64
|
+
|
|
65
|
+
neodata = realloc(buf->data, neoasz);
|
|
66
|
+
if (!neodata)
|
|
67
|
+
return BUF_ENOMEM;
|
|
68
|
+
|
|
69
|
+
buf->data = neodata;
|
|
70
|
+
buf->asize = neoasz;
|
|
71
|
+
return BUF_OK;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/* bufnew: allocation of a new buffer */
|
|
76
|
+
struct buf *
|
|
77
|
+
bufnew(size_t unit)
|
|
78
|
+
{
|
|
79
|
+
struct buf *ret;
|
|
80
|
+
ret = malloc(sizeof (struct buf));
|
|
81
|
+
|
|
82
|
+
if (ret) {
|
|
83
|
+
ret->data = 0;
|
|
84
|
+
ret->size = ret->asize = 0;
|
|
85
|
+
ret->unit = unit;
|
|
86
|
+
}
|
|
87
|
+
return ret;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* bufnullterm: NULL-termination of the string array */
|
|
91
|
+
const char *
|
|
92
|
+
bufcstr(struct buf *buf)
|
|
93
|
+
{
|
|
94
|
+
if (!buf || !buf->unit)
|
|
95
|
+
return NULL;
|
|
96
|
+
|
|
97
|
+
if (buf->size < buf->asize && buf->data[buf->size] == 0)
|
|
98
|
+
return (char *)buf->data;
|
|
99
|
+
|
|
100
|
+
if (buf->size + 1 <= buf->asize || bufgrow(buf, buf->size + 1) == 0) {
|
|
101
|
+
buf->data[buf->size] = 0;
|
|
102
|
+
return (char *)buf->data;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return NULL;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* bufprintf: formatted printing to a buffer */
|
|
109
|
+
void
|
|
110
|
+
bufprintf(struct buf *buf, const char *fmt, ...)
|
|
111
|
+
{
|
|
112
|
+
va_list ap;
|
|
113
|
+
if (!buf || !buf->unit)
|
|
114
|
+
return;
|
|
115
|
+
|
|
116
|
+
int n;
|
|
117
|
+
|
|
118
|
+
if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
|
|
119
|
+
return;
|
|
120
|
+
|
|
121
|
+
va_start(ap, fmt);
|
|
122
|
+
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
|
|
123
|
+
va_end(ap);
|
|
124
|
+
|
|
125
|
+
if (n < 0) {
|
|
126
|
+
#ifdef _MSC_VER
|
|
127
|
+
n = _vscprintf(fmt, ap);
|
|
128
|
+
#else
|
|
129
|
+
return;
|
|
130
|
+
#endif
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if ((size_t)n >= buf->asize - buf->size) {
|
|
134
|
+
if (bufgrow(buf, buf->size + n + 1) < 0)
|
|
135
|
+
return;
|
|
136
|
+
va_start(ap, fmt);
|
|
137
|
+
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
|
|
138
|
+
va_end(ap);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (n < 0)
|
|
142
|
+
return;
|
|
143
|
+
|
|
144
|
+
buf->size += n;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* bufput: appends raw data to a buffer */
|
|
148
|
+
void
|
|
149
|
+
bufput(struct buf *buf, const void *data, size_t len)
|
|
150
|
+
{
|
|
151
|
+
if (!buf)
|
|
152
|
+
return;
|
|
153
|
+
|
|
154
|
+
if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0)
|
|
155
|
+
return;
|
|
156
|
+
|
|
157
|
+
memcpy(buf->data + buf->size, data, len);
|
|
158
|
+
buf->size += len;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* bufputs: appends a NUL-terminated string to a buffer */
|
|
162
|
+
void
|
|
163
|
+
bufputs(struct buf *buf, const char *str)
|
|
164
|
+
{
|
|
165
|
+
bufput(buf, str, strlen(str));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
/* bufputc: appends a single uint8_t to a buffer */
|
|
170
|
+
void
|
|
171
|
+
bufputc(struct buf *buf, int c)
|
|
172
|
+
{
|
|
173
|
+
if (!buf)
|
|
174
|
+
return;
|
|
175
|
+
|
|
176
|
+
if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0)
|
|
177
|
+
return;
|
|
178
|
+
|
|
179
|
+
buf->data[buf->size] = c;
|
|
180
|
+
buf->size += 1;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* bufrelease: decrease the reference count and free the buffer if needed */
|
|
184
|
+
void
|
|
185
|
+
bufrelease(struct buf *buf)
|
|
186
|
+
{
|
|
187
|
+
if (!buf)
|
|
188
|
+
return;
|
|
189
|
+
|
|
190
|
+
free(buf->data);
|
|
191
|
+
free(buf);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
/* bufreset: frees internal data of the buffer */
|
|
196
|
+
void
|
|
197
|
+
bufreset(struct buf *buf)
|
|
198
|
+
{
|
|
199
|
+
if (!buf)
|
|
200
|
+
return;
|
|
201
|
+
|
|
202
|
+
free(buf->data);
|
|
203
|
+
buf->data = NULL;
|
|
204
|
+
buf->size = buf->asize = 0;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* bufslurp: removes a given number of bytes from the head of the array */
|
|
208
|
+
void
|
|
209
|
+
bufslurp(struct buf *buf, size_t len)
|
|
210
|
+
{
|
|
211
|
+
if (!buf || !buf->unit || len <= 0)
|
|
212
|
+
return;
|
|
213
|
+
|
|
214
|
+
if (len >= buf->size) {
|
|
215
|
+
buf->size = 0;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
buf->size -= len;
|
|
220
|
+
memmove(buf->data, buf->data + len, buf->size);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/* vbufprintf: stdarg variant of formatted printing into a buffer */
|
|
224
|
+
void
|
|
225
|
+
vbufprintf(struct buf *buf, const char *fmt, va_list ap)
|
|
226
|
+
{
|
|
227
|
+
|
|
228
|
+
}
|
|
229
|
+
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2008, Natacha Porté
|
|
3
|
+
* Copyright (c) 2011, Vicent Martí
|
|
4
|
+
*
|
|
5
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
6
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
* copyright notice and this permission notice appear in all copies.
|
|
8
|
+
*
|
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
#ifndef BUFFER_H__
|
|
19
|
+
#define BUFFER_H__
|
|
20
|
+
|
|
21
|
+
#include <stddef.h>
|
|
22
|
+
#include <stdarg.h>
|
|
23
|
+
#include <stdint.h>
|
|
24
|
+
|
|
25
|
+
#if defined(_MSC_VER)
|
|
26
|
+
#define __attribute__(x)
|
|
27
|
+
#define inline
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
typedef enum {
|
|
31
|
+
BUF_OK = 0,
|
|
32
|
+
BUF_ENOMEM = -1,
|
|
33
|
+
} buferror_t;
|
|
34
|
+
|
|
35
|
+
/* struct buf: character array buffer */
|
|
36
|
+
struct buf {
|
|
37
|
+
uint8_t *data; /* actual character data */
|
|
38
|
+
size_t size; /* size of the string */
|
|
39
|
+
size_t asize; /* allocated size (0 = volatile buffer) */
|
|
40
|
+
size_t unit; /* reallocation unit size (0 = read-only buffer) */
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/* CONST_BUF: global buffer from a string litteral */
|
|
44
|
+
#define BUF_STATIC(string) \
|
|
45
|
+
{ (uint8_t *)string, sizeof string -1, sizeof string, 0, 0 }
|
|
46
|
+
|
|
47
|
+
/* VOLATILE_BUF: macro for creating a volatile buffer on the stack */
|
|
48
|
+
#define BUF_VOLATILE(strname) \
|
|
49
|
+
{ (uint8_t *)strname, strlen(strname), 0, 0, 0 }
|
|
50
|
+
|
|
51
|
+
/* BUFPUTSL: optimized bufputs of a string litteral */
|
|
52
|
+
#define BUFPUTSL(output, literal) \
|
|
53
|
+
bufput(output, literal, sizeof literal - 1)
|
|
54
|
+
|
|
55
|
+
/* bufgrow: increasing the allocated size to the given value */
|
|
56
|
+
int bufgrow(struct buf *, size_t);
|
|
57
|
+
|
|
58
|
+
/* bufnew: allocation of a new buffer */
|
|
59
|
+
struct buf *bufnew(size_t) __attribute__ ((malloc));
|
|
60
|
+
|
|
61
|
+
/* bufnullterm: NUL-termination of the string array (making a C-string) */
|
|
62
|
+
const char *bufcstr(struct buf *);
|
|
63
|
+
|
|
64
|
+
/* bufprefix: compare the beginning of a buffer with a string */
|
|
65
|
+
int bufprefix(const struct buf *buf, const char *prefix);
|
|
66
|
+
|
|
67
|
+
/* bufput: appends raw data to a buffer */
|
|
68
|
+
void bufput(struct buf *, const void *, size_t);
|
|
69
|
+
|
|
70
|
+
/* bufputs: appends a NUL-terminated string to a buffer */
|
|
71
|
+
void bufputs(struct buf *, const char *);
|
|
72
|
+
|
|
73
|
+
/* bufputc: appends a single char to a buffer */
|
|
74
|
+
void bufputc(struct buf *, int);
|
|
75
|
+
|
|
76
|
+
/* bufrelease: decrease the reference count and free the buffer if needed */
|
|
77
|
+
void bufrelease(struct buf *);
|
|
78
|
+
|
|
79
|
+
/* bufreset: frees internal data of the buffer */
|
|
80
|
+
void bufreset(struct buf *);
|
|
81
|
+
|
|
82
|
+
/* bufslurp: removes a given number of bytes from the head of the array */
|
|
83
|
+
void bufslurp(struct buf *, size_t);
|
|
84
|
+
|
|
85
|
+
/* bufprintf: formatted printing to a buffer */
|
|
86
|
+
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
|
|
87
|
+
|
|
88
|
+
#endif
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#ifndef HOUDINI_H__
|
|
2
|
+
#define HOUDINI_H__
|
|
3
|
+
|
|
4
|
+
#include "buffer.h"
|
|
5
|
+
|
|
6
|
+
#ifdef HOUDINI_USE_LOCALE
|
|
7
|
+
# define _isxdigit(c) isxdigit(c)
|
|
8
|
+
# define _isdigit(c) isdigit(c)
|
|
9
|
+
#else
|
|
10
|
+
/*
|
|
11
|
+
* Helper _isdigit methods -- do not trust the current locale
|
|
12
|
+
* */
|
|
13
|
+
# define _isxdigit(c) (strchr("0123456789ABCDEFabcdef", (c)) != NULL)
|
|
14
|
+
# define _isdigit(c) ((c) >= '0' && (c) <= '9')
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
extern void houdini_escape_html(struct buf *ob, const uint8_t *src, size_t size);
|
|
18
|
+
extern void houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure);
|
|
19
|
+
extern void houdini_unescape_html(struct buf *ob, const uint8_t *src, size_t size);
|
|
20
|
+
extern void houdini_escape_xml(struct buf *ob, const uint8_t *src, size_t size);
|
|
21
|
+
extern void houdini_escape_uri(struct buf *ob, const uint8_t *src, size_t size);
|
|
22
|
+
extern void houdini_escape_url(struct buf *ob, const uint8_t *src, size_t size);
|
|
23
|
+
extern void houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size);
|
|
24
|
+
extern void houdini_unescape_uri(struct buf *ob, const uint8_t *src, size_t size);
|
|
25
|
+
extern void houdini_unescape_url(struct buf *ob, const uint8_t *src, size_t size);
|
|
26
|
+
extern void houdini_escape_js(struct buf *ob, const uint8_t *src, size_t size);
|
|
27
|
+
extern void houdini_unescape_js(struct buf *ob, const uint8_t *src, size_t size);
|
|
28
|
+
|
|
29
|
+
#endif
|