Giter VIP home page Giter VIP logo

serde-gff's Introduction

serde-gff

Crates.io Документация Лицензия MIT

Generic File Format (GFF) -- формат файлов, используемый играми на движке Bioware Aurora: Newerwinter Nights, The Witcher и Newerwinter Nights 2.

Формат имеет некоторые ограничения:

  • элементами верхнего уровня могут быть только структуры или перечисления Rust в unit или struct варианте
  • имена полей структур не должны быть длиннее 16 байт в UTF-8. При нарушении при сериализации будет ошибка
  • то же самое касается ключей карт. Кроме того, ключами могут быть только строки (&str или String)

Установка

Выполните в корне проекта

cargo add serde_gff

или добавьте следующую строку в Cargo.toml:

[dependencies]
serde_gff = "0.2"

Пример

use std::f32::consts::PI;
use std::f64::consts::E;
use std::io::Cursor;
use serde::{Serialize, Deserialize};

use serde_gff::de::Deserializer;
use serde_gff::ser::to_vec;
use serde_gff::value::Value;

#[derive(Debug, Serialize, Deserialize)]
struct Item { u8: u8, i8: i8 }

#[derive(Debug, Serialize, Deserialize)]
struct Struct {
  f32: f32,
  f64: f64,

  #[serde(with = "serde_bytes")]
  bytes: Vec<u8>,
}

#[derive(Debug, Serialize, Deserialize)]
#[allow(non_snake_case)]
struct Test {
  u16: u16,
  i16: i16,
  u32: u32,
  i32: i32,
  u64: u64,
  i64: i64,

  string: String,

  Struct: Struct,
  list: Vec<Item>,
}

fn main() {
  let data = Test {
    u16: 1, i16: 2,
    u32: 3, i32: 4,
    u64: 5, i64: 6,

    string: "String".into(),

    Struct: Struct { f32: PI, f64: E, bytes: b"Vec<u8>".to_vec() },
    list: vec![
      Item { u8: 7, i8:  -8 },
      Item { u8: 9, i8: -10 },
    ],
  };

  let mut vec = to_vec((*b"GFF ").into(), &data).expect("can't write data");
  // Важный нюанс - не забыть, что создание десериализатора читает заголовок и возвращает
  // Result, а не сам десериализатор, поэтому требуется распаковка результата
  let mut de = Deserializer::new(Cursor::new(vec)).expect("can't read GFF header");
  let val = Value::deserialize(&mut de).expect("can't deserialize data");

  println!("{:#?}", val);
}

serde-gff's People

Contributors

dependabot-preview[bot] avatar mingun avatar

Watchers

 avatar  avatar  avatar

serde-gff's Issues

Custom `Read` on uninitialized buffer may cause undefined behavior

Hello,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.

Issue Description

Methods parser::Parser::<R>::read_resref, parser::Parser::<R>::read_bytes, raw::Gff::read and macro read_into creates an uninitialized buffer and passes it to user-provided Read implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).

serde-gff/src/parser/mod.rs

Lines 248 to 257 in f5b0d10

pub fn read_resref(&mut self, index: ResRefIndex) -> Result<ResRef> {
self.seek(index)?;
let size = self.reader.read_u8()? as usize;
let mut bytes = Vec::with_capacity(size);
unsafe { bytes.set_len(size); }
self.reader.read_exact(&mut bytes)?;
Ok(ResRef(bytes))
}

serde-gff/src/parser/mod.rs

Lines 335 to 343 in f5b0d10

#[inline]
fn read_bytes(&mut self) -> Result<Vec<u8>> {
let size = self.read_u32()? as usize;
let mut bytes = Vec::with_capacity(size);
unsafe { bytes.set_len(size); }
self.reader.read_exact(&mut bytes)?;
Ok(bytes)
}

serde-gff/src/raw.rs

Lines 315 to 337 in f5b0d10

pub fn read<R: Read + Seek>(reader: &mut R) -> Result<Gff> {
let header = Header::read(reader)?;
let structs = read_exact!(reader, header.structs, Struct);
let fields = read_exact!(reader, header.fields , Field);
reader.seek(SeekFrom::Start(header.labels.offset as u64))?;
let mut labels = Vec::with_capacity(header.labels.count as usize);
for _ in 0..header.labels.count {
let mut label = [0u8; 16];
reader.read_exact(&mut label)?;
labels.push(label.into());
}
reader.seek(SeekFrom::Start(header.field_data.offset as u64))?;
let mut field_data = Vec::with_capacity(header.field_data.count as usize);
unsafe { field_data.set_len(header.field_data.count as usize); }
reader.read_exact(&mut field_data[..])?;
let field_indices = read_into!(reader, header.field_indices);
let list_indices = read_into!(reader, header.list_indices);
Ok(Gff { header, structs, fields, labels, field_data, field_indices, list_indices })
}

serde-gff/src/raw.rs

Lines 290 to 298 in f5b0d10

macro_rules! read_into {
($reader:expr, $section:expr) => ({
$reader.seek(SeekFrom::Start($section.offset as u64))?;
let mut vec = Vec::with_capacity($section.count as usize);
unsafe { vec.set_len(($section.count / 4) as usize); }
$reader.read_u32_into::<LE>(&mut vec[..])?;
vec
});
}

In case a user-provided Read reads from the given buffer, uninitialized buffer can make safe Rust code to cause memory safety errors by miscompilation. Uninitialized values are lowered to LLVM as llvm::UndefValue which may take different random values for each read. Propagation of UndefValue can quickly cause safe Rust code to exhibit undefined behavior.

This part from the Read trait documentation explains the issue:

It is your responsibility to make sure that buf is initialized before calling read. Calling read with an uninitialized buf (of the kind one obtains via MaybeUninit<T>) is not safe, and can lead to undefined behavior.

How to fix the issue?

The Naive & safe way to fix the issue is to always zero-initialize a buffer before lending it to a user-provided Read implementation. Note that this approach will add runtime performance overhead of zero-initializing the buffer.

As of March 2021, there is not yet an ideal fix that works with no performance overhead. Below are links to relevant discussions & suggestions for the fix.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.