blob: a4ced707216ca96785715b417193ad5a9d72496b [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
68 if (len < 0)
Neels Hofmeyr578ecbb2016-02-16 12:23:23 +010069 len = strlen((char*)str);
Harald Welte2d4f2bd2016-01-03 17:14:54 +010070
71 buf = MALLOC(len);
72 if (!buf) {
73 errno = ENOMEM;
74 return -1;
75 }
76
77 memcpy(buf, str, len);
78 FREEMEM(st->buf);
79 st->buf = buf;
80 st->size = len;
81 st->bits_unused = (len * 8) - bit_len;
82
83 return 0;
84}
85
Daniel Willmann95a112f2016-02-02 16:38:40 +010086void asn1_u32_to_str(OCTET_STRING_t *str, uint32_t *buf, uint32_t in)
87{
88 *buf = htonl(in);
89 str->buf = (uint8_t *) buf;
90 str->size = sizeof(uint32_t);
91}
92
Harald Welte2d4f2bd2016-01-03 17:14:54 +010093void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in)
94{
95 *buf = htons(in);
96 str->buf = (uint8_t *) buf;
97 str->size = sizeof(uint16_t);
98}
99
100void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in)
101{
102 *buf = in;
103 str->buf = buf;
104 str->size = sizeof(uint8_t);
105}
106
107int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n)
108{
109 size_t cpylen = n-1;
110
111 if (in->size < cpylen)
112 cpylen = in->size;
113
114 strncpy(out, (char *)in->buf, cpylen);
115 out[cpylen] = '\0';
116
117 return cpylen;
118}
119
Daniel Willmann95a112f2016-02-02 16:38:40 +0100120uint32_t asn1str_to_u32(const OCTET_STRING_t *in)
121{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200122 ASN1C_ASSERT(in && in->size == sizeof(uint32_t));
Daniel Willmann95a112f2016-02-02 16:38:40 +0100123 return ntohl(*(uint32_t *)in->buf);
124}
125
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100126uint16_t asn1str_to_u16(const OCTET_STRING_t *in)
127{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200128 ASN1C_ASSERT(in && in->size == sizeof(uint16_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100129 return ntohs(*(uint16_t *)in->buf);
130}
131
132uint8_t asn1str_to_u8(const OCTET_STRING_t *in)
133{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200134 ASN1C_ASSERT(in && in->size == sizeof(uint8_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100135 return *(uint8_t *)in->buf;
136}
137
138uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in)
139{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200140 ASN1C_ASSERT(in && in->size == sizeof(uint32_t));
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100141
142 return ntohl(*(uint32_t *)in->buf);
143}
144
145uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in)
146{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200147 ASN1C_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4);
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100148
149 return ntohl(*(uint32_t *)in->buf) >> 4;
150}
151
152uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
153{
Holger Hans Peter Freytherf3c30492016-04-05 16:25:43 +0200154 ASN1C_ASSERT(in && in->size == 3);
Harald Welte2d4f2bd2016-01-03 17:14:54 +0100155
156 return ntohl(*(uint32_t *)in->buf) >> 8;
157}