FString ANSI decoding/encoding

This commit is contained in:
mykola2312 2023-08-29 08:15:41 +03:00
parent 0eb6de377c
commit e24f67843f
2 changed files with 13 additions and 6 deletions

View file

@ -25,10 +25,10 @@ impl Decoder for FString {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> { fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rdr = Cursor::new(&raw.mem[offset..]); let mut rdr = Cursor::new(&raw.mem[offset..]);
let flen = rdr.read_u32::<LittleEndian>()? as usize; let flen = rdr.read_u32::<LittleEndian>()? as usize;
let len = flen ^ (1<<31); let len = flen & !(1<<31);
let start = offset + 4; let start = offset + 4;
if flen & (1<<31) == 0 { // ANSI if flen & (1<<31) == 0 { // ANSI
let str = str::from_utf8(&raw.mem[start..start+len])?; let (str, _, _) = WINDOWS_1251.decode(&raw.mem[start..start+len]);
Ok(FString { encoding: FStringEncoding::ANSI, enc_len: len, str: str.to_string() }) Ok(FString { encoding: FStringEncoding::ANSI, enc_len: len, str: str.to_string() })
} else { // WCS2 } else { // WCS2
let chars: Vec<u8> = raw.mem[start..start+len*2] let chars: Vec<u8> = raw.mem[start..start+len*2]
@ -42,8 +42,9 @@ impl Decoder for FString {
let mut buf = vec![0u8; 4]; let mut buf = vec![0u8; 4];
let mut wdr = Cursor::new(&mut buf[..]); let mut wdr = Cursor::new(&mut buf[..]);
if self.encoding == FStringEncoding::ANSI { if self.encoding == FStringEncoding::ANSI {
wdr.write_u32::<LittleEndian>(self.str.len() as u32 ^ (1<<31))?; let (chars, _, _) = WINDOWS_1251.encode(self.str.as_str());
buf.append(&mut self.str.clone().into_bytes()); wdr.write_u32::<LittleEndian>(chars.len() as u32 & !(1<<31))?;
buf.extend(chars.iter());
} else { // WCS2 } else { // WCS2
let (chars, _, _) = WINDOWS_1251.encode(self.str.as_str()); let (chars, _, _) = WINDOWS_1251.encode(self.str.as_str());
wdr.write_u32::<LittleEndian>(chars.len() as u32 | (1<<31))?; wdr.write_u32::<LittleEndian>(chars.len() as u32 | (1<<31))?;

View file

@ -1,3 +1,5 @@
use crate::fot::fstring::FStringEncoding;
use super::decoder::Decoder; use super::decoder::Decoder;
use super::raw::Raw; use super::raw::Raw;
use super::tag::Tag; use super::tag::Tag;
@ -22,12 +24,16 @@ impl World {
const WORLD_HDR_LEN: usize = 0x13; const WORLD_HDR_LEN: usize = 0x13;
pub fn test(&self) -> Result<()> { pub fn test(&self) -> Result<()> {
let a = FString::decode(&self.data, 0xA2, 0)?; let mut a = FString::decode(&self.data, 0xA2, 0)?;
dbg!(&a); dbg!(&a);
let b = a.encode(); a.encoding = FStringEncoding::ANSI;
let b = a.encode()?;
dbg!(&b); dbg!(&b);
let c = FString::decode(&b, 0, 0)?;
dbg!(&c);
Ok(()) Ok(())
} }
} }