-pub struct Reader<R: Read> {
- r: BufReader<R>,
-}
-
-pub const ASCII_MAGIC: &[u8; 4] = b"$FL2";
-pub const ASCII_ZMAGIC: &[u8; 4] = b"$FL3";
-pub const EBCDIC_MAGIC: &[u8; 4] = &[0x5b, 0xc6, 0xd3, 0xf2];
-
-pub struct FileHeader {
- /// First 4 bytes of the file, one of `ASCII_MAGIC`, `ASCII_ZMAGIC`, and
- /// `EBCDIC_MAGIC`.
- pub magic: [u8; 4],
-
- /// True if `magic` indicates that this file contained zlib-compressed data.
- pub is_zsav: bool,
-
- /// True if `magic` indicates that this file contained EBCDIC data.
- pub is_ebcdic: bool,
-
- /// 0-based variable index of the weight variable, or `None` if the file is
- /// unweighted.
- pub weight_index: Option<u32>,
-
- /// Number of variable positions, or `None` if the value in the file is
- /// questionably trustworthy.
- pub nominal_case_size: Option<u32>,
-
- /// `dd mmm yy` in the file's encoding.
- pub creation_date: [u8; 9],
-
- /// `HH:MM:SS` in the file's encoding.
- pub creation_time: [u8; 8],
-
- /// Eye-catcher string, then product name, in the file's encoding. Padded
- /// on the right with spaces.
- pub eye_catcher: [u8; 60],
-
- /// File label, in the file's encoding. Padded on the right with spaces.
- pub file_label: [u8; 64],
-}
-
-impl<R: Read> Reader<R> {
- pub fn new(r: R, warn: impl Fn(Warning)) -> Result<Reader<R>, Error> {
- let mut r = BufReader::new(r);
-
- let magic: [u8; 4] = read_bytes(&mut r)?;
- let (is_zsav, is_ebcdic) = match &magic {
- ASCII_MAGIC => (false, false),
- ASCII_ZMAGIC => (true, false),
- EBCDIC_MAGIC => (false, true),
- _ => return Err(Error::NotASystemFile),
- };
-
- let eye_catcher: [u8; 60] = read_bytes(&mut r)?;
- let layout_code: [u8; 4] = read_bytes(&mut r)?;
- let endianness = Endian::identify_u32(2, layout_code)
- .or_else(|| Endian::identify_u32(2, layout_code))
- .ok_or_else(|| Error::NotASystemFile)?;
-
- let nominal_case_size: u32 = endianness.parse(read_bytes(&mut r)?);
- let nominal_case_size = (nominal_case_size <= u32::MAX / 32).then_some(nominal_case_size);
-
- let compression_code: u32 = endianness.parse(read_bytes(&mut r)?);
- let compression = match (is_zsav, compression_code) {
- (false, 0) => None,
- (false, 1) => Some(Compression::Simple),
- (true, 2) => Some(Compression::ZLib),
- (false, code) => return Err(Error::InvalidSavCompression(code)),
- (true, code) => return Err(Error::InvalidZsavCompression(code)),
- };
-
- let weight_index: u32 = endianness.parse(read_bytes(&mut r)?);
- let weight_index = (weight_index > 0).then_some(weight_index - 1);
-
- let n_cases: u32 = endianness.parse(read_bytes(&mut r)?);
- let n_cases = (n_cases <= u32::MAX / 4).then_some(n_cases);
-
- let bias: f64 = endianness.parse(read_bytes(&mut r)?);
- if bias != 100.0 {
- warn(Warning::UnexpectedBias(bias))
- }
-
- let creation_date: [u8; 9] = read_bytes(&mut r)?;
- let creation_time: [u8; 8] = read_bytes(&mut r)?;
- let file_label: [u8; 64] = read_bytes(&mut r)?;
- let _: [u8; 3] = read_bytes(&mut r)?;
-
- let header = FileHeader {
- magic,
- is_zsav,
- is_ebcdic,
- weight_index,
- nominal_case_size,
- creation_date,
- creation_time,
- eye_catcher,
- file_label,
- };
-
- Ok(Reader { r })
- }