1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Implementation of the Apple Data Compression scheme in Rust
//!
//! ADC is a rather basic run length compression scheme. This library implements decompression only.
//!
//! # Example
//!
//! ```
//! use adc::AdcDecoder;
//!
//! let input: &[u8] = &[0x83, 0xfe, 0xed, 0xfa, 0xce, 0x00, 0x00, 0x40, 0x00, 0x06];
//! let mut d = AdcDecoder::new(input);
//! let mut data = vec![0; 11];
//! let bytes_out = match d.decompress_into(&mut data[..]) {
//!     Ok(val) => val,
//!     Err(err) => panic!("error: {:?}", err),
//! };
//! println!("{:?} bytes decompressed", bytes_out);
//! ````

extern crate bincode;

use std::io::prelude::*;

use bincode::Options;

#[derive(PartialEq, Debug)]
enum AdcChunkType {
    Plain,
    TwoByte,
    ThreeByte,
}

#[derive(PartialEq, Debug)]
struct AdcChunk {
    r#type: AdcChunkType,
    size: usize,
    offset: usize,
}

/// Main type for decompressing ADC data.
pub struct AdcDecoder<R> {
    input: R,
}

/// This type represents all possible errors that can occur when decompressing data.
#[derive(Debug, PartialEq)]
pub enum AdcError {
    /// The was an IO error while reading or writing.
    Io(String),
    /// The buffer was not large enough for the decompressed data.
    BufferTooSmall,
    /// The input is invalid.
    InvalidInput,
}

impl std::fmt::Display for AdcError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            AdcError::Io(ref err) => write!(fmt, "{}", err),
            AdcError::BufferTooSmall => write!(fmt, "output buffer too small"),
            AdcError::InvalidInput => write!(fmt, "invalid input data"),
        }
    }
}

impl<R: Read> AdcDecoder<R> {
    /// Create a new decoder instance from a readable input
    pub fn new(input: R) -> AdcDecoder<R> {
        AdcDecoder { input }
    }

    fn get_next_chunk(&mut self) -> Result<Option<AdcChunk>, AdcError> {
        let byte: u8 = match bincode::deserialize_from(&mut self.input) {
            Err(_) => return Ok(None), // reached eof
            Ok(val) => val,
        };
        let chunk_type = if (byte & 0x80) != 0 {
            AdcChunkType::Plain
        } else if (byte & 0x40) != 0 {
            AdcChunkType::ThreeByte
        } else {
            AdcChunkType::TwoByte
        };

        if chunk_type == AdcChunkType::Plain {
            return Ok(Some(AdcChunk {
                r#type: chunk_type,
                size: ((byte & 0x7f) + 1) as usize,
                offset: 0,
            }));
        } else if chunk_type == AdcChunkType::TwoByte {
            let byte2: u8 = match bincode::deserialize_from(&mut self.input) {
                Err(err) => return Err(AdcError::Io(format!("{}", err))),
                Ok(val) => val,
            };
            return Ok(Some(AdcChunk {
                r#type: chunk_type,
                size: (((byte & 0x3f) >> 2) + 3) as usize,
                offset: (((byte as usize) & 0x3) << 8) + byte2 as usize,
            }));
        } else {
            let offset: u16 = match bincode::DefaultOptions::new()
                .with_big_endian()
                .with_fixint_encoding()
                .deserialize_from(&mut self.input)
            {
                Err(err) => return Err(AdcError::Io(format!("{}", err))),
                Ok(val) => val,
            };
            return Ok(Some(AdcChunk {
                r#type: chunk_type,
                size: ((byte & 0x3f) + 4) as usize,
                offset: offset as usize,
            }));
        }
    }

    /// Decompress input into byte array
    pub fn decompress_into(&mut self, output: &mut [u8]) -> Result<usize, AdcError> {
        // ADC is basic run length compression. AdcChunkType::Plain chunk contains data, the other two
        // specify the run length and an optional offset.
        let mut cur_pos = 0;
        loop {
            // get next chunk or return if None
            let chunk = match self.get_next_chunk()? {
                None => return Ok(cur_pos),
                Some(val) => val,
            };

            if cur_pos + chunk.size > output.len() {
                return Err(AdcError::BufferTooSmall);
            }

            if chunk.r#type == AdcChunkType::Plain {
                // copy from input to output
                if let Err(err) = self
                    .input
                    .read_exact(&mut output[cur_pos..cur_pos + chunk.size])
                {
                    return Err(AdcError::Io(format!("{}", err)));
                }
                cur_pos += chunk.size;
            } else {
                if cur_pos == 0 || chunk.offset > cur_pos - 1 {
                    return Err(AdcError::InvalidInput);
                }
                // copy repeated bytes
                for _ in 0..chunk.size {
                    output[cur_pos] = output[cur_pos - chunk.offset - 1];
                    cur_pos += 1;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use AdcDecoder;
    use AdcError;

    #[test]
    fn all_types() {
        let input: &[u8] = &[0x83, 0xfe, 0xed, 0xfa, 0xce, 0x00, 0x00, 0x40, 0x00, 0x06];
        let output: &[u8] = &[
            0xfe, 0xed, 0xfa, 0xce, 0xce, 0xce, 0xce, 0xfe, 0xed, 0xfa, 0xce,
        ];
        let mut d = AdcDecoder::new(input);
        let mut data = vec![0; output.len()];
        let bytes_out = d.decompress_into(&mut data[..]).unwrap();
        assert_eq!(bytes_out, output.len());
        assert_eq!(output[..], data[..]);
    }

    #[test]
    fn invalid_input() {
        // offset is too big
        let input: &[u8] = &[0x83, 0xfe, 0xed, 0xfa, 0xce, 0x00, 0xff];
        let mut d = AdcDecoder::new(input);
        let mut data = vec![0; 10];
        let bytes_out = d.decompress_into(&mut data[..]);
        assert_eq!(bytes_out, Err(AdcError::InvalidInput));
    }

    #[test]
    fn invalid_input2() {
        // run-length chunk at position 0
        let input: &[u8] = &[0x00, 0x00];
        let mut d = AdcDecoder::new(input);
        let mut data = vec![0; 10];
        let bytes_out = d.decompress_into(&mut data[..]);
        assert_eq!(bytes_out, Err(AdcError::InvalidInput));
    }

    #[test]
    fn invalid_input3() {
        // missing 2nd byte
        let input: &[u8] = &[0x83, 0xfe, 0xed, 0xfa, 0xce, 0x00];
        let mut d = AdcDecoder::new(input);
        let mut data = vec![0; 10];
        let bytes_out = d.decompress_into(&mut data[..]);
        assert_eq!(
            bytes_out,
            Err(AdcError::Io(
                "io error: failed to fill whole buffer".to_string()
            ))
        );
    }

    #[test]
    fn empty() {
        let input: &[u8] = &[];
        let output: &[u8] = &[];
        let mut d = AdcDecoder::new(input);
        let mut data = vec![0; output.len()];
        let bytes_out = d.decompress_into(&mut data[..]).unwrap();
        assert_eq!(bytes_out, output.len());
        assert_eq!(output[..], data[..]);
    }
}