msla_format 0.2.0

Library for encoding and decoding various MSLA file formats: Elegoo (.goo), Chitu Encrypted (.ctb), NanoDLP (.nanodlp).
Documentation
//! ChituBox encrypted format v5 (`.ctb`).
//!
//! ## References
//!
//! This implementation would not be possible without the work done by the UV Tools contributors. Thank you!!!
//!
//! - [UV Tools](https://github.com/sn4k3/UVtools)

use anyhow::Result;

use crate::serde::{Deserializer, Serializer, SliceDeserializer};

mod crypto;
mod file;
mod layer;
mod layer_coding;
mod preview;
mod resin;

pub use crate::ctb::{
    file::File,
    layer::Layer,
    layer_coding::{LayerDecoder, LayerEncoder},
    preview::PreviewImage,
    resin::ResinParameters,
};

#[derive(Debug)]
struct Section {
    pub size: u32,
    pub offset: u32,
}

impl Section {
    pub fn new(offset: usize, size: usize) -> Self {
        Self {
            size: size as u32,
            offset: offset as u32,
        }
    }

    pub fn deserialize(des: &mut SliceDeserializer) -> Result<Self> {
        Ok(Self {
            offset: des.read_u32_le(),
            size: des.read_u32_le(),
        })
    }

    pub fn serialize<T: Serializer>(&self, ser: &mut T) {
        ser.write_u32_le(self.offset);
        ser.write_u32_le(self.size);
    }

    pub fn deserialize_rev(des: &mut SliceDeserializer) -> Result<Self> {
        Ok(Self {
            size: des.read_u32_le(),
            offset: des.read_u32_le(),
        })
    }

    pub fn serialize_rev<T: Serializer>(&self, ser: &mut T) {
        ser.write_u32_le(self.size);
        ser.write_u32_le(self.offset);
    }
}

fn read_string(des: &mut SliceDeserializer, section: Section) -> String {
    des.execute_at(section.offset as usize, |des| {
        String::from_utf8_lossy(des.read_slice(section.size as usize)).into_owned()
    })
}