Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* helper functions to dela with asn1c data types */ |
| 2 | |
| 3 | /* (C) 2014-2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 23 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 24 | #include <osmocom/core/utils.h> |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 25 | |
| 26 | #include "asn1helpers.h" |
| 27 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 28 | void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in) |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 29 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 30 | *buf = htonl(in); |
| 31 | bitstr->buf = (uint8_t *) buf; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 32 | bitstr->size = sizeof(uint32_t); |
| 33 | bitstr->bits_unused = 0; |
| 34 | } |
| 35 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 36 | void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in) |
Harald Welte | 0bb1261 | 2015-10-06 22:08:54 +0200 | [diff] [blame] | 37 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 38 | *buf = htonl(in); |
| 39 | bitstr->buf = (uint8_t *) buf; |
Harald Welte | 0bb1261 | 2015-10-06 22:08:54 +0200 | [diff] [blame] | 40 | bitstr->size = 24/8; |
| 41 | bitstr->bits_unused = 0; |
| 42 | } |
| 43 | |
Daniel Willmann | f3685c2 | 2015-12-07 17:18:56 +0100 | [diff] [blame] | 44 | void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in) |
| 45 | { |
| 46 | *buf = htons(in); |
| 47 | str->buf = (uint8_t *) buf; |
| 48 | str->size = sizeof(uint16_t); |
| 49 | } |
| 50 | |
| 51 | void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in) |
| 52 | { |
| 53 | *buf = in; |
| 54 | str->buf = buf; |
| 55 | str->size = sizeof(uint8_t); |
| 56 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 57 | |
| 58 | 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] | 59 | { |
Daniel Willmann | 53018e9 | 2015-11-23 15:49:29 +0100 | [diff] [blame] | 60 | size_t cpylen = n-1; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 61 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 62 | if (in->size < cpylen) |
| 63 | cpylen = in->size; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 64 | |
| 65 | strncpy(out, (char *)in->buf, cpylen); |
Daniel Willmann | 53018e9 | 2015-11-23 15:49:29 +0100 | [diff] [blame] | 66 | out[cpylen] = '\0'; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 67 | |
| 68 | return cpylen; |
| 69 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 70 | |
| 71 | uint16_t asn1str_to_u16(const OCTET_STRING_t *in) |
| 72 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 73 | OSMO_ASSERT(in && in->size == sizeof(uint16_t)); |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 74 | return ntohs(*(uint16_t *)in->buf); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | uint8_t asn1str_to_u8(const OCTET_STRING_t *in) |
| 78 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 79 | OSMO_ASSERT(in && in->size == sizeof(uint8_t)); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 80 | return *(uint8_t *)in->buf; |
| 81 | } |
| 82 | |
| 83 | uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in) |
| 84 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 85 | OSMO_ASSERT(in && in->size == sizeof(uint32_t)); |
Harald Welte | 4dd16b9 | 2015-10-06 22:08:10 +0200 | [diff] [blame] | 86 | |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 87 | return ntohl(*(uint32_t *)in->buf); |
| 88 | } |
Daniel Willmann | 6e59d41 | 2015-11-23 14:03:04 +0100 | [diff] [blame] | 89 | |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 90 | uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in) |
| 91 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 92 | OSMO_ASSERT(in && in->size == 3); |
Daniel Willmann | 6e59d41 | 2015-11-23 14:03:04 +0100 | [diff] [blame] | 93 | |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 94 | return *(uint32_t *)in->buf; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 95 | } |