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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Simple tools for reading and writing crc-checked frames of bytes.
//! * Uses crc32fast for a 4-byte crc
//! * Uses varint for frame sizing
//! * Tested against libfuzzer
//!
//! The `varint` module is also public for direct use.

pub mod varint;

use std::fs;
use std::io;
use std::os::unix::fs::FileExt;

use crc32fast::Hasher;
use fault_injection::{annotate, fallible, maybe};

const MAX_HEADER_SIZE: usize = 13;

/// Write a crc'd frame into the provided `Write` instance. Returns the
/// number of bytes written in total, including the varint size and crc.
///
/// # Examples
///
/// ```
/// use crc_frame::{write_frame, parse_frame};
///
/// let data = b"12345";
///
/// let mut buf = vec![];
///
/// write_frame(data, &mut buf).unwrap();
///
/// let (begin, end) = parse_frame(&buf).unwrap();
///
/// assert_eq!(&buf[begin..end], data);
/// ```
pub fn write_frame<W: io::Write>(buf: &[u8], mut into: W) -> io::Result<usize> {
    let (header_buf, header_end_offset) = frame_header(buf);

    fallible!(into.write_all(&header_buf[..header_end_offset]));
    fallible!(into.write_all(buf));

    Ok(header_end_offset + buf.len())
}

/// Write a crc'd frame into the provided `File` at the given offset.
/// Returns the number of bytes written in total, including the varint size and crc.
pub fn write_frame_at(buf: &[u8], file: &fs::File, at: u64) -> io::Result<usize> {
    let (header_buf, header_end_offset) = frame_header(buf);
    let header = &header_buf[..header_end_offset];

    fallible!(file.write_all_at(header, at));
    fallible!(file.write_all_at(buf, at + header.len() as u64));

    Ok(header_end_offset + buf.len())
}

fn uninit_boxed_slice(len: usize) -> Box<[u8]> {
    use std::alloc::{alloc, Layout};

    let layout = Layout::array::<u8>(len).unwrap();

    unsafe {
        let ptr = alloc(layout);
        let slice = std::slice::from_raw_parts_mut(ptr, len);
        Box::from_raw(slice)
    }
}

/// Read a frame out of the provided `Read` implementation.
pub fn read_frame<R: io::Read>(mut from: R, max_len: usize) -> io::Result<Box<[u8]>> {
    let header = &mut [0; MAX_HEADER_SIZE];

    match maybe!(from.read_exact(header)) {
        Ok(_) => {}
        Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {}
        Err(e) => return Err(e),
    }

    let (buf_len_u64, varint_len) = varint::deserialize(header)?;

    if buf_len_u64 > max_len as u64 {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "encountered a varint len that is larger than the \
            max_len, and is possibly corrupt or was written with \
            a different configuration.",
        )));
    }

    // at this point we know that the buffer len fits in a usize
    let buf_len = usize::try_from(buf_len_u64).unwrap();

    let mut buf = uninit_boxed_slice(buf_len);

    let crc_begin = varint_len;
    let crc_end = crc_begin + 4;
    let crc_expected = &header[crc_begin..crc_end];

    let potential_inline_len = MAX_HEADER_SIZE - crc_end;
    let header_buf_len = potential_inline_len.min(buf_len);
    let header_buf_begin = crc_end;
    let header_buf_end = header_buf_begin + header_buf_len;

    buf[..header_buf_len].copy_from_slice(&header[header_buf_begin..header_buf_end]);

    let remainder_buf_begin = header_buf_len;

    fallible!(from.read_exact(&mut buf[remainder_buf_begin..]));

    let crc_actual = hash(&buf, &header[..varint_len]);

    if crc_actual != crc_expected {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "input buffer crc does not match expected crc",
        )));
    }

    Ok(buf)
}

