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 | |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 68 | 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 Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 83 | void 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 Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 90 | void 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 | |
| 97 | void 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 | |
| 104 | int 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 Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 117 | uint32_t asn1str_to_u32(const OCTET_STRING_t *in) |
| 118 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 119 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t)); |
Daniel Willmann | 95a112f | 2016-02-02 16:38:40 +0100 | [diff] [blame] | 120 | return ntohl(*(uint32_t *)in->buf); |
| 121 | } |
| 122 | |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 123 | uint16_t asn1str_to_u16(const OCTET_STRING_t *in) |
| 124 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 125 | ASN1C_ASSERT(in && in->size == sizeof(uint16_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 126 | return ntohs(*(uint16_t *)in->buf); |
| 127 | } |
| 128 | |
| 129 | uint8_t asn1str_to_u8(const OCTET_STRING_t *in) |
| 130 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 131 | ASN1C_ASSERT(in && in->size == sizeof(uint8_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 132 | return *(uint8_t *)in->buf; |
| 133 | } |
| 134 | |
| 135 | uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in) |
| 136 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 137 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t)); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 138 | |
| 139 | return ntohl(*(uint32_t *)in->buf); |
| 140 | } |
| 141 | |
| 142 | uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in) |
| 143 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 144 | ASN1C_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 145 | |
| 146 | return ntohl(*(uint32_t *)in->buf) >> 4; |
| 147 | } |
| 148 | |
| 149 | uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in) |
| 150 | { |
Holger Hans Peter Freyther | f3c3049 | 2016-04-05 16:25:43 +0200 | [diff] [blame] | 151 | ASN1C_ASSERT(in && in->size == 3); |
Harald Welte | 2d4f2bd | 2016-01-03 17:14:54 +0100 | [diff] [blame] | 152 | |
| 153 | return ntohl(*(uint32_t *)in->buf) >> 8; |
| 154 | } |