blob: d30db74d8595da84a04a7be212dfaa2edc681a46 [file]
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use super::sys::mini_table::extension_registry::upb_ExtensionRegistry;
use super::sys::wire::wire::{upb_ByteSize, upb_Decode, upb_Encode, DecodeStatus, EncodeStatus};
use super::{Arena, AssociatedMiniTable, MessagePtr};
/// Contains the decode options that can be passed to `decode_with_options`.
#[allow(unused)] // FFI constants.
pub mod decode_options {
// LINT.IfChange(decode_option)
pub const ALIAS_STRING: i32 = 1;
pub const CHECK_REQUIRED: i32 = 2;
pub const EXPERIMENTAL_ALLOW_UNLINKED: i32 = 4;
pub const ALWAYS_VALIDATE_UTF8: i32 = 8;
// LINT.ThenChange()
}
/// Contains the encode options that can be passed to `encode_with_options`.
/// Only deterministic encoding is currently supported in Rust.
#[allow(unused)] // FFI constants.
pub mod encode_options {
pub const DETERMINISTIC: i32 = 1;
pub const SKIP_UNKNOWN: i32 = 2;
pub const CHECK_REQUIRED: i32 = 4;
}
/// Encodes the message with the given options. If Err, then EncodeStatus != Ok.
///
/// `encode_options_bitmask` is a bitmask of constants from the `encode_options` module.
pub fn encode_with_options<T: AssociatedMiniTable>(
msg: MessagePtr<T>,
encode_options_bitmask: i32,
) -> Result<Vec<u8>, EncodeStatus> {
let arena = Arena::new();
let mut buf: *mut u8 = core::ptr::null_mut();
let mut len = 0usize;
// SAFETY:
// - `T::mini_table()` is the one associated with `msg`.
// - `buf` and `buf_size` are legally writable.
let status = unsafe {
upb_Encode(
msg.raw(),
T::mini_table(),
encode_options_bitmask,
arena.raw(),
&mut buf,
&mut len,
)
};
if status == EncodeStatus::Ok {
assert!(!buf.is_null()); // EncodeStatus Ok should never return NULL data, even for len=0.
if len > isize::MAX as usize {
return Err(EncodeStatus::MaxSizeExceeded);
}
// SAFETY:
// - `upb_Encode` guarantees that `buf` is valid to read for `len` initialized bytes when
// `status == EncodeStatus::Ok`.
// - `buf` is non-null (`assert!` above) and `u8` has alignment 1.
// - `len <= isize::MAX as usize` is checked above.
Ok(unsafe { core::slice::from_raw_parts(buf, len) }.to_vec())
} else {
Err(status)
}
}
/// Returns the serialized size of the message.
pub fn byte_size<T: AssociatedMiniTable>(msg: MessagePtr<T>) -> usize {
// SAFETY:
// - `T::mini_table()` is the one associated with `msg`.
// - `msg` is guaranteed live.
unsafe { upb_ByteSize(msg.raw(), T::mini_table()) }
}
/// Decodes into the provided message (merge semantics). If Err, then
/// DecodeStatus != Ok.
///
/// Equivalent to `decode_with_options()` with the
/// `decode_options::CHECK_REQUIRED` option set.
///
/// # Safety
/// - `msg` must be mutable.
/// - `extreg` must be null or point to a valid `upb_ExtensionRegistry` that is not mutated during
/// the call, and every extension in it must outlive `msg`.
#[allow(unused)] // Not used yet.
pub unsafe fn decode<T: AssociatedMiniTable>(
buf: &[u8],
msg: MessagePtr<T>,
extreg: *const upb_ExtensionRegistry,
arena: &Arena,
) -> Result<(), DecodeStatus> {
// SAFETY:
// - `msg` is mutable and is associated with `mini_table`.
// - `extreg` is null or a valid registry whose extensions outlive `msg`, guaranteed by the
// caller.
// - `decode_options::CHECK_REQUIRED` is a valid decode option.
unsafe { decode_with_options(buf, msg, extreg, arena, decode_options::CHECK_REQUIRED) }
}
/// Decodes into the provided message (merge semantics). If Err, then
/// DecodeStatus != Ok.
///
/// # Safety
/// - `msg` must be mutable.
/// - `extreg` must be null or point to a valid `upb_ExtensionRegistry` that is not mutated during
/// the call, and every extension in it must outlive `msg`.
/// - `decode_options_bitmask` is a bitmask of constants from the `decode_options` module.
pub unsafe fn decode_with_options<T: AssociatedMiniTable>(
buf: &[u8],
msg: MessagePtr<T>,
extreg: *const upb_ExtensionRegistry,
arena: &Arena,
decode_options_bitmask: i32,
) -> Result<(), DecodeStatus> {
let len = buf.len();
let buf = buf.as_ptr();
// SAFETY:
// - `mini_table` is the one associated with `msg`
// - `buf` is legally readable for at least `buf_size` bytes.
// - `extreg` is null or a valid registry whose extensions outlive `msg`, guaranteed by the
// caller.
// - `decode_options_bitmask` is a bitmask of constants from the `decode_options` module.
let status = unsafe {
upb_Decode(
buf,
len,
msg.raw(),
T::mini_table(),
extreg,
decode_options_bitmask,
arena.raw(),
)
};
match status {
DecodeStatus::Ok => Ok(()),
_ => Err(status),
}
}