use super::*;
pub(super) struct Scanned {
pub(super) next: usize,
pub(super) wait: Option<u32>,
pub(super) reference: Option<(usize, usize)>,
pub(super) loop_block: Option<(usize, usize)>,
}
pub(super) fn scan_op(
data: &[u8],
start: usize,
end: usize,
in_restore: bool,
tables: &Tables,
) -> Option<Scanned> {
let s = &data[..end];
let mut pos = start;
let b = *s.get(pos)?;
pos += 1;
let (o, a) = (b >> 5, b & 31);
let mut wait = Some(0);
let mut reference = None;
let mut loop_block = None;
if in_restore {
let ok = o == op::TONE
|| (o == op::EXT
&& matches!(
a,
ext::PARAM
| ext::TEMPO
| ext::TEMPO_FINE
| ext::CHANNEL
| ext::ENVELOPE
| ext::VIBRATO
| ext::DETUNE
| ext::ARP
| ext::WAVE
| ext::DUTY
));
if !ok {
return None;
}
}
match o {
op::NOTE_REL | op::LEGATO_REL => {}
op::NOTE_WAIT => wait = None,
op::WAIT => wait = Some(a as u32 + 1),
op::VOLUME => {
if a > 15 {
return None;
}
}
op::OFF_WAIT => wait = Some(a as u32),
op::TONE => {
if a == 31 {
s.get(pos)?;
pos += 1;
}
}
_ => match a {
ext::NOTE_ABS | ext::LEGATO_ABS => {
if *s.get(pos)? > 127 {
return None;
}
pos += 1;
}
ext::PARAM => {
if read_varint(s, &mut pos)? >= 2 * crate::PARAM_PAGE as u32 {
return None;
}
read_varint(s, &mut pos)?;
}
ext::TEMPO => {
let t = read_varint(s, &mut pos)?;
if t == 0 || t > 999 {
return None;
}
}
ext::TEMPO_FINE => {
let t = read_varint(s, &mut pos)?;
if !(MIN_TEMPO_MILLI..=MAX_TEMPO_MILLI).contains(&t) {
return None;
}
}
ext::WAIT_LONG => {
let w = read_varint(s, &mut pos)?;
if w == 0 {
return None;
}
wait = Some(w);
}
ext::REF => {
let dist = read_varint(s, &mut pos)? as usize;
let len = read_varint(s, &mut pos)? as usize;
let target = start.checked_sub(dist)?;
if len == 0 || target.checked_add(len)? > start {
return None;
}
reference = Some((target, len));
}
ext::LOOP => {
let len = read_varint(s, &mut pos)? as usize;
let block_end = pos.checked_add(len)?;
if block_end > end {
return None;
}
loop_block = Some((pos, block_end));
pos = block_end;
}
ext::CHANNEL => {
if !in_restore {
return None;
}
s.get(pos)?;
pos += 1;
}
ext::ENVELOPE => {
if *s.get(pos)? >= tables.envelopes {
return None;
}
pos += 1;
}
ext::PORTAMENTO => {
let c = i16::from_le_bytes([*s.get(pos)?, *s.get(pos + 1)?]);
if !(-12700..=12700).contains(&c) {
return None;
}
pos += 2;
if read_varint(s, &mut pos)? > u16::MAX as u32 {
return None;
}
}
ext::VIBRATO | ext::DETUNE | ext::ARP => {
let limit = match a {
ext::VIBRATO => tables.vibratos,
ext::DETUNE => tables.detunes,
_ => tables.arps,
};
if *s.get(pos)? > limit {
return None;
}
pos += 1;
}
ext::DUTY => {
if *s.get(pos)? > tables.duties {
return None;
}
pos += 1;
}
ext::WAVE => {
if *s.get(pos)? >= tables.waves {
return None;
}
pos += 1;
}
ext::ESCAPE => {
s.get(pos)?;
return None;
}
_ => return None,
},
}
Some(Scanned {
next: pos,
wait,
reference,
loop_block,
})
}
pub(super) type LoopInfo = (u64, usize, (usize, usize));
pub(super) fn validate_track(
data: &[u8],
track: u8,
range: (usize, usize),
tables: &Tables,
budget: &mut u32,
) -> Result<Option<LoopInfo>, DecodeError> {
let mut stack = [(0usize, 0usize); MAX_REF_DEPTH + 1];
let mut sp = 0;
stack[0] = range;
let mut units: u64 = 0;
let mut last_wait: u32 = 0;
let mut found = None;
loop {
let (pos, end) = stack[sp];
if pos == end {
if sp == 0 {
return Ok(found);
}
sp -= 1;
continue;
}
if *budget == 0 {
return Err(DecodeError::TooComplex);
}
*budget -= 1;
let at = pos as u32;
let sc = scan_op(data, pos, end, false, tables).ok_or(DecodeError::BadOp { track, at })?;
stack[sp].0 = sc.next;
match sc.wait {
None => units += last_wait as u64,
Some(0) => {}
Some(w) => {
units += w as u64;
last_wait = w;
}
}
if let Some((block_start, block_end)) = sc.loop_block {
if sp != 0 || found.is_some() {
return Err(DecodeError::BadLoop { track });
}
let mut p = block_start;
while p < block_end {
let s = scan_op(data, p, block_end, true, tables).ok_or(DecodeError::BadOp {
track,
at: p as u32,
})?;
p = s.next;
}
found = Some((units, sc.next, (block_start, block_end)));
}
if let Some((target, len)) = sc.reference {
if sp + 1 > MAX_REF_DEPTH || target < range.0 {
return Err(DecodeError::BadRef { track, at });
}
sp += 1;
stack[sp] = (target, target + len);
}
}
}