encoding/codec/
whatwg.rs

1// This is a part of encoding-next.
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 crate::codec;
8use crate::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 {
17        "encoder-only-utf-8"
18    }
19    fn whatwg_name(&self) -> Option<&'static str> {
20        Some("replacement")
21    } // WHATWG compatibility
22    fn raw_encoder(&self) -> Box<dyn RawEncoder> {
23        codec::utf_8::UTF8Encoding.raw_encoder()
24    }
25    fn raw_decoder(&self) -> Box<dyn RawDecoder> {
26        codec::error::ErrorEncoding.raw_decoder()
27    }
28}
29
30/// Algorithmic mapping for `x-user-defined` encoding.
31pub mod x_user_defined {
32    #[inline]
33    pub fn forward(code: u8) -> u16 {
34        0xf700 | (code as u16)
35    }
36
37    #[inline]
38    pub fn backward(code: u32) -> u8 {
39        if (code & !0x7f) == 0xf780 {
40            (code & 0xff) as u8
41        } else {
42            0
43        }
44    }
45}