1 /*
2 * Copyright 2020 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 use crate::{
18 Follow, ForwardsUOffset, InvalidFlatbuffer, SkipSizePrefix, Verifiable, Verifier,
19 VerifierOptions,
20 };
21
22 /// Gets the root of the Flatbuffer, verifying it first with default options.
23 /// Note that verification is an experimental feature and may not be maximally performant or
24 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
25 /// behavior.
root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer> where T: 'buf + Follow<'buf> + Verifiable,26 pub fn root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer>
27 where
28 T: 'buf + Follow<'buf> + Verifiable,
29 {
30 let opts = VerifierOptions::default();
31 root_with_opts::<T>(&opts, data)
32 }
33
34 #[inline]
35 /// Gets the root of the Flatbuffer, verifying it first with given options.
36 /// Note that verification is an experimental feature and may not be maximally performant or
37 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
38 /// behavior.
root_with_opts<'opts, 'buf, T>( opts: &'opts VerifierOptions, data: &'buf [u8], ) -> Result<T::Inner, InvalidFlatbuffer> where T: 'buf + Follow<'buf> + Verifiable,39 pub fn root_with_opts<'opts, 'buf, T>(
40 opts: &'opts VerifierOptions,
41 data: &'buf [u8],
42 ) -> Result<T::Inner, InvalidFlatbuffer>
43 where
44 T: 'buf + Follow<'buf> + Verifiable,
45 {
46 let mut v = Verifier::new(opts, data);
47 <ForwardsUOffset<T>>::run_verifier(&mut v, 0)?;
48 // Safety:
49 // Run verifier above
50 Ok(unsafe { root_unchecked::<T>(data) })
51 }
52
53 #[inline]
54 /// Gets the root of a size prefixed Flatbuffer, verifying it first with default options.
55 /// Note that verification is an experimental feature and may not be maximally performant or
56 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
57 /// behavior.
size_prefixed_root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer> where T: 'buf + Follow<'buf> + Verifiable,58 pub fn size_prefixed_root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer>
59 where
60 T: 'buf + Follow<'buf> + Verifiable,
61 {
62 let opts = VerifierOptions::default();
63 size_prefixed_root_with_opts::<T>(&opts, data)
64 }
65
66 #[inline]
67 /// Gets the root of a size prefixed Flatbuffer, verifying it first with given options.
68 /// Note that verification is an experimental feature and may not be maximally performant or
69 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
70 /// behavior.
size_prefixed_root_with_opts<'opts, 'buf, T>( opts: &'opts VerifierOptions, data: &'buf [u8], ) -> Result<T::Inner, InvalidFlatbuffer> where T: 'buf + Follow<'buf> + Verifiable,71 pub fn size_prefixed_root_with_opts<'opts, 'buf, T>(
72 opts: &'opts VerifierOptions,
73 data: &'buf [u8],
74 ) -> Result<T::Inner, InvalidFlatbuffer>
75 where
76 T: 'buf + Follow<'buf> + Verifiable,
77 {
78 let mut v = Verifier::new(opts, data);
79 <SkipSizePrefix<ForwardsUOffset<T>>>::run_verifier(&mut v, 0)?;
80 // Safety:
81 // Run verifier above
82 Ok(unsafe { size_prefixed_root_unchecked::<T>(data) })
83 }
84
85 #[inline]
86 /// Gets root for a trusted Flatbuffer.
87 /// # Safety
88 /// Flatbuffers accessors do not perform validation checks before accessing. Unlike the other
89 /// `root` functions, this does not validate the flatbuffer before returning the accessor. Users
90 /// must trust `data` contains a valid flatbuffer (e.g. b/c it was built by your software). Reading
91 /// unchecked buffers may cause panics or even UB.
root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner where T: Follow<'buf> + 'buf,92 pub unsafe fn root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
93 where
94 T: Follow<'buf> + 'buf,
95 {
96 <ForwardsUOffset<T>>::follow(data, 0)
97 }
98
99 #[inline]
100 /// Gets root for a trusted, size prefixed, Flatbuffer.
101 /// # Safety
102 /// Flatbuffers accessors do not perform validation checks before accessing. Unlike the other
103 /// `root` functions, this does not validate the flatbuffer before returning the accessor. Users
104 /// must trust `data` contains a valid flatbuffer (e.g. b/c it was built by your software). Reading
105 /// unchecked buffers may cause panics or even UB.
size_prefixed_root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner where T: Follow<'buf> + 'buf,106 pub unsafe fn size_prefixed_root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
107 where
108 T: Follow<'buf> + 'buf,
109 {
110 <SkipSizePrefix<ForwardsUOffset<T>>>::follow(data, 0)
111 }
112