]>
git.proxmox.com Git - rustc.git/blob - vendor/pulldown-cmark-0.7.2/src/escape.rs
1 // Copyright 2015 Google Inc. All rights reserved.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 //! Utility functions for HTML escaping
24 use std
::str::from_utf8
;
26 use crate::html
::StrWrite
;
29 static HREF_SAFE
: [u8; 128] = [
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
34 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
36 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
40 static HEX_CHARS
: &[u8] = b
"0123456789ABCDEF";
41 static AMP_ESCAPE
: &str = "&";
42 static SLASH_ESCAPE
: &str = "'";
44 pub(crate) fn escape_href
<W
>(mut w
: W
, s
: &str) -> io
::Result
<()>
48 let bytes
= s
.as_bytes();
50 for i
in 0..bytes
.len() {
52 if c
>= 0x80 || HREF_SAFE
[c
as usize] == 0 {
53 // character needing escape
55 // write partial substring up to mark
57 w
.write_str(&s
[mark
..i
])?
;
61 w
.write_str(AMP_ESCAPE
)?
;
64 w
.write_str(SLASH_ESCAPE
)?
;
67 let mut buf
= [0u8; 3];
69 buf
[1] = HEX_CHARS
[((c
as usize) >> 4) & 0xF];
70 buf
[2] = HEX_CHARS
[(c
as usize) & 0xF];
71 let escaped
= from_utf8(&buf
).unwrap();
72 w
.write_str(escaped
)?
;
75 mark
= i
+ 1; // all escaped characters are ASCII
78 w
.write_str(&s
[mark
..])
81 const fn create_html_escape_table() -> [u8; 256] {
82 let mut table
= [0; 256];
83 table
[b'
"' as usize] = 1;
84 table[b'&' as usize] = 2;
85 table[b'<' as usize] = 3;
86 table[b'>' as usize] = 4;
90 static HTML_ESCAPE_TABLE: [u8; 256] = create_html_escape_table();
92 static HTML_ESCAPES: [&'static str; 5] = ["", ""
;", "&
;", "<
;", ">
;"];
94 /// Writes the given string to the Write sink, replacing special HTML bytes
95 /// (<, >, &, ") by escape sequences
.
96 pub(crate) fn escape_html
<W
: StrWrite
>(w
: W
, s
: &str) -> io
::Result
<()> {
97 #[cfg(all(target_arch = "x86_64", feature = "simd"))]
99 simd
::escape_html(w
, s
)
101 #[cfg(not(all(target_arch = "x86_64", feature = "simd")))]
103 escape_html_scalar(w
, s
)
107 fn escape_html_scalar
<W
: StrWrite
>(mut w
: W
, s
: &str) -> io
::Result
<()> {
108 let bytes
= s
.as_bytes();
114 .position(|&c
| HTML_ESCAPE_TABLE
[c
as usize] != 0)
122 let escape
= HTML_ESCAPE_TABLE
[c
as usize];
123 let escape_seq
= HTML_ESCAPES
[escape
as usize];
124 w
.write_str(&s
[mark
..i
])?
;
125 w
.write_str(escape_seq
)?
;
127 mark
= i
; // all escaped characters are ASCII
129 w
.write_str(&s
[mark
..])
132 #[cfg(all(target_arch = "x86_64", feature = "simd"))]
134 use crate::html
::StrWrite
;
135 use std
::arch
::x86_64
::*;
137 use std
::mem
::size_of
;
139 const VECTOR_SIZE
: usize = size_of
::<__m128i
>();
141 pub(crate) fn escape_html
<W
: StrWrite
>(mut w
: W
, s
: &str) -> io
::Result
<()> {
142 // The SIMD accelerated code uses the PSHUFB instruction, which is part
143 // of the SSSE3 instruction set. Further, we can only use this code if
144 // the buffer is at least one VECTOR_SIZE in length to prevent reading
145 // out of bounds. If either of these conditions is not met, we fall back
147 if is_x86_feature_detected
!("ssse3") && s
.len() >= VECTOR_SIZE
{
148 let bytes
= s
.as_bytes();
152 foreach_special_simd(bytes
, 0, |i
| {
153 let escape_ix
= *bytes
.get_unchecked(i
) as usize;
155 super::HTML_ESCAPES
[super::HTML_ESCAPE_TABLE
[escape_ix
] as usize];
156 w
.write_str(&s
.get_unchecked(mark
..i
))?
;
157 mark
= i
+ 1; // all escaped characters are ASCII
158 w
.write_str(replacement
)
160 w
.write_str(&s
.get_unchecked(mark
..))
163 super::escape_html_scalar(w
, s
)
167 /// Creates the lookup table for use in `compute_mask`.
168 const fn create_lookup() -> [u8; 16] {
169 let mut table
= [0; 16];
170 table
[(b'
<'
& 0x0f) as usize] = b'
<'
;
171 table
[(b'
>'
& 0x0f) as usize] = b'
>'
;
172 table
[(b'
&'
& 0x0f) as usize] = b'
&'
;
173 table
[(b'
"' & 0x0f) as usize] = b'"'
;
174 table
[0] = 0b0111_1111;
178 #[target_feature(enable = "ssse3")]
179 /// Computes a byte mask at given offset in the byte buffer. Its first 16 (least significant)
180 /// bits correspond to whether there is an HTML special byte (&, <, ", >) at the 16 bytes
181 /// `bytes[offset..]`. For example, the mask `(1 << 3)` states that there is an HTML byte
182 /// at `offset + 3`. It is only safe to call this function when
183 /// `bytes.len() >= offset + VECTOR_SIZE`.
184 unsafe fn compute_mask(bytes
: &[u8], offset
: usize) -> i32 {
185 debug_assert
!(bytes
.len() >= offset
+ VECTOR_SIZE
);
187 let table
= create_lookup();
188 let lookup
= _mm_loadu_si128(table
.as_ptr() as *const __m128i
);
189 let raw_ptr
= bytes
.as_ptr().offset(offset
as isize) as *const __m128i
;
191 // Load the vector from memory.
192 let vector
= _mm_loadu_si128(raw_ptr
);
193 // We take the least significant 4 bits of every byte and use them as indices
194 // to map into the lookup vector.
195 // Note that shuffle maps bytes with their most significant bit set to lookup[0].
196 // Bytes that share their lower nibble with an HTML special byte get mapped to that
197 // corresponding special byte. Note that all HTML special bytes have distinct lower
198 // nibbles. Other bytes either get mapped to 0 or 127.
199 let expected
= _mm_shuffle_epi8(lookup
, vector
);
200 // We compare the original vector to the mapped output. Bytes that shared a lower
201 // nibble with an HTML special byte match *only* if they are that special byte. Bytes
202 // that have either a 0 lower nibble or their most significant bit set were mapped to
203 // 127 and will hence never match. All other bytes have non-zero lower nibbles but
204 // were mapped to 0 and will therefore also not match.
205 let matches
= _mm_cmpeq_epi8(expected
, vector
);
207 // Translate matches to a bitmask, where every 1 corresponds to a HTML special character
208 // and a 0 is a non-HTML byte.
209 _mm_movemask_epi8(matches
)
212 /// Calls the given function with the index of every byte in the given byteslice
213 /// that is either ", &, <, or > and for no other byte.
214 /// Make sure to only call this when `bytes.len() >= 16`, undefined behaviour may
216 #[target_feature(enable = "ssse3")]
217 unsafe fn foreach_special_simd
<F
>(
223 F
: FnMut(usize) -> io
::Result
<()>,
225 // The strategy here is to walk the byte buffer in chunks of VECTOR_SIZE (16)
226 // bytes at a time starting at the given offset. For each chunk, we compute a
227 // a bitmask indicating whether the corresponding byte is a HTML special byte.
228 // We then iterate over all the 1 bits in this mask and call the callback function
229 // with the corresponding index in the buffer.
230 // When the number of HTML special bytes in the buffer is relatively low, this
231 // allows us to quickly go through the buffer without a lookup and for every
234 debug_assert
!(bytes
.len() >= VECTOR_SIZE
);
235 let upperbound
= bytes
.len() - VECTOR_SIZE
;
236 while offset
< upperbound
{
237 let mut mask
= compute_mask(bytes
, offset
);
239 let ix
= mask
.trailing_zeros();
240 callback(offset
+ ix
as usize)?
;
241 mask ^
= mask
& -mask
;
243 offset
+= VECTOR_SIZE
;
246 // Final iteration. We align the read with the end of the slice and
247 // shift off the bytes at start we have already scanned.
248 let mut mask
= compute_mask(bytes
, upperbound
);
249 mask
>>= offset
- upperbound
;
251 let ix
= mask
.trailing_zeros();
252 callback(offset
+ ix
as usize)?
;
253 mask ^
= mask
& -mask
;
259 mod html_scan_tests
{
262 let mut vec
= Vec
::new();
264 super::foreach_special_simd("&aXaaaa.a'aa9a<>aab&".as_bytes(), 0, |ix
| {
269 assert_eq
!(vec
, vec
![0, 14, 15, 19]);
272 // only match these bytes, and when we match them, match them VECTOR_SIZE times
274 fn only_right_bytes_matched() {
276 let right_byte
= b
== b'
&'
|| b
== b'
<'
|| b
== b'
>'
|| b
== b'
"';
277 let vek = vec![b; super::VECTOR_SIZE];
278 let mut match_count = 0;
280 super::foreach_special_simd(&vek, 0, |_| {
286 assert!((match_count > 0) == (match_count == super::VECTOR_SIZE));
288 (match_count == super::VECTOR_SIZE),
290 "match_count
: {}
, byte
: {:?}
",