add some support for INTEGER decoding
diff --git a/skeletons/INTEGER_oer.c b/skeletons/INTEGER_oer.c
index f21850e..a606bf9 100644
--- a/skeletons/INTEGER_oer.c
+++ b/skeletons/INTEGER_oer.c
@@ -17,21 +17,125 @@
asn_dec_rval_t rval = {RC_OK, 0};
INTEGER_t *st = (INTEGER_t *)*sptr;
asn_oer_constraint_t *ct;
- int repeat;
+ size_t req_bytes = 0; /* 0 = length determinant is required */
(void)opt_codec_ctx;
+ (void)specs;
if(!st) {
st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
if(!st) ASN__DECODE_FAILED;
}
- if(!constraints) constraints = td->oer_constraints;
- ct = constraints ? &constraints->value : 0;
-
FREEMEM(st->buf);
st->buf = 0;
st->size = 0;
+
+ if(!constraints) constraints = td->oer_constraints;
+ ct = constraints ? &constraints->value : 0;
+
+ if(ct && (ct->flags & AOC_HAS_LOWER_BOUND) && ct->lower_bound >= 0) {
+ /* X.969 08/2015 10.2(a) */
+ unsigned msb; /* Most significant bit */
+ size_t useful_size;
+
+ intmax_t ub = ct->upper_bound;
+ if(ct->flags & AOC_HAS_UPPER_BOUND) {
+ if(ub <= 255) {
+ req_bytes = 1;
+ } else if(ub <= 65535) {
+ req_bytes = 2;
+ } else if(ub <= 4294967295UL) {
+ req_bytes = 4;
+ } else if(ub <= 18446744073709551615ULL) {
+ req_bytes = 8;
+ }
+ }
+
+ if(req_bytes == 0) { /* #8.6, using length determinant */
+ ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
+ if(consumed == 0) {
+ ASN__DECODE_STARVED;
+ } else if(consumed == -1) {
+ ASN__DECODE_FAILED;
+ }
+ rval.consumed += consumed;
+ ptr = (const char *)ptr + consumed;
+ size -= consumed;
+ }
+
+ if(req_bytes > size) {
+ ASN__DECODE_STARVED;
+ }
+
+ /* Check most significant bit */
+ msb = *(const uint8_t *)ptr >> 7; /* yields 0 or 1 */
+ useful_size = msb + req_bytes;
+ st->buf = (uint8_t *)MALLOC(useful_size + 1);
+ if(!st->buf) {
+ ASN__DECODE_FAILED;
+ }
+
+ /*
+ * Record a large unsigned in a way not to confuse it
+ * with signed value.
+ */
+ st->buf[0] = '\0';
+ memcpy(st->buf + msb, ptr, req_bytes);
+ st->buf[useful_size] = '\0'; /* Just in case, 0-terminate */
+ st->size = useful_size;
+
+ rval.consumed += req_bytes;
+ return rval;
+ } else if(ct && (ct->flags & AOC_HAS_LOWER_BOUND)) {
+ /* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */
+
+ intmax_t lb = ct->lower_bound;
+ intmax_t ub = ct->upper_bound;
+
+ if(ct->flags & AOC_HAS_UPPER_BOUND) {
+ if(lb >= -128 && ub <= 127) {
+ req_bytes = 1;
+ } else if(lb >= -32768 && ub <= 32767) {
+ req_bytes = 2;
+ } else if(lb >= -2147483648L && ub <= 2147483647L) {
+ req_bytes = 4;
+ } else if(lb >= -9223372036854775808LL
+ && ub <= 9223372036854775807LL) {
+ req_bytes = 8;
+ }
+ }
+ }
+
+ /* No lower bound and no upper bound, effectively */
+
+ if(req_bytes == 0) { /* #8.6, using length determinant */
+ ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
+ if(consumed == 0) {
+ ASN__DECODE_STARVED;
+ } else if(consumed == -1) {
+ ASN__DECODE_FAILED;
+ }
+ rval.consumed += consumed;
+ ptr = (const char *)ptr + consumed;
+ size -= consumed;
+ }
+
+ if(req_bytes > size) {
+ ASN__DECODE_STARVED;
+ }
+
+ st->buf = (uint8_t *)MALLOC(req_bytes + 1);
+ if(!st->buf) {
+ ASN__DECODE_FAILED;
+ }
+
+ memcpy(st->buf, ptr, req_bytes);
+ st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
+ st->size = req_bytes;
+
+ rval.consumed += req_bytes;
+ return rval;
}
#endif /* ASN_DISABLE_OER_SUPPORT */
diff --git a/skeletons/Makefile.am b/skeletons/Makefile.am
index eeb69d3..df3d3da 100644
--- a/skeletons/Makefile.am
+++ b/skeletons/Makefile.am
@@ -71,7 +71,7 @@
der_encoder.c der_encoder.h \
oer_decoder.c oer_decoder.h \
oer_encoder.c oer_encoder.h \
- oer_support.h \
+ oer_support.c oer_support.h \
per_decoder.c per_decoder.h \
per_encoder.c per_encoder.h \
per_opentype.c per_opentype.h \
diff --git a/skeletons/oer_support.c b/skeletons/oer_support.c
new file mode 100644
index 0000000..62d2f6d
--- /dev/null
+++ b/skeletons/oer_support.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
+ * All rights reserved.
+ * Redistribution and modifications are oermitted subject to BSD license.
+ */
+#include <asn_system.h>
+#include <asn_internal.h>
+
+#include <oer_support.h>
+
+/*
+ * Fetch the length determinant (X.696 08/2015, #8.6) into *len_r.
+ * RETURN VALUES:
+ * 0: More data expected than bufptr contains.
+ * -1: Fatal error deciphering length.
+ * >0: Number of bytes used from bufptr.
+ */
+ssize_t
+oer_fetch_length(const void *bufptr, size_t size, size_t *len_r) {
+ uint8_t first_byte;
+ uint8_t len_len; /* Length of the length determinant */
+ const uint8_t *b;
+ const uint8_t *bend;
+ size_t len;
+
+ if(size == 0) {
+ *len_r = 0;
+ return 0;
+ }
+
+ first_byte = *(const uint8_t *)bufptr;
+ if((first_byte & 0x80) == 0) { /* Short form */
+ *len_r = first_byte; /* 0..127 */
+ return 1;
+ }
+
+ len_len = (first_byte & 0x7f);
+ if((1 + len_len) > size) {
+ *len_r = 0;
+ return 0;
+ }
+
+ b = (const uint8_t *)bufptr + 1;
+ bend = b + len_len;
+
+ for(; b < bend && *b == 0; b++) {
+ /* Skip the leading 0-bytes */
+ }
+
+ if((bend - b) > sizeof(size_t)) {
+ /* Length is not representable by the native size_t type */
+ *len_r = 0;
+ return -1;
+ }
+
+ for(len = 0; b < bend; b++) {
+ len = (len << 8) + *b;
+ }
+
+ *len_r = len;
+ assert(len_len + 1 == bend - (const uint8_t *)bufptr);
+ return len_len + 1;
+}
+
+
diff --git a/skeletons/oer_support.h b/skeletons/oer_support.h
index 764c88e..3fba47e 100644
--- a/skeletons/oer_support.h
+++ b/skeletons/oer_support.h
@@ -21,13 +21,24 @@
AOC_HAS_UPPER_BOUND = 0x02
} flags;
intmax_t lower_bound;
- intmax_t upoer_bound;
+ intmax_t upper_bound;
} asn_oer_constraint_t;
typedef const struct asn_oer_constraints_s {
struct asn_oer_constraint_s value;
struct asn_oer_constraint_s size;
} asn_oer_constraints_t;
+
+/*
+ * Fetch the length determinant (X.696 08/2015, #8.6) into *len_r.
+ * RETURN VALUES:
+ * 0: More data expected than bufptr contains.
+ * -1: Fatal error deciphering length.
+ * >0: Number of bytes used from bufptr.
+ */
+ssize_t oer_fetch_length(const void *bufptr, size_t size, size_t *len_r);
+
+
#ifdef __cplusplus
}
#endif
diff --git a/skeletons/tests/check-OER-INTEGER.c b/skeletons/tests/check-OER-INTEGER.c
index 1e91999..a202180 100644
--- a/skeletons/tests/check-OER-INTEGER.c
+++ b/skeletons/tests/check-OER-INTEGER.c
@@ -1,29 +1,66 @@
#include <stdio.h>
#include <assert.h>
+#include <asn_codecs.h>
#include <INTEGER.h>
-#include <INTEGER.c>
-#include <INTEGER_oer.c>
-#define CHECK_DECODE_OK(a, b, c) check_decode_ok(__LINE__, a, b, c)
+#define CHECK_DECODE(code, a, b, c, d, e, f) check_decode(__LINE__, code, a, b, c, d, e, f)
-void
-check_decode_ok(int lineno, intmax_t control, char *buf, size_t size) {
+static const intmax_t NoBound = -4200024;
+
+static void
+check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const char *buf, size_t size, const char *dummy, intmax_t lower_bound, intmax_t upper_bound) {
+ static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
+
+ fprintf(stderr, "%d: buf[%d]={%d, %d, ...}\n", lineno, size,
+ ((const uint8_t *)buf)[0],
+ ((const uint8_t *)buf)[1]);
INTEGER_t *st = NULL;
asn_dec_rval_t ret;
+ struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
+ struct asn_oer_constraints_s *constraints = &empty_constraints;
+ struct asn_oer_constraint_s *ct_value = &constraints->value;
- ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, 0, (void **)&st, buf, size);
+ /* Setup integer constraints as requested */
+ if(lower_bound == NoBound && upper_bound == NoBound) {
+ constraints = NULL;
+ } else {
+ if(lower_bound != NoBound) {
+ ct_value->flags |= AOC_HAS_LOWER_BOUND;
+ ct_value->lower_bound = lower_bound;
+ }
+ if(upper_bound != NoBound) {
+ ct_value->flags |= AOC_HAS_UPPER_BOUND;
+ ct_value->upper_bound = upper_bound;
+ }
+ }
+
+ (void)dummy;
+
+ ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
+ (void **)&st, buf, size);
if(ret.code != RC_OK) {
/* Basic OER decode does not work */
fprintf(stderr, "%d: Failed oer_decode(ctl=%" PRIdMAX ", size=%zu)\n",
lineno, control, size);
- assert(ret.code == RC_OK);
+ if(ret.code == code) {
+ fprintf(stderr, " (That was expected)\n");
+ return;
+ } else {
+ fprintf(
+ stderr, " Unexpected return code %s (%d) expected %s\n",
+ code_s[(unsigned)ret.code <= RC_FAIL ? RC_FAIL : (RC_FAIL + 1)],
+ (int)ret.code, code_s[code]);
+ assert(ret.code == code);
+ }
} else {
intmax_t outcome;
if(asn_INTEGER2imax(st, &outcome) != 0) {
/* Result of decode is structurally incorrect */
- fprintf(stderr, "%d: Failed to convert INTEGER 2 imax\n",
+ fprintf(stderr,
+ "%d: Failed to convert INTEGER 2 imax; structurally "
+ "incorrect INTEGER\n",
lineno);
assert(!"Unreachable");
} else if(outcome != control) {
@@ -41,5 +78,28 @@
int
main() {
- CHECK_DECODE_OK(0, "", 1);
+ CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", NoBound, NoBound);
+ CHECK_DECODE(RC_FAIL, 0, "\x00", 1, "bounds=", NoBound, NoBound);
+ CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 0);
+ CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 1);
+ CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 0, 1);
+ CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
+
+ CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
+ CHECK_DECODE(RC_OK, 1, "\x01", 1, "bounds=", -1, 1);
+ CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
+ CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
+ CHECK_DECODE(RC_OK, 127, "\x7f", 1, "bounds=", 0, 127);
+ CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 127);
+ CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 255);
+ CHECK_DECODE(RC_WMORE, 0, "\xff", 1, "bounds=", 0, 256);
+ CHECK_DECODE(RC_OK, 65535, "\xff\xff", 2, "bounds=", 0, 256);
+
+ CHECK_DECODE(RC_OK, 0, "\x01\x00", 2, "bounds=", NoBound, 1);
+ CHECK_DECODE(RC_OK, 1, "\x01\x01", 2, "bounds=", NoBound, 1);
+ CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
+ CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
+ CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 255);
+ CHECK_DECODE(RC_WMORE, -1, "\x02\x00\xff", 2, "bounds=", NoBound, 200);
+ CHECK_DECODE(RC_OK, 255, "\x02\x00\xff", 3, "bounds=", NoBound, 200);
}