From 2955a0fe607a60fe9b945e6cb651664a777d4739 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 3 Apr 2023 11:21:03 +0200 Subject: [PATCH] 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. --- parsers/Cargo.toml | 2 +- parsers/src/caps.rs | 15 +++++++++------ parsers/src/ecaps2.rs | 27 ++++++++++++++++++++------- parsers/src/hashes.rs | 9 ++++++--- parsers/src/jingle_ft.rs | 5 +++-- parsers/src/util/helpers.rs | 9 +++++---- 6 files changed, 44 insertions(+), 23 deletions(-) diff --git a/parsers/Cargo.toml b/parsers/Cargo.toml index a6837a45fb4d9a99fbafe2f3a32132ce0203bece..97ff01809d17c2acfa4d6326cf98c217bfa45e63 100644 --- a/parsers/Cargo.toml +++ b/parsers/Cargo.toml @@ -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" diff --git a/parsers/src/caps.rs b/parsers/src/caps.rs index 6b01b4bda1b8cd55a1df0611b0054a0b35226458..9a96f218996cf0fdad0bb329f4ca3399b87840af 100644 --- a/parsers/src/caps.rs +++ b/parsers/src/caps.rs @@ -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 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 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 { /// 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() ); } } diff --git a/parsers/src/ecaps2.rs b/parsers/src/ecaps2.rs index 5f944d42aac47053e388f4eb79fb14f2d745e51f..107b0f8db293c4c6ec5d74ba8c9e0652e5081916 100644 --- a/parsers/src/ecaps2.rs +++ b/parsers/src/ecaps2.rs @@ -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() ); } diff --git a/parsers/src/hashes.rs b/parsers/src/hashes.rs index 80acc401ff8416c6cdf84b1015ef12cb87d388eb..f4b3a9ceec71566ce934d1f0bc96c7d1e43fd401 100644 --- a/parsers/src/hashes.rs +++ b/parsers/src/hashes.rs @@ -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 { - 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() ); } diff --git a/parsers/src/jingle_ft.rs b/parsers/src/jingle_ft.rs index 30cf3e70531b35bc9e71090f3544f08c737dbae2..270a1f1156a28b4e5f0223b9b239699080ae0b65 100644 --- a/parsers/src/jingle_ft.rs +++ b/parsers/src/jingle_ft.rs @@ -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() ); } diff --git a/parsers/src/util/helpers.rs b/parsers/src/util/helpers.rs index 570e7cfc4a17626a32e150094f7c7e97c5744922..cdf4689f73ba5645150dd1d312c58785c25f4ae5 100644 --- a/parsers/src/util/helpers.rs +++ b/parsers/src/util/helpers.rs @@ -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, Error> { - Ok(base64::decode(s)?) + Ok(Base64Engine.decode(s)?) } pub fn encode(b: &[u8]) -> Option { - 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 { - Some(base64::encode(b)) + Some(Base64Engine.encode(b)) } }