blob: 3082ddc53f6b75068f6144e2f3b0b65d4f433205 [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>
22
Harald Welte27f9c4a2015-08-30 22:47:18 +020023#include <osmocom/core/utils.h>
Harald Welte30afef32015-08-30 12:28:29 +020024
25#include "asn1helpers.h"
26
Harald Welte27f9c4a2015-08-30 22:47:18 +020027void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *in)
28{
29 bitstr->buf = (uint8_t *) in;
30 bitstr->size = sizeof(uint32_t);
31 bitstr->bits_unused = 0;
32}
33
34
35int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
Harald Welte30afef32015-08-30 12:28:29 +020036{
37 size_t cpylen = n;
38
Harald Welte27f9c4a2015-08-30 22:47:18 +020039 if (in->size < cpylen)
40 cpylen = in->size;
Harald Welte30afef32015-08-30 12:28:29 +020041
42 strncpy(out, (char *)in->buf, cpylen);
43 out[n-1] = '\0';
44
45 return cpylen;
46}
Harald Welte27f9c4a2015-08-30 22:47:18 +020047
48uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
49{
50 OSMO_ASSERT(in && in->size >= sizeof(uint16_t));
51 return *(uint16_t *)in->buf;
52}
53
54uint8_t asn1str_to_u8(const OCTET_STRING_t *in)
55{
56 OSMO_ASSERT(in && in->size >= sizeof(uint8_t));
57 return *(uint8_t *)in->buf;
58}
59
60uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in)
61{
Harald Welte4dd16b92015-10-06 22:08:10 +020062 uint32_t ret = 0;
63 int i;
64
65 for (i = 0; i < 4; i++) {
66 if (in->size < i)
67 break;
68 ret <<= 8;
69 ret |= in->buf[i];
70 }
71 return ret;
Harald Welte27f9c4a2015-08-30 22:47:18 +020072}