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 follow::Follow;
18 use primitives::*;
19 use 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]
get_root<'a, T: Follow<'a> + 'a>(data: &'a [u8]) -> T::Inner59 pub fn get_root<'a, T: Follow<'a> + 'a>(data: &'a [u8]) -> T::Inner {
60 <ForwardsUOffset<T>>::follow(data, 0)
61 }
62 #[inline]
get_size_prefixed_root<'a, T: Follow<'a> + 'a>(data: &'a [u8]) -> T::Inner63 pub fn get_size_prefixed_root<'a, T: Follow<'a> + 'a>(data: &'a [u8]) -> T::Inner {
64 <SkipSizePrefix<ForwardsUOffset<T>>>::follow(data, 0)
65 }
66 #[inline]
buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool67 pub fn buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool {
68 assert_eq!(ident.len(), FILE_IDENTIFIER_LENGTH);
69
70 let got = if size_prefixed {
71 <SkipSizePrefix<SkipRootOffset<FileIdentifier>>>::follow(data, 0)
72 } else {
73 <SkipRootOffset<FileIdentifier>>::follow(data, 0)
74 };
75
76 ident.as_bytes() == got
77 }
78