• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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::follow::Follow;
18 use crate::primitives::*;
19 use crate::vtable::VTable;
20 
21 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
22 pub struct Table<'a> {
23     buf: &'a [u8],
24     loc: usize,
25 }
26 
27 impl<'a> Table<'a> {
28     #[inline]
buf(&self) -> &'a [u8]29     pub fn buf(&self) -> &'a [u8] {
30         self.buf
31     }
32 
33     #[inline]
loc(&self) -> usize34     pub fn loc(&self) -> usize {
35         self.loc
36     }
37 
38     /// # Safety
39     ///
40     /// `buf` must contain a `soffset_t` at `loc`, which points to a valid vtable
41     #[inline]
new(buf: &'a [u8], loc: usize) -> Self42     pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self {
43         Table { buf, loc }
44     }
45 
46     #[inline]
vtable(&self) -> VTable<'a>47     pub fn vtable(&self) -> VTable<'a> {
48         // Safety:
49         // Table::new is created with a valid buf and location
50         unsafe { <BackwardsSOffset<VTable<'a>>>::follow(self.buf, self.loc) }
51     }
52 
53     /// Retrieves the value at the provided `slot_byte_loc` returning `default`
54     /// if no value present
55     ///
56     /// # Safety
57     ///
58     /// The value of the corresponding slot must have type T
59     #[inline]
get<T: Follow<'a> + 'a>( &self, slot_byte_loc: VOffsetT, default: Option<T::Inner>, ) -> Option<T::Inner>60     pub unsafe fn get<T: Follow<'a> + 'a>(
61         &self,
62         slot_byte_loc: VOffsetT,
63         default: Option<T::Inner>,
64     ) -> Option<T::Inner> {
65         let o = self.vtable().get(slot_byte_loc) as usize;
66         if o == 0 {
67             return default;
68         }
69         Some(<T>::follow(self.buf, self.loc + o))
70     }
71 }
72 
73 impl<'a> Follow<'a> for Table<'a> {
74     type Inner = Table<'a>;
75     #[inline]
follow(buf: &'a [u8], loc: usize) -> Self::Inner76     unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
77         Table { buf, loc }
78     }
79 }
80 
81 /// Returns true if data contains a prefix of `ident`
82 #[inline]
buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool83 pub fn buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool {
84     assert_eq!(ident.len(), FILE_IDENTIFIER_LENGTH);
85 
86     let got = if size_prefixed {
87         assert!(data.len() >= SIZE_SIZEPREFIX + SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
88         // Safety:
89         // Verified data has sufficient bytes
90         unsafe { <SkipSizePrefix<SkipRootOffset<FileIdentifier>>>::follow(data, 0) }
91     } else {
92         assert!(data.len() >= SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
93         // Safety:
94         // Verified data has sufficient bytes
95         unsafe { <SkipRootOffset<FileIdentifier>>::follow(data, 0) }
96     };
97 
98     ident.as_bytes() == got
99 }
100