diff --git a/src/bc/de.rs b/src/bc/de.rs
index 01d9a92..95e028f 100644
--- a/src/bc/de.rs
+++ b/src/bc/de.rs
@@ -13,17 +13,30 @@ impl Bc {
fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> {
let (buf, header) = bc_header(buf)?;
- let (buf, msg) = bc_modern_msg(&header, buf)?;
+ let (buf, body) = bc_body(&header, buf)?;
let bc = Bc {
- header,
- body: BcBody::ModernMsg(msg),
+ meta: header.to_meta(),
+ body,
};
Ok((buf, bc))
}
-fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b [u8]) -> IResult<&'b [u8], ModernMsg, E> {
+fn bc_body<'a, 'b>(header: &'a BcHeader, buf: &'b [u8])
+ -> IResult<&'b [u8], BcBody>
+{
+ if header.is_modern() {
+ let (buf, body) = bc_modern_msg(header, buf)?;
+ Ok((buf, BcBody::ModernMsg(body)))
+ } else {
+ unimplemented!()
+ }
+}
+
+fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b [u8])
+ -> IResult<&'b [u8], ModernMsg, E>
+{
let end_of_xml = header.bin_offset.unwrap_or(buf.len() as u32);
let (mut buf, body_buf) = take(end_of_xml)(buf)?;
@@ -56,7 +69,7 @@ fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b
Ok((buf, msg))
}
-fn bc_header<'a, E: ParseError<&'a [u8]>>(buf: &'a [u8]) -> IResult<&[u8], BcHeader, E> {
+fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
let (buf, _magic) = verify(le_u32, |x| *x == MAGIC_HEADER)(buf)?;
let (buf, msg_id) = le_u32(buf)?;
let (buf, body_len) = le_u32(buf)?;
@@ -84,33 +97,16 @@ fn bc_header<'a, E: ParseError<&'a [u8]>>(buf: &'a [u8]) -> IResult<&[u8], BcHea
fn test_bc_modern_login() {
let sample = include_bytes!("samples/model_sample_modern_login.bin");
- let (_, msg) = bc_msg(&sample[..]).unwrap();
- assert_eq!(msg.header.msg_id, 1);
- assert_eq!(msg.header.body_len, 145);
- assert_eq!(msg.header.enc_offset, 0x1000000);
- assert_eq!(msg.header.encrypted, true);
- assert_eq!(msg.header.class, 0x6614);
- match msg.body {
- BcBody::ModernMsg(ModernMsg{ xml: Some(xml), binary: None }) => {
- assert_eq!(xml.encryption.unwrap().nonce, "9E6D1FCB9E69846D")
- }
- _ => assert!(false),
- }
-}
-
-#[test]
-fn test_bc_with_binary() {
- let sample = include_bytes!("samples/model_sample_modern_login.bin");
-
- let (_, msg) = bc_msg(&sample[..]).unwrap();
- assert_eq!(msg.header.msg_id, 1);
- assert_eq!(msg.header.body_len, 145);
- assert_eq!(msg.header.enc_offset, 0x1000000);
- assert_eq!(msg.header.encrypted, true);
- assert_eq!(msg.header.class, 0x6614);
- match msg.body {
- BcBody::ModernMsg(ModernMsg{ xml: Some(xml), binary: None }) => {
- assert_eq!(xml.encryption.unwrap().nonce, "9E6D1FCB9E69846D")
+ let (buf, header) = bc_header(&sample[..]).unwrap();
+ let (_, body) = bc_body(&header, buf).unwrap();
+ assert_eq!(header.msg_id, 1);
+ assert_eq!(header.body_len, 145);
+ assert_eq!(header.enc_offset, 0x1000000);
+ 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")
}
_ => assert!(false),
}
diff --git a/src/bc/model.rs b/src/bc/model.rs
index 528730c..161cd61 100644
--- a/src/bc/model.rs
+++ b/src/bc/model.rs
@@ -2,12 +2,12 @@ use super::xml::Body;
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
-const MSG_ID_LOGIN: u32 = 1;
-const MSG_ID_VIDEO: u32 = 3;
+pub const MSG_ID_LOGIN: u32 = 1;
+pub const MSG_ID_VIDEO: u32 = 3;
#[derive(Debug, PartialEq, Eq)]
pub struct Bc {
- pub(super) header: BcHeader,
+ pub meta: BcMeta,
pub body: BcBody,
}
@@ -17,7 +17,7 @@ pub enum BcBody {
ModernMsg(ModernMsg),
}
-#[derive(Debug, PartialEq, Eq)]
+#[derive(Debug, Default, PartialEq, Eq)]
pub struct ModernMsg {
pub xml: Option
,
pub binary: Option>,
@@ -33,12 +33,30 @@ pub enum LegacyMsg {
#[derive(Debug, PartialEq, Eq)]
pub(super) struct BcHeader {
- pub(super) body_len: u32,
- pub(super) msg_id: u32,
- pub(super) enc_offset: u32,
- pub(super) encrypted: bool,
- pub(super) class: u16,
- pub(super) bin_offset: Option,
+ pub body_len: u32,
+ pub msg_id: u32,
+ pub enc_offset: u32,
+ pub encrypted: bool,
+ pub class: u16,
+ pub bin_offset: Option,
+}
+
+/// The components of the Baichuan TLV header that are not
+/// descriptions of the Body (the application dictates these)
+#[derive(Debug, PartialEq, Eq)]
+pub struct BcMeta {
+ pub msg_id: u32,
+ pub client_idx: u32,
+ pub class: u16,
+ pub encrypted: bool,
+}
+
+/// The components of the Baichuan header that must be filled out after the body is serialized, or
+/// is needed for the deserialization of the body (strictly part of the wire format of the message)
+#[derive(Debug, PartialEq, Eq)]
+pub(super) struct BcSendInfo {
+ pub body_len: u32,
+ pub bin_offset: Option,
}
impl BcHeader {
@@ -59,6 +77,26 @@ impl BcHeader {
pub fn is_encrypted(&self) -> bool {
self.encrypted || self.class == 0x6414
}
+
+ pub fn to_meta(&self) -> BcMeta {
+ BcMeta {
+ msg_id: self.msg_id,
+ client_idx: self.enc_offset,
+ class: self.class,
+ encrypted: self.encrypted,
+ }
+ }
+
+ pub fn from_meta(meta: &BcMeta, body_len: u32, bin_offset: Option) -> BcHeader {
+ BcHeader {
+ bin_offset,
+ body_len,
+ msg_id: meta.msg_id,
+ enc_offset: meta.client_idx,
+ class: meta.class,
+ encrypted: meta.encrypted,
+ }
+ }
}
pub(super) fn has_bin_offset(class: u16) -> bool {
diff --git a/src/bc/ser.rs b/src/bc/ser.rs
index 096b34d..e10924e 100644
--- a/src/bc/ser.rs
+++ b/src/bc/ser.rs
@@ -7,39 +7,53 @@ use super::model::*;
use super::xml::Body;
use super::xml_crypto;
+pub type Error = GenError;
+
impl Bc {
- pub fn serialize(&self, buf: &mut [u8]) -> Result<(), GenError> {
- gen(bc_msg(self), buf).map(|_| ())
+ pub fn serialize(&self, buf: W) -> Result {
+ // Ideally this would be a combinator, but that would be hairy because we have to
+ // serialize the XML to have the metadata to build the header
+ let body_buf;
+ 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!())?;
+ 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 };
+ }
+ BcBody::LegacyMsg(..) => {
+ //bin_offset = None;
+ unimplemented!()
+ }
+ }
+
+ // 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)?;
+
+ // Put the binary part of the body, TODO this is poorly written
+ if let BcBody::ModernMsg(ModernMsg { binary: Some(ref binary), .. }) = self.body {
+ let (buf2, _) = gen(slice(binary), buf)?;
+ buf = buf2
+ }
+
+ Ok(buf)
}
}
-fn bc_msg<'a, W: Write + 'a>(msg: &'a Bc) -> impl SerializeFn + '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 + 'a {
- tuple((
- opt_ref(&body.xml, |xml| bc_xml(true, 0, xml)),
- opt_ref(&body.binary, slice),
- ))
-}
-
-fn bc_xml(encrypted: bool, enc_offset: u32, xml: &Body) -> impl SerializeFn
+fn bc_xml(enc_offset: u32, xml: &Body) -> impl SerializeFn
{
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)
- }
+ let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes);
+ slice(enc_bytes)
}
fn bc_header(header: &BcHeader) -> impl SerializeFn {
@@ -94,9 +108,7 @@ fn test_login_roundtrip() {
let msg = Bc::deserialize(&sample[..]).unwrap();
- let mut ser_buf = vec![0; sample.len()];
- msg.serialize(&mut ser_buf[..]).unwrap();
-
+ let ser_buf = msg.serialize(vec!()).unwrap();
let msg2 = Bc::deserialize(ser_buf.as_ref()).unwrap();
assert_eq!(msg, msg2);
}
diff --git a/src/bc/xml.rs b/src/bc/xml.rs
index 3977c9b..a851912 100644
--- a/src/bc/xml.rs
+++ b/src/bc/xml.rs
@@ -52,7 +52,7 @@ pub struct LoginUser {
pub user_ver: u32,
}
-#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
+#[derive(PartialEq, Eq, Debug, YaDeserialize, YaSerialize)]
pub struct LoginNet {
#[yaserde(attribute)]
pub version: String,
@@ -62,6 +62,20 @@ pub struct LoginNet {
pub udp_port: u16,
}
+impl Default for LoginNet {
+ fn default() -> Self {
+ LoginNet {
+ version: xml_ver(),
+ type_: "LAN".to_string(),
+ udp_port: 0,
+ }
+ }
+}
+
+pub fn xml_ver() -> String {
+ "1.1".to_string()
+}
+
#[test]
fn test_encryption_deser() {
let sample = indoc!(r#"
@@ -141,7 +155,7 @@ fn test_login_ser() {
};
let b2 = Body::try_parse(sample.as_bytes()).unwrap();
- let b3 = Body::try_parse(yaserde::ser::to_string(&b).unwrap().as_bytes()).unwrap();
+ let b3 = Body::try_parse(b.serialize(vec!()).unwrap().as_slice()).unwrap();
assert_eq!(b, b2);
assert_eq!(b, b3);