From d90e22cf8e00058fd95a21e232485bebf19149e2 Mon Sep 17 00:00:00 2001
From: George Hilliard
Date: Fri, 5 Jun 2020 17:35:20 -0500
Subject: [PATCH] Execute the Great Autoformatting of 2020
---
build.rs | 3 +-
src/bc/de.rs | 132 ++++++++++++++++++++--------------
src/bc/model.rs | 20 +++---
src/bc/ser.rs | 61 +++++++++-------
src/bc/xml.rs | 88 +++++++++++++----------
src/bc/xml_crypto.rs | 6 +-
src/bc_protocol.rs | 111 +++++++++++++++++-----------
src/bc_protocol/connection.rs | 50 +++++++------
src/config.rs | 4 +-
src/gst.rs | 43 ++++++-----
src/main.rs | 70 ++++++++++++------
11 files changed, 360 insertions(+), 228 deletions(-)
diff --git a/build.rs b/build.rs
index 5186107..b9498f5 100644
--- a/build.rs
+++ b/build.rs
@@ -10,5 +10,4 @@ fn main() {
}
#[cfg(not(windows))]
-fn main() {
-}
+fn main() {}
diff --git a/src/bc/de.rs b/src/bc/de.rs
index 85a6474..d377157 100644
--- a/src/bc/de.rs
+++ b/src/bc/de.rs
@@ -1,17 +1,17 @@
+use super::model::*;
+use super::xml::{AllTopXmls, Extension};
+use super::xml_crypto;
use err_derive::Error;
use log::*;
use nom::IResult;
-use nom::{bytes::streaming::take, number::streaming::*, combinator::*, sequence::*};
+use nom::{bytes::streaming::take, combinator::*, number::streaming::*, sequence::*};
use std::io::Read;
-use super::model::*;
-use super::xml::{Extension, AllTopXmls};
-use super::xml_crypto;
#[derive(Debug, Error)]
pub enum Error {
- #[error(display="Parsing error")]
+ #[error(display = "Parsing error")]
NomError(&'static str),
- #[error(display="I/O error")]
+ #[error(display = "I/O error")]
IoError(#[error(source)] std::io::Error),
}
@@ -36,9 +36,10 @@ impl Bc {
}
fn read_from_reader(mut parser: P, mut rdr: R) -> Result
- where R: Read,
- E: for<'a> From>> + From,
- P: FnMut(&[u8]) -> nom::IResult<&[u8], O>,
+where
+ R: Read,
+ E: for<'a> From>> + From,
+ P: FnMut(&[u8]) -> nom::IResult<&[u8], O>,
{
let mut input: Vec = Vec::new();
loop {
@@ -46,16 +47,19 @@ fn read_from_reader(mut parser: P, mut rdr: R) -> Result
Ok((_, parsed)) => return Ok(parsed),
Err(nom::Err::Incomplete(needed)) => {
match needed {
- nom::Needed::Unknown => 1, // read one byte
+ nom::Needed::Unknown => 1, // read one byte
nom::Needed::Size(len) => len,
}
- },
+ }
Err(e) => return Err(e.into()),
};
if 0 == (&mut rdr).take(to_read as u64).read_to_end(&mut input)? {
- return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof,
- "Read returned 0 bytes").into());
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::UnexpectedEof,
+ "Read returned 0 bytes",
+ )
+ .into());
}
}
}
@@ -72,9 +76,11 @@ fn bc_msg<'a, 'b>(context: &'a mut BcContext, buf: &'b [u8]) -> IResult<&'b [u8]
Ok((buf, bc))
}
-fn bc_body<'a, 'b, 'c>(context: &'c mut BcContext, header: &'a BcHeader, buf: &'b [u8])
- -> IResult<&'b [u8], BcBody>
-{
+fn bc_body<'a, 'b, 'c>(
+ context: &'c mut BcContext,
+ header: &'a BcHeader,
+ buf: &'b [u8],
+) -> IResult<&'b [u8], BcBody> {
if header.is_modern() {
let (buf, body) = bc_modern_msg(context, header, buf)?;
Ok((buf, BcBody::ModernMsg(body)))
@@ -88,41 +94,47 @@ fn bc_body<'a, 'b, 'c>(context: &'c mut BcContext, header: &'a BcHeader, buf: &'
}
fn hex32<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], String> {
- map_res(take(32usize), |slice: &'a [u8]| String::from_utf8(slice.to_vec()))
+ map_res(take(32usize), |slice: &'a [u8]| {
+ String::from_utf8(slice.to_vec())
+ })
}
-fn bc_legacy_login_msg<'a, 'b>(buf: &'b [u8])
- -> IResult<&'b [u8], LegacyMsg>
-{
+fn bc_legacy_login_msg<'a, 'b>(buf: &'b [u8]) -> IResult<&'b [u8], LegacyMsg> {
let (buf, username) = hex32()(buf)?;
let (buf, password) = hex32()(buf)?;
- Ok((buf, LegacyMsg::LoginMsg {
- username,
- password,
- }))
+ Ok((buf, LegacyMsg::LoginMsg { username, password }))
}
-fn bc_modern_msg<'a, 'b, 'c>(context: &mut BcContext, header: &'a BcHeader, buf: &'b [u8])
- -> IResult<&'b [u8], ModernMsg>
-{
- use nom::{Err, error::{make_error, ErrorKind}};
+fn bc_modern_msg<'a, 'b, 'c>(
+ context: &mut BcContext,
+ header: &'a BcHeader,
+ buf: &'b [u8],
+) -> IResult<&'b [u8], ModernMsg> {
+ use nom::{
+ error::{make_error, ErrorKind},
+ Err,
+ };
let mut in_bin_mode = context.in_bin_mode.contains(&header.msg_id);
// We'd like to know where the XML stops, but we haven't parsed the XML yet to see if the
// binaryData offset in the header is valid
- let end_of_xml = if in_bin_mode { 0 } else {
+ let end_of_xml = if in_bin_mode {
+ 0
+ } else {
match header.bin_offset {
Some(off) if off > 0 => off,
- _ => header.body_len
+ _ => header.body_len,
}
};
let (mut buf, body_buf) = take(end_of_xml)(buf)?;
let decrypted;
- let processed_body_buf = if !header.is_encrypted() { buf } else {
+ let processed_body_buf = if !header.is_encrypted() {
+ buf
+ } else {
decrypted = xml_crypto::crypt(header.enc_offset, body_buf);
&decrypted
};
@@ -140,7 +152,9 @@ fn bc_modern_msg<'a, 'b, 'c>(context: &mut BcContext, header: &'a BcHeader, buf:
// ID into binary mode, then the first binary is sent after the XML. All remaining
// messages for that ID are pure binary.
match parsed {
- AllTopXmls::BcXml(x) => { xml = Some(x); }
+ AllTopXmls::BcXml(x) => {
+ xml = Some(x);
+ }
AllTopXmls::Extension(Extension { binary_data: _ }) => {
in_bin_mode = true;
}
@@ -151,8 +165,8 @@ fn bc_modern_msg<'a, 'b, 'c>(context: &mut BcContext, header: &'a BcHeader, buf:
if in_bin_mode {
if let Some(bin_offset) = header.bin_offset {
// Extract remainder of message as binary, if it exists
- let (buf_after, payload) = map(take(header.body_len - bin_offset),
- |x: &[u8]| x.to_vec())(buf)?;
+ let (buf_after, payload) =
+ map(take(header.body_len - bin_offset), |x: &[u8]| x.to_vec())(buf)?;
// Since the parser operates in streaming mode, must wait until after we successfully
// receive enough bytes before modifying the context (otherwise we'll alter the
@@ -186,14 +200,17 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
let (buf, bin_offset) = cond(has_bin_offset(class), le_u32)(buf)?;
- Ok((buf, BcHeader {
- msg_id,
- body_len,
- enc_offset,
- encrypted,
- class,
- bin_offset,
- }))
+ Ok((
+ buf,
+ BcHeader {
+ msg_id,
+ body_len,
+ enc_offset,
+ encrypted,
+ class,
+ bin_offset,
+ },
+ ))
}
#[test]
@@ -210,9 +227,10 @@ fn test_bc_modern_login() {
assert_eq!(header.encrypted, true);
assert_eq!(header.class, 0x6614);
match body {
- BcBody::ModernMsg(ModernMsg{ xml: Some(ref xml), binary: None }) => {
- assert_eq!(xml.encryption.as_ref().unwrap().nonce, "9E6D1FCB9E69846D")
- }
+ BcBody::ModernMsg(ModernMsg {
+ xml: Some(ref xml),
+ binary: None,
+ }) => assert_eq!(xml.encryption.as_ref().unwrap().nonce, "9E6D1FCB9E69846D"),
_ => assert!(false),
}
}
@@ -231,9 +249,7 @@ fn test_bc_legacy_login() {
assert_eq!(header.encrypted, true);
assert_eq!(header.class, 0x6514);
match body {
- BcBody::LegacyMsg(LegacyMsg::LoginMsg {
- username, password
- }) => {
+ BcBody::LegacyMsg(LegacyMsg::LoginMsg { username, password }) => {
assert_eq!(username, "21232F297A57A5A743894A0E4A801FC\0");
assert_eq!(password, EMPTY_LEGACY_PASSWORD);
}
@@ -255,7 +271,10 @@ fn test_bc_modern_login_failed() {
assert_eq!(header.encrypted, true);
assert_eq!(header.class, 0x0000);
match body {
- BcBody::ModernMsg(ModernMsg{ xml: None, binary: None }) => {
+ BcBody::ModernMsg(ModernMsg {
+ xml: None,
+ binary: None,
+ }) => {
assert!(true);
}
_ => assert!(false),
@@ -279,7 +298,10 @@ fn test_bc_modern_login_success() {
// Previously, we were not handling bin_offset == 0 (no bin offset) correctly.
// Test that we decoded XML and no binary.
match body {
- BcBody::ModernMsg(ModernMsg{ xml: Some(_), binary: None }) => assert!(true),
+ BcBody::ModernMsg(ModernMsg {
+ xml: Some(_),
+ binary: None,
+ }) => assert!(true),
_ => assert!(false),
}
}
@@ -294,13 +316,19 @@ fn test_bc_binary_mode() {
let msg1 = Bc::deserialize(&mut context, &sample1[..]).unwrap();
let msg2 = Bc::deserialize(&mut context, &sample2[..]).unwrap();
match msg1.body {
- BcBody::ModernMsg(ModernMsg{ xml: None, binary: Some(bin) }) => {
+ BcBody::ModernMsg(ModernMsg {
+ xml: None,
+ binary: Some(bin),
+ }) => {
assert_eq!(bin.len(), 32);
}
_ => assert!(false),
}
match msg2.body {
- BcBody::ModernMsg(ModernMsg{ xml: None, binary: Some(bin) }) => {
+ BcBody::ModernMsg(ModernMsg {
+ xml: None,
+ binary: Some(bin),
+ }) => {
assert_eq!(bin.len(), 30344);
}
_ => assert!(false),
diff --git a/src/bc/model.rs b/src/bc/model.rs
index 827226b..e6c3115 100644
--- a/src/bc/model.rs
+++ b/src/bc/model.rs
@@ -1,5 +1,5 @@
-use std::collections::HashSet;
use super::xml::BcXml;
+use std::collections::HashSet;
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
@@ -7,7 +7,8 @@ pub const MSG_ID_LOGIN: u32 = 1;
pub const MSG_ID_VIDEO: u32 = 3;
pub const MSG_ID_PING: u32 = 93;
-pub const EMPTY_LEGACY_PASSWORD: &str = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
+pub const EMPTY_LEGACY_PASSWORD: &str =
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
#[derive(Debug, PartialEq, Eq)]
pub struct Bc {
@@ -29,10 +30,7 @@ pub struct ModernMsg {
#[derive(Debug, PartialEq, Eq)]
pub enum LegacyMsg {
- LoginMsg {
- username: String,
- password: String,
- },
+ LoginMsg { username: String, password: String },
UnknownMsg,
}
@@ -73,14 +71,20 @@ impl Bc {
/// Convenience function that constructs a modern Bc message from the given meta and XML, with
/// no binary payload.
pub fn new_from_xml(meta: BcMeta, xml: BcXml) -> Bc {
- Bc { meta, body: BcBody::ModernMsg(ModernMsg { xml: Some(xml), binary: None }) }
+ Bc {
+ meta,
+ body: BcBody::ModernMsg(ModernMsg {
+ xml: Some(xml),
+ binary: None,
+ }),
+ }
}
}
impl BcContext {
pub fn new() -> BcContext {
BcContext {
- in_bin_mode: HashSet::new()
+ in_bin_mode: HashSet::new(),
}
}
}
diff --git a/src/bc/ser.rs b/src/bc/ser.rs
index 794b08e..f8a9fa5 100644
--- a/src/bc/ser.rs
+++ b/src/bc/ser.rs
@@ -1,11 +1,11 @@
-use cookie_factory::bytes::*;
-use cookie_factory::{gen, combinator::*};
-use cookie_factory::sequence::tuple;
-use cookie_factory::{GenError, SerializeFn, WriteContext};
-use std::io::Write;
use super::model::*;
use super::xml::BcXml;
use super::xml_crypto;
+use cookie_factory::bytes::*;
+use cookie_factory::sequence::tuple;
+use cookie_factory::{combinator::*, gen};
+use cookie_factory::{GenError, SerializeFn, WriteContext};
+use std::io::Write;
pub type Error = GenError;
@@ -17,15 +17,24 @@ impl Bc {
let bin_offset;
match &self.body {
BcBody::ModernMsg(ref modern) => {
- let (buf, xml_len) = gen(opt_ref(&modern.xml, |xml| bc_xml(self.meta.client_idx, xml)), vec!())?;
+ let (buf, xml_len) = gen(
+ opt_ref(&modern.xml, |xml| bc_xml(self.meta.client_idx, xml)),
+ vec![],
+ )?;
body_buf = buf;
bin_offset = if has_bin_offset(self.meta.class) {
// If we're required to put binary length, put 0 if we have no binary
- Some(if modern.binary.is_some() { xml_len as u32 } else { 0 })
- } else { None };
+ Some(if modern.binary.is_some() {
+ xml_len as u32
+ } else {
+ 0
+ })
+ } else {
+ None
+ };
}
BcBody::LegacyMsg(ref legacy) => {
- let (buf, _) = gen(bc_legacy(legacy), vec!())?;
+ let (buf, _) = gen(bc_legacy(legacy), vec![])?;
body_buf = buf;
bin_offset = None;
}
@@ -34,14 +43,14 @@ impl Bc {
// Now have enough info to create the header
let header = BcHeader::from_meta(&self.meta, body_buf.len() as u32, bin_offset);
- let (mut buf, _n) = gen(
- tuple((
- bc_header(&header),
- slice(body_buf),
- )), buf)?;
+ let (mut buf, _n) = gen(tuple((bc_header(&header), slice(body_buf))), buf)?;
// Put the binary part of the body, TODO this is poorly written
- if let BcBody::ModernMsg(ModernMsg { binary: Some(ref binary), .. }) = self.body {
+ if let BcBody::ModernMsg(ModernMsg {
+ binary: Some(ref binary),
+ ..
+ }) = self.body
+ {
let (buf2, _) = gen(slice(binary), buf)?;
buf = buf2
}
@@ -50,9 +59,8 @@ impl Bc {
}
}
-fn bc_xml(enc_offset: u32, xml: &BcXml) -> impl SerializeFn
-{
- let xml_bytes = xml.serialize(vec!()).unwrap();
+fn bc_xml(enc_offset: u32, xml: &BcXml) -> impl SerializeFn {
+ let xml_bytes = xml.serialize(vec![]).unwrap();
let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes);
slice(enc_bytes)
}
@@ -90,7 +98,7 @@ fn bc_legacy<'a, W: Write>(legacy: &'a LegacyMsg) -> impl SerializeFn + 'a {
slice(password),
// Login messages are 1836 bytes total, username/password
// take up 32 chars each, 1772 zeros follow
- slice(&[0u8; 1772][..])
+ slice(&[0u8; 1772][..]),
))(out)
}
UnknownMsg => {
@@ -102,7 +110,10 @@ fn bc_legacy<'a, W: Write>(legacy: &'a LegacyMsg) -> impl SerializeFn + 'a {
/// Applies the supplied serializer with the Option's interior data if present
fn opt(opt: Option, ser: impl Fn(T) -> F) -> impl SerializeFn
- where F: SerializeFn, T: Copy, W: Write
+where
+ F: SerializeFn,
+ T: Copy,
+ W: Write,
{
move |buf: WriteContext| {
if let Some(val) = opt {
@@ -114,7 +125,10 @@ fn opt(opt: Option, ser: impl Fn(T) -> F) -> impl SerializeFn
}
fn opt_ref<'a, W, T, F, S>(opt: &'a Option, ser: S) -> impl SerializeFn + 'a
- where F: SerializeFn, W: Write, S: Fn(&'a T) -> F + 'a
+where
+ F: SerializeFn,
+ W: Write,
+ S: Fn(&'a T) -> F + 'a,
{
move |buf: WriteContext| {
if let Some(ref val) = opt {
@@ -130,7 +144,6 @@ fn do_nothing() -> impl SerializeFn {
move |out: WriteContext| Ok(out)
}
-
#[test]
fn test_legacy_login_roundtrip() {
let mut context = BcContext::new();
@@ -139,7 +152,7 @@ fn test_legacy_login_roundtrip() {
let sample = include_bytes!("samples/model_sample_legacy_login.bin");
let msg = Bc::deserialize::<&[u8]>(&mut context, &sample[..]).unwrap();
- let ser_buf = msg.serialize(vec!()).unwrap();
+ let ser_buf = msg.serialize(vec![]).unwrap();
let msg2 = Bc::deserialize::<&[u8]>(&mut context, ser_buf.as_ref()).unwrap();
assert_eq!(msg, msg2);
assert_eq!(&sample[..], ser_buf.as_slice());
@@ -154,7 +167,7 @@ fn test_modern_login_roundtrip() {
let msg = Bc::deserialize::<&[u8]>(&mut context, &sample[..]).unwrap();
- let ser_buf = msg.serialize(vec!()).unwrap();
+ let ser_buf = msg.serialize(vec![]).unwrap();
let msg2 = Bc::deserialize::<&[u8]>(&mut context, ser_buf.as_ref()).unwrap();
assert_eq!(msg, msg2);
}
diff --git a/src/bc/xml.rs b/src/bc/xml.rs
index 60bb958..d1fc015 100644
--- a/src/bc/xml.rs
+++ b/src/bc/xml.rs
@@ -3,8 +3,8 @@
use std::io::{Read, Write};
// YaSerde is currently naming the traits and the derive macros identically
-use yaserde_derive::{YaDeserialize, YaSerialize};
use yaserde::{ser::Config, YaDeserialize, YaSerialize};
+use yaserde_derive::{YaDeserialize, YaSerialize};
#[cfg(test)]
use indoc::indoc;
@@ -12,7 +12,7 @@ use indoc::indoc;
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
#[yaserde(flatten)]
pub(super) enum AllTopXmls {
- #[yaserde(rename="body")]
+ #[yaserde(rename = "body")]
BcXml(BcXml),
Extension(Extension),
}
@@ -25,17 +25,17 @@ impl Default for AllTopXmls {
}
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
-#[yaserde(rename="body")]
+#[yaserde(rename = "body")]
pub struct BcXml {
- #[yaserde(rename="Encryption")]
+ #[yaserde(rename = "Encryption")]
pub encryption: Option,
- #[yaserde(rename="LoginUser")]
+ #[yaserde(rename = "LoginUser")]
pub login_user: Option,
- #[yaserde(rename="LoginNet")]
+ #[yaserde(rename = "LoginNet")]
pub login_net: Option,
- #[yaserde(rename="DeviceInfo")]
+ #[yaserde(rename = "DeviceInfo")]
pub device_info: Option,
- #[yaserde(rename="Preview")]
+ #[yaserde(rename = "Preview")]
pub preview: Option,
}
@@ -58,7 +58,7 @@ impl BcXml {
pub struct Encryption {
#[yaserde(attribute)]
pub version: String,
- #[yaserde(rename="type")]
+ #[yaserde(rename = "type")]
pub type_: String,
pub nonce: String,
}
@@ -67,10 +67,10 @@ pub struct Encryption {
pub struct LoginUser {
#[yaserde(attribute)]
pub version: String,
- #[yaserde(rename="userName")]
+ #[yaserde(rename = "userName")]
pub user_name: String,
pub password: String,
- #[yaserde(rename="userVer")]
+ #[yaserde(rename = "userVer")]
pub user_ver: u32,
}
@@ -78,9 +78,9 @@ pub struct LoginUser {
pub struct LoginNet {
#[yaserde(attribute)]
pub version: String,
- #[yaserde(rename="type")]
+ #[yaserde(rename = "type")]
pub type_: String,
- #[yaserde(rename="udpPort")]
+ #[yaserde(rename = "udpPort")]
pub udp_port: u16,
}
@@ -101,7 +101,7 @@ pub struct DeviceInfo {
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct Resolution {
- #[yaserde(rename="resolutionName")]
+ #[yaserde(rename = "resolutionName")]
pub name: String,
pub width: u32,
pub height: u32,
@@ -112,16 +112,16 @@ pub struct Preview {
#[yaserde(attribute)]
pub version: String,
- #[yaserde(rename="channelId")]
+ #[yaserde(rename = "channelId")]
pub channel_id: u32,
pub handle: u32,
- #[yaserde(rename="streamType")]
+ #[yaserde(rename = "streamType")]
pub stream_type: String,
}
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct Extension {
- #[yaserde(rename="binaryData")]
+ #[yaserde(rename = "binaryData")]
pub binary_data: u32,
}
@@ -131,14 +131,16 @@ pub fn xml_ver() -> String {
#[test]
fn test_encryption_deser() {
- let sample = indoc!(r#"
+ let sample = indoc!(
+ r#"
md5
9E6D1FCB9E69846D
- "#);
+
@@ -167,7 +170,8 @@ fn test_login_deser() {
LAN
0
-
@@ -196,7 +201,8 @@ fn test_login_ser() {
LAN
0
-
@@ -243,21 +250,26 @@ fn test_deviceinfo_partial_deser() {
0
1
-