mail_parser/decoders/charsets/
multi_byte.rs

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

#[cfg(feature = "full_encoding")]
use encoding_rs::*;

#[cfg(feature = "full_encoding")]
fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> String {
    let mut result = String::with_capacity(bytes.len() * 3);

    if let (CoderResult::OutputFull, _, _) = decoder.decode_to_string(bytes, &mut result, true) {
        debug_assert!(false, "String full while decoding.")
    }

    result.shrink_to_fit();
    result
}

pub fn decoder_shift_jis(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(SHIFT_JIS.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_big5(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(BIG5.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_euc_jp(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(EUC_JP.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_euc_kr(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(EUC_KR.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_gb18030(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(GB18030.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_gbk(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(GBK.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_iso2022_jp(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(ISO_2022_JP.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_windows874(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(WINDOWS_874.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}

pub fn decoder_ibm866(bytes: &[u8]) -> String {
    #[cfg(feature = "full_encoding")]
    {
        multi_byte_decoder(IBM866.new_decoder(), bytes)
    }

    #[cfg(not(feature = "full_encoding"))]
    {
        String::from_utf8_lossy(bytes).into_owned()
    }
}