import ans1helpers.[ch] (originally part of osmo-iuh)

This commit is contained in:
Harald Welte 2016-01-03 17:14:54 +01:00
parent 667d758c92
commit 2d4f2bd91c
4 changed files with 179 additions and 2 deletions

View File

@ -1,3 +1,3 @@
asn1cdir = $(includedir)/asn1c
asn1c_HEADERS = ANY.h asn_application.h asn_codecs.h asn_codecs_prim.h asn_internal.h asn_SEQUENCE_OF.h asn_SET_OF.h asn_system.h ber_decoder.h ber_tlv_length.h ber_tlv_tag.h BIT_STRING.h BMPString.h BOOLEAN.h constraints.h constr_CHOICE.h constr_SEQUENCE.h constr_SEQUENCE_OF.h constr_SET.h constr_SET_OF.h constr_TYPE.h der_encoder.h ENUMERATED.h GeneralizedTime.h GeneralString.h GraphicString.h IA5String.h INTEGER.h ISO646String.h NativeEnumerated.h NativeInteger.h NativeReal.h NULL.h NumericString.h ObjectDescriptor.h OBJECT_IDENTIFIER.h OCTET_STRING.h per_decoder.h per_encoder.h per_support.h PrintableString.h REAL.h RELATIVE-OID.h T61String.h TeletexString.h UniversalString.h UTCTime.h UTF8String.h VideotexString.h VisibleString.h xer_decoder.h xer_encoder.h xer_support.h per_opentype.h
asn1c_HEADERS = ANY.h asn_application.h asn_codecs.h asn_codecs_prim.h asn_internal.h asn_SEQUENCE_OF.h asn_SET_OF.h asn_system.h ber_decoder.h ber_tlv_length.h ber_tlv_tag.h BIT_STRING.h BMPString.h BOOLEAN.h constraints.h constr_CHOICE.h constr_SEQUENCE.h constr_SEQUENCE_OF.h constr_SET.h constr_SET_OF.h constr_TYPE.h der_encoder.h ENUMERATED.h GeneralizedTime.h GeneralString.h GraphicString.h IA5String.h INTEGER.h ISO646String.h NativeEnumerated.h NativeInteger.h NativeReal.h NULL.h NumericString.h ObjectDescriptor.h OBJECT_IDENTIFIER.h OCTET_STRING.h per_decoder.h per_encoder.h per_support.h PrintableString.h REAL.h RELATIVE-OID.h T61String.h TeletexString.h UniversalString.h UTCTime.h UTF8String.h VideotexString.h VisibleString.h xer_decoder.h xer_encoder.h xer_support.h per_opentype.h asn1helpers.h

View File

@ -0,0 +1,26 @@
#pragma once
#include <unistd.h>
#include "asn_system.h"
#include "BIT_STRING.h"
#include "OCTET_STRING.h"
void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in);
void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in);
void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in);
int BIT_STRING_fromBuf(BIT_STRING_t *st, const uint8_t *str, unsigned int bit_len);
void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in);
void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in);
int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n);
uint16_t asn1str_to_u16(const OCTET_STRING_t *in);
uint8_t asn1str_to_u8(const OCTET_STRING_t *in);
uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in);
uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in);
uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in);
static inline void OCTET_STRING_noalloc(OCTET_STRING_t *s, const uint8_t *str, int size)
{
s->buf = str;
s->size = size;
}

View File

