Skip to content

Commit

Permalink
rust: run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
cypherpunks authored and nmathewson committed Aug 16, 2018
1 parent 32ad8e9 commit 6b609ce
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 242 deletions.
35 changes: 16 additions & 19 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io;
use std::io::prelude::*;
use std::path::PathBuf;

/// Wrapper around a key-value map.
struct Config(
HashMap<String,String>
);
struct Config(HashMap<String, String>);

/// Locate a config.rust file generated by autoconf, starting in the OUT_DIR
/// location provided by cargo and recursing up the directory tree. Note that
Expand All @@ -31,9 +29,9 @@ fn find_cfg() -> io::Result<String> {
return Ok(path.to_str().unwrap().to_owned());
}
path.pop(); // remove config.rust
if ! path.pop() { // can't remove last part of directory
return Err(io::Error::new(io::ErrorKind::NotFound,
"No config.rust"));
if !path.pop() {
// can't remove last part of directory
return Err(io::Error::new(io::ErrorKind::NotFound, "No config.rust"));
}
}
}
Expand All @@ -55,12 +53,11 @@ impl Config {
}
let idx = match s.find("=") {
None => {
return Err(io::Error::new(io::ErrorKind::InvalidData,
"missing ="));
},
Some(x) => x
return Err(io::Error::new(io::ErrorKind::InvalidData, "missing ="));
}
Some(x) => x,
};
let (var,eq_val) = s.split_at(idx);
let (var, eq_val) = s.split_at(idx);
let val = &eq_val[1..];
map.insert(var.to_owned(), val.to_owned());
}
Expand All @@ -70,34 +67,34 @@ impl Config {
/// Return a reference to the value whose key is 'key'.
///
/// Panics if 'key' is not found in the configuration.
fn get(&self, key : &str) -> &str {
fn get(&self, key: &str) -> &str {
self.0.get(key).unwrap()
}

/// Add a dependency on a static C library that is part of Tor, by name.
fn component(&self, s : &str) {
fn component(&self, s: &str) {
println!("cargo:rustc-link-lib=static={}", s);
}

/// Add a dependency on a native library that is not part of Tor, by name.
fn dependency(&self, s : &str) {
fn dependency(&self, s: &str) {
println!("cargo:rustc-link-lib={}", s);
}

/// Add a link path, relative to Tor's build directory.
fn link_relpath(&self, s : &str) {
fn link_relpath(&self, s: &str) {
let builddir = self.get("BUILDDIR");
println!("cargo:rustc-link-search=native={}/{}", builddir, s);
}

/// Add an absolute link path.
fn link_path(&self, s : &str) {
fn link_path(&self, s: &str) {
println!("cargo:rustc-link-search=native={}", s);
}

/// Parse the CFLAGS in s, looking for -l and -L items, and adding
/// rust configuration as appropriate.
fn from_cflags(&self, s : &str) {
fn from_cflags(&self, s: &str) {
let mut next_is_lib = false;
let mut next_is_path = false;
for ent in self.get(s).split_whitespace() {
Expand Down Expand Up @@ -184,7 +181,7 @@ pub fn main() {
cfg.from_cflags("TOR_LZMA_LIBS");
cfg.from_cflags("TOR_ZSTD_LIBS");
cfg.from_cflags("LIBS");
},
}
_ => {
panic!("No configuration in build.rs for package {}", package);
}
Expand Down
36 changes: 21 additions & 15 deletions src/rust/crypto/digests/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

pub use digest::Digest;

use digest::generic_array::typenum::U32;
use digest::generic_array::typenum::U64;
use digest::generic_array::GenericArray;
use digest::BlockInput;
use digest::FixedOutput;
use digest::Input;
use digest::generic_array::GenericArray;
use digest::generic_array::typenum::U32;
use digest::generic_array::typenum::U64;

use external::crypto_digest::CryptoDigest;
use external::crypto_digest::DigestAlgorithm;
use external::crypto_digest::get_256_bit_digest;
use external::crypto_digest::get_512_bit_digest;
use external::crypto_digest::CryptoDigest;
use external::crypto_digest::DigestAlgorithm;

pub use external::crypto_digest::DIGEST256_LEN;
pub use external::crypto_digest::DIGEST512_LEN;
Expand Down Expand Up @@ -54,7 +54,9 @@ pub struct Sha256 {
/// A new `Sha256` digest.
impl Default for Sha256 {
fn default() -> Sha256 {
Sha256{ engine: CryptoDigest::new(Some(DigestAlgorithm::SHA2_256)) }
Sha256 {
engine: CryptoDigest::new(Some(DigestAlgorithm::SHA2_256)),
}
}
}

Expand Down Expand Up @@ -121,7 +123,9 @@ pub struct Sha512 {
/// A new `Sha512` digest.
impl Default for Sha512 {
fn default() -> Sha512 {
Sha512{ engine: CryptoDigest::new(Some(DigestAlgorithm::SHA2_512)) }
Sha512 {
engine: CryptoDigest::new(Some(DigestAlgorithm::SHA2_512)),
}
}
}

Expand Down Expand Up @@ -182,9 +186,10 @@ mod test {
fn sha256_digest() {
let mut h: Sha256 = Sha256::new();
let mut result: [u8; DIGEST256_LEN] = [0u8; DIGEST256_LEN];
let expected = [151, 223, 53, 136, 181, 163, 242, 75, 171, 195,
133, 27, 55, 47, 11, 167, 26, 157, 205, 222, 212,
59, 20, 185, 208, 105, 97, 191, 193, 112, 125, 157];
let expected = [
151, 223, 53, 136, 181, 163, 242, 75, 171, 195, 133, 27, 55, 47, 11, 167, 26, 157, 205,
222, 212, 59, 20, 185, 208, 105, 97, 191, 193, 112, 125, 157,
];

h.input(b"foo");
h.input(b"bar");
Expand All @@ -209,11 +214,12 @@ mod test {
let mut h: Sha512 = Sha512::new();
let mut result: [u8; DIGEST512_LEN] = [0u8; DIGEST512_LEN];

let expected = [203, 55, 124, 16, 176, 245, 166, 44, 128, 54, 37, 167,
153, 217, 233, 8, 190, 69, 231, 103, 245, 209, 71, 212, 116,
73, 7, 203, 5, 89, 122, 164, 237, 211, 41, 160, 175, 20, 122,
221, 12, 244, 24, 30, 211, 40, 250, 30, 121, 148, 38, 88, 38,
179, 237, 61, 126, 246, 240, 103, 202, 153, 24, 90];
let expected = [
203, 55, 124, 16, 176, 245, 166, 44, 128, 54, 37, 167, 153, 217, 233, 8, 190, 69, 231,
103, 245, 209, 71, 212, 116, 73, 7, 203, 5, 89, 122, 164, 237, 211, 41, 160, 175, 20,
122, 221, 12, 244, 24, 30, 211, 40, 250, 30, 121, 148, 38, 88, 38, 179, 237, 61, 126,
246, 240, 103, 202, 153, 24, 90,
];

h.input(b"foo");
h.input(b"bar");
Expand Down
2 changes: 1 addition & 1 deletion src/rust/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ extern crate external;
#[macro_use]
extern crate tor_log;

pub mod digests; // Unfortunately named "digests" plural to avoid name conflict with the digest crate
pub mod digests; // Unfortunately named "digests" plural to avoid name conflict with the digest crate
pub mod rand;
29 changes: 17 additions & 12 deletions src/rust/crypto/rand/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
mod internal {
use std::u64;

use rand_core::impls::next_u32_via_fill;
use rand_core::impls::next_u64_via_fill;
use rand_core::CryptoRng;
use rand_core::Error;
use rand_core::RngCore;
use rand_core::impls::next_u32_via_fill;
use rand_core::impls::next_u64_via_fill;

use external::c_tor_crypto_rand;
use external::c_tor_crypto_strongest_rand;
use external::c_tor_crypto_seed_rng;
use external::c_tor_crypto_strongest_rand;

use tor_log::LogDomain;
use tor_log::LogSeverity;
Expand All @@ -45,12 +45,15 @@ mod internal {
#[allow(dead_code)]
pub fn new() -> Self {
if !c_tor_crypto_seed_rng() {
tor_log_msg!(LogSeverity::Warn, LogDomain::General,
"TorRng::from_seed()",
"The RNG could not be seeded!");
tor_log_msg!(
LogSeverity::Warn,
LogDomain::General,
"TorRng::from_seed()",
"The RNG could not be seeded!"
);
}
// XXX also log success at info level —isis
TorRng{ _unused: [0u8; 0] }
TorRng { _unused: [0u8; 0] }
}
}

Expand Down Expand Up @@ -92,12 +95,15 @@ mod internal {
#[allow(dead_code)]
pub fn new() -> Self {
if !c_tor_crypto_seed_rng() {
tor_log_msg!(LogSeverity::Warn, LogDomain::General,
"TorStrongestRng::from_seed()",
"The RNG could not be seeded!");
tor_log_msg!(
LogSeverity::Warn,
LogDomain::General,
"TorStrongestRng::from_seed()",
"The RNG could not be seeded!"
);
}
// XXX also log success at info level —isis
TorStrongestRng{ _unused: [0u8; 0] }
TorStrongestRng { _unused: [0u8; 0] }
}
}

Expand Down Expand Up @@ -137,4 +143,3 @@ mod internal {
// Finally, expose the public functionality of whichever appropriate internal
// module.
pub use self::internal::*;

Loading

0 comments on commit 6b609ce

Please sign in to comment.