pub const SOH: u8 = 0x01;
pub(crate) const MAX_BEGIN_STRING_LEN: usize = 16;
pub(crate) const MAX_BODY_LENGTH_DIGITS: usize = 10;
const CHECKSUM_FIELD_LEN: usize = 7;
pub(crate) const BEGIN_STRING_PREFIX: &[u8] = b"8=FIX";
pub struct Scanner {
max_frame_size: usize,
}
impl Scanner {
pub fn new(max_frame_size: usize) -> Self {
Self { max_frame_size }
}
pub fn scan<'a>(&self, buf: &'a [u8]) -> Outcome<'a> {
match self.try_scan(buf) {
Ok(frame) => Outcome::Frame(frame),
Err(halt) => halt.into(),
}
}
fn try_scan<'a>(&self, buf: &'a [u8]) -> Result<Frame<'a>, Halt> {
if !buf.starts_with(BEGIN_STRING_PREFIX) {
return if BEGIN_STRING_PREFIX.starts_with(buf) {
Err(Halt::Incomplete)
} else {
Err(structural_garble(GarbledReason::MissingBeginString, buf))
};
}
let (begin_string, body_length_start) = parse_begin_string(buf)?;
let (body_length, body_start) = parse_body_length(buf, body_length_start)?;
let frame_len = validated_frame_len(buf, body_start, body_length, self.max_frame_size)?;
let frame = &buf[..frame_len];
let checksum_start = body_start + body_length;
verify_trailer_start(buf, checksum_start)?;
verify_checksum(frame, checksum_start)?;
let msg_type = parse_msg_type(frame, body_start, body_length)?;
Ok(Frame {
bytes: frame,
begin_string,
body_length,
msg_type,
})
}
}
fn find_next_begin_string(buf: &[u8]) -> usize {
debug_assert!(
!BEGIN_STRING_PREFIX.starts_with(buf),
"a partial `8=FIX` has no resync distance; it is Incomplete",
);
if let Some(skip) = buf[1..]
.windows(BEGIN_STRING_PREFIX.len())
.position(|b| b == BEGIN_STRING_PREFIX)
{
return skip + 1;
}
let max_k = (BEGIN_STRING_PREFIX.len() - 1).min(buf.len());
for k in (1..=max_k).rev() {
if buf[buf.len() - k..] == BEGIN_STRING_PREFIX[..k] {
return buf.len() - k;
}
}
buf.len()
}
fn structural_garble(reason: GarbledReason, buf: &[u8]) -> Halt {
Halt::Garbled {
reason,
skipped: find_next_begin_string(buf),
}
}
fn post_structural_garble(reason: GarbledReason, frame: &[u8]) -> Halt {
Halt::Garbled {
reason,
skipped: frame.len(),
}
}
fn parse_begin_string(buf: &'_ [u8]) -> Result<(BeginString<'_>, usize), Halt> {
let search_end = buf.len().min(MAX_BEGIN_STRING_LEN + 1);
let soh = match buf[2..search_end].iter().position(|&b| b == SOH) {
Some(rel) => rel + 2,
None if buf.len() <= MAX_BEGIN_STRING_LEN => return Err(Halt::Incomplete),
None => return Err(structural_garble(GarbledReason::MalformedBeginString, buf)),
};
let value = &buf[2..soh];
let begin_string = match value {
b"FIX.4.0" => BeginString::Fix40,
b"FIX.4.1" => BeginString::Fix41,
b"FIX.4.2" => BeginString::Fix42,
b"FIX.4.3" => BeginString::Fix43,
b"FIX.4.4" => BeginString::Fix44,
b"FIXT.1.1" => BeginString::Fixt11,
other => BeginString::Other(other),
};
Ok((begin_string, soh + 1)) }
fn parse_body_length(buf: &[u8], body_length_start: usize) -> Result<(usize, usize), Halt> {
let tag = &buf[body_length_start..];
if tag.len() < 2 {
return Err(Halt::Incomplete);
}
if &tag[..2] != b"9=" {
return Err(structural_garble(GarbledReason::MissingBodyLength, buf));
}
let digits_start = body_length_start + 2;
let search_end = buf.len().min(digits_start + MAX_BODY_LENGTH_DIGITS + 1);
let soh_rel = match buf[digits_start..search_end].iter().position(|&b| b == SOH) {
Some(rel) => rel,
None if buf.len() <= digits_start + MAX_BODY_LENGTH_DIGITS => return Err(Halt::Incomplete),
None => return Err(structural_garble(GarbledReason::MalformedBodyLength, buf)),
};
let digits = &buf[digits_start..digits_start + soh_rel];
if digits.is_empty() || !digits.iter().all(|b| b.is_ascii_digit()) {
return Err(structural_garble(GarbledReason::MalformedBodyLength, buf));
}
let body_length = digits
.iter()
.try_fold(0usize, |acc, &b| {
acc.checked_mul(10)?.checked_add(usize::from(b - b'0'))
})
.ok_or_else(|| structural_garble(GarbledReason::MalformedBodyLength, buf))?;
let body_start = digits_start + soh_rel + 1; Ok((body_length, body_start))
}
fn validated_frame_len(
buf: &[u8],
body_start: usize,
body_length: usize,
max_frame_size: usize,
) -> Result<usize, Halt> {
let frame_len = body_start
.saturating_add(body_length)
.saturating_add(CHECKSUM_FIELD_LEN);
if frame_len > max_frame_size {
return Err(structural_garble(GarbledReason::FrameTooLarge, buf));
}
if buf.len() < frame_len {
return Err(Halt::Incomplete);
}
Ok(frame_len)
}
fn verify_trailer_start(buf: &[u8], checksum_start: usize) -> Result<(), Halt> {
if buf[checksum_start - 1] != SOH || &buf[checksum_start..checksum_start + 3] != b"10=" {
return Err(structural_garble(GarbledReason::BodyLengthMismatch, buf));
}
Ok(())
}
fn verify_checksum(frame: &[u8], checksum_start: usize) -> Result<(), Halt> {
let digits = &frame[checksum_start + 3..checksum_start + 6];
let trailing_soh = frame[checksum_start + 6];
if trailing_soh != SOH || !digits.iter().all(|b| b.is_ascii_digit()) {
return Err(post_structural_garble(
GarbledReason::MalformedChecksum,
frame,
));
}
let stated = u16::from(digits[0] - b'0') * 100
+ u16::from(digits[1] - b'0') * 10
+ u16::from(digits[2] - b'0');
let computed = frame[..checksum_start]
.iter()
.fold(0u8, |acc, &b| acc.wrapping_add(b));
if u16::from(computed) != stated {
return Err(post_structural_garble(
GarbledReason::ChecksumMismatch,
frame,
));
}
Ok(())
}
fn parse_msg_type(frame: &[u8], body_start: usize, body_length: usize) -> Result<&[u8], Halt> {
let region = &frame[body_start..body_start + body_length];
let Some(after_tag) = region.strip_prefix(b"35=") else {
return Err(post_structural_garble(GarbledReason::MissingMsgType, frame));
};
let Some(soh) = after_tag.iter().position(|&b| b == SOH) else {
return Err(post_structural_garble(GarbledReason::MissingMsgType, frame));
};
Ok(&after_tag[..soh])
}
impl Default for Scanner {
fn default() -> Self {
Self {
max_frame_size: 1 << 20,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome<'a> {
Frame(Frame<'a>),
Incomplete,
Garbled {
reason: GarbledReason,
skipped: usize,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame<'a> {
pub bytes: &'a [u8],
pub begin_string: BeginString<'a>,
pub body_length: usize,
pub msg_type: &'a [u8],
}
enum Halt {
Incomplete,
Garbled {
reason: GarbledReason,
skipped: usize,
},
}
impl<'a> From<Halt> for Outcome<'a> {
fn from(halt: Halt) -> Self {
match halt {
Halt::Incomplete => Outcome::Incomplete,
Halt::Garbled { reason, skipped } => Outcome::Garbled { reason, skipped },
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GarbledReason {
MissingBeginString,
MalformedBeginString,
MissingBodyLength,
MalformedBodyLength,
BodyLengthMismatch,
MissingMsgType,
MalformedChecksum,
ChecksumMismatch,
FrameTooLarge,
}
impl std::fmt::Display for GarbledReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
GarbledReason::MissingBeginString => "missing BeginString",
GarbledReason::MalformedBeginString => "malformed BeginString",
GarbledReason::MissingBodyLength => "missing BodyLength",
GarbledReason::MalformedBodyLength => "malformed BodyLength",
GarbledReason::BodyLengthMismatch => "BodyLength mismatch",
GarbledReason::MissingMsgType => "missing MsgType",
GarbledReason::MalformedChecksum => "malformed CheckSum",
GarbledReason::ChecksumMismatch => "CheckSum mismatch",
GarbledReason::FrameTooLarge => "frame too large",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BeginString<'a> {
Fix40,
Fix41,
Fix42,
Fix43,
Fix44,
Fixt11,
Other(&'a [u8]),
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{construct_valid_frame, to_wire};
fn scan(buf: &[u8]) -> Outcome<'_> {
Scanner::default().scan(buf)
}
#[track_caller]
fn assert_garbled(out: Outcome, reason: GarbledReason, skipped: usize) {
match out {
Outcome::Garbled {
reason: r,
skipped: s,
} => {
assert_eq!(r, reason, "reason");
assert_eq!(s, skipped, "skipped");
}
_ => panic!("expected Garbled({reason:?}), got a different outcome"),
}
}
#[track_caller]
fn assert_incomplete(out: Outcome) {
assert!(matches!(out, Outcome::Incomplete), "expected Incomplete");
}
#[track_caller]
fn frame(buf: &[u8]) -> Frame<'_> {
match scan(buf) {
Outcome::Frame(frame) => frame,
_ => panic!("expected Frame"),
}
}
mod resync {
use super::*;
#[test]
fn finds_needle_after_offset_zero() {
assert_eq!(find_next_begin_string(b"XX8=FIX"), 2);
}
#[test]
fn finds_needle_at_offset_one() {
assert_eq!(find_next_begin_string(b"X8=FIX"), 1);
}
#[test]
fn no_match_skips_whole_buffer() {
assert_eq!(find_next_begin_string(b"XYZW"), 4);
}
#[test]
fn preserves_longest_partial_tail() {
assert_eq!(find_next_begin_string(b"garbage8=FI"), 7);
}
#[test]
fn preserves_short_partial_tail() {
assert_eq!(find_next_begin_string(b"xx8="), 2);
}
#[test]
fn skip_is_always_in_range() {
for buf in [
&b"x"[..],
b"9",
b"=8",
b"junk",
b"8=FIXX",
b"xx8=",
b"x8=FI",
b"garbage8=FI",
b"XX8=FIX",
b"8=NOTFIX",
] {
let skipped = find_next_begin_string(buf);
assert!(
(1..=buf.len()).contains(&skipped),
"skip of {skipped} out of range 1..={} for {:?}",
buf.len(),
String::from_utf8_lossy(buf),
);
}
}
}
mod incomplete {
use super::*;
#[test]
fn empty() {
assert_incomplete(scan(b""));
}
#[test]
fn lone_8() {
assert_incomplete(scan(b"8"));
}
#[test]
fn begin_string_without_soh() {
assert_incomplete(scan(&to_wire("8=FIX.4.4")));
}
#[test]
fn body_not_yet_arrived() {
assert_incomplete(scan(&to_wire("8=FIX.4.4|9=100|35=A")));
}
}
mod structural {
use super::*;
#[test]
fn missing_begin_string() {
assert_garbled(scan(&to_wire("junk")), GarbledReason::MissingBeginString, 4);
}
#[test]
fn non_fix_prefix_is_missing_begin_string() {
assert_garbled(
scan(&to_wire("8=NOTFIX|9=5|")),
GarbledReason::MissingBeginString,
13,
);
}
#[test]
fn malformed_begin_string_overruns_bound() {
let mut buf = b"8=FIX".to_vec();
buf.extend([b'X'; 18]); assert_garbled(scan(&buf), GarbledReason::MalformedBeginString, 23);
}
#[test]
fn missing_body_length() {
assert_garbled(
scan(&to_wire("8=FIX.4.4|35=A|")),
GarbledReason::MissingBodyLength,
15,
);
}
#[test]
fn malformed_body_length_non_digit() {
assert_garbled(
scan(&to_wire("8=FIX.4.4|9=XY|")),
GarbledReason::MalformedBodyLength,
15,
);
}
#[test]
fn malformed_body_length_overruns_cap() {
assert_garbled(
scan(&to_wire("8=FIX.4.4|9=123456789012345")),
GarbledReason::MalformedBodyLength,
27,
);
}
#[test]
fn max_digit_body_length_does_not_panic() {
let buf = to_wire("8=FIX.4.4|9=9999999999|35=A|");
assert!(matches!(scan(&buf), Outcome::Garbled { .. }));
}
#[test]
fn frame_too_large_rejected_before_waiting() {
let buf = to_wire("8=FIX.4.4|9=100|");
assert_garbled(
Scanner::new(50).scan(&buf),
GarbledReason::FrameTooLarge,
16,
);
}
#[test]
fn body_length_mismatch() {
assert_garbled(
scan(&to_wire("8=FIX.4.4|9=3|35=A|10=000|")),
GarbledReason::BodyLengthMismatch,
26,
);
}
#[test]
fn resync_lands_on_next_message() {
let mut buf = to_wire("junk|X");
buf.extend_from_slice(&construct_valid_frame("FIX.4.4", "35=A|"));
assert_garbled(scan(&buf), GarbledReason::MissingBeginString, 6);
}
}
mod post_structural {
use super::*;
#[test]
fn checksum_mismatch() {
let mut buf = construct_valid_frame("FIX.4.4", "35=A|34=1|");
let len = buf.len();
let last_digit = &mut buf[len - 2]; *last_digit = if *last_digit == b'0' { b'1' } else { b'0' };
assert_garbled(scan(&buf), GarbledReason::ChecksumMismatch, len);
}
#[test]
fn malformed_checksum_non_digit() {
let mut buf = construct_valid_frame("FIX.4.4", "35=A|34=1|");
let len = buf.len();
buf[len - 2] = b'x'; assert_garbled(scan(&buf), GarbledReason::MalformedChecksum, len);
}
#[test]
fn missing_msg_type() {
let buf = construct_valid_frame("FIX.4.4", "34=1|");
let len = buf.len();
assert_garbled(scan(&buf), GarbledReason::MissingMsgType, len);
}
#[test]
fn missing_msg_type_on_empty_body() {
let buf = construct_valid_frame("FIX.4.4", "");
let len = buf.len();
assert_garbled(scan(&buf), GarbledReason::MissingMsgType, len);
}
#[test]
fn empty_first_field() {
let buf = construct_valid_frame("FIX.4.4", "|35=A|");
let len = buf.len();
assert_garbled(scan(&buf), GarbledReason::MissingMsgType, len);
}
#[test]
fn skip_does_not_resync_into_body() {
let mut buf = construct_valid_frame("FIX.4.4", "35=A|58=8=FIX.4.4|");
let len = buf.len();
buf[len - 2] = b'x';
assert_garbled(scan(&buf), GarbledReason::MalformedChecksum, len);
}
}
mod happy_path {
use super::*;
#[test]
fn valid_frame_scans() {
let buf = construct_valid_frame("FIX.4.4", "35=A|34=1|49=ME|56=YOU|");
assert!(matches!(scan(&buf), Outcome::Frame(_)));
}
}
mod preamble {
use super::*;
#[test]
fn msg_type() {
let buf = construct_valid_frame("FIX.4.4", "35=D|");
assert_eq!(frame(&buf).msg_type, b"D".as_slice());
}
#[test]
fn begin_string_and_body_length() {
let buf = construct_valid_frame("FIX.4.4", "35=A|");
let h = frame(&buf);
assert_eq!(h.begin_string, BeginString::Fix44);
assert_eq!(h.body_length, 5); }
#[test]
fn unknown_begin_string_is_other() {
let buf = construct_valid_frame("FIX.5.0", "35=A|");
assert_eq!(frame(&buf).begin_string, BeginString::Other(b"FIX.5.0"));
}
#[test]
fn fixt_begin_string() {
let buf = construct_valid_frame("FIXT.1.1", "35=A|");
assert_eq!(frame(&buf).begin_string, BeginString::Fixt11);
}
#[test]
fn msg_type_read_regardless_of_trailing_fields() {
let buf = construct_valid_frame("FIX.4.4", "35=A|34=42|49=ME|56=YOU|");
assert_eq!(frame(&buf).msg_type, b"A".as_slice());
}
}
mod truncation {
use super::*;
#[test]
fn every_prefix_of_a_valid_frame_is_incomplete() {
let frames = [
construct_valid_frame("FIX.4.4", "35=A|"),
construct_valid_frame("FIX.4.4", "35=D|34=2|49=SENDER|56=TARGET|"),
construct_valid_frame("FIXT.1.1", "35=A|1128=9|"),
construct_valid_frame("FIX.4.2", "35=0|"),
];
for full in &frames {
for len in 0..full.len() {
assert!(
matches!(scan(&full[..len]), Outcome::Incomplete),
"prefix of length {len} (of a {}-byte frame) should be Incomplete",
full.len(),
);
}
}
}
}
mod chunking {
use super::*;
fn drive(stream: &[u8], chunk_size: usize) -> Vec<Vec<u8>> {
let scanner = Scanner::default();
let mut frames = Vec::new();
let mut buf: Vec<u8> = Vec::new();
for chunk in stream.chunks(chunk_size) {
buf.extend_from_slice(chunk);
loop {
let advance = match scanner.scan(&buf) {
Outcome::Frame(f) => {
frames.push(f.bytes.to_vec());
f.bytes.len()
}
Outcome::Garbled { skipped, .. } => skipped,
Outcome::Incomplete => break,
};
buf.drain(..advance);
}
}
frames
}
#[test]
fn clean_stream_is_chunk_invariant() {
let f1 = construct_valid_frame("FIX.4.4", "35=A|34=1|");
let f2 = construct_valid_frame("FIXT.1.1", "35=D|1128=9|");
let mut stream = f1.clone();
stream.extend_from_slice(&f2);
let whole = drive(&stream, stream.len());
assert_eq!(whole, vec![f1, f2]);
for chunk_size in 1..=stream.len() {
assert_eq!(
drive(&stream, chunk_size),
whole,
"chunk_size = {chunk_size}"
);
}
}
#[test]
fn garbage_before_frame_is_chunk_invariant() {
let f = construct_valid_frame("FIX.4.4", "35=0|");
let mut stream = b"YY8=Z".to_vec();
stream.extend_from_slice(&f);
let whole = drive(&stream, stream.len());
assert_eq!(whole, vec![f]);
for chunk_size in 1..=stream.len() {
assert_eq!(
drive(&stream, chunk_size),
whole,
"chunk_size = {chunk_size}"
);
}
}
}
}