blob: 69e7bc737fd7ed3695f494b73b65dea67863aa47 [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
Lev Walkina5b02882017-10-01 22:48:44 -070039 BIT_STRING_decode_uper, /* Unaligned PER decoder */
40 BIT_STRING_encode_uper, /* Unaligned PER encoder */
Lev Walkinb33425f2017-07-14 14:59:52 +040041#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
Lev Walkina5b02882017-10-01 22:48:44 -0700200 if(st->bits_unused) {
201 int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
202 st->bits_unused, st->bits_unused == 1 ? "" : "s");
203 assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
204 if(ret > 0 && ret < (ssize_t)sizeof(scratch)
205 && cb(scratch, ret, app_key) < 0)
206 return -1;
207 }
208
Lev Walkincc6a9102004-09-23 22:06:26 +0000209 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000210}
211
Lev Walkincd2f48e2017-08-10 02:14:59 -0700212/*
Lev Walkina5b02882017-10-01 22:48:44 -0700213 * Non-destructively remove the trailing 0-bits from the given bit string.
214 */
215static const BIT_STRING_t *
216BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
217 const uint8_t *b;
218 union {
219 const uint8_t *c_buf;
220 uint8_t *nc_buf;
221 } unconst;
222
223 if(st->size == 0) {
224 assert(st->bits_unused == 0);
225 return st;
226 } else {
227 for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
228 ;
229 }
230 /* b points to the last byte which may contain data */
231 if(*b) {
232 int unused = 7;
233 uint8_t v = *b;
234 v &= -(int8_t)v;
235 if(v & 0x0F) unused -= 4;
236 if(v & 0x33) unused -= 2;
237 if(v & 0x55) unused -= 1;
238 tmp->size = b-st->buf + 1;
239 tmp->bits_unused = unused;
240 } else {
241 tmp->size = b-st->buf;
242 tmp->bits_unused = 0;
243 }
244
245 assert(b >= st->buf);
246 }
247
248 unconst.c_buf = st->buf;
249 tmp->buf = unconst.nc_buf;
250 return tmp;
251}
252
253/*
Lev Walkincd2f48e2017-08-10 02:14:59 -0700254 * Lexicographically compare the common prefix of both strings,
255 * and if it is the same return -1 for the smallest string.
256 */
257int
258BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
259 const void *bptr) {
Lev Walkina5b02882017-10-01 22:48:44 -0700260 /*
261 * Remove information about trailing bits, since
262 * X.680 (08/2015) #22.7 "ensure that different semantics are not"
263 * "associated with [values that differ only in] the trailing 0 bits."
264 */
265 BIT_STRING_t compact_a, compact_b;
266 const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
267 const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
268 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700269
Lev Walkina5b02882017-10-01 22:48:44 -0700270 assert(specs && specs->subvariant == ASN_OSUBV_BIT);
Lev Walkincd2f48e2017-08-10 02:14:59 -0700271
272 if(a && b) {
273 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
274 int ret = memcmp(a->buf, b->buf, common_prefix_size);
275 if(ret == 0) {
276 /* Figure out which string with equal prefixes is longer. */
277 if(a->size < b->size) {
278 return -1;
279 } else if(a->size > b->size) {
280 return 1;
281 } else {
282 /* Figure out how many unused bits */
Lev Walkina5b02882017-10-01 22:48:44 -0700283 if(a->bits_unused > b->bits_unused) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700284 return -1;
Lev Walkina5b02882017-10-01 22:48:44 -0700285 } else if(a->bits_unused < b->bits_unused) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700286 return 1;
287 } else {
288 return 0;
289 }
290 }
291 } else {
292 return ret;
293 }
294 } else if(!a && !b) {
295 return 0;
296 } else if(!a) {
297 return -1;
298 } else {
299 return 1;
300 }
301}
302
Lev Walkina5b02882017-10-01 22:48:44 -0700303#ifndef ASN_DISABLE_PER_SUPPORT
304
305#undef RETURN
306#define RETURN(_code) \
307 do { \
308 asn_dec_rval_t tmprval; \
309 tmprval.code = _code; \
310 tmprval.consumed = consumed_myself; \
311 return tmprval; \
312 } while(0)
313
314static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
315 APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
316
317asn_dec_rval_t
318BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
319 asn_TYPE_descriptor_t *td,
320 const asn_per_constraints_t *constraints, void **sptr,
321 asn_per_data_t *pd) {
Lev Walkind84f6032017-10-03 16:33:59 -0700322 const asn_OCTET_STRING_specifics_t *specs = td->specifics
323 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Lev Walkina5b02882017-10-01 22:48:44 -0700324 : &asn_SPC_BIT_STRING_specs;
325 const asn_per_constraints_t *pc =
326 constraints ? constraints : td->encoding_constraints.per_constraints;
327 const asn_per_constraint_t *csiz;
328 asn_dec_rval_t rval = { RC_OK, 0 };
329 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
330 ssize_t consumed_myself = 0;
331 int repeat;
332
333 (void)opt_codec_ctx;
334
335 if(pc) {
336 csiz = &pc->size;
337 } else {
338 csiz = &asn_DEF_BIT_STRING_constraint_size;
339 }
340
341 if(specs->subvariant != ASN_OSUBV_BIT) {
342 ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
343 RETURN(RC_FAIL);
344 }
345
346 /*
347 * Allocate the string.
348 */
349 if(!st) {
350 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
351 if(!st) RETURN(RC_FAIL);
352 }
353
354 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
355 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
356 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
357
358 if(csiz->flags & APC_EXTENSIBLE) {
359 int inext = per_get_few_bits(pd, 1);
360 if(inext < 0) RETURN(RC_WMORE);
361 if(inext) {
362 csiz = &asn_DEF_BIT_STRING_constraint_size;
363 }
364 }
365
366 if(csiz->effective_bits >= 0) {
367 FREEMEM(st->buf);
368 st->size = (csiz->upper_bound + 7) >> 3;
369 st->buf = (uint8_t *)MALLOC(st->size + 1);
370 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
371 }
372
373 /* X.691, #16.5: zero-length encoding */
374 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
375 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
376 if(csiz->effective_bits == 0) {
377 int ret;
378 ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
379 ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
380 if(ret < 0) RETURN(RC_WMORE);
381 consumed_myself += csiz->upper_bound;
382 st->buf[st->size] = 0;
383 st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
384 RETURN(RC_OK);
385 }
386
387 st->size = 0;
388 do {
389 ssize_t raw_len;
390 ssize_t len_bytes;
391 ssize_t len_bits;
392 void *p;
393 int ret;
394
395 /* Get the PER length */
396 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
397 &repeat);
398 if(raw_len < 0) RETURN(RC_WMORE);
Lev Walkin5d947a82017-10-03 01:04:03 -0700399 if(raw_len == 0 && st->buf) break;
Lev Walkina5b02882017-10-01 22:48:44 -0700400
401 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
402 (long)csiz->effective_bits, (long)raw_len,
403 repeat ? "repeat" : "once", td->name);
404 len_bits = raw_len;
405 len_bytes = (len_bits + 7) >> 3;
406 if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
407 /* len_bits be multiple of 16K if repeat is set */
408 p = REALLOC(st->buf, st->size + len_bytes + 1);
409 if(!p) RETURN(RC_FAIL);
410 st->buf = (uint8_t *)p;
411
412 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
413 if(ret < 0) RETURN(RC_WMORE);
414 st->size += len_bytes;
415 } while(repeat);
416 st->buf[st->size] = 0; /* nul-terminate */
417
418 return rval;
419}
420
421asn_enc_rval_t
422BIT_STRING_encode_uper(asn_TYPE_descriptor_t *td,
423 const asn_per_constraints_t *constraints, void *sptr,
424 asn_per_outp_t *po) {
Lev Walkind84f6032017-10-03 16:33:59 -0700425 const asn_OCTET_STRING_specifics_t *specs =
426 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Lev Walkina5b02882017-10-01 22:48:44 -0700427 : &asn_SPC_BIT_STRING_specs;
428 const asn_per_constraints_t *pc =
429 constraints ? constraints : td->encoding_constraints.per_constraints;
430 const asn_per_constraint_t *csiz;
431 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
432 BIT_STRING_t compact_bstr; /* Do not modify this directly! */
433 asn_enc_rval_t er = { 0, 0, 0 };
434 int inext = 0; /* Lies not within extension root */
435 size_t size_in_bits;
436 const uint8_t *buf;
437 int ret;
438 int ct_extensible;
439
440 if(!st || (!st->buf && st->size))
441 ASN__ENCODE_FAILED;
442
443 if(specs->subvariant == ASN_OSUBV_BIT) {
444 if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
445 ASN__ENCODE_FAILED;
Lev Walkina5b02882017-10-01 22:48:44 -0700446 } else {
447 ASN__ENCODE_FAILED;
448 }
449
450 if(pc) {
451 csiz = &pc->size;
452 } else {
453 csiz = &asn_DEF_BIT_STRING_constraint_size;
454 }
455 ct_extensible = csiz->flags & APC_EXTENSIBLE;
456
457 /* Figure out the size without the trailing bits */
458 st = BIT_STRING__compactify(st, &compact_bstr);
459 size_in_bits = 8 * st->size - st->bits_unused;
460
461 ASN_DEBUG(
462 "Encoding %s into %zu bits"
463 " (%ld..%ld, effective %d)%s",
464 td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
465 csiz->effective_bits, ct_extensible ? " EXT" : "");
466
467 /* Figure out whether size lies within PER visible constraint */
468
469 if(csiz->effective_bits >= 0) {
470 if((ssize_t)size_in_bits > csiz->upper_bound) {
471 if(ct_extensible) {
472 csiz = &asn_DEF_BIT_STRING_constraint_size;
473 inext = 1;
474 } else {
475 ASN__ENCODE_FAILED;
476 }
477 }
478 } else {
479 inext = 0;
480 }
481
482 if(ct_extensible) {
483 /* Declare whether length is [not] within extension root */
484 if(per_put_few_bits(po, inext, 1))
485 ASN__ENCODE_FAILED;
486 }
487
488 if(csiz->effective_bits >= 0 && !inext) {
Lev Walkind84f6032017-10-03 16:33:59 -0700489 int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
Lev Walkina5b02882017-10-01 22:48:44 -0700490 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits", st->size,
491 size_in_bits - csiz->lower_bound, csiz->effective_bits);
Lev Walkina5b02882017-10-01 22:48:44 -0700492 ret = per_put_few_bits(
493 po,
494 add_trailer ? csiz->lower_bound : size_in_bits - csiz->lower_bound,
495 csiz->effective_bits);
496 if(ret) ASN__ENCODE_FAILED;
497 ret = per_put_many_bits(po, st->buf, size_in_bits);
498 if(ret) ASN__ENCODE_FAILED;
499 if(add_trailer) {
500 static uint8_t zeros[16];
501 size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
502 while(trailing_zero_bits > 0) {
503 if(trailing_zero_bits > 8 * sizeof(zeros)) {
504 ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
505 trailing_zero_bits -= 8 * sizeof(zeros);
506 } else {
507 ret = per_put_many_bits(po, zeros, trailing_zero_bits);
508 trailing_zero_bits = 0;
509 }
510 if(ret) ASN__ENCODE_FAILED;
511 }
512 }
513 ASN__ENCODED_OK(er);
514 }
515
516 ASN_DEBUG("Encoding %zu bytes", st->size);
517
518 buf = st->buf;
519 do {
Lev Walkin5d947a82017-10-03 01:04:03 -0700520 int need_eom = 0;
521 ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
Lev Walkina5b02882017-10-01 22:48:44 -0700522 if(maySave < 0) ASN__ENCODE_FAILED;
523
524 ASN_DEBUG("Encoding %zd of %zu", maySave, size_in_bits);
525
526 ret = per_put_many_bits(po, buf, maySave);
527 if(ret) ASN__ENCODE_FAILED;
528
529 buf += maySave >> 3;
530 size_in_bits -= maySave;
531 assert(!(maySave & 0x07) || !size_in_bits);
Lev Walkin5d947a82017-10-03 01:04:03 -0700532 if(need_eom && uper_put_length(po, 0, 0))
533 ASN__ENCODE_FAILED; /* End of Message length */
Lev Walkina5b02882017-10-01 22:48:44 -0700534 } while(size_in_bits);
535
536 ASN__ENCODED_OK(er);
537}
538
539#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700540
541asn_random_fill_result_t
542BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
543 const asn_encoding_constraints_t *constraints,
544 size_t max_length) {
Lev Walkind84f6032017-10-03 16:33:59 -0700545 const asn_OCTET_STRING_specifics_t *specs =
546 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Lev Walkina5972be2017-09-29 23:15:58 -0700547 : &asn_SPC_BIT_STRING_specs;
548 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
549 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
550 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
551 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
552 126, 127, 128, 16383, 16384, 16385,
553 65534, 65535, 65536, 65537};
554 uint8_t *buf;
555 uint8_t *bend;
556 uint8_t *b;
557 size_t rnd_bits, rnd_len;
558 BIT_STRING_t *st;
559
560 if(max_length == 0) return result_skipped;
561
562 switch(specs->subvariant) {
563 case ASN_OSUBV_ANY:
564 return result_failed;
565 case ASN_OSUBV_BIT:
566 break;
567 default:
568 break;
569 }
570
571 /* Figure out how far we should go */
572 rnd_bits = lengths[asn_random_between(
573 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
Lev Walkina5b02882017-10-01 22:48:44 -0700574 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkina5972be2017-09-29 23:15:58 -0700575 if(constraints->per_constraints) {
576 const asn_per_constraint_t *pc =
577 &td->encoding_constraints.per_constraints->size;
578 if(pc->flags & APC_CONSTRAINED) {
Lev Walkina5b02882017-10-01 22:48:44 -0700579 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
580 ? pc->upper_bound
581 : max_length;
Lev Walkina5972be2017-09-29 23:15:58 -0700582 if(max_length < (size_t)pc->lower_bound) {
583 return result_skipped;
584 }
Lev Walkina5b02882017-10-01 22:48:44 -0700585 if(pc->flags & APC_EXTENSIBLE) {
586 switch(asn_random_between(0, 5)) {
587 case 0:
588 if(pc->lower_bound > 0) {
589 rnd_bits = pc->lower_bound - 1;
590 break;
591 }
592 /* Fall through */
593 case 1:
594 rnd_bits = pc->upper_bound + 1;
595 break;
596 case 2:
597 /* Keep rnd_bits from the table */
598 if(rnd_bits < max_length) {
599 break;
600 }
601 /* Fall through */
602 default:
603 rnd_bits = asn_random_between(pc->lower_bound,
604 suggested_upper_bound);
605 }
606 } else {
607 rnd_bits =
608 asn_random_between(pc->lower_bound, suggested_upper_bound);
609 }
Lev Walkina5972be2017-09-29 23:15:58 -0700610 } else {
611 rnd_bits = asn_random_between(0, max_length - 1);
612 }
613 } else if(rnd_bits >= max_length) {
614 rnd_bits = asn_random_between(0, max_length - 1);
615 }
616
617 rnd_len = (rnd_bits + 7) / 8;
618 buf = CALLOC(1, rnd_len + 1);
619 if(!buf) return result_failed;
620
621 bend = &buf[rnd_len];
622
623 for(b = buf; b < bend; b++) {
624 *(uint8_t *)b = asn_random_between(0, 255);
625 }
626
627 if(*sptr) {
628 st = *sptr;
629 FREEMEM(st->buf);
630 } else {
631 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
632 if(!st) {
633 FREEMEM(buf);
634 return result_failed;
635 }
636 }
637
638 st->buf = buf;
639 st->size = rnd_len;
640 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
641 if(st->bits_unused) {
642 assert(st->size > 0);
643 st->buf[st->size-1] &= 0xff << st->bits_unused;
644 }
645
646 result_ok.length = st->size;
647 return result_ok;
648}