blob: 4d883b7b8c1382d92a7d713e45e0aad2914aa176 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001/*
2 * Copyright 2021 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
17import Foundation
18
19/// Takes in a prefixed sized buffer, where the prefixed size would be skipped.
20/// And would verify that the buffer passed is a valid `Flatbuffers` Object.
21/// - Parameters:
22/// - byteBuffer: Buffer that needs to be checked and read
23/// - options: Verifier options
24/// - Throws: FlatbuffersErrors
25/// - Returns: Returns a valid, checked Flatbuffers object
26///
27/// ``getPrefixedSizeCheckedRoot(byteBuffer:options:)`` would skip the first Bytes in
28/// the ``ByteBuffer`` and verifies the buffer by calling ``getCheckedRoot(byteBuffer:options:)``
29public func getPrefixedSizeCheckedRoot<T: FlatBufferObject & Verifiable>(
30 byteBuffer: inout ByteBuffer,
31 options: VerifierOptions = .init()) throws -> T
32{
33 byteBuffer.skipPrefix()
34 return try getCheckedRoot(byteBuffer: &byteBuffer, options: options)
35}
36
37/// Takes in a prefixed sized buffer, where the prefixed size would be skipped.
38/// Returns a `NON-Checked` flatbuffers object
39/// - Parameter byteBuffer: Buffer that contains data
40/// - Returns: Returns a Flatbuffers object
41///
42/// ``getPrefixedSizeCheckedRoot(byteBuffer:options:)`` would skip the first Bytes in
43/// the ``ByteBuffer`` and then calls ``getRoot(byteBuffer:)``
44public func getPrefixedSizeRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer)
45 -> T
46{
47 byteBuffer.skipPrefix()
48 return getRoot(byteBuffer: &byteBuffer)
49
50}
51
52/// Verifies that the buffer passed is a valid `Flatbuffers` Object.
53/// - Parameters:
54/// - byteBuffer: Buffer that needs to be checked and read
55/// - options: Verifier options
56/// - Throws: FlatbuffersErrors
57/// - Returns: Returns a valid, checked Flatbuffers object
58///
59/// ``getCheckedRoot(byteBuffer:options:)`` Takes in a ``ByteBuffer`` and verifies
60/// that by creating a ``Verifier`` and checkes if all the `Bytes` and correctly aligned
61/// and within the ``ByteBuffer`` range.
62public func getCheckedRoot<T: FlatBufferObject & Verifiable>(
63 byteBuffer: inout ByteBuffer,
64 options: VerifierOptions = .init()) throws -> T
65{
66 var verifier = try Verifier(buffer: &byteBuffer, options: options)
67 try ForwardOffset<T>.verify(&verifier, at: 0, of: T.self)
68 return T.init(
69 byteBuffer,
70 o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) +
71 Int32(byteBuffer.reader))
72}
73
74/// Returns a `NON-Checked` flatbuffers object
75/// - Parameter byteBuffer: Buffer that contains data
76/// - Returns: Returns a Flatbuffers object
77public func getRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) -> T {
78 T.init(
79 byteBuffer,
80 o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) +
81 Int32(byteBuffer.reader))
82}