Refractor to apply ext xml before payload_offset

This commit is contained in:
QuantumEntangledAndy authored and Andrew committed 2020-10-15 11:03:19 +07:00
1 parent 31aefdbc74
commit d0f760ea7b
5 files changed
+57 -68

No files matched your search

+15 -15
View File
@@ -1,5 +1,5 @@
use super::model::*;
use super::xml::{BcPayloads, BcXml, BcXmls};
use super::xml::{BcPayloads, BcXml};
use super::xml_crypto;
use err_derive::Error;
use log::*;
@@ -116,33 +116,33 @@ fn bc_modern_msg<'a, 'b>(
Err,
};
let xml_len = match header.payload_offset {
let ext_len = match header.payload_offset {
Some(off) => off,
_ => header.body_len,
_ => 0, // If missing payload_offset treat all as payload
};
let (buf, xml_buf) = take(xml_len)(buf)?;
let payload_len = header.body_len - xml_len;
let (buf, ext_buf) = take(ext_len)(buf)?;
let payload_len = header.body_len - ext_len;
let (buf, payload_buf) = take(payload_len)(buf)?;
let decrypted;
let processed_xml_buf = if !header.is_encrypted() {
xml_buf
let processed_ext_buf = if !header.is_encrypted() {
ext_buf
} else {
decrypted = xml_crypto::crypt(header.enc_offset, xml_buf);
decrypted = xml_crypto::crypt(header.enc_offset, ext_buf);
&decrypted
};
// Now we'll take the buffer that Nom gave a ref to and parse it.
let xml;
if xml_len > 0 {
let extension;
if ext_len > 0 {
// Apply the XML parse function, but throw away the reference to decrypted in the Ok and
// Err case. This error-error-error thing is the same idiom Nom uses internally.
let parsed = BcXmls::try_parse(processed_xml_buf)
let parsed = Extension::try_parse(processed_ext_buf)
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
xml = Some(parsed);
extension = Some(parsed);
} else {
xml = None;
extension = None;
}
// Now to handle the payload block
@@ -154,7 +154,7 @@ fn bc_modern_msg<'a, 'b>(
// Extract remainder of message as binary, if it exists
let decrypted;
let processed_payload_buf = if !header.is_encrypted() {
xml_buf
payload_buf
} else {
decrypted = xml_crypto::crypt(header.enc_offset, payload_buf);
&decrypted
@@ -168,7 +168,7 @@ fn bc_modern_msg<'a, 'b>(
payload = None;
}
Ok((buf, ModernMsg { xml, payload }))
Ok((buf, ModernMsg { extension, payload }))
}
fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
+17 -7
View File
@@ -1,4 +1,4 @@
pub use super::xml::{BcXml, Extension, BcXmls, BcPayloads};
pub use super::xml::{BcPayloads, BcXml, Extension};
use std::collections::HashSet;
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
@@ -27,7 +27,7 @@ pub enum BcBody {
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ModernMsg {
pub xml: Option<BcXmls>,
pub extension: Option<Extension>,
pub payload: Option<BcPayloads>,
}
@@ -77,17 +77,17 @@ impl Bc {
Bc {
meta,
body: BcBody::ModernMsg(ModernMsg {
xml: Some(BcXmls::BcXml(xml)),
payload: None,
extension: None,
payload: Some(BcPayloads::BcXml(xml)),
}),
}
}
pub fn new_from_ext_xml(meta: BcMeta, xml: Extension) -> Bc {
pub fn new_from_ext(meta: BcMeta, xml: Extension) -> Bc {
Bc {
meta,
body: BcBody::ModernMsg(ModernMsg {
xml: Some(BcXmls::Extension(xml)),
extension: Some(xml),
payload: None,
}),
}
@@ -97,11 +97,21 @@ impl Bc {
Bc {
meta,
body: BcBody::ModernMsg(ModernMsg {
xml: None,
extension: None,
payload: None,
}),
}
}
pub fn new_from_ext_ext(meta: BcMeta, ext: Extension, xml: BcXml) -> Bc {
Bc {
meta,
body: BcBody::ModernMsg(ModernMsg {
extension: Some(ext),
payload: Some(BcPayloads::BcXml(xml)),
}),
}
}
}
impl Default for BcContext {
+22 -22
View File
@@ -1,5 +1,5 @@
use super::model::*;
use super::xml::{BcXmls, BcPayloads};
use super::xml::{BcPayloads};
use super::xml_crypto;
use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple;
@@ -15,55 +15,55 @@ impl Bc {
// serialize the XML to have the metadata to build the header
let body_buf;
let payload_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)),
// First serialize ext
let (temp_buf, ext_len) = gen(
opt_ref(&modern.extension, |ext| bc_ext(self.meta.client_idx, ext)),
vec![],
)?;
body_buf = buf;
// Now get the offset of the payload
payload_offset = if has_payload_offset(self.meta.class) {
// If we're required to put binary length, put 0 if we have no binary
Some(if modern.payload.is_some() {
xml_len as u32
ext_len as u32
} else {
0
})
} else {
None
};
}
// Now get the payload part of the body and add to ext_buf
let (temp_buf, _) = gen(
opt_ref(&modern.payload, |payload_offset| bc_payload(self.meta.client_idx, payload_offset)),
temp_buf,
)?;
body_buf = temp_buf;
},
BcBody::LegacyMsg(ref legacy) => {
let (buf, _) = gen(bc_legacy(legacy), vec![])?;
body_buf = buf;
payload_offset = None;
}
}
};
// Now have enough info to create the header
let header = BcHeader::from_meta(&self.meta, body_buf.len() as u32, payload_offset);
let (mut buf, _n) = gen(tuple((bc_header(&header), slice(body_buf))), buf)?;
let (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 {
payload: Some(ref payload),
..
}) = self.body
{
let (buf2, _) = gen(bc_payload(header.enc_offset, payload), buf)?;
buf = buf2;
}
Ok(buf)
}
}
fn bc_xml<W: Write>(enc_offset: u32, xml: &BcXmls) -> impl SerializeFn<W> {
let xml_bytes = match xml {
BcXmls::BcXml(x) => x.serialize(vec![]).unwrap(),
BcXmls::Extension(x) => x.serialize(vec![]).unwrap(),
};
fn bc_ext<W: Write>(enc_offset: u32, xml: &Extension) -> impl SerializeFn<W> {
let xml_bytes = xml.serialize(vec![]).unwrap();
let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes);
slice(enc_bytes)
}
-21
View File
@@ -9,21 +9,6 @@ use yaserde_derive::{YaDeserialize, YaSerialize};
#[cfg(test)]
use indoc::indoc;
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
#[yaserde(flatten)]
pub enum BcXmls {
#[yaserde(rename = "body")]
BcXml(BcXml),
Extension(Extension),
}
// Required for YaDeserialize
impl Default for BcXmls {
fn default() -> Self {
BcXmls::BcXml(Default::default())
}
}
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
#[yaserde(flatten)]
pub enum BcPayloads {
@@ -59,12 +44,6 @@ pub struct BcXml {
pub norm: Option<Norm>,
}
impl BcXmls {
pub fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s)
}
}
impl BcXml {
pub fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s)
+3 -3
View File
@@ -143,8 +143,8 @@ impl BcCamera {
let nonce;
match legacy_reply.body {
BcBody::ModernMsg(ModernMsg {
xml:
Some(BcXmls::BcXml(BcXml {
payload:
Some(BcPayloads::BcXml(BcXml {
encryption: Some(encryption),
..
}),
@@ -215,7 +215,7 @@ impl BcCamera {
device_info = info;
}
BcBody::ModernMsg(ModernMsg {
xml: None,
extension: None,
payload: None,
}) => return Err(Error::AuthFailed),
_ => {