1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! NP Rust C FFI functionality for V0 advertisement serialization.
16
17 use crate::{panic_if_invalid, unwrap, PanicReason};
18 use np_ffi_core::common::{ByteBuffer, DeallocateResult, FixedSizeArray};
19 use np_ffi_core::credentials::V0BroadcastCredential;
20 use np_ffi_core::serialize::v0::*;
21 use np_ffi_core::utils::FfiEnum;
22 use np_ffi_core::v0::V0DataElement;
23
24 /// Attempts to add the given data element to the V0
25 /// advertisement builder behind the passed handle.
26 ///
27 /// This method may invoke the panic handler if the passed DE
28 /// has an invalid layout, which may indicate that the backing
29 /// data on the stack was somehow tampered with in an unintended way.
30 #[no_mangle]
np_ffi_V0AdvertisementBuilder_add_de( adv_builder: V0AdvertisementBuilder, de: V0DataElement, ) -> AddV0DEResult31 pub extern "C" fn np_ffi_V0AdvertisementBuilder_add_de(
32 adv_builder: V0AdvertisementBuilder,
33 de: V0DataElement,
34 ) -> AddV0DEResult {
35 panic_if_invalid(adv_builder.add_de(de))
36 }
37
38 /// Attempts to serialize the contents of the advertisement builder
39 /// behind this handle to bytes. Assuming that the handle is valid,
40 /// this operation will always result in the contents behind the
41 /// advertisement builder handle being deallocated.
42 #[no_mangle]
np_ffi_V0AdvertisementBuilder_into_advertisement( adv_builder: V0AdvertisementBuilder, ) -> SerializeV0AdvertisementResult43 pub extern "C" fn np_ffi_V0AdvertisementBuilder_into_advertisement(
44 adv_builder: V0AdvertisementBuilder,
45 ) -> SerializeV0AdvertisementResult {
46 adv_builder.into_advertisement()
47 }
48
49 /// Attempts to deallocate the v0 advertisement builder behind
50 /// the given handle.
51 #[no_mangle]
np_ffi_deallocate_v0_advertisement_builder( adv_builder: V0AdvertisementBuilder, ) -> DeallocateResult52 pub extern "C" fn np_ffi_deallocate_v0_advertisement_builder(
53 adv_builder: V0AdvertisementBuilder,
54 ) -> DeallocateResult {
55 adv_builder.deallocate()
56 }
57
58 /// Creates a new V0 advertisement builder for a public advertisement.
59 #[no_mangle]
np_ffi_create_v0_public_advertisement_builder() -> V0AdvertisementBuilder60 pub extern "C" fn np_ffi_create_v0_public_advertisement_builder() -> V0AdvertisementBuilder {
61 unwrap(
62 create_v0_public_advertisement_builder().into_success(),
63 PanicReason::ExceededMaxHandleAllocations,
64 )
65 }
66
67 /// Creates a new V0 advertisement builder for an encrypted advertisement.
68 #[no_mangle]
np_ffi_create_v0_encrypted_advertisement_builder( broadcast_cred: V0BroadcastCredential, salt: FixedSizeArray<2>, ) -> V0AdvertisementBuilder69 pub extern "C" fn np_ffi_create_v0_encrypted_advertisement_builder(
70 broadcast_cred: V0BroadcastCredential,
71 salt: FixedSizeArray<2>,
72 ) -> V0AdvertisementBuilder {
73 unwrap(
74 create_v0_encrypted_advertisement_builder(broadcast_cred, salt).into_success(),
75 PanicReason::ExceededMaxHandleAllocations,
76 )
77 }
78
79 /// Gets the tag of a `SerializeV0AdvertisementResult` tagged-union.
80 #[no_mangle]
np_ffi_SerializeV0AdvertisementResult_kind( result: SerializeV0AdvertisementResult, ) -> SerializeV0AdvertisementResultKind81 pub extern "C" fn np_ffi_SerializeV0AdvertisementResult_kind(
82 result: SerializeV0AdvertisementResult,
83 ) -> SerializeV0AdvertisementResultKind {
84 result.kind()
85 }
86
87 /// Casts a `SerializeV0AdvertisementResult` to the `Success` variant,
88 /// panicking in the case where the passed value is of a different enum variant.
89 #[no_mangle]
np_ffi_SerializeV0AdvertisementResult_into_SUCCESS( result: SerializeV0AdvertisementResult, ) -> ByteBuffer<24>90 pub extern "C" fn np_ffi_SerializeV0AdvertisementResult_into_SUCCESS(
91 result: SerializeV0AdvertisementResult,
92 ) -> ByteBuffer<24> {
93 unwrap(result.into_success(), PanicReason::EnumCastFailed)
94 }
95