blob: dafbf7fcf675e76788fd21f5ece50efa833a9af7 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* 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 Welte30afef32015-08-30 12:28:29 +020020
21#include <string.h>
Daniel Willmannb2548fb2015-11-30 16:24:57 +010022#include <arpa/inet.h>
Harald Welte30afef32015-08-30 12:28:29 +020023
Harald Welte27f9c4a2015-08-30 22:47:18 +020024#include <osmocom/core/utils.h>
Harald Welte30afef32015-08-30 12:28:29 +020025
26#include "asn1helpers.h"
27
Daniel Willmannb2548fb2015-11-30 16:24:57 +010028void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
Harald Welte27f9c4a2015-08-30 22:47:18 +020029{
Daniel Willmannb2548fb2015-11-30 16:24:57 +010030 *buf = htonl(in);
31 bitstr->buf = (uint8_t *) buf;
Harald Welte27f9c4a2015-08-30 22:47:18 +020032 bitstr->size = sizeof(uint32_t);
33 bitstr->bits_unused = 0;
34}
35
Daniel Willmannd6a45b42015-12-08 13:55:17 +010036void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
37{
38 *buf = htonl(in<<4);
39 bitstr->buf = (uint8_t *) buf;
40 bitstr->size = sizeof(uint32_t);
41 bitstr->bits_unused = 4;
42}
43
Daniel Willmannb2548fb2015-11-30 16:24:57 +010044void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
Harald Welte0bb12612015-10-06 22:08:54 +020045{
Daniel Willmann92247312015-12-09 19:04:33 +010046 *buf = htonl(in<<8);
Daniel Willmannb2548fb2015-11-30 16:24:57 +010047 bitstr->buf = (uint8_t *) buf;
Harald Welte0bb12612015-10-06 22:08:54 +020048 bitstr->size = 24/8;
49 bitstr->bits_unused = 0;
50}
51
Daniel Willmannf3685c22015-12-07 17:18:56 +010052void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in)
53{
54 *buf = htons(in);
55 str->buf = (uint8_t *) buf;
56 str->size = sizeof(uint16_t);
57}
58
59void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in)
60{
61 *buf = in;
62 str->buf = buf;
63 str->size = sizeof(uint8_t);
64}
Harald Welte27f9c4a2015-08-30 22:47:18 +020065
66int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
Harald Welte30afef32015-08-30 12:28:29 +020067{
Daniel Willmann53018e92015-11-23 15:49:29 +010068 size_t cpylen = n-1;
Harald Welte30afef32015-08-30 12:28:29 +020069
Harald Welte27f9c4a2015-08-30 22:47:18 +020070 if (in->size < cpylen)
71 cpylen = in->size;
Harald Welte30afef32015-08-30 12:28:29 +020072
73 strncpy(out, (char *)in->buf, cpylen);
Daniel Willmann53018e92015-11-23 15:49:29 +010074 out[cpylen] = '\0';
Harald Welte30afef32015-08-30 12:28:29 +020075
76 return cpylen;
77}
Harald Welte27f9c4a2015-08-30 22:47:18 +020078
79uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
80{
Daniel Willmannb2548fb2015-11-30 16:24:57 +010081 OSMO_ASSERT(in && in->size == sizeof(uint16_t));
Daniel Willmanne3adf0e2015-11-27 17:53:19 +010082 return ntohs(*(uint16_t *)in->buf);
Harald Welte27f9c4a2015-08-30 22:47:18 +020083}
84
85uint8_t asn1str_to_u8(const OCTET_STRING_t *in)
86{
Daniel Willmannb2548fb2015-11-30 16:24:57 +010087 OSMO_ASSERT(in && in->size == sizeof(uint8_t));
Harald Welte27f9c4a2015-08-30 22:47:18 +020088 return *(uint8_t *)in->buf;
89}
90
91uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in)
92{
Daniel Willmannb2548fb2015-11-30 16:24:57 +010093 OSMO_ASSERT(in && in->size == sizeof(uint32_t));
Harald Welte4dd16b92015-10-06 22:08:10 +020094
Daniel Willmanne3adf0e2015-11-27 17:53:19 +010095 return ntohl(*(uint32_t *)in->buf);
96}
Daniel Willmann6e59d412015-11-23 14:03:04 +010097
Daniel Willmannd6a45b42015-12-08 13:55:17 +010098uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in)
99{
100 OSMO_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4);
101
102 return ntohl(*(uint32_t *)in->buf) >> 4;
103}
104
Daniel Willmanne3adf0e2015-11-27 17:53:19 +0100105uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
106{
Daniel Willmannb2548fb2015-11-30 16:24:57 +0100107 OSMO_ASSERT(in && in->size == 3);
Daniel Willmann6e59d412015-11-23 14:03:04 +0100108
Daniel Willmann92247312015-12-09 19:04:33 +0100109 return ntohl(*(uint32_t *)in->buf) >> 8;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200110}