• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Ok(unsafe { root_unchecked::<T>(data) })
49 }
50 
51 #[inline]
52 /// Gets the root of a size prefixed Flatbuffer, verifying it first with default options.
53 /// Note that verification is an experimental feature and may not be maximally performant or
54 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
55 /// behavior.
size_prefixed_root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer> where T: 'buf + Follow<'buf> + Verifiable,56 pub fn size_prefixed_root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer>
57 where
58     T: 'buf + Follow<'buf> + Verifiable,
59 {
60     let opts = VerifierOptions::default();
61     size_prefixed_root_with_opts::<T>(&opts, data)
62 }
63 
64 #[inline]
65 /// Gets the root of a size prefixed Flatbuffer, verifying it first with given options.
66 /// Note that verification is an experimental feature and may not be maximally performant or
67 /// catch every error (though that is the goal). See the `_unchecked` variants for previous
68 /// 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,69 pub fn size_prefixed_root_with_opts<'opts, 'buf, T>(
70     opts: &'opts VerifierOptions,
71     data: &'buf [u8],
72 ) -> Result<T::Inner, InvalidFlatbuffer>
73 where
74     T: 'buf + Follow<'buf> + Verifiable,
75 {
76     let mut v = Verifier::new(&opts, data);
77     <SkipSizePrefix<ForwardsUOffset<T>>>::run_verifier(&mut v, 0)?;
78     Ok(unsafe { size_prefixed_root_unchecked::<T>(data) })
79 }
80 
81 #[inline]
82 /// Gets root for a trusted Flatbuffer.
83 /// # Safety
84 /// Flatbuffers accessors do not perform validation checks before accessing. Unlike the other
85 /// `root` functions, this does not validate the flatbuffer before returning the accessor. Users
86 /// must trust `data` contains a valid flatbuffer (e.g. b/c it was built by your software). Reading
87 /// unchecked buffers may cause panics or even UB.
root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner where T: Follow<'buf> + 'buf,88 pub unsafe fn root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
89 where
90     T: Follow<'buf> + 'buf,
91 {
92     <ForwardsUOffset<T>>::follow(data, 0)
93 }
94 
95 #[inline]
96 /// Gets root for a trusted, size prefixed, Flatbuffer.
97 /// # Safety
98 /// Flatbuffers accessors do not perform validation checks before accessing. Unlike the other
99 /// `root` functions, this does not validate the flatbuffer before returning the accessor. Users
100 /// must trust `data` contains a valid flatbuffer (e.g. b/c it was built by your software). Reading
101 /// unchecked buffers may cause panics or even UB.
size_prefixed_root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner where T: Follow<'buf> + 'buf,102 pub unsafe fn size_prefixed_root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
103 where
104     T: Follow<'buf> + 'buf,
105 {
106     <SkipSizePrefix<ForwardsUOffset<T>>>::follow(data, 0)
107 }
108