aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Willmann <dwillmann@sysmocom.de>2015-11-30 16:24:57 +0100
committerDaniel Willmann <dwillmann@sysmocom.de>2015-11-30 16:24:57 +0100
commitb2548fb1e8463119db0b1a16303183745a8002fb (patch)
tree3812e46b2855f6a0ee83e391959d6af75e540265
parente2956431e8f938daff35efd35b9019a99c0e46ca (diff)
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.
-rw-r--r--src/asn1helpers.c19
-rw-r--r--src/asn1helpers.h4
-rw-r--r--src/tests/test-helpers.c12
3 files changed, 21 insertions, 14 deletions
diff --git a/src/asn1helpers.c b/src/asn1helpers.c
index d04298f..38907e1 100644
--- a/src/asn1helpers.c
+++ b/src/asn1helpers.c
@@ -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;
}
diff --git a/src/asn1helpers.h b/src/asn1helpers.h
index 17d0d89..cb558da 100644
--- a/src/asn1helpers.h
+++ b/src/asn1helpers.h
@@ -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);
diff --git a/src/tests/test-helpers.c b/src/tests/test-helpers.c
index 87cb349..76ab4dc 100644
--- a/src/tests/test-helpers.c
+++ b/src/tests/test-helpers.c
@@ -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);