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)]
22 pub struct Table<'a> {
23 pub buf: &'a [u8],
24 pub loc: usize,
25 }
26
27 impl<'a> Table<'a> {
28 #[inline]
new(buf: &'a [u8], loc: usize) -> Self29 pub fn new(buf: &'a [u8], loc: usize) -> Self {
30 Table { buf, loc }
31 }
32 #[inline]
vtable(&self) -> VTable<'a>33 pub fn vtable(&self) -> VTable<'a> {
34 <BackwardsSOffset<VTable<'a>>>::follow(self.buf, self.loc)
35 }
36 #[inline]
get<T: Follow<'a> + 'a>( &self, slot_byte_loc: VOffsetT, default: Option<T::Inner>, ) -> Option<T::Inner>37 pub fn get<T: Follow<'a> + 'a>(
38 &self,
39 slot_byte_loc: VOffsetT,
40 default: Option<T::Inner>,
41 ) -> Option<T::Inner> {
42 let o = self.vtable().get(slot_byte_loc) as usize;
43 if o == 0 {
44 return default;
45 }
46 Some(<T>::follow(self.buf, self.loc + o))
47 }
48 }
49
50 impl<'a> Follow<'a> for Table<'a> {
51 type Inner = Table<'a>;
52 #[inline]
follow(buf: &'a [u8], loc: usize) -> Self::Inner53 fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
54 Table { buf, loc }
55 }
56 }
57
58 #[inline]
buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool59 pub fn buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool {
60 assert_eq!(ident.len(), FILE_IDENTIFIER_LENGTH);
61
62 let got = if size_prefixed {
63 <SkipSizePrefix<SkipRootOffset<FileIdentifier>>>::follow(data, 0)
64 } else {
65 <SkipRootOffset<FileIdentifier>>::follow(data, 0)
66 };
67
68 ident.as_bytes() == got
69 }
70