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
use std::io::Write;
use crate::Result;
use byteorder::{BigEndian, ByteOrder};
use tikv_util::box_err;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Version {
V1 = 1,
V2 = 2,
}
impl Version {
fn from(input: u8) -> Result<Version> {
match input {
1 => Ok(Version::V1),
2 => Ok(Version::V2),
_ => Err(box_err!("unknown version {:x}", input)),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Header {
version: Version,
crc32: u32,
size: u64,
}
impl Header {
const SIZE: usize = 1 + 3 + 4 + 8;
pub fn new(content: &[u8], version: Version) -> Header {
let size = content.len() as u64;
let mut digest = crc32fast::Hasher::new();
digest.update(content);
let crc32 = digest.finalize();
Header {
version,
crc32,
size,
}
}
pub fn parse(buf: &[u8]) -> Result<(Header, &[u8], &[u8])> {
if buf.len() < Header::SIZE {
return Err(box_err!(
"file corrupted! header size mismatch {} != {}",
Header::SIZE,
buf.len()
));
}
let version = Version::from(buf[0])?;
let crc32 = BigEndian::read_u32(&buf[4..8]);
let size = BigEndian::read_u64(&buf[8..Header::SIZE]);
let remained_size = buf.len() - Header::SIZE;
match version {
Version::V1 => {
if remained_size != size as usize {
return Err(box_err!(
"file corrupted! content size isn't expected: {}, expected: {}",
remained_size,
size
));
}
}
Version::V2 => {
if remained_size < size as usize {
return Err(box_err!(
"file corrupted! content size is too small: {}, expected: {}",
remained_size,
size
));
}
}
}
let content = &buf[Header::SIZE..Header::SIZE + size as usize];
let remained = &buf[Header::SIZE + size as usize..];
let mut digest = crc32fast::Hasher::new();
digest.update(content);
let crc32_checksum = digest.finalize();
if crc32_checksum != crc32 {
return Err(box_err!(
"file corrupted! crc32 mismatch {} != {}",
crc32,
crc32_checksum
));
}
let header = Header {
version,
crc32,
size,
};
Ok((header, content, remained))
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = [0; Header::SIZE];
(&mut buf[0..4])
.write_all(&[self.version as u8, 0, 0, 0])
.unwrap();
BigEndian::write_u32(&mut buf[4..8], self.crc32);
BigEndian::write_u64(&mut buf[8..Header::SIZE], self.size);
buf.to_vec()
}
pub fn version(&self) -> Version {
self.version
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_header() {
let empty_header = Header {
version: Version::V1,
crc32: 0,
size: 0,
};
let bytes = empty_header.to_bytes();
let (header1, content1, _) = Header::parse(&bytes).unwrap();
assert_eq!(empty_header, header1);
let empty: Vec<u8> = vec![];
assert_eq!(content1, empty.as_slice())
}
#[test]
fn test_crc32_size() {
let content = [5; 32];
let header = Header::new(&content, Version::V1);
{
let mut bytes = header.to_bytes();
bytes.extend_from_slice(&content);
let (header1, content1, _) = Header::parse(&bytes).unwrap();
assert_eq!(header, header1);
assert_eq!(content, content1)
}
{
let bytes_missing_content = header.to_bytes();
Header::parse(&bytes_missing_content).unwrap_err();
}
{
let mut bytes_bad_content = header.to_bytes();
bytes_bad_content.extend_from_slice(&[7; 32]);
Header::parse(&bytes_bad_content).unwrap_err();
}
}
}