1 use crate::endian::{Endian, Parse, ToBytes};
3 use encoding_rs::mem::decode_latin1;
4 use flate2::read::ZlibDecoder;
7 use std::cmp::Ordering;
8 use std::fmt::{Debug, Formatter, Result as FmtResult};
10 use std::str::from_utf8;
12 collections::VecDeque,
13 io::{Error as IoError, Read, Seek, SeekFrom},
16 use thiserror::Error as ThisError;
18 use self::state::State;
20 #[derive(ThisError, Debug)]
22 #[error("Not an SPSS system file")]
25 #[error("Invalid magic number {0:?}")]
28 #[error("I/O error ({0})")]
31 #[error("Invalid SAV compression code {0}")]
32 InvalidSavCompression(u32),
34 #[error("Invalid ZSAV compression code {0}")]
35 InvalidZsavCompression(u32),
37 #[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
38 BadVariableWidth { offset: u64, width: i32 },
40 #[error("Document record at offset {offset:#x} has document line count ({n}) greater than the maximum number {max}.")]
41 BadDocumentLength { offset: u64, n: usize, max: usize },
43 #[error("At offset {offset:#x}, unrecognized record type {rec_type}.")]
44 BadRecordType { offset: u64, rec_type: u32 },
46 #[error("In variable record starting at offset {start_offset:#x}, variable label code {code} at offset {code_offset:#x} is not 0 or 1.")]
47 BadVariableLabelCode {
54 "At offset {offset:#x}, numeric missing value code ({code}) is not -3, -2, 0, 1, 2, or 3."
56 BadNumericMissingValueCode { offset: u64, code: i32 },
58 #[error("At offset {offset:#x}, string missing value code ({code}) is not 0, 1, 2, or 3.")]
59 BadStringMissingValueCode { offset: u64, code: i32 },
61 #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")]
62 BadNumberOfValueLabels { offset: u64, n: u32, max: u32 },
64 #[error("At offset {offset:#x}, following value label record, found record type {rec_type} instead of expected type 4 for variable index record")]
65 ExpectedVarIndexRecord { offset: u64, rec_type: u32 },
67 #[error("At offset {offset:#x}, number of variables indexes ({n}) is greater than the maximum number ({max}).")]
68 BadNumberOfVarIndexes { offset: u64, n: u32, max: u32 },
70 #[error("At offset {offset:#x}, record type 7 subtype {subtype} is too large with element size {size} and {count} elements.")]
71 ExtensionRecordTooLarge {
78 #[error("Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a {case_len}-byte case.")]
86 "Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a compressed case."
88 EofInCompressedCase { offset: u64, case_ofs: u64 },
90 #[error("Data ends at offset {offset:#x}, {case_ofs} bytes into a compressed case.")]
91 PartialCompressedCase { offset: u64, case_ofs: u64 },
93 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a string was found where a number was expected.")]
94 CompressedNumberExpected { offset: u64, case_ofs: u64 },
96 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a number was found where a string was expected.")]
97 CompressedStringExpected { offset: u64, case_ofs: u64 },
99 #[error("Block count {n_blocks} in ZLIB trailer at offset {offset:#x} differs from expected block count {expected_n_blocks} calculated from trailer length {ztrailer_len}.")]
100 BadZlibTrailerNBlocks {
103 expected_n_blocks: u64,
107 #[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
115 #[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
123 #[error("In long string missing values record starting at offset {record_offset:#x}, value length at offset {offset:#x} is {value_len} instead of the expected 8.")]
124 BadLongMissingValueLength {
130 #[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
131 BadEncodingName { offset: u64 },
133 #[error("Details TBD")]
137 #[derive(Clone, Debug)]
139 Header(HeaderRecord),
140 Variable(VariableRecord),
141 ValueLabel(ValueLabelRecord),
142 Document(DocumentRecord),
143 IntegerInfo(IntegerInfoRecord),
144 FloatInfo(FloatInfoRecord),
145 VariableSets(TextRecord),
146 VarDisplay(VarDisplayRecord),
147 MultipleResponse(MultipleResponseRecord),
148 LongStringValueLabels(LongStringValueLabelRecord),
149 LongStringMissingValues(LongStringMissingValueSet),
150 Encoding(EncodingRecord),
151 NumberOfCases(NumberOfCasesRecord),
152 ProductInfo(TextRecord),
153 LongNames(TextRecord),
154 VeryLongStrings(TextRecord),
155 FileAttributes(TextRecord),
156 VariableAttributes(TextRecord),
157 OtherExtension(Extension),
165 fn read<R: Read + Seek>(reader: &mut R, endian: Endian) -> Result<Record, Error> {
166 let rec_type: u32 = endian.parse(read_bytes(reader)?);
168 2 => Ok(Record::Variable(VariableRecord::read(reader, endian)?)),
169 3 => Ok(Record::ValueLabel(ValueLabelRecord::read(reader, endian)?)),
170 6 => Ok(Record::Document(DocumentRecord::read(reader, endian)?)),
171 7 => Ok(Extension::read(reader, endian)?),
172 999 => Ok(Record::EndOfHeaders(endian.parse(read_bytes(reader)?))),
173 _ => Err(Error::BadRecordType {
174 offset: reader.stream_position()?,
181 // If `s` is valid UTF-8, returns it decoded as UTF-8, otherwise returns it
182 // decoded as Latin-1 (actually bytes interpreted as Unicode code points).
183 fn default_decode(s: &[u8]) -> Cow<str> {
184 from_utf8(s).map_or_else(|_| decode_latin1(s), Cow::from)
187 #[derive(Copy, Clone, Debug)]
188 pub enum Compression {
194 fn offsets(&self) -> Range<u64>;
198 pub struct HeaderRecord {
200 pub offsets: Range<u64>,
205 /// Eye-catcher string, product name, in the file's encoding. Padded
206 /// on the right with spaces.
207 pub eye_catcher: UnencodedStr<60>,
209 /// Layout code, normally either 2 or 3.
210 pub layout_code: u32,
212 /// Number of variable positions, or `None` if the value in the file is
213 /// questionably trustworthy.
214 pub nominal_case_size: Option<u32>,
216 /// Compression type, if any,
217 pub compression: Option<Compression>,
219 /// 1-based variable index of the weight variable, or `None` if the file is
221 pub weight_index: Option<u32>,
223 /// Claimed number of cases, if known.
224 pub n_cases: Option<u32>,
226 /// Compression bias, usually 100.0.
229 /// `dd mmm yy` in the file's encoding.
230 pub creation_date: UnencodedStr<9>,
232 /// `HH:MM:SS` in the file's encoding.
233 pub creation_time: UnencodedStr<8>,
235 /// File label, in the file's encoding. Padded on the right with spaces.
236 pub file_label: UnencodedStr<64>,
238 /// Endianness of the data in the file header.
243 fn debug_field<T: Debug>(&self, f: &mut Formatter, name: &str, value: T) -> FmtResult {
244 writeln!(f, "{name:>17}: {:?}", value)
248 impl Debug for HeaderRecord {
249 fn fmt(&self, f: &mut Formatter) -> FmtResult {
250 writeln!(f, "File header record:")?;
251 self.debug_field(f, "Magic", self.magic)?;
252 self.debug_field(f, "Product name", self.eye_catcher)?;
253 self.debug_field(f, "Layout code", self.layout_code)?;
254 self.debug_field(f, "Nominal case size", self.nominal_case_size)?;
255 self.debug_field(f, "Compression", self.compression)?;
256 self.debug_field(f, "Weight index", self.weight_index)?;
257 self.debug_field(f, "Number of cases", self.n_cases)?;
258 self.debug_field(f, "Compression bias", self.bias)?;
259 self.debug_field(f, "Creation date", self.creation_date)?;
260 self.debug_field(f, "Creation time", self.creation_time)?;
261 self.debug_field(f, "File label", self.file_label)?;
262 self.debug_field(f, "Endianness", self.endian)
267 fn read<R: Read + Seek>(r: &mut R) -> Result<HeaderRecord, Error> {
268 let start = r.stream_position()?;
270 let magic: [u8; 4] = read_bytes(r)?;
271 let magic: Magic = magic.try_into().map_err(|_| Error::NotASystemFile)?;
273 let eye_catcher = UnencodedStr::<60>(read_bytes(r)?);
274 let layout_code: [u8; 4] = read_bytes(r)?;
275 let endian = Endian::identify_u32(2, layout_code)
276 .or_else(|| Endian::identify_u32(2, layout_code))
277 .ok_or_else(|| Error::NotASystemFile)?;
278 let layout_code = endian.parse(layout_code);
280 let nominal_case_size: u32 = endian.parse(read_bytes(r)?);
281 let nominal_case_size =
282 (nominal_case_size <= i32::MAX as u32 / 16).then_some(nominal_case_size);
284 let compression_code: u32 = endian.parse(read_bytes(r)?);
285 let compression = match (magic, compression_code) {
286 (Magic::ZSAV, 2) => Some(Compression::ZLib),
287 (Magic::ZSAV, code) => return Err(Error::InvalidZsavCompression(code)),
289 (_, 1) => Some(Compression::Simple),
290 (_, code) => return Err(Error::InvalidSavCompression(code)),
293 let weight_index: u32 = endian.parse(read_bytes(r)?);
294 let weight_index = (weight_index > 0).then_some(weight_index);
296 let n_cases: u32 = endian.parse(read_bytes(r)?);
297 let n_cases = (n_cases < i32::MAX as u32 / 2).then_some(n_cases);
299 let bias: f64 = endian.parse(read_bytes(r)?);
301 let creation_date = UnencodedStr::<9>(read_bytes(r)?);
302 let creation_time = UnencodedStr::<8>(read_bytes(r)?);
303 let file_label = UnencodedStr::<64>(read_bytes(r)?);
304 let _: [u8; 3] = read_bytes(r)?;
307 offsets: start..r.stream_position()?,
324 impl Header for HeaderRecord {
325 fn offsets(&self) -> Range<u64> {
330 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
331 pub struct Magic([u8; 4]);
334 /// Magic number for a regular system file.
335 pub const SAV: Magic = Magic(*b"$FL2");
337 /// Magic number for a system file that contains zlib-compressed data.
338 pub const ZSAV: Magic = Magic(*b"$FL3");
340 /// Magic number for an EBDIC-encoded system file. This is `$FL2` encoded
342 pub const EBCDIC: Magic = Magic([0x5b, 0xc6, 0xd3, 0xf2]);
345 impl Debug for Magic {
346 fn fmt(&self, f: &mut Formatter) -> FmtResult {
347 let s = match *self {
348 Magic::SAV => "$FL2",
349 Magic::ZSAV => "$FL3",
350 Magic::EBCDIC => "($FL2 in EBCDIC)",
351 _ => return write!(f, "{:?}", self.0),
357 impl TryFrom<[u8; 4]> for Magic {
360 fn try_from(value: [u8; 4]) -> Result<Self, Self::Error> {
361 let magic = Magic(value);
363 Magic::SAV | Magic::ZSAV | Magic::EBCDIC => Ok(magic),
364 _ => Err(Error::BadMagic(value)),
369 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
376 fn from_width(width: i32) -> VarType {
378 0 => VarType::Numeric,
379 _ => VarType::String,
386 Compression, Error, HeaderRecord, Record, Value, VarType, VariableRecord, ZHeader,
387 ZTrailer, ZlibDecodeMultiple,
389 use crate::endian::Endian;
391 collections::VecDeque,
396 #[allow(clippy::type_complexity)]
397 fn read(self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error>;
400 struct Start<R: Read + Seek> {
404 pub fn new<R: Read + Seek + 'static>(reader: R) -> Box<dyn State> {
405 Box::new(Start { reader })
408 struct CommonState<R: Read + Seek> {
412 compression: Option<Compression>,
413 var_types: Vec<VarType>,
416 impl<R: Read + Seek + 'static> State for Start<R> {
417 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
418 let header = HeaderRecord::read(&mut self.reader)?;
419 let next_state = Headers(CommonState {
421 endian: header.endian,
423 compression: header.compression,
424 var_types: Vec::new(),
426 Ok(Some((Record::Header(header), Box::new(next_state))))
430 struct Headers<R: Read + Seek>(CommonState<R>);
432 impl<R: Read + Seek + 'static> State for Headers<R> {
433 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
434 let record = Record::read(&mut self.0.reader, self.0.endian)?;
436 Record::Variable(VariableRecord { width, .. }) => {
437 self.0.var_types.push(VarType::from_width(width));
439 Record::EndOfHeaders(_) => {
440 let next_state: Box<dyn State> = match self.0.compression {
441 None => Box::new(Data(self.0)),
442 Some(Compression::Simple) => Box::new(CompressedData::new(self.0)),
443 Some(Compression::ZLib) => Box::new(ZlibHeader(self.0)),
445 return Ok(Some((record, next_state)));
449 Ok(Some((record, self)))
453 struct ZlibHeader<R: Read + Seek>(CommonState<R>);
455 impl<R: Read + Seek + 'static> State for ZlibHeader<R> {
456 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
457 let zheader = ZHeader::read(&mut self.0.reader, self.0.endian)?;
458 Ok(Some((Record::ZHeader(zheader), self)))
462 struct ZlibTrailer<R: Read + Seek>(CommonState<R>, ZHeader);
464 impl<R: Read + Seek + 'static> State for ZlibTrailer<R> {
465 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
466 let retval = ZTrailer::read(
469 self.1.ztrailer_offset,
472 let next_state = Box::new(CompressedData::new(CommonState {
473 reader: ZlibDecodeMultiple::new(self.0.reader),
474 endian: self.0.endian,
476 compression: self.0.compression,
477 var_types: self.0.var_types,
480 None => next_state.read(),
481 Some(ztrailer) => Ok(Some((Record::ZTrailer(ztrailer), next_state))),
486 struct Data<R: Read + Seek>(CommonState<R>);
488 impl<R: Read + Seek + 'static> State for Data<R> {
489 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
490 match Value::read_case(&mut self.0.reader, &self.0.var_types, self.0.endian)? {
492 Some(values) => Ok(Some((Record::Case(values), self))),
497 struct CompressedData<R: Read + Seek> {
498 common: CommonState<R>,
502 impl<R: Read + Seek + 'static> CompressedData<R> {
503 fn new(common: CommonState<R>) -> CompressedData<R> {
506 codes: VecDeque::new(),
511 impl<R: Read + Seek + 'static> State for CompressedData<R> {
512 fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
513 match Value::read_compressed_case(
514 &mut self.common.reader,
515 &self.common.var_types,
521 Some(values) => Ok(Some((Record::Case(values), self))),
527 #[derive(Copy, Clone)]
530 String(UnencodedStr<8>),
533 impl Debug for Value {
534 fn fmt(&self, f: &mut Formatter) -> FmtResult {
536 Value::Number(Some(number)) => write!(f, "{number:?}"),
537 Value::Number(None) => write!(f, "SYSMIS"),
538 Value::String(bytes) => write!(f, "{:?}", bytes),
544 fn read<R: Read>(r: &mut R, var_type: VarType, endian: Endian) -> Result<Value, IoError> {
546 UntypedValue(read_bytes(r)?),
552 pub fn from_raw(raw: UntypedValue, var_type: VarType, endian: Endian) -> Value {
554 VarType::String => Value::String(UnencodedStr(raw.0)),
555 VarType::Numeric => {
556 let number: f64 = endian.parse(raw.0);
557 Value::Number((number != -f64::MAX).then_some(number))
562 fn read_case<R: Read + Seek>(
564 var_types: &[VarType],
566 ) -> Result<Option<Vec<Value>>, Error> {
567 let case_start = reader.stream_position()?;
568 let mut values = Vec::with_capacity(var_types.len());
569 for (i, &var_type) in var_types.iter().enumerate() {
570 let Some(raw) = try_read_bytes(reader)? else {
574 let offset = reader.stream_position()?;
575 return Err(Error::EofInCase {
577 case_ofs: offset - case_start,
578 case_len: var_types.len() * 8,
582 values.push(Value::from_raw(UntypedValue(raw), var_type, endian));
587 fn read_compressed_case<R: Read + Seek>(
589 var_types: &[VarType],
590 codes: &mut VecDeque<u8>,
593 ) -> Result<Option<Vec<Value>>, Error> {
594 let case_start = reader.stream_position()?;
595 let mut values = Vec::with_capacity(var_types.len());
596 for (i, &var_type) in var_types.iter().enumerate() {
598 let Some(code) = codes.pop_front() else {
599 let Some(new_codes): Option<[u8; 8]> = try_read_bytes(reader)? else {
603 let offset = reader.stream_position()?;
604 return Err(Error::EofInCompressedCase {
606 case_ofs: offset - case_start,
610 codes.extend(new_codes.into_iter());
615 1..=251 => match var_type {
616 VarType::Numeric => break Value::Number(Some(code as f64 - bias)),
618 break Value::String(UnencodedStr(endian.to_bytes(code as f64 - bias)))
625 let offset = reader.stream_position()?;
626 return Err(Error::PartialCompressedCase {
628 case_ofs: offset - case_start,
633 break Value::from_raw(UntypedValue(read_bytes(reader)?), var_type, endian)
635 254 => match var_type {
636 VarType::String => break Value::String(UnencodedStr(*b" ")), // XXX EBCDIC
637 VarType::Numeric => {
638 return Err(Error::CompressedStringExpected {
640 case_ofs: reader.stream_position()? - case_start,
644 255 => match var_type {
645 VarType::Numeric => break Value::Number(None),
647 return Err(Error::CompressedNumberExpected {
649 case_ofs: reader.stream_position()? - case_start,
661 struct ZlibDecodeMultiple<R>
665 reader: Option<ZlibDecoder<R>>,
668 impl<R> ZlibDecodeMultiple<R>
672 fn new(reader: R) -> ZlibDecodeMultiple<R> {
674 reader: Some(ZlibDecoder::new(reader)),
679 impl<R> Read for ZlibDecodeMultiple<R>
683 fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
685 match self.reader.as_mut().unwrap().read(buf)? {
687 let inner = self.reader.take().unwrap().into_inner();
688 self.reader = Some(ZlibDecoder::new(inner));
696 impl<R> Seek for ZlibDecodeMultiple<R>
700 fn seek(&mut self, pos: SeekFrom) -> Result<u64, IoError> {
701 self.reader.as_mut().unwrap().get_mut().seek(pos)
706 state: Option<Box<dyn State>>,
710 pub fn new<R: Read + Seek + 'static>(reader: R) -> Result<Reader, Error> {
712 state: Some(state::new(reader)),
715 pub fn collect_headers(&mut self) -> Result<Vec<Record>, Error> {
716 let mut headers = Vec::new();
719 Record::EndOfHeaders(_) => break,
720 r => headers.push(r),
727 impl Iterator for Reader {
728 type Item = Result<Record, Error>;
730 fn next(&mut self) -> Option<Self::Item> {
731 match self.state.take()?.read() {
732 Ok(Some((record, next_state))) => {
733 self.state = Some(next_state);
737 Err(error) => Some(Err(error)),
742 impl FusedIterator for Reader {}
744 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
745 pub struct Spec(pub u32);
747 impl Debug for Spec {
748 fn fmt(&self, f: &mut Formatter) -> FmtResult {
749 let type_ = format_name(self.0 >> 16);
750 let w = (self.0 >> 8) & 0xff;
751 let d = self.0 & 0xff;
752 write!(f, "{:06x} ({type_}{w}.{d})", self.0)
756 fn format_name(type_: u32) -> Cow<'static, str> {
795 _ => return format!("<unknown format {type_}>").into(),
801 pub struct MissingValues {
802 /// Individual missing values, up to 3 of them.
803 pub values: Vec<Value>,
805 /// Optional range of missing values.
806 pub range: Option<(Value, Value)>,
809 impl Debug for MissingValues {
810 fn fmt(&self, f: &mut Formatter) -> FmtResult {
811 for (i, value) in self.values.iter().enumerate() {
815 write!(f, "{value:?}")?;
818 if let Some((low, high)) = self.range {
819 if !self.values.is_empty() {
822 write!(f, "{low:?} THRU {high:?}")?;
834 fn is_empty(&self) -> bool {
835 self.values.is_empty() && self.range.is_none()
838 fn read<R: Read + Seek>(
844 ) -> Result<MissingValues, Error> {
845 let (n_values, has_range) = match (width, code) {
846 (_, 0..=3) => (code, false),
847 (0, -2) => (0, true),
848 (0, -3) => (1, true),
849 (0, _) => return Err(Error::BadNumericMissingValueCode { offset, code }),
850 (_, _) => return Err(Error::BadStringMissingValueCode { offset, code }),
853 let var_type = VarType::from_width(width);
855 let mut values = Vec::new();
856 for _ in 0..n_values {
857 values.push(Value::read(r, var_type, endian)?);
859 let range = if has_range {
860 let low = Value::read(r, var_type, endian)?;
861 let high = Value::read(r, var_type, endian)?;
866 Ok(MissingValues { values, range })
871 pub struct VariableRecord {
872 /// Range of offsets in file.
873 pub offsets: Range<u64>,
875 /// Variable width, in the range -1..=255.
878 /// Variable name, padded on the right with spaces.
879 pub name: UnencodedStr<8>,
882 pub print_format: Spec,
885 pub write_format: Spec,
888 pub missing_values: MissingValues,
890 /// Optional variable label.
891 pub label: Option<UnencodedString>,
894 impl Debug for VariableRecord {
895 fn fmt(&self, f: &mut Formatter) -> FmtResult {
900 match self.width.cmp(&0) {
901 Ordering::Greater => "string",
902 Ordering::Equal => "numeric",
903 Ordering::Less => "long string continuation record",
906 writeln!(f, "Print format: {:?}", self.print_format)?;
907 writeln!(f, "Write format: {:?}", self.write_format)?;
908 writeln!(f, "Name: {:?}", &self.name)?;
909 writeln!(f, "Variable label: {:?}", self.label)?;
910 writeln!(f, "Missing values: {:?}", self.missing_values)
914 impl VariableRecord {
915 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<VariableRecord, Error> {
916 let start_offset = r.stream_position()?;
917 let width: i32 = endian.parse(read_bytes(r)?);
918 let code_offset = r.stream_position()?;
919 let has_variable_label: u32 = endian.parse(read_bytes(r)?);
920 let missing_value_code: i32 = endian.parse(read_bytes(r)?);
921 let print_format = Spec(endian.parse(read_bytes(r)?));
922 let write_format = Spec(endian.parse(read_bytes(r)?));
923 let name = UnencodedStr::<8>(read_bytes(r)?);
925 let label = match has_variable_label {
928 let len: u32 = endian.parse(read_bytes(r)?);
929 let read_len = len.min(65535) as usize;
930 let label = UnencodedString(read_vec(r, read_len)?);
932 let padding_bytes = Integer::next_multiple_of(&len, &4) - len;
933 let _ = read_vec(r, padding_bytes as usize)?;
938 return Err(Error::BadVariableLabelCode {
941 code: has_variable_label,
947 MissingValues::read(r, start_offset, width, missing_value_code, endian)?;
949 let end_offset = r.stream_position()?;
952 offsets: start_offset..end_offset,
963 #[derive(Copy, Clone)]
964 pub struct UntypedValue(pub [u8; 8]);
966 impl Debug for UntypedValue {
967 fn fmt(&self, f: &mut Formatter) -> FmtResult {
968 let little: f64 = Endian::Little.parse(self.0);
969 let little = format!("{:?}", little);
970 let big: f64 = Endian::Big.parse(self.0);
971 let big = format!("{:?}", big);
972 let number = if little.len() <= big.len() {
977 write!(f, "{number}")?;
979 let string = default_decode(&self.0);
981 .split(|c: char| c == '\0' || c.is_control())
984 write!(f, "{string:?}")?;
990 pub struct UnencodedString(pub Vec<u8>);
992 impl From<Vec<u8>> for UnencodedString {
993 fn from(source: Vec<u8>) -> Self {
998 impl From<&[u8]> for UnencodedString {
999 fn from(source: &[u8]) -> Self {
1004 impl Debug for UnencodedString {
1005 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1006 write!(f, "{:?}", default_decode(self.0.as_slice()))
1010 #[derive(Copy, Clone)]
1011 pub struct UnencodedStr<const N: usize>(pub [u8; N]);
1013 impl<const N: usize> From<[u8; N]> for UnencodedStr<N> {
1014 fn from(source: [u8; N]) -> Self {
1019 impl<const N: usize> Debug for UnencodedStr<N> {
1020 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1021 write!(f, "{:?}", default_decode(&self.0))
1026 pub struct ValueLabelRecord {
1027 /// Range of offsets in file.
1028 pub offsets: Range<u64>,
1031 pub labels: Vec<(UntypedValue, UnencodedString)>,
1033 /// The 1-based indexes of the variable indexes.
1034 pub dict_indexes: Vec<u32>,
1037 impl Debug for ValueLabelRecord {
1038 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1039 writeln!(f, "labels: ")?;
1040 for (value, label) in self.labels.iter() {
1041 writeln!(f, "{value:?}: {label:?}")?;
1043 write!(f, "apply to variables")?;
1044 for dict_index in self.dict_indexes.iter() {
1045 write!(f, " #{dict_index}")?;
1051 impl Header for ValueLabelRecord {
1052 fn offsets(&self) -> Range<u64> {
1053 self.offsets.clone()
1057 impl ValueLabelRecord {
1058 /// Maximum number of value labels in a record.
1059 pub const MAX_LABELS: u32 = u32::MAX / 8;
1061 /// Maximum number of variable indexes in a record.
1062 pub const MAX_INDEXES: u32 = u32::MAX / 8;
1064 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ValueLabelRecord, Error> {
1065 let label_offset = r.stream_position()?;
1066 let n: u32 = endian.parse(read_bytes(r)?);
1067 if n > Self::MAX_LABELS {
1068 return Err(Error::BadNumberOfValueLabels {
1069 offset: label_offset,
1071 max: Self::MAX_LABELS,
1075 let mut labels = Vec::new();
1077 let value = UntypedValue(read_bytes(r)?);
1078 let label_len: u8 = endian.parse(read_bytes(r)?);
1079 let label_len = label_len as usize;
1080 let padded_len = Integer::next_multiple_of(&(label_len + 1), &8);
1082 let mut label = read_vec(r, padded_len - 1)?;
1083 label.truncate(label_len);
1084 labels.push((value, UnencodedString(label)));
1087 let index_offset = r.stream_position()?;
1088 let rec_type: u32 = endian.parse(read_bytes(r)?);
1090 return Err(Error::ExpectedVarIndexRecord {
1091 offset: index_offset,
1096 let n: u32 = endian.parse(read_bytes(r)?);
1097 if n > Self::MAX_INDEXES {
1098 return Err(Error::BadNumberOfVarIndexes {
1099 offset: index_offset,
1101 max: Self::MAX_INDEXES,
1104 let mut dict_indexes = Vec::with_capacity(n as usize);
1106 dict_indexes.push(endian.parse(read_bytes(r)?));
1109 let end_offset = r.stream_position()?;
1110 Ok(ValueLabelRecord {
1111 offsets: label_offset..end_offset,
1118 #[derive(Clone, Debug)]
1119 pub struct DocumentRecord {
1120 pub offsets: Range<u64>,
1122 /// The document, as an array of 80-byte lines.
1123 pub lines: Vec<DocumentLine>,
1126 pub type DocumentLine = UnencodedStr<{ DocumentRecord::LINE_LEN }>;
1128 impl DocumentRecord {
1129 /// Length of a line in a document. Document lines are fixed-length and
1130 /// padded on the right with spaces.
1131 pub const LINE_LEN: usize = 80;
1133 /// Maximum number of lines we will accept in a document. This is simply
1134 /// the maximum number that will fit in a 32-bit space.
1135 pub const MAX_LINES: usize = i32::MAX as usize / Self::LINE_LEN;
1137 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<DocumentRecord, Error> {
1138 let start_offset = r.stream_position()?;
1139 let n: u32 = endian.parse(read_bytes(r)?);
1141 if n > Self::MAX_LINES {
1142 Err(Error::BadDocumentLength {
1143 offset: start_offset,
1145 max: Self::MAX_LINES,
1148 let mut lines = Vec::with_capacity(n);
1150 lines.push(UnencodedStr::<{ DocumentRecord::LINE_LEN }>(read_bytes(r)?));
1152 let end_offset = r.stream_position()?;
1154 offsets: start_offset..end_offset,
1161 impl Header for DocumentRecord {
1162 fn offsets(&self) -> Range<u64> {
1163 self.offsets.clone()
1167 trait ExtensionRecord {
1169 const SIZE: Option<u32>;
1170 const COUNT: Option<u32>;
1171 const NAME: &'static str;
1172 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error>;
1175 #[derive(Clone, Debug)]
1176 pub struct IntegerInfoRecord {
1177 pub offsets: Range<u64>,
1178 pub version: (i32, i32, i32),
1179 pub machine_code: i32,
1180 pub floating_point_rep: i32,
1181 pub compression_code: i32,
1182 pub endianness: i32,
1183 pub character_code: i32,
1186 impl ExtensionRecord for IntegerInfoRecord {
1187 const SUBTYPE: u32 = 3;
1188 const SIZE: Option<u32> = Some(4);
1189 const COUNT: Option<u32> = Some(8);
1190 const NAME: &'static str = "integer record";
1192 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1193 ext.check_size::<Self>()?;
1195 let mut input = &ext.data[..];
1196 let data: Vec<i32> = (0..8)
1197 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1199 Ok(Record::IntegerInfo(IntegerInfoRecord {
1200 offsets: ext.offsets.clone(),
1201 version: (data[0], data[1], data[2]),
1202 machine_code: data[3],
1203 floating_point_rep: data[4],
1204 compression_code: data[5],
1205 endianness: data[6],
1206 character_code: data[7],
1211 #[derive(Clone, Debug)]
1212 pub struct FloatInfoRecord {
1218 impl ExtensionRecord for FloatInfoRecord {
1219 const SUBTYPE: u32 = 4;
1220 const SIZE: Option<u32> = Some(8);
1221 const COUNT: Option<u32> = Some(3);
1222 const NAME: &'static str = "floating point record";
1224 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1225 ext.check_size::<Self>()?;
1227 let mut input = &ext.data[..];
1228 let data: Vec<f64> = (0..3)
1229 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1231 Ok(Record::FloatInfo(FloatInfoRecord {
1239 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1240 pub enum CategoryLabels {
1245 #[derive(Clone, Debug)]
1246 pub enum MultipleResponseType {
1248 value: UnencodedString,
1249 labels: CategoryLabels,
1254 impl MultipleResponseType {
1255 fn parse(input: &[u8]) -> Result<(MultipleResponseType, &[u8]), Error> {
1256 let (mr_type, input) = match input.first() {
1257 Some(b'C') => (MultipleResponseType::MultipleCategory, &input[1..]),
1259 let (value, input) = parse_counted_string(&input[1..])?;
1261 MultipleResponseType::MultipleDichotomy {
1263 labels: CategoryLabels::VarLabels,
1269 let Some(b' ') = input.get(1) else {
1270 return Err(Error::TBD);
1272 let input = &input[2..];
1273 let (labels, input) = if let Some(rest) = input.strip_prefix(b" 1 ") {
1274 (CategoryLabels::CountedValues, rest)
1275 } else if let Some(rest) = input.strip_prefix(b" 11 ") {
1276 (CategoryLabels::VarLabels, rest)
1278 return Err(Error::TBD);
1280 let (value, input) = parse_counted_string(input)?;
1282 MultipleResponseType::MultipleDichotomy { value, labels },
1286 _ => return Err(Error::TBD),
1288 Ok((mr_type, input))
1292 #[derive(Clone, Debug)]
1293 pub struct MultipleResponseSet {
1294 pub name: UnencodedString,
1295 pub label: UnencodedString,
1296 pub mr_type: MultipleResponseType,
1297 pub short_names: Vec<UnencodedString>,
1300 impl MultipleResponseSet {
1301 fn parse(input: &[u8]) -> Result<(MultipleResponseSet, &[u8]), Error> {
1302 let Some(equals) = input.iter().position(|&b| b == b'=') else {
1303 return Err(Error::TBD);
1305 let (name, input) = input.split_at(equals);
1306 let (mr_type, input) = MultipleResponseType::parse(input)?;
1307 let Some(b' ') = input.first() else {
1308 return Err(Error::TBD);
1310 let (label, mut input) = parse_counted_string(&input[1..])?;
1311 let mut vars = Vec::new();
1312 while input.first() == Some(&b' ') {
1313 input = &input[1..];
1314 let Some(length) = input.iter().position(|b| b" \n".contains(b)) else {
1315 return Err(Error::TBD);
1318 vars.push(input[..length].into());
1320 input = &input[length..];
1322 if input.first() != Some(&b'\n') {
1323 return Err(Error::TBD);
1325 while input.first() == Some(&b'\n') {
1326 input = &input[1..];
1329 MultipleResponseSet {
1340 #[derive(Clone, Debug)]
1341 pub struct MultipleResponseRecord(pub Vec<MultipleResponseSet>);
1343 impl ExtensionRecord for MultipleResponseRecord {
1344 const SUBTYPE: u32 = 7;
1345 const SIZE: Option<u32> = Some(1);
1346 const COUNT: Option<u32> = None;
1347 const NAME: &'static str = "multiple response set record";
1349 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Error> {
1350 ext.check_size::<Self>()?;
1352 let mut input = &ext.data[..];
1353 let mut sets = Vec::new();
1354 while !input.is_empty() {
1355 let (set, rest) = MultipleResponseSet::parse(input)?;
1359 Ok(Record::MultipleResponse(MultipleResponseRecord(sets)))
1363 fn parse_counted_string(input: &[u8]) -> Result<(UnencodedString, &[u8]), Error> {
1364 let Some(space) = input.iter().position(|&b| b == b' ') else {
1365 return Err(Error::TBD);
1367 let Ok(length) = from_utf8(&input[..space]) else {
1368 return Err(Error::TBD);
1370 let Ok(length): Result<usize, _> = length.parse() else {
1371 return Err(Error::TBD);
1374 let input = &input[space + 1..];
1375 if input.len() < length {
1376 return Err(Error::TBD);
1379 let (string, rest) = input.split_at(length);
1380 Ok((string.into(), rest))
1383 #[derive(Clone, Debug)]
1384 pub struct VarDisplayRecord(pub Vec<u32>);
1386 impl ExtensionRecord for VarDisplayRecord {
1387 const SUBTYPE: u32 = 11;
1388 const SIZE: Option<u32> = Some(4);
1389 const COUNT: Option<u32> = None;
1390 const NAME: &'static str = "variable display record";
1392 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1393 ext.check_size::<Self>()?;
1395 let mut input = &ext.data[..];
1396 let display = (0..ext.count)
1397 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1399 Ok(Record::VarDisplay(VarDisplayRecord(display)))
1403 #[derive(Clone, Debug)]
1404 pub struct LongStringMissingValues {
1406 pub var_name: UnencodedString,
1409 pub missing_values: MissingValues,
1412 #[derive(Clone, Debug)]
1413 pub struct LongStringMissingValueSet(Vec<LongStringMissingValues>);
1415 impl ExtensionRecord for LongStringMissingValueSet {
1416 const SUBTYPE: u32 = 22;
1417 const SIZE: Option<u32> = Some(1);
1418 const COUNT: Option<u32> = None;
1419 const NAME: &'static str = "long string missing values record";
1421 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1422 ext.check_size::<Self>()?;
1424 let mut input = &ext.data[..];
1425 let mut missing_value_set = Vec::new();
1426 while !input.is_empty() {
1427 let var_name = read_string(&mut input, endian)?;
1428 let n_missing_values: u8 = endian.parse(read_bytes(&mut input)?);
1429 let value_len: u32 = endian.parse(read_bytes(&mut input)?);
1431 let offset = (ext.data.len() - input.len() - 8) as u64 + ext.offsets.start;
1432 return Err(Error::BadLongMissingValueLength {
1433 record_offset: ext.offsets.start,
1438 let mut values = Vec::new();
1439 for i in 0..n_missing_values {
1440 let value: [u8; 8] = read_bytes(&mut input)?;
1441 let numeric_value: u64 = endian.parse(value);
1442 let value = if i > 0 && numeric_value == 8 {
1443 // Tolerate files written by old, buggy versions of PSPP
1444 // where we believed that the value_length was repeated
1445 // before each missing value.
1446 read_bytes(&mut input)?
1450 values.push(Value::String(UnencodedStr(value)));
1452 let missing_values = MissingValues {
1456 missing_value_set.push(LongStringMissingValues {
1461 Ok(Record::LongStringMissingValues(LongStringMissingValueSet(
1467 #[derive(Clone, Debug)]
1468 pub struct EncodingRecord(pub String);
1470 impl ExtensionRecord for EncodingRecord {
1471 const SUBTYPE: u32 = 20;
1472 const SIZE: Option<u32> = Some(1);
1473 const COUNT: Option<u32> = None;
1474 const NAME: &'static str = "encoding record";
1476 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Error> {
1477 ext.check_size::<Self>()?;
1479 Ok(Record::Encoding(EncodingRecord(
1480 String::from_utf8(ext.data.clone()).map_err(|_| Error::BadEncodingName {
1481 offset: ext.offsets.start,
1487 #[derive(Clone, Debug)]
1488 pub struct NumberOfCasesRecord {
1489 /// Always observed as 1.
1492 /// Number of cases.
1496 impl ExtensionRecord for NumberOfCasesRecord {
1497 const SUBTYPE: u32 = 16;
1498 const SIZE: Option<u32> = Some(8);
1499 const COUNT: Option<u32> = Some(2);
1500 const NAME: &'static str = "extended number of cases record";
1502 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1503 ext.check_size::<Self>()?;
1505 let mut input = &ext.data[..];
1506 let one = endian.parse(read_bytes(&mut input)?);
1507 let n_cases = endian.parse(read_bytes(&mut input)?);
1509 Ok(Record::NumberOfCases(NumberOfCasesRecord { one, n_cases }))
1513 #[derive(Clone, Debug)]
1514 pub struct TextRecord {
1515 pub offsets: Range<u64>,
1517 /// The text content of the record.
1518 pub text: UnencodedString,
1521 impl From<Extension> for TextRecord {
1522 fn from(source: Extension) -> Self {
1524 offsets: source.offsets,
1525 text: source.data.into(),
1530 #[derive(Clone, Debug)]
1531 pub struct Extension {
1532 pub offsets: Range<u64>,
1537 /// Size of each data element.
1540 /// Number of data elements.
1543 /// `size * count` bytes of data.
1548 fn check_size<E: ExtensionRecord>(&self) -> Result<(), Error> {
1549 if let Some(expected_size) = E::SIZE {
1550 if self.size != expected_size {
1551 return Err(Error::BadRecordSize {
1552 offset: self.offsets.start,
1553 record: E::NAME.into(),
1559 if let Some(expected_count) = E::COUNT {
1560 if self.count != expected_count {
1561 return Err(Error::BadRecordCount {
1562 offset: self.offsets.start,
1563 record: E::NAME.into(),
1572 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Record, Error> {
1573 let subtype = endian.parse(read_bytes(r)?);
1574 let header_offset = r.stream_position()?;
1575 let size: u32 = endian.parse(read_bytes(r)?);
1576 let count = endian.parse(read_bytes(r)?);
1577 let Some(product) = size.checked_mul(count) else {
1578 return Err(Error::ExtensionRecordTooLarge {
1579 offset: header_offset,
1585 let start_offset = r.stream_position()?;
1586 let data = read_vec(r, product as usize)?;
1587 let end_offset = start_offset + product as u64;
1588 let extension = Extension {
1589 offsets: start_offset..end_offset,
1596 IntegerInfoRecord::SUBTYPE => Ok(IntegerInfoRecord::parse(
1599 FloatInfoRecord::SUBTYPE => Ok(FloatInfoRecord::parse(
1602 VarDisplayRecord::SUBTYPE => Ok(VarDisplayRecord::parse(
1605 MultipleResponseRecord::SUBTYPE | 19 => Ok(
1606 MultipleResponseRecord::parse(&extension, endian)?,
1608 LongStringValueLabelRecord::SUBTYPE => Ok(
1609 LongStringValueLabelRecord::parse(&extension, endian)?,
1611 EncodingRecord::SUBTYPE => {
1612 Ok(EncodingRecord::parse(&extension, endian)?)
1614 NumberOfCasesRecord::SUBTYPE => Ok(NumberOfCasesRecord::parse(
1617 5 => Ok(Record::VariableSets(extension.into())),
1618 10 => Ok(Record::ProductInfo(extension.into())),
1619 13 => Ok(Record::LongNames(extension.into())),
1620 14 => Ok(Record::VeryLongStrings(extension.into())),
1621 17 => Ok(Record::FileAttributes(extension.into())),
1622 18 => Ok(Record::VariableAttributes(extension.into())),
1623 _ => Ok(Record::OtherExtension(extension)),
1628 #[derive(Clone, Debug)]
1629 pub struct ZHeader {
1630 /// File offset to the start of the record.
1633 /// File offset to the ZLIB data header.
1634 pub zheader_offset: u64,
1636 /// File offset to the ZLIB trailer.
1637 pub ztrailer_offset: u64,
1639 /// Length of the ZLIB trailer in bytes.
1640 pub ztrailer_len: u64,
1644 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZHeader, Error> {
1645 let offset = r.stream_position()?;
1646 let zheader_offset: u64 = endian.parse(read_bytes(r)?);
1647 let ztrailer_offset: u64 = endian.parse(read_bytes(r)?);
1648 let ztrailer_len: u64 = endian.parse(read_bytes(r)?);
1659 #[derive(Clone, Debug)]
1660 pub struct ZTrailer {
1661 /// File offset to the start of the record.
1664 /// Compression bias as a negative integer, e.g. -100.
1667 /// Always observed as zero.
1670 /// Uncompressed size of each block, except possibly the last. Only
1671 /// `0x3ff000` has been observed so far.
1672 pub block_size: u32,
1674 /// Block descriptors, always `(ztrailer_len - 24) / 24)` of them.
1675 pub blocks: Vec<ZBlock>,
1678 #[derive(Clone, Debug)]
1680 /// Offset of block of data if simple compression were used.
1681 pub uncompressed_ofs: u64,
1683 /// Actual offset within the file of the compressed data block.
1684 pub compressed_ofs: u64,
1686 /// The number of bytes in this data block after decompression. This is
1687 /// `block_size` in every data block but the last, which may be smaller.
1688 pub uncompressed_size: u32,
1690 /// The number of bytes in this data block, as stored compressed in this
1692 pub compressed_size: u32,
1696 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZBlock, Error> {
1698 uncompressed_ofs: endian.parse(read_bytes(r)?),
1699 compressed_ofs: endian.parse(read_bytes(r)?),
1700 uncompressed_size: endian.parse(read_bytes(r)?),
1701 compressed_size: endian.parse(read_bytes(r)?),
1707 fn read<R: Read + Seek>(
1712 ) -> Result<Option<ZTrailer>, Error> {
1713 let start_offset = reader.stream_position()?;
1714 if reader.seek(SeekFrom::Start(ztrailer_ofs)).is_err() {
1717 let int_bias = endian.parse(read_bytes(reader)?);
1718 let zero = endian.parse(read_bytes(reader)?);
1719 let block_size = endian.parse(read_bytes(reader)?);
1720 let n_blocks: u32 = endian.parse(read_bytes(reader)?);
1721 let expected_n_blocks = (ztrailer_len - 24) / 24;
1722 if n_blocks as u64 != expected_n_blocks {
1723 return Err(Error::BadZlibTrailerNBlocks {
1724 offset: ztrailer_ofs,
1730 let blocks = (0..n_blocks)
1731 .map(|_| ZBlock::read(reader, endian))
1732 .collect::<Result<Vec<_>, _>>()?;
1733 reader.seek(SeekFrom::Start(start_offset))?;
1735 offset: ztrailer_ofs,
1744 fn try_read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<Option<[u8; N]>, IoError> {
1745 let mut buf = [0; N];
1746 let n = r.read(&mut buf)?;
1749 r.read_exact(&mut buf[n..])?;
1757 fn read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<[u8; N], IoError> {
1758 let mut buf = [0; N];
1759 r.read_exact(&mut buf)?;
1763 fn read_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<u8>, IoError> {
1764 let mut vec = vec![0; n];
1765 r.read_exact(&mut vec)?;
1769 fn read_string<R: Read>(r: &mut R, endian: Endian) -> Result<UnencodedString, IoError> {
1770 let length: u32 = endian.parse(read_bytes(r)?);
1771 Ok(read_vec(r, length as usize)?.into())
1774 #[derive(Clone, Debug)]
1775 pub struct LongStringValueLabels {
1776 pub var_name: UnencodedString,
1779 /// `(value, label)` pairs, where each value is `width` bytes.
1780 pub labels: Vec<(UnencodedString, UnencodedString)>,
1783 #[derive(Clone, Debug)]
1784 pub struct LongStringValueLabelRecord(pub Vec<LongStringValueLabels>);
1786 impl ExtensionRecord for LongStringValueLabelRecord {
1787 const SUBTYPE: u32 = 21;
1788 const SIZE: Option<u32> = Some(1);
1789 const COUNT: Option<u32> = None;
1790 const NAME: &'static str = "long string value labels record";
1792 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1793 ext.check_size::<Self>()?;
1795 let mut input = &ext.data[..];
1796 let mut label_set = Vec::new();
1797 while !input.is_empty() {
1798 let var_name = read_string(&mut input, endian)?;
1799 let width: u32 = endian.parse(read_bytes(&mut input)?);
1800 let n_labels: u32 = endian.parse(read_bytes(&mut input)?);
1801 let mut labels = Vec::new();
1802 for _ in 0..n_labels {
1803 let value = read_string(&mut input, endian)?;
1804 let label = read_string(&mut input, endian)?;
1805 labels.push((value, label));
1807 label_set.push(LongStringValueLabels {
1813 Ok(Record::LongStringValueLabels(LongStringValueLabelRecord(