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;
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)
◆ 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] | value | The binary data. |
[in] | len | The 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] | value | Wrapped 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] | value | Wrapped 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] | value | Wrapped binary data. |
[out] | len | The 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] | value | Wrapped binary data. |
[out] | len | The length of the binary data. |
- Returns
- Allocated memory with a copy of data from value. The prepended length will not be duplicated.