Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <BIT_STRING.h> |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 7 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * BIT STRING basic type description. |
| 11 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 12 | static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | (ASN_TAG_CLASS_UNIVERSAL | (3 << 2)) |
| 14 | }; |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 15 | asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 16 | sizeof(BIT_STRING_t), |
| 17 | offsetof(BIT_STRING_t, _asn_ctx), |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 18 | ASN_OSUBV_BIT |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 19 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 20 | asn_TYPE_operation_t asn_OP_BIT_STRING = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | OCTET_STRING_free, /* Implemented in terms of OCTET STRING */ |
| 22 | BIT_STRING_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 23 | BIT_STRING_compare, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | BIT_STRING_constraint, |
| 25 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 26 | OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 27 | OCTET_STRING_decode_xer_binary, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 28 | BIT_STRING_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 29 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 30 | 0, |
| 31 | 0, |
| 32 | #else |
| 33 | 0, |
| 34 | 0, |
| 35 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 36 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 37 | 0, |
| 38 | 0, |
| 39 | #else |
| 40 | OCTET_STRING_decode_uper, /* Unaligned PER decoder */ |
| 41 | OCTET_STRING_encode_uper, /* Unaligned PER encoder */ |
| 42 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 43 | 0 /* Use generic outmost tag fetcher */ |
| 44 | }; |
| 45 | asn_TYPE_descriptor_t asn_DEF_BIT_STRING = { |
| 46 | "BIT STRING", |
| 47 | "BIT_STRING", |
| 48 | &asn_OP_BIT_STRING, |
| 49 | BIT_STRING_constraint, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 50 | asn_DEF_BIT_STRING_tags, |
| 51 | sizeof(asn_DEF_BIT_STRING_tags) |
| 52 | / sizeof(asn_DEF_BIT_STRING_tags[0]), |
| 53 | asn_DEF_BIT_STRING_tags, /* Same as above */ |
| 54 | sizeof(asn_DEF_BIT_STRING_tags) |
| 55 | / sizeof(asn_DEF_BIT_STRING_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 56 | 0, /* No OER visible constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 57 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 58 | 0, 0, /* No members */ |
Bi-Ruei, Chiu | 419ac3a | 2016-05-14 15:27:19 +0800 | [diff] [blame] | 59 | &asn_SPC_BIT_STRING_specs |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * BIT STRING generic constraint. |
| 64 | */ |
| 65 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 66 | BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 67 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 68 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 69 | |
| 70 | if(st && st->buf) { |
Lev Walkin | 9abdf26 | 2007-02-21 19:27:45 +0000 | [diff] [blame] | 71 | if((st->size == 0 && st->bits_unused) |
| 72 | || st->bits_unused < 0 || st->bits_unused > 7) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 73 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 74 | "%s: invalid padding byte (%s:%d)", |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 75 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
| 78 | } else { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 79 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 80 | "%s: value not given (%s:%d)", |
| 81 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
johvik | 5f619fb | 2017-05-09 09:05:04 +0200 | [diff] [blame] | 88 | static const char *_bit_pattern[16] = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 89 | "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", |
| 90 | "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" |
| 91 | }; |
| 92 | |
| 93 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 94 | BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 95 | int ilevel, enum xer_encoder_flags_e flags, |
| 96 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 97 | asn_enc_rval_t er; |
| 98 | char scratch[128]; |
| 99 | char *p = scratch; |
| 100 | char *scend = scratch + (sizeof(scratch) - 10); |
| 101 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 102 | int xcan = (flags & XER_F_CANONICAL); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 103 | uint8_t *buf; |
| 104 | uint8_t *end; |
| 105 | |
| 106 | if(!st || !st->buf) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 107 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 108 | |
| 109 | er.encoded = 0; |
| 110 | |
| 111 | buf = st->buf; |
| 112 | end = buf + st->size - 1; /* Last byte is special */ |
| 113 | |
| 114 | /* |
| 115 | * Binary dump |
| 116 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 117 | for(; buf < end; buf++) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 118 | int v = *buf; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 119 | int nline = xcan?0:(((buf - st->buf) % 8) == 0); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 120 | if(p >= scend || nline) { |
| 121 | er.encoded += p - scratch; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 122 | ASN__CALLBACK(scratch, p - scratch); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 123 | p = scratch; |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 124 | if(nline) ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 125 | } |
| 126 | memcpy(p + 0, _bit_pattern[v >> 4], 4); |
| 127 | memcpy(p + 4, _bit_pattern[v & 0x0f], 4); |
| 128 | p += 8; |
| 129 | } |
| 130 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 131 | if(!xcan && ((buf - st->buf) % 8) == 0) |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 132 | ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 133 | er.encoded += p - scratch; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 134 | ASN__CALLBACK(scratch, p - scratch); |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 135 | p = scratch; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 136 | |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 137 | if(buf == end) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 138 | int v = *buf; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 139 | int ubits = st->bits_unused; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 140 | int i; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 141 | for(i = 7; i >= ubits; i--) |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 142 | *p++ = (v & (1 << i)) ? 0x31 : 0x30; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 143 | er.encoded += p - scratch; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 144 | ASN__CALLBACK(scratch, p - scratch); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 147 | if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 148 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 149 | ASN__ENCODED_OK(er); |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 150 | cb_failed: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 151 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | /* |
| 156 | * BIT STRING specific contents printer. |
| 157 | */ |
| 158 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 159 | BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 160 | asn_app_consume_bytes_f *cb, void *app_key) { |
Wim Lewis | 59e8d28 | 2014-08-04 12:39:35 -0700 | [diff] [blame] | 161 | const char * const h2c = "0123456789ABCDEF"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | char scratch[64]; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 163 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 164 | uint8_t *buf; |
| 165 | uint8_t *end; |
| 166 | char *p = scratch; |
| 167 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 168 | (void)td; /* Unused argument */ |
| 169 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 170 | if(!st || !st->buf) |
| 171 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 173 | ilevel++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | buf = st->buf; |
| 175 | end = buf + st->size; |
| 176 | |
| 177 | /* |
| 178 | * Hexadecimal dump. |
| 179 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 180 | for(; buf < end; buf++) { |
| 181 | if((buf - st->buf) % 16 == 0 && (st->size > 16) |
| 182 | && buf != st->buf) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 183 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 184 | /* Dump the string */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 185 | if(cb(scratch, p - scratch, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | p = scratch; |
| 187 | } |
| 188 | *p++ = h2c[*buf >> 4]; |
| 189 | *p++ = h2c[*buf & 0x0F]; |
| 190 | *p++ = 0x20; |
| 191 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 193 | if(p > scratch) { |
| 194 | p--; /* Eat the tailing space */ |
| 195 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 196 | if((st->size > 16)) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 197 | _i_INDENT(1); |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Dump the incomplete 16-bytes row */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 201 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 208 | /* |
| 209 | * Lexicographically compare the common prefix of both strings, |
| 210 | * and if it is the same return -1 for the smallest string. |
| 211 | */ |
| 212 | int |
| 213 | BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 214 | const void *bptr) { |
| 215 | const BIT_STRING_t *a = aptr; |
| 216 | const BIT_STRING_t *b = bptr; |
| 217 | |
| 218 | (void)td; |
| 219 | |
| 220 | if(a && b) { |
| 221 | size_t common_prefix_size = a->size <= b->size ? a->size : b->size; |
| 222 | int ret = memcmp(a->buf, b->buf, common_prefix_size); |
| 223 | if(ret == 0) { |
| 224 | /* Figure out which string with equal prefixes is longer. */ |
| 225 | if(a->size < b->size) { |
| 226 | return -1; |
| 227 | } else if(a->size > b->size) { |
| 228 | return 1; |
| 229 | } else { |
| 230 | /* Figure out how many unused bits */ |
| 231 | if(a->bits_unused < b->bits_unused) { |
| 232 | return -1; |
| 233 | } else if(a->bits_unused > b->bits_unused) { |
| 234 | return 1; |
| 235 | } else { |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | } else { |
| 240 | return ret; |
| 241 | } |
| 242 | } else if(!a && !b) { |
| 243 | return 0; |
| 244 | } else if(!a) { |
| 245 | return -1; |
| 246 | } else { |
| 247 | return 1; |
| 248 | } |
| 249 | } |
| 250 | |