blob: fed5974d10e448a3f7adc637e05f5fc0562eb9ec [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) {
322 asn_OCTET_STRING_specifics_t *specs = td->specifics
323 ? (asn_OCTET_STRING_specifics_t *)td->specifics
324 : &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);
399
400 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
401 (long)csiz->effective_bits, (long)raw_len,
402 repeat ? "repeat" : "once", td->name);
403 len_bits = raw_len;
404 len_bytes = (len_bits + 7) >> 3;
405 if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
406 /* len_bits be multiple of 16K if repeat is set */
407 p = REALLOC(st->buf, st->size + len_bytes + 1);
408 if(!p) RETURN(RC_FAIL);
409 st->buf = (uint8_t *)p;
410
411 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
412 if(ret < 0) RETURN(RC_WMORE);
413 st->size += len_bytes;
414 } while(repeat);
415 st->buf[st->size] = 0; /* nul-terminate */
416
417 return rval;
418}
419
420asn_enc_rval_t
421BIT_STRING_encode_uper(asn_TYPE_descriptor_t *td,
422 const asn_per_constraints_t *constraints, void *sptr,
423 asn_per_outp_t *po) {
424 asn_OCTET_STRING_specifics_t *specs =
425 td->specifics ? (asn_OCTET_STRING_specifics_t *)td->specifics
426 : &asn_SPC_BIT_STRING_specs;
427 const asn_per_constraints_t *pc =
428 constraints ? constraints : td->encoding_constraints.per_constraints;
429 const asn_per_constraint_t *csiz;
430 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
431 BIT_STRING_t compact_bstr; /* Do not modify this directly! */
432 asn_enc_rval_t er = { 0, 0, 0 };
433 int inext = 0; /* Lies not within extension root */
434 size_t size_in_bits;
435 const uint8_t *buf;
436 int ret;
437 int ct_extensible;
438
439 if(!st || (!st->buf && st->size))
440 ASN__ENCODE_FAILED;
441
442 if(specs->subvariant == ASN_OSUBV_BIT) {
443 if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
444 ASN__ENCODE_FAILED;
445 ASN_DEBUG("BIT STRING of %zu bytes, %d bits unused", size_in_bits,
446 st->bits_unused);
447 } else {
448 ASN__ENCODE_FAILED;
449 }
450
451 if(pc) {
452 csiz = &pc->size;
453 } else {
454 csiz = &asn_DEF_BIT_STRING_constraint_size;
455 }
456 ct_extensible = csiz->flags & APC_EXTENSIBLE;
457
458 /* Figure out the size without the trailing bits */
459 st = BIT_STRING__compactify(st, &compact_bstr);
460 size_in_bits = 8 * st->size - st->bits_unused;
461
462 ASN_DEBUG(
463 "Encoding %s into %zu bits"
464 " (%ld..%ld, effective %d)%s",
465 td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
466 csiz->effective_bits, ct_extensible ? " EXT" : "");
467
468 /* Figure out whether size lies within PER visible constraint */
469
470 if(csiz->effective_bits >= 0) {
471 if((ssize_t)size_in_bits > csiz->upper_bound) {
472 if(ct_extensible) {
473 csiz = &asn_DEF_BIT_STRING_constraint_size;
474 inext = 1;
475 } else {
476 ASN__ENCODE_FAILED;
477 }
478 }
479 } else {
480 inext = 0;
481 }
482
483 if(ct_extensible) {
484 /* Declare whether length is [not] within extension root */
485 if(per_put_few_bits(po, inext, 1))
486 ASN__ENCODE_FAILED;
487 }
488
489 if(csiz->effective_bits >= 0 && !inext) {
490 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits", st->size,
491 size_in_bits - csiz->lower_bound, csiz->effective_bits);
492 int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
493 ret = per_put_few_bits(
494 po,
495 add_trailer ? csiz->lower_bound : size_in_bits - csiz->lower_bound,
496 csiz->effective_bits);
497 if(ret) ASN__ENCODE_FAILED;
498 ret = per_put_many_bits(po, st->buf, size_in_bits);
499 if(ret) ASN__ENCODE_FAILED;
500 if(add_trailer) {
501 static uint8_t zeros[16];
502 size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
503 while(trailing_zero_bits > 0) {
504 if(trailing_zero_bits > 8 * sizeof(zeros)) {
505 ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
506 trailing_zero_bits -= 8 * sizeof(zeros);
507 } else {
508 ret = per_put_many_bits(po, zeros, trailing_zero_bits);
509 trailing_zero_bits = 0;
510 }
511 if(ret) ASN__ENCODE_FAILED;
512 }
513 }
514 ASN__ENCODED_OK(er);
515 }
516
517 ASN_DEBUG("Encoding %zu bytes", st->size);
518
519 buf = st->buf;
520 do {
521 ssize_t maySave = uper_put_length(po, size_in_bits);
522 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);
532 } while(size_in_bits);
533
534 ASN__ENCODED_OK(er);
535}
536
537#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700538
539asn_random_fill_result_t
540BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
541 const asn_encoding_constraints_t *constraints,
542 size_t max_length) {
543 asn_OCTET_STRING_specifics_t *specs =
544 td->specifics ? (asn_OCTET_STRING_specifics_t *)td->specifics
545 : &asn_SPC_BIT_STRING_specs;
546 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
547 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
548 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
549 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
550 126, 127, 128, 16383, 16384, 16385,
551 65534, 65535, 65536, 65537};
552 uint8_t *buf;
553 uint8_t *bend;
554 uint8_t *b;
555 size_t rnd_bits, rnd_len;
556 BIT_STRING_t *st;
557
558 if(max_length == 0) return result_skipped;
559
560 switch(specs->subvariant) {
561 case ASN_OSUBV_ANY:
562 return result_failed;
563 case ASN_OSUBV_BIT:
564 break;
565 default:
566 break;
567 }
568
569 /* Figure out how far we should go */
570 rnd_bits = lengths[asn_random_between(
571 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
Lev Walkina5b02882017-10-01 22:48:44 -0700572 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkina5972be2017-09-29 23:15:58 -0700573 if(constraints->per_constraints) {
574 const asn_per_constraint_t *pc =
575 &td->encoding_constraints.per_constraints->size;
576 if(pc->flags & APC_CONSTRAINED) {
Lev Walkina5b02882017-10-01 22:48:44 -0700577 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
578 ? pc->upper_bound
579 : max_length;
Lev Walkina5972be2017-09-29 23:15:58 -0700580 if(max_length < (size_t)pc->lower_bound) {
581 return result_skipped;
582 }
Lev Walkina5b02882017-10-01 22:48:44 -0700583 if(pc->flags & APC_EXTENSIBLE) {
584 switch(asn_random_between(0, 5)) {
585 case 0:
586 if(pc->lower_bound > 0) {
587 rnd_bits = pc->lower_bound - 1;
588 break;
589 }
590 /* Fall through */
591 case 1:
592 rnd_bits = pc->upper_bound + 1;
593 break;
594 case 2:
595 /* Keep rnd_bits from the table */
596 if(rnd_bits < max_length) {
597 break;
598 }
599 /* Fall through */
600 default:
601 rnd_bits = asn_random_between(pc->lower_bound,
602 suggested_upper_bound);
603 }
604 } else {
605 rnd_bits =
606 asn_random_between(pc->lower_bound, suggested_upper_bound);
607 }
Lev Walkina5972be2017-09-29 23:15:58 -0700608 } else {
609 rnd_bits = asn_random_between(0, max_length - 1);
610 }
611 } else if(rnd_bits >= max_length) {
612 rnd_bits = asn_random_between(0, max_length - 1);
613 }
614
615 rnd_len = (rnd_bits + 7) / 8;
616 buf = CALLOC(1, rnd_len + 1);
617 if(!buf) return result_failed;
618
619 bend = &buf[rnd_len];
620
621 for(b = buf; b < bend; b++) {
622 *(uint8_t *)b = asn_random_between(0, 255);
623 }
624
625 if(*sptr) {
626 st = *sptr;
627 FREEMEM(st->buf);
628 } else {
629 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
630 if(!st) {
631 FREEMEM(buf);
632 return result_failed;
633 }
634 }
635
636 st->buf = buf;
637 st->size = rnd_len;
638 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
639 if(st->bits_unused) {
640 assert(st->size > 0);
641 st->buf[st->size-1] &= 0xff << st->bits_unused;
642 }
643
644 result_ok.length = st->size;
645 return result_ok;
646}