1 // This is a part of rust-encoding.
2 // Copyright (c) 2013-2015, Kang Seonghoon.
3 // See README.md and LICENSE.txt for details.
4 
5 //! Asymmetric or special encoding constructions required by the WHATWG Encoding standard.
6 
7 use codec;
8 use types::*;
9 
10 /// Replacement encoding used to solve a particular attack vector due to mismatching server and
11 /// client supports for encodings. It is rarely useful outside.
12 #[derive(Clone, Copy)]
13 pub struct EncoderOnlyUTF8Encoding;
14 
15 impl Encoding for EncoderOnlyUTF8Encoding {
name(&self) -> &'static str16     fn name(&self) -> &'static str { "encoder-only-utf-8" }
whatwg_name(&self) -> Option<&'static str>17     fn whatwg_name(&self) -> Option<&'static str> { Some("replacement") } // WHATWG compatibility
raw_encoder(&self) -> Box<RawEncoder>18     fn raw_encoder(&self) -> Box<RawEncoder> { codec::utf_8::UTF8Encoding.raw_encoder() }
raw_decoder(&self) -> Box<RawDecoder>19     fn raw_decoder(&self) -> Box<RawDecoder> { codec::error::ErrorEncoding.raw_decoder() }
20 }
21 
22 /// Algorithmic mapping for `x-user-defined` encoding.
23 pub mod x_user_defined {
24     #[inline]
forward(code: u8) -> u1625     pub fn forward(code: u8) -> u16 {
26         0xf700 | (code as u16)
27     }
28 
29     #[inline]
backward(code: u32) -> u830     pub fn backward(code: u32) -> u8 {
31         if (code & !0x7f) == 0xf780 {(code & 0xff) as u8} else {0}
32     }
33 }
34 
35