Skip to content

Commit

Permalink
feat: Support padding and formats in base64
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Jul 7, 2023
1 parent 6db8f24 commit e0be32b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
51 changes: 39 additions & 12 deletions caido-convert/src/encoding/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,62 @@ use crate::OperationError;

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub struct Base64Decode {}
#[cfg_attr(target_family = "wasm", serde(rename_all = "snake_case"))]
pub enum Base64Format {
Standard,
Url,
Mime,
}

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub struct Base64Decode {
format: Base64Format,
pad: bool,
}

impl Operation for Base64Decode {
fn execute(&self, input: &[u8]) -> Result<Vec<u8>, OperationError> {
data_encoding::BASE64
.decode(input)
.map_err(|_| OperationError::DecodeError("Invalid base64 input".to_string()))
match (&self.format, self.pad) {
(Base64Format::Standard, false) => data_encoding::BASE64_NOPAD.decode(input),
(Base64Format::Standard, true) => data_encoding::BASE64.decode(input),
(Base64Format::Url, false) => data_encoding::BASE64URL_NOPAD.decode(input),
(Base64Format::Url, true) => data_encoding::BASE64URL.decode(input),
(Base64Format::Mime, _) => data_encoding::BASE64_MIME.decode(input),
}
.map_err(|_| OperationError::DecodeError("Invalid base64 input".to_string()))
}
}

impl Base64Decode {
pub fn new() -> Self {
Base64Decode {}
pub fn new(format: Base64Format, pad: bool) -> Self {
Base64Decode { format, pad }
}
}

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub struct Base64Encode {}
pub struct Base64Encode {
format: Base64Format,
pad: bool,
}

impl Operation for Base64Encode {
fn execute(&self, input: &[u8]) -> Result<Vec<u8>, OperationError> {
Ok(data_encoding::BASE64.encode(input).into())
let encoded = match (&self.format, self.pad) {
(Base64Format::Standard, false) => data_encoding::BASE64_NOPAD.encode(input),
(Base64Format::Standard, true) => data_encoding::BASE64.encode(input),
(Base64Format::Url, false) => data_encoding::BASE64URL_NOPAD.encode(input),
(Base64Format::Url, true) => data_encoding::BASE64URL.encode(input),
(Base64Format::Mime, _) => data_encoding::BASE64_MIME.encode(input),
};
Ok(encoded.into())
}
}

impl Base64Encode {
pub fn new() -> Self {
Base64Encode {}
pub fn new(format: Base64Format, pad: bool) -> Self {
Base64Encode { format, pad }
}
}

Expand All @@ -45,15 +72,15 @@ mod tests {

#[test]
fn base64_decode() {
let encoder = Base64Decode::new();
let encoder = Base64Decode::new(Base64Format::Standard, true);
let actual = encoder.execute("Y2FpZG8=".as_bytes()).unwrap();
let expected = "caido".as_bytes().to_vec();
assert_eq!(actual, expected);
}

#[test]
fn base64_encode() {
let encoder = Base64Encode::new();
let encoder = Base64Encode::new(Base64Format::Standard, true);
let actual = encoder.execute("caido".as_bytes()).unwrap();
let expected = "Y2FpZG8=".as_bytes().to_vec();
assert_eq!(actual, expected);
Expand Down
10 changes: 8 additions & 2 deletions convert/src/encoding/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ impl Base64Encode {
#[wasm_bindgen(constructor)]
pub fn new() -> Base64Encode {
Base64Encode {
base64_encode: caido_convert::Base64Encode::new(),
base64_encode: caido_convert::Base64Encode::new(
caido_convert::Base64Format::Standard,
true,
),
}
}

Expand All @@ -32,7 +35,10 @@ impl Base64Decode {
#[wasm_bindgen(constructor)]
pub fn new() -> Base64Decode {
Base64Decode {
base64_decode: caido_convert::Base64Decode::new(),
base64_decode: caido_convert::Base64Decode::new(
caido_convert::Base64Format::Standard,
true,
),
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "1.67.0"
channel = "1.70.0"
targets = [
"wasm32-unknown-unknown",
"x86_64-apple-darwin",
Expand Down

0 comments on commit e0be32b

Please sign in to comment.