1 #![allow(unused_variables)]
2 use endian::{Endian, Parse};
3 use std::io::{BufReader, Error as IoError, Read};
8 #[derive(Error, Debug)]
10 #[error("Not an SPSS system file")]
13 #[error("I/O error ({source})")]
19 #[error("Invalid SAV compression code {0}")]
20 InvalidSavCompression(u32),
22 #[error("Invalid ZSAV compression code {0}")]
23 InvalidZsavCompression(u32),
26 #[derive(Error, Debug)]
28 #[error("Unexpected floating-point bias {0} or unrecognized floating-point format.")]
32 #[derive(Copy, Clone, Debug)]
33 pub enum Compression {
38 pub struct Reader<R: Read> {
42 pub const ASCII_MAGIC: &[u8; 4] = b"$FL2";
43 pub const ASCII_ZMAGIC: &[u8; 4] = b"$FL3";
44 pub const EBCDIC_MAGIC: &[u8; 4] = &[0x5b, 0xc6, 0xd3, 0xf2];
46 pub struct FileHeader {
47 /// First 4 bytes of the file, one of `ASCII_MAGIC`, `ASCII_ZMAGIC`, and
51 /// True if `magic` indicates that this file contained zlib-compressed data.
54 /// True if `magic` indicates that this file contained EBCDIC data.
57 /// 0-based variable index of the weight variable, or `None` if the file is
59 pub weight_index: Option<u32>,
61 /// Number of variable positions, or `None` if the value in the file is
62 /// questionably trustworthy.
63 pub nominal_case_size: Option<u32>,
65 /// `dd mmm yy` in the file's encoding.
66 pub creation_date: [u8; 9],
68 /// `HH:MM:SS` in the file's encoding.
69 pub creation_time: [u8; 8],
71 /// Eye-catcher string, then product name, in the file's encoding. Padded
72 /// on the right with spaces.
73 pub eye_catcher: [u8; 60],
75 /// File label, in the file's encoding. Padded on the right with spaces.
76 pub file_label: [u8; 64],
79 impl<R: Read> Reader<R> {
80 pub fn new(r: R, warn: impl Fn(Warning)) -> Result<Reader<R>, Error> {
81 let mut r = BufReader::new(r);
83 let magic: [u8; 4] = read_bytes(&mut r)?;
84 let (is_zsav, is_ebcdic) = match &magic {
85 ASCII_MAGIC => (false, false),
86 ASCII_ZMAGIC => (true, false),
87 EBCDIC_MAGIC => (false, true),
88 _ => return Err(Error::NotASystemFile),
91 let eye_catcher: [u8; 60] = read_bytes(&mut r)?;
92 let layout_code: [u8; 4] = read_bytes(&mut r)?;
93 let endianness = Endian::identify_u32(2, layout_code)
94 .or_else(|| Endian::identify_u32(2, layout_code))
95 .ok_or_else(|| Error::NotASystemFile)?;
97 let nominal_case_size: u32 = endianness.parse(read_bytes(&mut r)?);
98 let nominal_case_size = (nominal_case_size <= u32::MAX / 32).then_some(nominal_case_size);
100 let compression_code: u32 = endianness.parse(read_bytes(&mut r)?);
101 let compression = match (is_zsav, compression_code) {
103 (false, 1) => Some(Compression::Simple),
104 (true, 2) => Some(Compression::ZLib),
105 (false, code) => return Err(Error::InvalidSavCompression(code)),
106 (true, code) => return Err(Error::InvalidZsavCompression(code)),
109 let weight_index: u32 = endianness.parse(read_bytes(&mut r)?);
110 let weight_index = (weight_index > 0).then_some(weight_index - 1);
112 let n_cases: u32 = endianness.parse(read_bytes(&mut r)?);
113 let n_cases = (n_cases <= u32::MAX / 4).then_some(n_cases);
115 let bias: f64 = endianness.parse(read_bytes(&mut r)?);
117 warn(Warning::UnexpectedBias(bias))
120 let creation_date: [u8; 9] = read_bytes(&mut r)?;
121 let creation_time: [u8; 8] = read_bytes(&mut r)?;
122 let file_label: [u8; 64] = read_bytes(&mut r)?;
123 let _: [u8; 3] = read_bytes(&mut r)?;
125 let header = FileHeader {
141 fn read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<[u8; N], IoError> {
142 let mut buf = [0; N];
143 r.read_exact(&mut buf)?;
148 fn trim_end(mut s: Vec<u8>, c: u8) -> Vec<u8> {
149 while s.last() == Some(&c) {
155 fn skip_bytes<R: Read>(r: &mut R, mut n: u64) -> Result<(), IoError> {
156 let mut buf = [0; 1024];
158 let chunk = u64::min(n, buf.len() as u64);
159 r.read_exact(&mut buf[0..chunk as usize])?;