sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.14k stars 55 forks source link

Make endianness dynamic/conditional. #321

Closed Frostie314159 closed 1 year ago

Frostie314159 commented 1 year ago

I have a case, where I have to parse a squashfs-header and the endianness, of the fs, was only fixed to LE with a version newer, than I need. It would be helpful to choose the endianness based on runtime criteria.

wcampbell0x2a commented 1 year ago

I'm not at my computer to give an example, but Endian is just a Ctx. You should be able to store it in a struct and use it with #[deku(ctx)]

On another topic, I have a squashfs library that uses deku: https://github.com/wcampbell0x2a/backhand. Although I only support 4.0 so I didn't run into this issue.

Frostie314159 commented 1 year ago

Actually this is about backhand. I'm trying to parse/modify a big endian squashfs, with qshs magic. While writing the patch I stumbled upon the default endianness being little.

wcampbell0x2a commented 1 year ago

😆 of course! And yes, you would need to translate the magic to a Ctx::Endian before sending it to all the children. I haven't don't it, so if you run into deku related problems lmk

Frostie314159 commented 1 year ago

The need for this arises from the fact, that many routers/APs from the 200x Era use BE Mips with squashfs. Coincidentally, I am working on such devices and squashfs-tools was just like: Deprecating: Meh; Deleting BE support: YESSS!.

sharksforarms commented 1 year ago

here's an example of a conditional endian-ness

use deku::ctx::Endian;
use deku::prelude::*;
use std::convert::{TryFrom, TryInto};

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(ctx = "endian: deku::ctx::Endian")]
struct Child {
    #[deku(endian = "endian")]
    data: u16,
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuTest {
    criteria: u8,
    #[deku(ctx = "if *criteria == 1 { Endian::Big } else { Endian::Little }")]
    child: Child,
}

fn main() {
    let test_data: &[u8] = [0x01, 0xAB, 0xCD].as_ref();

    let test_deku = DekuTest::try_from(test_data).unwrap();
    assert_eq!(
        DekuTest {
            criteria: 0x01,
            child: Child { data: 0xABCD },
        },
        test_deku,
    );

    let test_deku: Vec<u8> = test_deku.try_into().unwrap();
    assert_eq!(test_data.to_vec(), test_deku);
}
Frostie314159 commented 1 year ago

Thanks a lot!