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 */
1617use crate::follow::Follow;
18use crate::primitives::*;
19use crate::vtable::VTable;
2021#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct Table<'a> {
23 buf: &'a [u8],
24 loc: usize,
25}
2627impl<'a> Table<'a> {
28#[inline]
29pub fn buf(&self) -> &'a [u8] {
30self.buf
31 }
3233#[inline]
34pub fn loc(&self) -> usize {
35self.loc
36 }
3738/// # Safety
39 ///
40 /// `buf` must contain a `soffset_t` at `loc`, which points to a valid vtable
41#[inline]
42pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self {
43 Table { buf, loc }
44 }
4546#[inline]
47pub fn vtable(&self) -> VTable<'a> {
48// Safety:
49 // Table::new is created with a valid buf and location
50unsafe { <BackwardsSOffset<VTable<'a>>>::follow(self.buf, self.loc) }
51 }
5253/// 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]
60pub unsafe fn get<T: Follow<'a> + 'a>(
61&self,
62 slot_byte_loc: VOffsetT,
63 default: Option<T::Inner>,
64 ) -> Option<T::Inner> {
65let o = self.vtable().get(slot_byte_loc) as usize;
66if o == 0 {
67return default;
68 }
69Some(<T>::follow(self.buf, self.loc + o))
70 }
71}
7273impl<'a> Follow<'a> for Table<'a> {
74type Inner = Table<'a>;
75#[inline]
76unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
77 Table { buf, loc }
78 }
79}
8081/// Returns true if data contains a prefix of `ident`
82#[inline]
83pub fn buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool {
84assert_eq!(ident.len(), FILE_IDENTIFIER_LENGTH);
8586let got = if size_prefixed {
87assert!(data.len() >= SIZE_SIZEPREFIX + SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
88// Safety:
89 // Verified data has sufficient bytes
90unsafe { <SkipSizePrefix<SkipRootOffset<FileIdentifier>>>::follow(data, 0) }
91 } else {
92assert!(data.len() >= SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
93// Safety:
94 // Verified data has sufficient bytes
95unsafe { <SkipRootOffset<FileIdentifier>>::follow(data, 0) }
96 };
9798 ident.as_bytes() == got
99}