Bc: implement serialization
This commit is contained in:
1 parent
86c0d7aaae
commit
97d8d8a677
6 files changed
+140
-12
No files matched your search
+8
-3
@@ -4,6 +4,13 @@ use super::model::*;
|
||||
use super::xml::Body;
|
||||
use super::xml_crypto;
|
||||
|
||||
impl Bc {
|
||||
pub fn deserialize(buf: &[u8]) -> Result<Bc, ()> {
|
||||
// Throw away the nom-specific return types
|
||||
bc_msg(buf).map(|(_, bc)| bc).map_err(|_| ()) // TODO better error
|
||||
}
|
||||
}
|
||||
|
||||
fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> {
|
||||
let (buf, header) = bc_header(buf)?;
|
||||
let (buf, msg) = bc_modern_msg(&header, buf)?;
|
||||
@@ -61,9 +68,7 @@ fn bc_header<'a, E: ParseError<&'a [u8]>>(buf: &'a [u8]) -> IResult<&[u8], BcHea
|
||||
// A copy of the source code for the camera would be very useful.
|
||||
let encrypted = response_code != 0;
|
||||
|
||||
// See BcHeader::is_modern() for a description of which packets have the bin offset
|
||||
let has_bin_offset = class == 0x6414 || class == 0x0000;
|
||||
let (buf, bin_offset) = cond(has_bin_offset, le_u32)(buf)?;
|
||||
let (buf, bin_offset) = cond(has_bin_offset(class), le_u32)(buf)?;
|
||||
|
||||
Ok((buf, BcHeader {
|
||||
msg_id,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod de;
|
||||
mod model;
|
||||
mod ser;
|
||||
mod xml;
|
||||
mod xml_crypto;
|
||||
|
||||
|
||||
@@ -5,21 +5,25 @@ pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
|
||||
const MSG_ID_LOGIN: u32 = 1;
|
||||
const MSG_ID_VIDEO: u32 = 3;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Bc {
|
||||
pub(super) header: BcHeader,
|
||||
pub body: BcBody,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BcBody {
|
||||
LegacyMsg(LegacyMsg),
|
||||
ModernMsg(ModernMsg),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct ModernMsg {
|
||||
pub xml: Option<Body>,
|
||||
pub binary: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum LegacyMsg {
|
||||
LoginMsg {
|
||||
username: String,
|
||||
@@ -27,6 +31,7 @@ pub enum LegacyMsg {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) struct BcHeader {
|
||||
pub(super) body_len: u32,
|
||||
pub(super) msg_id: u32,
|
||||
@@ -56,3 +61,7 @@ impl BcHeader {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn has_bin_offset(class: u16) -> bool {
|
||||
// See BcHeader::is_modern() for a description of which packets have the bin offset
|
||||
class == 0x6414 || class == 0x0000
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
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::Body;
|
||||
use super::xml_crypto;
|
||||
|
||||
impl Bc {
|
||||
pub fn serialize(&self, buf: &mut [u8]) -> Result<(), GenError> {
|
||||
gen(bc_msg(self), buf).map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
fn bc_msg<'a, W: Write + 'a>(msg: &'a Bc) -> impl SerializeFn<W> + 'a {
|
||||
use BcBody::*;
|
||||
tuple((
|
||||
bc_header(&msg.header),
|
||||
match msg.body {
|
||||
ModernMsg(ref body) => bc_modern(body),
|
||||
LegacyMsg(..) => unimplemented!()
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
fn bc_modern<'a, W: 'a + Write>(body: &'a ModernMsg) -> impl SerializeFn<W> + 'a {
|
||||
tuple((
|
||||
opt_ref(&body.xml, |xml| bc_xml(true, 0, xml)),
|
||||
opt_ref(&body.binary, slice),
|
||||
))
|
||||
}
|
||||
|
||||
fn bc_xml<W: Write>(encrypted: bool, enc_offset: u32, xml: &Body) -> impl SerializeFn<W>
|
||||
{
|
||||
let xml_bytes = xml.serialize(vec!()).unwrap();
|
||||
if encrypted {
|
||||
let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes);
|
||||
slice(enc_bytes)
|
||||
} else {
|
||||
slice(xml_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
fn bc_header<W: Write>(header: &BcHeader) -> impl SerializeFn<W> {
|
||||
tuple((
|
||||
le_u32(MAGIC_HEADER),
|
||||
le_u32(header.msg_id),
|
||||
le_u32(header.body_len),
|
||||
le_u32(header.enc_offset),
|
||||
le_u8(header.encrypted as u8),
|
||||
//le_u8(header.response_code),
|
||||
le_u8(0), // skipped byte
|
||||
le_u16(header.class),
|
||||
opt(header.bin_offset, le_u32),
|
||||
))
|
||||
}
|
||||
|
||||
/// Applies the supplied serializer with the Option's interior data if present
|
||||
fn opt<W, T, F>(opt: Option<T>, ser: impl Fn(T) -> F) -> impl SerializeFn<W>
|
||||
where F: SerializeFn<W>, T: Copy, W: Write
|
||||
{
|
||||
move |buf: WriteContext<W>| {
|
||||
if let Some(val) = opt {
|
||||
ser(val)(buf)
|
||||
} else {
|
||||
do_nothing()(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_ref<'a, W, T, F, S>(opt: &'a Option<T>, ser: S) -> impl SerializeFn<W> + 'a
|
||||
where F: SerializeFn<W>, W: Write, S: Fn(&'a T) -> F + 'a
|
||||
{
|
||||
move |buf: WriteContext<W>| {
|
||||
if let Some(ref val) = opt {
|
||||
ser(&*val)(buf)
|
||||
} else {
|
||||
do_nothing()(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A serializer combinator that does nothing with its input
|
||||
fn do_nothing<W>() -> impl SerializeFn<W> {
|
||||
move |out: WriteContext<W>| Ok(out)
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_login_roundtrip() {
|
||||
// I don't want to make up a sample message; just load it
|
||||
let sample = include_bytes!("samples/model_sample_modern_login.bin");
|
||||
|
||||
let msg = Bc::deserialize(&sample[..]).unwrap();
|
||||
|
||||
let mut ser_buf = vec![0; sample.len()];
|
||||
msg.serialize(&mut ser_buf[..]).unwrap();
|
||||
|
||||
let msg2 = Bc::deserialize(ser_buf.as_ref()).unwrap();
|
||||
assert_eq!(msg, msg2);
|
||||
}
|
||||
+10
-9
@@ -4,7 +4,7 @@
|
||||
use std::io::{Read, Write};
|
||||
// YaSerde is currently naming the traits and the derive macros identically
|
||||
use yaserde_derive::{YaDeserialize, YaSerialize};
|
||||
use yaserde::{YaDeserialize, YaSerialize};
|
||||
use yaserde::{ser::Config, YaDeserialize, YaSerialize};
|
||||
// YaSerde currently needs this imported
|
||||
#[allow(pub_use_of_private_extern_crate)]
|
||||
use yaserde::log;
|
||||
@@ -12,7 +12,7 @@ use yaserde::log;
|
||||
#[cfg(test)]
|
||||
use indoc::indoc;
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename="body")]
|
||||
pub struct Body {
|
||||
#[yaserde(rename="Encryption")]
|
||||
@@ -27,9 +27,12 @@ impl Body {
|
||||
pub fn try_parse(s: impl Read) -> Result<Self, String> {
|
||||
yaserde::de::from_reader(s)
|
||||
}
|
||||
pub fn serialize<W: Write>(&self, w: W) -> Result<W, String> {
|
||||
yaserde::ser::serialize_with_writer(self, w, &Config::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct Encryption {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
@@ -38,8 +41,7 @@ pub struct Encryption {
|
||||
pub nonce: String,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename_all="camelCase")]
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct LoginUser {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
@@ -50,8 +52,7 @@ pub struct LoginUser {
|
||||
pub user_ver: u32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename_all="camelCase")]
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct LoginNet {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
@@ -139,8 +140,8 @@ fn test_login_ser() {
|
||||
..Body::default()
|
||||
};
|
||||
|
||||
let b2: Body = yaserde::de::from_str(sample).unwrap();
|
||||
let b3: Body = yaserde::de::from_str(&yaserde::ser::to_string(&b).unwrap()).unwrap();
|
||||
let b2 = Body::try_parse(sample.as_bytes()).unwrap();
|
||||
let b3 = Body::try_parse(yaserde::ser::to_string(&b).unwrap().as_bytes()).unwrap();
|
||||
|
||||
assert_eq!(b, b2);
|
||||
assert_eq!(b, b3);
|
||||
|
||||
@@ -13,3 +13,13 @@ fn test_xml_crypto() {
|
||||
let decrypted = crypt(0, &sample[..]);
|
||||
assert_eq!(decrypted, &should_be[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xml_crypto_roundtrip() {
|
||||
let zeros: [u8; 256] = [0; 256];
|
||||
|
||||
let decrypted = crypt(0, &zeros[..]);
|
||||
let encrypted = crypt(0, &decrypted[..]);
|
||||
assert_eq!(encrypted, &zeros[..]);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user