blob: 3305efade8a4b0347d921700f9fd69469a10afd5 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001/*
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
17use 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.
26pub fn root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer>
27where
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.
39pub fn root_with_opts<'opts, 'buf, T>(
40 opts: &'opts VerifierOptions,
41 data: &'buf [u8],
42) -> Result<T::Inner, InvalidFlatbuffer>
43where
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.
56pub fn size_prefixed_root<'buf, T>(data: &'buf [u8]) -> Result<T::Inner, InvalidFlatbuffer>
57where
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.
69pub fn size_prefixed_root_with_opts<'opts, 'buf, T>(
70 opts: &'opts VerifierOptions,
71 data: &'buf [u8],
72) -> Result<T::Inner, InvalidFlatbuffer>
73where
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.
88pub unsafe fn root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
89where
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.
102pub unsafe fn size_prefixed_root_unchecked<'buf, T>(data: &'buf [u8]) -> T::Inner
103where
104 T: Follow<'buf> + 'buf,
105{
106 <SkipSizePrefix<ForwardsUOffset<T>>>::follow(data, 0)
107}