asn1helpers, test-helpers: Use ntoh/hton* to convert integers

Since the asn1_u32/24_to_bitstring functions need to change the source
variable change the signature to clarify that the uint32_t * will be
modified.
This commit is contained in:
Daniel Willmann 2015-11-30 16:24:57 +01:00
parent e2956431e8
commit b2548fb1e8
3 changed files with 21 additions and 14 deletions

View File

@ -19,21 +19,24 @@
*/
#include <string.h>
#include <arpa/inet.h>
#include <osmocom/core/utils.h>
#include "asn1helpers.h"
void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in)
void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
{
bitstr->buf = (uint8_t *) in;
*buf = htonl(in);
bitstr->buf = (uint8_t *) buf;
bitstr->size = sizeof(uint32_t);
bitstr->bits_unused = 0;
}
void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in)
void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
{
bitstr->buf = (uint8_t *) in;
*buf = htonl(in);
bitstr->buf = (uint8_t *) buf;
bitstr->size = 24/8;
bitstr->bits_unused = 0;
}
@ -54,26 +57,26 @@ int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
{
OSMO_ASSERT(in && in->size >= sizeof(uint16_t));
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));
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) && in->bits_unused == 0);
OSMO_ASSERT(in && in->size == sizeof(uint32_t));
return ntohl(*(uint32_t *)in->buf);
}
uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
{
OSMO_ASSERT(in && in->size >= 3 && in->bits_unused == 0);
OSMO_ASSERT(in && in->size == 3);
return *(uint32_t *)in->buf;
}

View File

@ -5,8 +5,8 @@
#include "BIT_STRING.h"
#include "OCTET_STRING.h"
void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in);
void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in);
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 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);

View File

@ -55,7 +55,7 @@ void test_iu_helpers(void)
ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded)));
}
uint32_t val1 = 0xdeadbeef;
const uint32_t val1 = 0xdeadbeef;
const OCTET_STRING_t text1 = {
.buf = "0123456789012345",
@ -72,15 +72,14 @@ void test_asn1_helpers(void)
int rc;
BIT_STRING_t enc;
uint32_t res;
uint32_t res, tmpval;
char text[32];
printf("Testing asn.1 helper functions\n");
printf("Encoding 0x%x to asn.1 bitstring\n", val1);
asn1_u32_to_bitstring(&enc, &val1);
asn1_u32_to_bitstring(&enc, &tmpval, val1);
ASSERT(enc.buf == (uint8_t *) &val1);
ASSERT(enc.size == sizeof(uint32_t));
ASSERT(enc.bits_unused == 0);
@ -89,6 +88,11 @@ void test_asn1_helpers(void)
printf("Decoding back to uint32_t: 0x%x\n", res);
ASSERT(res == val1);
printf("Encoding %s to 24-bit asn.1 bitstring\n", osmo_hexdump_nospc(&val1, 3));
asn1_u24_to_bitstring(&enc, &tmpval, val1);
ASSERT(enc.size == 24/8);
ASSERT(enc.bits_unused == 0);
rc = asn1_strncpy(text, &text1, sizeof(text));
printf("Decoding string from asn.1: %s\n", text);