| 1 |
// Copyright Mozilla Foundation |
| 2 |
// |
| 3 |
// Licensed under the Apache License (Version 2.0), or the MIT license, |
| 4 |
// (the "Licenses") at your option. You may not use this file except in |
| 5 |
// compliance with one of the Licenses. You may obtain copies of the |
| 6 |
// Licenses at: |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// http://opensource.org/licenses/MIT |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the Licenses is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the Licenses for the specific language governing permissions and |
| 15 |
// limitations under the Licenses. |
| 16 |
|
| 17 |
//! `xmldecl::parse()` extracts an encoding from an ASCII-based bogo-XML |
| 18 |
//! declaration in `text/html` in a WebKit-compatible way. |
| 19 |
|
| 20 |
extern crate encoding_rs; |
| 21 |
|
| 22 |
fn position(needle: u8, haystack: &[u8]) -> Option<usize> { |
| 23 |
haystack.iter().position(|&x| x == needle) |
| 24 |
} |
| 25 |
|
| 26 |
// The standard library lacks subslice search. |
| 27 |
// Since our needle, "encoding" is short, 'g' occurs in it only once, |
| 28 |
// and the other letters we expect to skip over are "version", let's |
| 29 |
// search for 'g' and verify match. |
| 30 |
fn skip_encoding(hay: &[u8]) -> Option<&[u8]> { |
| 31 |
let mut haystack = hay; |
| 32 |
loop { |
| 33 |
if let Some(g) = position(b'g', haystack) { |
| 34 |
let (head, tail) = haystack.split_at(g + 1); |
| 35 |
if let Some(_) = head.strip_suffix(b"encoding") { |
| 36 |
return Some(tail); |
| 37 |
} |
| 38 |
haystack = tail; |
| 39 |
} else { |
| 40 |
return None; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/// Extracts an encoding from an ASCII-based bogo-XML declaration. |
| 46 |
/// `bytes` must the prefix of a `text/html` resource. |
| 47 |
/// |
| 48 |
/// The intended use is that when the `meta` prescan fails, the HTML |
| 49 |
/// parser will have buffered the head section or the first 1024 |
| 50 |
/// bytes (whichever is larger) at which point the should be passed to |
| 51 |
/// this function. |
| 52 |
pub fn parse(bytes: &[u8]) -> Option<&'static encoding_rs::Encoding> { |
| 53 |
if let Some(after_xml) = bytes.strip_prefix(b"<?xml") { |
| 54 |
if let Some(gt) = position(b'>', after_xml) { |
| 55 |
let until_gt = &after_xml[..gt]; |
| 56 |
if let Some(tail) = skip_encoding(until_gt) { |
| 57 |
let mut pos = 0; |
| 58 |
loop { |
| 59 |
if pos >= tail.len() { |
| 60 |
return None; |
| 61 |
} |
| 62 |
let c = tail[pos]; |
| 63 |
pos += 1; |
| 64 |
if c == b'=' { |
| 65 |
break; |
| 66 |
} |
| 67 |
if c <= b' ' { |
| 68 |
continue; |
| 69 |
} |
| 70 |
return None; |
| 71 |
} |
| 72 |
// pos is now the index of the byte after = |
| 73 |
let is_single_quoted; |
| 74 |
let label_start; |
| 75 |
loop { |
| 76 |
if pos >= tail.len() { |
| 77 |
return None; |
| 78 |
} |
| 79 |
let c = tail[pos]; |
| 80 |
pos += 1; |
| 81 |
if c == b'"' { |
| 82 |
is_single_quoted = false; |
| 83 |
label_start = pos; |
| 84 |
break; |
| 85 |
} |
| 86 |
if c == b'\'' { |
| 87 |
is_single_quoted = true; |
| 88 |
label_start = pos; |
| 89 |
break; |
| 90 |
} |
| 91 |
if c <= b' ' { |
| 92 |
continue; |
| 93 |
} |
| 94 |
return None; |
| 95 |
} |
| 96 |
loop { |
| 97 |
if pos >= tail.len() { |
| 98 |
return None; |
| 99 |
} |
| 100 |
let c = tail[pos]; |
| 101 |
if c <= b' ' { |
| 102 |
return None; |
| 103 |
} |
| 104 |
if (c == b'"' && !is_single_quoted) || (c == b'\'' && is_single_quoted) { |
| 105 |
let encoding = encoding_rs::Encoding::for_label(&tail[label_start..pos]); |
| 106 |
if encoding == Some(encoding_rs::UTF_16LE) |
| 107 |
|| encoding == Some(encoding_rs::UTF_16BE) |
| 108 |
{ |
| 109 |
return Some(encoding_rs::UTF_8); |
| 110 |
} |
| 111 |
return encoding; |
| 112 |
} |
| 113 |
pos += 1; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
None |
| 119 |
} |
| 120 |
|
| 121 |
// Any copyright to the test code below this comment is dedicated to the |
| 122 |
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ |
| 123 |
|
| 124 |
#[cfg(test)] |
| 125 |
mod tests { |
| 126 |
use super::parse; |
| 127 |
#[test] |
| 128 |
fn baseline() { |
| 129 |
assert_eq!( |
| 130 |
parse(b"<?xml version=\"1.0\" encoding=\"windows-1251\"?>AAAA"), |
| 131 |
Some(encoding_rs::WINDOWS_1251) |
| 132 |
); |
| 133 |
} |
| 134 |
#[test] |
| 135 |
fn meta_encoding_before_charset() { |
| 136 |
assert_eq!(parse(b"<?xml version=\"1.0\" <meta encoding=\"windows-1251\" charset=\"windows-1253\"?>AAAA"), Some(encoding_rs::WINDOWS_1251)); |
| 137 |
} |
| 138 |
#[test] |
| 139 |
fn lt() { |
| 140 |
assert_eq!( |
| 141 |
parse(b"<?xml<encoding=\"windows-1251\"?>AAAA"), |
| 142 |
Some(encoding_rs::WINDOWS_1251) |
| 143 |
); |
| 144 |
} |
| 145 |
#[test] |
| 146 |
fn unmatched_quotes() { |
| 147 |
assert_eq!( |
| 148 |
parse(b"<?xml version=\"1.0\" encoding=\"windows-1251'?>AAAA"), |
| 149 |
None |
| 150 |
); |
| 151 |
} |
| 152 |
#[test] |
| 153 |
fn no_version() { |
| 154 |
assert_eq!( |
| 155 |
parse(b"<?xml encoding=\"windows-1251\"?>AAAA"), |
| 156 |
Some(encoding_rs::WINDOWS_1251) |
| 157 |
); |
| 158 |
} |
| 159 |
#[test] |
| 160 |
fn no_quotes_space() { |
| 161 |
assert_eq!( |
| 162 |
parse(b"<?xml version=\"1.0\" encoding=windows-1251 ?>AAAA"), |
| 163 |
None |
| 164 |
); |
| 165 |
} |
| 166 |
#[test] |
| 167 |
fn no_quotes() { |
| 168 |
assert_eq!( |
| 169 |
parse(b"<?xml version=\"1.0\" encoding=windows-1251?>AAAA"), |
| 170 |
None |
| 171 |
); |
| 172 |
} |
| 173 |
#[test] |
| 174 |
fn no_space_no_version_line_breaks_trailing_body() { |
| 175 |
assert_eq!( |
| 176 |
parse(b"<?xmlencoding \n = \n 'windows-1251'<body>AAAA"), |
| 177 |
Some(encoding_rs::WINDOWS_1251) |
| 178 |
); |
| 179 |
} |
| 180 |
#[test] |
| 181 |
fn space_before_label() { |
| 182 |
assert_eq!( |
| 183 |
parse(b"<?xml version=\"1.0\" encoding=\" windows-1251\"?>AAAA"), |
| 184 |
None |
| 185 |
); |
| 186 |
} |
| 187 |
#[test] |
| 188 |
fn space_after_label() { |
| 189 |
assert_eq!( |
| 190 |
parse(b"<?xml version=\"1.0\" encoding=\"windows-1251 \"?>AAAA"), |
| 191 |
None |
| 192 |
); |
| 193 |
} |
| 194 |
#[test] |
| 195 |
fn one_around_label() { |
| 196 |
assert_eq!( |
| 197 |
parse(b"<?xml version=\"1.0\" encoding=\"\x01windows-1251\x01\"?>AAAA"), |
| 198 |
None |
| 199 |
); |
| 200 |
} |
| 201 |
#[test] |
| 202 |
fn one_around_equals() { |
| 203 |
assert_eq!( |
| 204 |
parse(b"<?xml version=\"1.0\" encoding\x01=\x01\"windows-1251\"?>AAAA"), |
| 205 |
Some(encoding_rs::WINDOWS_1251) |
| 206 |
); |
| 207 |
} |
| 208 |
#[test] |
| 209 |
fn no_version_no_space_trailing_lt_without_question_mark() { |
| 210 |
assert_eq!( |
| 211 |
parse(b"<?xmlencoding=\"windows-1251\"<>AAAA"), |
| 212 |
Some(encoding_rs::WINDOWS_1251) |
| 213 |
); |
| 214 |
} |
| 215 |
#[test] |
| 216 |
fn no_version_no_space_spaces_around_equals_single_quotes_trailing_body() { |
| 217 |
assert_eq!( |
| 218 |
parse(b"<?xmlencoding = 'windows-1251'<body>AAAA"), |
| 219 |
Some(encoding_rs::WINDOWS_1251) |
| 220 |
); |
| 221 |
} |
| 222 |
#[test] |
| 223 |
fn no_version_no_space_single_quotes_trailing_body() { |
| 224 |
assert_eq!( |
| 225 |
parse(b"<?xmlencoding='windows-1251'<body>AAAA"), |
| 226 |
Some(encoding_rs::WINDOWS_1251) |
| 227 |
); |
| 228 |
} |
| 229 |
#[test] |
| 230 |
fn no_version_no_space_double_quotes_trailing_body() { |
| 231 |
assert_eq!( |
| 232 |
parse(b"<?xmlencoding=\"windows-1251\"<body>AAAA"), |
| 233 |
Some(encoding_rs::WINDOWS_1251) |
| 234 |
); |
| 235 |
} |
| 236 |
#[test] |
| 237 |
fn no_version_no_space_no_trailing_question_mark() { |
| 238 |
assert_eq!( |
| 239 |
parse(b"<?xmlencoding=\"windows-1251\">AAAA"), |
| 240 |
Some(encoding_rs::WINDOWS_1251) |
| 241 |
); |
| 242 |
} |
| 243 |
#[test] |
| 244 |
fn no_version_no_space() { |
| 245 |
assert_eq!( |
| 246 |
parse(b"<?xmlencoding=\"windows-1251\"?>AAAA"), |
| 247 |
Some(encoding_rs::WINDOWS_1251) |
| 248 |
); |
| 249 |
} |
| 250 |
#[test] |
| 251 |
fn upper_case_xml() { |
| 252 |
assert_eq!( |
| 253 |
parse(b"<?XML version=\"1.0\" encoding=\"windows-1251\"?>AAAA"), |
| 254 |
None |
| 255 |
); |
| 256 |
} |
| 257 |
#[test] |
| 258 |
fn meta_charset_before_encoding() { |
| 259 |
assert_eq!(parse(b"<?xml version=\"1.0\" <meta charset=\"windows-1253\" encoding=\"windows-1251\"?>AAAA"), Some(encoding_rs::WINDOWS_1251)); |
| 260 |
} |
| 261 |
#[test] |
| 262 |
fn lt_between_xml_and_encoding() { |
| 263 |
assert_eq!( |
| 264 |
parse(b"<?xml<encoding=\"windows-1251\"?>AAAA"), |
| 265 |
Some(encoding_rs::WINDOWS_1251) |
| 266 |
); |
| 267 |
} |
| 268 |
#[test] |
| 269 |
fn letter_between_xml_and_encoding() { |
| 270 |
assert_eq!( |
| 271 |
parse(b"<?xmlaencoding=\"windows-1251\"?>AAAA"), |
| 272 |
Some(encoding_rs::WINDOWS_1251) |
| 273 |
); |
| 274 |
} |
| 275 |
#[test] |
| 276 |
fn gt_between_xml_and_encoding() { |
| 277 |
assert_eq!(parse(b"<?xml>encoding=\"windows-1251\"?>"), None); |
| 278 |
} |
| 279 |
#[test] |
| 280 |
fn non_primary_label() { |
| 281 |
assert_eq!( |
| 282 |
parse(b"<?xml version=\"1.0\" encoding=\"cp1251\"?>AAAA"), |
| 283 |
Some(encoding_rs::WINDOWS_1251) |
| 284 |
); |
| 285 |
} |
| 286 |
#[test] |
| 287 |
fn upper_case_label() { |
| 288 |
assert_eq!( |
| 289 |
parse(b"<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>AAAA"), |
| 290 |
Some(encoding_rs::WINDOWS_1251) |
| 291 |
); |
| 292 |
} |
| 293 |
#[test] |
| 294 |
fn upper_case_version() { |
| 295 |
assert_eq!( |
| 296 |
parse(b"<?xml VERSION=\"1.0\" encoding=\"windows-1251\"?>AAAA"), |
| 297 |
Some(encoding_rs::WINDOWS_1251) |
| 298 |
); |
| 299 |
} |
| 300 |
#[test] |
| 301 |
fn upper_case_encoding() { |
| 302 |
assert_eq!( |
| 303 |
parse(b"<?xml version=\"1.0\" ENCODING=\"windows-1251\"?>AAAA"), |
| 304 |
None |
| 305 |
); |
| 306 |
} |
| 307 |
#[test] |
| 308 |
fn space_before() { |
| 309 |
assert_eq!( |
| 310 |
parse(b" <?xml version=\"1.0\" encoding=\"windows-1251\"?>AAAA"), |
| 311 |
None |
| 312 |
); |
| 313 |
} |
| 314 |
#[test] |
| 315 |
fn encoding_equals_encoding() { |
| 316 |
assert_eq!( |
| 317 |
parse(b"<?xml version=\"1.0\" encoding=encoding=\"windows-1251\"?>AAAA"), |
| 318 |
None |
| 319 |
); |
| 320 |
} |
| 321 |
#[test] |
| 322 |
fn encodingencoding() { |
| 323 |
assert_eq!( |
| 324 |
parse(b"<?xml version=\"1.0\" encodingencoding=\"windows-1251\"?>AAAA"), |
| 325 |
None |
| 326 |
); |
| 327 |
} |
| 328 |
#[test] |
| 329 |
fn utf16() { |
| 330 |
assert_eq!( |
| 331 |
parse(b"<?xml version=\"1.0\" encoding=\"UTF-16\"?>AAAA"), |
| 332 |
Some(encoding_rs::UTF_8) |
| 333 |
); |
| 334 |
} |
| 335 |
#[test] |
| 336 |
fn utf16le() { |
| 337 |
assert_eq!( |
| 338 |
parse(b"<?xml version=\"1.0\" encoding=\"UTF-16LE\"?>AAAA"), |
| 339 |
Some(encoding_rs::UTF_8) |
| 340 |
); |
| 341 |
} |
| 342 |
#[test] |
| 343 |
fn utf16be() { |
| 344 |
assert_eq!( |
| 345 |
parse(b"<?xml version=\"1.0\" encoding=\"UTF-16BE\"?>AAAA"), |
| 346 |
Some(encoding_rs::UTF_8) |
| 347 |
); |
| 348 |
} |
| 349 |
#[test] |
| 350 |
fn bytes_1024() { |
| 351 |
let mut v = Vec::new(); |
| 352 |
v.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"windows-1251\""); |
| 353 |
while v.len() < 1022 { |
| 354 |
v.push(b' '); |
| 355 |
} |
| 356 |
v.extend_from_slice(b"?>AAAA"); |
| 357 |
assert_eq!(v.len(), 1028); |
| 358 |
assert_eq!(parse(&v), Some(encoding_rs::WINDOWS_1251)); |
| 359 |
} |
| 360 |
#[test] |
| 361 |
fn bytes_1025() { |
| 362 |
let mut v = Vec::new(); |
| 363 |
v.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"windows-1251\""); |
| 364 |
while v.len() < 1023 { |
| 365 |
v.push(b' '); |
| 366 |
} |
| 367 |
v.extend_from_slice(b"?>AAAA"); |
| 368 |
assert_eq!(v.len(), 1029); |
| 369 |
assert_eq!(parse(&v), Some(encoding_rs::WINDOWS_1251)); |
| 370 |
} |
| 371 |
} |