Mstdlib-1.24.0
Binary data

Functions

M_uint8 * M_bin_wrap (const M_uint8 *value, size_t len)
 
M_uint8 * M_bin_wrapeddup (const M_uint8 *value)
 
void * M_bin_wrapeddup_vp (const void *value)
 
const M_uint8 * M_bin_unwrap (const M_uint8 *value, size_t *len)
 
M_uint8 * M_bin_unwrapdup (const M_uint8 *value, size_t *len)
 

Detailed Description

Allows for wrapping binary data into a data type that includes both length and data. This is to allow binary data to be passed around as a single pointer instead of having to manage the data separate from the length.

Binary data of form data, len will be wrapped into len+data. The length prefix is always fixed as 8. 8 was chosen instead of sizeof(size_t) because 32bit Solaris Sparc sizeof(size_t) is 4 but alignment is 8. 8 is also sizeof(size_t) on 64bit platforms.

Example:

M_uint8 data[3] = { 1, 2, 3 };
M_uint8 *wd;
M_uint8 *wd_dup;
size_t len;
wd = M_bin_wrap(data, 3);
wd_dup = M_bin_wrapeddup(wd);
M_free(wd);
wd = M_bin_unwrapdup(wd_dup, &len);
M_free(wd);
M_free(wd_dup);
M_printf(len='%zu'\n", len);
M_uint8 * M_bin_wrapeddup(const M_uint8 *value)
M_uint8 * M_bin_unwrapdup(const M_uint8 *value, size_t *len)
M_uint8 * M_bin_wrap(const M_uint8 *value, size_t len)
ssize_t M_printf(const char *fmt,...)
void M_free(void *ptr) M_FREE(1)

Function Documentation

◆ M_bin_wrap()

M_uint8 * M_bin_wrap ( const M_uint8 *  value,
size_t  len 
)

Duplicate a binary value with the length prepended.

Parameters
[in]valueThe binary data.
[in]lenThe length of value.
Returns
Allocated memory of len+data of length len+sizeof(len). Where sizeof(size_t) is typically 8.

◆ M_bin_wrapeddup()

M_uint8 * M_bin_wrapeddup ( const M_uint8 *  value)

Duplicates binary data that has the length prepended.

The data would have to have been created using M_bin_wrap. The prepended length is used to determine how much memory to duplicate.

Parameters
[in]valueWrapped binary data to duplicate.
Returns
Copy of wrapped data (len+data).

◆ M_bin_wrapeddup_vp()

void * M_bin_wrapeddup_vp ( const void *  value)

Duplicates data that has the length prepended.

The data would have to have been created using M_bin_wrap. The prepended length is used to determine how much memory to duplicate. This function should be considered unsafe and is only provided as a convience to avoid casting if the data is not an M_uint8.

Parameters
[in]valueWrapped binary data to duplicate.
Returns
Copy of wrapped data (len+data).
See also
M_bin_wrapeddup

◆ M_bin_unwrap()

const M_uint8 * M_bin_unwrap ( const M_uint8 *  value,
size_t *  len 
)

Unwrap wrapped binary data.

Parameters
[in]valueWrapped binary data.
[out]lenThe length of the binary data.
Returns
Pointer to start of data within value.

◆ M_bin_unwrapdup()

M_uint8 * M_bin_unwrapdup ( const M_uint8 *  value,
size_t *  len 
)

Unwrap wrapped binary data and return a copy of the data.

Parameters
[in]valueWrapped binary data.
[out]lenThe length of the binary data.
Returns
Allocated memory with a copy of data from value. The prepended length will not be duplicated.