blob: 4e5e08d99e545704357c73cd097e4f95816c681e [file] [log] [blame]
Harald Welte2d4f2bd2016-01-03 17:14:54 +01001/* helper functions to dela with asn1c data types */
2
3/* (C) 2014-2015 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
Harald Weltec8512b12017-08-14 00:42:30 +02006 * Redistribution and modifications are permitted subject to BSD license
7 * contained in COPYING file.
Harald Welte2d4f2bd2016-01-03 17:14:54 +01008 */
9
10#include <string.h>
11#include <errno.h>
12#include <arpa/inet.h>
13
Harald Welte2d4f2bd2016-01-03 17:14:54 +010014#include "asn1helpers.h"
15#include "asn_internal.h"
16
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +020017#define ASN1C_ASSERT(exp) \
18 if (!(exp)) { \
19 fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
20 abort(); \
21 }
22
Harald Welte2d4f2bd2016-01-03 17:14:54 +010023void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
24{
25 *buf = htonl(in);
26 bitstr->buf = (uint8_t *) buf;
27 bitstr->size = sizeof(uint32_t);
28 bitstr->bits_unused = 0;
29}
30
31void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
32{
33 *buf = htonl(in<<4);
34 bitstr->buf = (uint8_t *) buf;
35 bitstr->size = sizeof(uint32_t);
36 bitstr->bits_unused = 4;
37}
38
39void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
40{
41 *buf = htonl(in<<8);
42 bitstr->buf = (uint8_t *) buf;
43 bitstr->size = 24/8;
44 bitstr->bits_unused = 0;
45}
46
47int BIT_STRING_fromBuf(BIT_STRING_t *st, const uint8_t *str, unsigned int bit_len)
48{
49 void *buf;
50 unsigned int len = bit_len / 8;
51
52 if (bit_len % 8)
53 len++;
54
55 if (!st || (!str && len)) {
56 errno = EINVAL;
57 return -1;
58 }
59
60 if (!str) {
61 FREEMEM(st->buf);
62 st->buf = 0;
63 st->size = 0;
64 st->bits_unused = 0;
65 return 0;
66 }
67
Harald Welte2d4f2bd2016-01-03 17:14:54 +010068 buf = MALLOC(len);
69 if (!buf) {
70 errno = ENOMEM;
71 return -1;
72 }
73
74 memcpy(buf, str, len);
75 FREEMEM(st->buf);
76 st->buf = buf;
77 st->size = len;
78 st->bits_unused = (len * 8) - bit_len;
79
80 return 0;
81}
82
Daniel Willmann95a112f2016-02-02 16:38:40 +010083void asn1_u32_to_str(OCTET_STRING_t *str, uint32_t *buf, uint32_t in)
84{
85 *buf = htonl(in);
86 str->buf = (uint8_t *) buf;
87 str->size = sizeof(uint32_t);
88}
89
Harald Welte2d4f2bd2016-01-03 17:14:54 +010090void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in)
91{
92 *buf = htons(in);
93 str->buf = (uint8_t *) buf;
94 str->size = sizeof(uint16_t);
95}
96
97void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in)
98{
99 *buf = in;
100 str->buf = buf;
101 str->size = sizeof(uint8_t);
102}
103
104int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
105{
106 size_t cpylen = n-1;
107
108 if (in->size < cpylen)
109 cpylen = in->size;
110
111 strncpy(out, (char *)in->buf, cpylen);
112 out[cpylen] = '\0';
113
114 return cpylen;
115}
116
Daniel Willmann95a112f2016-02-02 16:38:40 +0100117uint32_t asn1str_to_u32(const OCTET_STRING_t *in)
118{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200119 ASN1C_ASSERT(in && in->size == sizeof(uint32_t));
Daniel Willmann95a112f2016-02-02 16:38:40 +0100120 return ntohl(*(uint32_t *)in->buf);
121}
122
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100123uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
124{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200125 ASN1C_ASSERT(in && in->size == sizeof(uint16_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100126 return ntohs(*(uint16_t *)in->buf);
127}
128
129uint8_t asn1str_to_u8(const OCTET_STRING_t *in)
130{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200131 ASN1C_ASSERT(in && in->size == sizeof(uint8_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100132 return *(uint8_t *)in->buf;
133}
134
135uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in)
136{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200137 ASN1C_ASSERT(in && in->size == sizeof(uint32_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100138
139 return ntohl(*(uint32_t *)in->buf);
140}
141
142uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in)
143{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200144 ASN1C_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4);
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100145
146 return ntohl(*(uint32_t *)in->buf) >> 4;
147}
148
149uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
150{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200151 ASN1C_ASSERT(in && in->size == 3);
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100152
153 return ntohl(*(uint32_t *)in->buf) >> 8;
154}