Skip to content

Commit

Permalink
lakers-c: Manually reconstruct the buffer size selection for C
Browse files Browse the repository at this point in the history
Workaround-For: mozilla/cbindgen#1018
  • Loading branch information
chrysn committed Feb 3, 2025
1 parent 435193a commit 8f2fc25
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
75 changes: 73 additions & 2 deletions shared/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,83 @@ header = """
* ================================================================================================
* WARNING: This file is automatically generated by cbindgen. Manual edits are likely to be lost.
* ================================================================================================
*/"""
include_guard = "LAKERS_SHARED_H"
*/
#ifndef LAKERS_SHARED_H
#define LAKERS_SHARED_H
/* Manually implemented to work around https://github.com/mozilla/cbindgen/issues/1018 */
#if defined(_MAX_MESSAGE_SIZE_LEN_1024)
#define MAX_MESSAGE_SIZE_LEN 1024
#elif defined(_MAX_MESSAGE_SIZE_LEN_512)
#define MAX_MESSAGE_SIZE_LEN 512
#elif defined(_MAX_MESSAGE_SIZE_LEN_448)
#define MAX_MESSAGE_SIZE_LEN 448
#elif defined(_MAX_MESSAGE_SIZE_LEN_384)
#define MAX_MESSAGE_SIZE_LEN 384
#else
#define MAX_MESSAGE_SIZE_LEN 256 + 64
#endif
#if defined(_MAX_KDF_CONTENT_LEN_1024)
#define MAX_KDF_CONTEXT_LEN 1024
#elif defined(_MAX_KDF_CONTENT_LEN_512)
#define MAX_KDF_CONTEXT_LEN 512
#elif defined(_MAX_KDF_CONTENT_LEN_448)
#define MAX_KDF_CONTEXT_LEN 448
#elif defined(_MAX_KDF_CONTENT_LEN_384)
#define MAX_KDF_CONTEXT_LEN 384
#elif defined(_MAX_KDF_CONTENT_LEN_320)
#define MAX_KDF_CONTEXT_LEN 320
#else
#define MAX_KDF_CONTEXT_LEN 256
#endif
#if defined(_MAX_KDF_CONTENT_LEN_1024)
#define MAX_BUFFER_LEN 1024
#elif defined(_MAX_KDF_CONTENT_LEN_512)
#define MAX_BUFFER_LEN 512
#elif defined(_MAX_KDF_CONTENT_LEN_448)
#define MAX_BUFFER_LEN 448
#elif defined(_MAX_KDF_CONTENT_LEN_384)
#define MAX_BUFFER_LEN 384
#else
#define MAX_BUFFER_LEN 256 + 64
#endif
#if defined(_MAX_CONNID_ENCODED_LEN_24)
#define MAX_CONNID_ENCODED_LEN 24
#else
#define MAX_CONNID_ENCODED_LEN 8
#endif
"""
trailer = """
#endif /* LAKERS_SHARED_H */
"""
# Done manually so that the manual parts of the MAX_MESSAGE_SIZE_LEN_xxx etc
# features can be placed after the include guard:
# include_guard = "LAKERS_SHARED_H"
cpp_compat = true

[defines]
"feature = large_buffers" = "LARGE_BUFFERS"
"feature = max_message_size_len_256" = "_MAX_MESSAGE_SIZE_LEN_256"
"feature = max_message_size_len_320" = "_MAX_MESSAGE_SIZE_LEN_320"
"feature = max_message_size_len_384" = "_MAX_MESSAGE_SIZE_LEN_384"
"feature = max_message_size_len_448" = "_MAX_MESSAGE_SIZE_LEN_448"
"feature = max_message_size_len_512" = "_MAX_MESSAGE_SIZE_LEN_512"
"feature = max_message_size_len_1024" = "_MAX_MESSAGE_SIZE_LEN_1024"
"feature = max_kdf_content_len_320" = "_MAX_KDF_CONTENT_LEN_320"
"feature = max_kdf_content_len_384" = "_MAX_KDF_CONTENT_LEN_384"
"feature = max_kdf_content_len_448" = "_MAX_KDF_CONTENT_LEN_448"
"feature = max_kdf_content_len_512" = "_MAX_KDF_CONTENT_LEN_512"
"feature = max_kdf_content_len_1024" = "_MAX_KDF_CONTENT_LEN_1024"
"feature = max_buffer_len_384" = "_MAX_BUFFER_LEN_384"
"feature = max_buffer_len_448" = "_MAX_BUFFER_LEN_448"
"feature = max_buffer_len_512" = "_MAX_BUFFER_LEN_512"
"feature = max_buffer_len_1024" = "_MAX_BUFFER_LEN_1024"
"feature = max_connid_encoded_len_24" = "_MAX_CONNID_ENCODED_LEN_24"

[export]
include = [
Expand Down
7 changes: 7 additions & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use pyo3::prelude::*;
#[cfg(feature = "python-bindings")]
mod python_bindings;

// When changing this, beware that it is re-implemented in cbindgen.toml
pub const MAX_MESSAGE_SIZE_LEN: usize = if cfg!(feature = "max_message_size_len_1024") {
1024
} else if cfg!(feature = "max_message_size_len_512") {
Expand Down Expand Up @@ -64,6 +65,8 @@ pub const MAC_LENGTH_3: usize = MAC_LENGTH_2;
pub const ENCODED_VOUCHER_LEN: usize = 1 + MAC_LENGTH; // 1 byte for the length of the bstr-encoded voucher

// maximum supported length of connection identifier for R
//
// When changing this, beware that it is re-implemented in cbindgen.toml
pub const MAX_KDF_CONTEXT_LEN: usize = if cfg!(feature = "max_kdf_content_len_1024") {
1024
} else if cfg!(feature = "max_kdf_content_len_512") {
Expand All @@ -78,6 +81,8 @@ pub const MAX_KDF_CONTEXT_LEN: usize = if cfg!(feature = "max_kdf_content_len_10
256
};
pub const MAX_KDF_LABEL_LEN: usize = 15; // for "KEYSTREAM_2"

// When changing this, beware that it is re-implemented in cbindgen.toml
pub const MAX_BUFFER_LEN: usize = if cfg!(feature = "max_buffer_len_1024") {
1024
} else if cfg!(feature = "max_buffer_len_512") {
Expand Down Expand Up @@ -121,6 +126,8 @@ pub const MAX_EAD_SIZE_LEN: usize = 64;
/// This length includes the leading CBOR encoding byte(s).
// Note that when implementing larger sizes than 24, the encoding will need to use actual CBOR
// rather than masking a known short length into a byte.
//
// When changing this, beware that it is re-implemented in cbindgen.toml
const MAX_CONNID_ENCODED_LEN: usize = if cfg!(feature = "max_connid_encoded_len_24") {
24
} else {
Expand Down

0 comments on commit 8f2fc25

Please sign in to comment.