implement BIT_STRING_fromBuf() similar to OCTET_STRING_fromBuf()
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 @@
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);