1 // Copyright Mozilla Foundation. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 use super::*;
11 
decode(encoding: &'static Encoding, bytes: &[u8], expect: &str)12 pub fn decode(encoding: &'static Encoding, bytes: &[u8], expect: &str) {
13     let mut vec = Vec::with_capacity(bytes.len() + 32);
14     let mut string = String::with_capacity(expect.len() + 32);
15     let range = if cfg!(miri) {
16         0usize..4usize
17     } else {
18         0usize..32usize
19     };
20     for i in range {
21         vec.clear();
22         string.clear();
23         for j in 0usize..i {
24             let c = 0x40u8 + (j as u8);
25             vec.push(c);
26             string.push(c as char);
27         }
28         vec.extend_from_slice(bytes);
29         string.push_str(expect);
30         decode_without_padding_impl(encoding, &vec[..], &string[..], i);
31     }
32 }
33 
decode_without_padding(encoding: &'static Encoding, bytes: &[u8], expect: &str)34 pub fn decode_without_padding(encoding: &'static Encoding, bytes: &[u8], expect: &str) {
35     decode_without_padding_impl(encoding, bytes, expect, 0);
36 }
37 
decode_without_padding_impl( encoding: &'static Encoding, bytes: &[u8], expect: &str, padding: usize, )38 fn decode_without_padding_impl(
39     encoding: &'static Encoding,
40     bytes: &[u8],
41     expect: &str,
42     padding: usize,
43 ) {
44     decode_to_utf8_impl(encoding, bytes, expect, padding);
45     decode_to_utf16_impl(encoding, bytes, &utf16_from_utf8(expect)[..], padding);
46     decode_to_string(encoding, bytes, expect);
47 }
48 
encode(encoding: &'static Encoding, str: &str, expect: &[u8])49 pub fn encode(encoding: &'static Encoding, str: &str, expect: &[u8]) {
50     let mut vec = Vec::with_capacity(expect.len() + 32);
51     let mut string = String::with_capacity(str.len() + 32);
52     let range = if cfg!(miri) {
53         0usize..4usize
54     } else {
55         0usize..32usize
56     };
57     for i in range {
58         vec.clear();
59         string.clear();
60         for j in 0usize..i {
61             let c = 0x40u8 + (j as u8);
62             vec.push(c);
63             string.push(c as char);
64         }
65         vec.extend_from_slice(expect);
66         string.push_str(str);
67         encode_without_padding(encoding, &string[..], &vec[..]);
68     }
69 }
70 
encode_without_padding(encoding: &'static Encoding, string: &str, expect: &[u8])71 pub fn encode_without_padding(encoding: &'static Encoding, string: &str, expect: &[u8]) {
72     encode_from_utf8(encoding, string, expect);
73     encode_from_utf16(encoding, &utf16_from_utf8(string)[..], expect);
74     encode_to_vec(encoding, string, expect);
75 }
76 
decode_to_utf16(encoding: &'static Encoding, bytes: &[u8], expect: &[u16])77 pub fn decode_to_utf16(encoding: &'static Encoding, bytes: &[u8], expect: &[u16]) {
78     decode_to_utf16_impl(encoding, bytes, expect, 0);
79 }
80 
decode_to_utf16_impl( encoding: &'static Encoding, bytes: &[u8], expect: &[u16], padding: usize, )81 pub fn decode_to_utf16_impl(
82     encoding: &'static Encoding,
83     bytes: &[u8],
84     expect: &[u16],
85     padding: usize,
86 ) {
87     for i in padding..bytes.len() {
88         let (head, tail) = bytes.split_at(i);
89         decode_to_utf16_with_boundary(encoding, head, tail, expect);
90     }
91 }
92 
decode_to_utf16_with_boundary( encoding: &'static Encoding, head: &[u8], tail: &[u8], expect: &[u16], )93 pub fn decode_to_utf16_with_boundary(
94     encoding: &'static Encoding,
95     head: &[u8],
96     tail: &[u8],
97     expect: &[u16],
98 ) {
99     let mut decoder = encoding.new_decoder();
100     let mut dest: Vec<u16> = Vec::with_capacity(
101         decoder
102             .max_utf16_buffer_length(head.len() + tail.len())
103             .unwrap(),
104     );
105     let capacity = dest.capacity();
106     dest.resize(capacity, 0u16);
107     let mut total_read = 0;
108     let mut total_written = 0;
109     {
110         let (complete, read, written, _) = decoder.decode_to_utf16(head, &mut dest, false);
111         match complete {
112             CoderResult::InputEmpty => {}
113             CoderResult::OutputFull => {
114                 unreachable!();
115             }
116         }
117         total_read += read;
118         total_written += written;
119     }
120     {
121         let (complete, read, written, _) =
122             decoder.decode_to_utf16(tail, &mut dest[total_written..], true);
123         match complete {
124             CoderResult::InputEmpty => {}
125             CoderResult::OutputFull => {
126                 unreachable!();
127             }
128         }
129         total_read += read;
130         total_written += written;
131     }
132     assert_eq!(total_read, head.len() + tail.len());
133     assert_eq!(total_written, expect.len());
134     dest.truncate(total_written);
135     assert_eq!(&dest[..], expect);
136 }
137 
decode_to_utf8(encoding: &'static Encoding, bytes: &[u8], expect: &str)138 pub fn decode_to_utf8(encoding: &'static Encoding, bytes: &[u8], expect: &str) {
139     decode_to_utf8_impl(encoding, bytes, expect, 0);
140 }
141 
decode_to_utf8_impl( encoding: &'static Encoding, bytes: &[u8], expect: &str, padding: usize, )142 pub fn decode_to_utf8_impl(
143     encoding: &'static Encoding,
144     bytes: &[u8],
145     expect: &str,
146     padding: usize,
147 ) {
148     for i in padding..bytes.len() {
149         let (head, tail) = bytes.split_at(i);
150         decode_to_utf8_with_boundary(encoding, head, tail, expect);
151     }
152 }
153 
decode_to_utf8_with_boundary( encoding: &'static Encoding, head: &[u8], tail: &[u8], expect: &str, )154 pub fn decode_to_utf8_with_boundary(
155     encoding: &'static Encoding,
156     head: &[u8],
157     tail: &[u8],
158     expect: &str,
159 ) {
160     let mut decoder = encoding.new_decoder();
161     let mut dest: Vec<u8> = Vec::with_capacity(
162         decoder
163             .max_utf8_buffer_length(head.len() + tail.len())
164             .unwrap(),
165     );
166     let capacity = dest.capacity();
167     dest.resize(capacity, 0u8);
168     let mut total_read = 0;
169     let mut total_written = 0;
170     {
171         let (complete, read, written, _) = decoder.decode_to_utf8(head, &mut dest, false);
172         match complete {
173             CoderResult::InputEmpty => {}
174             CoderResult::OutputFull => {
175                 unreachable!();
176             }
177         }
178         total_read += read;
179         total_written += written;
180     }
181     {
182         let (complete, read, written, _) =
183             decoder.decode_to_utf8(tail, &mut dest[total_written..], true);
184         match complete {
185             CoderResult::InputEmpty => {}
186             CoderResult::OutputFull => {
187                 unreachable!();
188             }
189         }
190         total_read += read;
191         total_written += written;
192     }
193     assert_eq!(total_read, head.len() + tail.len());
194     assert_eq!(total_written, expect.len());
195     dest.truncate(total_written);
196     assert_eq!(&dest[..], expect.as_bytes());
197 }
198 
decode_to_string(encoding: &'static Encoding, bytes: &[u8], expect: &str)199 pub fn decode_to_string(encoding: &'static Encoding, bytes: &[u8], expect: &str) {
200     let (cow, _, _) = encoding.decode(bytes);
201     assert_eq!(&cow[..], expect);
202 }
203 
encode_from_utf8(encoding: &'static Encoding, string: &str, expect: &[u8])204 pub fn encode_from_utf8(encoding: &'static Encoding, string: &str, expect: &[u8]) {
205     let mut encoder = encoding.new_encoder();
206     let mut dest: Vec<u8> = Vec::with_capacity(10 * (string.len() + 1)); // 10 is replacement worst case
207     let capacity = dest.capacity();
208     dest.resize(capacity, 0u8);
209     let (complete, read, written, _) = encoder.encode_from_utf8(string, &mut dest, true);
210     match complete {
211         CoderResult::InputEmpty => {}
212         CoderResult::OutputFull => {
213             unreachable!();
214         }
215     }
216     assert_eq!(read, string.len());
217     assert_eq!(written, expect.len());
218     dest.truncate(written);
219     assert_eq!(&dest[..], expect);
220 }
221 
encode_from_utf16(encoding: &'static Encoding, string: &[u16], expect: &[u8])222 pub fn encode_from_utf16(encoding: &'static Encoding, string: &[u16], expect: &[u8]) {
223     let mut encoder = encoding.new_encoder();
224     let mut dest: Vec<u8> = Vec::with_capacity(10 * (string.len() + 1)); // 10 is replacement worst case
225     let capacity = dest.capacity();
226     dest.resize(capacity, 0u8);
227     let (complete, read, written, _) = encoder.encode_from_utf16(string, &mut dest, true);
228     match complete {
229         CoderResult::InputEmpty => {}
230         CoderResult::OutputFull => {
231             unreachable!();
232         }
233     }
234     assert_eq!(read, string.len());
235     // assert_eq!(written, expect.len());
236     dest.truncate(written);
237     assert_eq!(&dest[..], expect);
238 }
239 
encode_to_vec(encoding: &'static Encoding, string: &str, expect: &[u8])240 pub fn encode_to_vec(encoding: &'static Encoding, string: &str, expect: &[u8]) {
241     let (cow, _, _) = encoding.encode(string);
242     assert_eq!(&cow[..], expect);
243 }
244 
utf16_from_utf8(string: &str) -> Vec<u16>245 pub fn utf16_from_utf8(string: &str) -> Vec<u16> {
246     let mut decoder = UTF_8.new_decoder_without_bom_handling();
247     let mut vec = Vec::with_capacity(decoder.max_utf16_buffer_length(string.len()).unwrap());
248     let capacity = vec.capacity();
249     vec.resize(capacity, 0);
250 
251     let (result, read, written) =
252         decoder.decode_to_utf16_without_replacement(string.as_bytes(), &mut vec[..], true);
253     match result {
254         DecoderResult::InputEmpty => {
255             debug_assert_eq!(read, string.len());
256             vec.resize(written, 0);
257             vec
258         }
259         DecoderResult::Malformed(_, _) => unreachable!("Malformed"),
260         DecoderResult::OutputFull => unreachable!("Output full"),
261     }
262 }
263