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.co--- .gitignore | 3 +- include/Makefile.am | 6 ++ include/osmocom/core/bitXXgen.h.tpl | 86 +++++++++++++++++++++++++++++ include/osmocom/core/bits.h | 45 ++++++++++++++- include/osmocom/core/msgb.h | 23 ++++---- tests/bits/bitrev_test.c | 107 ++++++++++++++++++++++++++++++++++-- tests/bits/bitrev_test.ok | 22 ++++++++ 7 files changed, 276 insertions(+), 16 deletions(-) create mode 100644 include/osmocom/core/bitXXgen.h.tpl diff --git a/.gitignore b/.gitignore index 71b27f2..3fa1bbc 100644 --- a/.gitignore +++ b/.gitignore @@ -55,6 +55,7 @@ tests/testsuite tests/testsuite.dir/ tests/testsuite.log +tests/utils/utils_test tests/sms/sms_test tests/timer/timer_test tests/msgfile/msgfile_test @@ -89,7 +90,7 @@ doc/html.tar src/crc*gen.c include/osmocom/core/crc*gen.h - +include/osmocom/core/bit*gen.h # vi files *.sw? diff --git a/include/Makefile.am b/include/Makefile.am index b035906..18011ff 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,6 +2,8 @@ nobase_include_HEADERS = \ osmocom/codec/codec.h \ osmocom/core/application.h \ osmocom/core/backtrace.h \ + osmocom/core/bit32gen.h \ + osmocom/core/bit64gen.h \ osmocom/core/bits.h \ osmocom/core/bitvec.h \ osmocom/core/conv.h \ @@ -107,6 +109,10 @@ endif noinst_HEADERS = osmocom/core/timer_compat.h +osmocom/core/bit%gen.h: osmocom/core/bitXXgen.h.tpl + $(AM_V_GEN)$(MKDIR_P) $(dir $@) + $(AM_V_GEN)sed -e's/XX/$*/g' $< > $@ + osmocom/core/crc%gen.h: osmocom/core/crcXXgen.h.tpl $(AM_V_GEN)$(MKDIR_P) $(dir $@) $(AM_V_GEN)sed -e's/XX/$*/g' $< > $@ diff --git a/include/osmocom/core/bitXXgen.h.tpl b/include/osmocom/core/bitXXgen.h.tpl new file mode 100644 index 0000000..1deffb9 --- /dev/null +++ b/include/osmocom/core/bitXXgen.h.tpl @@ -0,0 +1,86 @@ +/* + * bitXXgen.h + * + * Copyright (C) 2014 Max <max.suraev at fairwaves.ru> + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +/* Load unaligned n-byte integer (little-endian encoding) into uintXX_t */ +static inline uintXX_t osmo_loadXXle_ext(const uint8_t *p, uint8_t n) +{ + uint8_t i; + uintXX_t r = 0; + for(i = 0; i < n; r |= ((uintXX_t)p[i] << (8 * i)), i++); + return r; +} + +/* Load unaligned n-byte integer (big-endian encoding) into uintXX_t */ +static inline uintXX_t osmo_loadXXbe_ext(const uint8_t *p, uint8_t n) +{ + uint8_t i; + uintXX_t r = 0; + for(i = 0; i < n; r |= ((uintXX_t)p[i] << (XX - 8* (1 + i))), i++); + return r; +} + + +/* Store unaligned n-byte integer (little-endian encoding) into uintXX_t */ +static inline void osmo_storeXXle_ext(uintXX_t x, uint8_t *p, uint8_t n) +{ + uint8_t i; + for(i = 0; i < n; p[i] = (x >> i * 8) & 0xFF, i++); +} + +/* Store unaligned n-byte integer (big-endian encoding) into uintXX_t */ +static inline void osmo_storeXXbe_ext(uintXX_t x, uint8_t *p, uint8_t n) +{ + uint8_t i; + for(i = 0; i < n; p[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++); +} + + +/* Convenience function for most-used cases */ + + +/* Load unaligned XX-bit integer (little-endian encoding) */ +static inline uintXX_t osmo_loadXXle(const uint8_t *p) +{ + return osmo_loadXXle_ext(p, XX / 8); +} + +/* Load unaligned XX-bit integer (big-endian encoding) */ +static inline uintXX_t osmo_loadXXbe(const uint8_t *p) +{ + return osmo_loadXXbe_ext(p, XX / 8); +} + + +/* Store unaligned XX-bit integer (little-endian encoding) */ +static inline void osmo_storeXXle(uintXX_t x, uint8_t *p) +{ + return osmo_storeXXle_ext(x, p, XX / 8); +} + +/* Store unaligned XX-bit integer (big-endian encoding) */ +static inline void osmo_storeXXbe(uintXX_t x, uint8_t *p) +{ + return osmo_storeXXbe_ext(x, p, XX / 8); +} + + diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h index 4c68532..09d163d 100644 --- a/include/osmocom/core/bits.h +++ b/include/osmocom/core/bits.h @@ -2,7 +2,9 @@ #define _OSMO_BITS_H #include <stdint.h> - +#include <stddef.h> +#include <osmocom/core/bit32gen.h> +#include <osmocom/core/bit64gen.h> /*! \defgroup bits soft, unpacked and packed bits * @{ */ @@ -15,6 +17,37 @@ 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 unaligned 16-bit integer (little-endian encoding) */ +static inline uint16_t osmo_load16le(const uint8_t *p) +{ + return p[0] | (p[1] << 8); +} + +/* Load unaligned 16-bit integer (big-endian encoding) */ +static inline uint16_t osmo_load16be(const uint8_t *p) +{ + return (p[0] << 8) | p[1]; +} + +/* Store unaligned 16-bit integer (little-endian encoding) */ +static inline void osmo_store16le(uint16_t a, uint8_t *p) +{ + p[0] = a & 0xFF; + p[1] = (a >> 8) & 0xFF; +} + +/* Store unaligned 16-bit integer (big-endian encoding) */ +static inline void osmo_store16be(uint16_t a, uint8_t *p) +{ + p[0] = (a >> 8) & 0xFF; + p[1] = a & 0xFF; +} + +/* + Less trivial LE/BE functions (24 bits and above) are autogenerated + see included bitXXgen.h files +*/ + /* 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 +106,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/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c index 5eca990..9d5882c 100644 --- a/tests/bits/bitrev_test.c +++ b/tests/bits/bitrev_test.c @@ -1,8 +1,9 @@ - +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> +#include <time.h> #include <osmocom/core/utils.h> #include <osmocom/core/bits.h> @@ -10,12 +11,75 @@ static const uint8_t input[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; +static char s[18], *p; + +static void check_ls_64(uint8_t bytes) +{ + /* calculate various adjustment constants (number of bits, bytes, octets etc.) + based on number of bytes in type we actually test */ + uint8_t T[bytes], D = (8 - bytes), A = 8 * D, C = 2 * D; + /* zero exceeding bytes to avoid polluting test results */ + uint64_t _test = ((uint64_t)rand() << 32) + rand(), a_test = (_test << A) >> A; + + osmo_store64be_ext(_test, T, bytes); + snprintf(s, 17, "%.16" PRIx64, a_test); + p = osmo_hexdump_nospc(T, bytes); + if (0 != memcmp(s + C, p, bytes)) + printf("%s\t%s\t%u BE store FAILED!\n", s + C, p, bytes * 8); + else + printf("%u BE store OK\n", bytes * 8); + + osmo_store64le_ext(_test, T, bytes); + if (osmo_load64le_ext(T, bytes) == a_test) + printf("%u LE OK\n", bytes * 8); + else + printf("%u LE FAILED on %s- loaded %.16" PRIx64 " instead of %.16" PRIx64 "\n", bytes * 8, osmo_hexdump(T, bytes), osmo_load64le_ext(T, bytes), a_test); + + osmo_store64be_ext(_test, T, bytes); + if (osmo_load64be_ext(T, bytes) == (a_test << A)) + printf("%u BE OK\n", bytes * 8); + else + printf("%u BE FAILED on %s- loaded %.16" PRIx64 " instead of %.16" PRIx64 "\n", bytes * 8, osmo_hexdump(T, bytes), osmo_load64be_ext(T, bytes), (a_test << A)); +} + +static void check_ls_32(uint8_t bytes) +{ + /* calculate various adjustment constants (number of bits, bytes, octets etc.) + based on number of bytes in type we actually test */ + uint8_t T[bytes], D = (4 - bytes), A = 8 * D, C = 2 * D; + /* zero exceeding bytes to avoid polluting test results */ + uint32_t _test = rand(), a_test = (_test << A) >> A; + + osmo_store32be_ext(_test, T, bytes); + snprintf(s, 17, "%.8" PRIx32, a_test); + p = osmo_hexdump_nospc(T, bytes); + if (0 != memcmp(s + C, p, bytes)) + printf("%s\t%s\t%u BE store FAILED on %" PRIx32 "\n", s + C, p, bytes * 8, _test); + else + printf("%u BE store OK\n", bytes * 8); + + osmo_store32le_ext(_test, T, bytes); + if (osmo_load32le_ext(T, bytes) == a_test) + printf("%u LE OK\n", bytes * 8); + else + printf("%u LE FAILED on %s- loaded %.8" PRIx32 " instead of %.8" PRIx32 "\n", bytes * 8, osmo_hexdump(T, bytes), osmo_load32le_ext(T, bytes), a_test); + + osmo_store32be_ext(_test, T, bytes); + if (osmo_load32be_ext(T, bytes) == (a_test << A)) + printf("%u BE OK\n", bytes * 8); + else + printf("%u BE FAILED on %s- loaded %.8" PRIx32 " instead of %.8" PRIx32 "\n", bytes * 8, osmo_hexdump(T, bytes), osmo_load32be_ext(T, bytes), (a_test << A)); +} + 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++) { + srand(time(NULL)); + + for (offs = 0; offs < sizeof(out); offs++) + { uint8_t *start = out + offs; uint8_t len = sizeof(out) - offs; @@ -24,7 +88,8 @@ int main(int argc, char **argv) printf("INORDER: %s\n", osmo_hexdump(start, len)); osmo_revbytebits_buf(start, len); printf("REVERSED: %s\n", osmo_hexdump(start, len)); - if (memcmp(start, exp_out + offs, len)) { + if (memcmp(start, exp_out + offs, len)) + { printf("EXPECTED: %s\n", osmo_hexdump(exp_out+offs, len)); fprintf(stderr, "REVERSED != EXPECTED!\n"); exit(1); @@ -32,5 +97,39 @@ int main(int argc, char **argv) printf("\n"); } + printf("checking byte packing...\n"); + + check_ls_64(8); + check_ls_64(7); + check_ls_64(6); + check_ls_64(5); + check_ls_32(4); + check_ls_32(3); + + uint16_t _test16 = (uint16_t)rand(); + osmo_store16be(_test16, test); + + snprintf(s, 17, "%.4" PRIx16, _test16); + p = osmo_hexdump_nospc(test, 2); + if (0 != memcmp(s, p, 2)) + { + printf ("%s\t", s); + printf ("%s\t", p); + printf("16 BE FAILED on %" PRIx16 "\n"); + } else + printf("16 BE store OK\n"); + + osmo_store16le(_test16, test); + if (osmo_load16le(test) == _test16) + printf("16 LE OK\n"); + else + printf("16 LE FAILED: %s, %.4" PRIx16 ", %.4" PRIx16 "\n", osmo_hexdump(test, 2), osmo_load16le(test), _test16); + + osmo_store16be(_test16, test); + if (osmo_load16be(test) == _test16) + printf("16 BE OK\n"); + else + printf("16 BE FAILED: %s, %.4" PRIx16 ", %.4" PRIx16 "\n", osmo_hexdump(test, 2), osmo_load16be(test), _test16); + return 0; } diff --git a/tests/bits/bitrev_test.ok b/tests/bits/bitrev_test.ok index 47f402f..0cbc4db 100644 --- a/tests/bits/bitrev_test.ok +++ b/tests/bits/bitrev_test.ok @@ -22,3 +22,25 @@ REVERSED: 02 01 INORDER: 80 REVERSED: 01 +checking byte packing... +64 BE store OK +64 LE OK +64 BE OK +56 BE store OK +56 LE OK +56 BE OK +48 BE store OK +48 LE OK +48 BE OK +40 BE store OK +40 LE OK +40 BE OK +32 BE store OK +32 LE OK +32 BE OK +24 BE store OK +24 LE OK +24 BE OK +16 BE store OK +16 LE OK +16 BE OK -- 1.8.3.2 --------------020709010205080901010203 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"