@ -12,5 +12,5 @@ endif
lib_LTLIBRARIES = libasn1c.la
libasn1c_la_LDFLAGS = $(LIBOSMOCORE_LIBS)
libasn1c_la_SOURCES = ANY.c constraints.c GeneralizedTime.c NumericString.c T61String.c asn_codecs_prim.c constr_CHOICE.c GeneralString.c ObjectDescriptor.c TeletexString.c asn_SEQUENCE_OF.c constr_SEQUENCE.c GraphicString.c OBJECT_IDENTIFIER.c UniversalString.c asn_SET_OF.c constr_SEQUENCE_OF.c IA5String.c OCTET_STRING.c UTCTime.c ber_decoder.c constr_SET.c INTEGER.c per_decoder.c UTF8String.c ber_tlv_length.c constr_SET_OF.c ISO646String.c per_encoder.c VideotexString.c ber_tlv_tag.c constr_TYPE.c NativeEnumerated.c per_support.c VisibleString.c BIT_STRING.c NativeInteger.c PrintableString.c xer_decoder.c BMPString.c der_encoder.c NativeReal.c REAL.c xer_encoder.c BOOLEAN.c ENUMERATED.c NULL.c RELATIVE-OID.c xer_support.c per_opentype.c
libasn1c_la_SOURCES = ANY.c constraints.c GeneralizedTime.c NumericString.c T61String.c asn_codecs_prim.c constr_CHOICE.c GeneralString.c ObjectDescriptor.c TeletexString.c asn_SEQUENCE_OF.c constr_SEQUENCE.c GraphicString.c OBJECT_IDENTIFIER.c UniversalString.c asn_SET_OF.c constr_SEQUENCE_OF.c IA5String.c OCTET_STRING.c UTCTime.c ber_decoder.c constr_SET.c INTEGER.c per_decoder.c UTF8String.c ber_tlv_length.c constr_SET_OF.c ISO646String.c per_encoder.c VideotexString.c ber_tlv_tag.c constr_TYPE.c NativeEnumerated.c per_support.c VisibleString.c BIT_STRING.c NativeInteger.c PrintableString.c xer_decoder.c BMPString.c der_encoder.c NativeReal.c REAL.c xer_encoder.c BOOLEAN.c ENUMERATED.c NULL.c RELATIVE-OID.c xer_support.c per_opentype.c asn1helpers.c

151
src/asn1helpers.c Normal file
View File

@ -0,0 +1,151 @@
/* helper functions to dela with asn1c data types */
/* (C) 2014-2015 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <osmocom/core/utils.h>
#include "asn1helpers.h"
#include "asn_internal.h"
void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
{
*buf = htonl(in);
bitstr->buf = (uint8_t *) buf;
bitstr->size = sizeof(uint32_t);
bitstr->bits_unused = 0;
}
void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
{
*buf = htonl(in<<4);
bitstr->buf = (uint8_t *) buf;
bitstr->size = sizeof(uint32_t);
bitstr->bits_unused = 4;
}
void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
{
*buf = htonl(in<<8);
bitstr->buf = (uint8_t *) buf;
bitstr->size = 24/8;
bitstr->bits_unused = 0;
}
int BIT_STRING_fromBuf(BIT_STRING_t *st, const uint8_t *str, unsigned int bit_len)
{
void *buf;
unsigned int len = bit_len / 8;
if (bit_len % 8)
len++;
if (!st || (!str && len)) {
errno = EINVAL;
return -1;
}
if (!str) {
FREEMEM(st->buf);
st->buf = 0;
st->size = 0;
st->bits_unused = 0;
return 0;
}
if (len < 0)
len = strlen(str);
buf = MALLOC(len);
if (!buf) {
errno = ENOMEM;
return -1;
}
memcpy(buf, str, len);
FREEMEM(st->buf);
st->buf = buf;
st->size = len;
st->bits_unused = (len * 8) - bit_len;
return 0;
}
void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in)
{
*buf = htons(in);
str->buf = (uint8_t *) buf;
str->size = sizeof(uint16_t);
}
void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in)
{
*buf = in;
str->buf = buf;
str->size = sizeof(uint8_t);
}
int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
{
size_t cpylen = n-1;
if (in->size < cpylen)
cpylen = in->size;
strncpy(out, (char *)in->buf, cpylen);
out[cpylen] = '\0';
return cpylen;
}
uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
{
OSMO_ASSERT(in && in->size == sizeof(uint16_t));
return ntohs(*(uint16_t *)in->buf);
}
uint8_t asn1str_to_u8(const OCTET_STRING_t *in)
{
OSMO_ASSERT(in && in->size == sizeof(uint8_t));
return *(uint8_t *)in->buf;
}
uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in)
{
OSMO_ASSERT(in && in->size == sizeof(uint32_t));
return ntohl(*(uint32_t *)in->buf);
}
uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in)
{
OSMO_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4);
return ntohl(*(uint32_t *)in->buf) >> 4;
}
uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
{
OSMO_ASSERT(in && in->size == 3);
return ntohl(*(uint32_t *)in->buf) >> 8;
}