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
use crate::block::Block;
use crate::blockhandle::BlockHandle;
use crate::error::{err, Result, StatusCode};
use crate::filter;
use crate::filter_block::FilterBlockReader;
use crate::options::{self, CompressionType, Options};
use crate::table_builder;
use crate::types::{unmask_crc, RandomAccess};
use crc::crc32::{self, Hasher32};
use integer_encoding::FixedInt;
use snap::Decoder;
fn read_bytes(f: &dyn RandomAccess, location: &BlockHandle) -> Result<Vec<u8>> {
let mut buf = vec![0; location.size()];
f.read_at(location.offset(), &mut buf).map(|_| buf)
}
pub fn read_filter_block(
src: &dyn RandomAccess,
location: &BlockHandle,
policy: filter::BoxedFilterPolicy,
) -> Result<FilterBlockReader> {
if location.size() == 0 {
return err(
StatusCode::InvalidArgument,
"no filter block in empty location",
);
}
let buf = read_bytes(src, location)?;
Ok(FilterBlockReader::new_owned(policy, buf))
}
pub fn read_table_block(
opt: Options,
f: &dyn RandomAccess,
location: &BlockHandle,
) -> Result<Block> {
let buf = read_bytes(f, location)?;
let compress = read_bytes(
f,
&BlockHandle::new(
location.offset() + location.size(),
table_builder::TABLE_BLOCK_COMPRESS_LEN,
),
)?;
let cksum = read_bytes(
f,
&BlockHandle::new(
location.offset() + location.size() + table_builder::TABLE_BLOCK_COMPRESS_LEN,
table_builder::TABLE_BLOCK_CKSUM_LEN,
),
)?;
if !verify_table_block(&buf, compress[0], unmask_crc(u32::decode_fixed(&cksum))) {
return err(
StatusCode::Corruption,
&format!(
"checksum verification failed for block at {}",
location.offset()
),
);
}
if let Some(ctype) = options::int_to_compressiontype(compress[0] as u32) {
match ctype {
CompressionType::CompressionNone => Ok(Block::new(opt, buf)),
CompressionType::CompressionSnappy => {
let decoded = Decoder::new().decompress_vec(&buf)?;
Ok(Block::new(opt, decoded))
}
}
} else {
err(StatusCode::InvalidData, "invalid compression type")
}
}
fn verify_table_block(data: &[u8], compression: u8, want: u32) -> bool {
let mut digest = crc32::Digest::new(crc32::CASTAGNOLI);
digest.write(data);
digest.write(&[compression; 1]);
digest.sum32() == want
}