Skip to content

Commit

Permalink
Correctly decode strings
Browse files Browse the repository at this point in the history
Iracing uses CP1252 instead of UTF-8 to encode text
  • Loading branch information
gmartsenkov committed Apr 24, 2023
1 parent c37a3f1 commit 3456649
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ include = [
[dependencies]
serde = { version = "1.0", features = ["derive"], default-features = false }
serde_yaml = "0.9"
yore = "1.0.2"
17 changes: 4 additions & 13 deletions src/headers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::from_utf8;
use yore::code_pages::CP1252;

pub const HEADER_BYTES_SIZE: usize = 112;
pub const DISK_HEADER_BYTES_SIZE: usize = 32;
Expand Down Expand Up @@ -45,18 +45,9 @@ impl From<Vec<u8>> for VarHeader {
count: i32::from_le_bytes(data[8..12].try_into().unwrap()),
count_as_time: i8::from_le_bytes(data[12..13].try_into().unwrap()),
// padding here, 16 byte align (3 bytes)
name: from_utf8(&data[16..48])
.unwrap()
.to_string()
.replace('\0', ""),
description: from_utf8(&data[48..112])
.unwrap()
.to_string()
.replace('\0', ""),
unit: from_utf8(&data[112..144])
.unwrap()
.to_string()
.replace('\0', ""),
name: CP1252.decode(&data[16..48]).to_string().replace('\0', ""),
description: CP1252.decode(&data[48..112]).to_string().replace('\0', ""),
unit: CP1252.decode(&data[112..144]).to_string().replace('\0', ""),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::io::{Read, Seek, SeekFrom};
use headers::{DiskHeader, Header, VarHeader, DISK_HEADER_BYTES_SIZE, HEADER_BYTES_SIZE};
use samples::Samples;
use session_info::SessionInfo;
use yore::code_pages::CP1252;

use crate::headers::VAR_HEADER_BYTES_SIZE;

Expand All @@ -34,8 +35,7 @@ impl IbtReader {
header.sesion_info_length as usize,
)
.unwrap();
let session_info =
serde_yaml::from_str(&String::from_utf8_lossy(&session_info_data)).unwrap();
let session_info = serde_yaml::from_str(&CP1252.decode(&session_info_data)).unwrap();

let vars_data = get_var_header(&mut buffer, &header);
let vars: Vec<VarHeader> = (0..header.num_vars)
Expand Down

0 comments on commit 3456649

Please sign in to comment.