feat: implement deku binary reading

This commit is contained in:
theo 2024-08-28 21:44:08 +02:00
parent 9b279ce4cd
commit 488f719919
Signed by: theo
SSH Key Fingerprint: SHA256:IbehMhSwpXrGUj7vj9iVvfdwe3g09IL9KLUz0zFzcXU
4 changed files with 321 additions and 236 deletions

291
Cargo.lock generated
View File

@ -56,6 +56,55 @@ dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
[[package]]
name = "anstyle-parse"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "anyhow"
version = "1.0.86"
@ -275,6 +324,18 @@ dependencies = [
"serde",
]
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "block"
version = "0.1.6"
@ -340,9 +401,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.7.0"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fca2be1d5c43812bae364ee3f30b3afcb7877cf59f4aeb94c66f313a41d2fac9"
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
dependencies = [
"serde",
]
@ -416,9 +477,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.1.7"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc"
checksum = "504bdec147f2cc13c8b57ed9401fd8a147cc66b67ad5cb241394244f2c947549"
[[package]]
name = "cesu8"
@ -502,6 +563,12 @@ dependencies = [
"objc",
]
[[package]]
name = "colorchoice"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "combine"
version = "4.6.7"
@ -673,6 +740,31 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "deku"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "709ade444d53896e60f6265660eb50480dd08b77bfc822e5dcc233b88b0b2fba"
dependencies = [
"bitvec",
"deku_derive",
"no_std_io",
"rustversion",
]
[[package]]
name = "deku_derive"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7534973f93f9de83203e41c8ddd32d230599fa73fa889f3deb1580ccd186913"
dependencies = [
"darling",
"proc-macro-crate 3.1.0",
"proc-macro2",
"quote",
"syn 2.0.72",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -770,6 +862,12 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "dpi"
version = "0.1.1"
@ -796,9 +894,9 @@ dependencies = [
[[package]]
name = "dunce"
version = "1.0.4"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
[[package]]
name = "dyn-clone"
@ -826,6 +924,29 @@ version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7"
[[package]]
name = "env_filter"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -863,9 +984,9 @@ dependencies = [
[[package]]
name = "flate2"
version = "1.0.30"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae"
checksum = "7f211bbe8e69bbd0cfdea405084f128ae8b4aaa6b0b522fc8f2b009084797920"
dependencies = [
"crc32fast",
"miniz_oxide",
@ -913,6 +1034,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "futf"
version = "0.1.5"
@ -1413,6 +1540,12 @@ dependencies = [
"libm",
]
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "1.4.1"
@ -1435,9 +1568,9 @@ dependencies = [
[[package]]
name = "hyper-util"
version = "0.1.6"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ab92f4f49ee4fb4f997c784b7a2e0fa70050211e0b6a287f898c3c9785ca956"
checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9"
dependencies = [
"bytes",
"futures-channel",
@ -1548,6 +1681,12 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itoa"
version = "0.4.8"
@ -1905,6 +2044,15 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "no_std_io"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fa5f306a6f2c01b4fd172f29bb46195b1764061bf926c75e96ff55df3178208"
dependencies = [
"memchr",
]
[[package]]
name = "nodrop"
version = "0.1.14"
@ -2097,9 +2245,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.36.2"
version = "0.36.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e"
checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9"
dependencies = [
"memchr",
]
@ -2382,9 +2530,9 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.18"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee4364d9f3b902ef14fab8a1ddffb783a1cb6b4bba3bfc1fa3922732c7de97f"
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
dependencies = [
"zerocopy",
]
@ -2414,6 +2562,15 @@ dependencies = [
"toml_edit 0.20.7",
]
[[package]]
name = "proc-macro-crate"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284"
dependencies = [
"toml_edit 0.21.1",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -2471,6 +2628,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand"
version = "0.7.3"
@ -2586,9 +2749,9 @@ dependencies = [
[[package]]
name = "regex"
version = "1.10.5"
version = "1.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
dependencies = [
"aho-corasick",
"memchr",
@ -2812,9 +2975,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.121"
version = "1.0.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ab380d7d9f22ef3f21ad3e6c1ebe8e4fc7a2000ccba2e4d71fc96f15b2cb609"
checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da"
dependencies = [
"itoa 1.0.11",
"memchr",
@ -2919,6 +3082,11 @@ dependencies = [
[[package]]
name = "services-sesam-vitale-sys"
version = "0.1.0"
dependencies = [
"bitvec",
"deku",
"libc",
]
[[package]]
name = "servo_arc"
@ -2934,7 +3102,11 @@ dependencies = [
name = "sesam-vitale"
version = "0.1.0"
dependencies = [
"deku",
"dotenv",
"env_logger",
"libc",
"services-sesam-vitale-sys",
]
[[package]]
@ -3198,6 +3370,12 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "target-lexicon"
version = "0.12.16"
@ -3206,9 +3384,9 @@ checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
[[package]]
name = "tauri"
version = "2.0.0-beta.25"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4339c67eb7700fe4a80ad6507e3b9af0b4159959f3d74aae48feed298baf4cbf"
checksum = "7e2200ca115a6812984431f07fb0daa00afcd68b09d3ca24941b470f79462aa6"
dependencies = [
"anyhow",
"bytes",
@ -3255,9 +3433,9 @@ dependencies = [
[[package]]
name = "tauri-build"
version = "2.0.0-beta.19"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "498f587026501e4bbc5d6273b63f8956b03c37b3d3b2027f9c756fcd468e9c62"
checksum = "be3ff85695ade2315c82a7c04ac8904b12c0cc9981187cf4cd38700a6c739bfd"
dependencies = [
"anyhow",
"cargo_toml",
@ -3277,9 +3455,9 @@ dependencies = [
[[package]]
name = "tauri-codegen"
version = "2.0.0-beta.19"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43bbc731067e319ef60601bf5716d1e706ee9ae28e38c0587f7165c7d6824cdf"
checksum = "ba3751f726e0180dfe43e66d6a73fe891eb898a06118b59547228ce8d331a0df"
dependencies = [
"base64 0.22.1",
"brotli",
@ -3304,9 +3482,9 @@ dependencies = [
[[package]]
name = "tauri-macros"
version = "2.0.0-beta.19"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36b4a44346577ccde75a24c62405a4c3b4f7a3a76614ee6cf1ed14a0b756795c"
checksum = "b03b174fc38ac96701f57fa1a8cfcc0686b10d1112e1ed98e9788689745c61e2"
dependencies = [
"heck 0.5.0",
"proc-macro2",
@ -3318,9 +3496,9 @@ dependencies = [
[[package]]
name = "tauri-runtime"
version = "2.0.0-beta.21"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "189510033be50f6fde35cfa50b50c7ab4e0ced0c867ae0f643b4907b8385bbe5"
checksum = "68b0586932e7fd72778fb1067c16b5edf0d0d23c3fe1a1d9a6d9b212e7ab8394"
dependencies = [
"dpi",
"gtk",
@ -3337,9 +3515,9 @@ dependencies = [
[[package]]
name = "tauri-runtime-wry"
version = "2.0.0-beta.21"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6938bd63b6658e7b08f0fe2151390148ee5a8ccdba100d4dff961d2c8734d9a9"
checksum = "0ebb9ec03b2418a29f56da626da9c1b00ce085effd48bc7444bd864d889fe7ae"
dependencies = [
"cocoa",
"gtk",
@ -3361,9 +3539,9 @@ dependencies = [
[[package]]
name = "tauri-utils"
version = "2.0.0-beta.19"
version = "2.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e20e51856f343c503892749b27d34042e6ca83a0369a12de3c5552d9874d04e8"
checksum = "a640df6551e1d47f3c05c5296aa6c8b41ffad0f6fdcd42e244c2eaec160cb428"
dependencies = [
"brotli",
"cargo_metadata",
@ -3594,6 +3772,17 @@ dependencies = [
"winnow 0.5.40",
]
[[package]]
name = "toml_edit"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"
dependencies = [
"indexmap 2.3.0",
"toml_datetime",
"winnow 0.5.40",
]
[[package]]
name = "toml_edit"
version = "0.22.20"
@ -3868,6 +4057,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.10.0"
@ -4133,11 +4328,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.8"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@ -4257,6 +4452,15 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
@ -4514,6 +4718,15 @@ dependencies = [
"x11-dl",
]
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "x11"
version = "2.21.0"
@ -4537,9 +4750,9 @@ dependencies = [
[[package]]
name = "zerocopy"
version = "0.6.6"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive",
@ -4547,9 +4760,9 @@ dependencies = [
[[package]]
name = "zerocopy-derive"
version = "0.6.6"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",

View File

@ -2,3 +2,8 @@
name = "services-sesam-vitale-sys"
version = "0.1.0"
edition = "2021"
[dependencies]
bitvec = "1.0.1"
deku = "0.17.0"
libc = "0.2.155"

View File

@ -1,3 +1 @@
pub(crate) mod types;
pub mod common;
pub mod serialization_types;

View File

@ -1,210 +1,79 @@
use core::panic;
use std::io::Cursor;
use bitvec::index::BitIdx;
use std::{error::Error, vec::Vec};
use binrw::{
binread,
helpers::{read_u24, write_u24},
BinRead, BinReaderExt, BinResult, BinWriterExt, Endian,
use deku::{
bitvec::{BitStore, Msb0},
ctx::ByteSize,
deku_derive,
reader::{Reader, ReaderRet},
DekuError, DekuReader,
};
const U8_MAX: u32 = u8::MAX as u32;
const U16_MAX: u32 = u16::MAX as u32;
const U24_MAX: u32 = 16_777_215;
#[deku_derive(DekuRead)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[deku(endian = "big")]
pub struct GroupId(u16);
#[binrw::parser(reader)]
fn parse_data_size() -> BinResult<u32> {
let first_byte: u8 = reader.read_be()?;
let first_bit: bool = (first_byte & 0b1000_0000) == 0;
Ok(match first_bit {
// first bit is 0 -> size is encoded in the first byte
true => first_byte as u32,
// first bit is 1 -> size is encoded on N bytes
// N being encoded by the first byte
false => match first_byte {
0 => 0,
1 => reader.read_be::<u8>()? as u32,
2 => reader.read_be::<u16>()? as u32,
3 => read_u24(reader, Endian::Big, ())?,
4 => reader.read_be::<u32>()?,
_ => panic!("Length should not be more than 4 bytes"),
},
})
trait MapToDekuParseError<T> {
fn map_to_deku_parse_error(self) -> Result<T, DekuError>;
}
#[binrw::writer(writer)]
fn write_data_size(memory_size: &u32) -> BinResult<()> {
match memory_size {
..=U8_MAX => writer.write_be(&(*memory_size as u8)),
// Since size is not encodable on a single byte
// We write the length encoding the size first, marking it with a flipped first bit
// Then write the size on the following bytes
..=U16_MAX => {
let size_encoding_length = 2u8 | 0b1000_0000;
writer.write_be(&size_encoding_length)?;
writer.write_be(&(*memory_size as u16))
}
..=U24_MAX => {
let size_encoding_length = 3u8 | 0b1000_0000;
writer.write_be(&size_encoding_length)?;
write_u24(memory_size, writer, Endian::Big, ())
}
_ => {
let size_encoding_length = 4u8 | 0b1000_0000;
writer.write_be(&size_encoding_length)?;
writer.write_be(memory_size)
}
impl<T, E: Error> MapToDekuParseError<T> for Result<T, E> {
fn map_to_deku_parse_error(self) -> Result<T, DekuError> {
self.map_err(|e| DekuError::Parse(e.to_string().into()))
}
}
// To parse the data
// allocate the multiple buffers
// chain them to make a single buffer
// use the parse_data_size function to get a size
// use take method to limit number of bytes read
// use binread implementaiton on each struct/enum de structure it
// do this recursively until there is no more data
#[deku_derive(DekuRead)]
#[derive(Debug, PartialEq)]
pub struct DekuDataField {
#[deku(reader = "read_size(deku::reader)")]
data_size: ByteSize,
// Memory has three embricked concepts:
// Memory Zone(s) -Contains-> DataBlock(s) -Contains-> DataField(s)
// DataBlocks (and DataFields) can be cut off by the end of a memory zone
// the data continues on the following memory zone
//
// will probably not be used
#[binread]
pub struct DataBlock{//<T: From<Vec<u8>>> {
data_struct_id: u16,
#[br(temp, parse_with = parse_data_size)]
memory_size: u32,
// spec indicates the DataBlock can be very large (up to 4GB)
// in this case, we can use memmap2 to use the disk to store the data
//pub data: Vec<DataField<T>>,
#[deku(bytes_read = "data_size.0")]
pub data: Vec<u8>,
}
#[binread]
pub struct DataField<T>
where
for<'a> T: BinRead<Args<'a>= ()>,
{
#[br(parse_with = parse_data_size)]
memory_size: u32,
#[deku_derive(DekuRead)]
#[derive(Debug, PartialEq)]
pub struct BlockHeader {
pub group_id: GroupId,
// using data -> not using the parser fw well, I think we can directly parse to the
// corresponding enum
//
// spec indicates the DataBlock can be very large (up to 4GB)
// in this case, we can use memmap2 to use the disk to store the data
#[br(count = memory_size)]
#[br(try_map = |data: Vec<u8>| T::read_be(&mut Cursor::new(data)))]
pub value: T,
#[deku(reader = "read_size(deku::reader)")]
pub data_size: ByteSize,
}
//// Memory allocation functions
//trait DataMaxSize {
// fn max_size(&self) -> usize;
//}
//
//pub struct Real;
//pub struct Test;
//pub struct Demo;
//
//// Trait for categories
//pub trait Category: 'static {
// const NAME: &'static str;
//}
//
//impl Category for Real {
// const NAME: &'static str = "Real";
//}
//impl Category for Test {
// const NAME: &'static str = "Test";
//}
//impl Category for Demo {
// const NAME: &'static str = "Demo";
//}
//
//// Enum for runtime category representation
//pub enum CategoryType {
// Real(Real),
// Test(Test),
// Demo(Demo),
//}
//
//// Card type with generic category
//#[derive(Debug)]
//pub enum CartePS<C: CategoryType> {
// CPS {
// reader_port: u32,
// _category: std::marker::PhantomData<C >,
// },
// CPF {
// some_cpf_data: String,
// _category: std::marker::PhantomData<C>,
// },
// CPE {
// some_cpe_data: bool,
// _category: std::marker::PhantomData<C>,
// },
//}
//
//// Function that only accepts Real CPS cards
//fn process_real_cps_card(card: CartePS<>) {
// if let CartePS::CPS { reader_port, .. } = card {
// println!(
// "Processing a real CPS card with reader port: {}",
// reader_port
// );
// }
//}
//fn main() {
// let cps = CartePS::<Real>::CPS {
// reader_port: 1,
// _category: std::marker::PhantomData,
// };
// process_real_cps_card(cps);
//}
//// need to see how to interface enums with binrw
//enum IdentificationStructure {
// NumeroAdeliCabinet,
// NumeroFINESS,
// NumeroSIREN,
// NumeroSIRET,
// NumeroRPPSCabinet,
//}
//
//pub enum TypeDIdentificationNationale {
// NumeroAdeli,
// NumeroAdeliCabinetNumeroEmploye,
// NumeroDRASS,
// NumeroFINESSNumeroEmploye,
// NumeroSIRENNumeroEmploye,
// NumeroSIRETNumeroEmploye,
// NumeroRPPSCabinetNumeroEmploye,
// NumeroRPPS,
// /// N° Etudiant Médecin type ADELI sur 9 caractères (information transmise par lANS)
// NumeroEtudiantMedecin,
//}
//
////#[derive(BinRead)]
////#[br(repr = [char;2], map = |[u8;2]| )]
//pub(crate) enum TypeCartePS {
// /// Carte de Professionnel de Santé (CPS)
// // CarteDeProfessionnelSante = ('0', '0'),
// /// Carte de Professionnel de Santé en Formation (CPF)
// // CarteDeProfessionnelSanteEnFormation = ('0', '1'),
// /// Carte de Personnel d'Établissement de Santé (CDE/CPE)
// CarteDePersonnelEtablissementSante,
// /// Carte de Personnel Autorisé (CDA/CPA)
// CarteDePersonnelAutorise,
// /// Carte de Personne Morale
// CarteDePersonneMorale,
//}
//
//impl DataMaxSize for TypeCartePS {
// fn max_size(&self) -> usize {
// 2
// }
//}
#[deku_derive(DekuRead)]
#[derive(Debug, PartialEq)]
pub struct DataBlock {
pub header: BlockHeader,
#[deku(bytes_read = "header.data_size.0")]
pub data: Vec<DekuDataField>,
}
fn read_size<R: std::io::Read>(reader: &mut Reader<R>) -> Result<ByteSize, DekuError> {
let first_byte: u8 = u8::from_reader_with_ctx(reader, ())?;
let is_length_expanded = first_byte.get_bit::<Msb0>(BitIdx::new(0).map_to_deku_parse_error()?);
match is_length_expanded {
true => {
let size_of_data_size: ByteSize = ByteSize((first_byte & 0b0111_1111) as usize);
if size_of_data_size.0 > 4 {
return Err(DekuError::Parse("Size of the length encoding is > 4, this is not normal. Probable parsing error".to_string().into()));
};
// maximum size of the buffer is 4, we use the offset to read values less than 4 bytes
let buffer: &mut [u8; 4] = &mut [0; 4];
let write_offset = 4 - size_of_data_size.0;
match reader.read_bytes(size_of_data_size.0, &mut buffer[write_offset..])? {
ReaderRet::Bits(_bit_vec) => Err(DekuError::Parse("Got bits when trying to read bytes -> reader is unaligned, this is not normal.".to_string().into())),
ReaderRet::Bytes => Ok(ByteSize(u32::from_be_bytes(*buffer) as usize)),
}
}
false => Ok(ByteSize(first_byte as usize)),
}
}