add OER codec for string types
diff --git a/skeletons/BMPString.c b/skeletons/BMPString.c
index 482b5e1..cca7d72 100644
--- a/skeletons/BMPString.c
+++ b/skeletons/BMPString.c
@@ -37,8 +37,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/GeneralString.c b/skeletons/GeneralString.c
index 4d32d6e..0e87d12 100644
--- a/skeletons/GeneralString.c
+++ b/skeletons/GeneralString.c
@@ -26,8 +26,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/GraphicString.c b/skeletons/GraphicString.c
index 1e955ed..cb86412 100644
--- a/skeletons/GraphicString.c
+++ b/skeletons/GraphicString.c
@@ -26,8 +26,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/IA5String.c b/skeletons/IA5String.c
index 068c18d..9f51a8e 100644
--- a/skeletons/IA5String.c
+++ b/skeletons/IA5String.c
@@ -31,8 +31,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/ISO646String.c b/skeletons/ISO646String.c
index 87313ba..55447f9 100644
--- a/skeletons/ISO646String.c
+++ b/skeletons/ISO646String.c
@@ -31,8 +31,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/NumericString.c b/skeletons/NumericString.c
index cac26c8..b0eda2c 100644
--- a/skeletons/NumericString.c
+++ b/skeletons/NumericString.c
@@ -51,8 +51,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/OCTET_STRING.h b/skeletons/OCTET_STRING.h
index 013c7b1..7261992 100644
--- a/skeletons/OCTET_STRING.h
+++ b/skeletons/OCTET_STRING.h
@@ -30,6 +30,8 @@
xer_type_decoder_f OCTET_STRING_decode_xer_utf8; /* ASCII/UTF-8 */
xer_type_encoder_f OCTET_STRING_encode_xer;
xer_type_encoder_f OCTET_STRING_encode_xer_utf8;
+oer_type_decoder_f OCTET_STRING_decode_oer;
+oer_type_encoder_f OCTET_STRING_encode_oer;
per_type_decoder_f OCTET_STRING_decode_uper;
per_type_encoder_f OCTET_STRING_encode_uper;
diff --git a/skeletons/OCTET_STRING_oer.c b/skeletons/OCTET_STRING_oer.c
new file mode 100644
index 0000000..1b1e234
--- /dev/null
+++ b/skeletons/OCTET_STRING_oer.c
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
+ * All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+#ifndef ASN_DISABLE_OER_SUPPORT
+
+#include <asn_internal.h>
+#include <OCTET_STRING.h>
+#include <errno.h>
+
+static asn_oer_constraints_t asn_DEF_OCTET_STRING_oer_constraints = {
+ { 0, 0, 0 },
+ { AOC_HAS_LOWER_BOUND, 0, 0 }
+};
+
+asn_dec_rval_t
+OCTET_STRING_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
+ asn_TYPE_descriptor_t *td,
+ asn_oer_constraints_t *constraints, void **sptr,
+ const void *ptr, size_t size) {
+ asn_OCTET_STRING_specifics_t *specs =
+ td->specifics
+ ? (asn_OCTET_STRING_specifics_t *)td->specifics
+ : (asn_OCTET_STRING_specifics_t *)&asn_DEF_OCTET_STRING.specifics;
+ OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
+ asn_oer_constraints_t *cts =
+ constraints ? constraints : td->oer_constraints;
+ asn_oer_constraint_t *csiz =
+ cts ? &cts->size : &asn_DEF_OCTET_STRING_oer_constraints.size;
+ asn_dec_rval_t rval = {RC_OK, 0};
+ size_t expected_length = 0;
+
+ size_t unit_bytes;
+ switch(specs->subvariant) {
+ default:
+ case ASN_OSUBV_BIT:
+ ASN_DEBUG("Invalid use of OCTET STRING to decode BIT STRING");
+ ASN__DECODE_FAILED;
+ case ASN_OSUBV_ANY:
+ /* Fall through */
+ case ASN_OSUBV_STR:
+ unit_bytes = 1;
+ break;
+ case ASN_OSUBV_U16:
+ unit_bytes = 2;
+ break;
+ case ASN_OSUBV_U32:
+ unit_bytes = 4;
+ break;
+ }
+
+ (void)opt_codec_ctx;
+
+ if(!st) {
+ st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
+ if(!st) ASN__DECODE_FAILED;
+ }
+
+ if((csiz->flags
+ & (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
+ == (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND)
+ && csiz->lower_bound == csiz->upper_bound) {
+ expected_length = unit_bytes * csiz->lower_bound;
+ } else {
+ /*
+ * X.696 (08/2015) #27.2
+ * Encode length determinant as _number of octets_, but only
+ * if upper bound is not equal to lower bound.
+ */
+ ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
+ if(len_len > 0) {
+ rval.consumed = len_len;
+ ptr = (const char *)ptr + len_len;
+ size -= len_len;
+ } else if(len_len == 0) {
+ ASN__DECODE_STARVED;
+ } else if(len_len < 0) {
+ ASN__DECODE_FAILED;
+ }
+
+ if(expected_length % unit_bytes != 0) {
+ ASN_DEBUG(
+ "Data size %zu bytes is not consistent with multiplier %zu",
+ expected_length, unit_bytes);
+ ASN__DECODE_FAILED;
+ }
+ }
+
+ if(size < expected_length) {
+ ASN__DECODE_STARVED;
+ } else {
+ uint8_t *buf = MALLOC(expected_length + 1);
+ if(buf == NULL) {
+ ASN__DECODE_FAILED;
+ } else {
+ memcpy(buf, ptr, expected_length);
+ buf[expected_length] = '\0';
+ }
+ FREEMEM(st->buf);
+ st->buf = buf;
+ st->size = expected_length;
+
+ rval.consumed += expected_length;
+ return rval;
+ }
+}
+
+/*
+ * Encode as Canonical OER.
+ */
+asn_enc_rval_t
+OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td,
+ asn_oer_constraints_t *constraints, void *sptr,
+ asn_app_consume_bytes_f *cb, void *app_key) {
+ asn_OCTET_STRING_specifics_t *specs =
+ td->specifics
+ ? (asn_OCTET_STRING_specifics_t *)td->specifics
+ : (asn_OCTET_STRING_specifics_t *)&asn_DEF_OCTET_STRING.specifics;
+ OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
+ asn_oer_constraints_t *cts =
+ constraints ? constraints : td->oer_constraints;
+ asn_oer_constraint_t *csiz =
+ cts ? &cts->size : &asn_DEF_OCTET_STRING_oer_constraints.size;
+ asn_enc_rval_t er = {0, 0, 0};
+
+ if(!st) ASN__ENCODE_FAILED;
+
+ ASN_DEBUG("Encoding %s %d as OCTET STRING", td ? td->name : "", st->size);
+
+ if((csiz->flags
+ & (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
+ == (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND)
+ && csiz->lower_bound == csiz->upper_bound) {
+ /*
+ * Check that available data matches the constraint
+ */
+ size_t unit_bytes;
+ switch(specs->subvariant) {
+ default:
+ case ASN_OSUBV_BIT:
+ ASN_DEBUG("Invalid use of OCTET STRING to encode BIT STRING");
+ ASN__ENCODE_FAILED;
+ case ASN_OSUBV_ANY:
+ /* Fall through */
+ case ASN_OSUBV_STR:
+ unit_bytes = 1;
+ break;
+ case ASN_OSUBV_U16:
+ unit_bytes = 2;
+ break;
+ case ASN_OSUBV_U32:
+ unit_bytes = 4;
+ break;
+ }
+
+ if(st->size != unit_bytes * csiz->lower_bound) {
+ ASN_DEBUG(
+ "Trying to encode %s (%zu bytes) which doesn't fit SIZE "
+ "constraint (%d)",
+ td->name, st->size, csiz->lower_bound);
+ ASN__ENCODE_FAILED;
+ }
+ } else {
+ /*
+ * X.696 (08/2015) #27.2
+ * Encode length determinant as _number of octets_, but only
+ * if upper bound is not equal to lower bound.
+ */
+ ssize_t ret = oer_serialize_length(st->size, cb, app_key);
+ if(ret < 0) {
+ ASN__ENCODE_FAILED;
+ }
+ er.encoded += ret;
+ }
+
+ er.encoded += st->size;
+ if(cb(st->buf, st->size, app_key) < 0) {
+ ASN__ENCODE_FAILED;
+ } else {
+ ASN__ENCODED_OK(er);
+ }
+}
+
+#endif /* ASN_DISABLE_OER_SUPPORT */
diff --git a/skeletons/PrintableString.c b/skeletons/PrintableString.c
index d3ff090..4085aed 100644
--- a/skeletons/PrintableString.c
+++ b/skeletons/PrintableString.c
@@ -61,8 +61,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/T61String.c b/skeletons/T61String.c
index 43d1479..afea666 100644
--- a/skeletons/T61String.c
+++ b/skeletons/T61String.c
@@ -26,8 +26,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/TeletexString.c b/skeletons/TeletexString.c
index bbe859e..d1b4070 100644
--- a/skeletons/TeletexString.c
+++ b/skeletons/TeletexString.c
@@ -26,8 +26,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/UTF8String.c b/skeletons/UTF8String.c
index 9a2ce10..330d968 100644
--- a/skeletons/UTF8String.c
+++ b/skeletons/UTF8String.c
@@ -27,8 +27,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/UniversalString.c b/skeletons/UniversalString.c
index ecb31eb..b1e1d3e 100644
--- a/skeletons/UniversalString.c
+++ b/skeletons/UniversalString.c
@@ -37,8 +37,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/VideotexString.c b/skeletons/VideotexString.c
index c9c9949..46e127b 100644
--- a/skeletons/VideotexString.c
+++ b/skeletons/VideotexString.c
@@ -26,8 +26,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/VisibleString.c b/skeletons/VisibleString.c
index 4b8c4cf..2a09b8a 100644
--- a/skeletons/VisibleString.c
+++ b/skeletons/VisibleString.c
@@ -31,8 +31,8 @@
0,
0,
#else
- 0,
- 0,
+ OCTET_STRING_decode_oer,
+ OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
diff --git a/skeletons/file-dependencies b/skeletons/file-dependencies
index cb4b92a..eeba0ff 100644
--- a/skeletons/file-dependencies
+++ b/skeletons/file-dependencies
@@ -69,6 +69,7 @@
CODEC-OER: # THIS IS A SPECIAL SECTION
INTEGER_oer.c
+OCTET_STRING_oer.c
NativeInteger_oer.c
constr_SEQUENCE_oer.c