1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

handle malformed utf8 in names and cvars

This commit is contained in:
Robin Appelman 2019-05-25 23:53:44 +02:00
commit d12cf35e47
9 changed files with 41 additions and 13 deletions

6
Cargo.lock generated
View file

@ -10,7 +10,7 @@ dependencies = [
[[package]]
name = "bitstream_reader"
version = "0.5.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitstream_reader_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -218,7 +218,7 @@ dependencies = [
name = "tf-demo-parser"
version = "0.1.0"
dependencies = [
"bitstream_reader 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitstream_reader 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"enum-primitive-derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"enumflags2 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"enumflags2_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -261,7 +261,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum bitstream_reader 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "379870632be0e114a5b8951251ae7a8aaaa863590468d35f4a474a7ee8ea625b"
"checksum bitstream_reader 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce18d8bbe27f4f79e0221775535a640c40760888431a63c22d92e926c5839c77"
"checksum bitstream_reader_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7ae1ff36caa3f111504ee053686356bf98bbc1cfe9a5b71e19bbc8294b04bdb1"
"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb"
"checksum ctor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9a43db2bba5cafdc6aa068c892a518e477ee0df3705e53ec70247a9ff93546d5"

View file

@ -12,7 +12,7 @@ name = "parse_demo"
path = "src/bin/main.rs"
[dependencies]
bitstream_reader = "0.5"
bitstream_reader = "0.5.1"
enum-primitive-derive = "0.1.2"
num-traits = "0.2"
enumflags2 = "0.5"

BIN
data/malformed_cvar.dem Normal file

Binary file not shown.

1
data/malformed_cvar.json Normal file

File diff suppressed because one or more lines are too long

View file

@ -130,11 +130,3 @@ pub struct CmdKeyValuesMessage {
}
impl BitSkip<LittleEndian> for CmdKeyValuesMessage{}
#[derive(BitRead, Debug)]
pub struct SetConVarMessage {
#[size_bits = 8]
vars: HashMap<String, String>,
}
impl BitSkip<LittleEndian> for SetConVarMessage{}

View file

@ -14,6 +14,7 @@ use crate::demo::message::stringtable::*;
use crate::demo::message::tempentities::*;
use crate::demo::message::usermessage::*;
use crate::demo::message::voice::*;
use crate::demo::message::setconvar::*;
use crate::demo::parser::ParseBitSkip;
pub mod bspdecal;
@ -25,6 +26,7 @@ pub mod stringtable;
pub mod tempentities;
pub mod usermessage;
pub mod voice;
pub mod setconvar;
#[derive(Primitive, Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]

View file

@ -0,0 +1,27 @@
use bitstream_reader::{BitRead, BitReadSized, LittleEndian, BitSkip};
use crate::demo::message::stringtable::log_base2;
use crate::{ReadResult, Stream};
#[derive(Debug)]
pub struct SetConVarMessage {
vars: Vec<(String, String)>,
}
impl BitRead<LittleEndian> for SetConVarMessage {
fn read(stream: &mut Stream) -> ReadResult<Self> {
let count: u8 = stream.read()?;
let mut vars: Vec<(String, String)> = Vec::with_capacity(count as usize);
for _ in 0..count {
let key = stream.read().unwrap_or_else(|_| "Malformed cvar name".to_string());
let value = stream.read().unwrap_or_else(|_| "Malformed cvar value".to_string());
vars.push((key, value));
}
Ok(SetConVarMessage {
vars
})
}
}
impl BitSkip<LittleEndian> for SetConVarMessage {}

View file

@ -268,7 +268,7 @@ impl Analyser {
}
fn parse_user_info(&mut self, text: &String, mut data: Stream) -> ReadResult<()> {
let name: String = data.read_sized(32)?;
let name: String = data.read_sized(32).unwrap_or("Malformed Name".into());
let user_id = UserId((data.read::<u32>()? & 255) as u8);
let steam_id: String = data.read()?;
let entity_id: Option<u32> = text.parse().ok();

View file

@ -44,6 +44,12 @@ fn snapshot_test_comp() {
snapshot_test("data/comp.dem", "data/comp.json");
}
#[test]
fn snapshot_test_malformed_cvar() {
snapshot_test("data/malformed_cvar.dem", "data/malformed_cvar.json");
}
#[test]
fn message_type_test_comp() {
dump_message_types("data/comp.dem", "data/comp_message_types.json");