parsers: Bump base64

Version 0.21 replaced base64::decode() with an Engine trait and multiple
structs implementing it for various alphabets, various performance
profiles, etc.  It is slightly longer to import but in the end does the
very same thing.
This commit is contained in:
Emmanuel Gil Peyrot 2023-04-03 11:21:03 +02:00
parent 3ad616945a
commit 2955a0fe60
6 changed files with 44 additions and 23 deletions

View file

@ -16,7 +16,7 @@ edition = "2018"
[dependencies]
minidom = "0.15"
jid = { version = "0.9", features = ["minidom"] }
base64 = "0.20"
base64 = "0.21"
digest = "0.10"
sha1 = "0.10"
sha2 = "0.10"

View file

@ -11,6 +11,7 @@ use crate::ns;
use crate::presence::PresencePayload;
use crate::util::error::Error;
use crate::Element;
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
use blake2::Blake2bVar;
use digest::{Digest, Update, VariableOutput};
use sha1::Sha1;
@ -48,7 +49,7 @@ impl TryFrom<Element> for Caps {
let ver: String = get_attr!(elem, "ver", Required);
let hash = Hash {
algo: get_attr!(elem, "hash", Required),
hash: base64::decode(&ver)?,
hash: Base64.decode(&ver)?,
};
Ok(Caps {
ext: get_attr!(elem, "ext", Option),
@ -64,7 +65,7 @@ impl From<Caps> for Element {
.attr("ext", caps.ext)
.attr("hash", caps.hash.algo)
.attr("node", caps.node)
.attr("ver", base64::encode(&caps.hash.hash))
.attr("ver", Base64.encode(&caps.hash.hash))
.build()
}
}
@ -210,7 +211,7 @@ pub fn hash_caps(data: &[u8], algo: Algo) -> Result<Hash, String> {
/// caps hash.
pub fn query_caps(caps: Caps) -> DiscoInfoQuery {
DiscoInfoQuery {
node: Some(format!("{}#{}", caps.node, base64::encode(&caps.hash.hash))),
node: Some(format!("{}#{}", caps.node, Base64.encode(&caps.hash.hash))),
}
}
@ -239,7 +240,9 @@ mod tests {
assert_eq!(caps.hash.algo, Algo::Sha_256);
assert_eq!(
caps.hash.hash,
base64::decode("K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=").unwrap()
Base64
.decode("K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=")
.unwrap()
);
}
@ -287,7 +290,7 @@ mod tests {
let sha_1 = caps::hash_caps(&caps, Algo::Sha_1).unwrap();
assert_eq!(
sha_1.hash,
base64::decode("QgayPKawpkPSDYmwT/WM94uAlu0=").unwrap()
Base64.decode("QgayPKawpkPSDYmwT/WM94uAlu0=").unwrap()
);
}
@ -334,7 +337,7 @@ mod tests {
let sha_1 = caps::hash_caps(&caps, Algo::Sha_1).unwrap();
assert_eq!(
sha_1.hash,
base64::decode("q07IKJEyjvHSyhy//CH0CxmKi8w=").unwrap()
Base64.decode("q07IKJEyjvHSyhy//CH0CxmKi8w=").unwrap()
);
}
}

View file

@ -10,6 +10,7 @@ use crate::hashes::{Algo, Hash};
use crate::ns;
use crate::presence::PresencePayload;
use crate::util::error::Error;
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
use blake2::Blake2bVar;
use digest::{Digest, Update, VariableOutput};
use sha2::{Sha256, Sha512};
@ -174,7 +175,7 @@ pub fn query_ecaps2(hash: Hash) -> DiscoInfoQuery {
"{}#{}.{}",
ns::ECAPS2,
String::from(hash.algo),
base64::encode(&hash.hash)
Base64.encode(&hash.hash)
)),
}
}
@ -206,12 +207,16 @@ mod tests {
assert_eq!(ecaps2.hashes[0].algo, Algo::Sha_256);
assert_eq!(
ecaps2.hashes[0].hash,
base64::decode("K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=").unwrap()
Base64
.decode("K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=")
.unwrap()
);
assert_eq!(ecaps2.hashes[1].algo, Algo::Sha3_256);
assert_eq!(
ecaps2.hashes[1].hash,
base64::decode("+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=").unwrap()
Base64
.decode("+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=")
.unwrap()
);
}
@ -295,12 +300,16 @@ mod tests {
let sha_256 = hash_ecaps2(&ecaps2, Algo::Sha_256).unwrap();
assert_eq!(
sha_256.hash,
base64::decode("kzBZbkqJ3ADrj7v08reD1qcWUwNGHaidNUgD7nHpiw8=").unwrap()
Base64
.decode("kzBZbkqJ3ADrj7v08reD1qcWUwNGHaidNUgD7nHpiw8=")
.unwrap()
);
let sha3_256 = hash_ecaps2(&ecaps2, Algo::Sha3_256).unwrap();
assert_eq!(
sha3_256.hash,
base64::decode("79mdYAfU9rEdTOcWDO7UEAt6E56SUzk/g6TnqUeuD9Q=").unwrap()
Base64
.decode("79mdYAfU9rEdTOcWDO7UEAt6E56SUzk/g6TnqUeuD9Q=")
.unwrap()
);
}
@ -455,12 +464,16 @@ mod tests {
let sha_256 = hash_ecaps2(&ecaps2, Algo::Sha_256).unwrap();
assert_eq!(
sha_256.hash,
base64::decode("u79ZroNJbdSWhdSp311mddz44oHHPsEBntQ5b1jqBSY=").unwrap()
Base64
.decode("u79ZroNJbdSWhdSp311mddz44oHHPsEBntQ5b1jqBSY=")
.unwrap()
);
let sha3_256 = hash_ecaps2(&ecaps2, Algo::Sha3_256).unwrap();
assert_eq!(
sha3_256.hash,
base64::decode("XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg=").unwrap()
Base64
.decode("XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg=")
.unwrap()
);
}

