From 0a060e683659e718d0a5e8c85e132d3dd6877a9d Mon Sep 17 00:00:00 2001 From: winckel Date: Fri, 15 Nov 2013 10:59:51 +0000 Subject: [PATCH] Created an asn1 conversions file in common utils to perform conversion from ASN1 structure to integers. git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4412 818b1a75-f10b-46b9-bf7c-635c3b92a50f --- common/utils/Makefile.inc | 3 +- common/utils/asn1_conversions.h | 121 ++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 common/utils/asn1_conversions.h diff --git a/common/utils/Makefile.inc b/common/utils/Makefile.inc index 021f1167c7..81b2c172a0 100644 --- a/common/utils/Makefile.inc +++ b/common/utils/Makefile.inc @@ -8,5 +8,6 @@ ITTI_OBJS += $(ITTI_DIR)/timer.o UTILS_OBJS = $(ITTI_OBJS) -UTILS_incl = \ +UTILS_incl = \ + -I$(COMMON_UTILS_DIR) \ -I$(ITTI_DIR) diff --git a/common/utils/asn1_conversions.h b/common/utils/asn1_conversions.h new file mode 100644 index 0000000000..265957f601 --- /dev/null +++ b/common/utils/asn1_conversions.h @@ -0,0 +1,121 @@ +/******************************************************************************* + + Eurecom OpenAirInterface 3 + Copyright(c) 1999 - 2010 Eurecom + + This program is free software; you can redistribute it and/or modify it + under the terms and conditions of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA. + + The full GNU General Public License is included in this distribution in + the file called "COPYING". + + Contact Information + Openair Admin: openair_admin@eurecom.fr + Openair Tech : openair_tech@eurecom.fr + Forums : http://forums.eurecom.fsr/openairinterface + Address : Eurecom, 2229, route des crĂȘtes, 06560 Valbonne Sophia Antipolis, France + +*******************************************************************************/ + +#ifndef ASN1_CONVERSIONS_H_ +#define ASN1_CONVERSIONS_H_ + +#include "BIT_STRING.h" +#include "UTIL/assertions.h" + +//-----------------------begin func ------------------- + +/*! \fn uint8_t BIT_STRING_to_uint8(BIT_STRING_t *) + *\brief This function extract at most a 8 bits value from a BIT_STRING_t object, the exact bits number depend on the BIT_STRING_t contents. + *\param[in] pointer to the BIT_STRING_t object. + *\return the extracted value. + */ +inline uint8_t BIT_STRING_to_uint8(BIT_STRING_t *asn) { + DevCheck ((asn->size == 1), asn->size, 0, 0); + + return asn->buf[0] >> asn->bits_unused; +} + +/*! \fn uint16_t BIT_STRING_to_uint16(BIT_STRING_t *) + *\brief This function extract at most a 16 bits value from a BIT_STRING_t object, the exact bits number depend on the BIT_STRING_t contents. + *\param[in] pointer to the BIT_STRING_t object. + *\return the extracted value. + */ +inline uint16_t BIT_STRING_to_uint16(BIT_STRING_t *asn) { + uint16_t result = 0; + int index = 0; + + DevCheck ((asn->size > 0) && (asn->size <= 2), asn->size, 0, 0); + + switch (asn->size) { + case 2: + result |= asn->buf[index++] << (8 - asn->bits_unused); + + case 1: + result |= asn->buf[index] >> asn->bits_unused; + break; + + default: + break; + } + + return result; +} + +/*! \fn uint32_t BIT_STRING_to_uint32(BIT_STRING_t *) + *\brief This function extract at most a 32 bits value from a BIT_STRING_t object, the exact bits number depend on the BIT_STRING_t contents. + *\param[in] pointer to the BIT_STRING_t object. + *\return the extracted value. + */ +inline uint32_t BIT_STRING_to_uint32(BIT_STRING_t *asn) { + uint32_t result = 0; + int index; + int shift; + + DevCheck ((asn->size > 0) && (asn->size <= 4), asn->size, 0, 0); + + shift = ((asn->size - 1) * 8) - asn->bits_unused; + for (index = 0; index < (asn->size - 1); index++) { + result |= asn->buf[index] << shift; + shift -= 8; + } + + result |= asn->buf[index] >> asn->bits_unused; + + return result; +} + +/*! \fn uint64_t BIT_STRING_to_uint64(BIT_STRING_t *) + *\brief This function extract at most a 64 bits value from a BIT_STRING_t object, the exact bits number depend on the BIT_STRING_t contents. + *\param[in] pointer to the BIT_STRING_t object. + *\return the extracted value. + */ +inline uint64_t BIT_STRING_to_uint64(BIT_STRING_t *asn) { + uint64_t result = 0; + int index; + int shift; + + DevCheck ((asn->size > 0) && (asn->size <= 8), asn->size, 0, 0); + + shift = ((asn->size - 1) * 8) - asn->bits_unused; + for (index = 0; index < (asn->size - 1); index++) { + result |= asn->buf[index] << shift; + shift -= 8; + } + + result |= asn->buf[index] >> asn->bits_unused; + + return result; +} + +#endif