Commit 21e996b8 authored by johvik's avatar johvik

Incomplete: Various fixes

* Empty fragment encoding
* Dereference DEFAULT to get the actual value
* Don't write length bits for small fixed-sized sequence-of
* Index encoding in choice extensions
* Only align when range == 256
parent 1a900ebf
......@@ -1043,7 +1043,8 @@ INTEGER_encode_aper(const asn_TYPE_descriptor_t *td,
v = value - ct->lower_bound;
/* #12 <= 8 -> alignment ? */
if (ct->range_bits < 8) {
int range = ct->upper_bound - ct->lower_bound + 1;
if (ct->range_bits < 8 || (ct->range_bits == 8 && range < 256)) {
if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
ASN__ENCODE_FAILED;
} else if (ct->range_bits == 8) {
......@@ -1101,12 +1102,14 @@ INTEGER_encode_aper(const asn_TYPE_descriptor_t *td,
}
for(buf = st->buf, end = st->buf + st->size; buf < end;) {
ssize_t mayEncode = aper_put_length(po, -1, end - buf);
int need_eom = 0;
ssize_t mayEncode = aper_put_length(po, -1, end - buf, &need_eom);
if(mayEncode < 0)
ASN__ENCODE_FAILED;
if(per_put_many_bits(po, buf, 8 * mayEncode))
ASN__ENCODE_FAILED;
buf += mayEncode;
if(need_eom && aper_put_length(po, -1, 0, 0)) ASN__ENCODE_FAILED;
}
ASN__ENCODED_OK(er);
......
......@@ -1953,10 +1953,10 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td,
st->size, sizeinunits - csiz->lower_bound,
csiz->effective_bits);
if (csiz->effective_bits > 0) {
ret = aper_put_length(po, csiz->upper_bound - csiz->lower_bound + 1, sizeinunits - csiz->lower_bound);
ret = aper_put_length(po, csiz->upper_bound - csiz->lower_bound + 1, sizeinunits - csiz->lower_bound, 0);
if(ret) ASN__ENCODE_FAILED;
}
if ((st->size > 2) || (csiz->upper_bound > 2)) { /* X.691 #16 NOTE 1 */
if (csiz->effective_bits > 0 || (st->size > 2) || (csiz->upper_bound > (2 * 8 / unit_bits))) { /* X.691 #16 NOTE 1 */
if (aper_put_align(po) < 0)
ASN__ENCODE_FAILED;
}
......@@ -1975,14 +1975,15 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td,
ASN_DEBUG("Encoding %lu bytes", st->size);
if(sizeinunits == 0) {
if(aper_put_length(po, -1, 0))
if(aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
buf = st->buf;
while(sizeinunits) {
ssize_t maySave = aper_put_length(po, -1, sizeinunits);
int need_eom = 0;
ssize_t maySave = aper_put_length(po, -1, sizeinunits, &need_eom);
if(maySave < 0) ASN__ENCODE_FAILED;
......@@ -2004,6 +2005,8 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td,
buf += maySave >> 3;
sizeinunits -= maySave;
assert(!(maySave & 0x07) || !sizeinunits);
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
ASN__ENCODED_OK(er);
......
......@@ -1164,10 +1164,13 @@ CHOICE_encode_aper(const asn_TYPE_descriptor_t *td,
memb_ptr, po);
} else {
asn_enc_rval_t rval = {0,0,0};
int tmp_range_bits = (int) (ct? ct->range_bits : -1);
if(specs->ext_start == -1)
ASN__ENCODE_FAILED;
if(aper_put_nsnnwn(po, tmp_range_bits, present - specs->ext_start))
int n = present - specs->ext_start;
if(n <= 63) {
if(n < 0) ASN__ENCODE_FAILED;
if(per_put_few_bits(po, n, 7)) ASN__ENCODE_FAILED;
} else
ASN__ENCODE_FAILED;
if(aper_open_type_put(elm->type, elm->encoding_constraints.per_constraints,
memb_ptr, po))
......
......@@ -1835,7 +1835,7 @@ SEQUENCE_encode_aper(const asn_TYPE_descriptor_t *td,
/* Eliminate default values */
if(present && elm->default_value_cmp
&& elm->default_value_cmp(memb_ptr2) == 0)
&& elm->default_value_cmp(*memb_ptr2) == 0)
present = 0;
ASN_DEBUG("Element %s %s %s->%s is %s",
......@@ -1881,7 +1881,7 @@ SEQUENCE_encode_aper(const asn_TYPE_descriptor_t *td,
}
/* Eliminate default values */
if(elm->default_value_cmp && elm->default_value_cmp(memb_ptr2) == 0)
if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
continue;
ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
......
......@@ -269,17 +269,20 @@ SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
ct->effective_bits))
ASN__ENCODE_FAILED;
*/
if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound) < 0)
if (ct->lower_bound == ct->upper_bound && ct->upper_bound < 65536) {
/* No length determinant */
} else if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0)
ASN__ENCODE_FAILED;
}
for(seq = -1; seq < list->count;) {
ssize_t mayEncode;
int need_eom = 0;
if(seq < 0) seq = 0;
if(ct && ct->effective_bits >= 0) {
mayEncode = list->count;
} else {
mayEncode = aper_put_length(po, -1, list->count - seq);
mayEncode = aper_put_length(po, -1, list->count - seq, &need_eom);
if(mayEncode < 0) ASN__ENCODE_FAILED;
}
......@@ -291,6 +294,9 @@ SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
if(er.encoded == -1)
ASN__ENCODE_FAILED;
}
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
ASN__ENCODED_OK(er);
......
......@@ -1157,7 +1157,7 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
ct->effective_bits))
ASN__ENCODE_FAILED;*/
if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound) < 0) {
if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0) {
ASN__ENCODE_FAILED;
}
}
......@@ -1170,11 +1170,12 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
for(seq = 0; seq < list->count;) {
ssize_t may_encode;
int need_eom = 0;
if(ct && ct->effective_bits >= 0) {
may_encode = list->count;
} else {
may_encode =
aper_put_length(po, -1, list->count - seq);
aper_put_length(po, -1, list->count - seq, &need_eom);
if(may_encode < 0) ASN__ENCODE_FAILED;
}
......@@ -1185,6 +1186,8 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
break;
}
}
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
SET_OF__encode_sorted_free(encoded_els, list->count);
......
......@@ -487,11 +487,16 @@ aper_open_type_put(const asn_TYPE_descriptor_t *td,
if(size <= 0) return -1;
for(bptr = buf, toGo = size; toGo;) {
ssize_t maySave = aper_put_length(po, -1, toGo);
int need_eom = 0;
ssize_t maySave = aper_put_length(po, -1, toGo, &need_eom);
if(maySave < 0) break;
if(per_put_many_bits(po, bptr, maySave * 8)) break;
bptr = (char *)bptr + maySave;
toGo -= maySave;
if(need_eom && aper_put_length(po, -1, 0, 0)) {
FREEMEM(buf);
return -1;
}
}
FREEMEM(buf);
......
......@@ -440,7 +440,11 @@ int aper_put_align(asn_per_outp_t *po) {
}
ssize_t
aper_put_length(asn_per_outp_t *po, int range, size_t length) {
aper_put_length(asn_per_outp_t *po, int range, size_t length, int *need_eom) {
int dummy = 0;
if(!need_eom) need_eom = &dummy;
*need_eom = 0;
ASN_DEBUG("APER put length %zu with range %d", length, range);
......@@ -459,8 +463,12 @@ aper_put_length(asn_per_outp_t *po, int range, size_t length) {
return per_put_few_bits(po, length|0x8000, 16)
? -1 : (ssize_t)length;
*need_eom = 0 == (length & 16383);
length >>= 14;
if(length > 4) length = 4;
if(length > 4) {
*need_eom = 0;
length = 4;
}
return per_put_few_bits(po, 0xC0 | length, 8)
? -1 : (ssize_t)(length << 14);
......@@ -475,7 +483,7 @@ aper_put_nslength(asn_per_outp_t *po, size_t length) {
if(length == 0) return -1;
return per_put_few_bits(po, length-1, 7) ? -1 : 0;
} else {
if(aper_put_length(po, -1, length) != (ssize_t)length) {
if(aper_put_length(po, -1, length, 0) != (ssize_t)length) {
/* This might happen in case of >16K extensions */
return -1;
}
......
......@@ -101,7 +101,8 @@ int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int
ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
int *opt_need_eom);
ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length);
ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length,
int *opt_need_eom);
/* Align the current bit position to octet bundary */
int aper_put_align(asn_per_outp_t *po);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment