1 use crate::endian::{Endian, Parse, ToBytes};
3 use encoding_rs::{mem::decode_latin1, DecoderResult, Encoding};
4 use flate2::read::ZlibDecoder;
10 collections::VecDeque,
11 fmt::{Debug, Display, Formatter, Result as FmtResult},
12 io::{Error as IoError, Read, Seek, SeekFrom},
19 use thiserror::Error as ThisError;
21 #[derive(ThisError, Debug)]
23 #[error("Not an SPSS system file")]
26 #[error("Invalid magic number {0:?}")]
29 #[error("I/O error ({0})")]
32 #[error("Invalid SAV compression code {0}")]
33 InvalidSavCompression(u32),
35 #[error("Invalid ZSAV compression code {0}")]
36 InvalidZsavCompression(u32),
38 #[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
39 BadVariableWidth { offset: u64, width: i32 },
41 #[error("Document record at offset {offset:#x} has document line count ({n}) greater than the maximum number {max}.")]
42 BadDocumentLength { offset: u64, n: usize, max: usize },
44 #[error("At offset {offset:#x}, unrecognized record type {rec_type}.")]
45 BadRecordType { offset: u64, rec_type: u32 },
47 #[error("In variable record starting at offset {start_offset:#x}, variable label code {code} at offset {code_offset:#x} is not 0 or 1.")]
48 BadVariableLabelCode {
55 "At offset {offset:#x}, numeric missing value code ({code}) is not -3, -2, 0, 1, 2, or 3."
57 BadNumericMissingValueCode { offset: u64, code: i32 },
59 #[error("At offset {offset:#x}, string missing value code ({code}) is not 0, 1, 2, or 3.")]
60 BadStringMissingValueCode { offset: u64, code: i32 },
62 #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")]
63 BadNumberOfValueLabels { offset: u64, n: u32, max: u32 },
65 #[error("At offset {offset:#x}, following value label record, found record type {rec_type} instead of expected type 4 for variable index record")]
66 ExpectedVarIndexRecord { offset: u64, rec_type: u32 },
68 #[error("At offset {offset:#x}, number of variables indexes for value labels ({n}) is greater than the maximum number ({max}).")]
69 TooManyVarIndexes { offset: u64, n: u32, max: u32 },
71 #[error("At offset {offset:#x}, at least one valid variable index for value labels is required but none were specified.")]
72 NoVarIndexes { offset: u64 },
74 #[error("At offset {offset:#x}, the first variable index is for a {var_type} variable but the following variable indexes are for {} variables: {wrong_types:?}", var_type.opposite())]
78 wrong_types: Vec<u32>,
81 #[error("At offset {offset:#x}, one or more variable indexes for value labels were not in the valid range [1,{max}]: {invalid:?}")]
88 #[error("At offset {offset:#x}, record type 7 subtype {subtype} is too large with element size {size} and {count} elements.")]
89 ExtensionRecordTooLarge {
96 #[error("Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a {case_len}-byte case.")]
104 "Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a compressed case."
106 EofInCompressedCase { offset: u64, case_ofs: u64 },
108 #[error("Data ends at offset {offset:#x}, {case_ofs} bytes into a compressed case.")]
109 PartialCompressedCase { offset: u64, case_ofs: u64 },
111 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a string was found where a number was expected.")]
112 CompressedNumberExpected { offset: u64, case_ofs: u64 },
114 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a number was found where a string was expected.")]
115 CompressedStringExpected { offset: u64, case_ofs: u64 },
117 #[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}.")]
118 BadZlibTrailerNBlocks {
121 expected_n_blocks: u64,
125 #[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
133 #[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
141 #[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.")]
142 BadLongMissingValueLength {
148 #[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
149 BadEncodingName { offset: u64 },
151 // XXX This is risky because `text` might be arbitarily long.
152 #[error("Text string contains invalid bytes for {encoding} encoding: {text}")]
153 MalformedString { encoding: String, text: String },
155 #[error("Details TBD")]
159 #[derive(Clone, Debug)]
161 Header(HeaderRecord<RawString>),
162 Variable(VariableRecord<RawString, RawStr<8>>),
163 ValueLabel(ValueLabelRecord<RawStr<8>, RawString>),
164 Document(DocumentRecord),
165 IntegerInfo(IntegerInfoRecord),
166 FloatInfo(FloatInfoRecord),
167 VariableSets(TextRecord),
168 VarDisplay(VarDisplayRecord),
169 MultipleResponse(MultipleResponseRecord),
170 LongStringValueLabels(LongStringValueLabelRecord),
171 LongStringMissingValues(LongStringMissingValueRecord),
172 Encoding(EncodingRecord),
173 NumberOfCases(NumberOfCasesRecord),
174 ProductInfo(TextRecord),
175 LongNames(TextRecord),
176 VeryLongStrings(TextRecord),
177 FileAttributes(TextRecord),
178 VariableAttributes(TextRecord),
179 OtherExtension(Extension),
183 Cases(Rc<RefCell<Cases>>),
190 var_types: &[VarType],
191 warn: &Box<dyn Fn(Error)>,
192 ) -> Result<Option<Record>, Error>
196 let rec_type: u32 = endian.parse(read_bytes(reader)?);
198 2 => Ok(Some(VariableRecord::read(reader, endian)?)),
199 3 => Ok(ValueLabelRecord::read(reader, endian, var_types, warn)?),
200 6 => Ok(Some(DocumentRecord::read(reader, endian)?)),
201 7 => Extension::read(reader, endian, warn),
202 999 => Ok(Some(Record::EndOfHeaders(
203 endian.parse(read_bytes(reader)?),
205 _ => Err(Error::BadRecordType {
206 offset: reader.stream_position()?,
213 // If `s` is valid UTF-8, returns it decoded as UTF-8, otherwise returns it
214 // decoded as Latin-1 (actually bytes interpreted as Unicode code points).
215 fn default_decode(s: &[u8]) -> Cow<str> {
216 from_utf8(s).map_or_else(|_| decode_latin1(s), Cow::from)
219 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
220 pub enum Compression {
226 fn offsets(&self) -> Range<u64>;
230 pub struct HeaderRecord<S>
235 pub offsets: Range<u64>,
240 /// Eye-catcher string, product name, in the file's encoding. Padded
241 /// on the right with spaces.
244 /// Layout code, normally either 2 or 3.
245 pub layout_code: u32,
247 /// Number of variable positions, or `None` if the value in the file is
248 /// questionably trustworthy.
249 pub nominal_case_size: Option<u32>,
251 /// Compression type, if any,
252 pub compression: Option<Compression>,
254 /// 1-based variable index of the weight variable, or `None` if the file is
256 pub weight_index: Option<u32>,
258 /// Claimed number of cases, if known.
259 pub n_cases: Option<u32>,
261 /// Compression bias, usually 100.0.
264 /// `dd mmm yy` in the file's encoding.
265 pub creation_date: S,
267 /// `HH:MM:SS` in the file's encoding.
268 pub creation_time: S,
270 /// File label, in the file's encoding. Padded on the right with spaces.
273 /// Endianness of the data in the file header.
277 impl<S> HeaderRecord<S>
281 fn debug_field<T>(&self, f: &mut Formatter, name: &str, value: T) -> FmtResult
285 writeln!(f, "{name:>17}: {:?}", value)
289 impl<S> Debug for HeaderRecord<S>
293 fn fmt(&self, f: &mut Formatter) -> FmtResult {
294 writeln!(f, "File header record:")?;
295 self.debug_field(f, "Magic", self.magic)?;
296 self.debug_field(f, "Product name", &self.eye_catcher)?;
297 self.debug_field(f, "Layout code", self.layout_code)?;
298 self.debug_field(f, "Nominal case size", self.nominal_case_size)?;
299 self.debug_field(f, "Compression", self.compression)?;
300 self.debug_field(f, "Weight index", self.weight_index)?;
301 self.debug_field(f, "Number of cases", self.n_cases)?;
302 self.debug_field(f, "Compression bias", self.bias)?;
303 self.debug_field(f, "Creation date", &self.creation_date)?;
304 self.debug_field(f, "Creation time", &self.creation_time)?;
305 self.debug_field(f, "File label", &self.file_label)?;
306 self.debug_field(f, "Endianness", self.endian)
310 impl HeaderRecord<RawString> {
311 fn read<R: Read + Seek>(r: &mut R) -> Result<Self, Error> {
312 let start = r.stream_position()?;
314 let magic: [u8; 4] = read_bytes(r)?;
315 let magic: Magic = magic.try_into().map_err(|_| Error::NotASystemFile)?;
317 let eye_catcher = RawString(read_vec(r, 60)?);
318 let layout_code: [u8; 4] = read_bytes(r)?;
319 let endian = Endian::identify_u32(2, layout_code)
320 .or_else(|| Endian::identify_u32(2, layout_code))
321 .ok_or_else(|| Error::NotASystemFile)?;
322 let layout_code = endian.parse(layout_code);
324 let nominal_case_size: u32 = endian.parse(read_bytes(r)?);
325 let nominal_case_size =
326 (nominal_case_size <= i32::MAX as u32 / 16).then_some(nominal_case_size);
328 let compression_code: u32 = endian.parse(read_bytes(r)?);
329 let compression = match (magic, compression_code) {
330 (Magic::Zsav, 2) => Some(Compression::ZLib),
331 (Magic::Zsav, code) => return Err(Error::InvalidZsavCompression(code)),
333 (_, 1) => Some(Compression::Simple),
334 (_, code) => return Err(Error::InvalidSavCompression(code)),
337 let weight_index: u32 = endian.parse(read_bytes(r)?);
338 let weight_index = (weight_index > 0).then_some(weight_index);
340 let n_cases: u32 = endian.parse(read_bytes(r)?);
341 let n_cases = (n_cases < i32::MAX as u32 / 2).then_some(n_cases);
343 let bias: f64 = endian.parse(read_bytes(r)?);
345 let creation_date = RawString(read_vec(r, 9)?);
346 let creation_time = RawString(read_vec(r, 8)?);
347 let file_label = RawString(read_vec(r, 64)?);
348 let _: [u8; 3] = read_bytes(r)?;
351 offsets: start..r.stream_position()?,
367 fn decode<'a>(&'a self, decoder: &Decoder) -> HeaderRecord<Cow<'a, str>> {
368 let eye_catcher = decoder.decode(&self.eye_catcher);
369 let file_label = decoder.decode(&self.file_label);
370 let creation_date = decoder.decode(&self.creation_date);
371 let creation_time = decoder.decode(&self.creation_time);
374 weight_index: self.weight_index,
375 n_cases: self.n_cases,
377 offsets: self.offsets.clone(),
379 layout_code: self.layout_code,
380 nominal_case_size: self.nominal_case_size,
381 compression: self.compression,
391 encoding: &'static Encoding,
392 warn: Box<dyn Fn(Error)>,
396 fn decode<'a>(&self, input: &'a RawString) -> Cow<'a, str> {
397 let (output, malformed) = self.encoding.decode_without_bom_handling(&input.0);
399 (self.warn)(Error::MalformedString {
400 encoding: self.encoding.name().into(),
401 text: output.clone().into(),
407 /// Returns `input` decoded from `self.encoding` into UTF-8 such that
408 /// re-encoding the result back into `self.encoding` will have exactly the
409 /// same length in bytes.
411 /// XXX warn about errors?
412 fn decode_exact_length<'a>(&self, input: &'a [u8]) -> Cow<'a, str> {
413 if let (s, false) = self.encoding.decode_without_bom_handling(input) {
414 // This is the common case. Usually there will be no errors.
417 // Unusual case. Don't bother to optimize it much.
418 let mut decoder = self.encoding.new_decoder_without_bom_handling();
419 let mut output = String::with_capacity(
421 .max_utf8_buffer_length_without_replacement(input.len())
424 let mut rest = input;
425 while !rest.is_empty() {
426 match decoder.decode_to_string_without_replacement(rest, &mut output, true) {
427 (DecoderResult::InputEmpty, _) => break,
428 (DecoderResult::OutputFull, _) => unreachable!(),
429 (DecoderResult::Malformed(a, b), consumed) => {
430 let skipped = a as usize + b as usize;
431 output.extend(repeat('?').take(skipped));
432 rest = &rest[consumed..];
436 assert_eq!(self.encoding.encode(&output).0.len(), input.len());
442 impl<S> Header for HeaderRecord<S>
446 fn offsets(&self) -> Range<u64> {
451 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
453 /// Regular system file.
456 /// System file with Zlib-compressed data.
459 /// EBCDIC-encoded system file.
464 /// Magic number for a regular system file.
465 pub const SAV: [u8; 4] = *b"$FL2";
467 /// Magic number for a system file that contains zlib-compressed data.
468 pub const ZSAV: [u8; 4] = *b"$FL3";
470 /// Magic number for an EBCDIC-encoded system file. This is `$FL2` encoded
472 pub const EBCDIC: [u8; 4] = [0x5b, 0xc6, 0xd3, 0xf2];
475 impl Debug for Magic {
476 fn fmt(&self, f: &mut Formatter) -> FmtResult {
477 let s = match *self {
478 Magic::Sav => "$FL2",
479 Magic::Zsav => "$FL3",
480 Magic::Ebcdic => "($FL2 in EBCDIC)",
486 impl TryFrom<[u8; 4]> for Magic {
489 fn try_from(value: [u8; 4]) -> Result<Self, Self::Error> {
491 Magic::SAV => Ok(Magic::Sav),
492 Magic::ZSAV => Ok(Magic::Zsav),
493 Magic::EBCDIC => Ok(Magic::Ebcdic),
494 _ => Err(Error::BadMagic(value)),
499 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
506 fn from_width(width: i32) -> VarType {
508 0 => VarType::Numeric,
509 _ => VarType::String,
513 fn opposite(self) -> VarType {
515 Self::Numeric => Self::String,
516 Self::String => Self::Numeric,
521 impl Display for VarType {
522 fn fmt(&self, f: &mut Formatter) -> FmtResult {
524 VarType::Numeric => write!(f, "numeric"),
525 VarType::String => write!(f, "string"),
530 #[derive(Copy, Clone)]
539 type RawValue = Value<RawStr<8>>;
541 impl<S> Debug for Value<S>
545 fn fmt(&self, f: &mut Formatter) -> FmtResult {
547 Value::Number(Some(number)) => write!(f, "{number:?}"),
548 Value::Number(None) => write!(f, "SYSMIS"),
549 Value::String(s) => write!(f, "{:?}", s),
555 fn read<R: Read>(r: &mut R, var_type: VarType, endian: Endian) -> Result<Self, IoError> {
557 &UntypedValue(read_bytes(r)?),
563 pub fn from_raw(raw: &UntypedValue, var_type: VarType, endian: Endian) -> Self {
565 VarType::String => Value::String(RawStr(raw.0)),
566 VarType::Numeric => {
567 let number: f64 = endian.parse(raw.0);
568 Value::Number((number != -f64::MAX).then_some(number))
573 fn read_case<R: Read + Seek>(
575 var_types: &[VarType],
577 ) -> Result<Option<Vec<Self>>, Error> {
578 let case_start = reader.stream_position()?;
579 let mut values = Vec::with_capacity(var_types.len());
580 for (i, &var_type) in var_types.iter().enumerate() {
581 let Some(raw) = try_read_bytes(reader)? else {
585 let offset = reader.stream_position()?;
586 return Err(Error::EofInCase {
588 case_ofs: offset - case_start,
589 case_len: var_types.len() * 8,
593 values.push(Value::from_raw(&UntypedValue(raw), var_type, endian));
598 fn read_compressed_case<R: Read + Seek>(
600 var_types: &[VarType],
601 codes: &mut VecDeque<u8>,
604 ) -> Result<Option<Vec<Self>>, Error> {
605 let case_start = reader.stream_position()?;
606 let mut values = Vec::with_capacity(var_types.len());
607 for (i, &var_type) in var_types.iter().enumerate() {
609 let Some(code) = codes.pop_front() else {
610 let Some(new_codes): Option<[u8; 8]> = try_read_bytes(reader)? else {
614 let offset = reader.stream_position()?;
615 return Err(Error::EofInCompressedCase {
617 case_ofs: offset - case_start,
621 codes.extend(new_codes.into_iter());
626 1..=251 => match var_type {
627 VarType::Numeric => break Self::Number(Some(code as f64 - bias)),
629 break Self::String(RawStr(endian.to_bytes(code as f64 - bias)))
636 let offset = reader.stream_position()?;
637 return Err(Error::PartialCompressedCase {
639 case_ofs: offset - case_start,
644 break Self::from_raw(&UntypedValue(read_bytes(reader)?), var_type, endian)
646 254 => match var_type {
647 VarType::String => break Self::String(RawStr(*b" ")), // XXX EBCDIC
648 VarType::Numeric => {
649 return Err(Error::CompressedStringExpected {
651 case_ofs: reader.stream_position()? - case_start,
655 255 => match var_type {
656 VarType::Numeric => break Self::Number(None),
658 return Err(Error::CompressedNumberExpected {
660 case_ofs: reader.stream_position()? - case_start,
671 fn decode(&self, decoder: &Decoder) -> Value<String> {
673 Self::Number(x) => Value::Number(*x),
674 Self::String(s) => Value::String(decoder.decode_exact_length(&s.0).into()),
679 struct ZlibDecodeMultiple<R>
683 reader: Option<ZlibDecoder<R>>,
686 impl<R> ZlibDecodeMultiple<R>
690 fn new(reader: R) -> ZlibDecodeMultiple<R> {
692 reader: Some(ZlibDecoder::new(reader)),
697 impl<R> Read for ZlibDecodeMultiple<R>
701 fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
703 match self.reader.as_mut().unwrap().read(buf)? {
705 let inner = self.reader.take().unwrap().into_inner();
706 self.reader = Some(ZlibDecoder::new(inner));
714 impl<R> Seek for ZlibDecodeMultiple<R>
718 fn seek(&mut self, pos: SeekFrom) -> Result<u64, IoError> {
719 self.reader.as_mut().unwrap().get_mut().seek(pos)
728 ztrailer_offset: u64,
737 R: Read + Seek + 'static,
740 warn: Box<dyn Fn(Error)>,
742 header: HeaderRecord<RawString>,
743 var_types: Vec<VarType>,
750 R: Read + Seek + 'static,
752 pub fn new<F>(mut reader: R, warn: F) -> Result<Self, Error>
754 F: Fn(Error) + 'static,
756 let header = HeaderRecord::read(&mut reader)?;
758 reader: Some(reader),
759 warn: Box::new(warn),
761 var_types: Vec::new(),
762 state: ReaderState::Start,
765 fn cases(&mut self) -> Cases {
766 self.state = ReaderState::End;
768 self.reader.take().unwrap(),
769 take(&mut self.var_types),
775 impl<R> Iterator for Reader<R>
777 R: Read + Seek + 'static,
779 type Item = Result<Record, Error>;
781 fn next(&mut self) -> Option<Self::Item> {
783 ReaderState::Start => {
784 self.state = ReaderState::Headers;
785 Some(Ok(Record::Header(self.header.clone())))
787 ReaderState::Headers => {
790 self.reader.as_mut().unwrap(),
792 self.var_types.as_slice(),
795 Ok(Some(record)) => break record,
797 Err(error) => return Some(Err(error)),
801 Record::Variable(VariableRecord { width, .. }) => {
802 self.var_types.push(VarType::from_width(width));
804 Record::EndOfHeaders(_) => {
805 self.state = if let Some(Compression::ZLib) = self.header.compression {
806 ReaderState::ZlibHeader
815 ReaderState::ZlibHeader => {
816 let zheader = match ZHeader::read(self.reader.as_mut().unwrap(), self.header.endian)
818 Ok(zheader) => zheader,
819 Err(error) => return Some(Err(error)),
821 self.state = ReaderState::ZlibTrailer {
822 ztrailer_offset: zheader.ztrailer_offset,
823 ztrailer_len: zheader.ztrailer_len,
825 Some(Ok(Record::ZHeader(zheader)))
827 ReaderState::ZlibTrailer {
831 match ZTrailer::read(
832 self.reader.as_mut().unwrap(),
837 Ok(None) => Some(Ok(Record::Cases(Rc::new(RefCell::new(self.cases()))))),
838 Ok(Some(ztrailer)) => Some(Ok(Record::ZTrailer(ztrailer))),
839 Err(error) => Some(Err(error)),
842 ReaderState::Cases => Some(Ok(Record::Cases(Rc::new(RefCell::new(self.cases()))))),
843 ReaderState::End => None,
848 trait ReadSeek: Read + Seek {}
849 impl<T> ReadSeek for T where T: Read + Seek {}
852 reader: Box<dyn ReadSeek>,
853 var_types: Vec<VarType>,
854 compression: Option<Compression>,
861 impl Debug for Cases {
862 fn fmt(&self, f: &mut Formatter) -> FmtResult {
868 fn new<R>(reader: R, var_types: Vec<VarType>, header: &HeaderRecord<RawString>) -> Self
870 R: Read + Seek + 'static,
873 reader: if header.compression == Some(Compression::ZLib) {
874 Box::new(ZlibDecodeMultiple::new(reader))
879 compression: header.compression,
881 endian: header.endian,
882 codes: VecDeque::with_capacity(8),
888 impl Iterator for Cases {
889 type Item = Result<Vec<RawValue>, Error>;
891 fn next(&mut self) -> Option<Self::Item> {
896 let retval = if self.compression.is_some() {
897 Value::read_compressed_case(
906 Value::read_case(&mut self.reader, &self.var_types, self.endian).transpose()
908 self.eof = matches!(retval, None | Some(Err(_)));
913 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
914 pub struct Spec(pub u32);
916 impl Debug for Spec {
917 fn fmt(&self, f: &mut Formatter) -> FmtResult {
918 let type_ = format_name(self.0 >> 16);
919 let w = (self.0 >> 8) & 0xff;
920 let d = self.0 & 0xff;
921 write!(f, "{:06x} ({type_}{w}.{d})", self.0)
925 fn format_name(type_: u32) -> Cow<'static, str> {
964 _ => return format!("<unknown format {type_}>").into(),
970 pub struct MissingValues<S>
974 /// Individual missing values, up to 3 of them.
975 pub values: Vec<Value<S>>,
977 /// Optional range of missing values.
978 pub range: Option<(Value<S>, Value<S>)>,
981 impl<S> Debug for MissingValues<S>
985 fn fmt(&self, f: &mut Formatter) -> FmtResult {
986 for (i, value) in self.values.iter().enumerate() {
990 write!(f, "{value:?}")?;
993 if let Some((low, high)) = &self.range {
994 if !self.values.is_empty() {
997 write!(f, "{low:?} THRU {high:?}")?;
1000 if self.is_empty() {
1008 impl<S> MissingValues<S>
1012 fn is_empty(&self) -> bool {
1013 self.values.is_empty() && self.range.is_none()
1017 impl MissingValues<RawStr<8>> {
1018 fn read<R: Read + Seek>(
1024 ) -> Result<Self, Error> {
1025 let (n_values, has_range) = match (width, code) {
1026 (_, 0..=3) => (code, false),
1027 (0, -2) => (0, true),
1028 (0, -3) => (1, true),
1029 (0, _) => return Err(Error::BadNumericMissingValueCode { offset, code }),
1030 (_, _) => return Err(Error::BadStringMissingValueCode { offset, code }),
1033 let var_type = VarType::from_width(width);
1035 let mut values = Vec::new();
1036 for _ in 0..n_values {
1037 values.push(RawValue::read(r, var_type, endian)?);
1039 let range = if has_range {
1040 let low = RawValue::read(r, var_type, endian)?;
1041 let high = RawValue::read(r, var_type, endian)?;
1046 Ok(Self { values, range })
1048 fn decode<'a>(&'a self, decoder: &Decoder) -> MissingValues<String> {
1053 .map(|value| value.decode(decoder))
1058 .map(|(low, high)| (low.decode(decoder), high.decode(decoder))),
1064 pub struct VariableRecord<S, V>
1069 /// Range of offsets in file.
1070 pub offsets: Range<u64>,
1072 /// Variable width, in the range -1..=255.
1075 /// Variable name, padded on the right with spaces.
1079 pub print_format: Spec,
1082 pub write_format: Spec,
1085 pub missing_values: MissingValues<V>,
1087 /// Optional variable label.
1088 pub label: Option<S>,
1091 impl<S, V> Debug for VariableRecord<S, V>
1096 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1101 match self.width.cmp(&0) {
1102 Ordering::Greater => "string",
1103 Ordering::Equal => "numeric",
1104 Ordering::Less => "long string continuation record",
1107 writeln!(f, "Print format: {:?}", self.print_format)?;
1108 writeln!(f, "Write format: {:?}", self.write_format)?;
1109 writeln!(f, "Name: {:?}", &self.name)?;
1110 writeln!(f, "Variable label: {:?}", self.label)?;
1111 writeln!(f, "Missing values: {:?}", self.missing_values)
1115 impl VariableRecord<RawString, RawStr<8>> {
1116 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Record, Error> {
1117 let start_offset = r.stream_position()?;
1118 let width: i32 = endian.parse(read_bytes(r)?);
1119 let code_offset = r.stream_position()?;
1120 let has_variable_label: u32 = endian.parse(read_bytes(r)?);
1121 let missing_value_code: i32 = endian.parse(read_bytes(r)?);
1122 let print_format = Spec(endian.parse(read_bytes(r)?));
1123 let write_format = Spec(endian.parse(read_bytes(r)?));
1124 let name = RawString(read_vec(r, 8)?);
1126 let label = match has_variable_label {
1129 let len: u32 = endian.parse(read_bytes(r)?);
1130 let read_len = len.min(65535) as usize;
1131 let label = RawString(read_vec(r, read_len)?);
1133 let padding_bytes = Integer::next_multiple_of(&len, &4) - len;
1134 let _ = read_vec(r, padding_bytes as usize)?;
1139 return Err(Error::BadVariableLabelCode {
1142 code: has_variable_label,
1147 let missing_values =
1148 MissingValues::read(r, start_offset, width, missing_value_code, endian)?;
1150 let end_offset = r.stream_position()?;
1152 Ok(Record::Variable(VariableRecord {
1153 offsets: start_offset..end_offset,
1163 fn decode<'a>(&'a self, decoder: &Decoder) -> VariableRecord<Cow<'a, str>, String> {
1165 offsets: self.offsets.clone(),
1167 name: decoder.decode(&self.name),
1168 print_format: self.print_format,
1169 write_format: self.write_format,
1170 missing_values: self.missing_values.decode(decoder),
1171 label: self.label.as_ref().map(|label| decoder.decode(label)),
1176 #[derive(Copy, Clone)]
1177 pub struct UntypedValue(pub [u8; 8]);
1179 impl Debug for UntypedValue {
1180 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1181 let little: f64 = Endian::Little.parse(self.0);
1182 let little = format!("{:?}", little);
1183 let big: f64 = Endian::Big.parse(self.0);
1184 let big = format!("{:?}", big);
1185 let number = if little.len() <= big.len() {
1190 write!(f, "{number}")?;
1192 let string = default_decode(&self.0);
1194 .split(|c: char| c == '\0' || c.is_control())
1197 write!(f, "{string:?}")?;
1203 pub struct RawString(pub Vec<u8>);
1205 impl From<Vec<u8>> for RawString {
1206 fn from(source: Vec<u8>) -> Self {
1211 impl From<&[u8]> for RawString {
1212 fn from(source: &[u8]) -> Self {
1217 impl Debug for RawString {
1218 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1219 write!(f, "{:?}", default_decode(self.0.as_slice()))
1223 #[derive(Copy, Clone)]
1224 pub struct RawStr<const N: usize>(pub [u8; N]);
1226 impl<const N: usize> From<[u8; N]> for RawStr<N> {
1227 fn from(source: [u8; N]) -> Self {
1232 impl<const N: usize> Debug for RawStr<N> {
1233 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1234 write!(f, "{:?}", default_decode(&self.0))
1238 #[derive(Clone, Debug)]
1239 pub struct ValueLabel<V, S>
1244 pub value: Value<V>,
1249 pub struct ValueLabelRecord<V, S>
1254 /// Range of offsets in file.
1255 pub offsets: Range<u64>,
1258 pub labels: Vec<ValueLabel<V, S>>,
1260 /// The 1-based indexes of the variable indexes.
1261 pub dict_indexes: Vec<u32>,
1263 /// The types of the variables.
1264 pub var_type: VarType,
1267 impl<V, S> Debug for ValueLabelRecord<V, S>
1272 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1273 writeln!(f, "labels: ")?;
1274 for label in self.labels.iter() {
1275 writeln!(f, "{label:?}")?;
1277 write!(f, "apply to {} variables", self.var_type)?;
1278 for dict_index in self.dict_indexes.iter() {
1279 write!(f, " #{dict_index}")?;
1285 impl<V, S> Header for ValueLabelRecord<V, S>
1290 fn offsets(&self) -> Range<u64> {
1291 self.offsets.clone()
1295 impl<V, S> ValueLabelRecord<V, S>
1300 /// Maximum number of value labels in a record.
1301 pub const MAX_LABELS: u32 = u32::MAX / 8;
1303 /// Maximum number of variable indexes in a record.
1304 pub const MAX_INDEXES: u32 = u32::MAX / 8;
1307 impl ValueLabelRecord<RawStr<8>, RawString> {
1308 fn read<R: Read + Seek>(
1311 var_types: &[VarType],
1312 warn: &Box<dyn Fn(Error)>,
1313 ) -> Result<Option<Record>, Error> {
1314 let label_offset = r.stream_position()?;
1315 let n: u32 = endian.parse(read_bytes(r)?);
1316 if n > Self::MAX_LABELS {
1317 return Err(Error::BadNumberOfValueLabels {
1318 offset: label_offset,
1320 max: Self::MAX_LABELS,
1324 let mut labels = Vec::new();
1326 let value = UntypedValue(read_bytes(r)?);
1327 let label_len: u8 = endian.parse(read_bytes(r)?);
1328 let label_len = label_len as usize;
1329 let padded_len = Integer::next_multiple_of(&(label_len + 1), &8);
1331 let mut label = read_vec(r, padded_len - 1)?;
1332 label.truncate(label_len);
1333 labels.push((value, RawString(label)));
1336 let index_offset = r.stream_position()?;
1337 let rec_type: u32 = endian.parse(read_bytes(r)?);
1339 return Err(Error::ExpectedVarIndexRecord {
1340 offset: index_offset,
1345 let n: u32 = endian.parse(read_bytes(r)?);
1346 if n > Self::MAX_INDEXES {
1347 return Err(Error::TooManyVarIndexes {
1348 offset: index_offset,
1350 max: Self::MAX_INDEXES,
1354 let index_offset = r.stream_position()?;
1355 let mut dict_indexes = Vec::with_capacity(n as usize);
1356 let mut invalid_indexes = Vec::new();
1358 let index: u32 = endian.parse(read_bytes(r)?);
1359 if index == 0 || index as usize > var_types.len() {
1360 dict_indexes.push(index);
1362 invalid_indexes.push(index);
1365 if !invalid_indexes.is_empty() {
1366 warn(Error::InvalidVarIndexes {
1367 offset: index_offset,
1368 max: var_types.len(),
1369 invalid: invalid_indexes,
1373 let Some(&first_index) = dict_indexes.first() else {
1374 warn(Error::NoVarIndexes {
1375 offset: index_offset,
1379 let var_type = var_types[first_index as usize - 1];
1380 let mut wrong_type_indexes = Vec::new();
1381 dict_indexes.retain(|&index| {
1382 if var_types[index as usize - 1] != var_type {
1383 wrong_type_indexes.push(index);
1389 if !wrong_type_indexes.is_empty() {
1390 warn(Error::MixedVarTypes {
1391 offset: index_offset,
1393 wrong_types: wrong_type_indexes,
1399 .map(|(value, label)| ValueLabel {
1400 value: Value::from_raw(&value, var_type, endian),
1405 let end_offset = r.stream_position()?;
1406 Ok(Some(Record::ValueLabel(ValueLabelRecord {
1407 offsets: label_offset..end_offset,
1415 #[derive(Clone, Debug)]
1416 pub struct DocumentRecord {
1417 pub offsets: Range<u64>,
1419 /// The document, as an array of 80-byte lines.
1420 pub lines: Vec<DocumentLine>,
1423 pub type DocumentLine = RawStr<{ DocumentRecord::LINE_LEN }>;
1425 impl DocumentRecord {
1426 /// Length of a line in a document. Document lines are fixed-length and
1427 /// padded on the right with spaces.
1428 pub const LINE_LEN: usize = 80;
1430 /// Maximum number of lines we will accept in a document. This is simply
1431 /// the maximum number that will fit in a 32-bit space.
1432 pub const MAX_LINES: usize = i32::MAX as usize / Self::LINE_LEN;
1434 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Record, Error> {
1435 let start_offset = r.stream_position()?;
1436 let n: u32 = endian.parse(read_bytes(r)?);
1438 if n > Self::MAX_LINES {
1439 Err(Error::BadDocumentLength {
1440 offset: start_offset,
1442 max: Self::MAX_LINES,
1445 let mut lines = Vec::with_capacity(n);
1447 lines.push(RawStr::<{ DocumentRecord::LINE_LEN }>(read_bytes(r)?));
1449 let end_offset = r.stream_position()?;
1450 Ok(Record::Document(DocumentRecord {
1451 offsets: start_offset..end_offset,
1458 impl Header for DocumentRecord {
1459 fn offsets(&self) -> Range<u64> {
1460 self.offsets.clone()
1464 trait ExtensionRecord {
1466 const SIZE: Option<u32>;
1467 const COUNT: Option<u32>;
1468 const NAME: &'static str;
1469 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error>;
1472 #[derive(Clone, Debug)]
1473 pub struct IntegerInfoRecord {
1474 pub offsets: Range<u64>,
1475 pub version: (i32, i32, i32),
1476 pub machine_code: i32,
1477 pub floating_point_rep: i32,
1478 pub compression_code: i32,
1479 pub endianness: i32,
1480 pub character_code: i32,
1483 impl ExtensionRecord for IntegerInfoRecord {
1484 const SUBTYPE: u32 = 3;
1485 const SIZE: Option<u32> = Some(4);
1486 const COUNT: Option<u32> = Some(8);
1487 const NAME: &'static str = "integer record";
1489 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1490 ext.check_size::<Self>()?;
1492 let mut input = &ext.data[..];
1493 let data: Vec<i32> = (0..8)
1494 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1496 Ok(Record::IntegerInfo(IntegerInfoRecord {
1497 offsets: ext.offsets.clone(),
1498 version: (data[0], data[1], data[2]),
1499 machine_code: data[3],
1500 floating_point_rep: data[4],
1501 compression_code: data[5],
1502 endianness: data[6],
1503 character_code: data[7],
1508 #[derive(Clone, Debug)]
1509 pub struct FloatInfoRecord {
1515 impl ExtensionRecord for FloatInfoRecord {
1516 const SUBTYPE: u32 = 4;
1517 const SIZE: Option<u32> = Some(8);
1518 const COUNT: Option<u32> = Some(3);
1519 const NAME: &'static str = "floating point record";
1521 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1522 ext.check_size::<Self>()?;
1524 let mut input = &ext.data[..];
1525 let data: Vec<f64> = (0..3)
1526 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1528 Ok(Record::FloatInfo(FloatInfoRecord {
1536 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1537 pub enum CategoryLabels {
1542 #[derive(Clone, Debug)]
1543 pub enum MultipleResponseType {
1546 labels: CategoryLabels,
1551 impl MultipleResponseType {
1552 fn parse(input: &[u8]) -> Result<(MultipleResponseType, &[u8]), Error> {
1553 let (mr_type, input) = match input.split_first() {
1554 Some((b'C', input)) => (MultipleResponseType::MultipleCategory, input),
1555 Some((b'D', input)) => {
1556 let (value, input) = parse_counted_string(input)?;
1558 MultipleResponseType::MultipleDichotomy {
1560 labels: CategoryLabels::VarLabels,
1565 Some((b'E', input)) => {
1566 let (labels, input) = if let Some(rest) = input.strip_prefix(b" 1 ") {
1567 (CategoryLabels::CountedValues, rest)
1568 } else if let Some(rest) = input.strip_prefix(b" 11 ") {
1569 (CategoryLabels::VarLabels, rest)
1571 return Err(Error::TBD);
1573 let (value, input) = parse_counted_string(input)?;
1575 MultipleResponseType::MultipleDichotomy { value, labels },
1579 _ => return Err(Error::TBD),
1581 Ok((mr_type, input))
1585 #[derive(Clone, Debug)]
1586 pub struct MultipleResponseSet {
1587 pub name: RawString,
1588 pub label: RawString,
1589 pub mr_type: MultipleResponseType,
1590 pub short_names: Vec<RawString>,
1593 impl MultipleResponseSet {
1594 fn parse(input: &[u8]) -> Result<(MultipleResponseSet, &[u8]), Error> {
1595 let Some(equals) = input.iter().position(|&b| b == b'=') else {
1596 return Err(Error::TBD);
1598 let (name, input) = input.split_at(equals);
1599 let (mr_type, input) = MultipleResponseType::parse(input)?;
1600 let Some(input) = input.strip_prefix(b" ") else {
1601 return Err(Error::TBD);
1603 let (label, mut input) = parse_counted_string(input)?;
1604 let mut vars = Vec::new();
1605 while input.first() != Some(&b'\n') {
1606 match input.split_first() {
1607 Some((b' ', rest)) => {
1608 let Some(length) = rest.iter().position(|b| b" \n".contains(b)) else {
1609 return Err(Error::TBD);
1611 let (var, rest) = rest.split_at(length);
1612 if !var.is_empty() {
1613 vars.push(var.into());
1617 _ => return Err(Error::TBD),
1620 while input.first() == Some(&b'\n') {
1621 input = &input[1..];
1624 MultipleResponseSet {
1635 #[derive(Clone, Debug)]
1636 pub struct MultipleResponseRecord(pub Vec<MultipleResponseSet>);
1638 impl ExtensionRecord for MultipleResponseRecord {
1639 const SUBTYPE: u32 = 7;
1640 const SIZE: Option<u32> = Some(1);
1641 const COUNT: Option<u32> = None;
1642 const NAME: &'static str = "multiple response set record";
1644 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Error> {
1645 ext.check_size::<Self>()?;
1647 let mut input = &ext.data[..];
1648 let mut sets = Vec::new();
1649 while !input.is_empty() {
1650 let (set, rest) = MultipleResponseSet::parse(input)?;
1654 Ok(Record::MultipleResponse(MultipleResponseRecord(sets)))
1658 fn parse_counted_string(input: &[u8]) -> Result<(RawString, &[u8]), Error> {
1659 let Some(space) = input.iter().position(|&b| b == b' ') else {
1660 return Err(Error::TBD);
1662 let Ok(length) = from_utf8(&input[..space]) else {
1663 return Err(Error::TBD);
1665 let Ok(length): Result<usize, _> = length.parse() else {
1666 return Err(Error::TBD);
1669 let input = &input[space + 1..];
1670 if input.len() < length {
1671 return Err(Error::TBD);
1674 let (string, rest) = input.split_at(length);
1675 Ok((string.into(), rest))
1678 #[derive(Clone, Debug)]
1679 pub struct VarDisplayRecord(pub Vec<u32>);
1681 impl ExtensionRecord for VarDisplayRecord {
1682 const SUBTYPE: u32 = 11;
1683 const SIZE: Option<u32> = Some(4);
1684 const COUNT: Option<u32> = None;
1685 const NAME: &'static str = "variable display record";
1687 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1688 ext.check_size::<Self>()?;
1690 let mut input = &ext.data[..];
1691 let display = (0..ext.count)
1692 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1694 Ok(Record::VarDisplay(VarDisplayRecord(display)))
1698 #[derive(Clone, Debug)]
1699 pub struct LongStringMissingValues {
1701 pub var_name: RawString,
1704 pub missing_values: MissingValues<RawStr<8>>,
1707 #[derive(Clone, Debug)]
1708 pub struct LongStringMissingValueRecord(pub Vec<LongStringMissingValues>);
1710 impl ExtensionRecord for LongStringMissingValueRecord {
1711 const SUBTYPE: u32 = 22;
1712 const SIZE: Option<u32> = Some(1);
1713 const COUNT: Option<u32> = None;
1714 const NAME: &'static str = "long string missing values record";
1716 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1717 ext.check_size::<Self>()?;
1719 let mut input = &ext.data[..];
1720 let mut missing_value_set = Vec::new();
1721 while !input.is_empty() {
1722 let var_name = read_string(&mut input, endian)?;
1723 let n_missing_values: u8 = endian.parse(read_bytes(&mut input)?);
1724 let value_len: u32 = endian.parse(read_bytes(&mut input)?);
1726 let offset = (ext.data.len() - input.len() - 8) as u64 + ext.offsets.start;
1727 return Err(Error::BadLongMissingValueLength {
1728 record_offset: ext.offsets.start,
1733 let mut values = Vec::new();
1734 for i in 0..n_missing_values {
1735 let value: [u8; 8] = read_bytes(&mut input)?;
1736 let numeric_value: u64 = endian.parse(value);
1737 let value = if i > 0 && numeric_value == 8 {
1738 // Tolerate files written by old, buggy versions of PSPP
1739 // where we believed that the value_length was repeated
1740 // before each missing value.
1741 read_bytes(&mut input)?
1745 values.push(Value::String(RawStr(value)));
1747 let missing_values = MissingValues {
1751 missing_value_set.push(LongStringMissingValues {
1756 Ok(Record::LongStringMissingValues(
1757 LongStringMissingValueRecord(missing_value_set),
1762 #[derive(Clone, Debug)]
1763 pub struct EncodingRecord(pub String);
1765 impl ExtensionRecord for EncodingRecord {
1766 const SUBTYPE: u32 = 20;
1767 const SIZE: Option<u32> = Some(1);
1768 const COUNT: Option<u32> = None;
1769 const NAME: &'static str = "encoding record";
1771 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Error> {
1772 ext.check_size::<Self>()?;
1774 Ok(Record::Encoding(EncodingRecord(
1775 String::from_utf8(ext.data.clone()).map_err(|_| Error::BadEncodingName {
1776 offset: ext.offsets.start,
1782 #[derive(Copy, Clone, Debug)]
1783 pub struct NumberOfCasesRecord {
1784 /// Always observed as 1.
1787 /// Number of cases.
1791 impl ExtensionRecord for NumberOfCasesRecord {
1792 const SUBTYPE: u32 = 16;
1793 const SIZE: Option<u32> = Some(8);
1794 const COUNT: Option<u32> = Some(2);
1795 const NAME: &'static str = "extended number of cases record";
1797 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
1798 ext.check_size::<Self>()?;
1800 let mut input = &ext.data[..];
1801 let one = endian.parse(read_bytes(&mut input)?);
1802 let n_cases = endian.parse(read_bytes(&mut input)?);
1804 Ok(Record::NumberOfCases(NumberOfCasesRecord { one, n_cases }))
1808 #[derive(Clone, Debug)]
1809 pub struct TextRecord {
1810 pub offsets: Range<u64>,
1812 /// The text content of the record.
1813 pub text: RawString,
1816 impl From<Extension> for TextRecord {
1817 fn from(source: Extension) -> Self {
1819 offsets: source.offsets,
1820 text: source.data.into(),
1825 #[derive(Clone, Debug)]
1826 pub struct Extension {
1827 pub offsets: Range<u64>,
1832 /// Size of each data element.
1835 /// Number of data elements.
1838 /// `size * count` bytes of data.
1843 fn check_size<E: ExtensionRecord>(&self) -> Result<(), Error> {
1844 if let Some(expected_size) = E::SIZE {
1845 if self.size != expected_size {
1846 return Err(Error::BadRecordSize {
1847 offset: self.offsets.start,
1848 record: E::NAME.into(),
1854 if let Some(expected_count) = E::COUNT {
1855 if self.count != expected_count {
1856 return Err(Error::BadRecordCount {
1857 offset: self.offsets.start,
1858 record: E::NAME.into(),
1867 fn read<R: Read + Seek>(
1870 warn: &Box<dyn Fn(Error)>,
1871 ) -> Result<Option<Record>, Error> {
1872 let subtype = endian.parse(read_bytes(r)?);
1873 let header_offset = r.stream_position()?;
1874 let size: u32 = endian.parse(read_bytes(r)?);
1875 let count = endian.parse(read_bytes(r)?);
1876 let Some(product) = size.checked_mul(count) else {
1877 return Err(Error::ExtensionRecordTooLarge {
1878 offset: header_offset,
1884 let start_offset = r.stream_position()?;
1885 let data = read_vec(r, product as usize)?;
1886 let end_offset = start_offset + product as u64;
1887 let extension = Extension {
1888 offsets: start_offset..end_offset,
1894 let result = match subtype {
1895 IntegerInfoRecord::SUBTYPE => IntegerInfoRecord::parse(&extension, endian),
1896 FloatInfoRecord::SUBTYPE => FloatInfoRecord::parse(&extension, endian),
1897 VarDisplayRecord::SUBTYPE => VarDisplayRecord::parse(&extension, endian),
1898 MultipleResponseRecord::SUBTYPE | 19 => {
1899 MultipleResponseRecord::parse(&extension, endian)
1901 LongStringValueLabelRecord::SUBTYPE => {
1902 LongStringValueLabelRecord::parse(&extension, endian)
1904 EncodingRecord::SUBTYPE => EncodingRecord::parse(&extension, endian),
1905 NumberOfCasesRecord::SUBTYPE => NumberOfCasesRecord::parse(&extension, endian),
1906 5 => Ok(Record::VariableSets(extension.into())),
1907 10 => Ok(Record::ProductInfo(extension.into())),
1908 13 => Ok(Record::LongNames(extension.into())),
1909 14 => Ok(Record::VeryLongStrings(extension.into())),
1910 17 => Ok(Record::FileAttributes(extension.into())),
1911 18 => Ok(Record::VariableAttributes(extension.into())),
1912 _ => Ok(Record::OtherExtension(extension)),
1915 Ok(result) => Ok(Some(result)),
1924 #[derive(Clone, Debug)]
1925 pub struct ZHeader {
1926 /// File offset to the start of the record.
1929 /// File offset to the ZLIB data header.
1930 pub zheader_offset: u64,
1932 /// File offset to the ZLIB trailer.
1933 pub ztrailer_offset: u64,
1935 /// Length of the ZLIB trailer in bytes.
1936 pub ztrailer_len: u64,
1940 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZHeader, Error> {
1941 let offset = r.stream_position()?;
1942 let zheader_offset: u64 = endian.parse(read_bytes(r)?);
1943 let ztrailer_offset: u64 = endian.parse(read_bytes(r)?);
1944 let ztrailer_len: u64 = endian.parse(read_bytes(r)?);
1955 #[derive(Clone, Debug)]
1956 pub struct ZTrailer {
1957 /// File offset to the start of the record.
1960 /// Compression bias as a negative integer, e.g. -100.
1963 /// Always observed as zero.
1966 /// Uncompressed size of each block, except possibly the last. Only
1967 /// `0x3ff000` has been observed so far.
1968 pub block_size: u32,
1970 /// Block descriptors, always `(ztrailer_len - 24) / 24)` of them.
1971 pub blocks: Vec<ZBlock>,
1974 #[derive(Clone, Debug)]
1976 /// Offset of block of data if simple compression were used.
1977 pub uncompressed_ofs: u64,
1979 /// Actual offset within the file of the compressed data block.
1980 pub compressed_ofs: u64,
1982 /// The number of bytes in this data block after decompression. This is
1983 /// `block_size` in every data block but the last, which may be smaller.
1984 pub uncompressed_size: u32,
1986 /// The number of bytes in this data block, as stored compressed in this
1988 pub compressed_size: u32,
1992 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZBlock, Error> {
1994 uncompressed_ofs: endian.parse(read_bytes(r)?),
1995 compressed_ofs: endian.parse(read_bytes(r)?),
1996 uncompressed_size: endian.parse(read_bytes(r)?),
1997 compressed_size: endian.parse(read_bytes(r)?),
2003 fn read<R: Read + Seek>(
2008 ) -> Result<Option<ZTrailer>, Error> {
2009 let start_offset = reader.stream_position()?;
2010 if reader.seek(SeekFrom::Start(ztrailer_ofs)).is_err() {
2013 let int_bias = endian.parse(read_bytes(reader)?);
2014 let zero = endian.parse(read_bytes(reader)?);
2015 let block_size = endian.parse(read_bytes(reader)?);
2016 let n_blocks: u32 = endian.parse(read_bytes(reader)?);
2017 let expected_n_blocks = (ztrailer_len - 24) / 24;
2018 if n_blocks as u64 != expected_n_blocks {
2019 return Err(Error::BadZlibTrailerNBlocks {
2020 offset: ztrailer_ofs,
2026 let blocks = (0..n_blocks)
2027 .map(|_| ZBlock::read(reader, endian))
2028 .collect::<Result<Vec<_>, _>>()?;
2029 reader.seek(SeekFrom::Start(start_offset))?;
2031 offset: ztrailer_ofs,
2040 fn try_read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<Option<[u8; N]>, IoError> {
2041 let mut buf = [0; N];
2042 let n = r.read(&mut buf)?;
2045 r.read_exact(&mut buf[n..])?;
2053 fn read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<[u8; N], IoError> {
2054 let mut buf = [0; N];
2055 r.read_exact(&mut buf)?;
2059 fn read_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<u8>, IoError> {
2060 let mut vec = vec![0; n];
2061 r.read_exact(&mut vec)?;
2065 fn read_string<R: Read>(r: &mut R, endian: Endian) -> Result<RawString, IoError> {
2066 let length: u32 = endian.parse(read_bytes(r)?);
2067 Ok(read_vec(r, length as usize)?.into())
2070 #[derive(Clone, Debug)]
2071 pub struct LongStringValueLabels {
2072 pub var_name: RawString,
2075 /// `(value, label)` pairs, where each value is `width` bytes.
2076 pub labels: Vec<(RawString, RawString)>,
2079 #[derive(Clone, Debug)]
2080 pub struct LongStringValueLabelRecord(pub Vec<LongStringValueLabels>);
2082 impl ExtensionRecord for LongStringValueLabelRecord {
2083 const SUBTYPE: u32 = 21;
2084 const SIZE: Option<u32> = Some(1);
2085 const COUNT: Option<u32> = None;
2086 const NAME: &'static str = "long string value labels record";
2088 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Error> {
2089 ext.check_size::<Self>()?;
2091 let mut input = &ext.data[..];
2092 let mut label_set = Vec::new();
2093 while !input.is_empty() {
2094 let var_name = read_string(&mut input, endian)?;
2095 let width: u32 = endian.parse(read_bytes(&mut input)?);
2096 let n_labels: u32 = endian.parse(read_bytes(&mut input)?);
2097 let mut labels = Vec::new();
2098 for _ in 0..n_labels {
2099 let value = read_string(&mut input, endian)?;
2100 let label = read_string(&mut input, endian)?;
2101 labels.push((value, label));
2103 label_set.push(LongStringValueLabels {
2109 Ok(Record::LongStringValueLabels(LongStringValueLabelRecord(