blob: b5ab945cc0dc5079b3a72fe9fb800c11328f8486 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
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 Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <BIT_STRING.h>
Lev Walkin8e8078a2004-09-26 13:10:40 +00007#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * BIT STRING basic type description.
11 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070012static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000013 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
14};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080015asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000016 sizeof(BIT_STRING_t),
17 offsetof(BIT_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000018 ASN_OSUBV_BIT
Lev Walkinbbd93252004-10-12 05:57:23 +000019};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080020asn_TYPE_operation_t asn_OP_BIT_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
22 BIT_STRING_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070023 BIT_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
25 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000026 OCTET_STRING_decode_xer_binary,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 BIT_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070028#ifdef ASN_DISABLE_OER_SUPPORT
29 0,
30 0,
31#else
Lev Walkincfc16d32017-08-30 19:15:08 -070032 BIT_STRING_decode_oer,
33 BIT_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070034#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040035#ifdef ASN_DISABLE_PER_SUPPORT
36 0,
37 0,
38#else
39 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
40 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
41#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070042 BIT_STRING_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080043 0 /* Use generic outmost tag fetcher */
44};
45asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
46 "BIT STRING",
47 "BIT_STRING",
48 &asn_OP_BIT_STRING,
Lev Walkin5e033762004-09-29 13:26:15 +000049 asn_DEF_BIT_STRING_tags,
50 sizeof(asn_DEF_BIT_STRING_tags)
51 / sizeof(asn_DEF_BIT_STRING_tags[0]),
52 asn_DEF_BIT_STRING_tags, /* Same as above */
53 sizeof(asn_DEF_BIT_STRING_tags)
54 / sizeof(asn_DEF_BIT_STRING_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -070055 { 0, 0, BIT_STRING_constraint },
Lev Walkin449f8322004-08-20 13:23:42 +000056 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080057 &asn_SPC_BIT_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000058};
59
60/*
61 * BIT STRING generic constraint.
62 */
63int
Lev Walkin5e033762004-09-29 13:26:15 +000064BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000065 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000066 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000067
68 if(st && st->buf) {
Lev Walkin9abdf262007-02-21 19:27:45 +000069 if((st->size == 0 && st->bits_unused)
70 || st->bits_unused < 0 || st->bits_unused > 7) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070071 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinbbd93252004-10-12 05:57:23 +000072 "%s: invalid padding byte (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +000073 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000074 return -1;
75 }
76 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070077 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000078 "%s: value not given (%s:%d)",
79 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000080 return -1;
81 }
82
83 return 0;
84}
85
johvik5f619fb2017-05-09 09:05:04 +020086static const char *_bit_pattern[16] = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000087 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
88 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
89};
90
91asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000092BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000093 int ilevel, enum xer_encoder_flags_e flags,
94 asn_app_consume_bytes_f *cb, void *app_key) {
95 asn_enc_rval_t er;
96 char scratch[128];
97 char *p = scratch;
98 char *scend = scratch + (sizeof(scratch) - 10);
99 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkincc6a9102004-09-23 22:06:26 +0000100 int xcan = (flags & XER_F_CANONICAL);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101 uint8_t *buf;
102 uint8_t *end;
103
104 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700105 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000106
107 er.encoded = 0;
108
109 buf = st->buf;
110 end = buf + st->size - 1; /* Last byte is special */
111
112 /*
113 * Binary dump
114 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000115 for(; buf < end; buf++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000116 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000117 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000118 if(p >= scend || nline) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700119 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000120 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700121 if(nline) ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000122 }
123 memcpy(p + 0, _bit_pattern[v >> 4], 4);
124 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
125 p += 8;
126 }
127
Lev Walkinbbd93252004-10-12 05:57:23 +0000128 if(!xcan && ((buf - st->buf) % 8) == 0)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700129 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700130 ASN__CALLBACK(scratch, p - scratch);
Lev Walkin938c6fe2004-09-24 20:56:25 +0000131 p = scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132
Lev Walkin938c6fe2004-09-24 20:56:25 +0000133 if(buf == end) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000134 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000135 int ubits = st->bits_unused;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000136 int i;
Lev Walkinbbd93252004-10-12 05:57:23 +0000137 for(i = 7; i >= ubits; i--)
Lev Walkin5e033762004-09-29 13:26:15 +0000138 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700139 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000140 }
141
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700142 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000143
Lev Walkin7c1dc052016-03-14 03:08:15 -0700144 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000145cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700146 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000147}
148
149
Lev Walkinf15320b2004-06-03 03:38:44 +0000150/*
151 * BIT STRING specific contents printer.
152 */
153int
Lev Walkin5e033762004-09-29 13:26:15 +0000154BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700156 const char * const h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +0000158 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 uint8_t *buf;
160 uint8_t *end;
161 char *p = scratch;
162
Lev Walkind9bd7752004-06-05 08:17:50 +0000163 (void)td; /* Unused argument */
164
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 if(!st || !st->buf)
166 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000167
Lev Walkin8e8078a2004-09-26 13:10:40 +0000168 ilevel++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 buf = st->buf;
170 end = buf + st->size;
171
172 /*
173 * Hexadecimal dump.
174 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000175 for(; buf < end; buf++) {
176 if((buf - st->buf) % 16 == 0 && (st->size > 16)
177 && buf != st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000178 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 /* Dump the string */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000180 if(cb(scratch, p - scratch, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 p = scratch;
182 }
183 *p++ = h2c[*buf >> 4];
184 *p++ = h2c[*buf & 0x0F];
185 *p++ = 0x20;
186 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000187
Lev Walkincc6a9102004-09-23 22:06:26 +0000188 if(p > scratch) {
189 p--; /* Eat the tailing space */
190
Lev Walkinbbd93252004-10-12 05:57:23 +0000191 if((st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000192 _i_INDENT(1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000193 }
194
195 /* Dump the incomplete 16-bytes row */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000197 return -1;
198 }
199
200 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000201}
202
Lev Walkincd2f48e2017-08-10 02:14:59 -0700203/*
204 * Lexicographically compare the common prefix of both strings,
205 * and if it is the same return -1 for the smallest string.
206 */
207int
208BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
209 const void *bptr) {
210 const BIT_STRING_t *a = aptr;
211 const BIT_STRING_t *b = bptr;
212
213 (void)td;
214
215 if(a && b) {
216 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
217 int ret = memcmp(a->buf, b->buf, common_prefix_size);
218 if(ret == 0) {
219 /* Figure out which string with equal prefixes is longer. */
220 if(a->size < b->size) {
221 return -1;
222 } else if(a->size > b->size) {
223 return 1;
224 } else {
225 /* Figure out how many unused bits */
226 if(a->bits_unused < b->bits_unused) {
227 return -1;
228 } else if(a->bits_unused > b->bits_unused) {
229 return 1;
230 } else {
231 return 0;
232 }
233 }
234 } else {
235 return ret;
236 }
237 } else if(!a && !b) {
238 return 0;
239 } else if(!a) {
240 return -1;
241 } else {
242 return 1;
243 }
244}
245
Lev Walkina5972be2017-09-29 23:15:58 -0700246
247asn_random_fill_result_t
248BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
249 const asn_encoding_constraints_t *constraints,
250 size_t max_length) {
251 asn_OCTET_STRING_specifics_t *specs =
252 td->specifics ? (asn_OCTET_STRING_specifics_t *)td->specifics
253 : &asn_SPC_BIT_STRING_specs;
254 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
255 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
256 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
257 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
258 126, 127, 128, 16383, 16384, 16385,
259 65534, 65535, 65536, 65537};
260 uint8_t *buf;
261 uint8_t *bend;
262 uint8_t *b;
263 size_t rnd_bits, rnd_len;
264 BIT_STRING_t *st;
265
266 if(max_length == 0) return result_skipped;
267
268 switch(specs->subvariant) {
269 case ASN_OSUBV_ANY:
270 return result_failed;
271 case ASN_OSUBV_BIT:
272 break;
273 default:
274 break;
275 }
276
277 /* Figure out how far we should go */
278 rnd_bits = lengths[asn_random_between(
279 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
280 if(constraints->per_constraints) {
281 const asn_per_constraint_t *pc =
282 &td->encoding_constraints.per_constraints->size;
283 if(pc->flags & APC_CONSTRAINED) {
284 if(max_length < (size_t)pc->lower_bound) {
285 return result_skipped;
286 }
287 rnd_bits = asn_random_between(pc->lower_bound, pc->upper_bound);
288 } else {
289 rnd_bits = asn_random_between(0, max_length - 1);
290 }
291 } else if(rnd_bits >= max_length) {
292 rnd_bits = asn_random_between(0, max_length - 1);
293 }
294
295 rnd_len = (rnd_bits + 7) / 8;
296 buf = CALLOC(1, rnd_len + 1);
297 if(!buf) return result_failed;
298
299 bend = &buf[rnd_len];
300
301 for(b = buf; b < bend; b++) {
302 *(uint8_t *)b = asn_random_between(0, 255);
303 }
304
305 if(*sptr) {
306 st = *sptr;
307 FREEMEM(st->buf);
308 } else {
309 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
310 if(!st) {
311 FREEMEM(buf);
312 return result_failed;
313 }
314 }
315
316 st->buf = buf;
317 st->size = rnd_len;
318 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
319 if(st->bits_unused) {
320 assert(st->size > 0);
321 st->buf[st->size-1] &= 0xff << st->bits_unused;
322 }
323
324 result_ok.length = st->size;
325 return result_ok;
326}