From f5c19ad66b1ffadf49b9591b0960de007d52ffd1 Mon Sep 17 00:00:00 2001 From: Jake Probst Date: Sun, 23 Jun 2019 17:07:02 -0700 Subject: [PATCH] utf16 handling when parsing string bytes --- psopacket/src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index f1c11b5..abb6174 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -172,13 +172,17 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream { "String" => { from_bytes.push(quote! { #ident: { - let mut s = String::new(); - if let Ok(len) = cur.read_to_string(&mut s) { + let mut s: Vec = Vec::new(); + if let Ok(len) = cur.read_to_end(&mut s) { } else { return Err(PacketParseError::NotEnoughBytes); }; - s + let mut utf16 = Vec::new(); + for c in s.chunks(2) { + utf16.push(u16::from_le_bytes([c[0], c[1]])); + } + String::from_utf16_lossy(utf16.as_slice()) }, }); },