1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::payload::{NextChunk, PayloadHeader, SafeHeaderReader};
use crate::*;
impl ReadFrom for PayloadHeader {
/// Reads a complete `PayloadHeader` from the stream.
///
/// This implementation assumes the entire header is available and performs no pre-checks
/// on input length. It will fail immediately if any part of the header cannot be read.
///
/// # Field Order
/// 1. Signature length (1 byte)
/// 2. Signature (`sig_len` bytes)
/// 3. CRC length (1 byte)
/// 4. CRC (`crc_len` bytes)
/// 5. Payload length (4 bytes, LE)
///
/// # Errors
/// - `Error::InvalidCapacity` if signature or CRC length is invalid
/// - `std::io::Error` if reading fails
/// - Any conversion error from `ByteBlock::try_into()`
fn read<T: std::io::Read>(buf: &mut T) -> Result<Self, Error> {
let mut sig_len = [0u8; 1];
buf.read_exact(&mut sig_len)?;
let sig_len = sig_len[0];
ByteBlock::is_valid_capacity(sig_len)?;
let mut sig = vec![0u8; sig_len as usize];
buf.read_exact(&mut sig)?;
let mut crc_len = [0u8; 1];
buf.read_exact(&mut crc_len)?;
let crc_len = crc_len[0];
ByteBlock::is_valid_capacity(crc_len)?;
let mut crc = vec![0u8; crc_len as usize];
buf.read_exact(&mut crc)?;
let mut len = [0u8; 4];
buf.read_exact(&mut len)?;
Ok(Self {
crc: crc.try_into()?,
len: u32::from_le_bytes(len),
sig: sig.try_into()?,
})
}
}
impl TryReadFrom for PayloadHeader {
/// Tries to read a `PayloadHeader` from a seekable stream using a safe, chunked approach.
///
/// Uses `SafeHeaderReader` and `NextChunk` to avoid over-reading when data is incomplete.
///
/// # Returns
/// - `ReadStatus::Success(header)` if fully parsed
/// - `ReadStatus::NotEnoughData(n)` if more bytes are required
/// - `Error::FailToReadPayloadHeader` if structure is invalid or inconsistent
/// - `Error::InvalidCapacity` for unsupported signature or CRC lengths
fn try_read<T: std::io::Read + std::io::Seek>(buf: &mut T) -> Result<ReadStatus<Self>, Error> {
let mut reader = SafeHeaderReader::new(buf)?;
let sig_len = match reader.next_u8()? {
NextChunk::NotEnoughData(n) => return Ok(ReadStatus::NotEnoughData(n)),
NextChunk::U8(v) => v,
_ => return Err(Error::FailToReadPayloadHeader),
};
ByteBlock::is_valid_capacity(sig_len)?;
let sig = match reader.next_bytes(sig_len as u64)? {
NextChunk::NotEnoughData(n) => return Ok(ReadStatus::NotEnoughData(n)),
NextChunk::Bytes(v) => v,
_ => return Err(Error::FailToReadPayloadHeader),
};
let crc_len = match reader.next_u8()? {
NextChunk::NotEnoughData(n) => return Ok(ReadStatus::NotEnoughData(n)),
NextChunk::U8(v) => v,
_ => return Err(Error::FailToReadPayloadHeader),
};
ByteBlock::is_valid_capacity(crc_len)?;
let crc = match reader.next_bytes(crc_len as u64)? {
NextChunk::NotEnoughData(n) => return Ok(ReadStatus::NotEnoughData(n)),
NextChunk::Bytes(v) => v,
_ => return Err(Error::FailToReadPayloadHeader),
};
let len = match reader.next_u32()? {
NextChunk::NotEnoughData(n) => return Ok(ReadStatus::NotEnoughData(n)),
NextChunk::U32(v) => v,
_ => return Err(Error::FailToReadPayloadHeader),
};
Ok(ReadStatus::Success(Self {
crc: crc.try_into()?,
len,
sig: sig.try_into()?,
}))
}
}
impl TryReadFromBuffered for PayloadHeader {
/// Tries to read a `PayloadHeader` from a buffered reader (`BufRead`) without consuming it.
///
/// This implementation works purely on the internal buffer (via `fill_buf`) and returns
/// `NotEnoughData` if more bytes are required to complete the header.
///
/// After confirming that enough bytes are available, it delegates to `PayloadHeader::read()`
/// using a `Cursor`.
///
/// # Returns
/// - `ReadStatus::Success(header)` - fully parsed header
/// - `ReadStatus::NotEnoughData(bytes)` - indicates how many more bytes are needed
/// - `Error::InvalidCapacity` if signature or CRC size is unsupported
fn try_read<T: std::io::BufRead>(reader: &mut T) -> Result<ReadStatus<Self>, Error> {
/// Helper function used in `try_read` to early-return if not enough bytes are in buffer.
///
/// It calculates the required number of bytes and returns `ReadStatus::NotEnoughData` if needed.
fn ensure_available(buffer: &[u8], required: usize) -> Option<ReadStatus<PayloadHeader>> {
if buffer.len() < required {
Some(ReadStatus::NotEnoughData((required - buffer.len()) as u64))
} else {
None
}
}
let buffer = reader.fill_buf()?;
let mut required = 1; // Signature size (u8)
if let Some(rs) = ensure_available(buffer, required) {
return Ok(rs);
}
let sig_len = buffer[required - 1];
ByteBlock::is_valid_capacity(sig_len)?;
required += sig_len as usize; // Add signature len
if let Some(rs) = ensure_available(buffer, required) {
return Ok(rs);
}
required += 1; // CRC size (u8)
if let Some(rs) = ensure_available(buffer, required) {
return Ok(rs);
}
let crc_len = buffer[required - 1];
ByteBlock::is_valid_capacity(crc_len)?;
required += crc_len as usize; // Add CRC len
if let Some(rs) = ensure_available(buffer, required) {
return Ok(rs);
}
required += 4; // Payload length (u32)
if let Some(rs) = ensure_available(buffer, required) {
return Ok(rs);
}
let header = PayloadHeader::read(&mut std::io::Cursor::new(buffer))?;
Ok(ReadStatus::Success(header))
}
}