MCPcopy Create free account
hub / github.com/RustCrypto/formats / decode_padding

Function decode_padding

base64ct/src/encoding.rs:267–299  ·  view source on GitHub ↗
(input: &[u8])

Source from the content-addressed store, hash-verified

265/// encoding-related errors prior to branching.
266#[inline(always)]
267pub(crate) fn decode_padding(input: &[u8]) -> Result<(usize, i16), InvalidEncodingError> {
268 if input.len() % 4 != 0 {
269 return Err(InvalidEncodingError);
270 }
271
272 let unpadded_len = match *input {
273 [.., b0, b1] => is_pad_ct(b0)
274 .checked_add(is_pad_ct(b1))
275 .and_then(|len| len.try_into().ok())
276 .and_then(|len| input.len().checked_sub(len))
277 .ok_or(InvalidEncodingError)?,
278 _ => input.len(),
279 };
280
281 let padding_len = input
282 .len()
283 .checked_sub(unpadded_len)
284 .ok_or(InvalidEncodingError)?;
285
286 let err = match *input {
287 [.., b0] if padding_len == 1 => is_pad_ct(b0) ^ 1,
288 [.., b0, b1] if padding_len == 2 => (is_pad_ct(b0) & is_pad_ct(b1)) ^ 1,
289 _ => {
290 if padding_len == 0 {
291 0
292 } else {
293 return Err(InvalidEncodingError);
294 }
295 }
296 };
297
298 Ok((unpadded_len, err))
299}
300
301/// Validate that the last block of the decoded data round-trips back to the
302/// encoded data.

Callers 2

decodeMethod · 0.85
decode_in_placeMethod · 0.85

Calls 2

is_pad_ctFunction · 0.85
lenMethod · 0.45

Tested by

no test coverage detected