encoding/codec/
whatwg.rs

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
7use codec;
8use 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)]
13pub struct EncoderOnlyUTF8Encoding;
14
15impl Encoding for EncoderOnlyUTF8Encoding {
16    fn name(&self) -> &'static str { "encoder-only-utf-8" }
17    fn whatwg_name(&self) -> Option<&'static str> { Some("replacement") } // WHATWG compatibility
18    fn raw_encoder(&self) -> Box<RawEncoder> { codec::utf_8::UTF8Encoding.raw_encoder() }
19    fn raw_decoder(&self) -> Box<RawDecoder> { codec::error::ErrorEncoding.raw_decoder() }
20}
21
22/// Algorithmic mapping for `x-user-defined` encoding.
23pub mod x_user_defined {
24    #[inline]
25    pub fn forward(code: u8) -> u16 {
26        0xf700 | (code as u16)
27    }
28
29    #[inline]
30    pub fn backward(code: u32) -> u8 {
31        if (code & !0x7f) == 0xf780 {(code & 0xff) as u8} else {0}
32    }
33}
34