diff options
| -rw-r--r-- | include/osmocom/core/Makefile.am | 7 | ||||
| -rw-r--r-- | include/osmocom/core/crcXXgen.h.tpl | 59 | ||||
| -rw-r--r-- | include/osmocom/core/crcgen.h | 41 | ||||
| -rw-r--r-- | src/Makefile.am | 7 | ||||
| -rw-r--r-- | src/crcXXgen.c.tpl | 120 | 
5 files changed, 232 insertions, 2 deletions
diff --git a/include/osmocom/core/Makefile.am b/include/osmocom/core/Makefile.am index f131269d..aabb775f 100644 --- a/include/osmocom/core/Makefile.am +++ b/include/osmocom/core/Makefile.am @@ -3,7 +3,8 @@ osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h bits.h \  		   gsmtap.h write_queue.h prim.h \  		   logging.h rate_ctr.h gsmtap_util.h \  		   crc16.h panic.h process.h \ -		   backtrace.h conv.h application.h +		   backtrace.h conv.h application.h \ +		   crcgen.h crc8gen.h crc16gen.h crc32gen.h crc64gen.h  if ENABLE_PLUGIN  osmocore_HEADERS += plugin.h @@ -22,3 +23,7 @@ osmocore_HEADERS += serial.h  endif  osmocoredir = $(includedir)/osmocom/core + +crc%gen.h: crcXXgen.h.tpl +	@echo "  SED    $< -> $@" +	@sed -e's/XX/$*/g' $< > $@ diff --git a/include/osmocom/core/crcXXgen.h.tpl b/include/osmocom/core/crcXXgen.h.tpl new file mode 100644 index 00000000..b411276e --- /dev/null +++ b/include/osmocom/core/crcXXgen.h.tpl @@ -0,0 +1,59 @@ +/* + * crcXXgen.h + * + * Copyright (C) 2011  Sylvain Munaut <tnt@246tNt.com> + * + * 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. + */ + +#ifndef __OSMO_CRCXXGEN_H__ +#define __OSMO_CRCXXGEN_H__ + +/*! \addtogroup crcgen + *  @{ + */ + +/*! \file crcXXgen.h + *  \file Osmocom generic CRC routines (for max XX bits poly) header + */ + + +#include <stdint.h> +#include <osmocom/core/bits.h> + + +/*! \brief structure describing a given CRC code of max XX bits */ +struct osmo_crcXXgen_code { +	int bits;           /*!< \brief Actual number of bits of the CRC */ +	uintXX_t poly;      /*!< \brief Polynom (normal representation, MSB omitted */ +	uintXX_t init;      /*!< \brief Initialization value of the CRC state */ +	uintXX_t remainder; /*!< \brief Remainder of the CRC (final XOR) */ +}; + +uintXX_t osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code, +                                    const ubit_t *in, int len); +int osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code, +                             const ubit_t *in, int len, const ubit_t *crc_bits); +void osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code, +                            const ubit_t *in, int len, ubit_t *crc_bits); + + +/*! }@ */ + +#endif /* __OSMO_CRCXXGEN_H__ */ + +/* vim: set syntax=c: */ diff --git a/include/osmocom/core/crcgen.h b/include/osmocom/core/crcgen.h new file mode 100644 index 00000000..cd916c76 --- /dev/null +++ b/include/osmocom/core/crcgen.h @@ -0,0 +1,41 @@ +/* + * crcgen.h + * + * Copyright (C) 2011  Sylvain Munaut <tnt@246tNt.com> + * + * 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. + */ + +#ifndef __OSMO_CRCGEN_H__ +#define __OSMO_CRCGEN_H__ + +/*! \defgroup crcgen Osmocom generic CRC routines + *  @{ + */ + +/*! \file crcgen.h + *  \file Osmocom generic CRC routines global header + */ + +#include <osmocom/core/crc8gen.h> +#include <osmocom/core/crc16gen.h> +#include <osmocom/core/crc32gen.h> +#include <osmocom/core/crc64gen.h> + +/*! }@ */ + +#endif /* __OSMO_CRCGEN_H__ */ diff --git a/src/Makefile.am b/src/Makefile.am index 739095c8..b0ccaddb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,8 @@ libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c bits.c \  			 write_queue.c utils.c socket.c \  			 logging.c logging_syslog.c rate_ctr.c \  			 gsmtap_util.c crc16.c panic.c backtrace.c \ -			 conv.c application.c +			 conv.c application.c \ +			 crc8gen.c crc16gen.c crc32gen.c crc64gen.c  if ENABLE_PLUGIN  libosmocore_la_SOURCES += plugin.c @@ -34,3 +35,7 @@ endif  if ENABLE_SERIAL  libosmocore_la_SOURCES += serial.c  endif + +crc%gen.c: crcXXgen.c.tpl +	@echo "  SED    $< -> $@" +	@sed -e's/XX/$*/g' $< > $@ diff --git a/src/crcXXgen.c.tpl b/src/crcXXgen.c.tpl new file mode 100644 index 00000000..5d70753a --- /dev/null +++ b/src/crcXXgen.c.tpl @@ -0,0 +1,120 @@ +/* + * crcXXgen.c + * + * Generic CRC routines (for max XX bits poly) + * + * Copyright (C) 2011  Sylvain Munaut <tnt@246tNt.com> + * + * 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. + */ + +/*! \addtogroup crcgen + *  @{ + */ + +/*! \file crcXXgen.c + *  \file Osmocom generic CRC routines (for max XX bits poly) + */ + +#include <stdint.h> + +#include <osmocom/core/bits.h> +#include <osmocom/core/crcXXgen.h> + + +/*! \brief 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 + */ +uintXX_t +osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code, +                           const ubit_t *in, int len) +{ +	const uintXX_t poly = code->poly; +	uintXX_t crc = code->init; +	int i, n = code->bits-1; + +	for (i=0; i<len; i++) { +		uintXX_t bit = in[i] & 1; +		crc ^= (bit << n); +		if (crc & (1 << n)) { +			crc <<= 1; +			crc ^= poly; +		} else { +			crc <<= 1; +		} +		crc &= (1ULL << code->bits) - 1; +	} + +	crc ^= code->remainder; + +	return crc; +} + + +/*! \brief 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_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code, +                         const ubit_t *in, int len, const ubit_t *crc_bits) +{ +	uintXX_t crc; +	int i; + +	crc = osmo_crcXXgen_compute_bits(code, in, len); + +	for (i=0; i<code->bits; i++) +		if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) +			return 1; + +	return 0; +} + + +/*! \brief 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_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code, +                       const ubit_t *in, int len, ubit_t *crc_bits) +{ +	uintXX_t crc; +	int i; + +	crc = osmo_crcXXgen_compute_bits(code, in, len); + +	for (i=0; i<code->bits; i++) +		crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); +} + +/*! }@ */ + +/* vim: set syntax=c: */  | 
