implement BIT_STRING_fromBuf() similar to OCTET_STRING_fromBuf()

This commit is contained in:
Harald Welte 2015-12-18 17:50:57 +01:00
parent 16232780d8
commit fcabec3665
2 changed files with 42 additions and 0 deletions

View File

@ -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);

View File

@ -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);