BIT_STRING.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. */
  5. #include <asn_internal.h>
  6. #include <BIT_STRING.h>
  7. #include <asn_internal.h>
  8. /*
  9. * BIT STRING basic type description.
  10. */
  11. static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
  12. (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
  13. };
  14. asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
  15. sizeof(BIT_STRING_t),
  16. offsetof(BIT_STRING_t, _asn_ctx),
  17. ASN_OSUBV_BIT
  18. };
  19. asn_TYPE_operation_t asn_OP_BIT_STRING = {
  20. OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
  21. BIT_STRING_print,
  22. BIT_STRING_compare,
  23. OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
  24. OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
  25. OCTET_STRING_decode_xer_binary,
  26. BIT_STRING_encode_xer,
  27. #ifdef ASN_DISABLE_OER_SUPPORT
  28. 0,
  29. 0,
  30. #else
  31. BIT_STRING_decode_oer,
  32. BIT_STRING_encode_oer,
  33. #endif /* ASN_DISABLE_OER_SUPPORT */
  34. #ifdef ASN_DISABLE_PER_SUPPORT
  35. 0,
  36. 0,
  37. #else
  38. BIT_STRING_decode_uper, /* Unaligned PER decoder */
  39. BIT_STRING_encode_uper, /* Unaligned PER encoder */
  40. #endif /* ASN_DISABLE_PER_SUPPORT */
  41. BIT_STRING_random_fill,
  42. 0 /* Use generic outmost tag fetcher */
  43. };
  44. asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
  45. "BIT STRING",
  46. "BIT_STRING",
  47. &asn_OP_BIT_STRING,
  48. asn_DEF_BIT_STRING_tags,
  49. sizeof(asn_DEF_BIT_STRING_tags)
  50. / sizeof(asn_DEF_BIT_STRING_tags[0]),
  51. asn_DEF_BIT_STRING_tags, /* Same as above */
  52. sizeof(asn_DEF_BIT_STRING_tags)
  53. / sizeof(asn_DEF_BIT_STRING_tags[0]),
  54. { 0, 0, BIT_STRING_constraint },
  55. 0, 0, /* No members */
  56. &asn_SPC_BIT_STRING_specs
  57. };
  58. /*
  59. * BIT STRING generic constraint.
  60. */
  61. int
  62. BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
  63. asn_app_constraint_failed_f *ctfailcb, void *app_key) {
  64. const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
  65. if(st && st->buf) {
  66. if((st->size == 0 && st->bits_unused)
  67. || st->bits_unused < 0 || st->bits_unused > 7) {
  68. ASN__CTFAIL(app_key, td, sptr,
  69. "%s: invalid padding byte (%s:%d)",
  70. td->name, __FILE__, __LINE__);
  71. return -1;
  72. }
  73. } else {
  74. ASN__CTFAIL(app_key, td, sptr,
  75. "%s: value not given (%s:%d)",
  76. td->name, __FILE__, __LINE__);
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. static const char *_bit_pattern[16] = {
  82. "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
  83. "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
  84. };
  85. asn_enc_rval_t
  86. BIT_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
  87. int ilevel, enum xer_encoder_flags_e flags,
  88. asn_app_consume_bytes_f *cb, void *app_key) {
  89. asn_enc_rval_t er;
  90. char scratch[128];
  91. char *p = scratch;
  92. char *scend = scratch + (sizeof(scratch) - 10);
  93. const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
  94. int xcan = (flags & XER_F_CANONICAL);
  95. uint8_t *buf;
  96. uint8_t *end;
  97. if(!st || !st->buf)
  98. ASN__ENCODE_FAILED;
  99. er.encoded = 0;
  100. buf = st->buf;
  101. end = buf + st->size - 1; /* Last byte is special */
  102. /*
  103. * Binary dump
  104. */
  105. for(; buf < end; buf++) {
  106. int v = *buf;
  107. int nline = xcan?0:(((buf - st->buf) % 8) == 0);
  108. if(p >= scend || nline) {
  109. ASN__CALLBACK(scratch, p - scratch);
  110. p = scratch;
  111. if(nline) ASN__TEXT_INDENT(1, ilevel);
  112. }
  113. memcpy(p + 0, _bit_pattern[v >> 4], 4);
  114. memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
  115. p += 8;
  116. }
  117. if(!xcan && ((buf - st->buf) % 8) == 0)
  118. ASN__TEXT_INDENT(1, ilevel);
  119. ASN__CALLBACK(scratch, p - scratch);
  120. p = scratch;
  121. if(buf == end) {
  122. int v = *buf;
  123. int ubits = st->bits_unused;
  124. int i;
  125. for(i = 7; i >= ubits; i--)
  126. *p++ = (v & (1 << i)) ? 0x31 : 0x30;
  127. ASN__CALLBACK(scratch, p - scratch);
  128. }
  129. if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
  130. ASN__ENCODED_OK(er);
  131. cb_failed:
  132. ASN__ENCODE_FAILED;
  133. }
  134. /*
  135. * BIT STRING specific contents printer.
  136. */
  137. int
  138. BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  139. asn_app_consume_bytes_f *cb, void *app_key) {
  140. const char * const h2c = "0123456789ABCDEF";
  141. char scratch[64];
  142. const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
  143. uint8_t *buf;
  144. uint8_t *end;
  145. char *p = scratch;
  146. (void)td; /* Unused argument */
  147. if(!st || !st->buf)
  148. return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  149. ilevel++;
  150. buf = st->buf;
  151. end = buf + st->size;
  152. /*
  153. * Hexadecimal dump.
  154. */
  155. for(; buf < end; buf++) {
  156. if((buf - st->buf) % 16 == 0 && (st->size > 16)
  157. && buf != st->buf) {
  158. _i_INDENT(1);
  159. /* Dump the string */
  160. if(cb(scratch, p - scratch, app_key) < 0) return -1;
  161. p = scratch;
  162. }
  163. *p++ = h2c[*buf >> 4];
  164. *p++ = h2c[*buf & 0x0F];
  165. *p++ = 0x20;
  166. }
  167. if(p > scratch) {
  168. p--; /* Eat the tailing space */
  169. if((st->size > 16)) {
  170. _i_INDENT(1);
  171. }
  172. /* Dump the incomplete 16-bytes row */
  173. if(cb(scratch, p - scratch, app_key) < 0)
  174. return -1;
  175. }
  176. if(st->bits_unused) {
  177. int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
  178. st->bits_unused, st->bits_unused == 1 ? "" : "s");
  179. assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
  180. if(ret > 0 && ret < (ssize_t)sizeof(scratch)
  181. && cb(scratch, ret, app_key) < 0)
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * Non-destructively remove the trailing 0-bits from the given bit string.
  188. */
  189. static const BIT_STRING_t *
  190. BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
  191. const uint8_t *b;
  192. union {
  193. const uint8_t *c_buf;
  194. uint8_t *nc_buf;
  195. } unconst;
  196. if(st->size == 0) {
  197. assert(st->bits_unused == 0);
  198. return st;
  199. } else {
  200. for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
  201. ;
  202. }
  203. /* b points to the last byte which may contain data */
  204. if(*b) {
  205. int unused = 7;
  206. uint8_t v = *b;
  207. v &= -(int8_t)v;
  208. if(v & 0x0F) unused -= 4;
  209. if(v & 0x33) unused -= 2;
  210. if(v & 0x55) unused -= 1;
  211. tmp->size = b-st->buf + 1;
  212. tmp->bits_unused = unused;
  213. } else {
  214. tmp->size = b-st->buf;
  215. tmp->bits_unused = 0;
  216. }
  217. assert(b >= st->buf);
  218. }
  219. unconst.c_buf = st->buf;
  220. tmp->buf = unconst.nc_buf;
  221. return tmp;
  222. }
  223. /*
  224. * Lexicographically compare the common prefix of both strings,
  225. * and if it is the same return -1 for the smallest string.
  226. */
  227. int
  228. BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
  229. const void *bptr) {
  230. /*
  231. * Remove information about trailing bits, since
  232. * X.680 (08/2015) #22.7 "ensure that different semantics are not"
  233. * "associated with [values that differ only in] the trailing 0 bits."
  234. */
  235. BIT_STRING_t compact_a, compact_b;
  236. const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
  237. const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
  238. const asn_OCTET_STRING_specifics_t *specs = td->specifics;
  239. assert(specs && specs->subvariant == ASN_OSUBV_BIT);
  240. if(a && b) {
  241. size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
  242. int ret = memcmp(a->buf, b->buf, common_prefix_size);
  243. if(ret == 0) {
  244. /* Figure out which string with equal prefixes is longer. */
  245. if(a->size < b->size) {
  246. return -1;
  247. } else if(a->size > b->size) {
  248. return 1;
  249. } else {
  250. /* Figure out how many unused bits */
  251. if(a->bits_unused > b->bits_unused) {
  252. return -1;
  253. } else if(a->bits_unused < b->bits_unused) {
  254. return 1;
  255. } else {
  256. return 0;
  257. }
  258. }
  259. } else {
  260. return ret;
  261. }
  262. } else if(!a && !b) {
  263. return 0;
  264. } else if(!a) {
  265. return -1;
  266. } else {
  267. return 1;
  268. }
  269. }
  270. #ifndef ASN_DISABLE_PER_SUPPORT
  271. #undef RETURN
  272. #define RETURN(_code) \
  273. do { \
  274. asn_dec_rval_t tmprval; \
  275. tmprval.code = _code; \
  276. tmprval.consumed = consumed_myself; \
  277. return tmprval; \
  278. } while(0)
  279. static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
  280. APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
  281. asn_dec_rval_t
  282. BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
  283. const asn_TYPE_descriptor_t *td,
  284. const asn_per_constraints_t *constraints, void **sptr,
  285. asn_per_data_t *pd) {
  286. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  287. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  288. : &asn_SPC_BIT_STRING_specs;
  289. const asn_per_constraints_t *pc =
  290. constraints ? constraints : td->encoding_constraints.per_constraints;
  291. const asn_per_constraint_t *csiz;
  292. asn_dec_rval_t rval = { RC_OK, 0 };
  293. BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
  294. ssize_t consumed_myself = 0;
  295. int repeat;
  296. (void)opt_codec_ctx;
  297. if(pc) {
  298. csiz = &pc->size;
  299. } else {
  300. csiz = &asn_DEF_BIT_STRING_constraint_size;
  301. }
  302. if(specs->subvariant != ASN_OSUBV_BIT) {
  303. ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
  304. RETURN(RC_FAIL);
  305. }
  306. /*
  307. * Allocate the string.
  308. */
  309. if(!st) {
  310. st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  311. if(!st) RETURN(RC_FAIL);
  312. }
  313. ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
  314. csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
  315. csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
  316. if(csiz->flags & APC_EXTENSIBLE) {
  317. int inext = per_get_few_bits(pd, 1);
  318. if(inext < 0) RETURN(RC_WMORE);
  319. if(inext) {
  320. csiz = &asn_DEF_BIT_STRING_constraint_size;
  321. }
  322. }
  323. if(csiz->effective_bits >= 0) {
  324. FREEMEM(st->buf);
  325. st->size = (csiz->upper_bound + 7) >> 3;
  326. st->buf = (uint8_t *)MALLOC(st->size + 1);
  327. if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
  328. }
  329. /* X.691, #16.5: zero-length encoding */
  330. /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
  331. /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
  332. if(csiz->effective_bits == 0) {
  333. int ret;
  334. ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
  335. ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
  336. if(ret < 0) RETURN(RC_WMORE);
  337. consumed_myself += csiz->upper_bound;
  338. st->buf[st->size] = 0;
  339. st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
  340. RETURN(RC_OK);
  341. }
  342. st->size = 0;
  343. do {
  344. ssize_t raw_len;
  345. ssize_t len_bytes;
  346. ssize_t len_bits;
  347. void *p;
  348. int ret;
  349. /* Get the PER length */
  350. raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
  351. &repeat);
  352. if(raw_len < 0) RETURN(RC_WMORE);
  353. if(raw_len == 0 && st->buf) break;
  354. ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
  355. (long)csiz->effective_bits, (long)raw_len,
  356. repeat ? "repeat" : "once", td->name);
  357. len_bits = raw_len;
  358. len_bytes = (len_bits + 7) >> 3;
  359. if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
  360. /* len_bits be multiple of 16K if repeat is set */
  361. p = REALLOC(st->buf, st->size + len_bytes + 1);
  362. if(!p) RETURN(RC_FAIL);
  363. st->buf = (uint8_t *)p;
  364. ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
  365. if(ret < 0) RETURN(RC_WMORE);
  366. st->size += len_bytes;
  367. } while(repeat);
  368. st->buf[st->size] = 0; /* nul-terminate */
  369. return rval;
  370. }
  371. asn_enc_rval_t
  372. BIT_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
  373. const asn_per_constraints_t *constraints,
  374. const void *sptr, asn_per_outp_t *po) {
  375. const asn_OCTET_STRING_specifics_t *specs =
  376. td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  377. : &asn_SPC_BIT_STRING_specs;
  378. const asn_per_constraints_t *pc =
  379. constraints ? constraints : td->encoding_constraints.per_constraints;
  380. const asn_per_constraint_t *csiz;
  381. const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
  382. BIT_STRING_t compact_bstr; /* Do not modify this directly! */
  383. asn_enc_rval_t er = { 0, 0, 0 };
  384. int inext = 0; /* Lies not within extension root */
  385. size_t size_in_bits;
  386. const uint8_t *buf;
  387. int ret;
  388. int ct_extensible;
  389. if(!st || (!st->buf && st->size))
  390. ASN__ENCODE_FAILED;
  391. if(specs->subvariant == ASN_OSUBV_BIT) {
  392. if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
  393. ASN__ENCODE_FAILED;
  394. } else {
  395. ASN__ENCODE_FAILED;
  396. }
  397. if(pc) {
  398. csiz = &pc->size;
  399. } else {
  400. csiz = &asn_DEF_BIT_STRING_constraint_size;
  401. }
  402. ct_extensible = csiz->flags & APC_EXTENSIBLE;
  403. /* Figure out the size without the trailing bits */
  404. st = BIT_STRING__compactify(st, &compact_bstr);
  405. size_in_bits = 8 * st->size - st->bits_unused;
  406. ASN_DEBUG(
  407. "Encoding %s into %" ASN_PRI_SIZE " bits"
  408. " (%ld..%ld, effective %d)%s",
  409. td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
  410. csiz->effective_bits, ct_extensible ? " EXT" : "");
  411. /* Figure out whether size lies within PER visible constraint */
  412. if(csiz->effective_bits >= 0) {
  413. if((ssize_t)size_in_bits > csiz->upper_bound) {
  414. if(ct_extensible) {
  415. csiz = &asn_DEF_BIT_STRING_constraint_size;
  416. inext = 1;
  417. } else {
  418. ASN__ENCODE_FAILED;
  419. }
  420. }
  421. } else {
  422. inext = 0;
  423. }
  424. if(ct_extensible) {
  425. /* Declare whether length is [not] within extension root */
  426. if(per_put_few_bits(po, inext, 1))
  427. ASN__ENCODE_FAILED;
  428. }
  429. if(csiz->effective_bits >= 0 && !inext) {
  430. int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
  431. ASN_DEBUG(
  432. "Encoding %" ASN_PRI_SIZE " bytes (%ld), length (in %d bits) trailer %d; actual "
  433. "value %" ASN_PRI_SSIZE "",
  434. st->size, size_in_bits - csiz->lower_bound, csiz->effective_bits,
  435. add_trailer,
  436. add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound);
  437. ret = per_put_few_bits(
  438. po, add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound,
  439. csiz->effective_bits);
  440. if(ret) ASN__ENCODE_FAILED;
  441. ret = per_put_many_bits(po, st->buf, size_in_bits);
  442. if(ret) ASN__ENCODE_FAILED;
  443. if(add_trailer) {
  444. static const uint8_t zeros[16];
  445. size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
  446. while(trailing_zero_bits > 0) {
  447. if(trailing_zero_bits > 8 * sizeof(zeros)) {
  448. ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
  449. trailing_zero_bits -= 8 * sizeof(zeros);
  450. } else {
  451. ret = per_put_many_bits(po, zeros, trailing_zero_bits);
  452. trailing_zero_bits = 0;
  453. }
  454. if(ret) ASN__ENCODE_FAILED;
  455. }
  456. }
  457. ASN__ENCODED_OK(er);
  458. }
  459. ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
  460. buf = st->buf;
  461. do {
  462. int need_eom = 0;
  463. ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
  464. if(maySave < 0) ASN__ENCODE_FAILED;
  465. ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "", maySave, size_in_bits);
  466. ret = per_put_many_bits(po, buf, maySave);
  467. if(ret) ASN__ENCODE_FAILED;
  468. buf += maySave >> 3;
  469. size_in_bits -= maySave;
  470. assert(!(maySave & 0x07) || !size_in_bits);
  471. if(need_eom && uper_put_length(po, 0, 0))
  472. ASN__ENCODE_FAILED; /* End of Message length */
  473. } while(size_in_bits);
  474. ASN__ENCODED_OK(er);
  475. }
  476. #endif /* ASN_DISABLE_PER_SUPPORT */
  477. asn_random_fill_result_t
  478. BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
  479. const asn_encoding_constraints_t *constraints,
  480. size_t max_length) {
  481. const asn_OCTET_STRING_specifics_t *specs =
  482. td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  483. : &asn_SPC_BIT_STRING_specs;
  484. asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
  485. asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
  486. asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
  487. static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
  488. 126, 127, 128, 16383, 16384, 16385,
  489. 65534, 65535, 65536, 65537};
  490. uint8_t *buf;
  491. uint8_t *bend;
  492. uint8_t *b;
  493. size_t rnd_bits, rnd_len;
  494. BIT_STRING_t *st;
  495. if(max_length == 0) return result_skipped;
  496. switch(specs->subvariant) {
  497. case ASN_OSUBV_ANY:
  498. return result_failed;
  499. case ASN_OSUBV_BIT:
  500. break;
  501. default:
  502. break;
  503. }
  504. /* Figure out how far we should go */
  505. rnd_bits = lengths[asn_random_between(
  506. 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
  507. if(!constraints || !constraints->per_constraints)
  508. constraints = &td->encoding_constraints;
  509. if(constraints->per_constraints) {
  510. const asn_per_constraint_t *pc = &constraints->per_constraints->size;
  511. if(pc->flags & APC_CONSTRAINED) {
  512. long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
  513. ? pc->upper_bound
  514. : (ssize_t)max_length;
  515. if(max_length < (size_t)pc->lower_bound) {
  516. return result_skipped;
  517. }
  518. if(pc->flags & APC_EXTENSIBLE) {
  519. switch(asn_random_between(0, 5)) {
  520. case 0:
  521. if(pc->lower_bound > 0) {
  522. rnd_bits = pc->lower_bound - 1;
  523. break;
  524. }
  525. /* Fall through */
  526. case 1:
  527. rnd_bits = pc->upper_bound + 1;
  528. break;
  529. case 2:
  530. /* Keep rnd_bits from the table */
  531. if(rnd_bits < max_length) {
  532. break;
  533. }
  534. /* Fall through */
  535. default:
  536. rnd_bits = asn_random_between(pc->lower_bound,
  537. suggested_upper_bound);
  538. }
  539. } else {
  540. rnd_bits =
  541. asn_random_between(pc->lower_bound, suggested_upper_bound);
  542. }
  543. } else {
  544. rnd_bits = asn_random_between(0, max_length - 1);
  545. }
  546. } else if(rnd_bits >= max_length) {
  547. rnd_bits = asn_random_between(0, max_length - 1);
  548. }
  549. rnd_len = (rnd_bits + 7) / 8;
  550. buf = CALLOC(1, rnd_len + 1);
  551. if(!buf) return result_failed;
  552. bend = &buf[rnd_len];
  553. for(b = buf; b < bend; b++) {
  554. *(uint8_t *)b = asn_random_between(0, 255);
  555. }
  556. *b = 0; /* Zero-terminate just in case. */
  557. if(*sptr) {
  558. st = *sptr;
  559. FREEMEM(st->buf);
  560. } else {
  561. st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  562. if(!st) {
  563. FREEMEM(buf);
  564. return result_failed;
  565. }
  566. }
  567. st->buf = buf;
  568. st->size = rnd_len;
  569. st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
  570. if(st->bits_unused) {
  571. assert(st->size > 0);
  572. st->buf[st->size-1] &= 0xff << st->bits_unused;
  573. }
  574. result_ok.length = st->size;
  575. return result_ok;
  576. }