encode BIT STRING in a minimum number of bits for CANONICAL-UPER
diff --git a/skeletons/BIT_STRING.c b/skeletons/BIT_STRING.c
index b5ab945..fed5974 100644
--- a/skeletons/BIT_STRING.c
+++ b/skeletons/BIT_STRING.c
@@ -36,8 +36,8 @@
0,
0,
#else
- OCTET_STRING_decode_uper, /* Unaligned PER decoder */
- OCTET_STRING_encode_uper, /* Unaligned PER encoder */
+ BIT_STRING_decode_uper, /* Unaligned PER decoder */
+ BIT_STRING_encode_uper, /* Unaligned PER encoder */
#endif /* ASN_DISABLE_PER_SUPPORT */
BIT_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
@@ -197,20 +197,77 @@
return -1;
}
+ if(st->bits_unused) {
+ int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
+ st->bits_unused, st->bits_unused == 1 ? "" : "s");
+ assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
+ if(ret > 0 && ret < (ssize_t)sizeof(scratch)
+ && cb(scratch, ret, app_key) < 0)
+ return -1;
+ }
+
return 0;
}
/*
+ * Non-destructively remove the trailing 0-bits from the given bit string.
+ */
+static const BIT_STRING_t *
+BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
+ const uint8_t *b;
+ union {
+ const uint8_t *c_buf;
+ uint8_t *nc_buf;
+ } unconst;
+
+ if(st->size == 0) {
+ assert(st->bits_unused == 0);
+ return st;
+ } else {
+ for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
+ ;
+ }
+ /* b points to the last byte which may contain data */
+ if(*b) {
+ int unused = 7;
+ uint8_t v = *b;
+ v &= -(int8_t)v;
+ if(v & 0x0F) unused -= 4;
+ if(v & 0x33) unused -= 2;
+ if(v & 0x55) unused -= 1;
+ tmp->size = b-st->buf + 1;
+ tmp->bits_unused = unused;
+ } else {
+ tmp->size = b-st->buf;
+ tmp->bits_unused = 0;
+ }
+
+ assert(b >= st->buf);
+ }
+
+ unconst.c_buf = st->buf;
+ tmp->buf = unconst.nc_buf;
+ return tmp;
+}
+
+/*
* Lexicographically compare the common prefix of both strings,
* and if it is the same return -1 for the smallest string.
*/
int
BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
- const BIT_STRING_t *a = aptr;
- const BIT_STRING_t *b = bptr;
+ /*
+ * Remove information about trailing bits, since
+ * X.680 (08/2015) #22.7 "ensure that different semantics are not"
+ * "associated with [values that differ only in] the trailing 0 bits."
+ */
+ BIT_STRING_t compact_a, compact_b;
+ const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
+ const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
+ const asn_OCTET_STRING_specifics_t *specs = td->specifics;
- (void)td;
+ assert(specs && specs->subvariant == ASN_OSUBV_BIT);
if(a && b) {
size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
@@ -223,9 +280,9 @@
return 1;
} else {
/* Figure out how many unused bits */
- if(a->bits_unused < b->bits_unused) {
+ if(a->bits_unused > b->bits_unused) {
return -1;
- } else if(a->bits_unused > b->bits_unused) {
+ } else if(a->bits_unused < b->bits_unused) {
return 1;
} else {
return 0;
@@ -243,6 +300,241 @@
}
}
+#ifndef ASN_DISABLE_PER_SUPPORT
+
+#undef RETURN
+#define RETURN(_code) \
+ do { \
+ asn_dec_rval_t tmprval; \
+ tmprval.code = _code; \
+ tmprval.consumed = consumed_myself; \
+ return tmprval; \
+ } while(0)
+
+static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
+ APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
+
+asn_dec_rval_t
+BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
+ asn_TYPE_descriptor_t *td,
+ const asn_per_constraints_t *constraints, void **sptr,
+ asn_per_data_t *pd) {
+ asn_OCTET_STRING_specifics_t *specs = td->specifics
+ ? (asn_OCTET_STRING_specifics_t *)td->specifics
+ : &asn_SPC_BIT_STRING_specs;
+ const asn_per_constraints_t *pc =
+ constraints ? constraints : td->encoding_constraints.per_constraints;
+ const asn_per_constraint_t *csiz;
+ asn_dec_rval_t rval = { RC_OK, 0 };
+ BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
+ ssize_t consumed_myself = 0;
+ int repeat;
+
+ (void)opt_codec_ctx;
+
+ if(pc) {
+ csiz = &pc->size;
+ } else {
+ csiz = &asn_DEF_BIT_STRING_constraint_size;
+ }
+
+ if(specs->subvariant != ASN_OSUBV_BIT) {
+ ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
+ RETURN(RC_FAIL);
+ }
+
+ /*
+ * Allocate the string.
+ */
+ if(!st) {
+ st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
+ if(!st) RETURN(RC_FAIL);
+ }
+
+ ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
+ csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
+ csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
+
+ if(csiz->flags & APC_EXTENSIBLE) {
+ int inext = per_get_few_bits(pd, 1);
+ if(inext < 0) RETURN(RC_WMORE);
+ if(inext) {
+ csiz = &asn_DEF_BIT_STRING_constraint_size;
+ }
+ }
+
+ if(csiz->effective_bits >= 0) {
+ FREEMEM(st->buf);
+ st->size = (csiz->upper_bound + 7) >> 3;
+ st->buf = (uint8_t *)MALLOC(st->size + 1);
+ if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
+ }
+
+ /* X.691, #16.5: zero-length encoding */
+ /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
+ /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
+ if(csiz->effective_bits == 0) {
+ int ret;
+ ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
+ ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
+ if(ret < 0) RETURN(RC_WMORE);
+ consumed_myself += csiz->upper_bound;
+ st->buf[st->size] = 0;
+ st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
+ RETURN(RC_OK);
+ }
+
+ st->size = 0;
+ do {
+ ssize_t raw_len;
+ ssize_t len_bytes;
+ ssize_t len_bits;
+ void *p;
+ int ret;
+
+ /* Get the PER length */
+ raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
+ &repeat);
+ if(raw_len < 0) RETURN(RC_WMORE);
+
+ ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
+ (long)csiz->effective_bits, (long)raw_len,
+ repeat ? "repeat" : "once", td->name);
+ len_bits = raw_len;
+ len_bytes = (len_bits + 7) >> 3;
+ if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
+ /* len_bits be multiple of 16K if repeat is set */
+ p = REALLOC(st->buf, st->size + len_bytes + 1);
+ if(!p) RETURN(RC_FAIL);
+ st->buf = (uint8_t *)p;
+
+ ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
+ if(ret < 0) RETURN(RC_WMORE);
+ st->size += len_bytes;
+ } while(repeat);
+ st->buf[st->size] = 0; /* nul-terminate */
+
+ return rval;
+}
+
+asn_enc_rval_t
+BIT_STRING_encode_uper(asn_TYPE_descriptor_t *td,
+ const asn_per_constraints_t *constraints, void *sptr,
+ asn_per_outp_t *po) {
+ asn_OCTET_STRING_specifics_t *specs =
+ td->specifics ? (asn_OCTET_STRING_specifics_t *)td->specifics
+ : &asn_SPC_BIT_STRING_specs;
+ const asn_per_constraints_t *pc =
+ constraints ? constraints : td->encoding_constraints.per_constraints;
+ const asn_per_constraint_t *csiz;
+ const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
+ BIT_STRING_t compact_bstr; /* Do not modify this directly! */
+ asn_enc_rval_t er = { 0, 0, 0 };
+ int inext = 0; /* Lies not within extension root */
+ size_t size_in_bits;
+ const uint8_t *buf;
+ int ret;
+ int ct_extensible;
+
+ if(!st || (!st->buf && st->size))
+ ASN__ENCODE_FAILED;
+
+ if(specs->subvariant == ASN_OSUBV_BIT) {
+ if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
+ ASN__ENCODE_FAILED;
+ ASN_DEBUG("BIT STRING of %zu bytes, %d bits unused", size_in_bits,
+ st->bits_unused);
+ } else {
+ ASN__ENCODE_FAILED;
+ }
+
+ if(pc) {
+ csiz = &pc->size;
+ } else {
+ csiz = &asn_DEF_BIT_STRING_constraint_size;
+ }
+ ct_extensible = csiz->flags & APC_EXTENSIBLE;
+
+ /* Figure out the size without the trailing bits */
+ st = BIT_STRING__compactify(st, &compact_bstr);
+ size_in_bits = 8 * st->size - st->bits_unused;
+
+ ASN_DEBUG(
+ "Encoding %s into %zu bits"
+ " (%ld..%ld, effective %d)%s",
+ td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
+ csiz->effective_bits, ct_extensible ? " EXT" : "");
+
+ /* Figure out whether size lies within PER visible constraint */
+
+ if(csiz->effective_bits >= 0) {
+ if((ssize_t)size_in_bits > csiz->upper_bound) {
+ if(ct_extensible) {
+ csiz = &asn_DEF_BIT_STRING_constraint_size;
+ inext = 1;
+ } else {
+ ASN__ENCODE_FAILED;
+ }
+ }
+ } else {
+ inext = 0;
+ }
+
+ if(ct_extensible) {
+ /* Declare whether length is [not] within extension root */
+ if(per_put_few_bits(po, inext, 1))
+ ASN__ENCODE_FAILED;
+ }
+
+ if(csiz->effective_bits >= 0 && !inext) {
+ ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits", st->size,
+ size_in_bits - csiz->lower_bound, csiz->effective_bits);
+ int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
+ ret = per_put_few_bits(
+ po,
+ add_trailer ? csiz->lower_bound : size_in_bits - csiz->lower_bound,
+ csiz->effective_bits);
+ if(ret) ASN__ENCODE_FAILED;
+ ret = per_put_many_bits(po, st->buf, size_in_bits);
+ if(ret) ASN__ENCODE_FAILED;
+ if(add_trailer) {
+ static uint8_t zeros[16];
+ size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
+ while(trailing_zero_bits > 0) {
+ if(trailing_zero_bits > 8 * sizeof(zeros)) {
+ ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
+ trailing_zero_bits -= 8 * sizeof(zeros);
+ } else {
+ ret = per_put_many_bits(po, zeros, trailing_zero_bits);
+ trailing_zero_bits = 0;
+ }
+ if(ret) ASN__ENCODE_FAILED;
+ }
+ }
+ ASN__ENCODED_OK(er);
+ }
+
+ ASN_DEBUG("Encoding %zu bytes", st->size);
+
+ buf = st->buf;
+ do {
+ ssize_t maySave = uper_put_length(po, size_in_bits);
+ if(maySave < 0) ASN__ENCODE_FAILED;
+
+ ASN_DEBUG("Encoding %zd of %zu", maySave, size_in_bits);
+
+ ret = per_put_many_bits(po, buf, maySave);
+ if(ret) ASN__ENCODE_FAILED;
+
+ buf += maySave >> 3;
+ size_in_bits -= maySave;
+ assert(!(maySave & 0x07) || !size_in_bits);
+ } while(size_in_bits);
+
+ ASN__ENCODED_OK(er);
+}
+
+#endif /* ASN_DISABLE_PER_SUPPORT */
asn_random_fill_result_t
BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
@@ -277,14 +569,42 @@
/* Figure out how far we should go */
rnd_bits = lengths[asn_random_between(
0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
+ if(!constraints) constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc =
&td->encoding_constraints.per_constraints->size;
if(pc->flags & APC_CONSTRAINED) {
+ long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
+ ? pc->upper_bound
+ : max_length;
if(max_length < (size_t)pc->lower_bound) {
return result_skipped;
}
- rnd_bits = asn_random_between(pc->lower_bound, pc->upper_bound);
+ if(pc->flags & APC_EXTENSIBLE) {
+ switch(asn_random_between(0, 5)) {
+ case 0:
+ if(pc->lower_bound > 0) {
+ rnd_bits = pc->lower_bound - 1;
+ break;
+ }
+ /* Fall through */
+ case 1:
+ rnd_bits = pc->upper_bound + 1;
+ break;
+ case 2:
+ /* Keep rnd_bits from the table */
+ if(rnd_bits < max_length) {
+ break;
+ }
+ /* Fall through */
+ default:
+ rnd_bits = asn_random_between(pc->lower_bound,
+ suggested_upper_bound);
+ }
+ } else {
+ rnd_bits =
+ asn_random_between(pc->lower_bound, suggested_upper_bound);
+ }
} else {
rnd_bits = asn_random_between(0, max_length - 1);
}
diff --git a/skeletons/BIT_STRING.h b/skeletons/BIT_STRING.h
index dd7a611..ce3be19 100644
--- a/skeletons/BIT_STRING.h
+++ b/skeletons/BIT_STRING.h
@@ -30,14 +30,14 @@
xer_type_encoder_f BIT_STRING_encode_xer;
oer_type_decoder_f BIT_STRING_decode_oer;
oer_type_encoder_f BIT_STRING_encode_oer;
+per_type_decoder_f BIT_STRING_decode_uper;
+per_type_encoder_f BIT_STRING_encode_uper;
asn_random_fill_f BIT_STRING_random_fill;
#define BIT_STRING_free OCTET_STRING_free
#define BIT_STRING_decode_ber OCTET_STRING_decode_ber
#define BIT_STRING_encode_der OCTET_STRING_encode_der
#define BIT_STRING_decode_xer OCTET_STRING_decode_xer_binary
-#define BIT_STRING_decode_uper OCTET_STRING_decode_uper
-#define BIT_STRING_encode_uper OCTET_STRING_encode_uper
#ifdef __cplusplus
}
diff --git a/skeletons/BIT_STRING_oer.c b/skeletons/BIT_STRING_oer.c
index e887228..b18e27f 100644
--- a/skeletons/BIT_STRING_oer.c
+++ b/skeletons/BIT_STRING_oer.c
@@ -47,6 +47,8 @@
}
if(expected_length < 1) {
+ ASN__DECODE_FAILED;
+ } else if(expected_length > size) {
ASN__DECODE_STARVED;
}
diff --git a/skeletons/OCTET_STRING.c b/skeletons/OCTET_STRING.c
index 7ff0156..713c48e 100644
--- a/skeletons/OCTET_STRING.c
+++ b/skeletons/OCTET_STRING.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
+ * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
@@ -1356,11 +1356,10 @@
const asn_per_constraint_t *cval;
const asn_per_constraint_t *csiz;
asn_dec_rval_t rval = { RC_OK, 0 };
- BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
+ OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
ssize_t consumed_myself = 0;
int repeat;
enum {
- OS__BPC_BIT = 0,
OS__BPC_CHAR = 1,
OS__BPC_U16 = 2,
OS__BPC_U32 = 4
@@ -1381,11 +1380,9 @@
switch(specs->subvariant) {
default:
case ASN_OSUBV_ANY:
+ case ASN_OSUBV_BIT:
ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
RETURN(RC_FAIL);
- case ASN_OSUBV_BIT:
- canonical_unit_bits = unit_bits = 1;
- bpc = OS__BPC_BIT;
break;
case ASN_OSUBV_STR:
canonical_unit_bits = unit_bits = 8;
@@ -1411,7 +1408,7 @@
* Allocate the string.
*/
if(!st) {
- st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
+ st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
@@ -1460,10 +1457,6 @@
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += unit_bits * csiz->upper_bound;
st->buf[st->size] = 0;
- if(bpc == 0) {
- int ubs = (csiz->upper_bound & 0x7);
- st->bits_unused = ubs ? 8 - ubs : 0;
- }
RETURN(RC_OK);
}
@@ -1471,7 +1464,6 @@
do {
ssize_t raw_len;
ssize_t len_bytes;
- ssize_t len_bits;
void *p;
int ret;
@@ -1483,29 +1475,15 @@
ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
(long)csiz->effective_bits, (long)raw_len,
repeat ? "repeat" : "once", td->name);
- if(bpc) {
- len_bytes = raw_len * bpc;
- len_bits = len_bytes * unit_bits;
- } else {
- len_bits = raw_len;
- len_bytes = (len_bits + 7) >> 3;
- if(len_bits & 0x7)
- st->bits_unused = 8 - (len_bits & 0x7);
- /* len_bits be multiple of 16K if repeat is set */
- }
+ len_bytes = raw_len * bpc;
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
- if(bpc) {
- ret = OCTET_STRING_per_get_characters(pd,
- &st->buf[st->size], raw_len, bpc, unit_bits,
- cval->lower_bound, cval->upper_bound, pc);
- if(ret > 0) RETURN(RC_FAIL);
- } else {
- ret = per_get_many_bits(pd, &st->buf[st->size],
- 0, len_bits);
- }
+ ret = OCTET_STRING_per_get_characters(pd, &st->buf[st->size], raw_len,
+ bpc, unit_bits, cval->lower_bound,
+ cval->upper_bound, pc);
+ if(ret > 0) RETURN(RC_FAIL);
if(ret < 0) RETURN(RC_WMORE);
st->size += len_bytes;
} while(repeat);
@@ -1525,7 +1503,7 @@
: td->encoding_constraints.per_constraints;
const asn_per_constraint_t *cval;
const asn_per_constraint_t *csiz;
- const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
+ const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
asn_enc_rval_t er = { 0, 0, 0 };
int inext = 0; /* Lies not within extension root */
unsigned int unit_bits;
@@ -1534,7 +1512,6 @@
const uint8_t *buf;
int ret;
enum {
- OS__BPC_BIT = 0,
OS__BPC_CHAR = 1,
OS__BPC_U16 = 2,
OS__BPC_U32 = 4
@@ -1556,14 +1533,8 @@
switch(specs->subvariant) {
default:
case ASN_OSUBV_ANY:
- ASN__ENCODE_FAILED;
case ASN_OSUBV_BIT:
- canonical_unit_bits = unit_bits = 1;
- bpc = OS__BPC_BIT;
- size_in_units = st->size * 8 - (st->bits_unused & 0x07);
- ASN_DEBUG("BIT STRING of %zu bytes, %d bits unused",
- size_in_units, st->bits_unused);
- break;
+ ASN__ENCODE_FAILED;
case ASN_OSUBV_STR:
canonical_unit_bits = unit_bits = 8;
if(cval->flags & APC_CONSTRAINED)
@@ -1630,14 +1601,9 @@
ret = per_put_few_bits(po, size_in_units - csiz->lower_bound,
csiz->effective_bits);
if(ret) ASN__ENCODE_FAILED;
- if(bpc) {
- ret = OCTET_STRING_per_put_characters(
- po, st->buf, size_in_units, bpc, unit_bits, cval->lower_bound,
- cval->upper_bound, pc);
- } else {
- assert(unit_bits == 1);
- ret = per_put_many_bits(po, st->buf, size_in_units);
- }
+ ret = OCTET_STRING_per_put_characters(po, st->buf, size_in_units, bpc,
+ unit_bits, cval->lower_bound,
+ cval->upper_bound, pc);
if(ret) ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
@@ -1654,21 +1620,14 @@
ssize_t maySave = uper_put_length(po, size_in_units);
if(maySave < 0) ASN__ENCODE_FAILED;
- ASN_DEBUG("Encoding %ld of %ld", (long)maySave, (long)sizeinunits);
+ ASN_DEBUG("Encoding %zd of %zu", maySave, size_in_units);
- if(bpc) {
- ret = OCTET_STRING_per_put_characters(po, buf, maySave, bpc,
- unit_bits, cval->lower_bound,
- cval->upper_bound, pc);
- } else {
- ret = per_put_many_bits(po, buf, maySave);
- }
+ ret = OCTET_STRING_per_put_characters(po, buf, maySave, bpc, unit_bits,
+ cval->lower_bound,
+ cval->upper_bound, pc);
if(ret) ASN__ENCODE_FAILED;
- if(bpc)
- buf += maySave * bpc;
- else
- buf += maySave >> 3;
+ buf += maySave * bpc;
size_in_units -= maySave;
assert(!(maySave & 0x07) || !size_in_units);
}
@@ -1848,10 +1807,11 @@
int
OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
+ const asn_OCTET_STRING_specifics_t *specs = td->specifics;
const OCTET_STRING_t *a = aptr;
const OCTET_STRING_t *b = bptr;
- (void)td;
+ assert(!specs || specs->subvariant != ASN_OSUBV_BIT);
if(a && b) {
size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
@@ -1863,18 +1823,6 @@
} else if(a->size > b->size) {
return 1;
} else {
- asn_OCTET_STRING_specifics_t *specs = td->specifics;
- if(specs && specs->subvariant == ASN_OSUBV_BIT) {
- const BIT_STRING_t *ba = aptr;
- const BIT_STRING_t *bb = bptr;
- if(ba->bits_unused > bb->bits_unused) {
- return -1;
- } else if(ba->bits_unused < bb->bits_unused) {
- return 1;
- } else {
- return 0;
- }
- }
return 0;
}
} else {
@@ -1943,22 +1891,22 @@
case ASN_OSUBV_BIT:
/* Handled by BIT_STRING itself. */
return result_failed;
- case ASN_OSUBV_STR:
+ case ASN_OSUBV_STR:
unit_bytes = 1;
clb = 0;
cub = 255;
- break;
- case ASN_OSUBV_U16:
+ break;
+ case ASN_OSUBV_U16:
unit_bytes = 2;
clb = 0;
cub = 65535;
- break;
- case ASN_OSUBV_U32:
+ break;
+ case ASN_OSUBV_U32:
unit_bytes = 4;
clb = 0;
cub = 0x10FFFF;
- break;
- }
+ break;
+ }
if(!constraints) constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
@@ -1979,8 +1927,9 @@
const asn_per_constraint_t *pc =
&td->encoding_constraints.per_constraints->size;
if(pc->flags & APC_CONSTRAINED) {
- long suggested_upper_bound =
- pc->upper_bound < max_length ? pc->upper_bound : max_length;
+ long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
+ ? pc->upper_bound
+ : max_length;
if(max_length < (size_t)pc->lower_bound) {
return result_skipped;
}
diff --git a/skeletons/asn_application.c b/skeletons/asn_application.c
index b558d46..285f2ec 100644
--- a/skeletons/asn_application.c
+++ b/skeletons/asn_application.c
@@ -62,8 +62,8 @@
key->buffer_size = 0;
} else {
memcpy((char *)key->buffer + key->computed_size, data, size);
- key->computed_size += size;
}
+ key->computed_size += size;
return 0;
}
@@ -130,6 +130,10 @@
er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
overrun_encoder_cb, &buf_key);
+ if(er.encoded >= 0 && (size_t)er.encoded != buf_key.computed_size) {
+ ASN_DEBUG("asn_encode returned %zd yet wrote %zu bytes",
+ er.encoded, buf_key.computed_size);
+ }
assert(er.encoded < 0 || (size_t)er.encoded == buf_key.computed_size);
return er;