WIP on (no branch): 48db472 snapshot 2021-12-18 21:05:00.635887

This commit is contained in:
jan 2022-03-20 16:28:34 -07:00
commit 07100dee8e

View file

@ -1,18 +1,15 @@
/*
* Generic record-level read/write functionality.
*/
use nom::IResult;
///
/// Generic record-level read/write functionality.
///
use std::io::Write;
use byteorder::BigEndian;
use std::convert::TryInto;
use byteorder::{ByteOrder, BigEndian};
use basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8}; #[warn(unused_imports)]
use basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8}; #[warn(unused_imports)]
use basic::{OWResult};
use basic::{OResult, IResult, fail, parse_u16, take_bytes};
use records;
//from .basic import parse_int2, parse_int4, parse_real8, parse_datetime, parse_bitarray
//from .basic import pack_int2, pack_int4, pack_real8, pack_datetime, pack_bitarray
//from .basic import parse_ascii, read
//#[no_mangle]
@ -26,37 +23,45 @@ pub struct RecordHeader {
}
impl RecordHeader {
pub fn parse(input: &[u8]) -> IResult<&[u8], RecordHeader> {
let (_, size) = nom::number::streaming::be_u16(input[0..])?;
let (_, tag) = nom::number::streaming::be_u16(input[2..])?;
Ok((input[4..], RecordHeader{tag:tag, data_size:size - 4}))
pub fn read(input: &[u8]) -> IResult<RecordHeader> {
let (input, size) = parse_u16(input)?;
let (input, tag) = parse_u16(input)?;
Ok((input, RecordHeader{tag:tag, data_size:size - 4}))
}
pub fn pack(self) -> [u8; 4] {
assert!(self.size < 0xffff - 4, "Record too big!");
let vals = [self.size, self.tag];
pub fn pack_into(&self) -> [u8; 4] {
assert!(self.data_size < 0xffff - 4, "Record too big!");
let vals = [self.data_size, self.tag];
let mut buf = [0x77; 4];
BigEndian::write_u16_into(&vals, &mut buf);
buf
}
pub fn write<W: Write>(&self, ww: W) -> OWResult {
let bytes = self.pack();
ww.write(bytes)
pub fn write<W: Write>(&self, ww: &mut W) -> OResult {
let bytes = self.pack_into();
ww.write(&bytes)
}
}
pub trait Record {
fn tag() -> u32;
fn expected_size() -> Option<usize>;
pub trait RecordData {
type BareData;
type InData : ?Sized;
type ByteData : AsRef<[u8]>;
//fn parse_data(input: &[u8], size: usize) -> IResult<&[u8], Self>;
fn read(input: &[u8], size: u16) -> IResult<Self::BareData>;
fn pack_into(buf: &mut [u8], data: &Self::InData);
//fn size(data: &Self::BareData) -> u16;
fn pack(data: &Self::InData) -> Self::ByteData;
}
impl Record {
pub fn check_size(&self, actual_size: usize) -> Result<(), &str> {
match self.expected_size() {
pub trait Record<RData: RecordData> {
fn tag() -> u16;
fn expected_size() -> Option<u16>;
fn check_size(actual_size: u16) -> Result<(), String> {
match Self::expected_size() {
Some(size) => if size == actual_size {
Ok(())
} else {
@ -66,154 +71,323 @@ impl Record {
}
}
pub fn parse_header(input: &[u8]) -> IResult<&[u8], RecordHeader> {
RecordHeader::parse(input)
fn read_header(input: &[u8]) -> IResult<RecordHeader> {
RecordHeader::read(input)
}
pub fn write_header<W: Write>(ww: W, data_size: usize) -> OWResult {
RecordHeader{tag: Self.tag(), size: data_size}.write(ww)
fn write_header<W: Write>(ww: &mut W, data_size: u16) -> OResult {
RecordHeader{tag: Self::tag(), data_size: data_size}.write(ww)
}
pub fn skip_past(input: &[u8]) -> IResult<&[u8], bool> {
/*
* Skip to the end of the next occurence of this record.
*
* Return:
* True if the record was encountered and skipped.
* False if the end of the library was reached.
*/
let (input, header) = RecordHeader::parse(input)?;
while header.tag != Self.tag() {
let (input, _) = nom::bytes::streaming::take(header.size)?;
fn read_data(input: &[u8], size: u16) -> IResult<RData::BareData> {
RData::read(input, size)
}
fn pack_data(buf: &mut [u8], data: &RData::InData) {
RData::pack_into(buf, data)
}
///
/// Skip to the end of the next occurence of this record.
///
/// Return:
/// True if the record was encountered and skipped.
/// False if the end of the library was reached.
///
fn skip_past(input: &[u8]) -> IResult<bool> {
let original_input = input;
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != Self::tag() {
(input, _) = take_bytes(input, header.data_size)?;
if header.tag == records::RTAG_ENDLIB {
return Ok((input, false))
return Ok((original_input, false))
}
let (input, header) = RecordHeader::parse(input)?;
(input, header) = RecordHeader::read(input)?;
}
let (input, _) = nom::bytes::streaming::take(header.size)?;
(input, _) = take_bytes(input, header.data_size)?;
Ok((input, true))
}
/*
pub fn skip_and_read(input: &[u8]) -> IResult<&[u8], bool>{
size, tag = Record.read_header(stream)
while tag != cls.tag{
stream.seek(size, io.SEEK_CUR)
size, tag = Record.read_header(stream)
fn skip_and_read(input: &[u8]) -> IResult<RData::BareData> {
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != Self::tag() {
(input, _) = take_bytes(input, header.data_size)?;
(input, header) = RecordHeader::read(input)?;
}
data = cls.read_data(stream, size)
return data
let (input, data) = Self::read_data(input, header.data_size)?;
Ok((input, data))
}
def read(cls: Type[R], stream: BinaryIO){
size = expect_record(stream, cls.tag)
data = cls.read_data(stream, size)
return data
fn expect_header(input: &[u8]) -> IResult<u16> {
let (input, header) = RecordHeader::read(input)?;
if header.tag != Self::tag() {
fail(input, format!("Unexpected record! Got tag 0x{:04x}, expected 0x{:04x}", header.tag, Self::tag()))
} else {
Ok((input, header.data_size))
}
}
def write(cls, stream: BinaryIO, data) -> int {
data_bytes = cls.pack_data(data)
b = cls.write_header(stream, len(data_bytes))
b += stream.write(data_bytes)
return b
fn read(input: &[u8]) -> IResult<RData::BareData> {
let (input, size) = Self::expect_header(input)?;
Self::check_size(size).unwrap();
let (input, data) = Self::read_data(input, size)?;
Ok((input, data))
}
fn write<W: Write>(ww: &mut W, data: &RData::InData) -> OResult {
let packed_data = RData::pack(data);
let data_bytes = packed_data.as_ref();
let len: u16 = data_bytes.len().try_into().expect("Record longer than max size (u16)!");
let mut size = 0;
size += Self::write_header(ww, len)?;
size += ww.write(data_bytes)?;
Ok(size)
}
*/
}
pub struct BitArray;
impl RecordData for BitArray {
type BareData = [bool; 16];
type InData = [bool; 16];
type ByteData = [u8; 2];
pub trait BitArray {
fn parse_data(input: &[u8]) -> IResult<&[u8], [bool; 16]> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 2);
parse_bitarray(input)
}
fn pack_data(buf: &mut [u8], vals: &[bool; 16]) {
pack_bitarray(&mut buf, vals)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_bitarray(buf, data);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub trait Int2 {
fn parse_data(input: &[u8]) -> IResult<&[u8], i16> {
pub struct Int2;
impl RecordData for Int2 {
type BareData = i16;
type InData = i16;
type ByteData = [u8; 2];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 2);
parse_int2(input)
}
fn pack_data(buf: &mut [u8], val: i16) {
pack_int2(&mut buf, val)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_int2(buf, *data)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub trait Int4 {
fn parse_data(input: &[u8]) -> IResult<&[u8], i32> {
pub struct Int4;
impl RecordData for Int4 {
type BareData = i32;
type InData = i32;
type ByteData = [u8; 4];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 4);
parse_int4(input)
}
fn pack_data(buf: &mut [u8], val: i32) {
pack_int4(&mut buf, val)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_int4(buf, *data)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 4];
Self::pack_into(&mut buf, data);
buf
}
}
pub trait Int2Array {
fn parse_data(input: &[u8], size: usize) -> IResult<&[u8], Vec<i16>> {
pub struct Int2Array;
impl RecordData for Int2Array {
type BareData = Vec<i16>;
type InData = [i16];
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size % 2 == 0, "Record must contain an integer quantity of integers");
nom::multi::count(parse_int2, size / 2)(input)
let mut buf = Vec::with_capacity(size as usize / 2);
let mut input = input;
for ii in 0..buf.len() {
(input, buf[ii]) = parse_int2(input)?;
}
Ok((input, buf))
}
fn pack_data(buf: &mut [u8], vals: &[i16]) {
BigEndian::write_i16_into(&vals, &mut buf)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
BigEndian::write_i16_into(&data, buf)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = Vec::with_capacity(data.len() * 2);
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int4Array;
impl RecordData for Int4Array {
type BareData = Vec<i32>;
type InData = [i32];
type ByteData = Vec<u8>;
pub trait Int4Array {
fn parse_data(input: &[u8], size: usize) -> IResult<&[u8], Vec<i32>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size % 4 == 0, "Record must contain an integer quantity of integers");
nom::multi::count(parse_int4, size / 4)(input)
let mut buf = Vec::with_capacity(size as usize / 4);
let mut input = input;
for ii in 0..buf.len() {
(input, buf[ii]) = parse_int4(input)?;
}
Ok((input, buf))
}
fn pack_data(buf: &mut [u8], vals: &[i32]) {
BigEndian::write_i32_into(&vals, &mut buf)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
BigEndian::write_i32_into(&data, buf)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = Vec::with_capacity(data.len() * 4);
Self::pack_into(&mut buf, data);
buf
}
}
pub trait Real8 {
fn parse_data(input: &[u8]) -> IResult<&[u8], f64> {
pub struct Real8;
impl RecordData for Real8 {
type BareData = f64;
type InData = f64;
type ByteData = [u8; 8];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 8);
parse_real8(input)
}
fn pack_data(buf: &mut [u8], val: f64) {
pack_real8(&mut buf, val)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(buf, *data)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 8];
Self::pack_into(&mut buf, data);
buf
}
}
pub trait ASCII {
fn parse_data(input: &[u8]) -> IResult<&[u8], Vec<u8>> {
parse_ascii(input)
pub struct Real8Pair;
impl RecordData for Real8Pair {
type BareData = (f64, f64);
type InData = (f64, f64);
type ByteData = [u8; 2 * 8];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 2 * 8);
let (input, data0) = parse_real8(input)?;
let (input, data1) = parse_real8(input)?;
Ok((input, (data0, data1)))
}
fn pack_data(buf: &mut [u8], data: &[u8]) {
pack_ascii(&mut buf, data)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(&mut buf[8 * 0..], data.0);
pack_real8(&mut buf[8 * 1..], data.1);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2 * 8];
Self::pack_into(&mut buf, data);
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
pub struct ASCII;
impl RecordData for ASCII {
type BareData = Vec<u8>;
type InData = [u8];
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
parse_ascii(input, size)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_ascii(buf, data);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = Vec::with_capacity(data.len() * 4);
Self::pack_into(&mut buf, data);
buf
}
}
pub trait DateTime {
fn parse_data(input: &[u8]) -> IResult<&[u8], [u16; 6]> {
parse_datetime(input)
pub struct DateTimePair;
impl RecordData for DateTimePair {
type BareData = [[i16; 6]; 2];
type InData = [[i16; 6]; 2];
type ByteData = [u8; 2 * 6 * 2];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 2 * 6 * 2);
let (input, data0) = parse_datetime(input)?;
let (input, data1) = parse_datetime(input)?;
Ok((input, [data0, data1]))
}
fn pack_data(buf: &mut [u8], data: [u16; 6]) {
pack_datetime(&mut buf, data)
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_datetime(&mut buf[6 * 2 * 0..], &data[0]);
pack_datetime(&mut buf[6 * 2 * 1..], &data[1]);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2 * 6 * 2];
Self::pack_into(&mut buf, data);
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
impl<DTR: DateTime + Record> DTR {
fn skip_and_read(input: &[u8]) -> IResult<&[u8], [DTR; 2]> {
let mut header = Self.read_header(input)?;
while header.tag != Self.tag() {
nom::bytes::streaming::take(header.data_size)?;
header = Self.read_header(input)?;
}
assert!(header.data_size == 6 * 2);
let data0 = Self.read_data(&input)?;
let data1 = Self.read_data(&input)?;
Ok([data0, data1])
pub struct Empty;
impl RecordData for Empty {
type BareData = ();
type InData = ();
type ByteData = [u8; 0];
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
assert!(size == 0);
Ok((input, ()))
}
fn pack_into(_buf: &mut [u8], _data: &Self::InData) {
}
fn pack(_data: &Self::InData) -> Self::ByteData {
[]
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) {
//}
}