netdoc: add a check_len method for B64

This commit is contained in:
Nick Mathewson 2020-08-25 16:56:54 -04:00
parent d4af6a163f
commit 696e7ad334
1 changed files with 11 additions and 0 deletions

View File

@ -23,6 +23,7 @@ pub trait FromBytes: Sized {
mod b64impl {
use crate::{Error, Pos, Result};
use std::ops::RangeBounds;
pub struct B64(Vec<u8>);
@ -39,6 +40,16 @@ mod b64impl {
pub fn as_bytes(&self) -> &[u8] {
&self.0[..]
}
pub fn check_len<B: RangeBounds<usize>>(self, bounds: B) -> Result<Self> {
if bounds.contains(&self.0.len()) {
Ok(self)
} else {
Err(Error::BadObjectVal(
Pos::Unknown,
"Invalid length on base64 data".to_string(),
))
}
}
}
impl From<B64> for Vec<u8> {