---
include/osmocom/core/bits.h | 142 +++++++++++++++++++++++++++++++++++++++++++-
include/osmocom/core/msgb.h | 23 +++----
src/gsm/libosmogsm.map | 21 +++++++
tests/bits/bitrev_test.c | 19 +++++-
tests/bits/bitrev_test.ok | 1 +
5 files changed, 193 insertions(+), 13 deletions(-)
diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h
index 4c68532..fac87b8 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,136 @@ 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) */
+static inline uint16_t osmo_load16le(const uint8_t *p)
+{//Load unaligned 16-bit integer (little-endian encoding)
+ return (((uint16_t *)(p))[0] | (((uint16_t *)(p))[1] << 8));
+}
+
+static inline uint16_t osmo_load16be(const uint8_t *p)
+{//Load unaligned 16-bit integer (big-endian encoding)
+ return ((((uint16_t *)(p))[0] << 8) | ((uint16_t *)(p))[1]);
+}
+
+static inline uint32_t osmo_load24le(const uint8_t *p)
+{//Load unaligned 24-bit integer (little-endian encoding)
+ return (((uint32_t *)(p))[0] |
+ (((uint32_t *)(p))[1] << 8) |
+ (((uint32_t *)(p))[2] << 16));
+}
+
+static inline uint32_t osmo_load24be(const uint8_t *p)
+{//Load unaligned 24-bit integer (big-endian encoding)
+ return ((((uint32_t *)(p))[0] << 16) |
+ (((uint32_t *)(p))[1] << 8) |
+ ((uint32_t *)(p))[2]);
+}
+
+static inline uint32_t osmo_load32le(const uint8_t *p)
+{//Load unaligned 32-bit integer (little-endian encoding)
+ return (((uint32_t *)(p))[0] |
+ (((uint32_t *)(p))[1] << 8) |
+ (((uint32_t *)(p))[2] << 16) |
+ (((uint32_t *)(p))[3] << 24));
+}
+
+static inline uint32_t osmo_load32be(const uint8_t *p)
+{//Load unaligned 32-bit integer (big-endian encoding)
+ return ((((uint32_t *)(p))[0] << 24) |
+ (((uint32_t *)(p))[1] << 16) |
+ (((uint32_t *)(p))[2] << 8) |
+ ((uint32_t *)(p))[3]);
+}
+
+static inline uint64_t osmo_load64le(const uint8_t *p)
+{//Load unaligned 64-bit integer (little-endian encoding)
+ return (((uint64_t *)(p))[0] |
+ (((uint64_t *)(p))[1] << 8) |
+ (((uint64_t *)(p))[2] << 16) |
+ (((uint64_t *)(p))[3] << 24) |
+ (((uint64_t *)(p))[4] << 32) |
+ (((uint64_t *)(p))[5] << 40) |
+ (((uint64_t *)(p))[6] << 48) |
+ (((uint64_t *)(p))[7] << 56));
+}
+
+static inline uint64_t osmo_load64be(const uint8_t *p)
+{//Load unaligned 64-bit integer (big-endian encoding)
+ return ((((uint64_t *)(p))[0] << 56) |
+ (((uint64_t *)(p))[1] << 48) |
+ (((uint64_t *)(p))[2] << 40) |
+ (((uint64_t *)(p))[3] << 32) |
+ (((uint64_t *)(p))[4] << 24) |
+ (((uint64_t *)(p))[5] << 16) |
+ (((uint64_t *)(p))[6] << 8) |
+ ((uint64_t *)(p))[7]);
+}
+
+static inline void osmo_store16le(uint16_t a, uint8_t *p)
+{//Store unaligned 16-bit integer (little-endian encoding)
+ ((uint8_t *)(p))[0] = (a) & 0xFF;
+ ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF;
+}
+
+static inline void osmo_store16be(uint16_t a, uint8_t *p)
+{//Store unaligned 32-bit integer (big-endian encoding)
+ ((uint8_t *)(p))[0] = ((a) >> 8) & 0xFF;
+ ((uint8_t *)(p))[1] = (a) & 0xFF;
+}
+
+static inline void osmo_store24le(uint32_t a, uint8_t *p)
+{//Store unaligned 24-bit integer (little-endian encoding)
+ ((uint8_t *)(p))[0] = (a) & 0xFF;
+ ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF;
+ ((uint8_t *)(p))[2] = ((a) >> 16) & 0xFF;
+}
+
+static inline void osmo_store24be(uint32_t a, uint8_t *p)
+{//Store unaligned 24-bit integer (big-endian encoding)
+ ((uint8_t *)(p))[0] = ((a) >> 16) & 0xFF;
+ ((uint8_t *)(p))[1] = ((a) >> 8) & 0xFF;
+ ((uint8_t *)(p))[2] = (a) & 0xFF;
+}
+
+static inline void osmo_store32le(uint32_t a, uint8_t *p)
+{//Store unaligned 32-bit integer (little-endian encoding)
+ ((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;
+}
+
+static inline void osmo_store32be(uint32_t a, uint8_t *p)
+{//Store unaligned 32-bit integer (big-endian encoding)
+ ((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;
+}
+
+static inline void osmo_store64le(uint64_t a, uint8_t *p)
+{//Store unaligned 64-bit integer (little-endian encoding)
+ ((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;
+}
+
+static inline void osmo_store64be(uint64_t a, uint8_t *p)
+{//Store unaligned 64-bit integer (big-endian encoding)
+ ((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 +203,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..72fdc24 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;
+ osmo_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;
+ osmo_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 osmo_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 osmo_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 osmo_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 osmo_load32be(space);
}
/*! \brief Increase headroom of empty msgb, reducing the tailroom
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 9d15d66..41fff21 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -22,6 +22,23 @@ abis_nm_test_name;
osmo_sitype_strs;
+osmo_load16le;
+osmo_load16be;
+osmo_load24le;
+osmo_load24be;
+osmo_load32le;
+osmo_load32be;
+osmo_load64le;
+osmo_load64be;
+osmo_store16le;
+osmo_store16be;
+osmo_store24le;
+osmo_store24be;
+osmo_store32le;
+osmo_store32be;
+osmo_store64le;
+osmo_store64be;
+
comp128;
dbm2rxlev;
@@ -196,6 +213,10 @@ osmo_a5;
osmo_a5_1;
osmo_a5_2;
+_kasumi;
+_kasumi_key_expand;
+_kasumi_kgcore;
+
osmo_auth_alg_name;
osmo_auth_alg_parse;
osmo_auth_gen_vec;
diff --git a/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c
index 5eca990..2e3b44f 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();
+ osmo_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
--------------040302010900050708060301
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"