Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [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 | * |
Harald Welte | c8512b1 | 2017-08-14 00:42:30 +0200 | [diff] [blame] | 6 | * Redistribution and modifications are permitted subject to BSD license |
| 7 | * contained in COPYING file. |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <errno.h> |
| 12 | #include <arpa/inet.h> |
| 13 | |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 14 | #include "asn1helpers.h" |
| 15 | #include "asn_internal.h" |
| 16 | |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 17 | #define ASN1C_ASSERT(exp) \ |
| 18 | if (!(exp)) { \ |
| 19 | fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ |
| 20 | abort(); \ |
| 21 | } |
| 22 | |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 23 | void 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 | |
| 31 | void 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 | |
| 39 | void 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 | |
| 47 | int 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 Hofmeyr | 578ecbb | 2016-02-16 12:23:23 +0100 | [diff] [blame] | 69 | len = strlen((char*)str); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 70 | |
| 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 Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 86 | void 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 Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 93 | void 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 | |
| 100 | void 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 | |
| 107 | int 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 Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 120 | uint32_t asn1str_to_u32(const OCTET_STRING_t *in) |
| 121 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 122 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t)); |
Daniel Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 123 | return ntohl(*(uint32_t *)in->buf); |
| 124 | } |
| 125 | |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 126 | uint16_t asn1str_to_u16(const OCTET_STRING_t *in) |
| 127 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 128 | ASN1C_ASSERT(in && in->size == sizeof(uint16_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 129 | return ntohs(*(uint16_t *)in->buf); |
| 130 | } |
| 131 | |
| 132 | uint8_t asn1str_to_u8(const OCTET_STRING_t *in) |
| 133 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 134 | ASN1C_ASSERT(in && in->size == sizeof(uint8_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 135 | return *(uint8_t *)in->buf; |
| 136 | } |
| 137 | |
| 138 | uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in) |
| 139 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 140 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 141 | |
| 142 | return ntohl(*(uint32_t *)in->buf); |
| 143 | } |
| 144 | |
| 145 | uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in) |
| 146 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 147 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 148 | |
| 149 | return ntohl(*(uint32_t *)in->buf) >> 4; |
| 150 | } |
| 151 | |
| 152 | uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in) |
| 153 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 154 | ASN1C_ASSERT(in && in->size == 3); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 155 | |
| 156 | return ntohl(*(uint32_t *)in->buf) >> 8; |
| 157 | } |