Skip to content

Commit

Permalink
use base64 crate for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Feb 8, 2022
1 parent 46f5ffc commit ccc715d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 75 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ deflate = { version = "0.9.1", optional = true }
sha1 = { version = "0.6.0", features = ["std"] }
tempfile = "3.2.0"
futures = "0.3.17"
base64 = "=0.20.0-alpha.1"

[dev-dependencies]
pretty_assertions = "1.0.0"
Expand Down
86 changes: 17 additions & 69 deletions src/base64_plantuml.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,19 @@
use std::vec::Vec;

/// PlantUML has its own base64 dialect, this struct provides the implementation for that
pub struct Base64PlantUML {}

impl Base64PlantUML {
pub fn encode(data: &Vec<u8>) -> String {
let mut encoded = String::from("");

let len = data.len();
for i in (0..len).step_by(3) {
if i + 2 == len {
encoded.push_str(&encode3bytes(data[i], data[i + 1], 0));
} else if i + 1 == len {
encoded.push_str(&encode3bytes(data[i], 0, 0));
} else {
encoded.push_str(&encode3bytes(data[i], data[i + 1], data[i + 2]));
}
}

encoded
}
}

fn encode3bytes(b1: u8, b2: u8, b3: u8) -> String {
let c1 = b1 >> 2;
let c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
let c3 = ((b2 & 0xF) << 2) | (b3 >> 6);
let c4 = b3 & 0x3F;

let mut res = String::from("");
res.push(encode6bit(c1 & 0x3F));
res.push(encode6bit(c2 & 0x3F));
res.push(encode6bit(c3 & 0x3F));
res.push(encode6bit(c4 & 0x3F));

res
}

fn encode6bit(c: u8) -> char {
let mut b = c;
if b < 10 {
return (48 + b) as char;
}

b -= 10;
if b < 26 {
return (65 + b) as char;
}

b -= 26;
if b < 26 {
return (97 + b) as char;
}

b -= 26;
if b == 0 {
return '-';
}

if b == 1 {
return '_';
}

'?'
use base64::{
alphabet::Alphabet,
engine::fast_portable::{self, FastPortable},
};

const ENGINE: FastPortable =
match Alphabet::from_str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_") {
Ok(alphabet) => FastPortable::from(&alphabet, fast_portable::PAD),
Err(_e) => unreachable!(),
};

/// PlantUML has its own base64 dialect
pub fn encode(data: &Vec<u8>) -> String {
base64::encode_engine(data, &ENGINE)
}

#[cfg(test)]
Expand All @@ -73,13 +24,10 @@ mod tests {
#[test]
fn encodes_bytes() {
let data: Vec<u8> = b"froboz".iter().cloned().collect();
assert_eq!(String::from("Pd9lOczw"), Base64PlantUML::encode(&data));
assert_eq!(String::from("Pd9lOczw"), encode(&data));

let data: Vec<u8> = b"1234ABCDabcd\x12\x08\x01".iter().cloned().collect();
assert_eq!(
String::from("CJ8pD452GqHXOcDa4WW1"),
Base64PlantUML::encode(&data)
);
assert_eq!(String::from("CJ8pD452GqHXOcDa4WW1"), encode(&data));

//How would one pass 256 here?
let data: Vec<u8> = (0 as u8..255 as u8).collect();
Expand All @@ -92,7 +40,7 @@ mod tests {
YvkhkylRw_mC72myJ5niV8oShBpCtEpz3HqjFKrTRNsDdQszpTtj_WuUBZvENcv-\
ZfwklixUxlyF7oy_JrzlVu-Vhx_Ft-"
),
Base64PlantUML::encode(&data)
encode(&data)
);
}
}
8 changes: 3 additions & 5 deletions src/plantuml_server_backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::base64_plantuml::Base64PlantUML;
use crate::base64_plantuml;
use crate::plantuml_backend::PlantUMLBackend;
use deflate::deflate_bytes;
use failure::Error;
Expand Down Expand Up @@ -94,10 +94,8 @@ impl PlantUMLServer {

/// Compress and encode the image source, return the encoed Base64-ish string
fn encode_diagram_source(plantuml_code: &String) -> String {
let compressed = deflate_bytes(&plantuml_code.as_bytes());
let base64_compressed = Base64PlantUML::encode(&compressed);

base64_compressed
let compressed = deflate_bytes(plantuml_code.as_bytes());
base64_plantuml::encode(&compressed)
}

impl PlantUMLBackend for PlantUMLServer {
Expand Down

0 comments on commit ccc715d

Please sign in to comment.