/// Read a frame out of the provided `File`
pub fn read_frame_at(file: &fs::File, at: u64, max_len: usize) -> io::Result<Box<[u8]>> {
    const FIRST_READ_SIZE: usize = 512;

    let header = &mut [0; FIRST_READ_SIZE];

    match maybe!(file.read_exact_at(header, at)) {
        Ok(_) => {}
        Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {}
        Err(e) => return Err(e),
    }

    let (buf_len_u64, varint_len) = varint::deserialize(header)?;

    if buf_len_u64 > max_len as u64 {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "encountered a varint len that is larger than the \
            max_len, and is possibly corrupt or was written with \
            a different configuration.",
        )));
    }

    // at this point we know that the buffer len fits in a usize
    let buf_len = usize::try_from(buf_len_u64).unwrap();

    let mut buf = uninit_boxed_slice(buf_len);

    let crc_begin = varint_len;
    let crc_end = crc_begin + 4;
    let crc_expected = &header[crc_begin..crc_end];

    let potential_inline_len = FIRST_READ_SIZE - crc_end;
    let header_buf_len = potential_inline_len.min(buf_len);
    let header_buf_begin = crc_end;
    let header_buf_end = header_buf_begin + header_buf_len;

    buf[..header_buf_len].copy_from_slice(&header[header_buf_begin..header_buf_end]);

    let remainder_buf_begin = header_buf_len;

    fallible!(file.read_exact_at(&mut buf[remainder_buf_begin..], at + FIRST_READ_SIZE as u64));

    let crc_actual = hash(&buf, &header[..varint_len]);

    if crc_actual != crc_expected {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "input buffer crc does not match expected crc",
        )));
    }

    Ok(buf)
}

fn hash(buf: &[u8], len_bytes: &[u8]) -> [u8; 4] {
    let mut hasher = Hasher::new();
    hasher.update(&len_bytes);
    hasher.update(&buf);

    // We XOR one byte in the crc to make it non-zero
    // for empty buffers, which forces bit flips to
    // materialize in a crc mismatch more often.
    (hasher.finalize() ^ 0xFF).to_le_bytes()
}

/// Return an array which contains the crc and varint for
/// a given buffer, and a `usize` that is the length of
/// the provided array which corresponds to the valid
/// varint and crc. Returns an array instead of a Vec<u8>
/// to avoid allocations.
///
/// # Examples
/// ```
/// use crc_frame::frame_header;
///
/// let buf = b"12345";
///
/// let (header_buf, header_len) = frame_header(buf);
///
/// let mut out = vec![];
/// out.extend_from_slice(&header_buf[..header_len]);
/// out.extend_from_slice(buf);
/// ```
pub fn frame_header(buf: &[u8]) -> ([u8; MAX_HEADER_SIZE], usize) {
    let mut header_buf = [0_u8; MAX_HEADER_SIZE];

    // write the buf len varint into the header buffer
    let bytes_for_varint = varint::serialize_into_buf(buf.len() as u64, &mut header_buf).unwrap();

    let crc_start = bytes_for_varint;
    let crc_end = bytes_for_varint + 4;

    let crc_bytes = hash(buf, &header_buf[..bytes_for_varint]);

    // write crc
    header_buf[crc_start..crc_end].copy_from_slice(&crc_bytes);

    (header_buf, crc_end)
}

/// Reads a header out of an arbitrary buffer, checks the crc,
/// and if the crc matches the corresponding bytes, returns
/// the start and end offsets in the buffer for the inner
/// bytes.
///
/// # Examples
///
/// ```
/// use crc_frame::{write_frame, parse_frame};
///
/// let data = b"12345";
///
/// let mut buf = vec![];
///
/// write_frame(data, &mut buf).unwrap();
///
/// let (begin, end) = parse_frame(&buf).unwrap();
///
/// assert_eq!(&buf[begin..end], data);
/// ```
pub fn parse_frame(buf: &[u8]) -> io::Result<(usize, usize)> {
    let (buf_len_u64, varint_len) = varint::deserialize(buf)?;

    let expected_len = buf.len() as u64 - (4 + varint_len as u64);
    if buf_len_u64 != expected_len {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "encountered a corrupt varint len or an input \
            buffer that does not contain the full frame",
        )));
    }

    // If we got this far, we know that buf_len (a u64) is convertible
    // to our platform's usize, because we know that it is less than the
    // size of the input buffer.

    let buf_len = usize::try_from(buf_len_u64).unwrap();

    let crc_begin = varint_len;
    let buf_begin = varint_len + 4;
    let buf_end = buf_begin + buf_len;

    let expected_crc: [u8; 4] = buf[crc_begin..buf_begin].try_into().unwrap();

    let actual_crc = hash(&buf[buf_begin..buf_end], &buf[..varint_len]);

    if actual_crc != expected_crc {
        return Err(annotate!(io::Error::new(
            io::ErrorKind::InvalidData,
            "input buffer crc does not match expected crc",
        )));
    }

    Ok((buf_begin, buf_end))
}