| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
use std::borrow::Cow; |
| 55 |
|
| 56 |
|
| 57 |
pub struct CodecError { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
pub upto: isize, |
| 65 |
|
| 66 |
pub cause: Cow<'static, str>, |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
pub trait ByteWriter { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
fn writer_hint(&mut self, _expectedlen: usize) {} |
| 78 |
|
| 79 |
|
| 80 |
fn write_byte(&mut self, b: u8); |
| 81 |
|
| 82 |
|
| 83 |
fn write_bytes(&mut self, v: &[u8]); |
| 84 |
} |
| 85 |
|
| 86 |
impl ByteWriter for Vec<u8> { |
| 87 |
fn writer_hint(&mut self, expectedlen: usize) { |
| 88 |
self.reserve(expectedlen); |
| 89 |
} |
| 90 |
|
| 91 |
fn write_byte(&mut self, b: u8) { |
| 92 |
self.push(b); |
| 93 |
} |
| 94 |
|
| 95 |
fn write_bytes(&mut self, v: &[u8]) { |
| 96 |
self.extend(v.iter().cloned()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
pub trait StringWriter { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
fn writer_hint(&mut self, _expectedlen: usize) {} |
| 109 |
|
| 110 |
|
| 111 |
fn write_char(&mut self, c: char); |
| 112 |
|
| 113 |
|
| 114 |
fn write_str(&mut self, s: &str); |
| 115 |
} |
| 116 |
|
| 117 |
impl StringWriter for String { |
| 118 |
fn writer_hint(&mut self, expectedlen: usize) { |
| 119 |
let newlen = self.len() + expectedlen; |
| 120 |
self.reserve(newlen); |
| 121 |
} |
| 122 |
|
| 123 |
fn write_char(&mut self, c: char) { |
| 124 |
self.push(c); |
| 125 |
} |
| 126 |
|
| 127 |
fn write_str(&mut self, s: &str) { |
| 128 |
self.push_str(s); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
pub trait RawEncoder: 'static { |
| 135 |
|
| 136 |
fn from_self(&self) -> Box<RawEncoder>; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
fn is_ascii_compatible(&self) -> bool { false } |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
fn raw_feed(&mut self, input: &str, output: &mut ByteWriter) -> (usize, Option<CodecError>); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
fn raw_finish(&mut self, output: &mut ByteWriter) -> Option<CodecError>; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
pub trait RawDecoder: 'static { |
| 159 |
|
| 160 |
fn from_self(&self) -> Box<RawDecoder>; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn is_ascii_compatible(&self) -> bool { false } |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn raw_feed(&mut self, input: &[u8], output: &mut StringWriter) -> (usize, Option<CodecError>); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
fn raw_finish(&mut self, output: &mut StringWriter) -> Option<CodecError>; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
pub type EncodingRef = &'static (Encoding + Send + Sync); |
| 182 |
|
| 183 |
|
| 184 |
pub trait Encoding { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
fn name(&self) -> &'static str; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
fn whatwg_name(&self) -> Option<&'static str> { None } |
| 193 |
|
| 194 |
|
| 195 |
fn raw_encoder(&self) -> Box<RawEncoder>; |
| 196 |
|
| 197 |
|
| 198 |
fn raw_decoder(&self) -> Box<RawDecoder>; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
fn encode(&self, input: &str, trap: EncoderTrap) -> Result<Vec<u8>, Cow<'static, str>> { |
| 205 |
let mut ret = Vec::new(); |
| 206 |
self.encode_to(input, trap, &mut ret).map(|_| ret) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
fn encode_to(&self, input: &str, trap: EncoderTrap, ret: &mut ByteWriter) |
| 211 |
-> Result<(), Cow<'static, str>> |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
let mut encoder = self.raw_encoder(); |
| 216 |
let mut remaining = 0; |
| 217 |
|
| 218 |
loop { |
| 219 |
let (offset, err) = encoder.raw_feed(&input[remaining..], ret); |
| 220 |
let unprocessed = remaining + offset; |
| 221 |
match err { |
| 222 |
Some(err) => { |
| 223 |
remaining = (remaining as isize + err.upto) as usize; |
| 224 |
if !trap.trap(&mut *encoder, &input[unprocessed..remaining], ret) { |
| 225 |
return Err(err.cause); |
| 226 |
} |
| 227 |
} |
| 228 |
None => { |
| 229 |
remaining = input.len(); |
| 230 |
match encoder.raw_finish(ret) { |
| 231 |
Some(err) => { |
| 232 |
remaining = (remaining as isize + err.upto) as usize; |
| 233 |
if !trap.trap(&mut *encoder, &input[unprocessed..remaining], ret) { |
| 234 |
return Err(err.cause); |
| 235 |
} |
| 236 |
} |
| 237 |
None => {} |
| 238 |
} |
| 239 |
if remaining >= input.len() { return Ok(()); } |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
fn decode(&self, input: &[u8], trap: DecoderTrap) -> Result<String, Cow<'static, str>> { |
| 250 |
let mut ret = String::new(); |
| 251 |
self.decode_to(input, trap, &mut ret).map(|_| ret) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
fn decode_to(&self, input: &[u8], trap: DecoderTrap, ret: &mut StringWriter) |
| 259 |
-> Result<(), Cow<'static, str>> |
| 260 |
{ |
| 261 |
|
| 262 |
|
| 263 |
let mut decoder = self.raw_decoder(); |
| 264 |
let mut remaining = 0; |
| 265 |
|
| 266 |
loop { |
| 267 |
let (offset, err) = decoder.raw_feed(&input[remaining..], ret); |
| 268 |
let unprocessed = remaining + offset; |
| 269 |
match err { |
| 270 |
Some(err) => { |
| 271 |
remaining = (remaining as isize + err.upto) as usize; |
| 272 |
if !trap.trap(&mut *decoder, &input[unprocessed..remaining], ret) { |
| 273 |
return Err(err.cause); |
| 274 |
} |
| 275 |
} |
| 276 |
None => { |
| 277 |
remaining = input.len(); |
| 278 |
match decoder.raw_finish(ret) { |
| 279 |
Some(err) => { |
| 280 |
remaining = (remaining as isize + err.upto) as usize; |
| 281 |
if !trap.trap(&mut *decoder, &input[unprocessed..remaining], ret) { |
| 282 |
return Err(err.cause); |
| 283 |
} |
| 284 |
} |
| 285 |
None => {} |
| 286 |
} |
| 287 |
if remaining >= input.len() { return Ok(()); } |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
pub type EncoderTrapFunc = |
| 296 |
extern "Rust" fn(encoder: &mut RawEncoder, input: &str, output: &mut ByteWriter) -> bool; |
| 297 |
|
| 298 |
|
| 299 |
pub type DecoderTrapFunc = |
| 300 |
extern "Rust" fn(decoder: &mut RawDecoder, input: &[u8], output: &mut StringWriter) -> bool; |
| 301 |
|
| 302 |
|
| 303 |
#[derive(Copy)] |
| 304 |
pub enum DecoderTrap { |
| 305 |
|
| 306 |
|
| 307 |
Strict, |
| 308 |
|
| 309 |
|
| 310 |
Replace, |
| 311 |
|
| 312 |
Ignore, |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
Call(DecoderTrapFunc), |
| 317 |
} |
| 318 |
|
| 319 |
impl DecoderTrap { |
| 320 |
|
| 321 |
|
| 322 |
pub fn trap(&self, decoder: &mut RawDecoder, input: &[u8], output: &mut StringWriter) -> bool { |
| 323 |
match *self { |
| 324 |
DecoderTrap::Strict => false, |
| 325 |
DecoderTrap::Replace => { output.write_char('\u{fffd}'); true }, |
| 326 |
DecoderTrap::Ignore => true, |
| 327 |
DecoderTrap::Call(func) => func(decoder, input, output), |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
impl Clone for DecoderTrap { |
| 333 |
fn clone(&self) -> DecoderTrap { |
| 334 |
match *self { |
| 335 |
DecoderTrap::Strict => DecoderTrap::Strict, |
| 336 |
DecoderTrap::Replace => DecoderTrap::Replace, |
| 337 |
DecoderTrap::Ignore => DecoderTrap::Ignore, |
| 338 |
DecoderTrap::Call(f) => DecoderTrap::Call(f), |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
#[derive(Copy)] |
| 344 |
pub enum EncoderTrap { |
| 345 |
|
| 346 |
|
| 347 |
Strict, |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
Replace, |
| 352 |
|
| 353 |
Ignore, |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
NcrEscape, |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
Call(EncoderTrapFunc), |
| 362 |
} |
| 363 |
|
| 364 |
impl EncoderTrap { |
| 365 |
|
| 366 |
|
| 367 |
pub fn trap(&self, encoder: &mut RawEncoder, input: &str, output: &mut ByteWriter) -> bool { |
| 368 |
fn reencode(encoder: &mut RawEncoder, input: &str, output: &mut ByteWriter, |
| 369 |
trapname: &str) -> bool { |
| 370 |
if encoder.is_ascii_compatible() { |
| 371 |
output.write_bytes(input.as_bytes()); |
| 372 |
} else { |
| 373 |
let (_, err) = encoder.raw_feed(input, output); |
| 374 |
if err.is_some() { |
| 375 |
panic!("{} cannot reencode a replacement string", trapname); |
| 376 |
} |
| 377 |
} |
| 378 |
true |
| 379 |
} |
| 380 |
|
| 381 |
match *self { |
| 382 |
EncoderTrap::Strict => false, |
| 383 |
EncoderTrap::Replace => reencode(encoder, "?", output, "Replace"), |
| 384 |
EncoderTrap::Ignore => true, |
| 385 |
EncoderTrap::NcrEscape => { |
| 386 |
let mut escapes = String::new(); |
| 387 |
for ch in input.chars() { |
| 388 |
escapes.push_str(&format!("&#{};", ch as isize)); |
| 389 |
} |
| 390 |
reencode(encoder, &escapes, output, "NcrEscape") |
| 391 |
}, |
| 392 |
EncoderTrap::Call(func) => func(encoder, input, output), |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
impl Clone for EncoderTrap { |
| 398 |
fn clone(&self) -> EncoderTrap { |
| 399 |
match *self { |
| 400 |
EncoderTrap::Strict => EncoderTrap::Strict, |
| 401 |
EncoderTrap::Replace => EncoderTrap::Replace, |
| 402 |
EncoderTrap::Ignore => EncoderTrap::Ignore, |
| 403 |
EncoderTrap::NcrEscape => EncoderTrap::NcrEscape, |
| 404 |
EncoderTrap::Call(f) => EncoderTrap::Call(f), |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
pub fn decode(input: &[u8], trap: DecoderTrap, fallback_encoding: EncodingRef) |
| 413 |
-> (Result<String, Cow<'static, str>>, EncodingRef) { |
| 414 |
use all::{UTF_8, UTF_16LE, UTF_16BE}; |
| 415 |
if input.starts_with(&[0xEF, 0xBB, 0xBF]) { |
| 416 |
(UTF_8.decode(&input[3..], trap), UTF_8 as EncodingRef) |
| 417 |
} else if input.starts_with(&[0xFE, 0xFF]) { |
| 418 |
(UTF_16BE.decode(&input[2..], trap), UTF_16BE as EncodingRef) |
| 419 |
} else if input.starts_with(&[0xFF, 0xFE]) { |
| 420 |
(UTF_16LE.decode(&input[2..], trap), UTF_16LE as EncodingRef) |
| 421 |
} else { |
| 422 |
(fallback_encoding.decode(input, trap), fallback_encoding) |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
#[cfg(test)] |
| 427 |
mod tests { |
| 428 |
use super::*; |
| 429 |
use super::EncoderTrap::NcrEscape; |
| 430 |
use util::StrCharIndex; |
| 431 |
use std::convert::Into; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
struct MyEncoder { flag: bool, prohibit: char, prepend: &'static str, toggle: bool } |
| 437 |
impl RawEncoder for MyEncoder { |
| 438 |
fn from_self(&self) -> Box<RawEncoder> { |
| 439 |
Box::new(MyEncoder { flag: self.flag, |
| 440 |
prohibit: self.prohibit, |
| 441 |
prepend: self.prepend, |
| 442 |
toggle: false }) |
| 443 |
} |
| 444 |
fn is_ascii_compatible(&self) -> bool { self.flag } |
| 445 |
fn raw_feed(&mut self, input: &str, |
| 446 |
output: &mut ByteWriter) -> (usize, Option<CodecError>) { |
| 447 |
for ((i,j), ch) in input.index_iter() { |
| 448 |
if ch <= '\u{7f}' && ch != self.prohibit { |
| 449 |
if self.toggle && !self.prepend.is_empty() { |
| 450 |
output.write_bytes(self.prepend.as_bytes()); |
| 451 |
} |
| 452 |
output.write_byte(ch as u8); |
| 453 |
if ch == 'e' { |
| 454 |
self.toggle = !self.toggle; |
| 455 |
} |
| 456 |
} else { |
| 457 |
return (i, Some(CodecError { upto: j as isize, |
| 458 |
cause: "!!!".into() })); |
| 459 |
} |
| 460 |
} |
| 461 |
(input.len(), None) |
| 462 |
} |
| 463 |
fn raw_finish(&mut self, _output: &mut ByteWriter) -> Option<CodecError> { None } |
| 464 |
} |
| 465 |
|
| 466 |
struct MyEncoding { flag: bool, prohibit: char, prepend: &'static str } |
| 467 |
impl Encoding for MyEncoding { |
| 468 |
fn name(&self) -> &'static str { "my encoding" } |
| 469 |
fn raw_encoder(&self) -> Box<RawEncoder> { |
| 470 |
Box::new(MyEncoder { flag: self.flag, |
| 471 |
prohibit: self.prohibit, |
| 472 |
prepend: self.prepend, |
| 473 |
toggle: false }) |
| 474 |
} |
| 475 |
fn raw_decoder(&self) -> Box<RawDecoder> { panic!("not supported") } |
| 476 |
} |
| 477 |
|
| 478 |
#[test] |
| 479 |
fn test_reencoding_trap_with_ascii_compatible_encoding() { |
| 480 |
static COMPAT: &'static MyEncoding = |
| 481 |
&MyEncoding { flag: true, prohibit: '\u{80}', prepend: "" }; |
| 482 |
static INCOMPAT: &'static MyEncoding = |
| 483 |
&MyEncoding { flag: false, prohibit: '\u{80}', prepend: "" }; |
| 484 |
|
| 485 |
assert_eq!(COMPAT.encode("Hello\u{203d} I'm fine.", NcrEscape), |
| 486 |
Ok(b"Hello‽ I'm fine.".to_vec())); |
| 487 |
assert_eq!(INCOMPAT.encode("Hello\u{203d} I'm fine.", NcrEscape), |
| 488 |
Ok(b"Hello‽ I'm fine.".to_vec())); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn test_reencoding_trap_with_ascii_incompatible_encoding() { |
| 493 |
static COMPAT: &'static MyEncoding = |
| 494 |
&MyEncoding { flag: true, prohibit: '\u{80}', prepend: "*" }; |
| 495 |
static INCOMPAT: &'static MyEncoding = |
| 496 |
&MyEncoding { flag: false, prohibit: '\u{80}', prepend: "*" }; |
| 497 |
|
| 498 |
|
| 499 |
assert_eq!(COMPAT.encode("Hello\u{203d} I'm fine.", NcrEscape), |
| 500 |
Ok(b"He*l*l*o‽* *I*'*m* *f*i*n*e.".to_vec())); |
| 501 |
assert_eq!(INCOMPAT.encode("Hello\u{203d} I'm fine.", NcrEscape), |
| 502 |
Ok(b"He*l*l*o*&*#*8*2*5*3*;* *I*'*m* *f*i*n*e.".to_vec())); |
| 503 |
} |
| 504 |
|
| 505 |
#[test] |
| 506 |
#[should_panic] |
| 507 |
fn test_reencoding_trap_can_fail() { |
| 508 |
static FAIL: &'static MyEncoding = &MyEncoding { flag: false, prohibit: '&', prepend: "" }; |
| 509 |
|
| 510 |
|
| 511 |
let _ = FAIL.encode("Hello\u{203d} I'm fine.", NcrEscape); |
| 512 |
} |
| 513 |
} |