1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#ifndef BASE_BYTES_H
5#define BASE_BYTES_H
6
7/**
8 * Byte-manipulation utilities.
9 *
10 * @defgroup Bytes Bytes
11 */
12
13/**
14 * Swaps the endianness of data. Each element is swapped individually by reversing its bytes.
15 *
16 * @ingroup Bytes
17 *
18 * @param data Pointer to data to be swapped.
19 * @param elem_size Size in bytes of each element.
20 * @param num Number of elements.
21 *
22 * @remark The caller must ensure that the data is at least `elem_size * num` bytes in size.
23 */
24void swap_endian(void *data, unsigned elem_size, unsigned num);
25
26/**
27 * Packs 4 big endian bytes into an unsigned.
28 *
29 * @ingroup Bytes
30 *
31 * @param bytes Pointer to an array of bytes that will be packed.
32 *
33 * @return The packed unsigned.
34 *
35 * @remark Assumes the passed array is least 4 bytes in size.
36 * @remark Assumes unsigned is 4 bytes in size.
37 *
38 * @see uint_to_bytes_be
39 */
40unsigned bytes_be_to_uint(const unsigned char *bytes);
41
42/**
43 * Packs an unsigned into 4 big endian bytes.
44 *
45 * @ingroup Bytes
46 *
47 * @param bytes Pointer to an array where the bytes will be stored.
48 * @param value The values that will be packed into the array.
49 *
50 * @remark Assumes the passed array is least 4 bytes in size.
51 * @remark Assumes unsigned is 4 bytes in size.
52 *
53 * @see bytes_be_to_uint
54 */
55void uint_to_bytes_be(unsigned char *bytes, unsigned value);
56
57#endif
58