Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 1 | |
| 2 | #include <string.h> |
| 3 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 4 | #include <osmocom/core/utils.h> |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 5 | |
| 6 | #include "asn1helpers.h" |
| 7 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 8 | void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in) |
| 9 | { |
| 10 | bitstr->buf = (uint8_t *) in; |
| 11 | bitstr->size = sizeof(uint32_t); |
| 12 | bitstr->bits_unused = 0; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n) |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 17 | { |
| 18 | size_t cpylen = n; |
| 19 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 20 | if (in->size < cpylen) |
| 21 | cpylen = in->size; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 22 | |
| 23 | strncpy(out, (char *)in->buf, cpylen); |
| 24 | out[n-1] = '\0'; |
| 25 | |
| 26 | return cpylen; |
| 27 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 28 | |
| 29 | uint16_t asn1str_to_u16(const OCTET_STRING_t *in) |
| 30 | { |
| 31 | OSMO_ASSERT(in && in->size >= sizeof(uint16_t)); |
| 32 | return *(uint16_t *)in->buf; |
| 33 | } |
| 34 | |
| 35 | uint8_t asn1str_to_u8(const OCTET_STRING_t *in) |
| 36 | { |
| 37 | OSMO_ASSERT(in && in->size >= sizeof(uint8_t)); |
| 38 | return *(uint8_t *)in->buf; |
| 39 | } |
| 40 | |
| 41 | uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in) |
| 42 | { |
| 43 | OSMO_ASSERT(in && in->size >= sizeof(uint32_t)); |
| 44 | return *(uint32_t *)in->buf; |
| 45 | } |