Utilities (utils)#
- group utils
Miscellaneous utilities: time manipulation, checksums, hashing, bit and byte operations, and memory dumping.
For an overview, see Utils.
rcsw/utils/align.h#
Alignment helpers.
Functions
-
static inline bool utils_is_pow2(size_t n)#
True if
nis a power of two (0 is not).
-
static inline bool utils_is_addr_aligned(uintptr_t addr, size_t byte_count)#
True if address
addris aligned tobyte_countbytes.byte_countneed not be a power of two, but must be nonzero (function precondition; false is returned underLIBRA_FPC_RETURN). Address 0 is aligned to everything.
-
static inline bool utils_is_mem_aligned(const void *const p, size_t byte_count)#
True if pointer
pis aligned tobyte_countbytes.As utils_is_addr_aligned(), for a pointer. NULL is aligned to everything.
-
static inline bool utils_is_size_aligned(size_t size, size_t power_of_two)#
True if
sizeis a multiple ofpower_of_two.power_of_twomust be a power of two (function precondition; false is returned underLIBRA_FPC_RETURN).
-
static inline size_t utils_align_size(size_t size, size_t power_of_two)#
Round
sizeup to the next multiple ofpower_of_two.power_of_twomust be a power of two (function precondition; 0 is returned underLIBRA_FPC_RETURN). The result wraps if it does not fit in asize_t.
rcsw/utils/bit.h#
Bit-width helper.
Defines
-
RCSW_BIT_WIDTH(t)#
Width in bits of type
t. A char is 8 bits on almost every platform, but for some DSPs, it can be something different.
rcsw/utils/byteops.h#
Byte-level functions, such as swapping and string generation.
Functions
-
static inline uint16_t utils_bswap16(uint16_t w16)#
Swap the byte order of a 16-bit value.
-
static inline uint32_t utils_bswap32(uint32_t w32)#
Swap the byte order of a 32-bit value.
-
static inline uint64_t utils_bswap64(uint64_t w64)#
Swap the byte order of a 64-bit value.
-
static inline void utils_arr8_reverse(void *const arr, size_t size)#
Reverse the bytes of an array in place.
Header-only, so that stdio_strrev() can use it without linking against the utils module.
- Parameters:
arr – The byte array to reverse. Can be NULL if
sizeis 0.size – Number of bytes in the array.
-
void utils_elt_swap(void *elt1, void *elt2, size_t elt_size)#
Swap two elements of any size.
Works on raw bytes through a small fixed-size buffer, so the elements need no particular alignment and there is no limit on their size. Swapping an element with itself does nothing; otherwise the elements must not overlap.
- Parameters:
elt1 – The first element.
elt2 – The second element.
elt_size – Size of each element in bytes.
-
status_t utils_string_gen(char *buf, size_t len)#
Fill
bufwith a random printable ASCII string.Fills
len- 1 characters from the printable ASCII range [33, 126] and appends a null terminator, sostrlen(buf)== len - 1 afterward.Characters come from the C library’s random(). Not reentrant, and not suitable for anything security-related.
- Parameters:
buf – Buffer to fill. Must be at least
lenbytes.len – Total buffer size including the null terminator.
- Returns:
rcsw/utils/checksum.h#
Checksumming functions for embedded applications.
Functions
-
uint8_t utils_xchks8(const uint8_t *buf, size_t n_bytes, uint8_t seed)#
Compute an 8-bit checksum (XOR, rotate)
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
Computed 8-bit checksum
-
uint16_t utils_xchks16(const uint16_t *buf, size_t n_bytes, uint16_t seed)#
Compute an 16-bit checksum (XOR, rotate)
buf must be 16-bit aligned and n_bytes a multiple of 2; otherwise 0xFFFF is returned (indistinguishable from a real checksum, so check alignment beforehand).
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
computed 16-bit checksum
-
uint32_t utils_xchks32(const uint32_t *buf, size_t n_bytes, uint32_t seed)#
Compute an 32-bit checksum (XOR, rotate)
buf must be 32-bit aligned and n_bytes a multiple of 4; otherwise 0xFFFFFFFF is returned (indistinguishable from a real checksum, so check alignment beforehand).
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
Computed 32-bit checksum
-
uint8_t utils_achks8(const uint8_t *buf, size_t n_bytes, uint8_t seed)#
Compute an 8-bit checksum (add-ignore-carry)
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
computed 8-bit checksum
-
uint16_t utils_achks16(const uint16_t *buf, size_t n_bytes, uint16_t seed)#
Compute an 16-bit checksum (add-ignore-carry)
buf must be 16-bit aligned and n_bytes a multiple of 2; otherwise 0xFFFF is returned (indistinguishable from a real checksum, so check alignment beforehand).
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
computed 16-bit checksum
-
uint32_t utils_achks32(const uint32_t *buf, size_t n_bytes, uint32_t seed)#
Compute an 32-bit checksum (add-ignore-carry)
buf must be 32-bit aligned and n_bytes a multiple of 4; otherwise 0xFFFFFFFF is returned (indistinguishable from a real checksum, so check alignment beforehand).
- Parameters:
buf – Buffer to compute over
n_bytes – # bytes in buffer
seed – Starting value for checksum
- Returns:
computed 32-bit checksum
-
uint32_t utils_crc32(const uint8_t *buf, size_t n_bytes, uint32_t crc)#
Compute the standard CRC-32 (IEEE 802.3 / Ethernet, zlib, PNG) of a buffer, using a 256-entry lookup table.
Polynomial 0x04C11DB7, reflected input and output, initial value and final XOR 0xFFFFFFFF: the CRC of “123456789” is 0xCBF43926. Derived from Gary S. Brown’s public-domain table-driven implementation (1986).
The table is 1 KiB of constant data. Where that is too much, use utils_crc32_bitwise(), which returns the same values without a table and is several times slower. The two are in separate object files, so only the one used is linked in.
- Parameters:
buf – Buffer to compute over. May be NULL if
n_bytesis 0.n_bytes – # bytes in buffer.
crc – 0 to start a new CRC, or the result of a previous call to continue it over another buffer.
- Returns:
The CRC.
-
uint32_t utils_crc32_bitwise(const uint8_t *buf, size_t n_bytes, uint32_t crc)#
Same as utils_crc32(), computed bit by bit without a lookup table: smaller, but slower.
rcsw/utils/hash.h#
A collection of hashing algorithms.
Functions
-
status_t utils_hash_default(const void *data, size_t len, uint32_t *hash)#
Compute a hash over
data(Bob Jenkins / Wikipedia algorithm).- Parameters:
data – Pointer to the data to hash.
len – Number of bytes to hash.
hash – The computed hash.
- Returns:
-
status_t utils_hash_fnv1a(const void *data, size_t len, uint32_t *hash)#
Compute a hash over
datausing FNV-1a (Fowler–Noll–Vo).Good general-purpose hash with low collision rates, and a sensible choice for hashmap_config::hash.
- Parameters:
data – Pointer to the data to hash.
len – Number of bytes to hash.
hash – The computed hash.
- Returns:
rcsw/utils/mem.h#
Memory manipulation and dumping routines for use on embedded systems.
The dump functions print through DPRINTF(), i.e. the configured ER plugin’s print function, and print nothing when RCSW_ERL is NONE.
Functions
-
status_t utils_mem_write32(uintptr_t addr, uint32_t wval)#
Write a 32-bit register/memory location with a single 32-bit access.
- Parameters:
addr – The address of register/memory location to write. Must be 32-bit aligned.
wval – The value to write
- Returns:
-
uint32_t utils_mem_read32(uintptr_t addr)#
Read a 32-bit register/memory location.
- Parameters:
addr – The address of register/memory location to read from
- Returns:
The value of the register, or 0xFFFFFFFF if
addris not 32-bit aligned.
-
status_t utils_mem_rmwr32(uintptr_t addr, uint32_t wval, uint32_t mask)#
Read, modify, write, and readback a memory value.
Reading back the value written verifies the write was successful.
- Parameters:
addr – The address of register/memory location to write.
wval – The 32-bit value to write.
mask – The 32-bit insert mask. Only bits that are 1 are taken from
wval; bits that are 0 keep their current value. Pass 0xFFFFFFFF to write all 32 bits (the location is then not read first). Must not be 0. Every written bit is verified on readback.
- Returns:
-
void *utils_mem_cpy32(void *dest, const void *src, size_t n_bytes)#
Copy memory from source to dest in 32 bit chunks.
If src, dest or n_bytes is not 32-bit aligned, no action is performed. The source and destination must not overlap.
- Parameters:
dest – The destination
src – The source
n_bytes – How many bytes to copy
- Returns:
The destination
-
status_t utils_mem_dump32(const void *buf, size_t n_bytes)#
Dump 4 byte words in memory in hexadecimal (see the file description)
- Parameters:
buf – Address for start of dump. If this is not 32-bit aligned, ERROR is returned.
n_bytes – How large of a dump to take, in bytes
- Returns:
-
status_t utils_mem_dump16(const void *buf, size_t n_bytes)#
Dump 2 byte words in memory in hexadecimal (see the file description)
- Parameters:
buf – Address for start of dump. If this is not 16-bit aligned, ERROR is returned.
n_bytes – How large of a dump to take, in bytes
- Returns:
-
void utils_mem_dump8(const void *buf, size_t n_bytes)#
Dump 1 byte words in memory in hexadecimal (see the file description)
- Parameters:
buf – Address for start of dump
n_bytes – How large of a dump to take, in bytes
-
status_t utils_mem_dump32v(const void *buf, size_t n_bytes)#
Dump 4 byte words in memory in hexadecimal (see the file description), with offsets.
- Parameters:
buf – Address for start of dump. If this is not 32-bit aligned, ERROR is returned.
n_bytes – How large of a dump to take, in bytes
- Returns:
-
status_t utils_mem_dump16v(const void *buf, size_t n_bytes)#
Dump 2 byte words in memory in hexadecimal (see the file description), with offsets.
- Parameters:
buf – Address for start of dump. If this is not 16-bit aligned, ERROR is returned.
n_bytes – How large of a dump to take, in bytes
- Returns:
-
void utils_mem_dump8v(const void *buf, size_t n_bytes)#
Dump 1 byte words in memory in hexadecimal (see the file description), with offsets.
- Parameters:
buf – Address for start of dump
n_bytes – How large of a dump to take, in bytes
-
status_t utils_mem_bswap16(uint16_t *buf, size_t n_bytes)#
Reverse the byte order of each 16-bit word in a buffer, in place.
- Parameters:
buf – Memory to swap. Must be 16-bit aligned.
n_bytes – # bytes to swap. Must be a multiple of 2.
- Returns:
status_t. ERROR (nothing swapped) if an alignment requirement is not met.
-
status_t utils_mem_bswap32(uint32_t *buf, size_t n_bytes)#
Reverse the byte order of each 32-bit word in a buffer, in place (0xAABBCCDD becomes 0xDDCCBBAA).
- Parameters:
buf – Memory to swap. Must be 32-bit aligned.
n_bytes – # bytes to swap. Must be a multiple of 4.
- Returns:
status_t. ERROR (nothing swapped) if an alignment requirement is not met.
rcsw/utils/numeric.h#
Miscellaneous utility functions for numeric operations.
Functions
-
void utils_permute(void *arr, size_t n_elts, size_t elt_size, size_t start, void (*fp)(void *arr))#
Generate all permutations of elements [start, size) in
arr.fpis called once with each permutation.- Parameters:
arr – Array of elements to permute.
n_elts – Total number of elements in
arr.elt_size – Size of each element in bytes.
start – Index from which to begin permuting (pass 0 for all).
fp – Callback invoked for each permutation. Receives a pointer to the first element of the (reordered) array.
-
bool utils_zchk(void *elt, size_t elt_size)#
Test whether all bytes of an element are zero.
Checks byte by byte, so the element needs no particular alignment.
- Parameters:
elt – Pointer to the element to test.
elt_size – Size of the element in bytes.
- Returns:
true if all bytes are zero.
rcsw/utils/time.h#
Collection of struct timespec manipulation functions.
Note
These functions are POSIX-only and are not available in baremetal builds.
Functions
-
int utils_ts_cmp(const struct timespec *a, const struct timespec *b)#
Compare two timespecs.
- Parameters:
a – Timespec #1
b – Timespec #2
- Returns:
<0 if a < b, 0 if a == b, >0 if a > b.
-
void utils_ts_add(struct timespec *sum, const struct timespec *val)#
Add
valtosumin place.- Parameters:
sum – Timespec accumulator (modified in place).
val – Value to add.
-
void utils_ts_diff(const struct timespec *start, const struct timespec *end, struct timespec *diff)#
Compute the difference between two timespecs:
diff=end-start.Note
If
end<start, the result is undefined.- Parameters:
start – The value subtracted (subtrahend).
end – The value subtracted from (minuend).
diff – Output: result of end - start.
-
double utils_ts2mono(const struct timespec *ts)#
Convert a timespec to a monotonic scalar in seconds.
- Parameters:
ts – The timespec to convert.
- Returns:
Monotonic time in seconds as a
double.
-
uint64_t utils_ts2monons(const struct timespec *ts)#
Convert a timespec to a monotonic scalar in nanoseconds.
- Parameters:
ts – The timespec to convert.
- Returns:
Monotonic time in nanoseconds as a
uint64_t.
-
struct timespec utils_monons2ts(uint64_t val)#
Convert a monotonic nanosecond count to a timespec.
- Parameters:
val – Nanosecond count.
- Returns:
Equivalent
structtimespec.
-
status_t utils_ts_make_abs(const struct timespec *rel, struct timespec *out)#
Convert a relative timeout to an absolute one.
Uses clock_realtime() as the basis. Required by many POSIX blocking functions (e.g.,
pthread_cond_timedwait()).- Parameters:
rel – Relative timeout.
out – Output: absolute timeout equivalent to now +
rel.
- Returns:
-
status_t utils_ts_make_rel(const struct timespec *deadline, struct timespec *out)#
Compute the remaining relative timeout from an absolute deadline.
Subtracts the current wall-clock time from
deadlineto produce the remaining relative timeout inout. If the deadline has already passed,outis set to {0, 0} and ERROR is returned so callers can treat an expired deadline the same as a timed-out wait.- Parameters:
deadline – An absolute
CLOCK_REALTIMEdeadline, as produced by utils_ts_make_abs().out – Filled with the remaining relative timeout.
- Returns: