use std::fmt;
use std::fs::File;
use std::io::{BufReader, Seek, SeekFrom};
#[cfg(feature = "gzip")]
use std::io::Read;
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use crate::datatype::DataType;
use crate::error::{Error, Result};
use crate::reader::Schema;
use crate::samples::Samples;
use crate::time::JDate;
use crate::DEFAULT_MAX_SAMPLE_BYTES;
pub(crate) const CSS_LINE_LEN: usize = 283;
pub(crate) const NNSA_LINE_LEN: usize = 287;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SegType {
Velocity,
Acceleration,
Displacement,
Counts,
Other,
Unspecified,
Custom(char),
}
impl SegType {
fn parse(s: &str) -> Self {
match s.trim() {
"V" | "v" => SegType::Velocity,
"A" | "a" => SegType::Acceleration,
"D" | "d" => SegType::Displacement,
"n" => SegType::Counts,
"o" => SegType::Other,
"-" | "" => SegType::Unspecified,
other => SegType::Custom(other.chars().next().unwrap_or('-')),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClipFlag {
Clipped,
NotClipped,
Unspecified,
}
impl ClipFlag {
fn parse(s: &str) -> Self {
match s.trim() {
"c" | "C" => ClipFlag::Clipped,
"n" | "N" => ClipFlag::NotClipped,
_ => ClipFlag::Unspecified,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Wfdisc {
pub sta: String,
pub chan: String,
pub time: f64,
pub wfid: i64,
pub chanid: i64,
pub jdate: JDate,
pub endtime: f64,
pub nsamp: i64,
pub samprate: f64,
pub calib: f64,
pub calper: f64,
pub instype: String,
pub segtype: SegType,
pub datatype: DataType,
pub clip: ClipFlag,
pub dir: String,
pub dfile: String,
pub foff: i64,
pub commid: i64,
pub lddate: String,
pub schema: Schema,
}
impl Wfdisc {
pub fn parse(line: &str) -> Result<Self> {
let trimmed = line.trim_end_matches(['\r', '\n']);
match trimmed.len() {
CSS_LINE_LEN => Self::parse_with(trimmed, Schema::Css30),
NNSA_LINE_LEN => Self::parse_with(trimmed, Schema::NnsaKbCore),
_ => Self::parse_loose(trimmed),
}
}
pub fn parse_with(line: &str, schema: Schema) -> Result<Self> {
let positions = match schema.positions() {
Some(p) => p,
None => return Self::parse_loose(line),
};
if line.len() != positions.line_len {
return Err(Error::InvalidLineLength { got: line.len() });
}
let s = positions;
Ok(Wfdisc {
sta: trim_owned(slice(line, s.sta, "sta")?),
chan: trim_owned(slice(line, s.chan, "chan")?),
time: parse_f64(slice(line, s.time, "time")?, "time")?,
wfid: parse_i64(slice(line, s.wfid, "wfid")?, "wfid")?,
chanid: parse_i64(slice(line, s.chanid, "chanid")?, "chanid")?,
jdate: parse_jdate(slice(line, s.jdate, "jdate")?)?,
endtime: parse_f64(slice(line, s.endtime, "endtime")?, "endtime")?,
nsamp: parse_i64(slice(line, s.nsamp, "nsamp")?, "nsamp")?,
samprate: parse_f64(slice(line, s.samprate, "samprate")?, "samprate")?,
calib: parse_f64(slice(line, s.calib, "calib")?, "calib")?,
calper: parse_f64(slice(line, s.calper, "calper")?, "calper")?,
instype: trim_owned(slice(line, s.instype, "instype")?),
segtype: SegType::parse(slice(line, s.segtype, "segtype")?),
datatype: DataType::from_str(slice(line, s.datatype, "datatype")?)?,
clip: ClipFlag::parse(slice(line, s.clip, "clip")?),
dir: trim_owned(slice(line, s.dir, "dir")?),
dfile: trim_owned(slice(line, s.dfile, "dfile")?),
foff: parse_i64(slice(line, s.foff, "foff")?, "foff")?,
commid: parse_i64(slice(line, s.commid, "commid")?, "commid")?,
lddate: trim_owned(slice(line, s.lddate, "lddate")?),
schema,
})
}
pub fn parse_loose(line: &str) -> Result<Self> {
let line = line.trim_end_matches(['\r', '\n']);
let mut tokens: Vec<&str> = line.split_whitespace().collect();
if tokens.len() < 20 {
return Err(Error::InvalidLineLength { got: line.len() });
}
if tokens.len() == 21 {
let last = tokens.pop().unwrap();
let prev = tokens.pop().unwrap();
return build_from_tokens(&tokens, Some(format!("{prev} {last}")));
}
if tokens.len() > 21 {
return Err(Error::InvalidLineLength { got: line.len() });
}
build_from_tokens(&tokens, None)
}
pub fn resolve_path(&self, base: impl AsRef<Path>) -> PathBuf {
let dir = Path::new(&self.dir);
let with_dir = if dir.is_absolute() {
dir.to_path_buf()
} else {
base.as_ref().join(dir)
};
with_dir.join(&self.dfile)
}
pub fn read_samples(&self, base: impl AsRef<Path>) -> Result<Samples> {
self.read_samples_with_limit(base, DEFAULT_MAX_SAMPLE_BYTES)
}
pub fn read_samples_with_limit(
&self,
base: impl AsRef<Path>,
max_bytes: usize,
) -> Result<Samples> {
self.read_samples_inner(base.as_ref(), true, max_bytes)
}
pub fn read_samples_strict(&self, base: impl AsRef<Path>) -> Result<Samples> {
self.read_samples_strict_with_limit(base, DEFAULT_MAX_SAMPLE_BYTES)
}
pub fn read_samples_strict_with_limit(
&self,
base: impl AsRef<Path>,
max_bytes: usize,
) -> Result<Samples> {
self.read_samples_inner(base.as_ref(), false, max_bytes)
}
pub fn read_samples_sandboxed(&self, base: impl AsRef<Path>) -> Result<Samples> {
self.read_samples_sandboxed_with_limit(base, DEFAULT_MAX_SAMPLE_BYTES)
}
pub fn read_samples_sandboxed_with_limit(
&self,
base: impl AsRef<Path>,
max_bytes: usize,
) -> Result<Samples> {
let base = base.as_ref();
let path = self.resolve_path_sandboxed(base)?;
if path.is_file() {
return self.read_samples_from(&path, max_bytes);
}
#[cfg(feature = "gzip")]
{
let gz = with_gz_extension(&path);
if gz.is_file() {
return self.read_samples_from(&gz, max_bytes);
}
Err(Error::WaveformNotFound {
tried: vec![path, gz],
})
}
#[cfg(not(feature = "gzip"))]
Err(Error::WaveformNotFound { tried: vec![path] })
}
pub fn resolve_path_sandboxed(&self, base: impl AsRef<Path>) -> Result<PathBuf> {
let base = base.as_ref();
if Path::new(self.dir.trim()).is_absolute() {
return Err(Error::PathTraversal {
base: base.to_path_buf(),
reason: "absolute dir is not allowed in sandboxed mode",
});
}
if Path::new(self.dfile.trim()).is_absolute() {
return Err(Error::PathTraversal {
base: base.to_path_buf(),
reason: "absolute dfile is not allowed in sandboxed mode",
});
}
if path_has_parent_dir(&self.dir) || path_has_parent_dir(&self.dfile) {
return Err(Error::PathTraversal {
base: base.to_path_buf(),
reason: "path component '..' is not allowed",
});
}
let base_norm = sandbox_base(base)?;
let joined = base_norm
.join(self.dir.trim())
.join(self.dfile.trim());
let normalized = normalize_path(&joined);
if !normalized.starts_with(&base_norm) {
return Err(Error::PathTraversal {
base: base.to_path_buf(),
reason: "resolved path escapes sandbox base",
});
}
Ok(normalized)
}
fn read_samples_inner(
&self,
base: &Path,
lenient: bool,
max_bytes: usize,
) -> Result<Samples> {
let primary = self.resolve_path(base);
let mut tried: Vec<PathBuf> = Vec::new();
if primary.exists() {
return self.read_samples_from(&primary, max_bytes);
}
tried.push(primary.clone());
#[cfg(feature = "gzip")]
{
let gz = with_gz_extension(&primary);
if gz.exists() {
return self.read_samples_from(&gz, max_bytes);
}
tried.push(gz);
}
if lenient {
let fallback = base.join(&self.dfile);
if fallback != primary {
if fallback.exists() {
return self.read_samples_from(&fallback, max_bytes);
}
tried.push(fallback.clone());
#[cfg(feature = "gzip")]
{
let fallback_gz = with_gz_extension(&fallback);
if fallback_gz.exists() {
return self.read_samples_from(&fallback_gz, max_bytes);
}
tried.push(fallback_gz);
}
}
}
Err(Error::WaveformNotFound { tried })
}
fn read_samples_from(&self, path: &Path, max_bytes: usize) -> Result<Samples> {
read_samples_at_with_limit(path, self.foff, self.datatype, self.nsamp, max_bytes)
}
pub fn read_trace_relative_to(&self, base: impl AsRef<Path>) -> Result<Trace> {
let samples = self.read_samples(&base)?;
Ok(Trace {
descriptor: self.clone(),
samples,
})
}
}
pub fn read_samples_at(
path: impl AsRef<Path>,
foff: i64,
datatype: DataType,
nsamp: i64,
) -> Result<Samples> {
read_samples_at_with_limit(path, foff, datatype, nsamp, DEFAULT_MAX_SAMPLE_BYTES)
}
pub fn read_samples_at_with_limit(
path: impl AsRef<Path>,
foff: i64,
datatype: DataType,
nsamp: i64,
max_bytes: usize,
) -> Result<Samples> {
let path = path.as_ref();
let count = validated_nsamp(nsamp)?;
#[cfg(feature = "gzip")]
{
let mut file = File::open(path)?;
if is_gzip_magic(&mut file)? {
let skip_len = validate_foff_for_gzip_skip(foff, max_bytes)?;
file.seek(SeekFrom::Start(0))?;
let mut decoder = flate2::read::MultiGzDecoder::new(BufReader::new(file));
discard_decompressed_bytes(&mut decoder, skip_len)?;
return Samples::decode_with_max_bytes(&mut decoder, datatype, count, max_bytes)
.map_err(|e| map_truncation(e, path, foff, nsamp, datatype));
}
validate_foff_for_seek(foff)?;
let offset = u64::try_from(foff).expect("validated above");
file.seek(SeekFrom::Start(offset))?;
let mut reader = BufReader::new(file);
Samples::decode_with_max_bytes(&mut reader, datatype, count, max_bytes)
.map_err(|e| map_truncation(e, path, foff, nsamp, datatype))
}
#[cfg(not(feature = "gzip"))]
{
validate_foff_for_seek(foff)?;
let mut file = File::open(path)?;
let offset = u64::try_from(foff).expect("validated above");
file.seek(SeekFrom::Start(offset))?;
let mut reader = BufReader::new(file);
Samples::decode_with_max_bytes(&mut reader, datatype, count, max_bytes)
.map_err(|e| map_truncation(e, path, foff, nsamp, datatype))
}
}
impl fmt::Display for Wfdisc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}.{} {} samples @ {} Hz [{}] {}/{}",
self.sta, self.chan, self.nsamp, self.samprate, self.datatype, self.dir, self.dfile,
)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Trace {
pub descriptor: Wfdisc,
pub samples: Samples,
}
impl Trace {
pub fn sta(&self) -> &str {
&self.descriptor.sta
}
pub fn chan(&self) -> &str {
&self.descriptor.chan
}
pub fn start_time(&self) -> f64 {
self.descriptor.time
}
}
#[inline]
fn slice<'a>(line: &'a str, range: (usize, usize), field: &'static str) -> Result<&'a str> {
let (a, b) = range;
let bytes = line.as_bytes();
let end = b.min(bytes.len());
let start = a.min(end);
std::str::from_utf8(&bytes[start..end]).map_err(|_| Error::InvalidUtf8 {
field,
start,
end,
})
}
#[inline]
fn trim_owned(s: &str) -> String {
s.trim().to_string()
}
fn path_has_parent_dir(path: &str) -> bool {
Path::new(path.trim())
.components()
.any(|c| matches!(c, Component::ParentDir))
}
fn sandbox_base(base: &Path) -> Result<PathBuf> {
let resolved = if base.is_absolute() {
base.to_path_buf()
} else {
std::env::current_dir()
.map_err(Error::from)?
.join(base)
};
Ok(normalize_path(&resolved))
}
fn normalize_path(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
Component::ParentDir => {
out.pop();
}
Component::CurDir => {}
Component::Normal(c) => out.push(c),
Component::RootDir => {
out.push(component.as_os_str());
}
Component::Prefix(p) => {
out.push(p.as_os_str());
}
}
}
out
}
fn parse_f64(raw: &str, field: &'static str) -> Result<f64> {
let t = raw.trim();
if t.is_empty() {
return Err(Error::ParseField {
field,
value: raw.to_string(),
source: "empty field".into(),
});
}
t.parse::<f64>().map_err(|e| Error::ParseField {
field,
value: t.to_string(),
source: Box::new(e),
})
}
fn parse_i64(raw: &str, field: &'static str) -> Result<i64> {
let t = raw.trim();
if t.is_empty() {
return Err(Error::ParseField {
field,
value: raw.to_string(),
source: "empty field".into(),
});
}
if let Ok(v) = t.parse::<i64>() {
return Ok(v);
}
if let Some(int_str) = integral_decimal_string(t) {
let v = int_str.parse::<i128>().map_err(|e| Error::ParseField {
field,
value: t.to_string(),
source: Box::new(e),
})?;
return i128_to_i64(v, field, t);
}
Err(Error::ParseField {
field,
value: t.to_string(),
source: "expected integer or integer with .0 fractional suffix".into(),
})
}
fn integral_decimal_string(t: &str) -> Option<&str> {
let (head, tail) = t.split_once('.')?;
if !tail.chars().all(|c| c == '0') {
return None;
}
if head.is_empty() || head == "+" || head == "-" {
return None;
}
Some(head)
}
fn i128_to_i64(v: i128, field: &'static str, raw: &str) -> Result<i64> {
i64::try_from(v).map_err(|_| Error::ParseField {
field,
value: raw.to_string(),
source: "out of i64 range".into(),
})
}
fn parse_jdate(raw: &str) -> Result<JDate> {
let v = parse_i64(raw, "jdate")?;
let day = i32::try_from(v).map_err(|_| Error::ParseField {
field: "jdate",
value: raw.to_string(),
source: "out of i32 range".into(),
})?;
Ok(JDate(day))
}
fn build_from_tokens(tokens: &[&str], lddate_override: Option<String>) -> Result<Wfdisc> {
let expected = if lddate_override.is_some() { 19 } else { 20 };
if tokens.len() != expected {
return Err(Error::InvalidLineLength {
got: tokens.len(),
});
}
let lddate = lddate_override.unwrap_or_else(|| tokens[19].to_string());
Ok(Wfdisc {
sta: tokens[0].to_string(),
chan: tokens[1].to_string(),
time: parse_f64(tokens[2], "time")?,
wfid: parse_i64(tokens[3], "wfid")?,
chanid: parse_i64(tokens[4], "chanid")?,
jdate: parse_jdate(tokens[5])?,
endtime: parse_f64(tokens[6], "endtime")?,
nsamp: parse_i64(tokens[7], "nsamp")?,
samprate: parse_f64(tokens[8], "samprate")?,
calib: parse_f64(tokens[9], "calib")?,
calper: parse_f64(tokens[10], "calper")?,
instype: tokens[11].to_string(),
segtype: SegType::parse(tokens[12]),
datatype: DataType::from_str(tokens[13])?,
clip: ClipFlag::parse(tokens[14]),
dir: tokens[15].to_string(),
dfile: tokens[16].to_string(),
foff: parse_i64(tokens[17], "foff")?,
commid: parse_i64(tokens[18], "commid")?,
lddate,
schema: crate::reader::Schema::Loose,
})
}
#[cfg(feature = "gzip")]
fn is_gzip_magic(file: &mut File) -> Result<bool> {
use std::io::Read;
let mut magic = [0u8; 2];
let mut filled = 0;
while filled < magic.len() {
match file.read(&mut magic[filled..])? {
0 => break,
n => filled += n,
}
}
Ok(filled == magic.len() && magic == [0x1f, 0x8b])
}
#[cfg(feature = "gzip")]
fn with_gz_extension(path: &Path) -> PathBuf {
let mut name = path
.file_name()
.map(|s| s.to_os_string())
.unwrap_or_default();
name.push(".gz");
path.with_file_name(name)
}
fn validate_foff_for_seek(foff: i64) -> Result<()> {
if foff < 0 {
return Err(Error::InvalidFieldValue {
field: "foff",
reason: "must be non-negative",
});
}
usize::try_from(foff).map_err(|_| Error::InvalidFieldValue {
field: "foff",
reason: "exceeds platform usize",
})?;
Ok(())
}
#[cfg(feature = "gzip")]
fn validate_foff_for_gzip_skip(foff: i64, max_bytes: usize) -> Result<usize> {
validate_foff_for_seek(foff)?;
let skip = usize::try_from(foff).expect("validated above");
if skip > max_bytes {
return Err(Error::SampleSizeExceeded {
requested: skip,
max: max_bytes,
});
}
Ok(skip)
}
#[cfg(feature = "gzip")]
fn discard_decompressed_bytes<R: Read>(reader: &mut R, nbytes: usize) -> Result<()> {
const CHUNK: usize = 64 * 1024;
let mut buf = [0u8; CHUNK];
let mut remaining = nbytes;
while remaining > 0 {
let n = remaining.min(CHUNK);
reader.read_exact(&mut buf[..n])?;
remaining -= n;
}
Ok(())
}
fn validated_nsamp(nsamp: i64) -> Result<usize> {
if nsamp < 0 {
return Err(Error::InvalidFieldValue {
field: "nsamp",
reason: "must be non-negative",
});
}
usize::try_from(nsamp).map_err(|_| Error::InvalidFieldValue {
field: "nsamp",
reason: "exceeds platform usize",
})
}
fn map_truncation(
e: Error,
path: &Path,
foff: i64,
nsamp: i64,
dtype: DataType,
) -> Error {
match e {
Error::Io(io_err) if io_err.kind() == std::io::ErrorKind::UnexpectedEof => {
let bps = dtype.bytes_per_sample().unwrap_or(0);
let needed = bps.saturating_mul(usize::try_from(nsamp.max(0)).unwrap_or(0));
Error::TruncatedWaveform {
path: path.to_path_buf(),
offset: u64::try_from(foff.max(0)).unwrap_or(0),
needed,
got: 0,
}
}
other => other,
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
const CSS_LINE: &str = include_str!("../tests/data/test_css.wfdisc");
#[test]
fn parse_first_css_row() {
let line = CSS_LINE.lines().next().unwrap();
let row = Wfdisc::parse(line).unwrap();
assert_eq!(row.sta, "TESTbe");
assert_eq!(row.chan, "HHZ");
assert_relative_eq!(row.time, 1_296_474_900.0);
assert_eq!(row.wfid, 1);
assert_eq!(row.chanid, 1);
assert_eq!(row.jdate, JDate(2_011_031));
assert_relative_eq!(row.endtime, 1_296_474_959.988);
assert_eq!(row.nsamp, 4800);
assert_relative_eq!(row.samprate, 80.0);
assert_relative_eq!(row.calib, 1.0);
assert_relative_eq!(row.calper, 1.0);
assert_eq!(row.instype, "3ESPC");
assert_eq!(row.segtype, SegType::Unspecified);
assert_eq!(row.datatype, DataType::S4);
assert_eq!(row.clip, ClipFlag::Unspecified);
assert_eq!(row.dir, "./");
assert_eq!(row.dfile, "201101311155.10.be.w");
assert_eq!(row.foff, 0);
assert_eq!(row.commid, 0);
assert_eq!(row.lddate, "2011/01/31");
assert_eq!(row.schema, Schema::Css30);
}
#[test]
fn parse_all_six_css_rows() {
let rows: Vec<_> = CSS_LINE
.lines()
.map(|l| Wfdisc::parse(l).unwrap())
.collect();
assert_eq!(rows.len(), 6);
let stations: Vec<_> = rows.iter().map(|r| r.sta.as_str()).collect();
assert_eq!(
stations,
vec!["TESTbe", "TESTbe", "TESTbe", "TESTle", "TESTle", "TESTle"]
);
let chans: Vec<_> = rows.iter().map(|r| r.chan.as_str()).collect();
assert_eq!(chans, vec!["HHZ", "HHE", "HHN", "HHZ", "HHE", "HHN"]);
let dtypes: Vec<_> = rows.iter().map(|r| r.datatype).collect();
assert_eq!(
dtypes,
vec![
DataType::S4,
DataType::S4,
DataType::S4,
DataType::I4,
DataType::I4,
DataType::I4,
]
);
let foffs: Vec<_> = rows.iter().map(|r| r.foff).collect();
assert_eq!(foffs, vec![0, 19200, 38400, 0, 19200, 38400]);
}
const NNSA_LINE: &str = include_str!("../tests/data/test_nnsa.wfdisc");
#[test]
fn parse_first_nnsa_row() {
let line = NNSA_LINE.lines().next().unwrap();
let row = Wfdisc::parse(line).unwrap();
assert_eq!(row.schema, Schema::NnsaKbCore);
assert_eq!(row.sta, "TESTbe");
assert_eq!(row.chan, "HHZ");
assert_relative_eq!(row.time, 1_296_474_900.0);
assert_eq!(row.wfid, 1);
assert_eq!(row.chanid, 1);
assert_eq!(row.jdate, JDate(2_011_031));
assert_eq!(row.nsamp, 4800);
assert_relative_eq!(row.samprate, 80.0);
assert_eq!(row.datatype, DataType::S4);
assert_eq!(row.dfile, "201101311155.10.be.w");
assert_eq!(row.foff, 0);
}
#[test]
fn rejects_wrong_length() {
let err = Wfdisc::parse("too short").unwrap_err();
assert!(matches!(err, Error::InvalidLineLength { got: 9 }));
}
#[test]
fn parses_int_field_written_as_float() {
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
let nsamp_field = b" 4800.0";
bytes[79..87].copy_from_slice(nsamp_field);
let row = Wfdisc::parse(&line).unwrap();
assert_eq!(row.nsamp, 4800);
}
#[test]
fn unknown_datatype_surfaces_error() {
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
bytes[143..145].copy_from_slice(b"xx");
let err = Wfdisc::parse(&line).unwrap_err();
assert!(matches!(err, Error::UnknownDatatype { .. }));
}
#[test]
fn resolve_path_joins_base_dir_dfile() {
let line = CSS_LINE.lines().next().unwrap();
let row = Wfdisc::parse(line).unwrap();
let path = row.resolve_path("/data/run1");
assert_eq!(path, PathBuf::from("/data/run1/./201101311155.10.be.w"));
}
#[test]
fn resolve_path_respects_absolute_dir() {
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
let mut new_dir = b"/abs/path/".to_vec();
new_dir.resize(64, b' ');
bytes[148..212].copy_from_slice(&new_dir);
let row = Wfdisc::parse(&line).unwrap();
let path = row.resolve_path("/should/be/ignored");
assert_eq!(path, PathBuf::from("/abs/path/201101311155.10.be.w"));
}
#[test]
fn read_samples_falls_back_to_basename_in_base_dir() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!(
"wfdisc_fallback_{}",
std::process::id()
));
std::fs::create_dir_all(&tmp).unwrap();
let dfile_name = "fallback.w";
let mut f = std::fs::File::create(tmp.join(dfile_name)).unwrap();
f.write_all(&[0, 0, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap();
drop(f);
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
let mut new_dir = b"/nonexistent/dir/".to_vec();
new_dir.resize(64, b' ');
bytes[148..212].copy_from_slice(&new_dir);
let mut new_dfile = dfile_name.as_bytes().to_vec();
new_dfile.resize(32, b' ');
bytes[213..245].copy_from_slice(&new_dfile);
bytes[79..87].copy_from_slice(b" 2"); let row = Wfdisc::parse(&line).unwrap();
let samples = row.read_samples(&tmp).unwrap();
assert_eq!(samples.as_i32(), Some(&[1_i32, -1][..]));
let err = row.read_samples_strict(&tmp).unwrap_err();
match err {
Error::WaveformNotFound { tried } => {
let canonical = PathBuf::from("/nonexistent/dir/").join(dfile_name);
assert!(tried.contains(&canonical), "canonical path missing from {:?}", tried);
assert!(
!tried.contains(&tmp.join(dfile_name)),
"strict mode must not have tried the basename fallback in {:?}",
tried
);
}
other => panic!("expected WaveformNotFound, got {other:?}"),
}
std::fs::remove_dir_all(&tmp).ok();
}
#[test]
fn read_samples_strict_returns_not_found_when_only_fallback_exists() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!(
"wfdisc_fallback_strict_{}",
std::process::id()
));
std::fs::create_dir_all(&tmp).unwrap();
let dfile_name = "strict_only.w";
let mut f = std::fs::File::create(tmp.join(dfile_name)).unwrap();
f.write_all(&[0, 0, 0, 7]).unwrap();
drop(f);
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
let mut new_dir = b"/nonexistent/dir/".to_vec();
new_dir.resize(64, b' ');
bytes[148..212].copy_from_slice(&new_dir);
let mut new_dfile = dfile_name.as_bytes().to_vec();
new_dfile.resize(32, b' ');
bytes[213..245].copy_from_slice(&new_dfile);
bytes[79..87].copy_from_slice(b" 1");
let row = Wfdisc::parse(&line).unwrap();
assert!(matches!(
row.read_samples_strict(&tmp).unwrap_err(),
Error::WaveformNotFound { .. }
));
assert!(row.read_samples(&tmp).is_ok());
std::fs::remove_dir_all(&tmp).ok();
}
#[test]
fn read_samples_skips_fallback_when_canonical_equals_basename_fallback() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!(
"wfdisc_fallback_eq_{}",
std::process::id()
));
std::fs::create_dir_all(&tmp).unwrap();
let dfile_name = "same.w";
let mut f = std::fs::File::create(tmp.join(dfile_name)).unwrap();
f.write_all(&[0, 0, 0, 0, 0, 0, 0, 1]).unwrap();
drop(f);
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dir = "./".into();
row.dfile = dfile_name.into();
row.nsamp = 2;
let s = row.read_samples(&tmp).unwrap();
assert_eq!(s.as_i32(), Some(&[0_i32, 1][..]));
std::fs::remove_dir_all(&tmp).ok();
}
#[test]
fn sandboxed_rejects_absolute_dir() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dir = "/abs/data".into();
let err = row
.read_samples_sandboxed(Path::new("tests/data"))
.unwrap_err();
assert!(matches!(err, Error::PathTraversal { reason, .. } if reason.contains("absolute")));
}
#[test]
fn sandboxed_rejects_parent_dir_in_dir() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dir = "../escape".into();
let err = row
.read_samples_sandboxed(Path::new("tests/data"))
.unwrap_err();
assert!(matches!(err, Error::PathTraversal { reason, .. } if reason.contains("..")));
}
#[test]
fn sandboxed_rejects_parent_dir_in_dfile() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dfile = "../secret.w".into();
let err = row
.read_samples_sandboxed(Path::new("tests/data"))
.unwrap_err();
assert!(matches!(err, Error::PathTraversal { reason, .. } if reason.contains("..")));
}
#[test]
fn sandboxed_rejects_absolute_dfile() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dir = "rel".into();
row.dfile = "/etc/passwd".into();
let err = row.resolve_path_sandboxed(Path::new("tests/data")).unwrap_err();
assert!(
matches!(err, Error::PathTraversal { reason, .. } if reason.contains("absolute dfile"))
);
}
#[test]
fn sandboxed_rejects_absolute_dfile_with_relative_base() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.dir = ".".into();
row.dfile = "/etc/passwd".into();
let err = row.resolve_path_sandboxed(Path::new(".")).unwrap_err();
assert!(
matches!(err, Error::PathTraversal { reason, .. } if reason.contains("absolute dfile"))
);
}
#[test]
fn sandboxed_reads_file_under_base() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!("wfdisc_sandbox_{}", std::process::id()));
let safe = tmp.join("safe");
std::fs::create_dir_all(&safe).unwrap();
let mut f = std::fs::File::create(safe.join("trace.w")).unwrap();
f.write_all(&[0, 0, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap();
drop(f);
let mut row = Wfdisc::parse(CSS_LINE.lines().next().unwrap()).unwrap();
row.dir = "safe".into();
row.dfile = "trace.w".into();
row.nsamp = 2;
row.foff = 0;
let samples = row.read_samples_sandboxed(&tmp).unwrap();
assert_eq!(samples.as_i32(), Some(&[1_i32, -1][..]));
std::fs::remove_dir_all(&tmp).ok();
}
#[test]
fn parse_i64_accepts_only_exact_integer_forms() {
for (value, expected) in [("4800", 4800), ("4800.0", 4800), ("4800.00", 4800)] {
assert_eq!(parse_i64(value, "nsamp").unwrap(), expected);
}
}
#[test]
fn parse_i64_rejects_non_integer_field_values() {
for (value, msg) in [
("NaN", "expected integer"),
("inf", "expected integer"),
("-inf", "expected integer"),
("1.5", "expected integer"),
("1e3", "expected integer"),
("1e30", "expected integer"),
("9223372036854775808e0", "expected integer"),
("9223372036854775808.0", "out of i64"),
] {
let err = parse_i64(value, "wfid").unwrap_err();
assert!(
matches!(err, Error::ParseField { field: "wfid", .. }),
"value {value}: {err:?}"
);
if let Error::ParseField { source, .. } = err {
assert!(
source.to_string().contains(msg),
"value {value}: {}",
source
);
}
}
}
#[test]
fn invalid_utf8_at_column_boundary_returns_error() {
let mut line: String = CSS_LINE.lines().next().unwrap().into();
let bytes = unsafe { line.as_bytes_mut() };
bytes[5] = 0xC3;
bytes[6] = 0xA9;
let err = Wfdisc::parse(&line).unwrap_err();
assert!(matches!(
err,
Error::InvalidUtf8 {
field: "sta",
start: 0,
end: 6
}
));
}
#[test]
fn jdate_rejects_out_of_i32_range() {
let overflow = (i64::from(i32::MAX) + 1).to_string();
let err = parse_jdate(&overflow).unwrap_err();
assert!(matches!(err, Error::ParseField { field: "jdate", .. }));
}
#[test]
fn loose_line_with_spaced_lddate_has_21_tokens() {
let line = "TEST01 BHZ 1000000000.0 1 1 2011031 1000000009.99 1000 80.0 1.0 1.0 3ESPC - s4 - ./ sample.w 0 0 2011/01/31 12:34:56\n";
let row = Wfdisc::parse_loose(line).unwrap();
assert_eq!(row.schema, Schema::Loose);
assert_eq!(row.lddate, "2011/01/31 12:34:56");
assert_eq!(row.nsamp, 1000);
}
#[test]
fn negative_nsamp_rejected_on_read() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.nsamp = -1;
let base = Path::new("tests/data");
let err = row.read_samples(base).unwrap_err();
assert!(matches!(
err,
Error::InvalidFieldValue {
field: "nsamp",
reason: "must be non-negative"
}
));
}
#[test]
fn plain_file_large_foff_seek_not_capped_by_max_sample_bytes() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!("wfdisc_big_foff_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
std::fs::File::create(tmp.join("tiny.w"))
.unwrap()
.write_all(&[0u8; 4])
.unwrap();
let mut row = Wfdisc::parse(CSS_LINE.lines().next().unwrap()).unwrap();
row.dir = ".".into();
row.dfile = "tiny.w".into();
row.foff = 1 << 30;
row.nsamp = 0;
row.read_samples_with_limit(&tmp, DEFAULT_MAX_SAMPLE_BYTES)
.expect("plain seek past EOF with nsamp=0 should not hit foff byte cap");
std::fs::remove_dir_all(&tmp).ok();
}
#[cfg(feature = "gzip")]
#[test]
fn gzip_large_foff_skip_rejected_by_cap() {
use std::io::Write;
use flate2::write::GzEncoder;
use flate2::Compression;
let tmp = std::env::temp_dir().join(format!("wfdisc_gz_cap_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
let dfile = "cap.w";
let mut enc = GzEncoder::new(Vec::new(), Compression::fast());
enc.write_all(&[0u8; 8]).unwrap();
std::fs::write(tmp.join(format!("{dfile}.gz")), enc.finish().unwrap()).unwrap();
let mut row = Wfdisc::parse(CSS_LINE.lines().next().unwrap()).unwrap();
row.dir = ".".into();
row.dfile = dfile.into();
row.foff = 1 << 30;
row.nsamp = 0;
let err = row
.read_samples_with_limit(&tmp, DEFAULT_MAX_SAMPLE_BYTES)
.unwrap_err();
assert!(matches!(
err,
Error::SampleSizeExceeded {
requested,
max,
} if requested == (1 << 30) && max == DEFAULT_MAX_SAMPLE_BYTES
));
std::fs::remove_dir_all(&tmp).ok();
}
#[cfg(feature = "gzip")]
#[test]
fn gzip_skip_streams_past_foff_without_allocating_skip_buffer() {
use std::io::Write;
use flate2::write::GzEncoder;
use flate2::Compression;
let tmp = std::env::temp_dir().join(format!("wfdisc_gz_skip_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
let dfile = "skip_tiny.w";
let mut enc = GzEncoder::new(Vec::new(), Compression::fast());
enc.write_all(&[0u8; 8]).unwrap();
std::fs::write(tmp.join(format!("{dfile}.gz")), enc.finish().unwrap()).unwrap();
let mut row = Wfdisc::parse(CSS_LINE.lines().next().unwrap()).unwrap();
row.dir = ".".into();
row.dfile = dfile.into();
row.foff = 4096;
row.nsamp = 0;
let err = row
.read_samples_with_limit(&tmp, DEFAULT_MAX_SAMPLE_BYTES)
.unwrap_err();
assert!(
matches!(err, Error::Io(_) | Error::TruncatedWaveform { .. }),
"expected short read while skipping, got {err:?}"
);
std::fs::remove_dir_all(&tmp).ok();
}
#[test]
fn negative_foff_rejected_on_read() {
let line = CSS_LINE.lines().next().unwrap();
let mut row = Wfdisc::parse(line).unwrap();
row.foff = -1;
let base = Path::new("tests/data");
let err = row.read_samples(base).unwrap_err();
assert!(matches!(
err,
Error::InvalidFieldValue {
field: "foff",
reason: "must be non-negative"
}
));
}
#[test]
fn segtype_and_clip_parse_known_codes() {
assert_eq!(SegType::parse("V"), SegType::Velocity);
assert_eq!(SegType::parse("a"), SegType::Acceleration);
assert_eq!(SegType::parse("D"), SegType::Displacement);
assert_eq!(SegType::parse("n"), SegType::Counts);
assert_eq!(SegType::parse("o"), SegType::Other);
assert_eq!(SegType::parse("-"), SegType::Unspecified);
assert_eq!(SegType::parse(" Z "), SegType::Custom('Z'));
assert_eq!(ClipFlag::parse("c"), ClipFlag::Clipped);
assert_eq!(ClipFlag::parse("n"), ClipFlag::NotClipped);
assert_eq!(ClipFlag::parse("-"), ClipFlag::Unspecified);
}
#[test]
fn read_samples_at_seeks_plain_file_by_foff() {
use std::io::Write;
let tmp = std::env::temp_dir().join(format!("wfdisc_at_plain_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
let path = tmp.join("at_plain.w");
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(&[0xAA, 0xBB, 0xCC, 0xDD, 0, 0, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF])
.unwrap();
drop(f);
let s = read_samples_at(&path, 4, DataType::S4, 2).unwrap();
assert_eq!(s.as_i32(), Some(&[1_i32, -1][..]));
std::fs::remove_dir_all(&tmp).ok();
}
#[cfg(feature = "gzip")]
#[test]
fn read_samples_at_detects_gzip_by_magic_regardless_of_extension() {
use std::io::Write;
use flate2::write::GzEncoder;
use flate2::Compression;
let tmp = std::env::temp_dir().join(format!("wfdisc_at_magic_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
let path = tmp.join("misnamed.w");
let mut enc = GzEncoder::new(Vec::new(), Compression::fast());
enc.write_all(&[0xAA, 0xBB, 0xCC, 0xDD, 0, 0, 0, 7]).unwrap();
std::fs::write(&path, enc.finish().unwrap()).unwrap();
let s = read_samples_at(&path, 4, DataType::S4, 1).unwrap();
assert_eq!(s.as_i32(), Some(&[7_i32][..]));
std::fs::remove_dir_all(&tmp).ok();
}
#[cfg(feature = "gzip")]
#[test]
fn read_samples_at_reads_across_concatenated_gzip_members() {
use std::io::Write;
use flate2::write::GzEncoder;
use flate2::Compression;
let tmp = std::env::temp_dir().join(format!("wfdisc_at_multi_{}", std::process::id()));
std::fs::create_dir_all(&tmp).unwrap();
let path = tmp.join("multi.w.gz");
let mut m1 = GzEncoder::new(Vec::new(), Compression::fast());
m1.write_all(&[0, 0, 0, 1]).unwrap();
let mut bytes = m1.finish().unwrap();
let mut m2 = GzEncoder::new(Vec::new(), Compression::fast());
m2.write_all(&[0xFF, 0xFF, 0xFF, 0xFF]).unwrap();
bytes.extend(m2.finish().unwrap());
std::fs::write(&path, bytes).unwrap();
let s = read_samples_at(&path, 0, DataType::S4, 2).unwrap();
assert_eq!(
s.as_i32(),
Some(&[1_i32, -1][..]),
"second sample comes from the second gzip member"
);
std::fs::remove_dir_all(&tmp).ok();
}
}