Debian Codegraph @

ext/cgi/escape/escape.c

raw
1 #include "ruby.h"
2 #include "ruby/encoding.h"
3
4 RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow);
5 RUBY_EXTERN const char ruby_hexdigits[];
6 RUBY_EXTERN const signed char ruby_digit36_to_number_table[];
7 #define lower_hexdigits (ruby_hexdigits+0)
8 #define upper_hexdigits (ruby_hexdigits+16)
9 #define char_to_number(c) ruby_digit36_to_number_table[(unsigned char)(c)]
10
11 static VALUE rb_cCGI, rb_mUtil, rb_mEscape;
12 static ID id_accept_charset;
13
14 #define HTML_ESCAPE_MAX_LEN 6
15
16 static const struct {
17 uint8_t len;
18 char str[HTML_ESCAPE_MAX_LEN+1];
19 } html_escape_table[UCHAR_MAX+1] = {
20 #define HTML_ESCAPE(c, str) [c] = {rb_strlen_lit(str), str}
21 HTML_ESCAPE('\'', "'"),
22 HTML_ESCAPE('&', "&"),
23 HTML_ESCAPE('"', """),
24 HTML_ESCAPE('<', "&lt;"),
25 HTML_ESCAPE('>', "&gt;"),
26 #undef HTML_ESCAPE
27 };
28
29 static inline void
30 preserve_original_state(VALUE orig, VALUE dest)
31 {
32 rb_enc_associate(dest, rb_enc_get(orig));
33 }
34
35 static inline long
36 escaped_length(VALUE str)
37 {
38 const long len = RSTRING_LEN(str);
39 if (len >= LONG_MAX / HTML_ESCAPE_MAX_LEN) {
40 ruby_malloc_size_overflow(len, HTML_ESCAPE_MAX_LEN);
41 }
42 return len * HTML_ESCAPE_MAX_LEN;
43 }
44
45 static VALUE
46 optimized_escape_html(VALUE str)
47 {
48 VALUE vbuf;
49 char *buf = ALLOCV_N(char, vbuf, escaped_length(str));
50 const char *cstr = RSTRING_PTR(str);
51 const char *end = cstr + RSTRING_LEN(str);
52
53 char *dest = buf;
54 while (cstr < end) {
55 const unsigned char c = *cstr++;
56 uint8_t len = html_escape_table[c].len;
57 if (len) {
58 memcpy(dest, html_escape_table[c].str, len);
59 dest += len;
60 }
61 else {
62 *dest++ = c;
63 }
64 }
65
66 VALUE escaped;
67 if (RSTRING_LEN(str) < (dest - buf)) {
68 escaped = rb_str_new(buf, dest - buf);
69 preserve_original_state(str, escaped);
70 }
71 else {
72 escaped = rb_str_dup(str);
73 }
74 ALLOCV_END(vbuf);
75 return escaped;
76 }
77
78 static VALUE
79 optimized_unescape_html(VALUE str)
80 {
81 enum {UNICODE_MAX = 0x10ffff};
82 rb_encoding *enc = rb_enc_get(str);
83 unsigned long charlimit = (strcasecmp(rb_enc_name(enc), "UTF-8") == 0 ? UNICODE_MAX :
84 strcasecmp(rb_enc_name(enc), "ISO-8859-1") == 0 ? 256 :
85 128);
86 long i, j, len, beg = 0;
87 size_t clen, plen;
88 int overflow;
89 const char *cstr;
90 char buf[6];
91 VALUE dest = 0;
92
93 len = RSTRING_LEN(str);
94 cstr = RSTRING_PTR(str);
95
96 for (i = 0; i < len; i++) {
97 unsigned long cc;
98 char c = cstr[i];
99 if (c != '&') continue;
100 plen = i - beg;
101 if (++i >= len) break;
102 c = (unsigned char)cstr[i];
103 j = i;
104 #define MATCH(s) (len - i >= (int)rb_strlen_lit(s) && \
105 memcmp(&cstr[i], s, rb_strlen_lit(s)) == 0 && \
106 (i += rb_strlen_lit(s) - 1, 1))
107 switch (c) {
108 case 'a':
109 ++i;
110 if (MATCH("pos;")) {
111 c = '\'';
112 }
113 else if (MATCH("mp;")) {
114 c = '&';
115 }
116 else {
117 i = j;
118 continue;
119 }
120 break;
121 case 'q':
122 ++i;
123 if (MATCH("uot;")) {
124 c = '"';
125 }
126 else {
127 i = j;
128 continue;
129 }
130 break;
131 case 'g':
132 ++i;
133 if (MATCH("t;")) {
134 c = '>';
135 }
136 else {
137 i = j;
138 continue;
139 }
140 break;
141 case 'l':
142 ++i;
143 if (MATCH("t;")) {
144 c = '<';
145 }
146 else {
147 i = j;
148 continue;
149 }
150 break;
151 case '#':
152 if (len - ++i >= 2 && ISDIGIT(cstr[i])) {
153 cc = ruby_scan_digits(&cstr[i], len-i, 10, &clen, &overflow);
154 }
155 else if ((cstr[i] == 'x' || cstr[i] == 'X') && len - ++i >= 2 && ISXDIGIT(cstr[i])) {
156 cc = ruby_scan_digits(&cstr[i], len-i, 16, &clen, &overflow);
157 }
158 else {
159 i = j;
160 continue;
161 }
162 i += clen;
163 if (overflow || cc >= charlimit || cstr[i] != ';') {
164 i = j;
165 continue;
166 }
167 if (!dest) {
168 dest = rb_str_buf_new(len);
169 }
170 rb_str_cat(dest, cstr + beg, plen);
171 if (charlimit > 256) {
172 rb_str_cat(dest, buf, rb_enc_mbcput((OnigCodePoint)cc, buf, enc));
173 }
174 else {
175 c = (unsigned char)cc;
176 rb_str_cat(dest, &c, 1);
177 }
178 beg = i + 1;
179 continue;
180 default:
181 --i;
182 continue;
183 }
184 if (!dest) {
185 dest = rb_str_buf_new(len);
186 }
187 rb_str_cat(dest, cstr + beg, plen);
188 rb_str_cat(dest, &c, 1);
189 beg = i + 1;
190 }
191
192 if (dest) {
193 rb_str_cat(dest, cstr + beg, len - beg);
194 preserve_original_state(str, dest);
195 return dest;
196 }
197 else {
198 return rb_str_dup(str);
199 }
200 }
201
202 static unsigned char
203 url_unreserved_char(unsigned char c)
204 {
205 switch (c) {
206 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
207 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j':
208 case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't':
209 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
210 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':
211 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':
212 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
213 case '-': case '.': case '_': case '~':
214 return 1;
215 default:
216 break;
217 }
218 return 0;
219 }
220
221 static VALUE
222 optimized_escape(VALUE str, int plus_escape)
223 {
224 long i, len, beg = 0;
225 VALUE dest = 0;
226 const char *cstr;
227 char buf[4] = {'%'};
228
229 len = RSTRING_LEN(str);
230 cstr = RSTRING_PTR(str);
231
232 for (i = 0; i < len; ++i) {
233 const unsigned char c = (unsigned char)cstr[i];
234 if (!url_unreserved_char(c)) {
235 if (!dest) {
236 dest = rb_str_buf_new(len);
237 }
238
239 rb_str_cat(dest, cstr + beg, i - beg);
240 beg = i + 1;
241
242 if (plus_escape && c == ' ') {
243 rb_str_cat_cstr(dest, "+");
244 }
245 else {
246 buf[1] = upper_hexdigits[(c >> 4) & 0xf];
247 buf[2] = upper_hexdigits[c & 0xf];
248 rb_str_cat(dest, buf, 3);
249 }
250 }
251 }
252
253 if (dest) {
254 rb_str_cat(dest, cstr + beg, len - beg);
255 preserve_original_state(str, dest);
256 return dest;
257 }
258 else {
259 return rb_str_dup(str);
260 }
261 }
262
263 static VALUE
264 optimized_unescape(VALUE str, VALUE encoding, int unescape_plus)
265 {
266 long i, len, beg = 0;
267 VALUE dest = 0;
268 const char *cstr;
269 rb_encoding *enc = rb_to_encoding(encoding);
270 int cr, origenc, encidx = rb_enc_to_index(enc);
271
272 len = RSTRING_LEN(str);
273 cstr = RSTRING_PTR(str);
274
275 for (i = 0; i < len; ++i) {
276 char buf[1];
277 const char c = cstr[i];
278 int clen = 0;
279 if (c == '%') {
280 if (i + 3 > len) break;
281 if (!ISXDIGIT(cstr[i+1])) continue;
282 if (!ISXDIGIT(cstr[i+2])) continue;
283 buf[0] = ((char_to_number(cstr[i+1]) << 4)
284 | char_to_number(cstr[i+2]));
285 clen = 2;
286 }
287 else if (unescape_plus && c == '+') {
288 buf[0] = ' ';
289 }
290 else {
291 continue;
292 }
293
294 if (!dest) {
295 dest = rb_str_buf_new(len);
296 }
297
298 rb_str_cat(dest, cstr + beg, i - beg);
299 i += clen;
300 beg = i + 1;
301
302 rb_str_cat(dest, buf, 1);
303 }
304
305 if (dest) {
306 rb_str_cat(dest, cstr + beg, len - beg);
307 preserve_original_state(str, dest);
308 cr = ENC_CODERANGE_UNKNOWN;
309 }
310 else {
311 dest = rb_str_dup(str);
312 cr = ENC_CODERANGE(str);
313 }
314 origenc = rb_enc_get_index(str);
315 if (origenc != encidx) {
316 rb_enc_associate_index(dest, encidx);
317 if (!ENC_CODERANGE_CLEAN_P(rb_enc_str_coderange(dest))) {
318 rb_enc_associate_index(dest, origenc);
319 if (cr != ENC_CODERANGE_UNKNOWN)
320 ENC_CODERANGE_SET(dest, cr);
321 }
322 }
323 return dest;
324 }
325
326 /*
327 * call-seq:
328 * CGI.escapeHTML(string) -> string
329 *
330 * Returns HTML-escaped string.
331 *
332 */
333 static VALUE
334 cgiesc_escape_html(VALUE self, VALUE str)
335 {
336 StringValue(str);
337
338 if (rb_enc_str_asciicompat_p(str)) {
339 return optimized_escape_html(str);
340 }
341 else {
342 return rb_call_super(1, &str);
343 }
344 }
345
346 /*
347 * call-seq:
348 * CGI.unescapeHTML(string) -> string
349 *
350 * Returns HTML-unescaped string.
351 *
352 */
353 static VALUE
354 cgiesc_unescape_html(VALUE self, VALUE str)
355 {
356 StringValue(str);
357
358 if (rb_enc_str_asciicompat_p(str)) {
359 return optimized_unescape_html(str);
360 }
361 else {
362 return rb_call_super(1, &str);
363 }
364 }
365
366 /*
367 * call-seq:
368 * CGI.escape(string) -> string
369 *
370 * Returns URL-escaped string (+application/x-www-form-urlencoded+).
371 *
372 */
373 static VALUE
374 cgiesc_escape(VALUE self, VALUE str)
375 {
376 StringValue(str);
377
378 if (rb_enc_str_asciicompat_p(str)) {
379 return optimized_escape(str, 1);
380 }
381 else {
382 return rb_call_super(1, &str);
383 }
384 }
385
386 static VALUE
387 accept_charset(int argc, VALUE *argv, VALUE self)
388 {
389 if (argc > 0)
390 return argv[0];
391 return rb_cvar_get(CLASS_OF(self), id_accept_charset);
392 }
393
394 /*
395 * call-seq:
396 * CGI.unescape(string, encoding=@@accept_charset) -> string
397 *
398 * Returns URL-unescaped string (+application/x-www-form-urlencoded+).
399 *
400 */
401 static VALUE
402 cgiesc_unescape(int argc, VALUE *argv, VALUE self)
403 {
404 VALUE str = (rb_check_arity(argc, 1, 2), argv[0]);
405
406 StringValue(str);
407
408 if (rb_enc_str_asciicompat_p(str)) {
409 VALUE enc = accept_charset(argc-1, argv+1, self);
410 return optimized_unescape(str, enc, 1);
411 }
412 else {
413 return rb_call_super(argc, argv);
414 }
415 }
416
417 /*
418 * call-seq:
419 * CGI.escapeURIComponent(string) -> string
420 *
421 * Returns URL-escaped string following RFC 3986.
422 *
423 */
424 static VALUE
425 cgiesc_escape_uri_component(VALUE self, VALUE str)
426 {
427 StringValue(str);
428
429 if (rb_enc_str_asciicompat_p(str)) {
430 return optimized_escape(str, 0);
431 }
432 else {
433 return rb_call_super(1, &str);
434 }
435 }
436
437 /*
438 * call-seq:
439 * CGI.unescapeURIComponent(string, encoding=@@accept_charset) -> string
440 *
441 * Returns URL-unescaped string following RFC 3986.
442 *
443 */
444 static VALUE
445 cgiesc_unescape_uri_component(int argc, VALUE *argv, VALUE self)
446 {
447 VALUE str = (rb_check_arity(argc, 1, 2), argv[0]);
448
449 StringValue(str);
450
451 if (rb_enc_str_asciicompat_p(str)) {
452 VALUE enc = accept_charset(argc-1, argv+1, self);
453 return optimized_unescape(str, enc, 0);
454 }
455 else {
456 return rb_call_super(argc, argv);
457 }
458 }
459
460 void
461 Init_escape(void)
462 {
463 #ifdef HAVE_RB_EXT_RACTOR_SAFE
464 rb_ext_ractor_safe(true);
465 #endif
466
467 id_accept_charset = rb_intern_const("@@accept_charset");
468 InitVM(escape);
469 }
470
471 void
472 InitVM_escape(void)
473 {
474 rb_cCGI = rb_define_class("CGI", rb_cObject);
475 rb_mEscape = rb_define_module_under(rb_cCGI, "Escape");
476 rb_mUtil = rb_define_module_under(rb_cCGI, "Util");
477 rb_define_method(rb_mEscape, "escapeHTML", cgiesc_escape_html, 1);
478 rb_define_method(rb_mEscape, "unescapeHTML", cgiesc_unescape_html, 1);
479 rb_define_method(rb_mEscape, "escapeURIComponent", cgiesc_escape_uri_component, 1);
480 rb_define_alias(rb_mEscape, "escape_uri_component", "escapeURIComponent");
481 rb_define_method(rb_mEscape, "unescapeURIComponent", cgiesc_unescape_uri_component, -1);
482 rb_define_alias(rb_mEscape, "unescape_uri_component", "unescapeURIComponent");
483 rb_define_method(rb_mEscape, "escape", cgiesc_escape, 1);
484 rb_define_method(rb_mEscape, "unescape", cgiesc_unescape, -1);
485 rb_prepend_module(rb_mUtil, rb_mEscape);
486 rb_extend_object(rb_cCGI, rb_mEscape);
487 }