Commit 4329272a authored by Lev Walkin's avatar Lev Walkin

for OER use IEEE754 binary32 and binary64 format over the wire

parent afcc891c
...@@ -122,21 +122,6 @@ static int compar_enumMap_byValue(const void *ap, const void *bp) { ...@@ -122,21 +122,6 @@ static int compar_enumMap_byValue(const void *ap, const void *bp) {
return 1; return 1;
} }
static int
REAL_fits_float32(arg_t *arg, asn1p_expr_t *expr) {
asn1p_expr_type_e etype = expr_get_type(arg, arg->expr);
if(etype == ASN_BASIC_REAL) {
asn1cnst_range_t *range = asn1constraint_compute_OER_range(
expr->Identifier, etype, expr->combined_constraints, ACT_EL_RANGE,
0, 0, 0);
int fits = range->narrowing == NARROW_FLOAT32;
asn1constraint_range_free(range);
return fits;
} else {
return 0;
}
}
int int
asn1c_lang_C_type_common_INTEGER(arg_t *arg) { asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
asn1p_expr_t *expr = arg->expr; asn1p_expr_t *expr = arg->expr;
...@@ -1313,7 +1298,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { ...@@ -1313,7 +1298,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
&& expr_elements_count(arg, expr)) && expr_elements_count(arg, expr))
|| (expr->expr_type == ASN_BASIC_INTEGER || (expr->expr_type == ASN_BASIC_INTEGER
&& asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN)
|| REAL_fits_float32(arg, expr) || asn1c_REAL_fits(arg, expr) == RL_FITS_FLOAT32
) )
etd_spec = ETD_HAS_SPECIFICS; etd_spec = ETD_HAS_SPECIFICS;
else else
...@@ -1355,7 +1340,11 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { ...@@ -1355,7 +1340,11 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
REDIR(OT_STAT_DEFS); REDIR(OT_STAT_DEFS);
if(REAL_fits_float32(arg, expr)) { /*
* By default, NativeReal is double. We only override this if
* (OER) constraints suggested that we may use float.
*/
if(asn1c_REAL_fits(arg, expr) == RL_FITS_FLOAT32) {
if(!(expr->_type_referenced)) OUT("static "); if(!(expr->_type_referenced)) OUT("static ");
OUT("const asn_NativeReal_specifics_t asn_SPC_%s_specs_%d = {\n", OUT("const asn_NativeReal_specifics_t asn_SPC_%s_specs_%d = {\n",
MKID(expr), expr->_type_unique_index); MKID(expr), expr->_type_unique_index);
...@@ -1875,7 +1864,7 @@ emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range) { ...@@ -1875,7 +1864,7 @@ emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range) {
} else if(expr_get_type(arg, arg->expr) == ASN_BASIC_REAL) { } else if(expr_get_type(arg, arg->expr) == ASN_BASIC_REAL) {
if(range->narrowing == NARROW_FLOAT32) { if(range->narrowing == NARROW_FLOAT32) {
OUT("{ sizeof(float), 0 }"); OUT("{ sizeof(float), 0 }");
} else if(range->narrowing == NARROW_FLOAT64) { } else if(range->narrowing == NARROW_DOUBLE64) {
OUT("{ sizeof(double), 0 }"); OUT("{ sizeof(double), 0 }");
} else { } else {
OUT("{ 0, 0 }"); OUT("{ 0, 0 }");
......
...@@ -80,8 +80,9 @@ asn1c_emit_constraint_checking_code(arg_t *arg) { ...@@ -80,8 +80,9 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
produce_st = 1; produce_st = 1;
break; break;
case ASN_BASIC_REAL: case ASN_BASIC_REAL:
if((arg->flags & A1C_USE_WIDE_TYPES)) if((arg->flags & A1C_USE_WIDE_TYPES)
produce_st = 1; && asn1c_REAL_fits(arg, arg->expr) == RL_NOTFIT)
produce_st = 1;
break; break;
case ASN_BASIC_BIT_STRING: case ASN_BASIC_BIT_STRING:
case ASN_BASIC_OCTET_STRING: case ASN_BASIC_OCTET_STRING:
......
...@@ -235,10 +235,11 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) { ...@@ -235,10 +235,11 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
case ASN_BASIC_INTEGER: case ASN_BASIC_INTEGER:
case ASN_BASIC_ENUMERATED: case ASN_BASIC_ENUMERATED:
case ASN_BASIC_REAL: case ASN_BASIC_REAL:
if((expr->expr_type == ASN_BASIC_REAL if((expr->expr_type == ASN_BASIC_REAL
&& (_format == TNF_CONSTYPE || !(arg->flags & A1C_USE_WIDE_TYPES))) && (_format == TNF_CONSTYPE || !(arg->flags & A1C_USE_WIDE_TYPES)
|| asn1c_type_fits_long(arg, expr)) { || asn1c_REAL_fits(arg, expr) != RL_NOTFIT))
switch(_format) { || asn1c_type_fits_long(arg, expr)) {
switch(_format) {
case TNF_CONSTYPE: case TNF_CONSTYPE:
if(expr->expr_type == ASN_BASIC_REAL) { if(expr->expr_type == ASN_BASIC_REAL) {
return "double"; return "double";
...@@ -328,6 +329,40 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) { ...@@ -328,6 +329,40 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
return typename; return typename;
} }
static asn1p_expr_type_e
expr_get_type(arg_t *arg, asn1p_expr_t *expr) {
asn1p_expr_t *terminal;
terminal = asn1f_find_terminal_type_ex(arg->asn, arg->ns, expr);
if(terminal) return terminal->expr_type;
return A1TC_INVALID;
}
enum asn1c_fitsfloat_e
asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr) {
asn1p_expr_type_e etype = expr_get_type(arg, arg->expr);
if(etype == ASN_BASIC_REAL) {
asn1cnst_range_t *range = asn1constraint_compute_OER_range(
expr->Identifier, etype, expr->combined_constraints, ACT_EL_RANGE,
0, 0, 0);
enum asn1c_fitsfloat_e fits;
switch(range->narrowing) {
case NARROW_FLOAT32:
fits = RL_FITS_FLOAT32;
break;
case NARROW_DOUBLE64:
fits = RL_FITS_DOUBLE64;
break;
default:
fits = RL_NOTFIT;
break;
}
asn1constraint_range_free(range);
return fits;
} else {
return 0;
}
}
/* /*
* Check whether the specified INTEGER or ENUMERATED type can be represented * Check whether the specified INTEGER or ENUMERATED type can be represented
* using the generic 'long' or 'unsigned long' type. * using the generic 'long' or 'unsigned long' type.
......
...@@ -46,4 +46,11 @@ enum asn1c_fitslong_e { ...@@ -46,4 +46,11 @@ enum asn1c_fitslong_e {
}; };
enum asn1c_fitslong_e asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr); enum asn1c_fitslong_e asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr);
enum asn1c_fitsfloat_e {
RL_NOTFIT,
RL_FITS_FLOAT32,
RL_FITS_DOUBLE64
};
enum asn1c_fitsfloat_e asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr);
#endif /* ASN1_COMPILER_MISC_H */ #endif /* ASN1_COMPILER_MISC_H */
...@@ -943,7 +943,7 @@ asn1f_real_range_from_WCOMPS(const char *dbg_name, ...@@ -943,7 +943,7 @@ asn1f_real_range_from_WCOMPS(const char *dbg_name,
&& base->left.value == 2 && base->right.value == 2 && base->left.value == 2 && base->right.value == 2
&& exponent->left.type == ARE_VALUE && exponent->right.type == ARE_VALUE && exponent->left.type == ARE_VALUE && exponent->right.type == ARE_VALUE
&& exponent->left.value >= -1074 && exponent->right.value <= 971) { && exponent->left.value >= -1074 && exponent->right.value <= 971) {
range->narrowing = NARROW_FLOAT64; range->narrowing = NARROW_DOUBLE64;
} }
FREE_MEB(); FREE_MEB();
......
...@@ -16,9 +16,9 @@ typedef struct asn1cnst_range_s { ...@@ -16,9 +16,9 @@ typedef struct asn1cnst_range_s {
asn1cnst_edge_t right; /* 10 from (MIN..10) */ asn1cnst_edge_t right; /* 10 from (MIN..10) */
enum asn1cnst_range_narrowing { enum asn1cnst_range_narrowing {
/* Sorted from softest to hardest narrowing */ /* Sorted from softest to strictest narrowing */
NOT_NARROW, NOT_NARROW,
NARROW_FLOAT64, NARROW_DOUBLE64,
NARROW_FLOAT32, NARROW_FLOAT32,
} narrowing; /* Constrained to a known narrow type */ } narrowing; /* Constrained to a known narrow type */
......
...@@ -558,6 +558,7 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -558,6 +558,7 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
-1267650600228229401496703205376.0, 1267650600228229401496703205376.0, -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
#if __STDC_VERSION__ >= 199901L #if __STDC_VERSION__ >= 199901L
-FLT_MAX, FLT_MAX, -FLT_MAX, FLT_MAX,
-DBL_TRUE_MIN, DBL_TRUE_MIN,
#endif #endif
INFINITY, -INFINITY, NAN}; INFINITY, -INFINITY, NAN};
ssize_t float_set_size; ssize_t float_set_size;
......
...@@ -79,8 +79,8 @@ asn_TYPE_operation_t asn_OP_REAL = { ...@@ -79,8 +79,8 @@ asn_TYPE_operation_t asn_OP_REAL = {
0, 0,
0, 0,
#else #else
0, REAL_decode_oer,
0, REAL_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */ #endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT #ifdef ASN_DISABLE_PER_SUPPORT
0, 0,
...@@ -832,6 +832,90 @@ asn_double2REAL(REAL_t *st, double dbl_value) { ...@@ -832,6 +832,90 @@ asn_double2REAL(REAL_t *st, double dbl_value) {
return 0; return 0;
} }
#ifndef ASN_DISABLE_OER_SUPPORT
/*
* Encode as Canonical OER
*/
asn_enc_rval_t
REAL_encode_oer(asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
const REAL_t *st = sptr;
asn_enc_rval_t er;
ssize_t len_len;
if(!st || !st->buf || !td)
ASN__ENCODE_FAILED;
if(!constraints) constraints = td->encoding_constraints.oer_constraints;
if(constraints && constraints->value.width != 0) {
/* If we're constrained to a narrow float/double representation, we
* shouldn't have ended up using REAL. Expecting NativeReal. */
ASN__ENCODE_FAILED;
}
/* Encode a fake REAL */
len_len = oer_serialize_length(st->size, cb, app_key);
if(len_len < 0 || cb(st->buf, st->size, app_key) < 0) {
ASN__ENCODE_FAILED;
} else {
er.encoded = len_len + st->size;
ASN__ENCODED_OK(er);
}
}
asn_dec_rval_t
REAL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void **sptr,
const void *ptr, size_t size) {
asn_dec_rval_t ok = {RC_OK, 0};
REAL_t *st;
uint8_t *buf;
ssize_t len_len;
size_t real_body_len;
if(!constraints) constraints = td->encoding_constraints.oer_constraints;
if(constraints && constraints->value.width != 0) {
/* If we're constrained to a narrow float/double representation, we
* shouldn't have ended up using REAL. Expecting NativeReal. */
ASN__DECODE_FAILED;
}
len_len = oer_fetch_length(ptr, size, &real_body_len);
if(len_len < 0) ASN__DECODE_FAILED;
if(len_len == 0) ASN__DECODE_STARVED;
ptr = (const char *)ptr + len_len;
size -= len_len;
if(real_body_len > size) ASN__DECODE_STARVED;
buf = CALLOC(1, real_body_len + 1);
if(!buf) ASN__DECODE_FAILED;
if(!(st = *sptr)) {
st = (*sptr = CALLOC(1, sizeof(REAL_t)));
if(!st) {
FREEMEM(buf);
ASN__DECODE_FAILED;
}
} else {
FREEMEM(st->buf);
}
memcpy(buf, ptr, real_body_len);
buf[real_body_len] = '\0';
st->buf = buf;
st->size = real_body_len;
ok.consumed = len_len + real_body_len;
return ok;
}
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifndef ASN_DISABLE_PER_SUPPORT #ifndef ASN_DISABLE_PER_SUPPORT
asn_dec_rval_t asn_dec_rval_t
...@@ -870,6 +954,7 @@ REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -870,6 +954,7 @@ REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
-1267650600228229401496703205376.0, 1267650600228229401496703205376.0, -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
#if __STDC_VERSION__ >= 199901L #if __STDC_VERSION__ >= 199901L
-FLT_MAX, FLT_MAX, -FLT_MAX, FLT_MAX,
-DBL_TRUE_MIN, DBL_TRUE_MIN,
#endif #endif
INFINITY, -INFINITY, NAN}; INFINITY, -INFINITY, NAN};
REAL_t *st; REAL_t *st;
......
...@@ -19,10 +19,12 @@ extern asn_TYPE_operation_t asn_OP_REAL; ...@@ -19,10 +19,12 @@ extern asn_TYPE_operation_t asn_OP_REAL;
asn_struct_print_f REAL_print; asn_struct_print_f REAL_print;
asn_struct_compare_f REAL_compare; asn_struct_compare_f REAL_compare;
xer_type_decoder_f REAL_decode_xer; oer_type_decoder_f REAL_decode_oer;
xer_type_encoder_f REAL_encode_xer; oer_type_encoder_f REAL_encode_oer;
per_type_decoder_f REAL_decode_uper; per_type_decoder_f REAL_decode_uper;
per_type_encoder_f REAL_encode_uper; per_type_encoder_f REAL_encode_uper;
xer_type_decoder_f REAL_decode_xer;
xer_type_encoder_f REAL_encode_xer;
asn_random_fill_f REAL_random_fill; asn_random_fill_f REAL_random_fill;
#define REAL_free ASN__PRIMITIVE_TYPE_free, #define REAL_free ASN__PRIMITIVE_TYPE_free,
......
...@@ -14,10 +14,11 @@ usage() { ...@@ -14,10 +14,11 @@ usage() {
echo " $0 [--dirty] -t \"<ASN.1 text defining type T, in string form>\"" echo " $0 [--dirty] -t \"<ASN.1 text defining type T, in string form>\""
echo " $0 [--dirty] bundles/<bundle-name.txt> [<line>]" echo " $0 [--dirty] bundles/<bundle-name.txt> [<line>]"
echo "Where options are:" echo "Where options are:"
echo " -h Show this help screen" echo " -h Show this help screen"
echo " -e <syntax> Verify a given encoding explicitly (default is ALL)" echo " -e <syntax> Verify a given encoding explicitly (default is ALL)"
echo " --dirty Reuse compile results from the previous run(s)" echo " --asn1c <flag> Add this flag to asn1c"
echo " -t <ASN.1> Run this particular typel" echo " --dirty Reuse compile results from the previous run(s)"
echo " -t <ASN.1> Run this particular typel"
echo "Examples:" echo "Examples:"
echo " $0 -t UTF8String" echo " $0 -t UTF8String"
echo " $0 -t \"T ::= INTEGER (0..1)\"" echo " $0 -t \"T ::= INTEGER (0..1)\""
...@@ -38,6 +39,7 @@ need_clean_before_bundle=1 # Clean before testing a bundle file ...@@ -38,6 +39,7 @@ need_clean_before_bundle=1 # Clean before testing a bundle file
need_clean_before_test=1 # Before each line in a bundle file need_clean_before_test=1 # Before each line in a bundle file
encodings="" # Default is to verify all supported ASN.1 transfer syntaxes encodings="" # Default is to verify all supported ASN.1 transfer syntaxes
parallelism=4 parallelism=4
asn1c_flags=""
make_clean_before_bundle() { make_clean_before_bundle() {
test "${need_clean_before_bundle}" = "1" && make clean test "${need_clean_before_bundle}" = "1" && make clean
...@@ -81,8 +83,6 @@ verify_asn_type() { ...@@ -81,8 +83,6 @@ verify_asn_type() {
local where="$*" local where="$*"
test "x$asn" != "x" || usage test "x$asn" != "x" || usage
make_clean_before_test
if echo "$asn" | grep -qv "::="; then if echo "$asn" | grep -qv "::="; then
asn="T ::= $asn" asn="T ::= $asn"
fi fi
...@@ -98,11 +98,35 @@ verify_asn_type() { ...@@ -98,11 +98,35 @@ verify_asn_type() {
fi fi
} }
# compile_and_test "<text>" "<where found>" [<asn.1 flags>]
compile_and_test() { compile_and_test() {
if [ "x${asn1c_flags}" != "x" ]; then
if ! compile_and_test_with "$@" ${asn1c_flags} ; then
return 1
fi
else
if ! compile_and_test_with "$@" ; then
echo "Can't compile and test narrow"
return 1
fi
if ! compile_and_test_with "$@" "-fwide-types"; then
echo "Can't compile and test wide"
return 2
fi
fi
return 0
}
compile_and_test_with() {
local asn="$1" local asn="$1"
shift local where="$2"
shift 2
make_clean_before_test
if ! asn_compile "$asn" "$*"; then if ! asn_compile "$asn" "$where" "$@"; then
echo "Cannot compile ASN.1 $asn" echo "Cannot compile ASN.1 $asn"
return 1 return 1
fi fi
...@@ -170,7 +194,8 @@ compile_and_test() { ...@@ -170,7 +194,8 @@ compile_and_test() {
asn_compile() { asn_compile() {
local asn="$1" local asn="$1"
shift local where="$2"
shift 2
# Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5" # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5"
local short_asn=$(echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;') local short_asn=$(echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;')
...@@ -180,10 +205,10 @@ asn_compile() { ...@@ -180,10 +205,10 @@ asn_compile() {
test ! -f Makefile.am # Protection from accidental clobbering test ! -f Makefile.am # Protection from accidental clobbering
echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1 echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1
echo "-- $*" >> test.asn1 echo "-- ${where}" >> test.asn1
echo "END" >> test.asn1 echo "END" >> test.asn1
if ! ${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons \ if ! ${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons \
-gen-OER -gen-PER test.asn1 -gen-OER -gen-PER "$@" test.asn1
then then
return 1 return 1
fi fi
...@@ -199,6 +224,7 @@ asn_compile() { ...@@ -199,6 +224,7 @@ asn_compile() {
while :; do while :; do
case "$1" in case "$1" in
-h) usage ;; -h) usage ;;
--asn1c) asn1c_flags+="$2"; shift 2; continue ;;
--dirty) --dirty)
need_clean_before_bundle=0 need_clean_before_bundle=0
need_clean_before_test=0 need_clean_before_test=0
......
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