ASN.1: Support wider bit sets (fix #514)

This commit is contained in:
Nicolas Williams
2019-01-06 23:12:24 -06:00
committed by Nico Williams
parent 10164490b7
commit a3a8c1e4a4
5 changed files with 73 additions and 22 deletions

View File

@@ -763,6 +763,27 @@ define_type (int level, const char *name, const char *basename, Type *t, int typ
Member *m;
Type i;
struct range range = { 0, UINT_MAX };
size_t max_memno = 0;
size_t bitset_size;
/*
* range.max implies the size of the base unsigned integer used for the
* bitfield members. If it's less than or equal to UINT_MAX, then that
* will be unsigned int, otherwise it will be uint64_t.
*
* We could just use uint64_t, yes, but for now, and in case that any
* projects were exposing the BIT STRING types' C representations in
* ABIs prior to this compiler supporting BIT STRING with larger
* members, we stick to this.
*/
ASN1_TAILQ_FOREACH(m, t->members, members) {
if (m->val > max_memno)
max_memno = m->val;
}
if (max_memno > 63)
range.max = INT64_MAX;
else
range.max = 1LU << max_memno;
i.type = TInteger;
i.range = &range;
@@ -780,10 +801,13 @@ define_type (int level, const char *name, const char *basename, Type *t, int typ
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *n = NULL;
/* pad unused */
/*
* pad unused bits beween declared members (hopefully this
* forces the compiler to give us an obvious layout)
*/
while (pos < m->val) {
if (asprintf (&n, "_unused%d:1", pos) < 0 || n == NULL)
errx(1, "malloc");
err(1, "malloc");
define_type (level + 1, n, newbasename, &i, FALSE, FALSE);
free(n);
pos++;
@@ -797,8 +821,13 @@ define_type (int level, const char *name, const char *basename, Type *t, int typ
n = NULL;
pos++;
}
/* pad to 32 elements */
while (pos < 32) {
/* pad unused tail (ditto) */
bitset_size = max_memno;
if (max_memno > 31)
bitset_size += 64 - (max_memno % 64);
else
bitset_size = 32;
while (pos < bitset_size) {
char *n = NULL;
if (asprintf (&n, "_unused%d:1", pos) < 0 || n == NULL)
errx(1, "malloc");