# HG changeset patch # User Mychaela Falconia # Date 1679100741 28800 # Node ID 05d01e810217af6159ebaccb06c184132f8f1150 # Parent 1f9a6cede2c54dabf5b5e869cc51cca7cd69ebd6 libutil: add TFO message gen function based on Osmocom crc8gen diff -r 1f9a6cede2c5 -r 05d01e810217 libutil/Makefile --- a/libutil/Makefile Fri Mar 17 14:31:54 2023 -0800 +++ b/libutil/Makefile Fri Mar 17 16:52:21 2023 -0800 @@ -1,7 +1,7 @@ CC= gcc CFLAGS= -O2 -OBJS= dtmf_valid.o extdigits.o imsi_entry.o mncc_debug.o mncc_utils.o \ - nanp_valid.o numstring.o sockinit.o +OBJS= bitfunc.o crc8gen.o dtmf_valid.o extdigits.o imsi_entry.o mncc_debug.o \ + mncc_utils.o nanp_valid.o numstring.o sockinit.o tfo_msg_enc.o LIB= libutil.a all: ${LIB} diff -r 1f9a6cede2c5 -r 05d01e810217 libutil/bitfunc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/bitfunc.c Fri Mar 17 16:52:21 2023 -0800 @@ -0,0 +1,14 @@ +/* + * Some bit manipulation functions. + */ + +#include +#include "osmo_bits.h" + +void uint16_to_bits(uint16_t word, ubit_t *out, unsigned nbits) +{ + unsigned n; + + for (n = 0; n < nbits; n++) + out[n] = (word >> (nbits-n-1)) & 1; +} diff -r 1f9a6cede2c5 -r 05d01e810217 libutil/crc8gen.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/crc8gen.c Fri Mar 17 16:52:21 2023 -0800 @@ -0,0 +1,115 @@ +/*! \file crc8gen.c + * Osmocom generic CRC routines (for max 8 bits poly). */ +/* + * Copyright (C) 2011 Sylvain Munaut + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * 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. + */ + +/*! \addtogroup crc + * @{ + * Osmocom generic CRC routines (for max 8 bits poly). + * + * \file crc8gen.c.tpl */ + +#include +#include "osmo_bits.h" + +/*! Compute the CRC value of a given array of hard-bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \returns The CRC value + */ +uint8_t +osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len) +{ + const uint8_t poly = code->poly; + uint8_t crc = code->init; + int i, n = code->bits-1; + + for (i=0; ibits) - 1; + } + + crc ^= code->remainder; + + return crc; +} + + +/*! Checks the CRC value of a given array of hard-bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \param[in] crc_bits Array of hard bits with the alleged CRC + * \returns 0 if CRC matches. 1 in case of error. + * + * The crc_bits array must have a length of code->len + */ +int +osmo_crc8gen_check_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len, const ubit_t *crc_bits) +{ + uint8_t crc; + int i; + + crc = osmo_crc8gen_compute_bits(code, in, len); + + for (i=0; ibits; i++) + if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) + return 1; + + return 0; +} + + +/*! Computes and writes the CRC value of a given array of bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \param[in] crc_bits Array of hard bits to write the computed CRC to + * + * The crc_bits array must have a length of code->len + */ +void +osmo_crc8gen_set_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len, ubit_t *crc_bits) +{ + uint8_t crc; + int i; + + crc = osmo_crc8gen_compute_bits(code, in, len); + + for (i=0; ibits; i++) + crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); +} + +/*! @} */ + +/* vim: set syntax=c: */ diff -r 1f9a6cede2c5 -r 05d01e810217 libutil/osmo_bits.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/osmo_bits.h Fri Mar 17 16:52:21 2023 -0800 @@ -0,0 +1,35 @@ +/* + * This include file has been put together from Osmocom (specifically + * libosmocore) header files, containing definitions for bit vector + * manipulation and CRC functions. + */ + +/* from bits.h */ + +/*! unpacked bit (0 or 1): 1 bit per byte */ +typedef uint8_t ubit_t; + +/* from crc8gen.h */ + +/*! structure describing a given CRC code of max 8 bits */ +struct osmo_crc8gen_code { + int bits; /*!< Actual number of bits of the CRC */ + uint8_t poly; /*!< Polynom (normal representation, MSB omitted */ + uint8_t init; /*!< Initialization value of the CRC state */ + uint8_t remainder; /*!< Remainder of the CRC (final XOR) */ +}; + +uint8_t osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len); +int osmo_crc8gen_check_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len, const ubit_t *crc_bits); +void osmo_crc8gen_set_bits(const struct osmo_crc8gen_code *code, + const ubit_t *in, int len, ubit_t *crc_bits); + +/* + * Themyscira-added (not Osmocom-originating) functions + * that use the ubit_t defined type that (to our knowledge) + * was invented by Osmocom. + */ + +void uint16_to_bits(uint16_t word, ubit_t *out, unsigned nbits); diff -r 1f9a6cede2c5 -r 05d01e810217 libutil/tfo_msg_enc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/tfo_msg_enc.c Fri Mar 17 16:52:21 2023 -0800 @@ -0,0 +1,33 @@ +/* + * The function implemented in this module encodes a GSM 08.62 Extension_Block. + */ + +#include +#include "osmo_bits.h" + +/* + * EFR TRAU parity + * + * g(x) = x^3 + x^1 + 1 + */ +static const struct osmo_crc8gen_code gsm0860_efr_crc3 = { + .bits = 3, + .poly = 0x3, + .init = 0x0, + .remainder = 0x7, +}; + +void +encode_tfo_ext_words(bits_2_10, bits_12_15, ex, out) + unsigned bits_2_10, bits_12_15, ex; + uint16_t *out; +{ + ubit_t crc_in[13]; + uint8_t crc; + + uint16_to_bits(bits_2_10, crc_in, 9); + uint16_to_bits(bits_12_15, crc_in + 9, 4); + crc = osmo_crc8gen_compute_bits(&gsm0860_efr_crc3, crc_in, 13); + out[0] = bits_2_10; + out[1] = (bits_12_15 << 5) | (crc << 2) | ex; +}