From f4f3bc3f7ceec641ff24eac65319da1fd9c30ce2 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Sat, 18 Apr 2020 19:04:24 -0500 Subject: [PATCH] Implement support for de/serializing legacy messages --- src/bc/de.rs | 48 +++++++++++++++++-- src/bc/model.rs | 3 +- src/bc/samples/model_sample_legacy_login.bin | Bin 0 -> 1856 bytes src/bc/ser.rs | 47 ++++++++++++++++-- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/bc/samples/model_sample_legacy_login.bin diff --git a/src/bc/de.rs b/src/bc/de.rs index 95e028f..db8ee76 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -30,12 +30,32 @@ fn bc_body<'a, 'b>(header: &'a BcHeader, buf: &'b [u8]) let (buf, body) = bc_modern_msg(header, buf)?; Ok((buf, BcBody::ModernMsg(body))) } else { - unimplemented!() + let (buf, body) = match header.msg_id { + MSG_ID_LOGIN => bc_legacy_login_msg(buf)?, + _ => (buf, LegacyMsg::UnknownMsg), + }; + Ok((buf, BcBody::LegacyMsg(body))) } } -fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b [u8]) - -> IResult<&'b [u8], ModernMsg, E> +fn hex32<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], String> { + 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> +{ + let (buf, username) = hex32()(buf)?; + let (buf, password) = hex32()(buf)?; + + Ok((buf, LegacyMsg::LoginMsg { + username, + password, + })) +} + +fn bc_modern_msg<'a, 'b>(header: &'a BcHeader, buf: &'b [u8]) + -> IResult<&'b [u8], ModernMsg> { let end_of_xml = header.bin_offset.unwrap_or(buf.len() as u32); @@ -111,3 +131,25 @@ fn test_bc_modern_login() { _ => assert!(false), } } + +#[test] +fn test_bc_legacy_login() { + let sample = include_bytes!("samples/model_sample_legacy_login.bin"); + + 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, 1836); + assert_eq!(header.enc_offset, 0x1000000); + assert_eq!(header.encrypted, true); + assert_eq!(header.class, 0x6514); + match body { + BcBody::LegacyMsg(LegacyMsg::LoginMsg { + username, password + }) => { + assert_eq!(username, "21232F297A57A5A743894A0E4A801FC\0"); + assert_eq!(password, "\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"); + } + _ => assert!(false), + } +} diff --git a/src/bc/model.rs b/src/bc/model.rs index 161cd61..aca624e 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -28,7 +28,8 @@ pub enum LegacyMsg { LoginMsg { username: String, password: String, - } + }, + UnknownMsg, } #[derive(Debug, PartialEq, Eq)] diff --git a/src/bc/samples/model_sample_legacy_login.bin b/src/bc/samples/model_sample_legacy_login.bin new file mode 100644 index 0000000000000000000000000000000000000000..d055a880bc15fa347bfd5f931127c9cea4d816f7 GIT binary patch literal 1856 zcmexxZx0tE0|SE&JCI;tU}U@_l4@jVWNhSSWNGeb3Pg_PCdL+)CXNQKCXN;chHlPa Xg`>o12#kinXb6mkz-S1J { - //bin_offset = None; - unimplemented!() + BcBody::LegacyMsg(ref legacy) => { + let (buf, _) = gen(bc_legacy(legacy), vec!())?; + body_buf = buf; + bin_offset = None; } } @@ -64,12 +65,36 @@ fn bc_header(header: &BcHeader) -> impl SerializeFn { le_u32(header.enc_offset), le_u8(header.encrypted as u8), //le_u8(header.response_code), - le_u8(0), // skipped byte + le_u8(0xdc), // skipped byte le_u16(header.class), opt(header.bin_offset, le_u32), )) } +fn bc_legacy<'a, W: Write>(legacy: &'a LegacyMsg) -> impl SerializeFn + 'a { + move |out: WriteContext| { + use LegacyMsg::*; + match legacy { + LoginMsg { username, password } => { + if username.len() != 32 || password.len() != 32 { + // Error handling could be improved here... + return Err(GenError::CustomError(0)); + } + tuple(( + slice(username), + slice(password), + // Login messages are 1836 bytes total, username/password + // take up 32 chars each, 1772 zeros follow + slice(&[0u8; 1772][..]) + ))(out) + } + UnknownMsg => { + panic!("Cannot serialize an unknown message!"); + } + } + } +} + /// 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 @@ -102,7 +127,19 @@ fn do_nothing() -> impl SerializeFn { #[test] -fn test_login_roundtrip() { +fn test_legacy_login_roundtrip() { + // I don't want to make up a sample message; just load it + let sample = include_bytes!("samples/model_sample_legacy_login.bin"); + let msg = Bc::deserialize(&sample[..]).unwrap(); + + let ser_buf = msg.serialize(vec!()).unwrap(); + let msg2 = Bc::deserialize(ser_buf.as_ref()).unwrap(); + assert_eq!(msg, msg2); + assert_eq!(&sample[..], ser_buf.as_slice()); +} + +#[test] +fn test_modern_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");