This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/baseband-devel@lists.osmocom.org/.
Max max.suraev at fairwaves.ru--- include/osmocom/core/bits.h | 65 ++++++++++++++++++++++++++++++++++++++++++++- include/osmocom/core/msgb.h | 23 +++++++++------- tests/bits/bitrev_test.c | 19 +++++++++++-- tests/bits/bitrev_test.ok | 1 + 4 files changed, 95 insertions(+), 13 deletions(-) diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h index 4c68532..ade6113 100644 --- a/include/osmocom/core/bits.h +++ b/include/osmocom/core/bits.h @@ -2,7 +2,7 @@ #define _OSMO_BITS_H #include <stdint.h> - +#include <stddef.h> /*! \defgroup bits soft, unpacked and packed bits * @{ */ @@ -15,6 +15,59 @@ typedef int8_t sbit_t; /*!< \brief soft bit (-127...127) */ typedef uint8_t ubit_t; /*!< \brief unpacked bit (0 or 1) */ typedef uint8_t pbit_t; /*!< \brief packed bis (8 bits in a byte) */ + +// load/store macro taken from GPL code by Oryx Embedded. + +//Load unaligned 16-bit integer (little-endian encoding) +#define LOAD16LE(p) ((uint16_t) (((uint8_t *)(p))[0] | (((uint8_t *)(p))[1] << 8))) + +//Load unaligned 16-bit integer (big-endian encoding) +#define LOAD16BE(p) ((uint16_t) ((((uint8_t *)(p))[0] << 8) | ((uint8_t *)(p))[1])) + +//Load unaligned 24-bit integer (little-endian encoding) +#define LOAD24LE(p) ((uint32_t) (((uint8_t *)(p))[0] | (((uint8_t *)(p))[1] << 8) | (((uint8_t *)(p))[2] << 16))) + +//Load unaligned 24-bit integer (big-endian encoding) +#define LOAD24BE(p) ((uint32_t) ((((uint8_t *)(p))[0] << 16) | (((uint8_t *)(p))[1] << 8) | ((uint8_t *)(p))[2])) + +//Load unaligned 32-bit integer (little-endian encoding) +#define LOAD32LE(p) ((uint32_t) (((uint8_t *)(p))[0] | (((uint8_t *)(p))[1] << 8) | (((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24))) + +//Load unaligned 32-bit integer (big-endian encoding) +#define LOAD32BE(p) ((uint32_t) ((((uint8_t *)(p))[0] << 24) | (((uint8_t *)(p))[1] << 16) | (((uint8_t *)(p))[2] << 8) | ((uint8_t *)(p))[3])) + +//Load unaligned 64-bit integer (little-endian encoding) +#define LOAD64LE(p) ((uint64_t) (((uint8_t *)(p))[0] | (((uint8_t *)(p))[1] << 8) | (((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24) | (((uint8_t *)(p))[4] << 32) | (((uint8_t *)(p))[5] << 40) | (((uint8_t *)(p))[6] << 48) | (((uint8_t *)(p))[7] << 56))) + +//Load unaligned 64-bit integer (big-endian encoding) +#define LOAD64BE(p) ((uint64_t) ((((uint8_t *)(p))[0] << 56) | (((uint8_t *)(p))[1] << 48) | (((uint8_t *)(p))[2] << 40) | ((uint8_t *)(p))[3] << 32) | (((uint8_t *)(p))[4] << 24) | (((uint8_t *)(p))[5] << 16) | (((uint8_t *)(p))[6] << 8) | (((uint8_t *)(p))[7])) + +//Store unaligned 16-bit integer (little-endian encoding) +#define STORE16LE(a, p) ((uint8_t *)(p))[0] = (a) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF + +//Store unaligned 32-bit integer (big-endian encoding) +#define STORE16BE(a, p) ((uint8_t *)(p))[0] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[1] = (a) & 0xFF + +//Store unaligned 24-bit integer (little-endian encoding) +#define STORE24LE(a, p) ((uint8_t *)(p))[0] = (a) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[2] = ((a) >> 16) & 0xFF + +//Store unaligned 24-bit integer (big-endian encoding) +#define STORE24BE(a, p) ((uint8_t *)(p))[0] = ((a) >> 16) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[2] = (a) & 0xFF + +//Store unaligned 32-bit integer (little-endian encoding) +#define STORE32LE(a, p) ((uint8_t *)(p))[0] = (a) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[2] = ((a) >> 16) & 0xFF, ((uint8_t *)(p))[3] = ((a) >> 24) & 0xFF + +//Store unaligned 32-bit integer (big-endian encoding) +#define STORE32BE(a, p) ((uint8_t *)(p))[0] = ((a) >> 24) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 16) & 0xFF, ((uint8_t *)(p))[2] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[3] = (a) & 0xFF + +//Store unaligned 64-bit integer (little-endian encoding) +#define STORE64LE(a, p) ((uint8_t *)(p))[0] = (a) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[2] = ((a) >> 16) & 0xFF, ((uint8_t *)(p))[3] = ((a) >> 24) & 0xFF, ((uint8_t *)(p))[4] = ((a) >> 32) & 0xFF, ((uint8_t *)(p))[5] = ((a) >> 40) & 0xFF, ((uint8_t *)(p))[6] = ((a) >> 48) & 0xFF, ((uint8_t *)(p))[7] = ((a) >> 56) & 0xFF + +//Store unaligned 64-bit integer (big-endian encoding) +#define STORE64BE(a, p) ((uint8_t *)(p))[0] = ((a) >> 56) & 0xFF, ((uint8_t *)(p))[1] = ((a) >> 48) & 0xFF, ((uint8_t *)(p))[2] = ((a) >> 40) & 0xFF, ((uint8_t *)(p))[3] = ((a) >> 32) & 0xFF, ((uint8_t *)(p))[4] = ((a) >> 24) & 0xFF, ((uint8_t *)(p))[5] = ((a) >> 16) & 0xFF, ((uint8_t *)(p))[6] = ((a) >> 8) & 0xFF, ((uint8_t *)(p))[7] = (a) & 0xFF + + + /* NOTE on the endianess of pbit_t: Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit. @@ -73,6 +126,16 @@ uint32_t osmo_revbytebits_8(uint8_t x); /* \brief reverse the bits of each byte in a given buffer */ void osmo_revbytebits_buf(uint8_t *buf, int len); +/* \brief reverse the order of the bytes in a given buffer */ +void osmo_revbytes_buf(uint8_t *buf, size_t len); + +/* \brief left circular shift */ +static inline uint16_t rol16(uint16_t in, unsigned shift) +{ + return (in << shift) | (in >> (16 - shift)); +} + + /*! @} */ #endif /* _OSMO_BITS_H */ diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index fe2733b..d82c678 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -23,6 +23,7 @@ #include <stdint.h> #include <osmocom/core/linuxlist.h> #include <osmocom/core/utils.h> +#include <osmocom/core/bits.h> /*! \defgroup msgb Message buffers * @{ @@ -204,8 +205,7 @@ static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) { uint8_t *space = msgb_put(msgb, 2); - space[0] = word >> 8 & 0xFF; - space[1] = word & 0xFF; + STORE16BE(word, space); } /*! \brief append a uint32 value to the end of the message @@ -215,10 +215,7 @@ static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) { uint8_t *space = msgb_put(msgb, 4); - space[0] = word >> 24 & 0xFF; - space[1] = word >> 16 & 0xFF; - space[2] = word >> 8 & 0xFF; - space[3] = word & 0xFF; + STORE32BE(word, space); } /*! \brief remove data from end of message @@ -235,6 +232,7 @@ static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) msgb->len -= len; return tmp; } + /*! \brief remove uint8 from end of message * \param[in] msgb message buffer * \returns 8bit value taken from end of msgb @@ -244,6 +242,7 @@ static inline uint8_t msgb_get_u8(struct msgb *msgb) uint8_t *space = msgb_get(msgb, 1); return space[0]; } + /*! \brief remove uint16 from end of message * \param[in] msgb message buffer * \returns 16bit value taken from end of msgb @@ -251,8 +250,9 @@ static inline uint8_t msgb_get_u8(struct msgb *msgb) static inline uint16_t msgb_get_u16(struct msgb *msgb) { uint8_t *space = msgb_get(msgb, 2); - return space[0] << 8 | space[1]; + return LOAD16BE(space); } + /*! \brief remove uint32 from end of message * \param[in] msgb message buffer * \returns 32bit value taken from end of msgb @@ -260,7 +260,7 @@ static inline uint16_t msgb_get_u16(struct msgb *msgb) static inline uint32_t msgb_get_u32(struct msgb *msgb) { uint8_t *space = msgb_get(msgb, 4); - return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; + return LOAD32BE(space); } /*! \brief prepend (push) some data to start of message @@ -284,6 +284,7 @@ static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) msgb->len += len; return msgb->data; } + /*! \brief remove (pull) a header from the front of the message buffer * \param[in] msgb message buffer * \param[in] len number of octets to be pulled @@ -308,6 +309,7 @@ static inline uint8_t msgb_pull_u8(struct msgb *msgb) uint8_t *space = msgb_pull(msgb, 1) - 1; return space[0]; } + /*! \brief remove uint16 from front of message * \param[in] msgb message buffer * \returns 16bit value taken from end of msgb @@ -315,8 +317,9 @@ static inline uint8_t msgb_pull_u8(struct msgb *msgb) static inline uint16_t msgb_pull_u16(struct msgb *msgb) { uint8_t *space = msgb_pull(msgb, 2) - 2; - return space[0] << 8 | space[1]; + return LOAD16BE(space); } + /*! \brief remove uint32 from front of message * \param[in] msgb message buffer * \returns 32bit value taken from end of msgb @@ -324,7 +327,7 @@ static inline uint16_t msgb_pull_u16(struct msgb *msgb) static inline uint32_t msgb_pull_u32(struct msgb *msgb) { uint8_t *space = msgb_pull(msgb, 4) - 4; - return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; + return LOAD32BE(space); } /*! \brief Increase headroom of empty msgb, reducing the tailroom diff --git a/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c index 5eca990..8c82dce 100644 --- a/tests/bits/bitrev_test.c +++ b/tests/bits/bitrev_test.c @@ -1,4 +1,4 @@ - +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> @@ -12,7 +12,7 @@ static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x0 int main(int argc, char **argv) { - uint8_t out[ARRAY_SIZE(input)]; + uint8_t out[ARRAY_SIZE(input)], test[8]; unsigned int offs; for (offs = 0; offs < sizeof(out); offs++) { @@ -32,5 +32,20 @@ int main(int argc, char **argv) printf("\n"); } + printf("checking bit packer... "); + uint64_t _test = ((uint64_t)rand() << 32) + rand(); + STORE64BE(_test, test); + + char s[18], *p; + snprintf(s, 17, "%.16" PRIx64, _test); + p = osmo_hexdump_nospc(test, 8); + if (0 != memcmp(s, p, 8)) { + printf ("%s\t", s); + printf ("%s\t", p); + printf("FAILED!\n"); + return 2; + } + printf("OK\n"); + return 0; } diff --git a/tests/bits/bitrev_test.ok b/tests/bits/bitrev_test.ok index 47f402f..8a5fc6e 100644 --- a/tests/bits/bitrev_test.ok +++ b/tests/bits/bitrev_test.ok @@ -22,3 +22,4 @@ REVERSED: 02 01 INORDER: 80 REVERSED: 01 +checking bit packer... OK -- 1.8.3.2 --------------020601090900020101060502 Content-Type: text/x-patch; name="0002-Add-Kasumi-cipher-implementation.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0002-Add-Kasumi-cipher-implementation.patch"