Debian Codegraph @

src/data_format/mod.rs

raw symbols SCIP
1 //! Allows serialization of the adblock engine into a compact binary format, as well as subsequent
2 //! rapid deserialization back into an engine.
3 //!
4 //! In order to support multiple format versions simultaneously, this module wraps around different
5 //! serialization/deserialization implementations and can automatically dispatch to the appropriate
6 //! one.
7
8 mod v0;
9
10 pub(crate) mod utils;
11
12 use crate::blocker::Blocker;
13 use crate::cosmetic_filter_cache::CosmeticFilterCache;
14
15 /// Newer formats start with this magic byte sequence.
16 /// Calculated as the leading 4 bytes of `echo -n 'brave/adblock-rust' | sha512sum`.
17 const ADBLOCK_RUST_DAT_MAGIC: [u8; 4] = [0xd1, 0xd9, 0x3a, 0xaf];
18
19 /// Provides structural aggregration of referenced adblock engine data to allow for allocation-free
20 /// serialization.
21 ///
22 /// Note that this does not implement `Serialize` directly, as it is composed of parts which must
23 /// be serialized independently. Instead, use the `serialize` method.
24 pub(crate) enum SerializeFormat<'a> {
25 V0(v0::SerializeFormat<'a>),
26 }
27
28 #[derive(Debug)]
29 pub enum SerializationError {
30 RmpSerdeError(rmp_serde::encode::Error),
31 }
32
33 impl From<rmp_serde::encode::Error> for SerializationError {
34 fn from(e: rmp_serde::encode::Error) -> Self {
35 Self::RmpSerdeError(e)
36 }
37 }
38
39 impl<'a> SerializeFormat<'a> {
40 pub(crate) fn build(blocker: &'a Blocker, cfc: &'a CosmeticFilterCache) -> Self {
41 Self::V0(v0::SerializeFormat::from((blocker, cfc)))
42 }
43
44 pub(crate) fn serialize(&self) -> Result<Vec<u8>, SerializationError> {
45 match self {
46 Self::V0(v) => v.serialize(),
47 }
48 }
49 }
50
51 /// Structural representation of adblock engine data that can be built up from deserialization and
52 /// used directly to construct new `Engine` components without unnecessary allocation.
53 ///
54 /// Note that this does not implement `Deserialize` directly, as it is composed of parts which must
55 /// be deserialized independently. Instead, use the `deserialize` method.
56 pub(crate) enum DeserializeFormat {
57 V0(v0::DeserializeFormat),
58 }
59
60 #[derive(Debug)]
61 pub enum DeserializationError {
62 RmpSerdeError(rmp_serde::decode::Error),
63 UnsupportedFormatVersion(u8),
64 NoHeaderFound,
65 /// Support for the legacy gzip-compressed data format was removed in version 0.8.0 of this
66 /// crate. If you still need it for some reason, you can convert it using 0.7.x by
67 /// deserializing and then reserializing it into the newer V0 format.
68 LegacyFormatNoLongerSupported,
69 }
70
71 impl From<rmp_serde::decode::Error> for DeserializationError {
72 fn from(e: rmp_serde::decode::Error) -> Self {
73 Self::RmpSerdeError(e)
74 }
75 }
76
77 impl DeserializeFormat {
78 pub(crate) fn build(self) -> (Blocker, CosmeticFilterCache) {
79 match self {
80 Self::V0(v) => v.into(),
81 }
82 }
83
84 pub(crate) fn deserialize(serialized: &[u8]) -> Result<Self, DeserializationError> {
85 /// adblock-rust's legacy DAT format has always used flate2 1.0.x, which has never changed
86 /// the header sequence from these 10 bits when the GzEncoder is left uncustomized.
87 const FLATE2_GZ_HEADER_BYTES: [u8; 10] = [31, 139, 8, 0, 0, 0, 0, 0, 0, 255];
88
89 if serialized.starts_with(&ADBLOCK_RUST_DAT_MAGIC) {
90 let version = serialized[ADBLOCK_RUST_DAT_MAGIC.len()];
91 match version {
92 0 => Ok(Self::V0(v0::DeserializeFormat::deserialize(serialized)?)),
93 v => Err(DeserializationError::UnsupportedFormatVersion(v)),
94 }
95 } else if serialized.starts_with(&FLATE2_GZ_HEADER_BYTES) {
96 Err(DeserializationError::LegacyFormatNoLongerSupported)
97 } else {
98 Err(DeserializationError::NoHeaderFound)
99 }
100 }
101 }
102
103 #[cfg(test)]
104 mod tests {
105 use super::*;
106
107 #[test]
108 fn validate_magic_bytes() {
109 use sha2::Digest;
110
111 let mut hasher = sha2::Sha512::new();
112
113 hasher.update("brave/adblock-rust");
114
115 let result = hasher.finalize();
116
117 assert!(result.starts_with(&ADBLOCK_RUST_DAT_MAGIC));
118 }
119 }