1
- #[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
2
- use crate :: engine:: DecodeEstimate ;
3
1
use crate :: engine:: Engine ;
4
2
#[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
5
3
use crate :: engine:: DEFAULT_ENGINE ;
@@ -61,138 +59,50 @@ impl error::Error for DecodeError {
61
59
}
62
60
63
61
///Decode base64 using the [default engine](DEFAULT_ENGINE).
64
- ///Returns a `Result` containing a `Vec<u8>`.
65
- ///
66
- ///# Example
67
62
///
68
- ///```rust
69
- /// let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap();
70
- /// println!("{:?}", bytes);
71
- ///```
63
+ /// See [Engine::decode].
64
+ #[ deprecated( since = "0.21.0" , note = "Use Engine::decode" ) ]
72
65
#[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
73
66
pub fn decode < T : AsRef < [ u8 ] > > ( input : T ) -> Result < Vec < u8 > , DecodeError > {
74
- decode_engine ( input, & DEFAULT_ENGINE )
67
+ DEFAULT_ENGINE . decode ( input)
75
68
}
76
69
77
70
///Decode from string reference as octets using the specified [Engine].
78
- ///Returns a `Result` containing a `Vec<u8>`.
79
- ///
80
- ///# Example
81
- ///
82
- ///```rust
83
- /// let bytes = base64::decode_engine(
84
- /// "aGVsbG8gd29ybGR+Cg==",
85
- /// &base64::engine::DEFAULT_ENGINE,
86
- /// ).unwrap();
87
- /// println!("{:?}", bytes);
88
71
///
89
- /// // custom engine setup
90
- /// let bytes_url = base64::decode_engine(
91
- /// "aGVsbG8gaW50ZXJuZXR-Cg",
92
- /// &base64::engine::GeneralPurpose::new(
93
- /// &base64::alphabet::URL_SAFE,
94
- /// base64::engine::general_purpose::NO_PAD),
95
- ///
96
- /// ).unwrap();
97
- /// println!("{:?}", bytes_url);
98
- ///```
72
+ /// See [Engine::decode].
73
+ ///Returns a `Result` containing a `Vec<u8>`.
74
+ #[ deprecated( since = "0.21.0" , note = "Use Engine::decode" ) ]
99
75
#[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
100
76
pub fn decode_engine < E : Engine , T : AsRef < [ u8 ] > > (
101
77
input : T ,
102
78
engine : & E ,
103
79
) -> Result < Vec < u8 > , DecodeError > {
104
- let decoded_length_estimate = ( input
105
- . as_ref ( )
106
- . len ( )
107
- . checked_add ( 3 )
108
- . expect ( "decoded length calculation overflow" ) )
109
- / 4
110
- * 3 ;
111
- let mut buffer = Vec :: < u8 > :: with_capacity ( decoded_length_estimate) ;
112
- decode_engine_vec ( input, & mut buffer, engine) . map ( |_| buffer)
80
+ engine. decode ( input)
113
81
}
114
82
115
- ///Decode from string reference as octets.
116
- ///Writes into the supplied `Vec`, which may allocate if its internal buffer isn't big enough.
117
- ///Returns a `Result` containing an empty tuple, aka `()`.
118
- ///
119
- ///# Example
120
- ///
121
- ///```rust
122
- ///const URL_SAFE_ENGINE: base64::engine::GeneralPurpose =
123
- /// base64::engine::GeneralPurpose::new(
124
- /// &base64::alphabet::URL_SAFE,
125
- /// base64::engine::general_purpose::PAD);
126
- ///
127
- ///fn main() {
128
- /// let mut buffer = Vec::<u8>::new();
129
- /// // with the default engine
130
- /// base64::decode_engine_vec(
131
- /// "aGVsbG8gd29ybGR+Cg==",
132
- /// &mut buffer,
133
- /// &base64::engine::DEFAULT_ENGINE
134
- /// ).unwrap();
135
- /// println!("{:?}", buffer);
83
+ /// Decode from string reference as octets.
136
84
///
137
- /// buffer.clear();
138
- ///
139
- /// // with a custom engine
140
- /// base64::decode_engine_vec(
141
- /// "aGVsbG8gaW50ZXJuZXR-Cg==",
142
- /// &mut buffer,
143
- /// &URL_SAFE_ENGINE
144
- /// ).unwrap();
145
- /// println!("{:?}", buffer);
146
- ///}
147
- ///```
85
+ /// See [Engine::decode_vec].
148
86
#[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
87
+ #[ deprecated( since = "0.21.0" , note = "Use Engine::decode_vec" ) ]
149
88
pub fn decode_engine_vec < E : Engine , T : AsRef < [ u8 ] > > (
150
89
input : T ,
151
90
buffer : & mut Vec < u8 > ,
152
91
engine : & E ,
153
92
) -> Result < ( ) , DecodeError > {
154
- let input_bytes = input. as_ref ( ) ;
155
-
156
- let starting_output_len = buffer. len ( ) ;
157
-
158
- let estimate = engine. decoded_length_estimate ( input_bytes. len ( ) ) ;
159
- let total_len_estimate = estimate
160
- . decoded_length_estimate ( )
161
- . checked_add ( starting_output_len)
162
- . expect ( "Overflow when calculating output buffer length" ) ;
163
- buffer. resize ( total_len_estimate, 0 ) ;
164
-
165
- let buffer_slice = & mut buffer. as_mut_slice ( ) [ starting_output_len..] ;
166
- let bytes_written = engine. decode ( input_bytes, buffer_slice, estimate) ?;
167
-
168
- buffer. truncate ( starting_output_len + bytes_written) ;
169
-
170
- Ok ( ( ) )
93
+ engine. decode_vec ( input, buffer)
171
94
}
172
95
173
96
/// Decode the input into the provided output slice.
174
97
///
175
- /// This will not write any bytes past exactly what is decoded (no stray garbage bytes at the end).
176
- ///
177
- /// If you don't know ahead of time what the decoded length should be, size your buffer with a
178
- /// conservative estimate for the decoded length of an input: 3 bytes of output for every 4 bytes of
179
- /// input, rounded up, or in other words `(input_len + 3) / 4 * 3`.
180
- ///
181
- /// # Panics
182
- ///
183
- /// If the slice is not large enough, this will panic.
98
+ /// See [Engine::decode_slice].
99
+ #[ deprecated( since = "0.21.0" , note = "Use Engine::decode_slice" ) ]
184
100
pub fn decode_engine_slice < E : Engine , T : AsRef < [ u8 ] > > (
185
101
input : T ,
186
102
output : & mut [ u8 ] ,
187
103
engine : & E ,
188
104
) -> Result < usize , DecodeError > {
189
- let input_bytes = input. as_ref ( ) ;
190
-
191
- engine. decode (
192
- input_bytes,
193
- output,
194
- engine. decoded_length_estimate ( input_bytes. len ( ) ) ,
195
- )
105
+ engine. decode_slice ( input, output)
196
106
}
197
107
198
108
#[ cfg( test) ]
@@ -249,9 +159,13 @@ mod tests {
249
159
decoded_with_prefix. copy_from_slice ( & prefix) ;
250
160
251
161
// decode into the non-empty buf
252
- decode_engine_vec ( & encoded_data, & mut decoded_with_prefix, & engine) . unwrap ( ) ;
162
+ engine
163
+ . decode_vec ( & encoded_data, & mut decoded_with_prefix)
164
+ . unwrap ( ) ;
253
165
// also decode into the empty buf
254
- decode_engine_vec ( & encoded_data, & mut decoded_without_prefix, & engine) . unwrap ( ) ;
166
+ engine
167
+ . decode_vec ( & encoded_data, & mut decoded_without_prefix)
168
+ . unwrap ( ) ;
255
169
256
170
assert_eq ! (
257
171
prefix_len + decoded_without_prefix. len( ) ,
@@ -304,8 +218,9 @@ mod tests {
304
218
let offset = 1000 ;
305
219
306
220
// decode into the non-empty buf
307
- let decode_bytes_written =
308
- decode_engine_slice ( & encoded_data, & mut decode_buf[ offset..] , & engine) . unwrap ( ) ;
221
+ let decode_bytes_written = engine
222
+ . decode_slice ( & encoded_data, & mut decode_buf[ offset..] )
223
+ . unwrap ( ) ;
309
224
310
225
assert_eq ! ( orig_data. len( ) , decode_bytes_written) ;
311
226
assert_eq ! (
@@ -347,8 +262,9 @@ mod tests {
347
262
decode_buf. resize ( input_len, 0 ) ;
348
263
349
264
// decode into the non-empty buf
350
- let decode_bytes_written =
351
- decode_engine_slice ( & encoded_data, & mut decode_buf[ ..] , & engine) . unwrap ( ) ;
265
+ let decode_bytes_written = engine
266
+ . decode_slice ( & encoded_data, & mut decode_buf[ ..] )
267
+ . unwrap ( ) ;
352
268
353
269
assert_eq ! ( orig_data. len( ) , decode_bytes_written) ;
354
270
assert_eq ! ( orig_data, decode_buf) ;
@@ -363,7 +279,7 @@ mod tests {
363
279
let mut prefix = "AAAA" . repeat ( num_prefix_quads) ;
364
280
prefix. push_str ( suffix) ;
365
281
// make sure no overflow (and thus a panic) occurs
366
- let res = decode_engine ( prefix, & engine ) ;
282
+ let res = engine . decode ( prefix) ;
367
283
assert ! ( res. is_ok( ) ) ;
368
284
}
369
285
}
0 commit comments