walters 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +28 -0
- data/README.md +20 -0
- data/Rakefile +6 -0
- data/ext/Rakefile +5 -0
- data/ext/houdini/buffer.c +249 -0
- data/ext/houdini/buffer.h +113 -0
- data/ext/houdini/houdini.h +44 -0
- data/ext/houdini/houdini_href_e.c +115 -0
- data/ext/houdini/houdini_html_e.c +90 -0
- data/ext/houdini/houdini_html_u.c +122 -0
- data/ext/houdini/houdini_js_e.c +90 -0
- data/ext/houdini/houdini_js_u.c +60 -0
- data/ext/houdini/houdini_uri_e.c +101 -0
- data/ext/houdini/houdini_uri_u.c +65 -0
- data/ext/houdini/houdini_xml_e.c +136 -0
- data/ext/houdini/html_unescape.h +754 -0
- data/ext/walters.cpp +105 -0
- data/ext/walters_buffer.cpp +113 -0
- data/ext/x86_64-darwin/__xni_walters.cpp +450 -0
- data/ext/x86_64-darwin/walters.h +43 -0
- data/lib/walters.rb +2 -0
- data/lib/walters/version.rb +3 -0
- data/lib/walters/walters.rb +87 -0
- data/walters.gemspec +21 -0
- metadata +134 -0
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
you may not use this file except in compliance with the License.
|
3
|
+
You may obtain a copy of the License at
|
4
|
+
|
5
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10
|
+
See the License for the specific language governing permissions and
|
11
|
+
limitations under the License.
|
12
|
+
|
13
|
+
|
14
|
+
Alternatively, you can redistribute it and/or modify it under
|
15
|
+
the terms of the GNU Lesser General Public License as published by
|
16
|
+
the Free Software Foundation, either version 3 of the License, or
|
17
|
+
(at your option) any later version.
|
18
|
+
|
19
|
+
This code is distributed in the hope that it will be useful, but WITHOUT
|
20
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
22
|
+
version 3 for more details.
|
23
|
+
|
24
|
+
You should have received a copy of the GNU Lesser General Public License
|
25
|
+
version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
|
26
|
+
|
27
|
+
|
28
|
+
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
walters [![Build Status](https://travis-ci.org/wmeissner/walters.png)](https://travis-ci.org/wmeissner/walters)
|
2
|
+
======
|
3
|
+
|
4
|
+
[Walters](https://github.com/wmeissner/walters) is a JRuby wrapper for the [Houdini](https://github.com/vmg/houdini)
|
5
|
+
html escaping library
|
6
|
+
|
7
|
+
##### Example usage
|
8
|
+
|
9
|
+
jruby-1.7.4.dev :001 > require 'walters'
|
10
|
+
=> true
|
11
|
+
jruby-1.7.4.dev :002 > Walters.escape_html('<html>')
|
12
|
+
=> "<html>"
|
13
|
+
|
14
|
+
|
15
|
+
The same extension can be used from the legacy CRuby VM as well (aka MRI)
|
16
|
+
|
17
|
+
2.0.0p0 :001 > require 'walters'
|
18
|
+
=> true
|
19
|
+
2.0.0p0 :002 > Walters.escape_html('<html>')
|
20
|
+
=> "<html>"
|
data/Rakefile
ADDED
data/ext/Rakefile
ADDED
@@ -0,0 +1,249 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) the libgit2 contributors. All rights reserved.
|
3
|
+
*
|
4
|
+
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
5
|
+
* a Linking Exception. For full terms see the included COPYING file.
|
6
|
+
*/
|
7
|
+
#include <stdarg.h>
|
8
|
+
#include <ctype.h>
|
9
|
+
#include <string.h>
|
10
|
+
#include <assert.h>
|
11
|
+
#include <string.h>
|
12
|
+
#include <stdio.h>
|
13
|
+
#include <stdlib.h>
|
14
|
+
#include <sys/param.h>
|
15
|
+
|
16
|
+
#include "buffer.h"
|
17
|
+
|
18
|
+
/* Used as default value for gh_buf->ptr so that people can always
|
19
|
+
* assume ptr is non-NULL and zero terminated even for new gh_bufs.
|
20
|
+
*/
|
21
|
+
char gh_buf__initbuf[1];
|
22
|
+
char gh_buf__oom[1];
|
23
|
+
|
24
|
+
#define ENSURE_SIZE(b, d) \
|
25
|
+
if ((d) > buf->asize && gh_buf_grow(b, (d)) < 0)\
|
26
|
+
return -1;
|
27
|
+
|
28
|
+
void gh_buf_init(gh_buf *buf, size_t initial_size)
|
29
|
+
{
|
30
|
+
buf->asize = 0;
|
31
|
+
buf->size = 0;
|
32
|
+
buf->ptr = gh_buf__initbuf;
|
33
|
+
|
34
|
+
if (initial_size)
|
35
|
+
gh_buf_grow(buf, initial_size);
|
36
|
+
}
|
37
|
+
|
38
|
+
int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom)
|
39
|
+
{
|
40
|
+
char *new_ptr;
|
41
|
+
size_t new_size;
|
42
|
+
|
43
|
+
if (buf->ptr == gh_buf__oom)
|
44
|
+
return -1;
|
45
|
+
|
46
|
+
if (target_size <= buf->asize)
|
47
|
+
return 0;
|
48
|
+
|
49
|
+
if (buf->asize == 0) {
|
50
|
+
new_size = target_size;
|
51
|
+
new_ptr = NULL;
|
52
|
+
} else {
|
53
|
+
new_size = buf->asize;
|
54
|
+
new_ptr = buf->ptr;
|
55
|
+
}
|
56
|
+
|
57
|
+
/* grow the buffer size by 1.5, until it's big enough
|
58
|
+
* to fit our target size */
|
59
|
+
while (new_size < target_size)
|
60
|
+
new_size = (new_size << 1) - (new_size >> 1);
|
61
|
+
|
62
|
+
/* round allocation up to multiple of 8 */
|
63
|
+
new_size = (new_size + 7) & ~7;
|
64
|
+
|
65
|
+
new_ptr = realloc(new_ptr, new_size);
|
66
|
+
|
67
|
+
if (!new_ptr) {
|
68
|
+
if (mark_oom)
|
69
|
+
buf->ptr = gh_buf__oom;
|
70
|
+
return -1;
|
71
|
+
}
|
72
|
+
|
73
|
+
buf->asize = new_size;
|
74
|
+
buf->ptr = new_ptr;
|
75
|
+
|
76
|
+
/* truncate the existing buffer size if necessary */
|
77
|
+
if (buf->size >= buf->asize)
|
78
|
+
buf->size = buf->asize - 1;
|
79
|
+
buf->ptr[buf->size] = '\0';
|
80
|
+
|
81
|
+
return 0;
|
82
|
+
}
|
83
|
+
|
84
|
+
void gh_buf_free(gh_buf *buf)
|
85
|
+
{
|
86
|
+
if (!buf) return;
|
87
|
+
|
88
|
+
if (buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom)
|
89
|
+
free(buf->ptr);
|
90
|
+
|
91
|
+
gh_buf_init(buf, 0);
|
92
|
+
}
|
93
|
+
|
94
|
+
void gh_buf_clear(gh_buf *buf)
|
95
|
+
{
|
96
|
+
buf->size = 0;
|
97
|
+
if (buf->asize > 0)
|
98
|
+
buf->ptr[0] = '\0';
|
99
|
+
}
|
100
|
+
|
101
|
+
int gh_buf_set(gh_buf *buf, const char *data, size_t len)
|
102
|
+
{
|
103
|
+
if (len == 0 || data == NULL) {
|
104
|
+
gh_buf_clear(buf);
|
105
|
+
} else {
|
106
|
+
if (data != buf->ptr) {
|
107
|
+
ENSURE_SIZE(buf, len + 1);
|
108
|
+
memmove(buf->ptr, data, len);
|
109
|
+
}
|
110
|
+
buf->size = len;
|
111
|
+
buf->ptr[buf->size] = '\0';
|
112
|
+
}
|
113
|
+
return 0;
|
114
|
+
}
|
115
|
+
|
116
|
+
int gh_buf_sets(gh_buf *buf, const char *string)
|
117
|
+
{
|
118
|
+
return gh_buf_set(buf, string, string ? strlen(string) : 0);
|
119
|
+
}
|
120
|
+
|
121
|
+
int gh_buf_putc(gh_buf *buf, char c)
|
122
|
+
{
|
123
|
+
ENSURE_SIZE(buf, buf->size + 2);
|
124
|
+
buf->ptr[buf->size++] = c;
|
125
|
+
buf->ptr[buf->size] = '\0';
|
126
|
+
return 0;
|
127
|
+
}
|
128
|
+
|
129
|
+
int gh_buf_put(gh_buf *buf, const void *data, size_t len)
|
130
|
+
{
|
131
|
+
ENSURE_SIZE(buf, buf->size + len + 1);
|
132
|
+
memmove(buf->ptr + buf->size, data, len);
|
133
|
+
buf->size += len;
|
134
|
+
buf->ptr[buf->size] = '\0';
|
135
|
+
return 0;
|
136
|
+
}
|
137
|
+
|
138
|
+
int gh_buf_puts(gh_buf *buf, const char *string)
|
139
|
+
{
|
140
|
+
assert(string);
|
141
|
+
return gh_buf_put(buf, string, strlen(string));
|
142
|
+
}
|
143
|
+
|
144
|
+
int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap)
|
145
|
+
{
|
146
|
+
int len;
|
147
|
+
const size_t expected_size = buf->size + (strlen(format) * 2);
|
148
|
+
|
149
|
+
ENSURE_SIZE(buf, expected_size);
|
150
|
+
|
151
|
+
while (1) {
|
152
|
+
va_list args;
|
153
|
+
va_copy(args, ap);
|
154
|
+
|
155
|
+
len = vsnprintf(
|
156
|
+
buf->ptr + buf->size,
|
157
|
+
buf->asize - buf->size,
|
158
|
+
format, args
|
159
|
+
);
|
160
|
+
|
161
|
+
if (len < 0) {
|
162
|
+
free(buf->ptr);
|
163
|
+
buf->ptr = gh_buf__oom;
|
164
|
+
return -1;
|
165
|
+
}
|
166
|
+
|
167
|
+
if ((size_t)len + 1 <= buf->asize - buf->size) {
|
168
|
+
buf->size += len;
|
169
|
+
break;
|
170
|
+
}
|
171
|
+
|
172
|
+
ENSURE_SIZE(buf, buf->size + len + 1);
|
173
|
+
}
|
174
|
+
|
175
|
+
return 0;
|
176
|
+
}
|
177
|
+
|
178
|
+
int gh_buf_printf(gh_buf *buf, const char *format, ...)
|
179
|
+
{
|
180
|
+
int r;
|
181
|
+
va_list ap;
|
182
|
+
|
183
|
+
va_start(ap, format);
|
184
|
+
r = gh_buf_vprintf(buf, format, ap);
|
185
|
+
va_end(ap);
|
186
|
+
|
187
|
+
return r;
|
188
|
+
}
|
189
|
+
|
190
|
+
void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf)
|
191
|
+
{
|
192
|
+
size_t copylen;
|
193
|
+
|
194
|
+
assert(data && datasize && buf);
|
195
|
+
|
196
|
+
data[0] = '\0';
|
197
|
+
|
198
|
+
if (buf->size == 0 || buf->asize <= 0)
|
199
|
+
return;
|
200
|
+
|
201
|
+
copylen = buf->size;
|
202
|
+
if (copylen > datasize - 1)
|
203
|
+
copylen = datasize - 1;
|
204
|
+
memmove(data, buf->ptr, copylen);
|
205
|
+
data[copylen] = '\0';
|
206
|
+
}
|
207
|
+
|
208
|
+
void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b)
|
209
|
+
{
|
210
|
+
gh_buf t = *buf_a;
|
211
|
+
*buf_a = *buf_b;
|
212
|
+
*buf_b = t;
|
213
|
+
}
|
214
|
+
|
215
|
+
char *gh_buf_detach(gh_buf *buf)
|
216
|
+
{
|
217
|
+
char *data = buf->ptr;
|
218
|
+
|
219
|
+
if (buf->asize == 0 || buf->ptr == gh_buf__oom)
|
220
|
+
return NULL;
|
221
|
+
|
222
|
+
gh_buf_init(buf, 0);
|
223
|
+
|
224
|
+
return data;
|
225
|
+
}
|
226
|
+
|
227
|
+
void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize)
|
228
|
+
{
|
229
|
+
gh_buf_free(buf);
|
230
|
+
|
231
|
+
if (ptr) {
|
232
|
+
buf->ptr = ptr;
|
233
|
+
buf->size = strlen(ptr);
|
234
|
+
if (asize)
|
235
|
+
buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
|
236
|
+
else /* pass 0 to fall back on strlen + 1 */
|
237
|
+
buf->asize = buf->size + 1;
|
238
|
+
} else {
|
239
|
+
gh_buf_grow(buf, asize);
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
int gh_buf_cmp(const gh_buf *a, const gh_buf *b)
|
244
|
+
{
|
245
|
+
int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size));
|
246
|
+
return (result != 0) ? result :
|
247
|
+
(a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
|
248
|
+
}
|
249
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) the libgit2 contributors. All rights reserved.
|
3
|
+
*
|
4
|
+
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
5
|
+
* a Linking Exception. For full terms see the included COPYING file.
|
6
|
+
*/
|
7
|
+
#ifndef INCLUDE_buffer_h__
|
8
|
+
#define INCLUDE_buffer_h__
|
9
|
+
|
10
|
+
#include <stdbool.h>
|
11
|
+
#include <stddef.h>
|
12
|
+
#include <stdarg.h>
|
13
|
+
#include <sys/types.h>
|
14
|
+
#include <stdint.h>
|
15
|
+
|
16
|
+
typedef struct {
|
17
|
+
char *ptr;
|
18
|
+
size_t asize, size;
|
19
|
+
} gh_buf;
|
20
|
+
|
21
|
+
extern char gh_buf__initbuf[];
|
22
|
+
extern char gh_buf__oom[];
|
23
|
+
|
24
|
+
#define GH_BUF_INIT { gh_buf__initbuf, 0, 0 }
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Initialize a gh_buf structure.
|
28
|
+
*
|
29
|
+
* For the cases where GH_BUF_INIT cannot be used to do static
|
30
|
+
* initialization.
|
31
|
+
*/
|
32
|
+
extern void gh_buf_init(gh_buf *buf, size_t initial_size);
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Attempt to grow the buffer to hold at least `target_size` bytes.
|
36
|
+
*
|
37
|
+
* If the allocation fails, this will return an error. If mark_oom is true,
|
38
|
+
* this will mark the buffer as invalid for future operations; if false,
|
39
|
+
* existing buffer content will be preserved, but calling code must handle
|
40
|
+
* that buffer was not expanded.
|
41
|
+
*/
|
42
|
+
extern int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom);
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Grow the buffer to hold at least `target_size` bytes.
|
46
|
+
*
|
47
|
+
* If the allocation fails, this will return an error and the buffer will be
|
48
|
+
* marked as invalid for future operations, invaliding contents.
|
49
|
+
*
|
50
|
+
* @return 0 on success or -1 on failure
|
51
|
+
*/
|
52
|
+
static inline int gh_buf_grow(gh_buf *buf, size_t target_size)
|
53
|
+
{
|
54
|
+
return gh_buf_try_grow(buf, target_size, true);
|
55
|
+
}
|
56
|
+
|
57
|
+
extern void gh_buf_free(gh_buf *buf);
|
58
|
+
extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Test if there have been any reallocation failures with this gh_buf.
|
62
|
+
*
|
63
|
+
* Any function that writes to a gh_buf can fail due to memory allocation
|
64
|
+
* issues. If one fails, the gh_buf will be marked with an OOM error and
|
65
|
+
* further calls to modify the buffer will fail. Check gh_buf_oom() at the
|
66
|
+
* end of your sequence and it will be true if you ran out of memory at any
|
67
|
+
* point with that buffer.
|
68
|
+
*
|
69
|
+
* @return false if no error, true if allocation error
|
70
|
+
*/
|
71
|
+
static inline bool gh_buf_oom(const gh_buf *buf)
|
72
|
+
{
|
73
|
+
return (buf->ptr == gh_buf__oom);
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
static inline size_t gh_buf_len(const gh_buf *buf)
|
78
|
+
{
|
79
|
+
return buf->size;
|
80
|
+
}
|
81
|
+
|
82
|
+
extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
|
83
|
+
|
84
|
+
extern void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize);
|
85
|
+
extern char *gh_buf_detach(gh_buf *buf);
|
86
|
+
extern void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf);
|
87
|
+
|
88
|
+
static inline const char *gh_buf_cstr(const gh_buf *buf)
|
89
|
+
{
|
90
|
+
return buf->ptr;
|
91
|
+
}
|
92
|
+
|
93
|
+
/*
|
94
|
+
* Functions below that return int value error codes will return 0 on
|
95
|
+
* success or -1 on failure (which generally means an allocation failed).
|
96
|
+
* Using a gh_buf where the allocation has failed with result in -1 from
|
97
|
+
* all further calls using that buffer. As a result, you can ignore the
|
98
|
+
* return code of these functions and call them in a series then just call
|
99
|
+
* gh_buf_oom at the end.
|
100
|
+
*/
|
101
|
+
extern int gh_buf_set(gh_buf *buf, const char *data, size_t len);
|
102
|
+
extern int gh_buf_sets(gh_buf *buf, const char *string);
|
103
|
+
extern int gh_buf_putc(gh_buf *buf, char c);
|
104
|
+
extern int gh_buf_put(gh_buf *buf, const void *data, size_t len);
|
105
|
+
extern int gh_buf_puts(gh_buf *buf, const char *string);
|
106
|
+
extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
|
107
|
+
__attribute__((format (printf, 2, 3)));
|
108
|
+
extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
|
109
|
+
extern void gh_buf_clear(gh_buf *buf);
|
110
|
+
|
111
|
+
#define gh_buf_PUTS(buf, str) gh_buf_put(buf, str, sizeof(str) - 1)
|
112
|
+
|
113
|
+
#endif
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#ifndef __HOUDINI_H__
|
2
|
+
#define __HOUDINI_H__
|
3
|
+
|
4
|
+
#ifdef __cplusplus
|
5
|
+
extern "C" {
|
6
|
+
#endif
|
7
|
+
|
8
|
+
#include <stdint.h>
|
9
|
+
#include "buffer.h"
|
10
|
+
|
11
|
+
#define likely(x) __builtin_expect((x),1)
|
12
|
+
#define unlikely(x) __builtin_expect((x),0)
|
13
|
+
|
14
|
+
#ifdef HOUDINI_USE_LOCALE
|
15
|
+
# define _isxdigit(c) isxdigit(c)
|
16
|
+
# define _isdigit(c) isdigit(c)
|
17
|
+
#else
|
18
|
+
/*
|
19
|
+
* Helper _isdigit methods -- do not trust the current locale
|
20
|
+
* */
|
21
|
+
# define _isxdigit(c) (strchr("0123456789ABCDEFabcdef", (c)) != NULL)
|
22
|
+
# define _isdigit(c) ((c) >= '0' && (c) <= '9')
|
23
|
+
#endif
|
24
|
+
|
25
|
+
#define HOUDINI_ESCAPED_SIZE(x) (((x) * 12) / 10)
|
26
|
+
#define HOUDINI_UNESCAPED_SIZE(x) (x)
|
27
|
+
|
28
|
+
extern int houdini_escape_html(gh_buf *ob, const uint8_t *src, size_t size);
|
29
|
+
extern int houdini_escape_html0(gh_buf *ob, const uint8_t *src, size_t size, int secure);
|
30
|
+
extern int houdini_unescape_html(gh_buf *ob, const uint8_t *src, size_t size);
|
31
|
+
extern int houdini_escape_xml(gh_buf *ob, const uint8_t *src, size_t size);
|
32
|
+
extern int houdini_escape_uri(gh_buf *ob, const uint8_t *src, size_t size);
|
33
|
+
extern int houdini_escape_url(gh_buf *ob, const uint8_t *src, size_t size);
|
34
|
+
extern int houdini_escape_href(gh_buf *ob, const uint8_t *src, size_t size);
|
35
|
+
extern int houdini_unescape_uri(gh_buf *ob, const uint8_t *src, size_t size);
|
36
|
+
extern int houdini_unescape_url(gh_buf *ob, const uint8_t *src, size_t size);
|
37
|
+
extern int houdini_escape_js(gh_buf *ob, const uint8_t *src, size_t size);
|
38
|
+
extern int houdini_unescape_js(gh_buf *ob, const uint8_t *src, size_t size);
|
39
|
+
|
40
|
+
#ifdef __cplusplus
|
41
|
+
}
|
42
|
+
#endif
|
43
|
+
|
44
|
+
#endif
|