Skip to content

Commit

Permalink
String lengths can be u32
Browse files Browse the repository at this point in the history
  • Loading branch information
raiguard committed Mar 2, 2022
1 parent e677456 commit 603683c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ pub trait ReadFactorioDat: io::Read {
}

fn read_string(&mut self) -> Result<String> {
let scenario_len = self.read_u8()?;
let mut scenario_name = vec![0; scenario_len as usize];
let string_len = self.read_u32_optimized()?;
let mut scenario_name = vec![0; string_len as usize];
self.read_exact(&mut scenario_name)?;

Ok(String::from_utf8_lossy(&scenario_name).to_string())
Expand All @@ -204,6 +204,14 @@ pub trait ReadFactorioDat: io::Read {
}
Ok(num)
}

fn read_u32_optimized(&mut self) -> Result<u32> {
let mut num: u32 = self.read_u8()?.into();
if num == 255 {
num = self.read_u32::<LittleEndian>()?;
}
Ok(num)
}
}

/// All types that implement `Read` get methods defined in `ReadFactorioDat` for free.
Expand Down

0 comments on commit 603683c

Please sign in to comment.