From 95a112f75ab7154e375d9c407f233f95c826f2f3 Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Tue, 2 Feb 2016 16:38:40 +0100 Subject: Add helper functions for uint32_t in octet strings --- include/asn1c/asn1helpers.h | 2 ++ src/asn1helpers.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/asn1c/asn1helpers.h b/include/asn1c/asn1helpers.h index 007f207..d6b5e18 100644 --- a/include/asn1c/asn1helpers.h +++ b/include/asn1c/asn1helpers.h @@ -10,9 +10,11 @@ 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_u32_to_str(OCTET_STRING_t *str, uint32_t *buf, uint32_t in); 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); +uint32_t asn1str_to_u32(const OCTET_STRING_t *in); 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); diff --git a/src/asn1helpers.c b/src/asn1helpers.c index 25f4af4..638cdb6 100644 --- a/src/asn1helpers.c +++ b/src/asn1helpers.c @@ -90,6 +90,13 @@ int BIT_STRING_fromBuf(BIT_STRING_t *st, const uint8_t *str, unsigned int bit_le return 0; } +void asn1_u32_to_str(OCTET_STRING_t *str, uint32_t *buf, uint32_t in) +{ + *buf = htonl(in); + str->buf = (uint8_t *) buf; + str->size = sizeof(uint32_t); +} + void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in) { *buf = htons(in); @@ -117,6 +124,12 @@ int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n) return cpylen; } +uint32_t asn1str_to_u32(const OCTET_STRING_t *in) +{ + OSMO_ASSERT(in && in->size == sizeof(uint32_t)); + return ntohl(*(uint32_t *)in->buf); +} + uint16_t asn1str_to_u16(const OCTET_STRING_t *in) { OSMO_ASSERT(in && in->size == sizeof(uint16_t)); -- cgit v1.2.3