blob: f5001f6d1ca86d08271407f68666f427cc90438e [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
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
Austin Schuh272c6132020-11-14 16:37:52 -080017use crate::follow::Follow;
18use crate::primitives::*;
19use crate::vtable::VTable;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070020
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080021#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Austin Schuhe89fa2d2019-08-14 20:24:23 -070022pub struct Table<'a> {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080023 buf: &'a [u8],
24 loc: usize,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070025}
26
27impl<'a> Table<'a> {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080028 /// # Safety
29 ///
30 /// `buf` must contain a `soffset_t` at `loc`, which points to a valid vtable
Austin Schuhe89fa2d2019-08-14 20:24:23 -070031 #[inline]
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080032 pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self {
Austin Schuh272c6132020-11-14 16:37:52 -080033 Table { buf, loc }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070034 }
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080035
Austin Schuhe89fa2d2019-08-14 20:24:23 -070036 #[inline]
37 pub fn vtable(&self) -> VTable<'a> {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080038 // Safety:
39 // Table::new is created with a valid buf and location
40 unsafe { <BackwardsSOffset<VTable<'a>>>::follow(self.buf, self.loc) }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070041 }
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080042
43 /// Retrieves the value at the provided `slot_byte_loc` returning `default`
44 /// if no value present
45 ///
46 /// # Safety
47 ///
48 /// The value of the corresponding slot must have type T
Austin Schuhe89fa2d2019-08-14 20:24:23 -070049 #[inline]
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080050 pub unsafe fn get<T: Follow<'a> + 'a>(
Austin Schuhe89fa2d2019-08-14 20:24:23 -070051 &self,
52 slot_byte_loc: VOffsetT,
53 default: Option<T::Inner>,
54 ) -> Option<T::Inner> {
55 let o = self.vtable().get(slot_byte_loc) as usize;
56 if o == 0 {
57 return default;
58 }
59 Some(<T>::follow(self.buf, self.loc + o))
60 }
61}
62
63impl<'a> Follow<'a> for Table<'a> {
64 type Inner = Table<'a>;
65 #[inline]
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080066 unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Austin Schuh272c6132020-11-14 16:37:52 -080067 Table { buf, loc }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070068 }
69}
70
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080071/// Returns true if data contains a prefix of `ident`
Austin Schuhe89fa2d2019-08-14 20:24:23 -070072#[inline]
Austin Schuhe89fa2d2019-08-14 20:24:23 -070073pub fn buffer_has_identifier(data: &[u8], ident: &str, size_prefixed: bool) -> bool {
74 assert_eq!(ident.len(), FILE_IDENTIFIER_LENGTH);
75
76 let got = if size_prefixed {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080077 assert!(data.len() >= SIZE_SIZEPREFIX + SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
78 // Safety:
79 // Verified data has sufficient bytes
80 unsafe { <SkipSizePrefix<SkipRootOffset<FileIdentifier>>>::follow(data, 0) }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070081 } else {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080082 assert!(data.len() >= SIZE_UOFFSET + FILE_IDENTIFIER_LENGTH);
83 // Safety:
84 // Verified data has sufficient bytes
85 unsafe { <SkipRootOffset<FileIdentifier>>::follow(data, 0) }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070086 };
87
88 ident.as_bytes() == got
89}