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--- .gitignore | 2 +- include/osmocom/core/bitXXgen.h.tpl | 100 ++++++++++++++++++++++++++++++++++++ include/osmocom/core/bits.h | 45 +++++++++++++++- tests/bits/bitrev_test.c | 92 ++++++++++++++++++++++++++++++++- tests/bits/bitrev_test.ok | 22 ++++++++ 5 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 include/osmocom/core/bitXXgen.h.tpl diff --git a/.gitignore b/.gitignore index 71b27f2..c85d04d 100644 --- a/.gitignore +++ b/.gitignore @@ -89,7 +89,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/osmocom/core/bitXXgen.h.tpl b/include/osmocom/core/bitXXgen.h.tpl new file mode 100644 index 0000000..5714251 --- /dev/null +++ b/include/osmocom/core/bitXXgen.h.tpl @@ -0,0 +1,100 @@ +/* + * 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++); +/* + + uint8_t i, adj = 8 * (8 - n); + uintXX_t y = (x << adj) >> adj; + for(i = 0; i < n; p[i] = (y >> 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++); + +/* + uint8_t i, adj = 8 * (8 - n); + uintXX_t y = (x << adj) >> adj; + for(i = 0; i < n; p[i] = (y >> ((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..c636d71 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 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/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c index 5eca990..6cf340e 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,11 +11,66 @@ 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 }; +char s[18], *p; + +void check_ls_64(uint8_t bytes) +{ + uint8_t T[bytes], D = (8 - bytes), A = 8 * D, C = 2 * D; + 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)); +} + +void check_ls_32(uint8_t bytes) +{ + uint8_t T[bytes], D = (4 - bytes), A = 8 * D, C = 2 * D; + 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; + + srand(time(NULL)); + for (offs = 0; offs < sizeof(out); offs++) { uint8_t *start = out + offs; uint8_t len = sizeof(out) - offs; @@ -32,5 +88,37 @@ 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 --------------030008030204080305010405 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"