Implement support for de/serializing legacy messages
This commit is contained in:
1 parent
1897bf33c4
commit
f4f3bc3f7c
4 files changed
+89
-9
No files matched your search
+45
-3
@@ -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),
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -28,7 +28,8 @@ pub enum LegacyMsg {
|
||||
LoginMsg {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
},
|
||||
UnknownMsg,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
||||
Binary file not shown.
+42
-5
@@ -24,9 +24,10 @@ impl Bc {
|
||||
Some(if modern.binary.is_some() { xml_len as u32 } else { 0 })
|
||||
} else { None };
|
||||
}
|
||||
BcBody::LegacyMsg(..) => {
|
||||
//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<W: Write>(header: &BcHeader) -> impl SerializeFn<W> {
|
||||
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<W> + 'a {
|
||||
move |out: WriteContext<W>| {
|
||||
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<W, T, F>(opt: Option<T>, ser: impl Fn(T) -> F) -> impl SerializeFn<W>
|
||||
where F: SerializeFn<W>, T: Copy, W: Write
|
||||
@@ -102,7 +127,19 @@ fn do_nothing<W>() -> impl SerializeFn<W> {
|
||||
|
||||
|
||||
#[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");
|
||||
|
||||
|
||||
Reference in new issue
Block a user