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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2015-2016 Mozilla Foundation. See the COPYRIGHT
// 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.
use handles::*;
use data::*;
use variant::*;
use super::*;
pub struct EucKrDecoder {
lead: u8,
}
impl EucKrDecoder {
pub fn new() -> VariantDecoder {
VariantDecoder::EucKr(EucKrDecoder { lead: 0 })
}
fn plus_one_if_lead(&self, byte_length: usize) -> usize {
byte_length +
if self.lead == 0 {
0
} else {
1
}
}
pub fn max_utf16_buffer_length(&self, byte_length: usize) -> usize {
self.plus_one_if_lead(byte_length)
}
pub fn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> usize {
// worst case: 2 to 3
let len = self.plus_one_if_lead(byte_length);
len + (len + 1) / 2
}
pub fn max_utf8_buffer_length(&self, byte_length: usize) -> usize {
self.plus_one_if_lead(byte_length) * 3
}
decoder_functions!({},
{
if self.lead != 0 {
self.lead = 0;
return (DecoderResult::Malformed(1, 0),
src_consumed,
dest.written());
}
},
{
if self.lead == 0 {
if b <= 0x7f {
// TODO optimize ASCII run
destination_handle.write_ascii(b);
continue;
}
if b >= 0x81 && b <= 0xFE {
self.lead = b;
continue;
}
return (DecoderResult::Malformed(1, 0),
unread_handle.consumed(),
destination_handle.written());
}
let lead = self.lead as usize;
self.lead = 0;
if b >= 0x41 && b <= 0xFE {
let pointer = (lead as usize - 0x81) * 190usize +
(b as usize - 0x41);
let bmp = euc_kr_decode(pointer);
if bmp != 0 {
destination_handle.write_bmp_excl_ascii(bmp);
continue;
}
}
if b <= 0x7F {
return (DecoderResult::Malformed(1, 0),
unread_handle.unread(),
destination_handle.written());
}
return (DecoderResult::Malformed(2, 0),
unread_handle.consumed(),
destination_handle.written());
},
self,
src_consumed,
dest,
b,
destination_handle,
unread_handle,
check_space_bmp);
}
pub struct EucKrEncoder;
impl EucKrEncoder {
pub fn new(encoding: &'static Encoding) -> Encoder {
Encoder::new(encoding, VariantEncoder::EucKr(EucKrEncoder))
}
pub fn max_buffer_length_from_utf16_without_replacement(&self, u16_length: usize) -> usize {
u16_length * 2
}
pub fn max_buffer_length_from_utf8_without_replacement(&self, byte_length: usize) -> usize {
byte_length
}
encoder_functions!({},
{
if c <= '\u{7F}' {
// TODO optimize ASCII run
destination_handle.write_one(c as u8);
continue;
}
let pointer = euc_kr_encode(c);
if pointer == usize::max_value() {
return (EncoderResult::Unmappable(c),
unread_handle.consumed(),
destination_handle.written());
}
let lead = (pointer / 190) + 0x81;
let trail = (pointer % 190) + 0x41;
destination_handle.write_two(lead as u8, trail as u8);
continue;
},
self,
src_consumed,
source,
dest,
c,
destination_handle,
unread_handle,
check_space_two);
}
// Any copyright to the test code below this comment is dedicated to the
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
#[cfg(test)]
mod tests {
use super::super::testing::*;
use super::super::*;
fn decode_euc_kr(bytes: &[u8], expect: &str) {
decode(EUC_KR, bytes, expect);
}
fn encode_euc_kr(string: &str, expect: &[u8]) {
encode(EUC_KR, string, expect);
}
#[test]
fn test_euc_kr_decode() {
// Empty
decode_euc_kr(b"", &"");
// ASCII
decode_euc_kr(b"\x61\x62", "\u{0061}\u{0062}");
decode_euc_kr(b"\x81\x41", "\u{AC02}");
decode_euc_kr(b"\x81\x5B", "\u{FFFD}\x5B");
decode_euc_kr(b"\xFD\xFE", "\u{8A70}");
decode_euc_kr(b"\xFE\x41", "\u{FFFD}\x41");
decode_euc_kr(b"\xFF\x41", "\u{FFFD}\x41");
decode_euc_kr(b"\x80\x41", "\u{FFFD}\x41");
}
#[test]
fn test_euc_kr_encode() {
// Empty
encode_euc_kr("", b"");
// ASCII
encode_euc_kr("\u{0061}\u{0062}", b"\x61\x62");
encode_euc_kr("\u{AC02}", b"\x81\x41");
encode_euc_kr("\u{8A70}", b"\xFD\xFE");
}
}