From 8e831fc045bc4a5b94b5b848b5111f7c57008a15 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Sat, 18 Apr 2020 22:12:41 -0500 Subject: [PATCH] Bc deserializer: handle missing XML, binary correctly --- src/bc/de.rs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/bc/de.rs b/src/bc/de.rs index ed48192..6c837c9 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -115,10 +115,12 @@ fn bc_modern_msg<'a, 'b>(header: &'a BcHeader, buf: &'b [u8]) // Extract remainder of message as binary, if it exists let mut binary = None; if let Some(bin_offset) = header.bin_offset { - let (buf_after, payload) = map(take(header.body_len - bin_offset), - |x: &[u8]| x.to_vec())(buf)?; - binary = Some(payload); - buf = buf_after; + if bin_offset > 0 { + let (buf_after, payload) = map(take(header.body_len - bin_offset), + |x: &[u8]| x.to_vec())(buf)?; + binary = Some(payload); + buf = buf_after; + } } // Now we'll take the buffer that Nom gave a ref to and parse it. Do this last, so we don't @@ -126,13 +128,13 @@ fn bc_modern_msg<'a, 'b>(header: &'a BcHeader, buf: &'b [u8]) // 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. use nom::{Err, error::{make_error, ErrorKind}}; - let body = Body::try_parse(processed_body_buf) - .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + let xml = if end_of_xml > 0 { + let parsed = Body::try_parse(processed_body_buf) + .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + Some(parsed) + } else { None }; - let msg = ModernMsg { - xml: Some(body), - binary, - }; + let msg = ModernMsg { xml, binary, }; Ok((buf, msg)) } @@ -201,3 +203,23 @@ fn test_bc_legacy_login() { _ => assert!(false), } } + +#[test] +fn test_bc_modern_login_failed() { + let sample = include_bytes!("samples/modern_login_failed.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, 0); + assert_eq!(header.enc_offset, 0x0); + assert_eq!(header.encrypted, true); + assert_eq!(header.class, 0x0000); + println!("{:?}", body); + match body { + BcBody::ModernMsg(ModernMsg{ xml: None, binary: None }) => { + assert!(true); + } + _ => assert!(false), + } +}