View file

@ -6,6 +6,7 @@
use crate::util::error::Error;
use crate::util::helpers::Base64;
use base64::{engine::general_purpose::STANDARD as Base64Engine, Engine};
use minidom::IntoAttributeValue;
use std::num::ParseIntError;
use std::ops::{Deref, DerefMut};
@ -117,7 +118,7 @@ impl Hash {
/// Like [new](#method.new) but takes base64-encoded data before decoding
/// it.
pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
Ok(Hash::new(algo, base64::decode(hash)?))
Ok(Hash::new(algo, Base64Engine.decode(hash)?))
}
/// Like [new](#method.new) but takes hex-encoded data before decoding it.
@ -145,7 +146,7 @@ impl Hash {
/// Formats this hash into base64.
pub fn to_base64(&self) -> String {
base64::encode(&self.hash[..])
Base64Engine.encode(&self.hash[..])
}
/// Formats this hash into hexadecimal.
@ -227,7 +228,9 @@ mod tests {
assert_eq!(hash.algo, Algo::Sha_256);
assert_eq!(
hash.hash,
base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap()
Base64Engine
.decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=")
.unwrap()
);
}

View file

@ -331,6 +331,7 @@ generate_element!(
mod tests {
use super::*;
use crate::hashes::Algo;
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
// Apparently, i686 and AArch32/PowerPC seem to disagree here. So instead
// of trying to figure this out now, we just ignore the test.
@ -383,7 +384,7 @@ mod tests {
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
assert_eq!(
desc.file.hashes[0].hash,
base64::decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
Base64.decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
);
}
@ -408,7 +409,7 @@ mod tests {
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
assert_eq!(
desc.file.hashes[0].hash,
base64::decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
Base64.decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
);
}

View file

@ -5,6 +5,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::util::error::Error;
use base64::{engine::general_purpose::STANDARD as Base64Engine, Engine};
use jid::Jid;
use std::str::FromStr;
@ -58,11 +59,11 @@ pub struct Base64;
impl Base64 {
pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
Ok(base64::decode(s)?)
Ok(Base64Engine.decode(s)?)
}
pub fn encode(b: &[u8]) -> Option<String> {
Some(base64::encode(b))
Some(Base64Engine.encode(b))
}
}
@ -75,11 +76,11 @@ impl WhitespaceAwareBase64 {
.chars()
.filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t')
.collect();
Ok(base64::decode(&s)?)
Ok(Base64Engine.decode(&s)?)
}
pub fn encode(b: &[u8]) -> Option<String> {
Some(base64::encode(b))
Some(Base64Engine.encode(b))
}
}