aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2015-12-18 17:50:57 +0100
committerHarald Welte <laforge@gnumonks.org>2015-12-18 17:51:50 +0100
commitfcabec3665a29a25b9ceb80c047165de3b79a82f (patch)
treee0c80b04b7c72d3d8dd8340dffa2d8a479e4cc84 /src
parent16232780d893de5bcac5f6b670d8173e444d7661 (diff)
implement BIT_STRING_fromBuf() similar to OCTET_STRING_fromBuf()
Diffstat (limited to 'src')
-rw-r--r--src/asn1helpers.c41
-rw-r--r--src/asn1helpers.h1
2 files changed, 42 insertions, 0 deletions
diff --git a/src/asn1helpers.c b/src/asn1helpers.c
index dafbf7f..25f4af4 100644
--- a/src/asn1helpers.c
+++ b/src/asn1helpers.c
@@ -19,11 +19,13 @@
*/
#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)
{
@@ -49,6 +51,45 @@ void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
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);
diff --git a/src/asn1helpers.h b/src/asn1helpers.h
index 124df79..bb0f4de 100644
--- a/src/asn1helpers.h
+++ b/src/asn1helpers.h
@@ -7,6 +7,7 @@
void asn1_u32_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);