From 571b7d1991b8ea5cc64a76f38940d5ac9b4b84e2 Mon Sep 17 00:00:00 2001 From: Balaji Shankaran Date: Tue, 24 Mar 2020 13:56:08 +0530 Subject: [PATCH] E2AP review changes part1 Change-Id: Ia4b066346fe2450da7ea5832c72e6825c4a9ff1d Signed-off-by: Balaji Shankaran --- build/common/e2ap.mak | 2 +- build/common/ric_stub.mak | 2 +- docs/README | 2 +- src/codec_utils/E2AP/INTEGER.c | 1735 -------------------------------- src/codec_utils/E2AP/INTEGER.h | 108 -- src/codec_utils/E2AP/INTEGER_oer.c | 179 ---- src/codec_utils/E2AP/OPEN_TYPE.c | 509 ---------- src/codec_utils/E2AP/OPEN_TYPE.h | 77 -- src/codec_utils/E2AP/OPEN_TYPE_oer.c | 92 -- src/codec_utils/E2AP/asn_SEQUENCE_OF.c | 41 - src/codec_utils/E2AP/asn_SEQUENCE_OF.h | 52 - src/codec_utils/E2AP/asn_SET_OF.c | 88 -- src/codec_utils/E2AP/asn_SET_OF.h | 72 -- src/codec_utils/E2AP/asn_application.c | 481 --------- src/codec_utils/E2AP/asn_application.h | 171 ---- src/codec_utils/E2AP/asn_bit_data.c | 333 ------ src/codec_utils/E2AP/asn_bit_data.h | 83 -- src/codec_utils/E2AP/asn_codecs.h | 108 -- src/codec_utils/E2AP/asn_codecs_prim.c | 317 ------ src/codec_utils/E2AP/asn_codecs_prim.h | 51 - src/codec_utils/E2AP/asn_internal.c | 48 - src/codec_utils/E2AP/asn_internal.h | 165 --- src/codec_utils/E2AP/asn_ioc.h | 51 - src/codec_utils/E2AP/asn_random_fill.c | 56 -- src/codec_utils/E2AP/asn_random_fill.h | 51 - src/codec_utils/E2AP/asn_system.h | 150 --- src/codec_utils/E2AP/ber_decoder.c | 283 ------ src/codec_utils/E2AP/ber_decoder.h | 66 -- src/codec_utils/E2AP/ber_tlv_length.c | 168 ---- src/codec_utils/E2AP/ber_tlv_length.h | 50 - src/codec_utils/E2AP/ber_tlv_tag.c | 144 --- src/codec_utils/E2AP/ber_tlv_tag.h | 60 -- src/codec_utils/E2AP/der_encoder.c | 194 ---- src/codec_utils/E2AP/der_encoder.h | 68 -- src/codec_utils/E2AP/oer_decoder.c | 152 --- src/codec_utils/E2AP/oer_decoder.h | 72 -- src/codec_utils/E2AP/oer_encoder.c | 141 --- src/codec_utils/E2AP/oer_encoder.h | 70 -- src/codec_utils/E2AP/oer_support.c | 122 --- src/codec_utils/E2AP/oer_support.h | 47 - src/codec_utils/E2AP/per_decoder.c | 185 ---- src/codec_utils/E2AP/per_decoder.h | 82 -- src/codec_utils/E2AP/per_opentype.c | 533 ---------- src/codec_utils/E2AP/per_opentype.h | 44 - src/codec_utils/E2AP/per_support.c | 489 --------- src/codec_utils/E2AP/per_support.h | 127 --- src/cu_stub/cu_stub_sctp.h | 1 + src/du_app/du_cell_mgr.c | 141 --- src/du_app/du_cfg.c | 20 +- src/du_app/du_cfg.h | 7 - src/du_app/du_e2ap_msg_hdl.c | 195 +++- src/du_app/du_e2ap_msg_hdl.h | 3 +- src/du_app/du_f1ap_msg_hdl.c | 2 +- src/du_app/du_mgr.h | 4 +- src/du_app/du_msg_hdl.c | 40 +- src/du_app/du_sctp.c | 66 +- src/du_app/du_sctp.h | 5 +- 57 files changed, 231 insertions(+), 8374 deletions(-) delete mode 100644 src/codec_utils/E2AP/INTEGER.c delete mode 100644 src/codec_utils/E2AP/INTEGER.h delete mode 100644 src/codec_utils/E2AP/INTEGER_oer.c delete mode 100644 src/codec_utils/E2AP/OPEN_TYPE.c delete mode 100644 src/codec_utils/E2AP/OPEN_TYPE.h delete mode 100644 src/codec_utils/E2AP/OPEN_TYPE_oer.c delete mode 100644 src/codec_utils/E2AP/asn_SEQUENCE_OF.c delete mode 100644 src/codec_utils/E2AP/asn_SEQUENCE_OF.h delete mode 100644 src/codec_utils/E2AP/asn_SET_OF.c delete mode 100644 src/codec_utils/E2AP/asn_SET_OF.h delete mode 100644 src/codec_utils/E2AP/asn_application.c delete mode 100644 src/codec_utils/E2AP/asn_application.h delete mode 100644 src/codec_utils/E2AP/asn_bit_data.c delete mode 100644 src/codec_utils/E2AP/asn_bit_data.h delete mode 100644 src/codec_utils/E2AP/asn_codecs.h delete mode 100644 src/codec_utils/E2AP/asn_codecs_prim.c delete mode 100644 src/codec_utils/E2AP/asn_codecs_prim.h delete mode 100644 src/codec_utils/E2AP/asn_internal.c delete mode 100644 src/codec_utils/E2AP/asn_internal.h delete mode 100644 src/codec_utils/E2AP/asn_ioc.h delete mode 100644 src/codec_utils/E2AP/asn_random_fill.c delete mode 100644 src/codec_utils/E2AP/asn_random_fill.h delete mode 100644 src/codec_utils/E2AP/asn_system.h delete mode 100644 src/codec_utils/E2AP/ber_decoder.c delete mode 100644 src/codec_utils/E2AP/ber_decoder.h delete mode 100644 src/codec_utils/E2AP/ber_tlv_length.c delete mode 100644 src/codec_utils/E2AP/ber_tlv_length.h delete mode 100644 src/codec_utils/E2AP/ber_tlv_tag.c delete mode 100644 src/codec_utils/E2AP/ber_tlv_tag.h delete mode 100644 src/codec_utils/E2AP/der_encoder.c delete mode 100644 src/codec_utils/E2AP/der_encoder.h delete mode 100644 src/codec_utils/E2AP/oer_decoder.c delete mode 100644 src/codec_utils/E2AP/oer_decoder.h delete mode 100644 src/codec_utils/E2AP/oer_encoder.c delete mode 100644 src/codec_utils/E2AP/oer_encoder.h delete mode 100644 src/codec_utils/E2AP/oer_support.c delete mode 100644 src/codec_utils/E2AP/oer_support.h delete mode 100644 src/codec_utils/E2AP/per_decoder.c delete mode 100644 src/codec_utils/E2AP/per_decoder.h delete mode 100644 src/codec_utils/E2AP/per_opentype.c delete mode 100644 src/codec_utils/E2AP/per_opentype.h delete mode 100644 src/codec_utils/E2AP/per_support.c delete mode 100644 src/codec_utils/E2AP/per_support.h diff --git a/build/common/e2ap.mak b/build/common/e2ap.mak index 22bf55b33..967d9b80f 100644 --- a/build/common/e2ap.mak +++ b/build/common/e2ap.mak @@ -14,7 +14,7 @@ # limitations under the License. # ################################################################################ -# This is makefile for DU APP +# This is makefile for E2 APP include ../common/rsys_fancy.mak include ../common/env.mak diff --git a/build/common/ric_stub.mak b/build/common/ric_stub.mak index a5c138bf2..6d4348105 100644 --- a/build/common/ric_stub.mak +++ b/build/common/ric_stub.mak @@ -14,7 +14,7 @@ # limitations under the License. # ################################################################################ -# This is makefile for CU STUB +# This is makefile for RIC STUB include ../common/rsys_fancy.mak include ../common/env.mak diff --git a/docs/README b/docs/README index 49037b96a..f47fee2d9 100644 --- a/docs/README +++ b/docs/README @@ -78,4 +78,4 @@ How to execute: 5. Run ODU binary: ./odu -PS: CU stub and RIC stub must be run first +PS: CU stub and RIC stub must be run (in no particular sequence) before ODU diff --git a/src/codec_utils/E2AP/INTEGER.c b/src/codec_utils/E2AP/INTEGER.c deleted file mode 100644 index 2a2f4d720..000000000 --- a/src/codec_utils/E2AP/INTEGER.c +++ /dev/null @@ -1,1735 +0,0 @@ -/*- - * Copyright (c) 2003-2014 Lev Walkin . - * All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include /* Encoder and decoder of a primitive type */ -#include - -/* - * INTEGER basic type description. - */ -static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = { - (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) -}; -asn_TYPE_operation_t asn_OP_INTEGER = { - INTEGER_free, - INTEGER_print, - INTEGER_compare, - ber_decode_primitive, - INTEGER_encode_der, - INTEGER_decode_xer, - INTEGER_encode_xer, -#ifdef ASN_DISABLE_OER_SUPPORT - 0, - 0, -#else - INTEGER_decode_oer, /* OER decoder */ - INTEGER_encode_oer, /* Canonical OER encoder */ -#endif /* ASN_DISABLE_OER_SUPPORT */ -#ifdef ASN_DISABLE_PER_SUPPORT - 0, - 0, - 0, - 0, -#else - INTEGER_decode_uper, /* Unaligned PER decoder */ - INTEGER_encode_uper, /* Unaligned PER encoder */ - INTEGER_decode_aper, /* Aligned PER decoder */ - INTEGER_encode_aper, /* Aligned PER encoder */ -#endif /* ASN_DISABLE_PER_SUPPORT */ - INTEGER_random_fill, - 0 /* Use generic outmost tag fetcher */ -}; -asn_TYPE_descriptor_t asn_DEF_INTEGER = { - "INTEGER", - "INTEGER", - &asn_OP_INTEGER, - asn_DEF_INTEGER_tags, - sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), - asn_DEF_INTEGER_tags, /* Same as above */ - sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), - { 0, 0, asn_generic_no_constraint }, - 0, 0, /* No members */ - 0 /* No specifics */ -}; - -/* - * Encode INTEGER type using DER. - */ -asn_enc_rval_t -INTEGER_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, - int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, - void *app_key) { - const INTEGER_t *st = (const INTEGER_t *)sptr; - asn_enc_rval_t rval; - INTEGER_t effective_integer; - - ASN_DEBUG("%s %s as INTEGER (tm=%d)", - cb?"Encoding":"Estimating", td->name, tag_mode); - - /* - * Canonicalize integer in the buffer. - * (Remove too long sign extension, remove some first 0x00 bytes) - */ - if(st->buf) { - uint8_t *buf = st->buf; - uint8_t *end1 = buf + st->size - 1; - int shift; - - /* Compute the number of superfluous leading bytes */ - for(; buf < end1; buf++) { - /* - * If the contents octets of an integer value encoding - * consist of more than one octet, then the bits of the - * first octet and bit 8 of the second octet: - * a) shall not all be ones; and - * b) shall not all be zero. - */ - switch(*buf) { - case 0x00: if((buf[1] & 0x80) == 0) - continue; - break; - case 0xff: if((buf[1] & 0x80)) - continue; - break; - } - break; - } - - /* Remove leading superfluous bytes from the integer */ - shift = buf - st->buf; - if(shift) { - union { - const uint8_t *c_buf; - uint8_t *nc_buf; - } unconst; - unconst.c_buf = st->buf; - effective_integer.buf = unconst.nc_buf + shift; - effective_integer.size = st->size - shift; - - st = &effective_integer; - } - } - - rval = der_encode_primitive(td, st, tag_mode, tag, cb, app_key); - if(rval.structure_ptr == &effective_integer) { - rval.structure_ptr = sptr; - } - return rval; -} - -static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value( - const asn_INTEGER_specifics_t *specs, const char *lstart, - const char *lstop); - -/* - * INTEGER specific human-readable output. - */ -static ssize_t -INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - char scratch[32]; - uint8_t *buf = st->buf; - uint8_t *buf_end = st->buf + st->size; - intmax_t value; - ssize_t wrote = 0; - char *p; - int ret; - - if(specs && specs->field_unsigned) - ret = asn_INTEGER2umax(st, (uintmax_t *)&value); - else - ret = asn_INTEGER2imax(st, &value); - - /* Simple case: the integer size is small */ - if(ret == 0) { - const asn_INTEGER_enum_map_t *el; - el = (value >= 0 || !specs || !specs->field_unsigned) - ? INTEGER_map_value2enum(specs, value) : 0; - if(el) { - if(plainOrXER == 0) - return asn__format_to_callback(cb, app_key, - "%" ASN_PRIdMAX " (%s)", value, el->enum_name); - else - return asn__format_to_callback(cb, app_key, - "<%s/>", el->enum_name); - } else if(plainOrXER && specs && specs->strict_enumeration) { - ASN_DEBUG("ASN.1 forbids dealing with " - "unknown value of ENUMERATED type"); - errno = EPERM; - return -1; - } else { - return asn__format_to_callback(cb, app_key, - (specs && specs->field_unsigned) - ? "%" ASN_PRIuMAX - : "%" ASN_PRIdMAX, - value); - } - } else if(plainOrXER && specs && specs->strict_enumeration) { - /* - * Here and earlier, we cannot encode the ENUMERATED values - * if there is no corresponding identifier. - */ - ASN_DEBUG("ASN.1 forbids dealing with " - "unknown value of ENUMERATED type"); - errno = EPERM; - return -1; - } - - /* Output in the long xx:yy:zz... format */ - /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ - for(p = scratch; buf < buf_end; buf++) { - const char * const h2c = "0123456789ABCDEF"; - if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { - /* Flush buffer */ - if(cb(scratch, p - scratch, app_key) < 0) - return -1; - wrote += p - scratch; - p = scratch; - } - *p++ = h2c[*buf >> 4]; - *p++ = h2c[*buf & 0x0F]; - *p++ = 0x3a; /* ":" */ - } - if(p != scratch) - p--; /* Remove the last ":" */ - - wrote += p - scratch; - return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; -} - -/* - * INTEGER specific human-readable output. - */ -int -INTEGER_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, - asn_app_consume_bytes_f *cb, void *app_key) { - const INTEGER_t *st = (const INTEGER_t *)sptr; - ssize_t ret; - - (void)ilevel; - - if(!st || !st->buf) - ret = cb("", 8, app_key); - else - ret = INTEGER__dump(td, st, cb, app_key, 0); - - return (ret < 0) ? -1 : 0; -} - -struct e2v_key { - const char *start; - const char *stop; - const asn_INTEGER_enum_map_t *vemap; - const unsigned int *evmap; -}; -static int -INTEGER__compar_enum2value(const void *kp, const void *am) { - const struct e2v_key *key = (const struct e2v_key *)kp; - const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; - const char *ptr, *end, *name; - - /* Remap the element (sort by different criterion) */ - el = key->vemap + key->evmap[el - key->vemap]; - - /* Compare strings */ - for(ptr = key->start, end = key->stop, name = el->enum_name; - ptr < end; ptr++, name++) { - if(*ptr != *name || !*name) - return *(const unsigned char *)ptr - - *(const unsigned char *)name; - } - return name[0] ? -1 : 0; -} - -static const asn_INTEGER_enum_map_t * -INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart, - const char *lstop) { - const asn_INTEGER_enum_map_t *el_found; - int count = specs ? specs->map_count : 0; - struct e2v_key key; - const char *lp; - - if(!count) return NULL; - - /* Guaranteed: assert(lstart < lstop); */ - /* Figure out the tag name */ - for(lstart++, lp = lstart; lp < lstop; lp++) { - switch(*lp) { - case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */ - case 0x2f: /* '/' */ case 0x3e: /* '>' */ - break; - default: - continue; - } - break; - } - if(lp == lstop) return NULL; /* No tag found */ - lstop = lp; - - key.start = lstart; - key.stop = lstop; - key.vemap = specs->value2enum; - key.evmap = specs->enum2value; - el_found = (asn_INTEGER_enum_map_t *)bsearch(&key, - specs->value2enum, count, sizeof(specs->value2enum[0]), - INTEGER__compar_enum2value); - if(el_found) { - /* Remap enum2value into value2enum */ - el_found = key.vemap + key.evmap[el_found - key.vemap]; - } - return el_found; -} - -static int -INTEGER__compar_value2enum(const void *kp, const void *am) { - long a = *(const long *)kp; - const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; - long b = el->nat_value; - if(a < b) return -1; - else if(a == b) return 0; - else return 1; -} - -const asn_INTEGER_enum_map_t * -INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) { - int count = specs ? specs->map_count : 0; - if(!count) return 0; - return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum, - count, sizeof(specs->value2enum[0]), - INTEGER__compar_value2enum); -} - -static int -INTEGER_st_prealloc(INTEGER_t *st, int min_size) { - void *p = MALLOC(min_size + 1); - if(p) { - void *b = st->buf; - st->size = 0; - st->buf = p; - FREEMEM(b); - return 0; - } else { - return -1; - } -} - -/* - * Decode the chunk of XML text encoding INTEGER. - */ -static enum xer_pbd_rval -INTEGER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr, - const void *chunk_buf, size_t chunk_size) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - INTEGER_t *st = (INTEGER_t *)sptr; - intmax_t dec_value; - intmax_t hex_value = 0; - const char *lp; - const char *lstart = (const char *)chunk_buf; - const char *lstop = lstart + chunk_size; - enum { - ST_LEADSPACE, - ST_SKIPSPHEX, - ST_WAITDIGITS, - ST_DIGITS, - ST_DIGITS_TRAILSPACE, - ST_HEXDIGIT1, - ST_HEXDIGIT2, - ST_HEXDIGITS_TRAILSPACE, - ST_HEXCOLON, - ST_END_ENUM, - ST_UNEXPECTED - } state = ST_LEADSPACE; - const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */ - const char *dec_value_end = 0; - - if(chunk_size) - ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x", - (long)chunk_size, *lstart, lstop[-1]); - - if(INTEGER_st_prealloc(st, (chunk_size/3) + 1)) - return XPBD_SYSTEM_FAILURE; - - /* - * We may have received a tag here. It will be processed inline. - * Use strtoul()-like code and serialize the result. - */ - for(lp = lstart; lp < lstop; lp++) { - int lv = *lp; - switch(lv) { - case 0x09: case 0x0a: case 0x0d: case 0x20: - switch(state) { - case ST_LEADSPACE: - case ST_DIGITS_TRAILSPACE: - case ST_HEXDIGITS_TRAILSPACE: - case ST_SKIPSPHEX: - continue; - case ST_DIGITS: - dec_value_end = lp; - state = ST_DIGITS_TRAILSPACE; - continue; - case ST_HEXCOLON: - state = ST_HEXDIGITS_TRAILSPACE; - continue; - default: - break; - } - break; - case 0x2d: /* '-' */ - if(state == ST_LEADSPACE) { - dec_value = 0; - dec_value_start = lp; - state = ST_WAITDIGITS; - continue; - } - break; - case 0x2b: /* '+' */ - if(state == ST_LEADSPACE) { - dec_value = 0; - dec_value_start = lp; - state = ST_WAITDIGITS; - continue; - } - break; - case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: - case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: - switch(state) { - case ST_DIGITS: continue; - case ST_SKIPSPHEX: /* Fall through */ - case ST_HEXDIGIT1: - hex_value = (lv - 0x30) << 4; - state = ST_HEXDIGIT2; - continue; - case ST_HEXDIGIT2: - hex_value += (lv - 0x30); - state = ST_HEXCOLON; - st->buf[st->size++] = (uint8_t)hex_value; - continue; - case ST_HEXCOLON: - return XPBD_BROKEN_ENCODING; - case ST_LEADSPACE: - dec_value = 0; - dec_value_start = lp; - /* FALL THROUGH */ - case ST_WAITDIGITS: - state = ST_DIGITS; - continue; - default: - break; - } - break; - case 0x3c: /* '<', start of XML encoded enumeration */ - if(state == ST_LEADSPACE) { - const asn_INTEGER_enum_map_t *el; - el = INTEGER_map_enum2value( - (const asn_INTEGER_specifics_t *) - td->specifics, lstart, lstop); - if(el) { - ASN_DEBUG("Found \"%s\" => %ld", - el->enum_name, el->nat_value); - dec_value = el->nat_value; - state = ST_END_ENUM; - lp = lstop - 1; - continue; - } - ASN_DEBUG("Unknown identifier for INTEGER"); - } - return XPBD_BROKEN_ENCODING; - case 0x3a: /* ':' */ - if(state == ST_HEXCOLON) { - /* This colon is expected */ - state = ST_HEXDIGIT1; - continue; - } else if(state == ST_DIGITS) { - /* The colon here means that we have - * decoded the first two hexadecimal - * places as a decimal value. - * Switch decoding mode. */ - ASN_DEBUG("INTEGER re-evaluate as hex form"); - state = ST_SKIPSPHEX; - dec_value_start = 0; - lp = lstart - 1; - continue; - } else { - ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart)); - break; - } - /* [A-Fa-f] */ - case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46: - case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66: - switch(state) { - case ST_SKIPSPHEX: - case ST_LEADSPACE: /* Fall through */ - case ST_HEXDIGIT1: - hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61); - hex_value += 10; - hex_value <<= 4; - state = ST_HEXDIGIT2; - continue; - case ST_HEXDIGIT2: - hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61); - hex_value += 10; - st->buf[st->size++] = (uint8_t)hex_value; - state = ST_HEXCOLON; - continue; - case ST_DIGITS: - ASN_DEBUG("INTEGER re-evaluate as hex form"); - state = ST_SKIPSPHEX; - dec_value_start = 0; - lp = lstart - 1; - continue; - default: - break; - } - break; - } - - /* Found extra non-numeric stuff */ - ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld", - lv, (long)(lp - lstart)); - state = ST_UNEXPECTED; - break; - } - - switch(state) { - case ST_END_ENUM: - /* Got a complete and valid enumeration encoded as a tag. */ - break; - case ST_DIGITS: - dec_value_end = lstop; - /* FALL THROUGH */ - case ST_DIGITS_TRAILSPACE: - /* The last symbol encountered was a digit. */ - switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) { - case ASN_STRTOX_OK: - if(specs && specs->field_unsigned && (uintmax_t) dec_value <= ULONG_MAX) { - break; - } else if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) { - break; - } else { - /* - * We model INTEGER on long for XER, - * to avoid rewriting all the tests at once. - */ - ASN_DEBUG("INTEGER exceeds long range"); - } - /* Fall through */ - case ASN_STRTOX_ERROR_RANGE: - ASN_DEBUG("INTEGER decode %s hit range limit", td->name); - return XPBD_DECODER_LIMIT; - case ASN_STRTOX_ERROR_INVAL: - case ASN_STRTOX_EXPECT_MORE: - case ASN_STRTOX_EXTRA_DATA: - return XPBD_BROKEN_ENCODING; - } - break; - case ST_HEXCOLON: - case ST_HEXDIGITS_TRAILSPACE: - st->buf[st->size] = 0; /* Just in case termination */ - return XPBD_BODY_CONSUMED; - case ST_HEXDIGIT1: - case ST_HEXDIGIT2: - case ST_SKIPSPHEX: - return XPBD_BROKEN_ENCODING; - case ST_LEADSPACE: - /* Content not found */ - return XPBD_NOT_BODY_IGNORE; - case ST_WAITDIGITS: - case ST_UNEXPECTED: - ASN_DEBUG("INTEGER: No useful digits (state %d)", state); - return XPBD_BROKEN_ENCODING; /* No digits */ - } - - /* - * Convert the result of parsing of enumeration or a straight - * decimal value into a BER representation. - */ - if(asn_imax2INTEGER(st, dec_value)) { - ASN_DEBUG("INTEGER decode %s conversion failed", td->name); - return XPBD_SYSTEM_FAILURE; - } - - return XPBD_BODY_CONSUMED; -} - -asn_dec_rval_t -INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, - const char *opt_mname, const void *buf_ptr, size_t size) { - return xer_decode_primitive(opt_codec_ctx, td, - sptr, sizeof(INTEGER_t), opt_mname, - buf_ptr, size, INTEGER__xer_body_decode); -} - -asn_enc_rval_t -INTEGER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, - int ilevel, enum xer_encoder_flags_e flags, - asn_app_consume_bytes_f *cb, void *app_key) { - const INTEGER_t *st = (const INTEGER_t *)sptr; - asn_enc_rval_t er = {0,0,0}; - - (void)ilevel; - (void)flags; - - if(!st || !st->buf) - ASN__ENCODE_FAILED; - - er.encoded = INTEGER__dump(td, st, cb, app_key, 1); - if(er.encoded < 0) ASN__ENCODE_FAILED; - - ASN__ENCODED_OK(er); -} - -#ifndef ASN_DISABLE_PER_SUPPORT - -asn_dec_rval_t -INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, - asn_per_data_t *pd) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - asn_dec_rval_t rval = { RC_OK, 0 }; - INTEGER_t *st = (INTEGER_t *)*sptr; - const asn_per_constraint_t *ct; - int repeat; - - (void)opt_codec_ctx; - - if(!st) { - st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); - if(!st) ASN__DECODE_FAILED; - } - - if(!constraints) constraints = td->encoding_constraints.per_constraints; - ct = constraints ? &constraints->value : 0; - - if(ct && ct->flags & APC_EXTENSIBLE) { - int inext = per_get_few_bits(pd, 1); - if(inext < 0) ASN__DECODE_STARVED; - if(inext) ct = 0; - } - - FREEMEM(st->buf); - st->buf = 0; - st->size = 0; - if(ct) { - if(ct->flags & APC_SEMI_CONSTRAINED) { - st->buf = (uint8_t *)CALLOC(1, 2); - if(!st->buf) ASN__DECODE_FAILED; - st->size = 1; - } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) { - size_t size = (ct->range_bits + 7) >> 3; - st->buf = (uint8_t *)MALLOC(1 + size + 1); - if(!st->buf) ASN__DECODE_FAILED; - st->size = size; - } - } - - /* X.691-2008/11, #13.2.2, constrained whole number */ - if(ct && ct->flags != APC_UNCONSTRAINED) { - /* #11.5.6 */ - ASN_DEBUG("Integer with range %d bits", ct->range_bits); - if(ct->range_bits >= 0) { - if((size_t)ct->range_bits > 8 * sizeof(unsigned long)) - ASN__DECODE_FAILED; - - if(specs && specs->field_unsigned) { - unsigned long uvalue = 0; - if(uper_get_constrained_whole_number(pd, - &uvalue, ct->range_bits)) - ASN__DECODE_STARVED; - ASN_DEBUG("Got value %lu + low %ld", - uvalue, ct->lower_bound); - uvalue += ct->lower_bound; - if(asn_ulong2INTEGER(st, uvalue)) - ASN__DECODE_FAILED; - } else { - unsigned long uvalue = 0; - long svalue; - if(uper_get_constrained_whole_number(pd, - &uvalue, ct->range_bits)) - ASN__DECODE_STARVED; - ASN_DEBUG("Got value %lu + low %ld", - uvalue, ct->lower_bound); - if(per_long_range_unrebase(uvalue, ct->lower_bound, - ct->upper_bound, &svalue) - || asn_long2INTEGER(st, svalue)) { - ASN__DECODE_FAILED; - } - } - return rval; - } - } else { - ASN_DEBUG("Decoding unconstrained integer %s", td->name); - } - - /* X.691, #12.2.3, #12.2.4 */ - do { - ssize_t len = 0; - void *p = NULL; - int ret = 0; - - /* Get the PER length */ - len = uper_get_length(pd, -1, 0, &repeat); - if(len < 0) ASN__DECODE_STARVED; - - p = REALLOC(st->buf, st->size + len + 1); - if(!p) ASN__DECODE_FAILED; - st->buf = (uint8_t *)p; - - ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); - if(ret < 0) ASN__DECODE_STARVED; - st->size += len; - } while(repeat); - st->buf[st->size] = 0; /* JIC */ - - /* #12.2.3 */ - if(ct && ct->lower_bound) { - /* - * TODO: replace by in-place arithmetics. - */ - long value = 0; - if(asn_INTEGER2long(st, &value)) - ASN__DECODE_FAILED; - if(asn_imax2INTEGER(st, value + ct->lower_bound)) - ASN__DECODE_FAILED; - } - - return rval; -} - -asn_enc_rval_t -INTEGER_encode_uper(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, const void *sptr, - asn_per_outp_t *po) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - asn_enc_rval_t er = {0,0,0}; - const INTEGER_t *st = (const INTEGER_t *)sptr; - const uint8_t *buf; - const uint8_t *end; - const asn_per_constraint_t *ct; - long value = 0; - - if(!st || st->size == 0) ASN__ENCODE_FAILED; - - if(!constraints) constraints = td->encoding_constraints.per_constraints; - ct = constraints ? &constraints->value : 0; - - er.encoded = 0; - - if(ct) { - int inext = 0; - if(specs && specs->field_unsigned) { - unsigned long uval; - if(asn_INTEGER2ulong(st, &uval)) - ASN__ENCODE_FAILED; - /* Check proper range */ - if(ct->flags & APC_SEMI_CONSTRAINED) { - if(uval < (unsigned long)ct->lower_bound) - inext = 1; - } else if(ct->range_bits >= 0) { - if(uval < (unsigned long)ct->lower_bound - || uval > (unsigned long)ct->upper_bound) - inext = 1; - } - ASN_DEBUG("Value %lu (%02x/%" ASN_PRI_SIZE ") lb %lu ub %lu %s", - uval, st->buf[0], st->size, - ct->lower_bound, ct->upper_bound, - inext ? "ext" : "fix"); - value = uval; - } else { - if(asn_INTEGER2long(st, &value)) - ASN__ENCODE_FAILED; - /* Check proper range */ - if(ct->flags & APC_SEMI_CONSTRAINED) { - if(value < ct->lower_bound) - inext = 1; - } else if(ct->range_bits >= 0) { - if(value < ct->lower_bound - || value > ct->upper_bound) - inext = 1; - } - ASN_DEBUG("Value %ld (%02x/%" ASN_PRI_SIZE ") lb %ld ub %ld %s", - value, st->buf[0], st->size, - ct->lower_bound, ct->upper_bound, - inext ? "ext" : "fix"); - } - if(ct->flags & APC_EXTENSIBLE) { - if(per_put_few_bits(po, inext, 1)) - ASN__ENCODE_FAILED; - if(inext) ct = 0; - } else if(inext) { - ASN__ENCODE_FAILED; - } - } - - - /* X.691-11/2008, #13.2.2, test if constrained whole number */ - if(ct && ct->range_bits >= 0) { - unsigned long v; - /* #11.5.6 -> #11.3 */ - ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", - value, value - ct->lower_bound, ct->range_bits); - if(specs && specs->field_unsigned) { - if ( ((unsigned long)ct->lower_bound > (unsigned long)(ct->upper_bound) - || ((unsigned long)value < (unsigned long)ct->lower_bound)) - || ((unsigned long)value > (unsigned long)ct->upper_bound) - ) { - ASN_DEBUG("Value %lu to-be-encoded is outside the bounds [%lu, %lu]!", - value, ct->lower_bound, ct->upper_bound); - ASN__ENCODE_FAILED; - } - v = (unsigned long)value - (unsigned long)ct->lower_bound; - } else { - if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) { - ASN__ENCODE_FAILED; - } - } - if(uper_put_constrained_whole_number_u(po, v, ct->range_bits)) - ASN__ENCODE_FAILED; - ASN__ENCODED_OK(er); - } - - if(ct && ct->lower_bound) { - ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound); - /* TODO: adjust lower bound */ - ASN__ENCODE_FAILED; - } - - for(buf = st->buf, end = st->buf + st->size; buf < end;) { - int need_eom = 0; - ssize_t mayEncode = uper_put_length(po, 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 && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED; - } - - ASN__ENCODED_OK(er); -} - -asn_dec_rval_t -INTEGER_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { - const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics; - asn_dec_rval_t rval = { RC_OK, 0 }; - INTEGER_t *st = (INTEGER_t *)*sptr; - const asn_per_constraint_t *ct; - int repeat; - - (void)opt_codec_ctx; - - if(!st) { - st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); - if(!st) ASN__DECODE_FAILED; - } - - if(!constraints) constraints = td->encoding_constraints.per_constraints; - ct = constraints ? &constraints->value : 0; - - if(ct && ct->flags & APC_EXTENSIBLE) { - int inext = per_get_few_bits(pd, 1); - if(inext < 0) ASN__DECODE_STARVED; - if(inext) ct = 0; - } - - FREEMEM(st->buf); - st->buf = 0; - st->size = 0; - if(ct) { - if(ct->flags & APC_SEMI_CONSTRAINED) { - st->buf = (uint8_t *)CALLOC(1, 2); - if(!st->buf) ASN__DECODE_FAILED; - st->size = 1; - } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) { - size_t size = (ct->range_bits + 7) >> 3; - st->buf = (uint8_t *)MALLOC(1 + size + 1); - if(!st->buf) ASN__DECODE_FAILED; - st->size = size; - } - } - - /* X.691, #12.2.2 */ - if(ct && ct->flags != APC_UNCONSTRAINED) { - /* #10.5.6 */ - ASN_DEBUG("Integer with range %d bits", ct->range_bits); - if(ct->range_bits >= 0) { - if (ct->range_bits > 16) { - int max_range_bytes = (ct->range_bits >> 3) + - (((ct->range_bits % 8) > 0) ? 1 : 0); - int length = 0, i; - long value = 0; - - for (i = 1; ; i++) { - int upper = 1 << i; - if (upper >= max_range_bytes) - break; - } - ASN_DEBUG("Can encode %d (%d bytes) in %d bits", ct->range_bits, - max_range_bytes, i); - - if ((length = per_get_few_bits(pd, i)) < 0) - ASN__DECODE_FAILED; - - /* X.691 #12.2.6 length determinant + lb (1) */ - length += 1; - ASN_DEBUG("Got length %d", length); - if (aper_get_align(pd) != 0) - ASN__DECODE_FAILED; - while (length--) { - int buf = per_get_few_bits(pd, 8); - if (buf < 0) - ASN__DECODE_FAILED; - value += (((long)buf) << (8 * length)); - } - - value += ct->lower_bound; - if((specs && specs->field_unsigned) - ? asn_uint642INTEGER(st, (unsigned long)value) - : asn_int642INTEGER(st, value)) - ASN__DECODE_FAILED; - ASN_DEBUG("Got value %ld + low %ld", - value, ct->lower_bound); - } else { - long value = 0; - if (ct->range_bits < 8) { - value = per_get_few_bits(pd, ct->range_bits); - if(value < 0) ASN__DECODE_STARVED; - } else if (ct->range_bits == 8) { - if (aper_get_align(pd) < 0) - ASN__DECODE_FAILED; - value = per_get_few_bits(pd, ct->range_bits); - if(value < 0) ASN__DECODE_STARVED; - } else { - /* Align */ - if (aper_get_align(pd) < 0) - ASN__DECODE_FAILED; - value = per_get_few_bits(pd, 16); - if(value < 0) ASN__DECODE_STARVED; - } - value += ct->lower_bound; - if((specs && specs->field_unsigned) - ? asn_ulong2INTEGER(st, value) - : asn_long2INTEGER(st, value)) - ASN__DECODE_FAILED; - ASN_DEBUG("Got value %ld + low %ld", - value, ct->lower_bound); - } - return rval; - } else { - ASN__DECODE_FAILED; - } - } else { - ASN_DEBUG("Decoding unconstrained integer %s", td->name); - } - - /* X.691, #12.2.3, #12.2.4 */ - do { - ssize_t len; - void *p; - int ret; - - /* Get the PER length */ - len = aper_get_length(pd, -1, -1, &repeat); - if(len < 0) ASN__DECODE_STARVED; - - p = REALLOC(st->buf, st->size + len + 1); - if(!p) ASN__DECODE_FAILED; - st->buf = (uint8_t *)p; - - ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); - if(ret < 0) ASN__DECODE_STARVED; - st->size += len; - } while(repeat); - st->buf[st->size] = 0; /* JIC */ - - /* #12.2.3 */ - if(ct && ct->lower_bound) { - /* - * TODO: replace by in-place arithmetics. - */ - long value; - if(asn_INTEGER2long(st, &value)) - ASN__DECODE_FAILED; - if(asn_long2INTEGER(st, value + ct->lower_bound)) - ASN__DECODE_FAILED; - } - - return rval; -} - -asn_enc_rval_t -INTEGER_encode_aper(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po) { - const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics; - asn_enc_rval_t er = {0,0,0}; - const INTEGER_t *st = (const INTEGER_t *)sptr; - const uint8_t *buf; - const uint8_t *end; - const asn_per_constraint_t *ct; - long value = 0; - - if(!st || st->size == 0) ASN__ENCODE_FAILED; - - if(!constraints) constraints = td->encoding_constraints.per_constraints; - ct = constraints ? &constraints->value : 0; - - er.encoded = 0; - - if(ct) { - int inext = 0; - if(specs && specs->field_unsigned) { - unsigned long uval; - if(asn_INTEGER2ulong(st, &uval)) - ASN__ENCODE_FAILED; - /* Check proper range */ - if(ct->flags & APC_SEMI_CONSTRAINED) { - if(uval < (unsigned long)ct->lower_bound) - inext = 1; - } else if(ct->range_bits >= 0) { - if(uval < (unsigned long)ct->lower_bound - || uval > (unsigned long)ct->upper_bound) - inext = 1; - } - ASN_DEBUG("Value %lu (%02x/%lu) lb %ld ub %ld %s", - uval, st->buf[0], st->size, - ct->lower_bound, ct->upper_bound, - inext ? "ext" : "fix"); - value = uval; - } else { - if(asn_INTEGER2long(st, &value)) ASN__ENCODE_FAILED; - /* Check proper range */ - if(ct->flags & APC_SEMI_CONSTRAINED) { - if(value < ct->lower_bound) - inext = 1; - } else if(ct->range_bits >= 0) { - if(value < ct->lower_bound - || value > ct->upper_bound) - inext = 1; - } - ASN_DEBUG("Value %lu (%02x/%lu) lb %ld ub %ld %s", - value, st->buf[0], st->size, - ct->lower_bound, ct->upper_bound, - inext ? "ext" : "fix"); - } - if(ct->flags & APC_EXTENSIBLE) { - if(per_put_few_bits(po, inext, 1)) - ASN__ENCODE_FAILED; - if(inext) ct = 0; - } else if(inext) { - ASN__ENCODE_FAILED; - } - } - - /* X.691, #12.2.2 */ - if(ct && ct->range_bits >= 0) { - unsigned long v; - - /* #10.5.6 */ - ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", - value, value - ct->lower_bound, ct->range_bits); - - v = value - ct->lower_bound; - - /* #12 <= 8 -> alignment ? */ - if (ct->range_bits < 8) { - if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) - ASN__ENCODE_FAILED; - } else if (ct->range_bits == 8) { - if(aper_put_align(po) < 0) - ASN__ENCODE_FAILED; - if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) - ASN__ENCODE_FAILED; - } else if (ct->range_bits <= 16) { - /* Consume the bytes to align on octet */ - if(aper_put_align(po) < 0) - ASN__ENCODE_FAILED; - if(per_put_few_bits(po, 0x0000 | v, - 16)) - ASN__ENCODE_FAILED; - } else { - /* TODO: extend to >64 bits */ - int64_t v64 = v; - int i, j; - int max_range_bytes = (ct->range_bits >> 3) + - (((ct->range_bits % 8) > 0) ? 1 : 0); - - for (i = 1; ; i++) { - int upper = 1 << i; - if (upper >= max_range_bytes) - break; - } - - for (j = sizeof(int64_t) -1; j != 0; j--) { - int64_t val; - val = v64 >> (j * 8); - if (val != 0) - break; - } - - /* Putting length in the minimum number of bits ex: 5 = 3bits */ - if (per_put_few_bits(po, j, i)) - ASN__ENCODE_FAILED; - - /* Consume the bits to align on octet */ - if (aper_put_align(po) < 0) - ASN__ENCODE_FAILED; - /* Put the value */ - for (i = 0; i <= j; i++) { - if(per_put_few_bits(po, (v64 >> (8 * (j - i))) & 0xff, 8)) - ASN__ENCODE_FAILED; - } - } - ASN__ENCODED_OK(er); - } - - if(ct && ct->lower_bound) { - ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound); - /* TODO: adjust lower bound */ - ASN__ENCODE_FAILED; - } - - for(buf = st->buf, end = st->buf + st->size; buf < end;) { - ssize_t mayEncode = aper_put_length(po, -1, end - buf); - if(mayEncode < 0) - ASN__ENCODE_FAILED; - if(per_put_many_bits(po, buf, 8 * mayEncode)) - ASN__ENCODE_FAILED; - buf += mayEncode; - } - - ASN__ENCODED_OK(er); -} - -#endif /* ASN_DISABLE_PER_SUPPORT */ - -static intmax_t -asn__integer_convert(const uint8_t *b, const uint8_t *end) { - uintmax_t value; - - /* Perform the sign initialization */ - /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */ - if((*b >> 7)) { - value = (uintmax_t)(-1); - } else { - value = 0; - } - - /* Conversion engine */ - for(; b < end; b++) { - value = (value << 8) | *b; - } - - return value; -} - -int -asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { - uint8_t *b, *end; - size_t size; - - /* Sanity checking */ - if(!iptr || !iptr->buf || !lptr) { - errno = EINVAL; - return -1; - } - - /* Cache the begin/end of the buffer */ - b = iptr->buf; /* Start of the INTEGER buffer */ - size = iptr->size; - end = b + size; /* Where to stop */ - - if(size > sizeof(intmax_t)) { - uint8_t *end1 = end - 1; - /* - * Slightly more advanced processing, - * able to process INTEGERs with >sizeof(intmax_t) bytes - * when the actual value is small, e.g. for intmax_t == int32_t - * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t) - */ - /* Skip out the insignificant leading bytes */ - for(; b < end1; b++) { - switch(*b) { - case 0x00: if((b[1] & 0x80) == 0) continue; break; - case 0xff: if((b[1] & 0x80) != 0) continue; break; - } - break; - } - - size = end - b; - if(size > sizeof(intmax_t)) { - /* Still cannot fit the sizeof(intmax_t) */ - errno = ERANGE; - return -1; - } - } - - /* Shortcut processing of a corner case */ - if(end == b) { - *lptr = 0; - return 0; - } - - *lptr = asn__integer_convert(b, end); - return 0; -} - -/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */ -int -asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) { - uint8_t *b, *end; - uintmax_t value; - size_t size; - - if(!iptr || !iptr->buf || !lptr) { - errno = EINVAL; - return -1; - } - - b = iptr->buf; - size = iptr->size; - end = b + size; - - /* If all extra leading bytes are zeroes, ignore them */ - for(; size > sizeof(value); b++, size--) { - if(*b) { - /* Value won't fit into uintmax_t */ - errno = ERANGE; - return -1; - } - } - - /* Conversion engine */ - for(value = 0; b < end; b++) - value = (value << 8) | *b; - - *lptr = value; - return 0; -} - -int -asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) { - uint8_t *buf; - uint8_t *end; - uint8_t *b; - int shr; - - if(value <= ((~(uintmax_t)0) >> 1)) { - return asn_imax2INTEGER(st, value); - } - - buf = (uint8_t *)MALLOC(1 + sizeof(value)); - if(!buf) return -1; - - end = buf + (sizeof(value) + 1); - buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */ - for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++) - *b = (uint8_t)(value >> shr); - - if(st->buf) FREEMEM(st->buf); - st->buf = buf; - st->size = 1 + sizeof(value); - - return 0; -} - -int -asn_imax2INTEGER(INTEGER_t *st, intmax_t value) { - uint8_t *buf, *bp; - uint8_t *p; - uint8_t *pstart; - uint8_t *pend1; - int littleEndian = 1; /* Run-time detection */ - int add; - - if(!st) { - errno = EINVAL; - return -1; - } - - buf = (uint8_t *)(long *)MALLOC(sizeof(value)); - if(!buf) return -1; - - if(*(char *)&littleEndian) { - pstart = (uint8_t *)&value + sizeof(value) - 1; - pend1 = (uint8_t *)&value; - add = -1; - } else { - pstart = (uint8_t *)&value; - pend1 = pstart + sizeof(value) - 1; - add = 1; - } - - /* - * If the contents octet consists of more than one octet, - * then bits of the first octet and bit 8 of the second octet: - * a) shall not all be ones; and - * b) shall not all be zero. - */ - for(p = pstart; p != pend1; p += add) { - switch(*p) { - case 0x00: if((*(p+add) & 0x80) == 0) - continue; - break; - case 0xff: if((*(p+add) & 0x80)) - continue; - break; - } - break; - } - /* Copy the integer body */ - for(bp = buf, pend1 += add; p != pend1; p += add) - *bp++ = *p; - - if(st->buf) FREEMEM(st->buf); - st->buf = buf; - st->size = bp - buf; - - return 0; -} - -int -asn_INTEGER2long(const INTEGER_t *iptr, long *l) { - intmax_t v; - if(asn_INTEGER2imax(iptr, &v) == 0) { - if(v < LONG_MIN || v > LONG_MAX) { - errno = ERANGE; - return -1; - } - *l = v; - return 0; - } else { - return -1; - } -} - -int -asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) { - uintmax_t v; - if(asn_INTEGER2umax(iptr, &v) == 0) { - if(v > ULONG_MAX) { - errno = ERANGE; - return -1; - } - *l = v; - return 0; - } else { - return -1; - } -} - -int -asn_long2INTEGER(INTEGER_t *st, long value) { - return asn_imax2INTEGER(st, value); -} - -int -asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) { - return asn_imax2INTEGER(st, value); -} - - -int -asn_uint642INTEGER(INTEGER_t *st, uint64_t value) { - uint8_t *buf; - uint8_t *end; - uint8_t *b; - int shr; - - if(value <= INT64_MAX) - return asn_int642INTEGER(st, value); - - buf = (uint8_t *)MALLOC(1 + sizeof(value)); - if(!buf) return -1; - - end = buf + (sizeof(value) + 1); - buf[0] = 0; - for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++) - *b = (uint8_t)(value >> shr); - - if(st->buf) FREEMEM(st->buf); - st->buf = buf; - st->size = 1 + sizeof(value); - - return 0; -} - -int -asn_int642INTEGER(INTEGER_t *st, int64_t value) { - uint8_t *buf, *bp; - uint8_t *p; - uint8_t *pstart; - uint8_t *pend1; - int littleEndian = 1; /* Run-time detection */ - int add; - - if(!st) { - errno = EINVAL; - return -1; - } - - buf = (uint8_t *)MALLOC(sizeof(value)); - if(!buf) return -1; - - if(*(char *)&littleEndian) { - pstart = (uint8_t *)&value + sizeof(value) - 1; - pend1 = (uint8_t *)&value; - add = -1; - } else { - pstart = (uint8_t *)&value; - pend1 = pstart + sizeof(value) - 1; - add = 1; - } - - /* - * If the contents octet consists of more than one octet, - * then bits of the first octet and bit 8 of the second octet: - * a) shall not all be ones; and - * b) shall not all be zero. - */ - for(p = pstart; p != pend1; p += add) { - switch(*p) { - case 0x00: if((*(p+add) & 0x80) == 0) - continue; - break; - case 0xff: if((*(p+add) & 0x80)) - continue; - break; - } - break; - } - /* Copy the integer body */ - for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) - *bp++ = *p; - - if(st->buf) FREEMEM(st->buf); - st->buf = buf; - st->size = bp - buf; - - return 0; -} - -/* - * Parse the number in the given string until the given *end position, - * returning the position after the last parsed character back using the - * same (*end) pointer. - * WARNING: This behavior is different from the standard strtol/strtoimax(3). - */ -enum asn_strtox_result_e -asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) { - int sign = 1; - intmax_t value; - -#define ASN1_INTMAX_MAX ((~(uintmax_t)0) >> 1) - const intmax_t upper_boundary = ASN1_INTMAX_MAX / 10; - intmax_t last_digit_max = ASN1_INTMAX_MAX % 10; -#undef ASN1_INTMAX_MAX - - if(str >= *end) return ASN_STRTOX_ERROR_INVAL; - - switch(*str) { - case '-': - last_digit_max++; - sign = -1; - /* FALL THROUGH */ - case '+': - str++; - if(str >= *end) { - *end = str; - return ASN_STRTOX_EXPECT_MORE; - } - } - - for(value = 0; str < (*end); str++) { - switch(*str) { - case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: - case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: { - int d = *str - '0'; - if(value < upper_boundary) { - value = value * 10 + d; - } else if(value == upper_boundary) { - if(d <= last_digit_max) { - if(sign > 0) { - value = value * 10 + d; - } else { - sign = 1; - value = -value * 10 - d; - } - } else { - *end = str; - return ASN_STRTOX_ERROR_RANGE; - } - } else { - *end = str; - return ASN_STRTOX_ERROR_RANGE; - } - } - continue; - default: - *end = str; - *intp = sign * value; - return ASN_STRTOX_EXTRA_DATA; - } - } - - *end = str; - *intp = sign * value; - return ASN_STRTOX_OK; -} - -/* - * Parse the number in the given string until the given *end position, - * returning the position after the last parsed character back using the - * same (*end) pointer. - * WARNING: This behavior is different from the standard strtoul/strtoumax(3). - */ -enum asn_strtox_result_e -asn_strtoumax_lim(const char *str, const char **end, uintmax_t *uintp) { - uintmax_t value; - -#define ASN1_UINTMAX_MAX ((~(uintmax_t)0)) - const uintmax_t upper_boundary = ASN1_UINTMAX_MAX / 10; - uintmax_t last_digit_max = ASN1_UINTMAX_MAX % 10; -#undef ASN1_UINTMAX_MAX - - if(str >= *end) return ASN_STRTOX_ERROR_INVAL; - - switch(*str) { - case '-': - return ASN_STRTOX_ERROR_INVAL; - case '+': - str++; - if(str >= *end) { - *end = str; - return ASN_STRTOX_EXPECT_MORE; - } - } - - for(value = 0; str < (*end); str++) { - switch(*str) { - case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: - case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: { - unsigned int d = *str - '0'; - if(value < upper_boundary) { - value = value * 10 + d; - } else if(value == upper_boundary) { - if(d <= last_digit_max) { - value = value * 10 + d; - } else { - *end = str; - return ASN_STRTOX_ERROR_RANGE; - } - } else { - *end = str; - return ASN_STRTOX_ERROR_RANGE; - } - } - continue; - default: - *end = str; - *uintp = value; - return ASN_STRTOX_EXTRA_DATA; - } - } - - *end = str; - *uintp = value; - return ASN_STRTOX_OK; -} - -enum asn_strtox_result_e -asn_strtol_lim(const char *str, const char **end, long *lp) { - intmax_t value; - switch(asn_strtoimax_lim(str, end, &value)) { - case ASN_STRTOX_ERROR_RANGE: - return ASN_STRTOX_ERROR_RANGE; - case ASN_STRTOX_ERROR_INVAL: - return ASN_STRTOX_ERROR_INVAL; - case ASN_STRTOX_EXPECT_MORE: - return ASN_STRTOX_EXPECT_MORE; - case ASN_STRTOX_OK: - if(value >= LONG_MIN && value <= LONG_MAX) { - *lp = value; - return ASN_STRTOX_OK; - } else { - return ASN_STRTOX_ERROR_RANGE; - } - case ASN_STRTOX_EXTRA_DATA: - if(value >= LONG_MIN && value <= LONG_MAX) { - *lp = value; - return ASN_STRTOX_EXTRA_DATA; - } else { - return ASN_STRTOX_ERROR_RANGE; - } - } - - assert(!"Unreachable"); - return ASN_STRTOX_ERROR_INVAL; -} - -enum asn_strtox_result_e -asn_strtoul_lim(const char *str, const char **end, unsigned long *ulp) { - uintmax_t value; - switch(asn_strtoumax_lim(str, end, &value)) { - case ASN_STRTOX_ERROR_RANGE: - return ASN_STRTOX_ERROR_RANGE; - case ASN_STRTOX_ERROR_INVAL: - return ASN_STRTOX_ERROR_INVAL; - case ASN_STRTOX_EXPECT_MORE: - return ASN_STRTOX_EXPECT_MORE; - case ASN_STRTOX_OK: - if(value <= ULONG_MAX) { - *ulp = value; - return ASN_STRTOX_OK; - } else { - return ASN_STRTOX_ERROR_RANGE; - } - case ASN_STRTOX_EXTRA_DATA: - if(value <= ULONG_MAX) { - *ulp = value; - return ASN_STRTOX_EXTRA_DATA; - } else { - return ASN_STRTOX_ERROR_RANGE; - } - } - - assert(!"Unreachable"); - return ASN_STRTOX_ERROR_INVAL; -} - -int -INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr, - const void *bptr) { - const INTEGER_t *a = aptr; - const INTEGER_t *b = bptr; - - (void)td; - - if(a && b) { - if(a->size && b->size) { - int sign_a = (a->buf[0] & 0x80) ? -1 : 1; - int sign_b = (b->buf[0] & 0x80) ? -1 : 1; - - if(sign_a < sign_b) return -1; - if(sign_a > sign_b) return 1; - - /* The shortest integer wins, unless comparing negatives */ - if(a->size < b->size) { - return -1 * sign_a; - } else if(a->size > b->size) { - return 1 * sign_b; - } - - return sign_a * memcmp(a->buf, b->buf, a->size); - } else if(a->size) { - int sign = (a->buf[0] & 0x80) ? -1 : 1; - return (1) * sign; - } else if(b->size) { - int sign = (a->buf[0] & 0x80) ? -1 : 1; - return (-1) * sign; - } else { - return 0; - } - } else if(!a && !b) { - return 0; - } else if(!a) { - return -1; - } else { - return 1; - } - -} - -asn_random_fill_result_t -INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, - const asn_encoding_constraints_t *constraints, - size_t max_length) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; - asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; - asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; - INTEGER_t *st = *sptr; - const asn_INTEGER_enum_map_t *emap; - size_t emap_len; - intmax_t value; - int find_inside_map; - - if(max_length == 0) return result_skipped; - - if(st == NULL) { - st = (INTEGER_t *)CALLOC(1, sizeof(*st)); - if(st == NULL) { - return result_failed; - } - } - - if(specs) { - emap = specs->value2enum; - emap_len = specs->map_count; - if(specs->strict_enumeration) { - find_inside_map = emap_len > 0; - } else { - find_inside_map = emap_len ? asn_random_between(0, 1) : 0; - } - } else { - emap = 0; - emap_len = 0; - find_inside_map = 0; - } - - if(find_inside_map) { - assert(emap_len > 0); - value = emap[asn_random_between(0, emap_len - 1)].nat_value; - } else { - const asn_per_constraints_t *ct; - - static const long variants[] = { - -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384, - -16383, -257, -256, -255, -254, -129, -128, -127, - -126, -1, 0, 1, 126, 127, 128, 129, - 254, 255, 256, 257, 16383, 16384, 16385, 32767, - 32768, 32769, 65534, 65535, 65536, 65537}; - if(specs && specs->field_unsigned) { - assert(variants[18] == 0); - value = variants[asn_random_between( - 18, sizeof(variants) / sizeof(variants[0]) - 1)]; - } else { - value = variants[asn_random_between( - 0, sizeof(variants) / sizeof(variants[0]) - 1)]; - } - - if(!constraints) constraints = &td->encoding_constraints; - ct = constraints ? constraints->per_constraints : 0; - if(ct && (ct->value.flags & APC_CONSTRAINED)) { - if(value < ct->value.lower_bound || value > ct->value.upper_bound) { - value = asn_random_between(ct->value.lower_bound, - ct->value.upper_bound); - } - } - } - - if(asn_imax2INTEGER(st, value)) { - if(st == *sptr) { - ASN_STRUCT_RESET(*td, st); - } else { - ASN_STRUCT_FREE(*td, st); - } - return result_failed; - } else { - *sptr = st; - result_ok.length = st->size; - return result_ok; - } -} diff --git a/src/codec_utils/E2AP/INTEGER.h b/src/codec_utils/E2AP/INTEGER.h deleted file mode 100644 index f776c072e..000000000 --- a/src/codec_utils/E2AP/INTEGER.h +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _INTEGER_H_ -#define _INTEGER_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef ASN__PRIMITIVE_TYPE_t INTEGER_t; - -extern asn_TYPE_descriptor_t asn_DEF_INTEGER; -extern asn_TYPE_operation_t asn_OP_INTEGER; - -/* Map with to integer value association */ -typedef struct asn_INTEGER_enum_map_s { - long nat_value; /* associated native integer value */ - size_t enum_len; /* strlen("tag") */ - const char *enum_name; /* "tag" */ -} asn_INTEGER_enum_map_t; - -/* This type describes an enumeration for INTEGER and ENUMERATED types */ -typedef struct asn_INTEGER_specifics_s { - const asn_INTEGER_enum_map_t *value2enum; /* N -> "tag"; sorted by N */ - const unsigned int *enum2value; /* "tag" => N; sorted by tag */ - int map_count; /* Elements in either map */ - int extension; /* This map is extensible */ - int strict_enumeration; /* Enumeration set is fixed */ - int field_width; /* Size of native integer */ - int field_unsigned; /* Signed=0, unsigned=1 */ -} asn_INTEGER_specifics_t; - -#define INTEGER_free ASN__PRIMITIVE_TYPE_free -#define INTEGER_decode_ber ber_decode_primitive -#define INTEGER_constraint asn_generic_no_constraint -asn_struct_print_f INTEGER_print; -asn_struct_compare_f INTEGER_compare; -der_type_encoder_f INTEGER_encode_der; -xer_type_decoder_f INTEGER_decode_xer; -xer_type_encoder_f INTEGER_encode_xer; -oer_type_decoder_f INTEGER_decode_oer; -oer_type_encoder_f INTEGER_encode_oer; -per_type_decoder_f INTEGER_decode_uper; -per_type_encoder_f INTEGER_encode_uper; -per_type_decoder_f INTEGER_decode_aper; -per_type_encoder_f INTEGER_encode_aper; -asn_random_fill_f INTEGER_random_fill; - -/*********************************** - * Some handy conversion routines. * - ***********************************/ - -/* - * Natiwe size-independent conversion of native integers to/from INTEGER. - * (l_size) is in bytes. - * Returns 0 if it was possible to convert, -1 otherwise. - * -1/EINVAL: Mandatory argument missing - * -1/ERANGE: Value encoded is out of range for long representation - * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()). - */ -int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l); -int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l); -int asn_imax2INTEGER(INTEGER_t *i, intmax_t l); -int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l); - -/* - * Size-specific conversion helpers. - */ -int asn_INTEGER2long(const INTEGER_t *i, long *l); -int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l); -int asn_long2INTEGER(INTEGER_t *i, long l); -int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l); -int asn_int642INTEGER(INTEGER_t *i, int64_t l); -int asn_uint642INTEGER(INTEGER_t *i, uint64_t l); - -/* A version of strtol/strtoimax(3) with nicer error reporting. */ -enum asn_strtox_result_e { - ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */ - ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */ - ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */ - ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */ - ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */ -}; -enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end, - long *l); -enum asn_strtox_result_e asn_strtoul_lim(const char *str, const char **end, - unsigned long *l); -enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end, - intmax_t *l); -enum asn_strtox_result_e asn_strtoumax_lim(const char *str, const char **end, - uintmax_t *l); - -/* - * Convert the integer value into the corresponding enumeration map entry. - */ -const asn_INTEGER_enum_map_t *INTEGER_map_value2enum( - const asn_INTEGER_specifics_t *specs, long value); - -#ifdef __cplusplus -} -#endif - -#endif /* _INTEGER_H_ */ diff --git a/src/codec_utils/E2AP/INTEGER_oer.c b/src/codec_utils/E2AP/INTEGER_oer.c deleted file mode 100644 index 110689b2d..000000000 --- a/src/codec_utils/E2AP/INTEGER_oer.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . - * All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_DISABLE_OER_SUPPORT - -#include -#include -#include - -asn_dec_rval_t -INTEGER_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_oer_constraints_t *constraints, void **sptr, - const void *ptr, size_t size) { - const asn_INTEGER_specifics_t *specs = - (const asn_INTEGER_specifics_t *)td->specifics; - asn_dec_rval_t rval = {RC_OK, 0}; - INTEGER_t *st = (INTEGER_t *)*sptr; - struct asn_oer_constraint_number_s ct = {0, 0}; - size_t req_bytes; - - (void)opt_codec_ctx; - (void)specs; - - if(!st) { - st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); - if(!st) ASN__DECODE_FAILED; - } - - FREEMEM(st->buf); - st->buf = 0; - st->size = 0; - - if(!constraints) constraints = td->encoding_constraints.oer_constraints; - if(constraints) ct = constraints->value; - - if(ct.width) { - req_bytes = ct.width; - } else { - /* No lower bound and no upper bound, effectively */ - - 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; - } - - if(ct.positive) { - /* X.969 08/2015 10.2(a) */ - unsigned msb; /* Most significant bit */ - size_t useful_size; - - /* 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 { - /* X.969 08/2015 10.2(b) */ - 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; - } -} - -/* - * Encode as Canonical OER. - */ -asn_enc_rval_t -INTEGER_encode_oer(const asn_TYPE_descriptor_t *td, - const asn_oer_constraints_t *constraints, const void *sptr, - asn_app_consume_bytes_f *cb, void *app_key) { - const INTEGER_t *st = sptr; - asn_enc_rval_t er = {0,0,0}; - struct asn_oer_constraint_number_s ct = {0, 0}; - const uint8_t *buf; - const uint8_t *end; - size_t useful_bytes; - size_t req_bytes = 0; - int sign = 0; - - if(!st || st->size == 0) ASN__ENCODE_FAILED; - - if(!constraints) constraints = td->encoding_constraints.oer_constraints; - if(constraints) ct = constraints->value; - - er.encoded = 0; - - buf = st->buf; - end = buf + st->size; - - sign = (buf && buf < end) ? buf[0] & 0x80 : 0; - - /* Ignore 9 leading zeroes or ones */ - if(ct.positive) { - if(sign) { - /* The value given is a signed value. Can't proceed. */ - ASN__ENCODE_FAILED; - } - /* Remove leading zeros. */ - for(; buf + 1 < end; buf++) { - if(buf[0] != 0x0) break; - } - } else { - for(; buf + 1 < end; buf++) { - if(buf[0] == 0x0 && (buf[1] & 0x80) == 0) { - continue; - } else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) { - continue; - } - break; - } - } - - useful_bytes = end - buf; - if(ct.width) { - req_bytes = ct.width; - } else { - ssize_t r = oer_serialize_length(useful_bytes, cb, app_key); - if(r < 0) { - ASN__ENCODE_FAILED; - } - er.encoded += r; - req_bytes = useful_bytes; - } - - if(req_bytes < useful_bytes) { - ASN__ENCODE_FAILED; - } - - er.encoded += req_bytes; - - for(; req_bytes > useful_bytes; req_bytes--) { - if(cb(sign?"\xff":"\0", 1, app_key) < 0) { - ASN__ENCODE_FAILED; - } - } - - if(cb(buf, useful_bytes, app_key) < 0) { - ASN__ENCODE_FAILED; - } - - ASN__ENCODED_OK(er); -} - -#endif /* ASN_DISABLE_OER_SUPPORT */ diff --git a/src/codec_utils/E2AP/OPEN_TYPE.c b/src/codec_utils/E2AP/OPEN_TYPE.c deleted file mode 100644 index a54e99c3c..000000000 --- a/src/codec_utils/E2AP/OPEN_TYPE.c +++ /dev/null @@ -1,509 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include -#include -#include - -asn_TYPE_operation_t asn_OP_OPEN_TYPE = { - OPEN_TYPE_free, - OPEN_TYPE_print, - OPEN_TYPE_compare, - OPEN_TYPE_decode_ber, - OPEN_TYPE_encode_der, - OPEN_TYPE_decode_xer, - OPEN_TYPE_encode_xer, -#ifdef ASN_DISABLE_OER_SUPPORT - 0, 0, /* No OER support, use "-gen-OER" to enable */ -#else - OPEN_TYPE_decode_oer, - OPEN_TYPE_encode_oer, -#endif -#ifdef ASN_DISABLE_PER_SUPPORT - 0, 0, 0, 0, -#else - OPEN_TYPE_decode_uper, - OPEN_TYPE_encode_uper, - OPEN_TYPE_decode_aper, - OPEN_TYPE_encode_aper, -#endif - 0, /* Random fill is not supported for open type */ - 0 /* Use generic outmost tag fetcher */ -}; - -#undef ADVANCE -#define ADVANCE(num_bytes) \ - do { \ - size_t num = num_bytes; \ - ptr = ((const char *)ptr) + num; \ - size -= num; \ - consumed_myself += num; \ - } while(0) - -asn_dec_rval_t -OPEN_TYPE_ber_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void *sptr, - const asn_TYPE_member_t *elm, const void *ptr, size_t size) { - size_t consumed_myself = 0; - asn_type_selector_result_t selected; - void *memb_ptr; /* Pointer to the member */ - void **memb_ptr2; /* Pointer to that pointer */ - void *inner_value; - asn_dec_rval_t rv; - - if(!(elm->flags & ATF_OPEN_TYPE)) { - ASN__DECODE_FAILED; - } - - if(!elm->type_selector) { - ASN_DEBUG("Type selector is not defined for Open Type %s->%s->%s", - td->name, elm->name, elm->type->name); - ASN__DECODE_FAILED; - } - - selected = elm->type_selector(td, sptr); - if(!selected.presence_index) { - ASN__DECODE_FAILED; - } - - /* Fetch the pointer to this member */ - if(elm->flags & ATF_POINTER) { - memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); - } else { - memb_ptr = (char *)sptr + elm->memb_offset; - memb_ptr2 = &memb_ptr; - } - if(*memb_ptr2 != NULL) { - /* Make sure we reset the structure first before encoding */ - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, 0) != 0) { - ASN__DECODE_FAILED; - } - } - - inner_value = - (char *)*memb_ptr2 - + elm->type->elements[selected.presence_index - 1].memb_offset; - - ASN_DEBUG("presence %d\n", selected.presence_index); - - rv = selected.type_descriptor->op->ber_decoder( - opt_codec_ctx, selected.type_descriptor, &inner_value, ptr, size, - elm->tag_mode); - ADVANCE(rv.consumed); - rv.consumed = 0; - switch(rv.code) { - case RC_OK: - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, - selected.presence_index) - == 0) { - rv.code = RC_OK; - rv.consumed = consumed_myself; - return rv; - } else { - /* Oh, now a full-blown failure failure */ - } - /* Fall through */ - case RC_FAIL: - rv.consumed = consumed_myself; - /* Fall through */ - case RC_WMORE: - break; - } - - if(*memb_ptr2) { - if(elm->flags & ATF_POINTER) { - ASN_STRUCT_FREE(*selected.type_descriptor, inner_value); - *memb_ptr2 = NULL; - } else { - ASN_STRUCT_RESET(*selected.type_descriptor, - inner_value); - } - } - return rv; -} - -asn_dec_rval_t -OPEN_TYPE_xer_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void *sptr, - const asn_TYPE_member_t *elm, const void *ptr, size_t size) { - size_t consumed_myself = 0; - asn_type_selector_result_t selected; - void *memb_ptr; /* Pointer to the member */ - void **memb_ptr2; /* Pointer to that pointer */ - void *inner_value; - asn_dec_rval_t rv; - - int xer_context = 0; - ssize_t ch_size; - pxer_chunk_type_e ch_type; - - if(!(elm->flags & ATF_OPEN_TYPE)) { - ASN__DECODE_FAILED; - } - - if(!elm->type_selector) { - ASN_DEBUG("Type selector is not defined for Open Type %s->%s->%s", - td->name, elm->name, elm->type->name); - ASN__DECODE_FAILED; - } - - selected = elm->type_selector(td, sptr); - if(!selected.presence_index) { - ASN__DECODE_FAILED; - } - - /* Fetch the pointer to this member */ - assert(elm->flags == ATF_OPEN_TYPE); - if(elm->flags & ATF_POINTER) { - memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); - } else { - memb_ptr = (char *)sptr + elm->memb_offset; - memb_ptr2 = &memb_ptr; - } - if(*memb_ptr2 != NULL) { - /* Make sure we reset the structure first before encoding */ - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, 0) - != 0) { - ASN__DECODE_FAILED; - } - } - - /* - * Confirm wrapper. - */ - for(;;) { - ch_size = xer_next_token(&xer_context, ptr, size, &ch_type); - if(ch_size < 0) { - ASN__DECODE_FAILED; - } else { - switch(ch_type) { - case PXER_WMORE: - ASN__DECODE_STARVED; - case PXER_COMMENT: - case PXER_TEXT: - ADVANCE(ch_size); - continue; - case PXER_TAG: - break; - } - break; - } - } - - /* - * Wrapper value confirmed. - */ - switch(xer_check_tag(ptr, ch_size, elm->name)) { - case XCT_OPENING: - ADVANCE(ch_size); - break; - case XCT_BROKEN: - default: - ASN__DECODE_FAILED; - } - - inner_value = - (char *)*memb_ptr2 - + elm->type->elements[selected.presence_index - 1].memb_offset; - - rv = selected.type_descriptor->op->xer_decoder( - opt_codec_ctx, selected.type_descriptor, &inner_value, NULL, ptr, size); - ADVANCE(rv.consumed); - rv.consumed = 0; - switch(rv.code) { - case RC_OK: - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, - selected.presence_index) - == 0) { - break; - } else { - rv.code = RC_FAIL; - } - /* Fall through */ - case RC_FAIL: - /* Point to a best position where failure occurred */ - rv.consumed = consumed_myself; - /* Fall through */ - case RC_WMORE: - /* Wrt. rv.consumed==0: - * In case a genuine RC_WMORE, the whole Open Type decoding - * will have to be restarted. - */ - if(*memb_ptr2) { - if(elm->flags & ATF_POINTER) { - ASN_STRUCT_FREE(*selected.type_descriptor, inner_value); - *memb_ptr2 = NULL; - } else { - ASN_STRUCT_RESET(*selected.type_descriptor, - inner_value); - } - } - return rv; - } - - /* - * Finalize wrapper. - */ - for(;;) { - ch_size = xer_next_token(&xer_context, ptr, size, &ch_type); - if(ch_size < 0) { - ASN__DECODE_FAILED; - } else { - switch(ch_type) { - case PXER_WMORE: - ASN__DECODE_STARVED; - case PXER_COMMENT: - case PXER_TEXT: - ADVANCE(ch_size); - continue; - case PXER_TAG: - break; - } - break; - } - } - - /* - * Wrapper value confirmed. - */ - switch(xer_check_tag(ptr, ch_size, elm->name)) { - case XCT_CLOSING: - ADVANCE(ch_size); - break; - case XCT_BROKEN: - default: - ASN__DECODE_FAILED; - } - - rv.consumed += consumed_myself; - - return rv; -} - - -#ifndef ASN_DISABLE_PER_SUPPORT - -asn_dec_rval_t -OPEN_TYPE_uper_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void *sptr, - const asn_TYPE_member_t *elm, asn_per_data_t *pd) { - asn_type_selector_result_t selected; - void *memb_ptr; /* Pointer to the member */ - void **memb_ptr2; /* Pointer to that pointer */ - void *inner_value; - asn_dec_rval_t rv; - - if(!(elm->flags & ATF_OPEN_TYPE)) { - ASN__DECODE_FAILED; - } - - if(!elm->type_selector) { - ASN_DEBUG("Type selector is not defined for Open Type %s->%s->%s", - td->name, elm->name, elm->type->name); - ASN__DECODE_FAILED; - } - - selected = elm->type_selector(td, sptr); - if(!selected.presence_index) { - ASN__DECODE_FAILED; - } - - /* Fetch the pointer to this member */ - assert(elm->flags == ATF_OPEN_TYPE); - if(elm->flags & ATF_POINTER) { - memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); - } else { - memb_ptr = (char *)sptr + elm->memb_offset; - memb_ptr2 = &memb_ptr; - } - if(*memb_ptr2 != NULL) { - /* Make sure we reset the structure first before encoding */ - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, 0) - != 0) { - ASN__DECODE_FAILED; - } - } - - inner_value = - (char *)*memb_ptr2 - + elm->type->elements[selected.presence_index - 1].memb_offset; - - rv = uper_open_type_get(opt_codec_ctx, selected.type_descriptor, NULL, - &inner_value, pd); - switch(rv.code) { - case RC_OK: - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, - selected.presence_index) - == 0) { - break; - } else { - rv.code = RC_FAIL; - } - /* Fall through */ - case RC_WMORE: - case RC_FAIL: - if(*memb_ptr2) { - if(elm->flags & ATF_POINTER) { - ASN_STRUCT_FREE(*selected.type_descriptor, inner_value); - *memb_ptr2 = NULL; - } else { - ASN_STRUCT_RESET(*selected.type_descriptor, - inner_value); - } - } - } - return rv; -} - -asn_enc_rval_t -OPEN_TYPE_encode_uper(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po) { - const void *memb_ptr; /* Pointer to the member */ - asn_TYPE_member_t *elm; /* CHOICE's element */ - asn_enc_rval_t er = {0,0,0}; - unsigned present; - - (void)constraints; - - present = CHOICE_variant_get_presence(td, sptr); - if(present == 0 || present > td->elements_count) { - ASN__ENCODE_FAILED; - } else { - present--; - } - - ASN_DEBUG("Encoding %s OPEN TYPE element %d", td->name, present); - - elm = &td->elements[present]; - if(elm->flags & ATF_POINTER) { - /* Member is a pointer to another structure */ - memb_ptr = - *(const void *const *)((const char *)sptr + elm->memb_offset); - if(!memb_ptr) ASN__ENCODE_FAILED; - } else { - memb_ptr = (const char *)sptr + elm->memb_offset; - } - - if(uper_open_type_put(elm->type, NULL, memb_ptr, po) < 0) { - ASN__ENCODE_FAILED; - } - - er.encoded = 0; - ASN__ENCODED_OK(er); -} - -asn_dec_rval_t -OPEN_TYPE_aper_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void *sptr, - const asn_TYPE_member_t *elm, asn_per_data_t *pd) { - asn_type_selector_result_t selected; - void *memb_ptr; /* Pointer to the member */ - void **memb_ptr2; /* Pointer to that pointer */ - void *inner_value; - asn_dec_rval_t rv; - - if(!(elm->flags & ATF_OPEN_TYPE)) { - ASN__DECODE_FAILED; - } - - if(!elm->type_selector) { - ASN_DEBUG("Type selector is not defined for Open Type %s->%s->%s", - td->name, elm->name, elm->type->name); - ASN__DECODE_FAILED; - } - - selected = elm->type_selector(td, sptr); - if(!selected.presence_index) { - ASN__DECODE_FAILED; - } - - /* Fetch the pointer to this member */ - assert(elm->flags == ATF_OPEN_TYPE); - if(elm->flags & ATF_POINTER) { - memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); - } else { - memb_ptr = (char *)sptr + elm->memb_offset; - memb_ptr2 = &memb_ptr; - } - if(*memb_ptr2 != NULL) { - /* Make sure we reset the structure first before encoding */ - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, 0) - != 0) { - ASN__DECODE_FAILED; - } - } - - inner_value = - (char *)*memb_ptr2 - + elm->type->elements[selected.presence_index - 1].memb_offset; - - rv = aper_open_type_get(opt_codec_ctx, selected.type_descriptor, NULL, - &inner_value, pd); - switch(rv.code) { - case RC_OK: - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, - selected.presence_index) - == 0) { - break; - } else { - rv.code = RC_FAIL; - } - /* Fall through */ - case RC_WMORE: - case RC_FAIL: - if(*memb_ptr2) { - if(elm->flags & ATF_POINTER) { - ASN_STRUCT_FREE(*selected.type_descriptor, inner_value); - *memb_ptr2 = NULL; - } else { - ASN_STRUCT_RESET(*selected.type_descriptor, - inner_value); - } - } - } - return rv; -} - -asn_enc_rval_t -OPEN_TYPE_encode_aper(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po) { - const void *memb_ptr; /* Pointer to the member */ - asn_TYPE_member_t *elm; /* CHOICE's element */ - asn_enc_rval_t er = {0,0,0}; - unsigned present; - - (void)constraints; - - present = CHOICE_variant_get_presence(td, sptr); - if(present == 0 || present > td->elements_count) { - ASN__ENCODE_FAILED; - } else { - present--; - } - - ASN_DEBUG("Encoding %s OPEN TYPE element %d", td->name, present); - - elm = &td->elements[present]; - if(elm->flags & ATF_POINTER) { - /* Member is a pointer to another structure */ - memb_ptr = - *(const void *const *)((const char *)sptr + elm->memb_offset); - if(!memb_ptr) ASN__ENCODE_FAILED; - } else { - memb_ptr = (const char *)sptr + elm->memb_offset; - } - - if(aper_open_type_put(elm->type, NULL, memb_ptr, po) < 0) { - ASN__ENCODE_FAILED; - } - - er.encoded = 0; - ASN__ENCODED_OK(er); -} - -#endif /* ASN_DISABLE_PER_SUPPORT */ diff --git a/src/codec_utils/E2AP/OPEN_TYPE.h b/src/codec_utils/E2AP/OPEN_TYPE.h deleted file mode 100644 index b0d023c1f..000000000 --- a/src/codec_utils/E2AP/OPEN_TYPE.h +++ /dev/null @@ -1,77 +0,0 @@ -/*- - * Copyright (c) 2017-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_OPEN_TYPE_H -#define ASN_OPEN_TYPE_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define OPEN_TYPE_free CHOICE_free -#define OPEN_TYPE_print CHOICE_print -#define OPEN_TYPE_compare CHOICE_compare -#define OPEN_TYPE_constraint CHOICE_constraint -#define OPEN_TYPE_decode_ber NULL -#define OPEN_TYPE_encode_der CHOICE_encode_der -#define OPEN_TYPE_decode_xer NULL -#define OPEN_TYPE_encode_xer CHOICE_encode_xer -#define OPEN_TYPE_decode_oer NULL -#define OPEN_TYPE_encode_oer CHOICE_encode_oer -#define OPEN_TYPE_decode_uper NULL -#define OPEN_TYPE_decode_aper NULL - -extern asn_TYPE_operation_t asn_OP_OPEN_TYPE; - -/* - * Decode an Open Type which is potentially constraiend - * by the other members of the parent structure. - */ -asn_dec_rval_t OPEN_TYPE_ber_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *parent_type, - void *parent_structure, - const asn_TYPE_member_t *element, - const void *ptr, size_t size); - -asn_dec_rval_t OPEN_TYPE_xer_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *parent_type, - void *parent_structure, - const asn_TYPE_member_t *element, - const void *ptr, size_t size); - -asn_dec_rval_t OPEN_TYPE_oer_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *parent_type, - void *parent_structure, - asn_TYPE_member_t *element, const void *ptr, - size_t size); - -asn_dec_rval_t OPEN_TYPE_uper_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *parent_type, - void *parent_structure, - const asn_TYPE_member_t *element, - asn_per_data_t *pd); - -asn_dec_rval_t OPEN_TYPE_aper_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *parent_type, - void *parent_structure, - const asn_TYPE_member_t *element, - asn_per_data_t *pd); - -asn_enc_rval_t OPEN_TYPE_encode_uper( - const asn_TYPE_descriptor_t *type_descriptor, - const asn_per_constraints_t *constraints, const void *struct_ptr, - asn_per_outp_t *per_output); - -asn_enc_rval_t OPEN_TYPE_encode_aper( - const asn_TYPE_descriptor_t *type_descriptor, - const asn_per_constraints_t *constraints, const void *struct_ptr, - asn_per_outp_t *per_output); - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_OPEN_TYPE_H */ diff --git a/src/codec_utils/E2AP/OPEN_TYPE_oer.c b/src/codec_utils/E2AP/OPEN_TYPE_oer.c deleted file mode 100644 index dd2f5c644..000000000 --- a/src/codec_utils/E2AP/OPEN_TYPE_oer.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include -#include - -asn_dec_rval_t -OPEN_TYPE_oer_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void *sptr, - asn_TYPE_member_t *elm, const void *ptr, size_t size) { - asn_type_selector_result_t selected; - void *memb_ptr; /* Pointer to the member */ - void **memb_ptr2; /* Pointer to that pointer */ - void *inner_value; - asn_dec_rval_t rv; - size_t ot_ret; - - - if(!(elm->flags & ATF_OPEN_TYPE)) { - ASN__DECODE_FAILED; - } - - if(!elm->type_selector) { - ASN_DEBUG("Type selector is not defined for Open Type %s->%s->%s", - td->name, elm->name, elm->type->name); - ASN__DECODE_FAILED; - } - - selected = elm->type_selector(td, sptr); - if(!selected.presence_index) { - ASN__DECODE_FAILED; - } - - /* Fetch the pointer to this member */ - if(elm->flags & ATF_POINTER) { - memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); - } else { - memb_ptr = (char *)sptr + elm->memb_offset; - memb_ptr2 = &memb_ptr; - } - if(*memb_ptr2 != NULL) { - /* Make sure we reset the structure first before encoding */ - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, 0) != 0) { - ASN__DECODE_FAILED; - } - } - - inner_value = - (char *)*memb_ptr2 - + elm->type->elements[selected.presence_index - 1].memb_offset; - - ot_ret = oer_open_type_get(opt_codec_ctx, selected.type_descriptor, NULL, - &inner_value, ptr, size); - switch(ot_ret) { - default: - if(CHOICE_variant_set_presence(elm->type, *memb_ptr2, - selected.presence_index) - == 0) { - rv.code = RC_OK; - rv.consumed = ot_ret; - return rv; - } else { - /* Oh, now a full-blown failure failure */ - } - /* Fall through */ - case -1: - rv.code = RC_FAIL; - rv.consumed = ot_ret; - break; - case 0: - rv.code = RC_WMORE; - rv.consumed = 0; - break; - } - - if(*memb_ptr2) { - const asn_CHOICE_specifics_t *specs = - selected.type_descriptor->specifics; - if(elm->flags & ATF_POINTER) { - ASN_STRUCT_FREE(*selected.type_descriptor, inner_value); - *memb_ptr2 = NULL; - } else { - ASN_STRUCT_FREE_CONTENTS_ONLY(*selected.type_descriptor, - inner_value); - memset(*memb_ptr2, 0, specs->struct_size); - } - } - return rv; -} diff --git a/src/codec_utils/E2AP/asn_SEQUENCE_OF.c b/src/codec_utils/E2AP/asn_SEQUENCE_OF.c deleted file mode 100644 index ec952fc99..000000000 --- a/src/codec_utils/E2AP/asn_SEQUENCE_OF.c +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include - -typedef A_SEQUENCE_OF(void) asn_sequence; - -void -asn_sequence_del(void *asn_sequence_of_x, int number, int _do_free) { - asn_sequence *as = (asn_sequence *)asn_sequence_of_x; - - if(as) { - void *ptr; - int n; - - if(number < 0 || number >= as->count) - return; /* Nothing to delete */ - - if(_do_free && as->free) { - ptr = as->array[number]; - } else { - ptr = 0; - } - - /* - * Shift all elements to the left to hide the gap. - */ - --as->count; - for(n = number; n < as->count; n++) - as->array[n] = as->array[n+1]; - - /* - * Invoke the third-party function only when the state - * of the parent structure is consistent. - */ - if(ptr) as->free(ptr); - } -} - diff --git a/src/codec_utils/E2AP/asn_SEQUENCE_OF.h b/src/codec_utils/E2AP/asn_SEQUENCE_OF.h deleted file mode 100644 index e35bc447a..000000000 --- a/src/codec_utils/E2AP/asn_SEQUENCE_OF.h +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_SEQUENCE_OF_H -#define ASN_SEQUENCE_OF_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * SEQUENCE OF is the same as SET OF with a tiny difference: - * the delete operation preserves the initial order of elements - * and thus MAY operate in non-constant time. - */ -#define A_SEQUENCE_OF(type) A_SET_OF(type) - -#define ASN_SEQUENCE_ADD(headptr, ptr) \ - asn_sequence_add((headptr), (ptr)) - -/*********************************************** - * Implementation of the SEQUENCE OF structure. - */ - -#define asn_sequence_add asn_set_add -#define asn_sequence_empty asn_set_empty - -/* - * Delete the element from the set by its number (base 0). - * This is NOT a constant-time operation. - * The order of elements is preserved. - * If _do_free is given AND the (*free) is initialized, the element - * will be freed using the custom (*free) function as well. - */ -void asn_sequence_del(void *asn_sequence_of_x, int number, int _do_free); - -/* - * Cope with different conversions requirements to/from void in C and C++. - * This is mostly useful for support library. - */ -typedef A_SEQUENCE_OF(void) asn_anonymous_sequence_; -#define _A_SEQUENCE_FROM_VOID(ptr) ((asn_anonymous_sequence_ *)(ptr)) -#define _A_CSEQUENCE_FROM_VOID(ptr) ((const asn_anonymous_sequence_ *)(ptr)) - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_SEQUENCE_OF_H */ diff --git a/src/codec_utils/E2AP/asn_SET_OF.c b/src/codec_utils/E2AP/asn_SET_OF.c deleted file mode 100644 index 944f2cb8a..000000000 --- a/src/codec_utils/E2AP/asn_SET_OF.c +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -/* - * Add another element into the set. - */ -int -asn_set_add(void *asn_set_of_x, void *ptr) { - asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x); - - if(as == 0 || ptr == 0) { - errno = EINVAL; /* Invalid arguments */ - return -1; - } - - /* - * Make sure there's enough space to insert an element. - */ - if(as->count == as->size) { - int _newsize = as->size ? (as->size << 1) : 4; - void *_new_arr; - _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0])); - if(_new_arr) { - as->array = (void **)_new_arr; - as->size = _newsize; - } else { - /* ENOMEM */ - return -1; - } - } - - as->array[as->count++] = ptr; - - return 0; -} - -void -asn_set_del(void *asn_set_of_x, int number, int _do_free) { - asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x); - - if(as) { - void *ptr; - if(number < 0 || number >= as->count) - return; - - if(_do_free && as->free) { - ptr = as->array[number]; - } else { - ptr = 0; - } - - as->array[number] = as->array[--as->count]; - - /* - * Invoke the third-party function only when the state - * of the parent structure is consistent. - */ - if(ptr) as->free(ptr); - } -} - -/* - * Free the contents of the set, do not free the set itself. - */ -void -asn_set_empty(void *asn_set_of_x) { - asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x); - - if(as) { - if(as->array) { - if(as->free) { - while(as->count--) - as->free(as->array[as->count]); - } - FREEMEM(as->array); - as->array = 0; - } - as->count = 0; - as->size = 0; - } - -} - diff --git a/src/codec_utils/E2AP/asn_SET_OF.h b/src/codec_utils/E2AP/asn_SET_OF.h deleted file mode 100644 index 882e1a47d..000000000 --- a/src/codec_utils/E2AP/asn_SET_OF.h +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_SET_OF_H -#define ASN_SET_OF_H - -#ifdef __cplusplus -#define A_SET_OF(type) \ - struct { \ - type **array; \ - int count; /* Meaningful size */ \ - int size; /* Allocated size */ \ - void (*free)(decltype(*array)); \ - } -#else /* C */ -#define A_SET_OF(type) \ - struct { \ - type **array; \ - int count; /* Meaningful size */ \ - int size; /* Allocated size */ \ - void (*free)(type *); \ - } -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#define ASN_SET_ADD(headptr, ptr) \ - asn_set_add((headptr), (ptr)) - -/******************************************* - * Implementation of the SET OF structure. - */ - -/* - * Add another structure into the set by its pointer. - * RETURN VALUES: - * 0 for success and -1/errno for failure. - */ -int asn_set_add(void *asn_set_of_x, void *ptr); - -/* - * Delete the element from the set by its number (base 0). - * This is a constant-time operation. The order of elements before the - * deleted ones is guaranteed, the order of elements after the deleted - * one is NOT guaranteed. - * If _do_free is given AND the (*free) is initialized, the element - * will be freed using the custom (*free) function as well. - */ -void asn_set_del(void *asn_set_of_x, int number, int _do_free); - -/* - * Empty the contents of the set. Will free the elements, if (*free) is given. - * Will NOT free the set itself. - */ -void asn_set_empty(void *asn_set_of_x); - -/* - * Cope with different conversions requirements to/from void in C and C++. - * This is mostly useful for support library. - */ -typedef A_SET_OF(void) asn_anonymous_set_; -#define _A_SET_FROM_VOID(ptr) ((asn_anonymous_set_ *)(ptr)) -#define _A_CSET_FROM_VOID(ptr) ((const asn_anonymous_set_ *)(ptr)) - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_SET_OF_H */ diff --git a/src/codec_utils/E2AP/asn_application.c b/src/codec_utils/E2AP/asn_application.c deleted file mode 100644 index 2bff46064..000000000 --- a/src/codec_utils/E2AP/asn_application.c +++ /dev/null @@ -1,481 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -static asn_enc_rval_t asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, - const asn_TYPE_descriptor_t *td, - const void *sptr, - asn_app_consume_bytes_f *callback, - void *callback_key); - - -struct callback_count_bytes_key { - asn_app_consume_bytes_f *callback; - void *callback_key; - size_t computed_size; -}; - -/* - * Encoder which just counts bytes that come through it. - */ -static int -callback_count_bytes_cb(const void *data, size_t size, void *keyp) { - struct callback_count_bytes_key *key = keyp; - int ret; - - ret = key->callback(data, size, key->callback_key); - if(ret >= 0) { - key->computed_size += size; - } - - return ret; -} - -struct overrun_encoder_key { - void *buffer; - size_t buffer_size; - size_t computed_size; -}; - -struct dynamic_encoder_key { - void *buffer; - size_t buffer_size; - size_t computed_size; -}; - -struct callback_failure_catch_key { - asn_app_consume_bytes_f *callback; - void *callback_key; - int callback_failed; -}; - -/* - * Encoder which doesn't stop counting bytes - * even if it reaches the end of the buffer. - */ -static int -overrun_encoder_cb(const void *data, size_t size, void *keyp) { - struct overrun_encoder_key *key = keyp; - - if(key->computed_size + size > key->buffer_size) { - /* - * Avoid accident on the next call: - * stop adding bytes to the buffer. - */ - key->buffer_size = 0; - } else { - memcpy((char *)key->buffer + key->computed_size, data, size); - } - key->computed_size += size; - - return 0; -} - -/* - * Encoder which dynamically allocates output, and continues - * to count even if allocation failed. - */ -static int -dynamic_encoder_cb(const void *data, size_t size, void *keyp) { - struct dynamic_encoder_key *key = keyp; - - if(key->buffer) { - if(key->computed_size + size >= key->buffer_size) { - void *p; - size_t new_size = key->buffer_size; - - do { - new_size *= 2; - } while(new_size <= key->computed_size + size); - - p = REALLOC(key->buffer, new_size); - if(p) { - key->buffer = p; - key->buffer_size = new_size; - } else { - FREEMEM(key->buffer); - key->buffer = 0; - key->buffer_size = 0; - key->computed_size += size; - return 0; - } - } - memcpy((char *)key->buffer + key->computed_size, data, size); - } - - key->computed_size += size; - - return 0; -} - -/* - * Encoder which help convert the application level encoder failure into EIO. - */ -static int -callback_failure_catch_cb(const void *data, size_t size, void *keyp) { - struct callback_failure_catch_key *key = keyp; - int ret; - - ret = key->callback(data, size, key->callback_key); - if(ret < 0) { - key->callback_failed = 1; - } - - return ret; -} - -asn_enc_rval_t -asn_encode(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td, - const void *sptr, asn_app_consume_bytes_f *callback, void *callback_key) { - struct callback_failure_catch_key cb_key; - asn_enc_rval_t er = {0,0,0}; - - if(!callback) { - errno = EINVAL; - ASN__ENCODE_FAILED; - } - - cb_key.callback = callback; - cb_key.callback_key = callback_key; - cb_key.callback_failed = 0; - - er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr, - callback_failure_catch_cb, &cb_key); - if(cb_key.callback_failed) { - assert(er.encoded == -1); - assert(errno == EBADF); - errno = EIO; - } - - return er; -} - -asn_enc_rval_t -asn_encode_to_buffer(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, - const asn_TYPE_descriptor_t *td, const void *sptr, - void *buffer, size_t buffer_size) { - struct overrun_encoder_key buf_key; - asn_enc_rval_t er = {0,0,0}; - - if(buffer_size > 0 && !buffer) { - errno = EINVAL; - ASN__ENCODE_FAILED; - } - - buf_key.buffer = buffer; - buf_key.buffer_size = buffer_size; - buf_key.computed_size = 0; - - 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 %" ASN_PRI_SSIZE - " yet produced %" ASN_PRI_SIZE " bytes", - er.encoded, buf_key.computed_size); - assert(er.encoded < 0 || (size_t)er.encoded == buf_key.computed_size); - } - - return er; -} - -asn_encode_to_new_buffer_result_t -asn_encode_to_new_buffer(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, - const asn_TYPE_descriptor_t *td, const void *sptr) { - struct dynamic_encoder_key buf_key; - asn_encode_to_new_buffer_result_t res; - - buf_key.buffer_size = 16; - buf_key.buffer = MALLOC(buf_key.buffer_size); - buf_key.computed_size = 0; - - res.result = asn_encode_internal(opt_codec_ctx, syntax, td, sptr, - dynamic_encoder_cb, &buf_key); - - if(res.result.encoded >= 0 - && (size_t)res.result.encoded != buf_key.computed_size) { - ASN_DEBUG("asn_encode() returned %" ASN_PRI_SSIZE - " yet produced %" ASN_PRI_SIZE " bytes", - res.result.encoded, buf_key.computed_size); - assert(res.result.encoded < 0 - || (size_t)res.result.encoded == buf_key.computed_size); - } - - res.buffer = buf_key.buffer; - - /* 0-terminate just in case. */ - if(res.buffer) { - assert(buf_key.computed_size < buf_key.buffer_size); - ((char *)res.buffer)[buf_key.computed_size] = '\0'; - } - - return res; -} - -static asn_enc_rval_t -asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, - const asn_TYPE_descriptor_t *td, const void *sptr, - asn_app_consume_bytes_f *callback, void *callback_key) { - asn_enc_rval_t er = {0,0,0}; - enum xer_encoder_flags_e xer_flags = XER_F_CANONICAL; - - (void)opt_codec_ctx; /* Parameters are not checked on encode yet. */ - - if(!td || !sptr) { - errno = EINVAL; - ASN__ENCODE_FAILED; - } - - switch(syntax) { - case ATS_NONSTANDARD_PLAINTEXT: - if(td->op->print_struct) { - struct callback_count_bytes_key cb_key; - cb_key.callback = callback; - cb_key.callback_key = callback_key; - cb_key.computed_size = 0; - if(td->op->print_struct(td, sptr, 1, callback_count_bytes_cb, - &cb_key) - < 0 - || callback_count_bytes_cb("\n", 1, &cb_key) < 0) { - errno = EBADF; /* Structure has incorrect form. */ - er.encoded = -1; - er.failed_type = td; - er.structure_ptr = sptr; - } else { - er.encoded = cb_key.computed_size; - er.failed_type = 0; - er.structure_ptr = 0; - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; - - case ATS_RANDOM: - errno = ENOENT; /* Randomization doesn't make sense on output. */ - ASN__ENCODE_FAILED; - - case ATS_BER: - /* BER is a superset of DER. */ - /* Fall through. */ - case ATS_DER: - if(td->op->der_encoder) { - er = der_encode(td, sptr, callback, callback_key); - if(er.encoded == -1) { - if(er.failed_type && er.failed_type->op->der_encoder) { - errno = EBADF; /* Structure has incorrect form. */ - } else { - errno = ENOENT; /* DER is not defined for this type. */ - } - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; - case ATS_CER: - errno = ENOENT; /* Transfer syntax is not defined for any type. */ - ASN__ENCODE_FAILED; - -#ifdef ASN_DISABLE_OER_SUPPORT - case ATS_BASIC_OER: - case ATS_CANONICAL_OER: - errno = ENOENT; /* PER is not defined. */ - ASN__ENCODE_FAILED; - break; -#else /* ASN_DISABLE_OER_SUPPORT */ - case ATS_BASIC_OER: - /* CANONICAL-OER is a superset of BASIC-OER. */ - /* Fall through. */ - case ATS_CANONICAL_OER: - if(td->op->oer_encoder) { - er = oer_encode(td, sptr, callback, callback_key); - if(er.encoded == -1) { - if(er.failed_type && er.failed_type->op->oer_encoder) { - errno = EBADF; /* Structure has incorrect form. */ - } else { - errno = ENOENT; /* OER is not defined for this type. */ - } - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; -#endif /* ASN_DISABLE_OER_SUPPORT */ - -#ifdef ASN_DISABLE_PER_SUPPORT - case ATS_UNALIGNED_BASIC_PER: - case ATS_UNALIGNED_CANONICAL_PER: - case ATS_ALIGNED_BASIC_PER: - case ATS_ALIGNED_CANONICAL_PER: - errno = ENOENT; /* PER is not defined. */ - ASN__ENCODE_FAILED; - break; -#else /* ASN_DISABLE_PER_SUPPORT */ - case ATS_UNALIGNED_BASIC_PER: - /* CANONICAL-UPER is a superset of BASIC-UPER. */ - /* Fall through. */ - case ATS_UNALIGNED_CANONICAL_PER: - if(td->op->uper_encoder) { - er = uper_encode(td, 0, sptr, callback, callback_key); - if(er.encoded == -1) { - if(er.failed_type && er.failed_type->op->uper_encoder) { - errno = EBADF; /* Structure has incorrect form. */ - } else { - errno = ENOENT; /* UPER is not defined for this type. */ - } - } else { - ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded); - if(er.encoded == 0) { - /* Enforce "Complete Encoding" of X.691 #11.1 */ - if(callback("\0", 1, callback_key) < 0) { - errno = EBADF; - ASN__ENCODE_FAILED; - } - er.encoded = 8; /* Exactly 8 zero bits is added. */ - } - /* Convert bits into bytes */ - er.encoded = (er.encoded + 7) >> 3; - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; - case ATS_ALIGNED_BASIC_PER: - /* CANONICAL-APER is a superset of BASIC-APER. */ - /* Fall through. */ - case ATS_ALIGNED_CANONICAL_PER: - if(td->op->aper_encoder) { - er = aper_encode(td, 0, sptr, callback, callback_key); - if(er.encoded == -1) { - if(er.failed_type && er.failed_type->op->aper_encoder) { - errno = EBADF; /* Structure has incorrect form. */ - } else { - errno = ENOENT; /* APER is not defined for this type. */ - } - } else { - ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded); - if(er.encoded == 0) { - /* Enforce "Complete Encoding" of X.691 #11.1 */ - if(callback("\0", 1, callback_key) < 0) { - errno = EBADF; - ASN__ENCODE_FAILED; - } - er.encoded = 8; /* Exactly 8 zero bits is added. */ - } - /* Convert bits into bytes */ - er.encoded = (er.encoded + 7) >> 3; - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; -#endif /* ASN_DISABLE_PER_SUPPORT */ - - case ATS_BASIC_XER: - /* CANONICAL-XER is a superset of BASIC-XER. */ - xer_flags &= ~XER_F_CANONICAL; - xer_flags |= XER_F_BASIC; - /* Fall through. */ - case ATS_CANONICAL_XER: - if(td->op->xer_encoder) { - er = xer_encode(td, sptr, xer_flags, callback, callback_key); - if(er.encoded == -1) { - if(er.failed_type && er.failed_type->op->xer_encoder) { - errno = EBADF; /* Structure has incorrect form. */ - } else { - errno = ENOENT; /* XER is not defined for this type. */ - } - } - } else { - errno = ENOENT; /* Transfer syntax is not defined for this type. */ - ASN__ENCODE_FAILED; - } - break; - - default: - errno = ENOENT; - ASN__ENCODE_FAILED; - } - - return er; -} - -asn_dec_rval_t -asn_decode(const asn_codec_ctx_t *opt_codec_ctx, - enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td, - void **sptr, const void *buffer, size_t size) { - if(!td || !td->op || !sptr || (size && !buffer)) { - ASN__DECODE_FAILED; - } - - switch(syntax) { - case ATS_CER: - case ATS_NONSTANDARD_PLAINTEXT: - default: - errno = ENOENT; - ASN__DECODE_FAILED; - - case ATS_RANDOM: - if(!td->op->random_fill) { - ASN__DECODE_FAILED; - } else { - if(asn_random_fill(td, sptr, 16000) == 0) { - asn_dec_rval_t ret = {RC_OK, 0}; - return ret; - } else { - ASN__DECODE_FAILED; - } - } - break; - - case ATS_DER: - case ATS_BER: - return ber_decode(opt_codec_ctx, td, sptr, buffer, size); - - case ATS_BASIC_OER: - case ATS_CANONICAL_OER: -#ifdef ASN_DISABLE_OER_SUPPORT - errno = ENOENT; - ASN__DECODE_FAILED; -#else - return oer_decode(opt_codec_ctx, td, sptr, buffer, size); -#endif - - case ATS_UNALIGNED_BASIC_PER: - case ATS_UNALIGNED_CANONICAL_PER: -#ifdef ASN_DISABLE_PER_SUPPORT - errno = ENOENT; - ASN__DECODE_FAILED; -#else - return uper_decode_complete(opt_codec_ctx, td, sptr, buffer, size); -#endif - - case ATS_ALIGNED_BASIC_PER: - case ATS_ALIGNED_CANONICAL_PER: -#ifdef ASN_DISABLE_PER_SUPPORT - errno = ENOENT; - ASN__DECODE_FAILED; -#else - return aper_decode_complete(opt_codec_ctx, td, sptr, buffer, size); -#endif - - case ATS_BASIC_XER: - case ATS_CANONICAL_XER: - return xer_decode(opt_codec_ctx, td, sptr, buffer, size); - } -} - diff --git a/src/codec_utils/E2AP/asn_application.h b/src/codec_utils/E2AP/asn_application.h deleted file mode 100644 index 034f64612..000000000 --- a/src/codec_utils/E2AP/asn_application.h +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * Copyright (c) 2004-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -/* - * Application-level ASN.1 callbacks. - */ -#ifndef ASN_APPLICATION_H -#define ASN_APPLICATION_H - -#include "asn_system.h" /* for platform-dependent types */ -#include "asn_codecs.h" /* for ASN.1 codecs specifics */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * A selection of ASN.1 Transfer Syntaxes to use with generalized - * encoders and decoders declared further in this .h file. - */ -enum asn_transfer_syntax { - /* Avoid appearance of a default transfer syntax. */ - ATS_INVALID = 0, - /* Plaintext output (not conforming to any standard), for debugging. */ - ATS_NONSTANDARD_PLAINTEXT, - /* Returns a randomly generated structure. */ - ATS_RANDOM, - /* - * X.690: - * BER: Basic Encoding Rules. - * DER: Distinguished Encoding Rules. - * CER: Canonical Encoding Rules. - * DER and CER are more strict variants of BER. - */ - ATS_BER, - ATS_DER, - ATS_CER, /* Only decoding is supported */ - /* - * X.696: - * OER: Octet Encoding Rules. - * CANONICAL-OER is a more strict variant of BASIC-OER. - */ - ATS_BASIC_OER, - ATS_CANONICAL_OER, - /* - * X.691: - * PER: Packed Encoding Rules. - * CANONICAL-PER is a more strict variant of BASIC-PER. - * NOTE: Produces or consumes a complete encoding (X.691 (08/2015) #11.1). - */ - ATS_UNALIGNED_BASIC_PER, - ATS_UNALIGNED_CANONICAL_PER, - ATS_ALIGNED_BASIC_PER, - ATS_ALIGNED_CANONICAL_PER, - /* - * X.693: - * XER: XML Encoding Rules. - * CANONICAL-XER is a more strict variant of BASIC-XER. - */ - ATS_BASIC_XER, - ATS_CANONICAL_XER -}; - -/* - * A generic encoder for any supported transfer syntax. - * RETURN VALUES: - * The (.encoded) field of the return value is REDEFINED to mean the following: - * >=0: The computed size of the encoded data. Can exceed the (buffer_size). - * -1: Error encoding the structure. See the error code in (errno): - * EINVAL: Incorrect parameters to the function, such as NULLs. - * ENOENT: Encoding transfer syntax is not defined (for this type). - * EBADF: The structure has invalid form or content constraint failed. - * The (.failed_type) and (.structure_ptr) MIGHT be set to the appropriate - * values at the place of failure, if at all possible. - * WARNING: The (.encoded) field of the return value can exceed the buffer_size. - * This is similar to snprintf(3) contract which might return values - * greater than the buffer size. - */ -asn_enc_rval_t asn_encode_to_buffer( - const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */ - enum asn_transfer_syntax, - const struct asn_TYPE_descriptor_s *type_to_encode, - const void *structure_to_encode, void *buffer, size_t buffer_size); - -/* - * A variant of asn_encode_to_buffer() with automatically allocated buffer. - * RETURN VALUES: - * On success, returns a newly allocated (.buffer) containing the whole message. - * The message size is returned in (.result.encoded). - * On failure: - * (.buffer) is NULL, - * (.result.encoded) as in asn_encode_to_buffer(), - * The errno codes as in asn_encode_to_buffer(), plus the following: - * ENOMEM: Memory allocation failed due to system or internal limits. - * The user is responsible for freeing the (.buffer). - */ -typedef struct asn_encode_to_new_buffer_result_s { - void *buffer; /* NULL if failed to encode. */ - asn_enc_rval_t result; -} asn_encode_to_new_buffer_result_t; -asn_encode_to_new_buffer_result_t asn_encode_to_new_buffer( - const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */ - enum asn_transfer_syntax, - const struct asn_TYPE_descriptor_s *type_to_encode, - const void *structure_to_encode); - - -/* - * Generic type of an application-defined callback to return various - * types of data to the application. - * EXPECTED RETURN VALUES: - * -1: Failed to consume bytes. Abort the mission. - * Non-negative return values indicate success, and ignored. - */ -typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size, - void *application_specific_key); - - -/* - * A generic encoder for any supported transfer syntax. - * Returns the comprehensive encoding result descriptor (see asn_codecs.h). - * RETURN VALUES: - * The negative (.encoded) field of the return values is accompanied with the - * following error codes (errno): - * EINVAL: Incorrect parameters to the function, such as NULLs. - * ENOENT: Encoding transfer syntax is not defined (for this type). - * EBADF: The structure has invalid form or content constraint failed. - * EIO: The (callback) has returned negative value during encoding. - */ -asn_enc_rval_t asn_encode( - const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */ - enum asn_transfer_syntax, - const struct asn_TYPE_descriptor_s *type_to_encode, - const void *structure_to_encode, - asn_app_consume_bytes_f *callback, void *callback_key); - - -/* - * A generic decoder for any supported transfer syntax. - */ -asn_dec_rval_t asn_decode( - const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax, - const struct asn_TYPE_descriptor_s *type_to_decode, - void **structure_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size /* Size of that buffer */ -); - - -/* - * A callback of this type is called whenever constraint validation fails - * on some ASN.1 type. See "constraints.h" for more details on constraint - * validation. - * This callback specifies a descriptor of the ASN.1 type which failed - * the constraint check, as well as human readable message on what - * particular constraint has failed. - */ -typedef void (asn_app_constraint_failed_f)(void *application_specific_key, - const struct asn_TYPE_descriptor_s *type_descriptor_which_failed, - const void *structure_which_failed_ptr, - const char *error_message_format, ...) CC_PRINTFLIKE(4, 5); - - -#ifdef __cplusplus -} -#endif - -#include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */ - -#endif /* ASN_APPLICATION_H */ diff --git a/src/codec_utils/E2AP/asn_bit_data.c b/src/codec_utils/E2AP/asn_bit_data.c deleted file mode 100644 index fe4b89ba7..000000000 --- a/src/codec_utils/E2AP/asn_bit_data.c +++ /dev/null @@ -1,333 +0,0 @@ -/* - * Copyright (c) 2005-2017 Lev Walkin . - * All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -/* - * Create a contiguous non-refillable bit data structure. - * Can be freed by FREEMEM(). - */ -asn_bit_data_t * -asn_bit_data_new_contiguous(const void *data, size_t size_bits) { - size_t size_bytes = (size_bits + 7) / 8; - asn_bit_data_t *pd; - uint8_t *bytes; - - /* Get the extensions map */ - pd = CALLOC(1, sizeof(*pd) + size_bytes + 1); - if(!pd) { - return NULL; - } - bytes = (void *)(((char *)pd) + sizeof(*pd)); - memcpy(bytes, data, size_bytes); - bytes[size_bytes] = 0; - pd->buffer = bytes; - pd->nboff = 0; - pd->nbits = size_bits; - - return pd; -} - - -char * -asn_bit_data_string(asn_bit_data_t *pd) { - static char buf[2][32]; - static int n; - n = (n+1) % 2; - snprintf(buf[n], sizeof(buf[n]), - "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE - "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}", - pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits, - pd->nbits - pd->nboff); - return buf[n]; -} - -void -asn_get_undo(asn_bit_data_t *pd, int nbits) { - if((ssize_t)pd->nboff < nbits) { - assert((ssize_t)pd->nboff < nbits); - } else { - pd->nboff -= nbits; - pd->moved -= nbits; - } -} - -/* - * Extract a small number of bits (<= 31) from the specified PER data pointer. - */ -int32_t -asn_get_few_bits(asn_bit_data_t *pd, int nbits) { - size_t off; /* Next after last bit offset */ - ssize_t nleft; /* Number of bits left in this stream */ - uint32_t accum; - const uint8_t *buf; - - if(nbits < 0) - return -1; - - nleft = pd->nbits - pd->nboff; - if(nbits > nleft) { - int32_t tailv, vhead; - if(!pd->refill || nbits > 31) return -1; - /* Accumulate unused bytes before refill */ - ASN_DEBUG("Obtain the rest %d bits (want %d)", - (int)nleft, (int)nbits); - tailv = asn_get_few_bits(pd, nleft); - if(tailv < 0) return -1; - /* Refill (replace pd contents with new data) */ - if(pd->refill(pd)) - return -1; - nbits -= nleft; - vhead = asn_get_few_bits(pd, nbits); - /* Combine the rest of previous pd with the head of new one */ - tailv = (tailv << nbits) | vhead; /* Could == -1 */ - return tailv; - } - - /* - * Normalize position indicator. - */ - if(pd->nboff >= 8) { - pd->buffer += (pd->nboff >> 3); - pd->nbits -= (pd->nboff & ~0x07); - pd->nboff &= 0x07; - } - pd->moved += nbits; - pd->nboff += nbits; - off = pd->nboff; - buf = pd->buffer; - - /* - * Extract specified number of bits. - */ - if(off <= 8) - accum = nbits ? (buf[0]) >> (8 - off) : 0; - else if(off <= 16) - accum = ((buf[0] << 8) + buf[1]) >> (16 - off); - else if(off <= 24) - accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off); - else if(off <= 31) - accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16) - + (buf[2] << 8) + (buf[3])) >> (32 - off); - else if(nbits <= 31) { - asn_bit_data_t tpd = *pd; - /* Here are we with our 31-bits limit plus 1..7 bits offset. */ - asn_get_undo(&tpd, nbits); - /* The number of available bits in the stream allow - * for the following operations to take place without - * invoking the ->refill() function */ - accum = asn_get_few_bits(&tpd, nbits - 24) << 24; - accum |= asn_get_few_bits(&tpd, 24); - } else { - asn_get_undo(pd, nbits); - return -1; - } - - accum &= (((uint32_t)1 << nbits) - 1); - - ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]", - (int)nbits, (int)nleft, - (int)pd->moved, - (((long)pd->buffer) & 0xf), - (int)pd->nboff, (int)pd->nbits, - ((pd->buffer != NULL)?pd->buffer[0]:0), - (int)(pd->nbits - pd->nboff), - (int)accum); - - return accum; -} - -/* - * Extract a large number of bits from the specified PER data pointer. - */ -int -asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) { - int32_t value; - - if(alright && (nbits & 7)) { - /* Perform right alignment of a first few bits */ - value = asn_get_few_bits(pd, nbits & 0x07); - if(value < 0) return -1; - *dst++ = value; /* value is already right-aligned */ - nbits &= ~7; - } - - while(nbits) { - if(nbits >= 24) { - value = asn_get_few_bits(pd, 24); - if(value < 0) return -1; - *(dst++) = value >> 16; - *(dst++) = value >> 8; - *(dst++) = value; - nbits -= 24; - } else { - value = asn_get_few_bits(pd, nbits); - if(value < 0) return -1; - if(nbits & 7) { /* implies left alignment */ - value <<= 8 - (nbits & 7), - nbits += 8 - (nbits & 7); - if(nbits > 24) - *dst++ = value >> 24; - } - if(nbits > 16) - *dst++ = value >> 16; - if(nbits > 8) - *dst++ = value >> 8; - *dst++ = value; - break; - } - } - - return 0; -} - -/* - * Put a small number of bits (<= 31). - */ -int -asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) { - size_t off; /* Next after last bit offset */ - size_t omsk; /* Existing last byte meaningful bits mask */ - uint8_t *buf; - - if(obits <= 0 || obits >= 32) return obits ? -1 : 0; - - ASN_DEBUG("[PER put %d bits %x to %p+%d bits]", - obits, (int)bits, (void *)po->buffer, (int)po->nboff); - - /* - * Normalize position indicator. - */ - if(po->nboff >= 8) { - po->buffer += (po->nboff >> 3); - po->nbits -= (po->nboff & ~0x07); - po->nboff &= 0x07; - } - - /* - * Flush whole-bytes output, if necessary. - */ - if(po->nboff + obits > po->nbits) { - size_t complete_bytes; - if(!po->buffer) po->buffer = po->tmpspace; - complete_bytes = (po->buffer - po->tmpspace); - ASN_DEBUG("[PER output %ld complete + %ld]", - (long)complete_bytes, (long)po->flushed_bytes); - if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) - return -1; - if(po->nboff) - po->tmpspace[0] = po->buffer[0]; - po->buffer = po->tmpspace; - po->nbits = 8 * sizeof(po->tmpspace); - po->flushed_bytes += complete_bytes; - } - - /* - * Now, due to sizeof(tmpspace), we are guaranteed large enough space. - */ - buf = po->buffer; - omsk = ~((1 << (8 - po->nboff)) - 1); - off = (po->nboff + obits); - - /* Clear data of debris before meaningful bits */ - bits &= (((uint32_t)1 << obits) - 1); - - ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits, - (int)bits, (int)bits, - (int)po->nboff, (int)off, - buf[0], (int)(omsk&0xff), - (int)(buf[0] & omsk)); - - if(off <= 8) /* Completely within 1 byte */ - po->nboff = off, - bits <<= (8 - off), - buf[0] = (buf[0] & omsk) | bits; - else if(off <= 16) - po->nboff = off, - bits <<= (16 - off), - buf[0] = (buf[0] & omsk) | (bits >> 8), - buf[1] = bits; - else if(off <= 24) - po->nboff = off, - bits <<= (24 - off), - buf[0] = (buf[0] & omsk) | (bits >> 16), - buf[1] = bits >> 8, - buf[2] = bits; - else if(off <= 31) - po->nboff = off, - bits <<= (32 - off), - buf[0] = (buf[0] & omsk) | (bits >> 24), - buf[1] = bits >> 16, - buf[2] = bits >> 8, - buf[3] = bits; - else { - if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1; - if(asn_put_few_bits(po, bits, obits - 24)) return -1; - } - - ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]", - (int)bits, (int)bits, buf[0], - (long)(po->buffer - po->tmpspace)); - - return 0; -} - - -/* - * Output a large number of bits. - */ -int -asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) { - - while(nbits) { - uint32_t value; - - if(nbits >= 24) { - value = (src[0] << 16) | (src[1] << 8) | src[2]; - src += 3; - nbits -= 24; - if(asn_put_few_bits(po, value, 24)) - return -1; - } else { - value = src[0]; - if(nbits > 8) - value = (value << 8) | src[1]; - if(nbits > 16) - value = (value << 8) | src[2]; - if(nbits & 0x07) - value >>= (8 - (nbits & 0x07)); - if(asn_put_few_bits(po, value, nbits)) - return -1; - break; - } - } - - return 0; -} - - -int -asn_put_aligned_flush(asn_bit_outp_t *po) { - uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07))); - size_t complete_bytes = - (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3); - - if(unused_bits) { - po->buffer[po->nboff >> 3] &= ~0u << unused_bits; - } - - if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) { - return -1; - } else { - po->buffer = po->tmpspace; - po->nboff = 0; - po->nbits = 8 * sizeof(po->tmpspace); - po->flushed_bytes += complete_bytes; - return 0; - } -} - diff --git a/src/codec_utils/E2AP/asn_bit_data.h b/src/codec_utils/E2AP/asn_bit_data.h deleted file mode 100644 index 59de7af5f..000000000 --- a/src/codec_utils/E2AP/asn_bit_data.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2005-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_BIT_DATA -#define ASN_BIT_DATA - -#include /* Platform-specific types */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * This structure describes a position inside an incoming PER bit stream. - */ -typedef struct asn_bit_data_s { - const uint8_t *buffer; /* Pointer to the octet stream */ - size_t nboff; /* Bit offset to the meaningful bit */ - size_t nbits; /* Number of bits in the stream */ - size_t moved; /* Number of bits moved through this bit stream */ - int (*refill)(struct asn_bit_data_s *); - void *refill_key; -} asn_bit_data_t; - -/* - * Create a contiguous non-refillable bit data structure. - * Can be freed by FREEMEM(). - */ -asn_bit_data_t *asn_bit_data_new_contiguous(const void *data, size_t size_bits); - -/* - * Extract a small number of bits (<= 31) from the specified PER data pointer. - * This function returns -1 if the specified number of bits could not be - * extracted due to EOD or other conditions. - */ -int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits); - -/* Undo the immediately preceeding "get_few_bits" operation */ -void asn_get_undo(asn_bit_data_t *, int get_nbits); - -/* - * Extract a large number of bits from the specified PER data pointer. - * This function returns -1 if the specified number of bits could not be - * extracted due to EOD or other conditions. - */ -int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align, - int get_nbits); - -/* Non-thread-safe debugging function, don't use it */ -char *asn_bit_data_string(asn_bit_data_t *); - -/* - * This structure supports forming bit output. - */ -typedef struct asn_bit_outp_s { - uint8_t *buffer; /* Pointer into the (tmpspace) */ - size_t nboff; /* Bit offset to the meaningful bit */ - size_t nbits; /* Number of bits left in (tmpspace) */ - uint8_t tmpspace[32]; /* Preliminary storage to hold data */ - int (*output)(const void *data, size_t size, void *op_key); - void *op_key; /* Key for (output) data callback */ - size_t flushed_bytes; /* Bytes already flushed through (output) */ -} asn_bit_outp_t; - -/* Output a small number of bits (<= 31) */ -int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits); - -/* Output a large number of bits */ -int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits); - -/* - * Flush whole bytes (0 or more) through (outper) member. - * The least significant bits which are not used are guaranteed to be set to 0. - * Returns -1 if callback returns -1. Otherwise, 0. - */ -int asn_put_aligned_flush(asn_bit_outp_t *); - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_BIT_DATA */ diff --git a/src/codec_utils/E2AP/asn_codecs.h b/src/codec_utils/E2AP/asn_codecs.h deleted file mode 100644 index e75c2709c..000000000 --- a/src/codec_utils/E2AP/asn_codecs.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_CODECS_H -#define ASN_CODECS_H - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ - -/* - * This structure defines a set of parameters that may be passed - * to every ASN.1 encoder or decoder function. - * WARNING: if max_stack_size member is set, and you are calling the - * function pointers of the asn_TYPE_descriptor_t directly, - * this structure must be ALLOCATED ON THE STACK! - * If you can't always satisfy this requirement, use ber_decode(), - * xer_decode() and uper_decode() functions instead. - */ -typedef struct asn_codec_ctx_s { - /* - * Limit the decoder routines to use no (much) more stack than a given - * number of bytes. Most of decoders are stack-based, and this - * would protect against stack overflows if the number of nested - * encodings is high. - * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based, - * and are safe from this kind of overflow. - * A value from getrlimit(RLIMIT_STACK) may be used to initialize - * this variable. Be careful in multithreaded environments, as the - * stack size is rather limited. - */ - size_t max_stack_size; /* 0 disables stack bounds checking */ -} asn_codec_ctx_t; - -/* - * Type of the return value of the encoding functions (der_encode, xer_encode). - */ -typedef struct asn_enc_rval_s { - /* - * Number of bytes encoded. - * -1 indicates failure to encode the structure. - * In this case, the members below this one are meaningful. - */ - ssize_t encoded; - - /* - * Members meaningful when (encoded == -1), for post mortem analysis. - */ - - /* Type which cannot be encoded */ - const struct asn_TYPE_descriptor_s *failed_type; - - /* Pointer to the structure of that type */ - const void *structure_ptr; -} asn_enc_rval_t; -#define ASN__ENCODE_FAILED do { \ - asn_enc_rval_t tmp_error; \ - tmp_error.encoded = -1; \ - tmp_error.failed_type = td; \ - tmp_error.structure_ptr = sptr; \ - ASN_DEBUG("Failed to encode element %s", td ? td->name : ""); \ - return tmp_error; \ -} while(0) -#define ASN__ENCODED_OK(rval) do { \ - rval.structure_ptr = 0; \ - rval.failed_type = 0; \ - return rval; \ -} while(0) - -/* - * Type of the return value of the decoding functions (ber_decode, xer_decode) - * - * Please note that the number of consumed bytes is ALWAYS meaningful, - * even if code==RC_FAIL. This is to indicate the number of successfully - * decoded bytes, hence providing a possibility to fail with more diagnostics - * (i.e., print the offending remainder of the buffer). - */ -enum asn_dec_rval_code_e { - RC_OK, /* Decoded successfully */ - RC_WMORE, /* More data expected, call again */ - RC_FAIL /* Failure to decode data */ -}; -typedef struct asn_dec_rval_s { - enum asn_dec_rval_code_e code; /* Result code */ - size_t consumed; /* Number of bytes consumed */ -} asn_dec_rval_t; -#define ASN__DECODE_FAILED do { \ - asn_dec_rval_t tmp_error; \ - tmp_error.code = RC_FAIL; \ - tmp_error.consumed = 0; \ - ASN_DEBUG("Failed to decode element %s", td ? td->name : ""); \ - return tmp_error; \ -} while(0) -#define ASN__DECODE_STARVED do { \ - asn_dec_rval_t tmp_error; \ - tmp_error.code = RC_WMORE; \ - tmp_error.consumed = 0; \ - return tmp_error; \ -} while(0) - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_CODECS_H */ diff --git a/src/codec_utils/E2AP/asn_codecs_prim.c b/src/codec_utils/E2AP/asn_codecs_prim.c deleted file mode 100644 index fc2424773..000000000 --- a/src/codec_utils/E2AP/asn_codecs_prim.c +++ /dev/null @@ -1,317 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -/* - * Decode an always-primitive type. - */ -asn_dec_rval_t -ber_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, - const void *buf_ptr, size_t size, int tag_mode) { - ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr; - asn_dec_rval_t rval; - ber_tlv_len_t length = 0; /* =0 to avoid [incorrect] warning. */ - - /* - * If the structure is not there, allocate it. - */ - if(st == NULL) { - st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st)); - if(st == NULL) ASN__DECODE_FAILED; - *sptr = (void *)st; - } - - ASN_DEBUG("Decoding %s as plain primitive (tm=%d)", - td->name, tag_mode); - - /* - * Check tags and extract value length. - */ - rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, - tag_mode, 0, &length, 0); - if(rval.code != RC_OK) - return rval; - - ASN_DEBUG("%s length is %d bytes", td->name, (int)length); - - /* - * Make sure we have this length. - */ - buf_ptr = ((const char *)buf_ptr) + rval.consumed; - size -= rval.consumed; - if(length > (ber_tlv_len_t)size) { - rval.code = RC_WMORE; - rval.consumed = 0; - return rval; - } - - st->size = (int)length; - /* The following better be optimized away. */ - if(sizeof(st->size) != sizeof(length) - && (ber_tlv_len_t)st->size != length) { - st->size = 0; - ASN__DECODE_FAILED; - } - - st->buf = (uint8_t *)MALLOC(length + 1); - if(!st->buf) { - st->size = 0; - ASN__DECODE_FAILED; - } - - memcpy(st->buf, buf_ptr, length); - st->buf[length] = '\0'; /* Just in case */ - - rval.code = RC_OK; - rval.consumed += length; - - ASN_DEBUG("Took %ld/%ld bytes to encode %s", - (long)rval.consumed, - (long)length, td->name); - - return rval; -} - -/* - * Encode an always-primitive type using DER. - */ -asn_enc_rval_t -der_encode_primitive(const asn_TYPE_descriptor_t *td, const void *sptr, - int tag_mode, ber_tlv_tag_t tag, - asn_app_consume_bytes_f *cb, void *app_key) { - asn_enc_rval_t erval = {0,0,0}; - const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr; - - ASN_DEBUG("%s %s as a primitive type (tm=%d)", - cb?"Encoding":"Estimating", td->name, tag_mode); - - erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag, - cb, app_key); - ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded); - if(erval.encoded == -1) { - erval.failed_type = td; - erval.structure_ptr = sptr; - return erval; - } - - if(cb && st->buf) { - if(cb(st->buf, st->size, app_key) < 0) { - erval.encoded = -1; - erval.failed_type = td; - erval.structure_ptr = sptr; - return erval; - } - } else { - assert(st->buf || st->size == 0); - } - - erval.encoded += st->size; - ASN__ENCODED_OK(erval); -} - -void -ASN__PRIMITIVE_TYPE_free(const asn_TYPE_descriptor_t *td, void *sptr, - enum asn_struct_free_method method) { - ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr; - - if(!td || !sptr) - return; - - ASN_DEBUG("Freeing %s as a primitive type", td->name); - - if(st->buf) - FREEMEM(st->buf); - - switch(method) { - case ASFM_FREE_EVERYTHING: - FREEMEM(sptr); - break; - case ASFM_FREE_UNDERLYING: - break; - case ASFM_FREE_UNDERLYING_AND_RESET: - memset(sptr, 0, sizeof(ASN__PRIMITIVE_TYPE_t)); - break; - } -} - - -/* - * Local internal type passed around as an argument. - */ -struct xdp_arg_s { - const asn_TYPE_descriptor_t *type_descriptor; - void *struct_key; - xer_primitive_body_decoder_f *prim_body_decoder; - int decoded_something; - int want_more; -}; - -/* - * Since some kinds of primitive values can be encoded using value-specific - * tags (, , etc), the primitive decoder must - * be supplied with such tags to parse them as needed. - */ -static int -xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) { - struct xdp_arg_s *arg = (struct xdp_arg_s *)key; - enum xer_pbd_rval bret; - - /* - * The chunk_buf is guaranteed to start at '<'. - */ - assert(chunk_size && ((const char *)chunk_buf)[0] == 0x3c); - - /* - * Decoding was performed once already. Prohibit doing it again. - */ - if(arg->decoded_something) - return -1; - - bret = arg->prim_body_decoder(arg->type_descriptor, - arg->struct_key, chunk_buf, chunk_size); - switch(bret) { - case XPBD_SYSTEM_FAILURE: - case XPBD_DECODER_LIMIT: - case XPBD_BROKEN_ENCODING: - break; - case XPBD_BODY_CONSUMED: - /* Tag decoded successfully */ - arg->decoded_something = 1; - /* Fall through */ - case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */ - return 0; - } - - return -1; -} - -static ssize_t -xer_decode__primitive_body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) { - struct xdp_arg_s *arg = (struct xdp_arg_s *)key; - enum xer_pbd_rval bret; - size_t lead_wsp_size; - - if(arg->decoded_something) { - if(xer_whitespace_span(chunk_buf, chunk_size) == chunk_size) { - /* - * Example: - * "123 " - * ^- chunk_buf position. - */ - return chunk_size; - } - /* - * Decoding was done once already. Prohibit doing it again. - */ - return -1; - } - - if(!have_more) { - /* - * If we've received something like "1", we can't really - * tell whether it is really `1` or `123`, until we know - * that there is no more data coming. - * The have_more argument will be set to 1 once something - * like this is available to the caller of this callback: - * "1want_more = 1; - return -1; - } - - lead_wsp_size = xer_whitespace_span(chunk_buf, chunk_size); - chunk_buf = (const char *)chunk_buf + lead_wsp_size; - chunk_size -= lead_wsp_size; - - bret = arg->prim_body_decoder(arg->type_descriptor, - arg->struct_key, chunk_buf, chunk_size); - switch(bret) { - case XPBD_SYSTEM_FAILURE: - case XPBD_DECODER_LIMIT: - case XPBD_BROKEN_ENCODING: - break; - case XPBD_BODY_CONSUMED: - /* Tag decoded successfully */ - arg->decoded_something = 1; - /* Fall through */ - case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */ - return lead_wsp_size + chunk_size; - } - - return -1; -} - - -asn_dec_rval_t -xer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, - size_t struct_size, const char *opt_mname, - const void *buf_ptr, size_t size, - xer_primitive_body_decoder_f *prim_body_decoder) { - const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; - asn_struct_ctx_t s_ctx; - struct xdp_arg_s s_arg; - asn_dec_rval_t rc; - - /* - * Create the structure if does not exist. - */ - if(!*sptr) { - *sptr = CALLOC(1, struct_size); - if(!*sptr) ASN__DECODE_FAILED; - } - - memset(&s_ctx, 0, sizeof(s_ctx)); - s_arg.type_descriptor = td; - s_arg.struct_key = *sptr; - s_arg.prim_body_decoder = prim_body_decoder; - s_arg.decoded_something = 0; - s_arg.want_more = 0; - - rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg, - xml_tag, buf_ptr, size, - xer_decode__unexpected_tag, xer_decode__primitive_body); - switch(rc.code) { - case RC_OK: - if(!s_arg.decoded_something) { - char ch; - ASN_DEBUG("Primitive body is not recognized, " - "supplying empty one"); - /* - * Decoding opportunity has come and gone. - * Where's the result? - * Try to feed with empty body, see if it eats it. - */ - if(prim_body_decoder(s_arg.type_descriptor, - s_arg.struct_key, &ch, 0) - != XPBD_BODY_CONSUMED) { - /* - * This decoder does not like empty stuff. - */ - ASN__DECODE_FAILED; - } - } - break; - case RC_WMORE: - /* - * Redo the whole thing later. - * We don't have a context to save intermediate parsing state. - */ - rc.consumed = 0; - break; - case RC_FAIL: - rc.consumed = 0; - if(s_arg.want_more) - rc.code = RC_WMORE; - else - ASN__DECODE_FAILED; - break; - } - return rc; -} - diff --git a/src/codec_utils/E2AP/asn_codecs_prim.h b/src/codec_utils/E2AP/asn_codecs_prim.h deleted file mode 100644 index fbc557648..000000000 --- a/src/codec_utils/E2AP/asn_codecs_prim.h +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * Copyright (c) 2004-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_CODECS_PRIM_H -#define ASN_CODECS_PRIM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct ASN__PRIMITIVE_TYPE_s { - uint8_t *buf; /* Buffer with consecutive primitive encoding bytes */ - size_t size; /* Size of the buffer */ -} ASN__PRIMITIVE_TYPE_t; /* Do not use this type directly! */ - -asn_struct_free_f ASN__PRIMITIVE_TYPE_free; -ber_type_decoder_f ber_decode_primitive; -der_type_encoder_f der_encode_primitive; - -/* - * A callback specification for the xer_decode_primitive() function below. - */ -enum xer_pbd_rval { - XPBD_SYSTEM_FAILURE, /* System failure (memory shortage, etc) */ - XPBD_DECODER_LIMIT, /* Hit some decoder limitation or deficiency */ - XPBD_BROKEN_ENCODING, /* Encoding of a primitive body is broken */ - XPBD_NOT_BODY_IGNORE, /* Not a body format, but safe to ignore */ - XPBD_BODY_CONSUMED /* Body is recognized and consumed */ -}; -typedef enum xer_pbd_rval(xer_primitive_body_decoder_f)( - const asn_TYPE_descriptor_t *td, void *struct_ptr, const void *chunk_buf, - size_t chunk_size); - -/* - * Specific function to decode simple primitive types. - * Also see xer_decode_general() in xer_decoder.h - */ -asn_dec_rval_t xer_decode_primitive( - const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr, - size_t struct_size, const char *opt_mname, const void *buf_ptr, size_t size, - xer_primitive_body_decoder_f *prim_body_decoder); - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_CODECS_PRIM_H */ diff --git a/src/codec_utils/E2AP/asn_internal.c b/src/codec_utils/E2AP/asn_internal.c deleted file mode 100644 index 1aff95f80..000000000 --- a/src/codec_utils/E2AP/asn_internal.c +++ /dev/null @@ -1,48 +0,0 @@ -#include - -ssize_t -asn__format_to_callback(int (*cb)(const void *, size_t, void *key), void *key, - const char *fmt, ...) { - char scratch[64]; - char *buf = scratch; - size_t buf_size = sizeof(scratch); - int wrote; - int cb_ret; - - do { - va_list args; - va_start(args, fmt); - - wrote = vsnprintf(buf, buf_size, fmt, args); - if(wrote < (ssize_t)buf_size) { - if(wrote < 0) { - if(buf != scratch) FREEMEM(buf); - va_end(args); - return -1; - } - break; - } - - buf_size <<= 1; - if(buf == scratch) { - buf = MALLOC(buf_size); - if(!buf) return -1; - } else { - void *p = REALLOC(buf, buf_size); - if(!p) { - FREEMEM(buf); - return -1; - } - buf = p; - } - } while(1); - - cb_ret = cb(buf, wrote, key); - if(buf != scratch) FREEMEM(buf); - if(cb_ret < 0) { - return -1; - } - - return wrote; -} - diff --git a/src/codec_utils/E2AP/asn_internal.h b/src/codec_utils/E2AP/asn_internal.h deleted file mode 100644 index b4c580db9..000000000 --- a/src/codec_utils/E2AP/asn_internal.h +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -/* - * Declarations internally useful for the ASN.1 support code. - */ -#ifndef ASN_INTERNAL_H -#define ASN_INTERNAL_H -#define __EXTENSIONS__ /* for Sun */ - -#include "asn_application.h" /* Application-visible API */ - -#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */ -#include /* for assert() macro */ -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* Environment version might be used to avoid running with the old library */ -#define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */ -int get_asn1c_environment_version(void); /* Run-time version */ - -#define CALLOC(nmemb, size) calloc(nmemb, size) -#define MALLOC(size) malloc(size) -#define REALLOC(oldptr, size) realloc(oldptr, size) -#define FREEMEM(ptr) free(ptr) - -#define asn_debug_indent 0 -#define ASN_DEBUG_INDENT_ADD(i) do{}while(0) - -#ifdef EMIT_ASN_DEBUG -#warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG" -#define ASN_EMIT_DEBUG EMIT_ASN_DEBUG -#endif - -/* - * A macro for debugging the ASN.1 internals. - * You may enable or override it. - */ -#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */ -#if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */ -#if !defined(BELL_LABS) /* Bell Labs */ - //#if __STDC_VERSION__ >= 199901L -#ifdef ASN_THREAD_SAFE -/* Thread safety requires sacrifice in output indentation: - * Retain empty definition of ASN_DEBUG_INDENT_ADD. */ -#else /* !ASN_THREAD_SAFE */ -#undef ASN_DEBUG_INDENT_ADD -#undef asn_debug_indent -int asn_debug_indent; -#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0) -#endif /* ASN_THREAD_SAFE */ -#if defined(BELL_LABS) /* Bell Labs version */ -extern int logAsn1c(const char *filename, int linenumber, const char *format, ...); -#define ASN_DEBUG(fmt, args...) do { \ - (void) logAsn1c(__FILE__, __LINE__, fmt, ##args); \ - } while(0) -#else -#define ASN_DEBUG(fmt, args...) do { \ - int adi = asn_debug_indent; \ - while(adi--) fprintf(stderr, " "); \ - fprintf(stderr, fmt, ##args); \ - fprintf(stderr, " (%s:%d)\n", \ - __FILE__, __LINE__); \ - } while(0) -#endif /* BELL_LABS */ -#else /* !C99 */ -void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...); -#define ASN_DEBUG ASN_DEBUG_f -#endif /* C99 */ -#else /* ASN_EMIT_DEBUG != 1 */ -#if __STDC_VERSION__ >= 199901L -#if 1 -#define ASN_DEBUG(...) do{}while(0) -#else -#define ASN_DEBUG(fmt, args...) do { \ - (void) logAsn1c(__FILE__, __LINE__, fmt, ##args); \ - } while(0) -#endif -#else /* not C99 */ -static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; } -#endif /* C99 or better */ -#endif /* ASN_EMIT_DEBUG */ -#endif /* ASN_DEBUG */ - -/* - * Print to a callback. - * The callback is expected to return negative values on error. - * 0 and positive values are treated as success. - * RETURN VALUES: - * -1: Failed to format or invoke the callback. - * >0: Size of the data that got delivered to the callback. - */ -ssize_t CC_PRINTFLIKE(3, 4) -asn__format_to_callback( - int (*callback)(const void *, size_t, void *key), void *key, - const char *fmt, ...); - -/* - * Invoke the application-supplied callback and fail, if something is wrong. - */ -#define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0) -#define ASN__E_CALLBACK(size, foo) \ - do { \ - if(foo) goto cb_failed; \ - er.encoded += (size); \ - } while(0) -#define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size)) -#define ASN__CALLBACK2(buf1, size1, buf2, size2) \ - ASN__E_CALLBACK((size1) + (size2), \ - ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2)) -#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \ - ASN__E_CALLBACK((size1) + (size2) + (size3), \ - ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \ - || ASN__E_cbc(buf3, size3)) - -#define ASN__TEXT_INDENT(nl, level) \ - do { \ - int tmp_level = (level); \ - int tmp_nl = ((nl) != 0); \ - int tmp_i; \ - if(tmp_nl) ASN__CALLBACK("\n", 1); \ - if(tmp_level < 0) tmp_level = 0; \ - for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \ - } while(0) - -#define _i_INDENT(nl) do { \ - int tmp_i; \ - if((nl) && cb("\n", 1, app_key) < 0) \ - return -1; \ - for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \ - if(cb(" ", 4, app_key) < 0) \ - return -1; \ - } while(0) - -/* - * Check stack against overflow, if limit is set. - */ -#define ASN__DEFAULT_STACK_MAX (30000) -static int CC_NOTUSED -ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) { - if(ctx && ctx->max_stack_size) { - - /* ctx MUST be allocated on the stack */ - ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx); - if(usedstack > 0) usedstack = -usedstack; /* grows up! */ - - /* double negative required to avoid int wrap-around */ - if(usedstack < -(ptrdiff_t)ctx->max_stack_size) { - ASN_DEBUG("Stack limit %ld reached", - (long)ctx->max_stack_size); - return -1; - } - } - return 0; -} - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_INTERNAL_H */ diff --git a/src/codec_utils/E2AP/asn_ioc.h b/src/codec_utils/E2AP/asn_ioc.h deleted file mode 100644 index 7de210ee0..000000000 --- a/src/codec_utils/E2AP/asn_ioc.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Run-time support for Information Object Classes. - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_IOC_H -#define ASN_IOC_H - -#include /* Platform-specific types */ - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; -struct asn_ioc_cell_s; - -/* - * X.681, #13 - */ -typedef struct asn_ioc_set_s { - size_t rows_count; - size_t columns_count; - const struct asn_ioc_cell_s *rows; -} asn_ioc_set_t; - - -typedef struct asn_ioc_cell_s { - const char *field_name; /* Is equal to corresponding column_name */ - enum { - aioc__undefined = 0, - aioc__value, - aioc__type, - aioc__open_type, - } cell_kind; - struct asn_TYPE_descriptor_s *type_descriptor; - const void *value_sptr; - struct { - size_t types_count; - struct { - unsigned choice_position; - } *types; - } open_type; -} asn_ioc_cell_t; - - -#ifdef __cplusplus -} -#endif - -#endif /* ASN_IOC_H */ diff --git a/src/codec_utils/E2AP/asn_random_fill.c b/src/codec_utils/E2AP/asn_random_fill.c deleted file mode 100644 index 819cf700e..000000000 --- a/src/codec_utils/E2AP/asn_random_fill.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . - * All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -int -asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr, - size_t length) { - - if(td && td->op->random_fill) { - asn_random_fill_result_t res = - td->op->random_fill(td, struct_ptr, 0, length); - return (res.code == ARFILL_OK) ? 0 : -1; - } else { - return -1; - } -} - -static uintmax_t -asn__intmax_range(intmax_t lb, intmax_t ub) { - assert(lb <= ub); - if((ub < 0) == (lb < 0)) { - return ub - lb; - } else if(lb < 0) { - return 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1)); - } else { - assert(!"Unreachable"); - return 0; - } -} - -intmax_t -asn_random_between(intmax_t lb, intmax_t rb) { - if(lb == rb) { - return lb; - } else { - const uintmax_t intmax_max = ((~(uintmax_t)0) >> 1); - uintmax_t range = asn__intmax_range(lb, rb); - uintmax_t value = 0; - uintmax_t got_entropy = 0; - - assert(RAND_MAX > 0xffffff); /* Seen 7ffffffd! */ - assert(range < intmax_max); - - for(; got_entropy < range;) { - got_entropy = (got_entropy << 24) | 0xffffff; - value = (value << 24) | (random() % 0xffffff); - } - - return lb + (intmax_t)(value % (range + 1)); - } -} diff --git a/src/codec_utils/E2AP/asn_random_fill.h b/src/codec_utils/E2AP/asn_random_fill.h deleted file mode 100644 index 47f9b8af0..000000000 --- a/src/codec_utils/E2AP/asn_random_fill.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef ASN_RANDOM_FILL -#define ASN_RANDOM_FILL - -/* Forward declarations */ -struct asn_TYPE_descriptor_s; -struct asn_encoding_constraints_s; - -/* - * Initialize a structure with random data according to the type specification - * and optional member constraints. - * ARGUMENTS: - * (max_length) - See (approx_max_length_limit). - * (memb_constraints) - Member constraints, if exist. - * The type can be constrained differently according - * to PER and OER specifications, so we find a value - * at the intersection of these constraints. - * In case the return differs from ARFILL_OK, the (struct_ptr) contents - * and (current_length) value remain in their original state. - */ -typedef struct asn_random_fill_result_s { - enum { - ARFILL_FAILED = -1, /* System error (memory?) */ - ARFILL_OK = 0, /* Initialization succeeded */ - ARFILL_SKIPPED = 1 /* Not done due to (length?) constraint */ - } code; - size_t length; /* Approximate number of bytes created. */ -} asn_random_fill_result_t; -typedef asn_random_fill_result_t(asn_random_fill_f)( - const struct asn_TYPE_descriptor_s *td, void **struct_ptr, - const struct asn_encoding_constraints_s *memb_constraints, - size_t max_length); - -/* - * Returns 0 if the structure was properly initialized, -1 otherwise. - * The (approx_max_length_limit) specifies the approximate limit of the - * resulting structure in units closely resembling bytes. The actual result - * might be several times larger or smaller than the length limit. - */ -int asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr, - size_t approx_max_length_limit); - -/* - * Returns a random number between min and max. - */ -intmax_t asn_random_between(intmax_t min, intmax_t max); - -#endif /* ASN_RANDOM_FILL */ diff --git a/src/codec_utils/E2AP/asn_system.h b/src/codec_utils/E2AP/asn_system.h deleted file mode 100644 index fa8cf1165..000000000 --- a/src/codec_utils/E2AP/asn_system.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -/* - * Miscellaneous system-dependent types. - */ -#ifndef ASN_SYSTEM_H -#define ASN_SYSTEM_H - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifndef _DEFAULT_SOURCE -#define _DEFAULT_SOURCE 1 -#endif - -#ifndef _BSD_SOURCE -#define _BSD_SOURCE /* for snprintf() on some linux systems */ -#endif - -#include /* For snprintf(3) */ -#include /* For *alloc(3) */ -#include /* For memcpy(3) */ -#include /* For size_t */ -#include /* For LONG_MAX */ -#include /* For va_start */ -#include /* for offsetof and ptrdiff_t */ - -#ifdef _WIN32 - -#include -#define snprintf _snprintf -#define vsnprintf _vsnprintf - -/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */ -#define sys_ntohl(l) ((((l) << 24) & 0xff000000) \ - | (((l) << 8) & 0xff0000) \ - | (((l) >> 8) & 0xff00) \ - | ((l >> 24) & 0xff)) - -#ifdef _MSC_VER /* MSVS.Net */ -#ifndef __cplusplus -#define inline __inline -#endif -#ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */ -#define ssize_t SSIZE_T -#if _MSC_VER < 1600 -typedef char int8_t; -typedef short int16_t; -typedef int int32_t; -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -#else /* _MSC_VER >= 1600 */ -#include -#endif /* _MSC_VER < 1600 */ -#endif /* ASSUMESTDTYPES */ -#define WIN32_LEAN_AND_MEAN -#include -#include -#define isnan _isnan -#define finite _finite -#define copysign _copysign -#define ilogb _logb -#else /* !_MSC_VER */ -#include -#endif /* _MSC_VER */ - -#else /* !_WIN32 */ - -#if defined(__vxworks) -#include -#else /* !defined(__vxworks) */ - -#include /* C99 specifies this file */ -#include /* for ntohl() */ -#define sys_ntohl(foo) ntohl(foo) -#endif /* defined(__vxworks) */ - -#endif /* _WIN32 */ - -#if __GNUC__ >= 3 || defined(__clang__) -#define CC_ATTRIBUTE(attr) __attribute__((attr)) -#else -#define CC_ATTRIBUTE(attr) -#endif -#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var)) -#define CC_NOTUSED CC_ATTRIBUTE(unused) -#ifndef CC_ATTR_NO_SANITIZE -#define CC_ATTR_NO_SANITIZE(what) CC_ATTRIBUTE(no_sanitize(what)) -#endif - -/* Figure out if thread safety is requested */ -#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT)) -#define ASN_THREAD_SAFE -#endif /* Thread safety */ - -#ifndef offsetof /* If not defined by */ -#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0)) -#endif /* offsetof */ - -#ifndef MIN /* Suitable for comparing primitive types (integers) */ -#if defined(__GNUC__) -#define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \ - ((_a)<(_b)?(_a):(_b)); }) -#else /* !__GNUC__ */ -#define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */ -#endif /* __GNUC__ */ -#endif /* MIN */ - -#if __STDC_VERSION__ >= 199901L -#ifndef SIZE_MAX -#define SIZE_MAX ((~((size_t)0)) >> 1) -#endif - -#ifndef RSIZE_MAX /* C11, Annex K */ -#define RSIZE_MAX (SIZE_MAX >> 1) -#endif -#ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */ -#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) -#endif -#else /* Old compiler */ -#undef SIZE_MAX -#undef RSIZE_MAX -#undef RSSIZE_MAX -#define SIZE_MAX ((~((size_t)0)) >> 1) -#define RSIZE_MAX (SIZE_MAX >> 1) -#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) -#endif - -#if __STDC_VERSION__ >= 199901L -#define ASN_PRI_SIZE "zu" -#define ASN_PRI_SSIZE "zd" -#define ASN_PRIuMAX PRIuMAX -#define ASN_PRIdMAX PRIdMAX -#else -#define ASN_PRI_SIZE "lu" -#define ASN_PRI_SSIZE "ld" -#if LLONG_MAX > LONG_MAX -#define ASN_PRIuMAX "llu" -#define ASN_PRIdMAX "lld" -#else -#define ASN_PRIuMAX "lu" -#define ASN_PRIdMAX "ld" -#endif -#endif - -#endif /* ASN_SYSTEM_H */ diff --git a/src/codec_utils/E2AP/ber_decoder.c b/src/codec_utils/E2AP/ber_decoder.c deleted file mode 100644 index 75d601695..000000000 --- a/src/codec_utils/E2AP/ber_decoder.c +++ /dev/null @@ -1,283 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include - -#undef ADVANCE -#define ADVANCE(num_bytes) do { \ - size_t num = num_bytes; \ - ptr = ((const char *)ptr) + num; \ - size -= num; \ - consumed_myself += num; \ - } while(0) -#undef RETURN -#define RETURN(_code) do { \ - asn_dec_rval_t rval; \ - rval.code = _code; \ - if(opt_ctx) opt_ctx->step = step; /* Save context */ \ - if(_code == RC_OK || opt_ctx) \ - rval.consumed = consumed_myself; \ - else \ - rval.consumed = 0; /* Context-free */ \ - return rval; \ - } while(0) - -/* - * The BER decoder of any type. - */ -asn_dec_rval_t -ber_decode(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr, - const void *ptr, size_t size) { - asn_codec_ctx_t s_codec_ctx; - - /* - * Stack checker requires that the codec context - * must be allocated on the stack. - */ - if(opt_codec_ctx) { - if(opt_codec_ctx->max_stack_size) { - s_codec_ctx = *opt_codec_ctx; - opt_codec_ctx = &s_codec_ctx; - } - } else { - /* If context is not given, be security-conscious anyway */ - memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); - s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; - opt_codec_ctx = &s_codec_ctx; - } - - /* - * Invoke type-specific decoder. - */ - return type_descriptor->op->ber_decoder(opt_codec_ctx, type_descriptor, - struct_ptr, /* Pointer to the destination structure */ - ptr, size, /* Buffer and its size */ - 0 /* Default tag mode is 0 */ - ); -} - -/* - * Check the set of >> tags matches the definition. - */ -asn_dec_rval_t -ber_check_tags(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx, - const void *ptr, size_t size, int tag_mode, int last_tag_form, - ber_tlv_len_t *last_length, int *opt_tlv_form) { - ssize_t consumed_myself = 0; - ssize_t tag_len; - ssize_t len_len; - ber_tlv_tag_t tlv_tag; - ber_tlv_len_t tlv_len; - ber_tlv_len_t limit_len = -1; - int expect_00_terminators = 0; - int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */ - int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */ - int tagno; - - /* - * Make sure we didn't exceed the maximum stack size. - */ - if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) - RETURN(RC_FAIL); - - /* - * So what does all this implicit skip stuff mean? - * Imagine two types, - * A ::= [5] IMPLICIT T - * B ::= [2] EXPLICIT T - * Where T is defined as - * T ::= [4] IMPLICIT SEQUENCE { ... } - * - * Let's say, we are starting to decode type A, given the - * following TLV stream: <5> <0>. What does this mean? - * It means that the type A contains type T which is, - * in turn, empty. - * Remember though, that we are still in A. We cannot - * just pass control to the type T decoder. Why? Because - * the type T decoder expects <4> <0>, not <5> <0>. - * So, we must make sure we are going to receive <5> while - * still in A, then pass control to the T decoder, indicating - * that the tag <4> was implicitly skipped. The decoder of T - * hence will be prepared to treat <4> as valid tag, and decode - * it appropriately. - */ - - tagno = step /* Continuing where left previously */ - + (tag_mode==1?-1:0) - ; - ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)", - td->name, (long)size, tag_mode, step, tagno); - /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */ - - if(tag_mode == 0 && tagno == (int)td->tags_count) { - /* - * This must be the _untagged_ ANY type, - * which outermost tag isn't known in advance. - * Fetch the tag and length separately. - */ - tag_len = ber_fetch_tag(ptr, size, &tlv_tag); - switch(tag_len) { - case -1: RETURN(RC_FAIL); - case 0: RETURN(RC_WMORE); - } - tlv_constr = BER_TLV_CONSTRUCTED(ptr); - len_len = ber_fetch_length(tlv_constr, - (const char *)ptr + tag_len, size - tag_len, &tlv_len); - switch(len_len) { - case -1: RETURN(RC_FAIL); - case 0: RETURN(RC_WMORE); - } - ASN_DEBUG("Advancing %ld in ANY case", - (long)(tag_len + len_len)); - ADVANCE(tag_len + len_len); - } else { - assert(tagno < (int)td->tags_count); /* At least one loop */ - } - for((void)tagno; tagno < (int)td->tags_count; tagno++, step++) { - - /* - * Fetch and process T from TLV. - */ - tag_len = ber_fetch_tag(ptr, size, &tlv_tag); - ASN_DEBUG("Fetching tag from {%p,%ld}: " - "len %ld, step %d, tagno %d got %s", - ptr, (long)size, - (long)tag_len, step, tagno, - ber_tlv_tag_string(tlv_tag)); - switch(tag_len) { - case -1: RETURN(RC_FAIL); - case 0: RETURN(RC_WMORE); - } - - tlv_constr = BER_TLV_CONSTRUCTED(ptr); - - /* - * If {I}, don't check anything. - * If {I,B,C}, check B and C unless we're at I. - */ - if(tag_mode != 0 && step == 0) { - /* - * We don't expect tag to match here. - * It's just because we don't know how the tag - * is supposed to look like. - */ - } else { - assert(tagno >= 0); /* Guaranteed by the code above */ - if(tlv_tag != td->tags[tagno]) { - /* - * Unexpected tag. Too bad. - */ - ASN_DEBUG("Expected: %s, " - "expectation failed (tn=%d, tm=%d)", - ber_tlv_tag_string(td->tags[tagno]), - tagno, tag_mode - ); - RETURN(RC_FAIL); - } - } - - /* - * Attention: if there are more tags expected, - * ensure that the current tag is presented - * in constructed form (it contains other tags!). - * If this one is the last one, check that the tag form - * matches the one given in descriptor. - */ - if(tagno < ((int)td->tags_count - 1)) { - if(tlv_constr == 0) { - ASN_DEBUG("tlv_constr = %d, expfail", - tlv_constr); - RETURN(RC_FAIL); - } - } else { - if(last_tag_form != tlv_constr - && last_tag_form != -1) { - ASN_DEBUG("last_tag_form %d != %d", - last_tag_form, tlv_constr); - RETURN(RC_FAIL); - } - } - - /* - * Fetch and process L from TLV. - */ - len_len = ber_fetch_length(tlv_constr, - (const char *)ptr + tag_len, size - tag_len, &tlv_len); - ASN_DEBUG("Fetching len = %ld", (long)len_len); - switch(len_len) { - case -1: RETURN(RC_FAIL); - case 0: RETURN(RC_WMORE); - } - - /* - * FIXME - * As of today, the chain of tags - * must either contain several indefinite length TLVs, - * or several definite length ones. - * No mixing is allowed. - */ - if(tlv_len == -1) { - /* - * Indefinite length. - */ - if(limit_len == -1) { - expect_00_terminators++; - } else { - ASN_DEBUG("Unexpected indefinite length " - "in a chain of definite lengths"); - RETURN(RC_FAIL); - } - ADVANCE(tag_len + len_len); - continue; - } else { - if(expect_00_terminators) { - ASN_DEBUG("Unexpected definite length " - "in a chain of indefinite lengths"); - RETURN(RC_FAIL); - } - } - - /* - * Check that multiple TLVs specify ever decreasing length, - * which is consistent. - */ - if(limit_len == -1) { - limit_len = tlv_len + tag_len + len_len; - if(limit_len < 0) { - /* Too great tlv_len value? */ - RETURN(RC_FAIL); - } - } else if(limit_len != tlv_len + tag_len + len_len) { - /* - * Inner TLV specifies length which is inconsistent - * with the outer TLV's length value. - */ - ASN_DEBUG("Outer TLV is %ld and inner is %ld", - (long)limit_len, (long)tlv_len); - RETURN(RC_FAIL); - } - - ADVANCE(tag_len + len_len); - - limit_len -= (tag_len + len_len); - if((ssize_t)size > limit_len) { - /* - * Make sure that we won't consume more bytes - * from the parent frame than the inferred limit. - */ - size = limit_len; - } - } - - if(opt_tlv_form) - *opt_tlv_form = tlv_constr; - if(expect_00_terminators) - *last_length = -expect_00_terminators; - else - *last_length = tlv_len; - - RETURN(RC_OK); -} diff --git a/src/codec_utils/E2AP/ber_decoder.h b/src/codec_utils/E2AP/ber_decoder.h deleted file mode 100644 index 1ac2a5ef0..000000000 --- a/src/codec_utils/E2AP/ber_decoder.h +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _BER_DECODER_H_ -#define _BER_DECODER_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ -struct asn_codec_ctx_s; /* Forward declaration */ - -/* - * The BER decoder of any type. - * This function may be invoked directly from the application. - * Decodes BER, DER and CER data (DER and CER are different subsets of BER). - * - * NOTE: Use the der_encode() function (der_encoder.h) to produce encoding - * which is compliant with ber_decode(). - */ -asn_dec_rval_t ber_decode( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size /* Size of that buffer */ -); - -/* - * Type of generic function which decodes the byte stream into the structure. - */ -typedef asn_dec_rval_t(ber_type_decoder_f)( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, void **struct_ptr, - const void *buf_ptr, size_t size, int tag_mode); - -/******************************* - * INTERNALLY USEFUL FUNCTIONS * - *******************************/ - -/* - * Check that all tags correspond to the type definition (as given in head). - * On return, last_length would contain either a non-negative length of the - * value part of the last TLV, or the negative number of expected - * "end of content" sequences. The number may only be negative if the - * head->last_tag_form is non-zero. - */ -asn_dec_rval_t ber_check_tags( - const struct asn_codec_ctx_s *opt_codec_ctx, /* codec options */ - const struct asn_TYPE_descriptor_s *type_descriptor, - asn_struct_ctx_t *opt_ctx, /* saved decoding context */ - const void *ptr, size_t size, - int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */ - int last_tag_form, /* {-1,0:1}: any, primitive, constr */ - ber_tlv_len_t *last_length, int *opt_tlv_form /* optional tag form */ -); - -#ifdef __cplusplus -} -#endif - -#endif /* _BER_DECODER_H_ */ diff --git a/src/codec_utils/E2AP/ber_tlv_length.c b/src/codec_utils/E2AP/ber_tlv_length.c deleted file mode 100644 index 0a0deec1a..000000000 --- a/src/codec_utils/E2AP/ber_tlv_length.c +++ /dev/null @@ -1,168 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -ssize_t -ber_fetch_length(int _is_constructed, const void *bufptr, size_t size, - ber_tlv_len_t *len_r) { - const uint8_t *buf = (const uint8_t *)bufptr; - unsigned oct; - - if(size == 0) - return 0; /* Want more */ - - oct = *(const uint8_t *)buf; - if((oct & 0x80) == 0) { - /* - * Short definite length. - */ - *len_r = oct; /* & 0x7F */ - return 1; - } else { - ber_tlv_len_t len; - size_t skipped; - - if(_is_constructed && oct == 0x80) { - *len_r = -1; /* Indefinite length */ - return 1; - } - - if(oct == 0xff) { - /* Reserved in standard for future use. */ - return -1; - } - - oct &= 0x7F; /* Leave only the 7 LS bits */ - for(len = 0, buf++, skipped = 1; - oct && (++skipped <= size); buf++, oct--) { - - /* Verify that we won't overflow. */ - if(!(len >> ((8 * sizeof(len)) - (8+1)))) { - len = (len << 8) | *buf; - } else { - /* Too large length value. */ - return -1; - } - } - - if(oct == 0) { - if(len < 0 || len > RSSIZE_MAX) { - /* Length value out of sane range. */ - return -1; - } - - *len_r = len; - return skipped; - } - - return 0; /* Want more */ - } - -} - -ssize_t -ber_skip_length(const asn_codec_ctx_t *opt_codec_ctx, - int _is_constructed, const void *ptr, size_t size) { - ber_tlv_len_t vlen; /* Length of V in TLV */ - ssize_t tl; /* Length of L in TLV */ - ssize_t ll; /* Length of L in TLV */ - size_t skip; - - /* - * Make sure we didn't exceed the maximum stack size. - */ - if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) - return -1; - - /* - * Determine the size of L in TLV. - */ - ll = ber_fetch_length(_is_constructed, ptr, size, &vlen); - if(ll <= 0) return ll; - - /* - * Definite length. - */ - if(vlen >= 0) { - skip = ll + vlen; - if(skip > size) - return 0; /* Want more */ - return skip; - } - - /* - * Indefinite length! - */ - ASN_DEBUG("Skipping indefinite length"); - for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) { - ber_tlv_tag_t tag; - - /* Fetch the tag */ - tl = ber_fetch_tag(ptr, size, &tag); - if(tl <= 0) return tl; - - ll = ber_skip_length(opt_codec_ctx, - BER_TLV_CONSTRUCTED(ptr), - ((const char *)ptr) + tl, size - tl); - if(ll <= 0) return ll; - - skip += tl + ll; - - /* - * This may be the end of the indefinite length structure, - * two consecutive 0 octets. - * Check if it is true. - */ - if(((const uint8_t *)ptr)[0] == 0 - && ((const uint8_t *)ptr)[1] == 0) - return skip; - - ptr = ((const char *)ptr) + tl + ll; - size -= tl + ll; - } - - /* UNREACHABLE */ -} - -size_t -der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) { - size_t required_size; /* Size of len encoding */ - uint8_t *buf = (uint8_t *)bufp; - uint8_t *end; - int i; - - if(len <= 127) { - /* Encoded in 1 octet */ - if(size) *buf = (uint8_t)len; - return 1; - } - - /* - * Compute the size of the subsequent bytes. - */ - for(required_size = 1, i = 8; i < 8 * (int)sizeof(len); i += 8) { - if(len >> i) - required_size++; - else - break; - } - - if(size <= required_size) - return required_size + 1; - - *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */ - - /* - * Produce the len encoding, space permitting. - */ - end = buf + required_size; - for(i -= 8; buf < end; i -= 8, buf++) - *buf = (uint8_t)(len >> i); - - return required_size + 1; -} - diff --git a/src/codec_utils/E2AP/ber_tlv_length.h b/src/codec_utils/E2AP/ber_tlv_length.h deleted file mode 100644 index d1e4d48dd..000000000 --- a/src/codec_utils/E2AP/ber_tlv_length.h +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _BER_TLV_LENGTH_H_ -#define _BER_TLV_LENGTH_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -typedef ssize_t ber_tlv_len_t; - -/* - * This function tries to fetch the length of the BER TLV value and place it - * in *len_r. - * RETURN VALUES: - * 0: More data expected than bufptr contains. - * -1: Fatal error deciphering length. - * >0: Number of bytes used from bufptr. - * On return with >0, len_r is constrained as -1..MAX, where -1 mean - * that the value is of indefinite length. - */ -ssize_t ber_fetch_length(int _is_constructed, const void *bufptr, size_t size, - ber_tlv_len_t *len_r); - -/* - * This function expects bufptr to be positioned over L in TLV. - * It returns number of bytes occupied by L and V together, suitable - * for skipping. The function properly handles indefinite length. - * RETURN VALUES: - * Standard {-1,0,>0} convention. - */ -ssize_t ber_skip_length( - const struct asn_codec_ctx_s *opt_codec_ctx, /* optional context */ - int _is_constructed, const void *bufptr, size_t size); - -/* - * This function serializes the length (L from TLV) in DER format. - * It always returns number of bytes necessary to represent the length, - * it is a caller's responsibility to check the return value - * against the supplied buffer's size. - */ -size_t der_tlv_length_serialize(ber_tlv_len_t len, void *bufptr, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif /* _BER_TLV_LENGTH_H_ */ diff --git a/src/codec_utils/E2AP/ber_tlv_tag.c b/src/codec_utils/E2AP/ber_tlv_tag.c deleted file mode 100644 index 4a7d732f8..000000000 --- a/src/codec_utils/E2AP/ber_tlv_tag.c +++ /dev/null @@ -1,144 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -ssize_t -ber_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) { - ber_tlv_tag_t val; - ber_tlv_tag_t tclass; - size_t skipped; - - if(size == 0) - return 0; - - val = *(const uint8_t *)ptr; - tclass = (val >> 6); - if((val &= 0x1F) != 0x1F) { - /* - * Simple form: everything encoded in a single octet. - * Tag Class is encoded using two least significant bits. - */ - *tag_r = (val << 2) | tclass; - return 1; - } - - /* - * Each octet contains 7 bits of useful information. - * The MSB is 0 if it is the last octet of the tag. - */ - for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; - skipped <= size; - ptr = ((const char *)ptr) + 1, skipped++) { - unsigned int oct = *(const uint8_t *)ptr; - if(oct & 0x80) { - val = (val << 7) | (oct & 0x7F); - /* - * Make sure there are at least 9 bits spare - * at the MS side of a value. - */ - if(val >> ((8 * sizeof(val)) - 9)) { - /* - * We would not be able to accomodate - * any more tag bits. - */ - return -1; - } - } else { - val = (val << 7) | oct; - *tag_r = (val << 2) | tclass; - return skipped; - } - } - - return 0; /* Want more */ -} - - -ssize_t -ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) { - char buf[sizeof("[APPLICATION ]") + 32]; - ssize_t ret; - - ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf)); - if(ret >= (ssize_t)sizeof(buf) || ret < 2) { - errno = EPERM; - return -1; - } - - return fwrite(buf, 1, ret, f); -} - -ssize_t -ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) { - const char *type = 0; - int ret; - - switch(tag & 0x3) { - case ASN_TAG_CLASS_UNIVERSAL: type = "UNIVERSAL "; break; - case ASN_TAG_CLASS_APPLICATION: type = "APPLICATION "; break; - case ASN_TAG_CLASS_CONTEXT: type = ""; break; - case ASN_TAG_CLASS_PRIVATE: type = "PRIVATE "; break; - } - - ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2); - if(ret <= 0 && size) buf[0] = '\0'; /* against broken libc's */ - - return ret; -} - -char * -ber_tlv_tag_string(ber_tlv_tag_t tag) { - static char buf[sizeof("[APPLICATION ]") + 32]; - - (void)ber_tlv_tag_snprint(tag, buf, sizeof(buf)); - - return buf; -} - - -size_t -ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) { - int tclass = BER_TAG_CLASS(tag); - ber_tlv_tag_t tval = BER_TAG_VALUE(tag); - uint8_t *buf = (uint8_t *)bufp; - uint8_t *end; - size_t required_size; - size_t i; - - if(tval <= 30) { - /* Encoded in 1 octet */ - if(size) buf[0] = (tclass << 6) | tval; - return 1; - } else if(size) { - *buf++ = (tclass << 6) | 0x1F; - size--; - } - - /* - * Compute the size of the subsequent bytes. - */ - for(required_size = 1, i = 7; i < 8 * sizeof(tval); i += 7) { - if(tval >> i) - required_size++; - else - break; - } - - if(size < required_size) - return required_size + 1; - - /* - * Fill in the buffer, space permitting. - */ - end = buf + required_size - 1; - for(i -= 7; buf < end; i -= 7, buf++) - *buf = 0x80 | ((tval >> i) & 0x7F); - *buf = (tval & 0x7F); /* Last octet without high bit */ - - return required_size + 1; -} - diff --git a/src/codec_utils/E2AP/ber_tlv_tag.h b/src/codec_utils/E2AP/ber_tlv_tag.h deleted file mode 100644 index ce227add6..000000000 --- a/src/codec_utils/E2AP/ber_tlv_tag.h +++ /dev/null @@ -1,60 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _BER_TLV_TAG_H_ -#define _BER_TLV_TAG_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -enum asn_tag_class { - ASN_TAG_CLASS_UNIVERSAL = 0, /* 0b00 */ - ASN_TAG_CLASS_APPLICATION = 1, /* 0b01 */ - ASN_TAG_CLASS_CONTEXT = 2, /* 0b10 */ - ASN_TAG_CLASS_PRIVATE = 3 /* 0b11 */ -}; -typedef unsigned ber_tlv_tag_t; /* BER TAG from Tag-Length-Value */ - -/* - * Tag class is encoded together with tag value for optimization purposes. - */ -#define BER_TAG_CLASS(tag) ((tag) & 0x3) -#define BER_TAG_VALUE(tag) ((tag) >> 2) -#define BER_TLV_CONSTRUCTED(tagptr) (((*(const uint8_t *)tagptr)&0x20)?1:0) - -#define BER_TAGS_EQUAL(tag1, tag2) ((tag1) == (tag2)) - -/* - * Several functions for printing the TAG in the canonical form - * (i.e. "[PRIVATE 0]"). - * Return values correspond to their libc counterparts (if any). - */ -ssize_t ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t buflen); -ssize_t ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *); -char *ber_tlv_tag_string(ber_tlv_tag_t tag); - - -/* - * This function tries to fetch the tag from the input stream. - * RETURN VALUES: - * 0: More data expected than bufptr contains. - * -1: Fatal error deciphering tag. - * >0: Number of bytes used from bufptr. tag_r will contain the tag. - */ -ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r); - -/* - * This function serializes the tag (T from TLV) in BER format. - * It always returns number of bytes necessary to represent the tag, - * it is a caller's responsibility to check the return value - * against the supplied buffer's size. - */ -size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif /* _BER_TLV_TAG_H_ */ diff --git a/src/codec_utils/E2AP/der_encoder.c b/src/codec_utils/E2AP/der_encoder.c deleted file mode 100644 index 2c6a6f769..000000000 --- a/src/codec_utils/E2AP/der_encoder.c +++ /dev/null @@ -1,194 +0,0 @@ -/*- - * Copyright (c) 2003, 2004 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include - -static ssize_t der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len, - asn_app_consume_bytes_f *cb, void *app_key, int constructed); - -/* - * The DER encoder of any type. - */ -asn_enc_rval_t -der_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr, - asn_app_consume_bytes_f *consume_bytes, void *app_key) { - ASN_DEBUG("DER encoder invoked for %s", - type_descriptor->name); - - /* - * Invoke type-specific encoder. - */ - return type_descriptor->op->der_encoder( - type_descriptor, struct_ptr, /* Pointer to the destination structure */ - 0, 0, consume_bytes, app_key); -} - -/* - * Argument type and callback necessary for der_encode_to_buffer(). - */ -typedef struct enc_to_buf_arg { - void *buffer; - size_t left; -} enc_to_buf_arg; -static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) { - enc_to_buf_arg *arg = (enc_to_buf_arg *)key; - - if(arg->left < size) - return -1; /* Data exceeds the available buffer size */ - - memcpy(arg->buffer, buffer, size); - arg->buffer = ((char *)arg->buffer) + size; - arg->left -= size; - - return 0; -} - -/* - * A variant of the der_encode() which encodes the data into the provided buffer - */ -asn_enc_rval_t -der_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor, - const void *struct_ptr, void *buffer, size_t buffer_size) { - enc_to_buf_arg arg; - asn_enc_rval_t ec; - - arg.buffer = buffer; - arg.left = buffer_size; - - ec = type_descriptor->op->der_encoder(type_descriptor, - struct_ptr, /* Pointer to the destination structure */ - 0, 0, encode_to_buffer_cb, &arg); - if(ec.encoded != -1) { - assert(ec.encoded == (ssize_t)(buffer_size - arg.left)); - /* Return the encoded contents size */ - } - return ec; -} - - -/* - * Write out leading TL[v] sequence according to the type definition. - */ -ssize_t -der_write_tags(const asn_TYPE_descriptor_t *sd, size_t struct_length, - int tag_mode, int last_tag_form, - ber_tlv_tag_t tag, /* EXPLICIT or IMPLICIT tag */ - asn_app_consume_bytes_f *cb, void *app_key) { -#define ASN1_DER_MAX_TAGS_COUNT 4 - ber_tlv_tag_t - tags_buf_scratch[ASN1_DER_MAX_TAGS_COUNT * sizeof(ber_tlv_tag_t)]; - ssize_t lens[ASN1_DER_MAX_TAGS_COUNT * sizeof(ssize_t)]; - const ber_tlv_tag_t *tags; /* Copy of tags stream */ - int tags_count; /* Number of tags */ - size_t overall_length; - int i; - - ASN_DEBUG("Writing tags (%s, tm=%d, tc=%d, tag=%s, mtc=%d)", - sd->name, tag_mode, sd->tags_count, - ber_tlv_tag_string(tag), - tag_mode - ?(sd->tags_count+1 - -((tag_mode == -1) && sd->tags_count)) - :sd->tags_count - ); - - if(sd->tags_count + 1 > ASN1_DER_MAX_TAGS_COUNT) { - ASN_DEBUG("System limit %d on tags count", ASN1_DER_MAX_TAGS_COUNT); - return -1; - } - - if(tag_mode) { - /* - * Instead of doing shaman dance like we do in ber_check_tags(), - * allocate a small array on the stack - * and initialize it appropriately. - */ - int stag_offset; - ber_tlv_tag_t *tags_buf = tags_buf_scratch; - tags_count = sd->tags_count - + 1 /* EXPLICIT or IMPLICIT tag is given */ - - ((tag_mode == -1) && sd->tags_count); - /* Copy tags over */ - tags_buf[0] = tag; - stag_offset = -1 + ((tag_mode == -1) && sd->tags_count); - for(i = 1; i < tags_count; i++) - tags_buf[i] = sd->tags[i + stag_offset]; - tags = tags_buf; - } else { - tags = sd->tags; - tags_count = sd->tags_count; - } - - /* No tags to write */ - if(tags_count == 0) - return 0; - - /* - * Array of tags is initialized. - * Now, compute the size of the TLV pairs, from right to left. - */ - overall_length = struct_length; - for(i = tags_count - 1; i >= 0; --i) { - lens[i] = der_write_TL(tags[i], overall_length, 0, 0, 0); - if(lens[i] == -1) return -1; - overall_length += lens[i]; - lens[i] = overall_length - lens[i]; - } - - if(!cb) return overall_length - struct_length; - - ASN_DEBUG("Encoding %s TL sequence (%d elements)", sd->name, - tags_count); - - /* - * Encode the TL sequence for real. - */ - for(i = 0; i < tags_count; i++) { - ssize_t len; - int _constr; - - /* Check if this tag happens to be constructed */ - _constr = (last_tag_form || i < (tags_count - 1)); - - len = der_write_TL(tags[i], lens[i], cb, app_key, _constr); - if(len == -1) return -1; - } - - return overall_length - struct_length; -} - -static ssize_t -der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len, - asn_app_consume_bytes_f *cb, void *app_key, - int constructed) { - uint8_t buf[32]; - size_t size = 0; - int buf_size = cb?sizeof(buf):0; - ssize_t tmp; - - /* Serialize tag (T from TLV) into possibly zero-length buffer */ - tmp = ber_tlv_tag_serialize(tag, buf, buf_size); - if(tmp == -1 || tmp > (ssize_t)sizeof(buf)) return -1; - size += tmp; - - /* Serialize length (L from TLV) into possibly zero-length buffer */ - tmp = der_tlv_length_serialize(len, buf+size, buf_size?buf_size-size:0); - if(tmp == -1) return -1; - size += tmp; - - if(size > sizeof(buf)) - return -1; - - /* - * If callback is specified, invoke it, and check its return value. - */ - if(cb) { - if(constructed) *buf |= 0x20; - if(cb(buf, size, app_key) < 0) - return -1; - } - - return size; -} diff --git a/src/codec_utils/E2AP/der_encoder.h b/src/codec_utils/E2AP/der_encoder.h deleted file mode 100644 index e93944edc..000000000 --- a/src/codec_utils/E2AP/der_encoder.h +++ /dev/null @@ -1,68 +0,0 @@ -/*- - * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _DER_ENCODER_H_ -#define _DER_ENCODER_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ - -/* - * The DER encoder of any type. May be invoked by the application. - * Produces DER- and BER-compliant encoding. (DER is a subset of BER). - * - * NOTE: Use the ber_decode() function (ber_decoder.h) to decode data - * produced by der_encode(). - */ -asn_enc_rval_t der_encode(const struct asn_TYPE_descriptor_s *type_descriptor, - const void *struct_ptr, /* Structure to be encoded */ - asn_app_consume_bytes_f *consume_bytes_cb, - void *app_key /* Arbitrary callback argument */ -); - -/* A variant of der_encode() which encodes data into the pre-allocated buffer */ -asn_enc_rval_t der_encode_to_buffer( - const struct asn_TYPE_descriptor_s *type_descriptor, - const void *struct_ptr, /* Structure to be encoded */ - void *buffer, /* Pre-allocated buffer */ - size_t buffer_size /* Initial buffer size (maximum) */ -); - -/* - * Type of the generic DER encoder. - */ -typedef asn_enc_rval_t(der_type_encoder_f)( - const struct asn_TYPE_descriptor_s *type_descriptor, - const void *struct_ptr, /* Structure to be encoded */ - int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */ - ber_tlv_tag_t tag, asn_app_consume_bytes_f *consume_bytes_cb, /* Callback */ - void *app_key /* Arbitrary callback argument */ -); - - -/******************************* - * INTERNALLY USEFUL FUNCTIONS * - *******************************/ - -/* - * Write out leading TL[v] sequence according to the type definition. - */ -ssize_t der_write_tags(const struct asn_TYPE_descriptor_s *type_descriptor, - size_t struct_length, - int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */ - int last_tag_form, /* {0,!0}: prim, constructed */ - ber_tlv_tag_t tag, - asn_app_consume_bytes_f *consume_bytes_cb, - void *app_key); - -#ifdef __cplusplus -} -#endif - -#endif /* _DER_ENCODER_H_ */ diff --git a/src/codec_utils/E2AP/oer_decoder.c b/src/codec_utils/E2AP/oer_decoder.c deleted file mode 100644 index 070173826..000000000 --- a/src/codec_utils/E2AP/oer_decoder.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include - -/* - * The OER decoder of any type. - */ -asn_dec_rval_t -oer_decode(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr, - const void *ptr, size_t size) { - asn_codec_ctx_t s_codec_ctx; - - /* - * Stack checker requires that the codec context - * must be allocated on the stack. - */ - if(opt_codec_ctx) { - if(opt_codec_ctx->max_stack_size) { - s_codec_ctx = *opt_codec_ctx; - opt_codec_ctx = &s_codec_ctx; - } - } else { - /* If context is not given, be security-conscious anyway */ - memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); - s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; - opt_codec_ctx = &s_codec_ctx; - } - - /* - * Invoke type-specific decoder. - */ - return type_descriptor->op->oer_decoder(opt_codec_ctx, type_descriptor, 0, - struct_ptr, /* Pointer to the destination structure */ - ptr, size /* Buffer and its size */ - ); -} - -/* - * Open Type is encoded as a length (#8.6) followed by that number of bytes. - * Since we're just skipping, reading the length would be enough. - */ -ssize_t -oer_open_type_skip(const void *bufptr, size_t size) { - size_t len = 0; - return oer_fetch_length(bufptr, size, &len); -} - -/* - * Read the Open Type (X.696 (08/2015), #30). - * RETURN VALUES: - * 0: More data expected than bufptr contains. - * -1: Fatal error deciphering length. - * >0: Number of bytes used from bufptr. - */ -ssize_t -oer_open_type_get(const asn_codec_ctx_t *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *td, - const asn_oer_constraints_t *constraints, void **struct_ptr, - const void *bufptr, size_t size) { - asn_dec_rval_t dr; - size_t container_len = 0; - ssize_t len_len; - enum asn_struct_free_method dispose_method = - (*struct_ptr) ? ASFM_FREE_UNDERLYING_AND_RESET : ASFM_FREE_EVERYTHING; - - /* Get the size of a length determinant */ - len_len = oer_fetch_length(bufptr, size, &container_len); - if(len_len <= 0) { - return len_len; /* Error or more data expected */ - } - - /* - * len_len can't be bigger than size, but size without len_len - * should be bigger or equal to container length - */ - if(size - len_len < container_len) { - /* More data is expected */ - return 0; - } - - dr = td->op->oer_decoder(opt_codec_ctx, td, constraints, struct_ptr, - (const uint8_t *)bufptr + len_len, container_len); - if(dr.code == RC_OK) { - return len_len + container_len; - } else { - /* Even if RC_WMORE, we can't get more data into a closed container. */ - td->op->free_struct(td, *struct_ptr, dispose_method); - *struct_ptr = NULL; - return -1; - } -} - - -asn_dec_rval_t -oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_oer_constraints_t *constraints, void **sptr, - const void *ptr, size_t size) { - ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr; - asn_dec_rval_t rval = {RC_OK, 0}; - size_t expected_length = 0; - ssize_t len_len; - - (void)td; - (void)opt_codec_ctx; - (void)constraints; - - if(!st) { - st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC( - 1, sizeof(ASN__PRIMITIVE_TYPE_t))); - if(!st) ASN__DECODE_FAILED; - } - - - /* - * X.696 (08/2015) #27.2 - * Encode length determinant as _number of octets_, but only - * if upper bound is not equal to lower bound. - */ - 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(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; - } -} diff --git a/src/codec_utils/E2AP/oer_decoder.h b/src/codec_utils/E2AP/oer_decoder.h deleted file mode 100644 index 40992e948..000000000 --- a/src/codec_utils/E2AP/oer_decoder.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef OER_DECODER_H -#define OER_DECODER_H - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ -struct asn_codec_ctx_s; /* Forward declaration */ - -/* - * The Octet Encoding Rules (OER, X.696 08/2015) decoder for any given type. - * This function may be invoked directly by the application. - * Parses CANONICAL-OER and BASIC-OER. - */ -asn_dec_rval_t oer_decode(const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size /* Size of that buffer */ - ); - -/* - * Type of generic function which decodes the byte stream into the structure. - */ -typedef asn_dec_rval_t(oer_type_decoder_f)( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, - const asn_oer_constraints_t *constraints, - void **struct_ptr, - const void *buf_ptr, - size_t size); - -/* - * Swallow the Open Type (X.696 (08/2015), #30) into /dev/null. - * RETURN VALUES: - * -1: Fatal error deciphering length. - * 0: More data expected than bufptr contains. - * >0: Number of bytes used from bufptr. - */ -ssize_t oer_open_type_skip(const void *bufptr, size_t size); - -/* - * Read the Open Type (X.696 (08/2015), #30). - * RETURN VALUES: - * 0: More data expected than bufptr contains. - * -1: Fatal error deciphering length. - * >0: Number of bytes used from bufptr. - */ -ssize_t oer_open_type_get(const asn_codec_ctx_t *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *td, - const asn_oer_constraints_t *constraints, - void **struct_ptr, const void *bufptr, size_t size); - -/* - * Length-prefixed buffer decoding for primitive types. - */ -oer_type_decoder_f oer_decode_primitive; - - -#ifdef __cplusplus -} -#endif - -#endif /* OER_DECODER_H */ diff --git a/src/codec_utils/E2AP/oer_encoder.c b/src/codec_utils/E2AP/oer_encoder.c deleted file mode 100644 index a284cc2f3..000000000 --- a/src/codec_utils/E2AP/oer_encoder.c +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include - -/* - * The OER encoder of any type. - */ -asn_enc_rval_t -oer_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr, - asn_app_consume_bytes_f *consume_bytes, void *app_key) { - ASN_DEBUG("OER encoder invoked for %s", type_descriptor->name); - - /* - * Invoke type-specific encoder. - */ - return type_descriptor->op->oer_encoder( - type_descriptor, 0, - struct_ptr, /* Pointer to the destination structure */ - consume_bytes, app_key); -} - -/* - * Argument type and callback necessary for oer_encode_to_buffer(). - */ -typedef struct enc_to_buf_arg { - void *buffer; - size_t left; -} enc_to_buf_arg; -static int -encode_to_buffer_cb(const void *buffer, size_t size, void *key) { - enc_to_buf_arg *arg = (enc_to_buf_arg *)key; - - if(arg->left < size) return -1; /* Data exceeds the available buffer size */ - - memcpy(arg->buffer, buffer, size); - arg->buffer = ((char *)arg->buffer) + size; - arg->left -= size; - - return 0; -} - -/* - * A variant of the oer_encode() which encodes the data into the provided buffer - */ -asn_enc_rval_t -oer_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor, - const asn_oer_constraints_t *constraints, - const void *struct_ptr, /* Structure to be encoded */ - void *buffer, /* Pre-allocated buffer */ - size_t buffer_size /* Initial buffer size (maximum) */ -) { - enc_to_buf_arg arg; - asn_enc_rval_t ec; - - arg.buffer = buffer; - arg.left = buffer_size; - - if(type_descriptor->op->oer_encoder == NULL) { - ec.encoded = -1; - ec.failed_type = type_descriptor; - ec.structure_ptr = struct_ptr; - ASN_DEBUG("OER encoder is not defined for %s", - type_descriptor->name); - } else { - ec = type_descriptor->op->oer_encoder( - type_descriptor, constraints, - struct_ptr, /* Pointer to the destination structure */ - encode_to_buffer_cb, &arg); - if(ec.encoded != -1) { - assert(ec.encoded == (ssize_t)(buffer_size - arg.left)); - /* Return the encoded contents size */ - } - } - return ec; -} - -asn_enc_rval_t -oer_encode_primitive(const asn_TYPE_descriptor_t *td, - const asn_oer_constraints_t *constraints, const void *sptr, - asn_app_consume_bytes_f *cb, void *app_key) { - const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr; - asn_enc_rval_t er = {0, 0, 0}; - ssize_t ret; - - (void)constraints; - - if(!st) ASN__ENCODE_FAILED; - - ASN_DEBUG("Encoding %s (%" ASN_PRI_SIZE " bytes)", td ? td->name : "", st->size); - - /* - * X.696 (08/2015) #27.2 - */ - 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); - } -} - -static int -oer__count_bytes(const void *buffer, size_t size, void *bytes_ptr) { - size_t *bytes = bytes_ptr; - (void)buffer; - *bytes += size; - return 0; -} - -ssize_t -oer_open_type_put(const asn_TYPE_descriptor_t *td, - const asn_oer_constraints_t *constraints, const void *sptr, - asn_app_consume_bytes_f *cb, void *app_key) { - size_t serialized_byte_count = 0; - asn_enc_rval_t er = {0,0,0}; - ssize_t len_len; - - er = td->op->oer_encoder(td, constraints, sptr, oer__count_bytes, - &serialized_byte_count); - if(er.encoded < 0) return -1; - assert(serialized_byte_count == (size_t)er.encoded); - - len_len = oer_serialize_length(serialized_byte_count, cb, app_key); - if(len_len == -1) return -1; - - er = td->op->oer_encoder(td, constraints, sptr, cb, app_key); - if(er.encoded < 0) return -1; - assert(serialized_byte_count == (size_t)er.encoded); - - return len_len + er.encoded; -} - diff --git a/src/codec_utils/E2AP/oer_encoder.h b/src/codec_utils/E2AP/oer_encoder.h deleted file mode 100644 index 6a7b68125..000000000 --- a/src/codec_utils/E2AP/oer_encoder.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef OER_ENCODER_H -#define OER_ENCODER_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ - -/* - * The Octet Encoding Rules (OER, X.696 08/2015) encoder for any type. - * This function may be invoked directly by the application. - * Produces CANONICAL-OER output compatible with CANONICAL-OER - * and BASIC-OER decoders. - */ -asn_enc_rval_t oer_encode(const struct asn_TYPE_descriptor_s *type_descriptor, - const void *struct_ptr, /* Structure to be encoded */ - asn_app_consume_bytes_f *consume_bytes_cb, - void *app_key /* Arbitrary callback argument */ -); - -/* A variant of oer_encode() which encodes data into the pre-allocated buffer */ -asn_enc_rval_t oer_encode_to_buffer( - const struct asn_TYPE_descriptor_s *type_descriptor, - const asn_oer_constraints_t *constraints, - const void *struct_ptr, /* Structure to be encoded */ - void *buffer, /* Pre-allocated buffer */ - size_t buffer_size /* Initial buffer size (maximum) */ -); - -/* - * Type of the generic OER encoder. - */ -typedef asn_enc_rval_t(oer_type_encoder_f)( - const struct asn_TYPE_descriptor_s *type_descriptor, - const asn_oer_constraints_t *constraints, - const void *struct_ptr, /* Structure to be encoded */ - asn_app_consume_bytes_f *consume_bytes_cb, /* Callback */ - void *app_key /* Arbitrary callback argument */ -); - -/* - * Write out the Open Type (X.696 (08/2015), #30). - * RETURN VALUES: - * -1: Fatal error encoding the type. - * >0: Number of bytes serialized. - */ -ssize_t oer_open_type_put(const struct asn_TYPE_descriptor_s *td, - const asn_oer_constraints_t *constraints, - const void *struct_ptr, - asn_app_consume_bytes_f *consume_bytes_cb, - void *app_key); - - -/* - * Length-prefixed buffer encoding for primitive types. - */ -oer_type_encoder_f oer_encode_primitive; - -#ifdef __cplusplus -} -#endif - -#endif /* OER_ENCODER_H */ diff --git a/src/codec_utils/E2AP/oer_support.c b/src/codec_utils/E2AP/oer_support.c deleted file mode 100644 index b15a3bc93..000000000 --- a/src/codec_utils/E2AP/oer_support.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . - * All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include - -#include - -/* - * 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; - size_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) > (ssize_t)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; - } - - if(len > RSIZE_MAX) { /* A bit of C11 validation */ - *len_r = 0; - return -1; - } - - *len_r = len; - assert(len_len + 1 == (size_t)(bend - (const uint8_t *)bufptr)); - return len_len + 1; -} - - -/* - * Serialize OER length. Returns the number of bytes serialized - * or -1 if a given callback returned with negative result. - */ -ssize_t -oer_serialize_length(size_t length, asn_app_consume_bytes_f *cb, - void *app_key) { - uint8_t scratch[1 + sizeof(length)]; - uint8_t *sp = scratch; - int littleEndian = 1; /* Run-time detection */ - const uint8_t *pstart; - const uint8_t *pend; - const uint8_t *p; - int add; - - if(length <= 127) { - uint8_t b = length; - if(cb(&b, 1, app_key) < 0) { - return -1; - } - return 1; - } - - if(*(char *)&littleEndian) { - pstart = (const uint8_t *)&length + sizeof(length) - 1; - pend = (const uint8_t *)&length; - add = -1; - } else { - pstart = (const uint8_t *)&length; - pend = pstart + sizeof(length); - add = 1; - } - - for(p = pstart; p != pend; p += add) { - /* Skip leading zeros. */ - if(*p) break; - } - - for(sp = scratch + 1; ; p += add) { - *sp++ = *p; - if(p == pend) break; - } - assert((sp - scratch) - 1 <= 0x7f); - scratch[0] = 0x80 + ((sp - scratch) - 1); - - if(cb(scratch, sp - scratch, app_key) < 0) { - return -1; - } - - return sp - scratch; -} - diff --git a/src/codec_utils/E2AP/oer_support.h b/src/codec_utils/E2AP/oer_support.h deleted file mode 100644 index dbc9b5fcf..000000000 --- a/src/codec_utils/E2AP/oer_support.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef OER_SUPPORT_H -#define OER_SUPPORT_H - -#include /* Platform-specific types */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Pre-computed OER constraints. - */ -typedef struct asn_oer_constraint_number_s { - unsigned width; /* ±8,4,2,1 fixed bytes */ - unsigned positive; /* 1 for unsigned number, 0 for signed */ -} asn_oer_constraint_number_t; -typedef struct asn_oer_constraints_s { - asn_oer_constraint_number_t value; - ssize_t size; /* -1 (no constraint) or >= 0 */ -} 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); - -/* - * Serialize OER length. Returns the number of bytes serialized - * or -1 if a given callback returned with negative result. - */ -ssize_t oer_serialize_length(size_t length, asn_app_consume_bytes_f *cb, void *app_key); - - -#ifdef __cplusplus -} -#endif - -#endif /* OER_SUPPORT_H */ diff --git a/src/codec_utils/E2AP/per_decoder.c b/src/codec_utils/E2AP/per_decoder.c deleted file mode 100644 index 8a3e39df8..000000000 --- a/src/codec_utils/E2AP/per_decoder.c +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include -#include - -/* - * Decode a "Production of a complete encoding", X.691#10.1. - * The complete encoding contains at least one byte, and is an integral - * multiple of 8 bytes. - */ -asn_dec_rval_t -uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, - const void *buffer, size_t size) { - asn_dec_rval_t rval; - - rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0); - if(rval.consumed) { - /* - * We've always given 8-aligned data, - * so convert bits to integral bytes. - */ - rval.consumed += 7; - rval.consumed >>= 3; - } else if(rval.code == RC_OK) { - if(size) { - if(((const uint8_t *)buffer)[0] == 0) { - rval.consumed = 1; /* 1 byte */ - } else { - ASN_DEBUG("Expecting single zeroed byte"); - rval.code = RC_FAIL; - } - } else { - /* Must contain at least 8 bits. */ - rval.code = RC_WMORE; - } - } - - return rval; -} - -asn_dec_rval_t -uper_decode(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, - size_t size, int skip_bits, int unused_bits) { - asn_codec_ctx_t s_codec_ctx; - asn_dec_rval_t rval; - asn_per_data_t pd; - - if(skip_bits < 0 || skip_bits > 7 - || unused_bits < 0 || unused_bits > 7 - || (unused_bits > 0 && !size)) - ASN__DECODE_FAILED; - - /* - * Stack checker requires that the codec context - * must be allocated on the stack. - */ - if(opt_codec_ctx) { - if(opt_codec_ctx->max_stack_size) { - s_codec_ctx = *opt_codec_ctx; - opt_codec_ctx = &s_codec_ctx; - } - } else { - /* If context is not given, be security-conscious anyway */ - memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); - s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; - opt_codec_ctx = &s_codec_ctx; - } - - /* Fill in the position indicator */ - memset(&pd, 0, sizeof(pd)); - pd.buffer = (const uint8_t *)buffer; - pd.nboff = skip_bits; - pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from */ - if(pd.nboff > pd.nbits) - ASN__DECODE_FAILED; - - /* - * Invoke type-specific decoder. - */ - if(!td->op->uper_decoder) - ASN__DECODE_FAILED; /* PER is not compiled in */ - rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd); - if(rval.code == RC_OK) { - /* Return the number of consumed bits */ - rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) - + pd.nboff - skip_bits; - ASN_DEBUG("PER decoding consumed %ld, counted %ld", - (long)rval.consumed, (long)pd.moved); - assert(rval.consumed == pd.moved); - } else { - /* PER codec is not a restartable */ - rval.consumed = 0; - } - return rval; -} - -asn_dec_rval_t -aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, - const void *buffer, size_t size) { - asn_dec_rval_t rval; - - rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0); - if(rval.consumed) { - /* - * We've always given 8-aligned data, - * so convert bits to integral bytes. - */ - rval.consumed += 7; - rval.consumed >>= 3; - } else if(rval.code == RC_OK) { - if(size) { - if(((const uint8_t *)buffer)[0] == 0) { - rval.consumed = 1; /* 1 byte */ - } else { - ASN_DEBUG("Expecting single zeroed byte"); - rval.code = RC_FAIL; - } - } else { - /* Must contain at least 8 bits. */ - rval.code = RC_WMORE; - } - } - - return rval; -} - -asn_dec_rval_t -aper_decode(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, - size_t size, int skip_bits, int unused_bits) { - asn_codec_ctx_t s_codec_ctx; - asn_dec_rval_t rval; - asn_per_data_t pd; - - if(skip_bits < 0 || skip_bits > 7 - || unused_bits < 0 || unused_bits > 7 - || (unused_bits > 0 && !size)) - ASN__DECODE_FAILED; - - /* - * Stack checker requires that the codec context - * must be allocated on the stack. - */ - if(opt_codec_ctx) { - if(opt_codec_ctx->max_stack_size) { - s_codec_ctx = *opt_codec_ctx; - opt_codec_ctx = &s_codec_ctx; - } - } else { - /* If context is not given, be security-conscious anyway */ - memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); - s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; - opt_codec_ctx = &s_codec_ctx; - } - - /* Fill in the position indicator */ - memset(&pd, 0, sizeof(pd)); - pd.buffer = (const uint8_t *)buffer; - pd.nboff = skip_bits; - pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from */ - if(pd.nboff > pd.nbits) - ASN__DECODE_FAILED; - - /* - * Invoke type-specific decoder. - */ - if(!td->op->aper_decoder) - ASN__DECODE_FAILED; /* PER is not compiled in */ - rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd); - if(rval.code == RC_OK) { - /* Return the number of consumed bits */ - rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) - + pd.nboff - skip_bits; - ASN_DEBUG("PER decoding consumed %zu, counted %zu", - rval.consumed, pd.moved); - assert(rval.consumed == pd.moved); - } else { - /* PER codec is not a restartable */ - rval.consumed = 0; - } - return rval; -} - diff --git a/src/codec_utils/E2AP/per_decoder.h b/src/codec_utils/E2AP/per_decoder.h deleted file mode 100644 index eea474a9e..000000000 --- a/src/codec_utils/E2AP/per_decoder.h +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * Copyright (c) 2005-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _PER_DECODER_H_ -#define _PER_DECODER_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct asn_TYPE_descriptor_s; /* Forward declaration */ - -/* - * Unaligned PER decoder of a "complete encoding" as per X.691 (08/2015) #11.1. - * On success, this call always returns (.consumed >= 1), as per #11.1.3. - */ -asn_dec_rval_t uper_decode_complete( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, /* Type to decode */ - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size /* Size of data buffer */ -); - -/* - * Unaligned PER decoder of any ASN.1 type. May be invoked by the application. - * WARNING: This call returns the number of BITS read from the stream. Beware. - */ -asn_dec_rval_t uper_decode( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, /* Type to decode */ - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size, /* Size of the input data buffer, in bytes */ - int skip_bits, /* Number of unused leading bits, 0..7 */ - int unused_bits /* Number of unused tailing bits, 0..7 */ -); - -/* - * Aligned PER decoder of a "complete encoding" as per X.691#10.1. - * On success, this call always returns (.consumed >= 1), in BITS, as per X.691#10.1.3. - */ -asn_dec_rval_t aper_decode_complete( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, /* Type to decode */ - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size /* Size of data buffer */ - ); - -/* - * Aligned PER decoder of any ASN.1 type. May be invoked by the application. - * WARNING: This call returns the number of BITS read from the stream. Beware. - */ -asn_dec_rval_t aper_decode( - const struct asn_codec_ctx_s *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, /* Type to decode */ - void **struct_ptr, /* Pointer to a target structure's pointer */ - const void *buffer, /* Data to be decoded */ - size_t size, /* Size of data buffer */ - int skip_bits, /* Number of unused leading bits, 0..7 */ - int unused_bits /* Number of unused tailing bits, 0..7 */ - ); - -/* - * Type of the type-specific PER decoder function. - */ -typedef asn_dec_rval_t(per_type_decoder_f)( - const asn_codec_ctx_t *opt_codec_ctx, - const struct asn_TYPE_descriptor_s *type_descriptor, - const asn_per_constraints_t *constraints, void **struct_ptr, - asn_per_data_t *per_data); - -#ifdef __cplusplus -} -#endif - -#endif /* _PER_DECODER_H_ */ diff --git a/src/codec_utils/E2AP/per_opentype.c b/src/codec_utils/E2AP/per_opentype.c deleted file mode 100644 index 28f3cb67d..000000000 --- a/src/codec_utils/E2AP/per_opentype.c +++ /dev/null @@ -1,533 +0,0 @@ -/* - * Copyright (c) 2007 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include -#include - -typedef struct uper_ugot_key { - asn_per_data_t oldpd; /* Old per data source */ - size_t unclaimed; - size_t ot_moved; /* Number of bits moved by OT processing */ - int repeat; -} uper_ugot_key; - -static int uper_ugot_refill(asn_per_data_t *pd); -static int per_skip_bits(asn_per_data_t *pd, int skip_nbits); -static asn_dec_rval_t uper_sot_suck(const asn_codec_ctx_t *, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - void **sptr, asn_per_data_t *pd); - -/* - * Encode an "open type field". - * #10.1, #10.2 - */ -int -uper_open_type_put(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, const void *sptr, - asn_per_outp_t *po) { - void *buf; - void *bptr; - ssize_t size; - - ASN_DEBUG("Open type put %s ...", td->name); - - size = uper_encode_to_new_buffer(td, constraints, sptr, &buf); - if(size <= 0) return -1; - - ASN_DEBUG("Open type put %s of length %" ASN_PRI_SSIZE " + overhead (1byte?)", td->name, - size); - - bptr = buf; - do { - int need_eom = 0; - ssize_t may_save = uper_put_length(po, size, &need_eom); - ASN_DEBUG("Prepending length %" ASN_PRI_SSIZE - " to %s and allowing to save %" ASN_PRI_SSIZE, - size, td->name, may_save); - if(may_save < 0) break; - if(per_put_many_bits(po, bptr, may_save * 8)) break; - bptr = (char *)bptr + may_save; - size -= may_save; - if(need_eom && uper_put_length(po, 0, 0)) { - FREEMEM(buf); - return -1; - } - } while(size); - - FREEMEM(buf); - if(size) return -1; - - return 0; -} - -static asn_dec_rval_t -uper_open_type_get_simple(const asn_codec_ctx_t *ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, - asn_per_data_t *pd) { - asn_dec_rval_t rv; - ssize_t chunk_bytes; - int repeat; - uint8_t *buf = 0; - size_t bufLen = 0; - size_t bufSize = 0; - asn_per_data_t spd; - size_t padding; - - ASN__STACK_OVERFLOW_CHECK(ctx); - - ASN_DEBUG("Getting open type %s...", td->name); - - do { - chunk_bytes = uper_get_length(pd, -1, 0, &repeat); - if(chunk_bytes < 0) { - FREEMEM(buf); - ASN__DECODE_STARVED; - } - if(bufLen + chunk_bytes > bufSize) { - void *ptr; - bufSize = chunk_bytes + (bufSize << 2); - ptr = REALLOC(buf, bufSize); - if(!ptr) { - FREEMEM(buf); - ASN__DECODE_FAILED; - } - buf = ptr; - } - if(per_get_many_bits(pd, buf + bufLen, 0, chunk_bytes << 3)) { - FREEMEM(buf); - ASN__DECODE_STARVED; - } - bufLen += chunk_bytes; - } while(repeat); - - ASN_DEBUG("Getting open type %s encoded in %ld bytes", td->name, - (long)bufLen); - - memset(&spd, 0, sizeof(spd)); - spd.buffer = buf; - spd.nbits = bufLen << 3; - - ASN_DEBUG_INDENT_ADD(+4); - rv = td->op->uper_decoder(ctx, td, constraints, sptr, &spd); - ASN_DEBUG_INDENT_ADD(-4); - - if(rv.code == RC_OK) { - /* Check padding validity */ - padding = spd.nbits - spd.nboff; - if (((padding > 0 && padding < 8) || - /* X.691#10.1.3 */ - (spd.nboff == 0 && spd.nbits == 8 && spd.buffer == buf)) && - per_get_few_bits(&spd, padding) == 0) { - /* Everything is cool */ - FREEMEM(buf); - return rv; - } - FREEMEM(buf); - if(padding >= 8) { - ASN_DEBUG("Too large padding %d in open type", (int)padding); - ASN__DECODE_FAILED; - } else { - ASN_DEBUG("No padding"); - } - } else { - FREEMEM(buf); - /* rv.code could be RC_WMORE, nonsense in this context */ - rv.code = RC_FAIL; /* Noone would give us more */ - } - - return rv; -} - -static asn_dec_rval_t CC_NOTUSED -uper_open_type_get_complex(const asn_codec_ctx_t *ctx, - const asn_TYPE_descriptor_t *td, - asn_per_constraints_t *constraints, void **sptr, - asn_per_data_t *pd) { - uper_ugot_key arg; - asn_dec_rval_t rv; - ssize_t padding; - - ASN__STACK_OVERFLOW_CHECK(ctx); - - ASN_DEBUG("Getting open type %s from %s", td->name, - asn_bit_data_string(pd)); - arg.oldpd = *pd; - arg.unclaimed = 0; - arg.ot_moved = 0; - arg.repeat = 1; - pd->refill = uper_ugot_refill; - pd->refill_key = &arg; - pd->nbits = pd->nboff; /* 0 good bits at this point, will refill */ - pd->moved = 0; /* This now counts the open type size in bits */ - - ASN_DEBUG_INDENT_ADD(+4); - rv = td->op->uper_decoder(ctx, td, constraints, sptr, pd); - ASN_DEBUG_INDENT_ADD(-4); - -#define UPDRESTOREPD do { \ - /* buffer and nboff are valid, preserve them. */ \ - pd->nbits = arg.oldpd.nbits - (pd->moved - arg.ot_moved); \ - pd->moved = arg.oldpd.moved + (pd->moved - arg.ot_moved); \ - pd->refill = arg.oldpd.refill; \ - pd->refill_key = arg.oldpd.refill_key; \ - } while(0) - - if(rv.code != RC_OK) { - UPDRESTOREPD; - return rv; - } - - ASN_DEBUG("OpenType %s pd%s old%s unclaimed=%d, repeat=%d", td->name, - asn_bit_data_string(pd), - asn_bit_data_string(&arg.oldpd), - (int)arg.unclaimed, (int)arg.repeat); - - padding = pd->moved % 8; - if(padding) { - int32_t pvalue; - if(padding > 7) { - ASN_DEBUG("Too large padding %d in open type", - (int)padding); - rv.code = RC_FAIL; - UPDRESTOREPD; - return rv; - } - padding = 8 - padding; - ASN_DEBUG("Getting padding of %d bits", (int)padding); - pvalue = per_get_few_bits(pd, padding); - switch(pvalue) { - case -1: - ASN_DEBUG("Padding skip failed"); - UPDRESTOREPD; - ASN__DECODE_STARVED; - case 0: break; - default: - ASN_DEBUG("Non-blank padding (%d bits 0x%02x)", - (int)padding, (int)pvalue); - UPDRESTOREPD; - ASN__DECODE_FAILED; - } - } - if(pd->nboff != pd->nbits) { - ASN_DEBUG("Open type %s overhead pd%s old%s", td->name, - asn_bit_data_string(pd), asn_bit_data_string(&arg.oldpd)); - if(1) { - UPDRESTOREPD; - ASN__DECODE_FAILED; - } else { - arg.unclaimed += pd->nbits - pd->nboff; - } - } - - /* Adjust pd back so it points to original data */ - UPDRESTOREPD; - - /* Skip data not consumed by the decoder */ - if(arg.unclaimed) { - ASN_DEBUG("Getting unclaimed %d", (int)arg.unclaimed); - switch(per_skip_bits(pd, arg.unclaimed)) { - case -1: - ASN_DEBUG("Claim of %d failed", (int)arg.unclaimed); - ASN__DECODE_STARVED; - case 0: - ASN_DEBUG("Got claim of %d", (int)arg.unclaimed); - break; - default: - /* Padding must be blank */ - ASN_DEBUG("Non-blank unconsumed padding"); - ASN__DECODE_FAILED; - } - arg.unclaimed = 0; - } - - if(arg.repeat) { - ASN_DEBUG("Not consumed the whole thing"); - rv.code = RC_FAIL; - return rv; - } - - return rv; -} - - -asn_dec_rval_t -uper_open_type_get(const asn_codec_ctx_t *ctx, const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, - asn_per_data_t *pd) { - return uper_open_type_get_simple(ctx, td, constraints, sptr, pd); -} - -int -uper_open_type_skip(const asn_codec_ctx_t *ctx, asn_per_data_t *pd) { - asn_TYPE_descriptor_t s_td; - asn_TYPE_operation_t s_op; - asn_dec_rval_t rv; - - s_td.name = ""; - s_td.op = &s_op; - s_op.uper_decoder = uper_sot_suck; - - rv = uper_open_type_get(ctx, &s_td, 0, 0, pd); - if(rv.code != RC_OK) - return -1; - else - return 0; -} - -/* - * Internal functions. - */ - -static asn_dec_rval_t -uper_sot_suck(const asn_codec_ctx_t *ctx, const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, - asn_per_data_t *pd) { - asn_dec_rval_t rv; - - (void)ctx; - (void)td; - (void)constraints; - (void)sptr; - - while(per_get_few_bits(pd, 1) >= 0); - - rv.code = RC_OK; - rv.consumed = pd->moved; - - return rv; -} - -static int -uper_ugot_refill(asn_per_data_t *pd) { - uper_ugot_key *arg = pd->refill_key; - ssize_t next_chunk_bytes, next_chunk_bits; - ssize_t avail; - - asn_per_data_t *oldpd = &arg->oldpd; - - ASN_DEBUG("REFILLING pd->moved=%ld, oldpd->moved=%ld", - (long)pd->moved, (long)oldpd->moved); - - /* Advance our position to where pd is */ - oldpd->buffer = pd->buffer; - oldpd->nboff = pd->nboff; - oldpd->nbits -= pd->moved - arg->ot_moved; - oldpd->moved += pd->moved - arg->ot_moved; - arg->ot_moved = pd->moved; - - if(arg->unclaimed) { - /* Refill the container */ - if(per_get_few_bits(oldpd, 1)) - return -1; - if(oldpd->nboff == 0) { - assert(0); - return -1; - } - pd->buffer = oldpd->buffer; - pd->nboff = oldpd->nboff - 1; - pd->nbits = oldpd->nbits; - ASN_DEBUG("UNCLAIMED <- return from (pd->moved=%ld)", - (long)pd->moved); - return 0; - } - - if(!arg->repeat) { - ASN_DEBUG("Want more but refill doesn't have it"); - return -1; - } - - next_chunk_bytes = uper_get_length(oldpd, -1, 0, &arg->repeat); - ASN_DEBUG("Open type LENGTH %ld bytes at off %ld, repeat %ld", - (long)next_chunk_bytes, (long)oldpd->moved, (long)arg->repeat); - if(next_chunk_bytes < 0) return -1; - if(next_chunk_bytes == 0) { - pd->refill = 0; /* No more refills, naturally */ - assert(!arg->repeat); /* Implementation guarantee */ - } - next_chunk_bits = next_chunk_bytes << 3; - avail = oldpd->nbits - oldpd->nboff; - if(avail >= next_chunk_bits) { - pd->nbits = oldpd->nboff + next_chunk_bits; - arg->unclaimed = 0; - ASN_DEBUG("!+Parent frame %ld bits, alloting %ld [%ld..%ld] (%ld)", - (long)next_chunk_bits, (long)oldpd->moved, - (long)oldpd->nboff, (long)oldpd->nbits, - (long)(oldpd->nbits - oldpd->nboff)); - } else { - pd->nbits = oldpd->nbits; - arg->unclaimed = next_chunk_bits - avail; - ASN_DEBUG("!-Parent frame %ld, require %ld, will claim %ld", - (long)avail, (long)next_chunk_bits, - (long)arg->unclaimed); - } - pd->buffer = oldpd->buffer; - pd->nboff = oldpd->nboff; - ASN_DEBUG("Refilled pd%s old%s", - asn_bit_data_string(pd), asn_bit_data_string(oldpd)); - return 0; -} - -static int -per_skip_bits(asn_per_data_t *pd, int skip_nbits) { - int hasNonZeroBits = 0; - while(skip_nbits > 0) { - int skip; - - /* per_get_few_bits() is more efficient when nbits <= 24 */ - if(skip_nbits < 24) - skip = skip_nbits; - else - skip = 24; - skip_nbits -= skip; - - switch(per_get_few_bits(pd, skip)) { - case -1: return -1; /* Starving */ - case 0: continue; /* Skipped empty space */ - default: hasNonZeroBits = 1; continue; - } - } - return hasNonZeroBits; -} - -static asn_dec_rval_t -aper_open_type_get_simple(const asn_codec_ctx_t *ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { - asn_dec_rval_t rv; - ssize_t chunk_bytes; - int repeat; - uint8_t *buf = 0; - size_t bufLen = 0; - size_t bufSize = 0; - asn_per_data_t spd; - size_t padding; - - ASN__STACK_OVERFLOW_CHECK(ctx); - - ASN_DEBUG("Getting open type %s...", td->name); - - do { - chunk_bytes = aper_get_length(pd, -1, -1, &repeat); - if(chunk_bytes < 0) { - FREEMEM(buf); - ASN__DECODE_STARVED; - } - if(bufLen + chunk_bytes > bufSize) { - void *ptr; - bufSize = chunk_bytes + (bufSize << 2); - ptr = REALLOC(buf, bufSize); - if(!ptr) { - FREEMEM(buf); - ASN__DECODE_FAILED; - } - buf = ptr; - } - if(per_get_many_bits(pd, buf + bufLen, 0, chunk_bytes << 3)) { - FREEMEM(buf); - ASN__DECODE_STARVED; - } - bufLen += chunk_bytes; - } while(repeat); - - ASN_DEBUG("Getting open type %s encoded in %ld bytes", td->name, - (long)bufLen); - - memset(&spd, 0, sizeof(spd)); - spd.buffer = buf; - spd.nbits = bufLen << 3; - - ASN_DEBUG_INDENT_ADD(+4); - rv = td->op->aper_decoder(ctx, td, constraints, sptr, &spd); - ASN_DEBUG_INDENT_ADD(-4); - - if(rv.code == RC_OK) { - /* Check padding validity */ - padding = spd.nbits - spd.nboff; - if (((padding > 0 && padding < 8) || - /* X.691#10.1.3 */ - (spd.nboff == 0 && spd.nbits == 8 && spd.buffer == buf)) && - per_get_few_bits(&spd, padding) == 0) { - /* Everything is cool */ - FREEMEM(buf); - return rv; - } - FREEMEM(buf); - if(padding >= 8) { - ASN_DEBUG("Too large padding %d in open type", (int)padding); - ASN__DECODE_FAILED; - } else { - ASN_DEBUG("No padding"); - } - } else { - FREEMEM(buf); - /* rv.code could be RC_WMORE, nonsense in this context */ - rv.code = RC_FAIL; /* Noone would give us more */ - } - - return rv; -} - -int -aper_open_type_put(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po) { - void *buf; - void *bptr; - ssize_t size; - size_t toGo; - - ASN_DEBUG("Open type put %s ...", td->name); - - size = aper_encode_to_new_buffer(td, constraints, sptr, &buf); - if(size <= 0) return -1; - - for(bptr = buf, toGo = size; toGo;) { - ssize_t maySave = aper_put_length(po, -1, toGo); - if(maySave < 0) break; - if(per_put_many_bits(po, bptr, maySave * 8)) break; - bptr = (char *)bptr + maySave; - toGo -= maySave; - } - - FREEMEM(buf); - if(toGo) return -1; - - ASN_DEBUG("Open type put %s of length %ld + overhead (1byte?)", - td->name, size); - - return 0; -} - -asn_dec_rval_t -aper_open_type_get(const asn_codec_ctx_t *ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - void **sptr, asn_per_data_t *pd) { - - return aper_open_type_get_simple(ctx, td, constraints, sptr, pd); -} - -int -aper_open_type_skip(const asn_codec_ctx_t *ctx, asn_per_data_t *pd) { - asn_TYPE_descriptor_t s_td; - asn_dec_rval_t rv; - asn_TYPE_operation_t op_t; - - memset(&op_t, 0, sizeof(op_t)); - s_td.name = ""; - s_td.op = &op_t; - s_td.op->aper_decoder = uper_sot_suck; - - rv = aper_open_type_get(ctx, &s_td, 0, 0, pd); - if(rv.code != RC_OK) - return -1; - else - return 0; -} - - diff --git a/src/codec_utils/E2AP/per_opentype.h b/src/codec_utils/E2AP/per_opentype.h deleted file mode 100644 index 1493b2d8e..000000000 --- a/src/codec_utils/E2AP/per_opentype.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2007-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _PER_OPENTYPE_H_ -#define _PER_OPENTYPE_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -asn_dec_rval_t uper_open_type_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - void **sptr, asn_per_data_t *pd); - -int uper_open_type_skip(const asn_codec_ctx_t *opt_codec_ctx, - asn_per_data_t *pd); - -/* - * X.691 (2015/08), #11.2 - * Returns -1 if error is encountered. 0 if all OK. - */ -int uper_open_type_put(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po); - -asn_dec_rval_t aper_open_type_get(const asn_codec_ctx_t *opt_codec_ctx, - const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - void **sptr, asn_per_data_t *pd); - - -int aper_open_type_skip(const asn_codec_ctx_t *opt_codec_ctx, asn_per_data_t *pd); - -int aper_open_type_put(const asn_TYPE_descriptor_t *td, - const asn_per_constraints_t *constraints, - const void *sptr, asn_per_outp_t *po); - -#ifdef __cplusplus -} -#endif - -#endif /* _PER_OPENTYPE_H_ */ diff --git a/src/codec_utils/E2AP/per_support.c b/src/codec_utils/E2AP/per_support.c deleted file mode 100644 index 228567760..000000000 --- a/src/codec_utils/E2AP/per_support.c +++ /dev/null @@ -1,489 +0,0 @@ -/* - * Copyright (c) 2005-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#include -#include -#include - -/* - * X.691-201508 #10.9 General rules for encoding a length determinant. - * Get the optionally constrained length "n" from the stream. - */ -ssize_t -uper_get_length(asn_per_data_t *pd, int ebits, size_t lower_bound, - int *repeat) { - ssize_t value; - - *repeat = 0; - - /* #11.9.4.1 Encoding if constrained (according to effective bits) */ - if(ebits >= 0 && ebits <= 16) { - value = per_get_few_bits(pd, ebits); - if(value >= 0) value += lower_bound; - return value; - } - - value = per_get_few_bits(pd, 8); - if((value & 0x80) == 0) { /* #11.9.3.6 */ - return (value & 0x7F); - } else if((value & 0x40) == 0) { /* #11.9.3.7 */ - /* bit 8 ... set to 1 and bit 7 ... set to zero */ - value = ((value & 0x3f) << 8) | per_get_few_bits(pd, 8); - return value; /* potential -1 from per_get_few_bits passes through. */ - } else if(value < 0) { - ASN_DEBUG("END of stream reached for PER"); - return -1; - } - value &= 0x3f; /* this is "m" from X.691, #11.9.3.8 */ - if(value < 1 || value > 4) { - return -1; /* Prohibited by #11.9.3.8 */ - } - *repeat = 1; - return (16384 * value); -} - -/* - * Get the normally small length "n". - * This procedure used to decode length of extensions bit-maps - * for SET and SEQUENCE types. - */ -ssize_t -uper_get_nslength(asn_per_data_t *pd) { - ssize_t length; - - ASN_DEBUG("Getting normally small length"); - - if(per_get_few_bits(pd, 1) == 0) { - length = per_get_few_bits(pd, 6) + 1; - if(length <= 0) return -1; - ASN_DEBUG("l=%d", (int)length); - return length; - } else { - int repeat; - length = uper_get_length(pd, -1, 0, &repeat); - if(length >= 0 && !repeat) return length; - return -1; /* Error, or do not support >16K extensions */ - } -} - -/* - * Get the normally small non-negative whole number. - * X.691, #10.6 - */ -ssize_t -uper_get_nsnnwn(asn_per_data_t *pd) { - ssize_t value; - - value = per_get_few_bits(pd, 7); - if(value & 64) { /* implicit (value < 0) */ - value &= 63; - value <<= 2; - value |= per_get_few_bits(pd, 2); - if(value & 128) /* implicit (value < 0) */ - return -1; - if(value == 0) - return 0; - if(value >= 3) - return -1; - value = per_get_few_bits(pd, 8 * value); - return value; - } - - return value; -} - -/* - * X.691-11/2008, #11.6 - * Encoding of a normally small non-negative whole number - */ -int -uper_put_nsnnwn(asn_per_outp_t *po, int n) { - int bytes; - - if(n <= 63) { - if(n < 0) return -1; - return per_put_few_bits(po, n, 7); - } - if(n < 256) - bytes = 1; - else if(n < 65536) - bytes = 2; - else if(n < 256 * 65536) - bytes = 3; - else - return -1; /* This is not a "normally small" value */ - if(per_put_few_bits(po, bytes, 8)) - return -1; - - return per_put_few_bits(po, n, 8 * bytes); -} - - -/* X.691-2008/11, #11.5.6 -> #11.3 */ -int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *out_value, int nbits) { - unsigned long lhalf; /* Lower half of the number*/ - long half; - - if(nbits <= 31) { - half = per_get_few_bits(pd, nbits); - if(half < 0) return -1; - *out_value = half; - return 0; - } - - if((size_t)nbits > 8 * sizeof(*out_value)) - return -1; /* RANGE */ - - half = per_get_few_bits(pd, 31); - if(half < 0) return -1; - - if(uper_get_constrained_whole_number(pd, &lhalf, nbits - 31)) - return -1; - - *out_value = ((unsigned long)half << (nbits - 31)) | lhalf; - return 0; -} - - -/* X.691-2008/11, #11.5.6 -> #11.3 */ -int -uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, - int nbits) { - if(nbits <= 31) { - return per_put_few_bits(po, v, nbits); - } else { - /* Put higher portion first, followed by lower 31-bit */ - if(uper_put_constrained_whole_number_u(po, v >> 31, nbits - 31)) - return -1; - return per_put_few_bits(po, v, 31); - } -} - -/* - * X.691 (08/2015) #11.9 "General rules for encoding a length determinant" - * Put the length "n" (or part of it) into the stream. - */ -ssize_t -uper_put_length(asn_per_outp_t *po, size_t length, int *need_eom) { - int dummy = 0; - if(!need_eom) need_eom = &dummy; - - if(length <= 127) { /* #11.9.3.6 */ - *need_eom = 0; - return per_put_few_bits(po, length, 8) - ? -1 : (ssize_t)length; - } else if(length < 16384) { /* #10.9.3.7 */ - *need_eom = 0; - return per_put_few_bits(po, length|0x8000, 16) - ? -1 : (ssize_t)length; - } - - *need_eom = 0 == (length & 16383); - length >>= 14; - if(length > 4) { - *need_eom = 0; - length = 4; - } - - return per_put_few_bits(po, 0xC0 | length, 8) - ? -1 : (ssize_t)(length << 14); - -} - - -/* - * Put the normally small length "n" into the stream. - * This procedure used to encode length of extensions bit-maps - * for SET and SEQUENCE types. - */ -int -uper_put_nslength(asn_per_outp_t *po, size_t length) { - if(length <= 64) { - /* #11.9.3.4 */ - if(length == 0) return -1; - return per_put_few_bits(po, length - 1, 7) ? -1 : 0; - } else { - int need_eom = 0; - if(uper_put_length(po, length, &need_eom) != (ssize_t)length - || need_eom) { - /* This might happen in case of >16K extensions */ - return -1; - } - } - - return 0; -} - -static int -per__long_range(long lb, long ub, unsigned long *range_r) { - unsigned long bounds_range; - if((ub < 0) == (lb < 0)) { - bounds_range = ub - lb; - } else if(lb < 0) { - assert(ub >= 0); - bounds_range = 1 + ((unsigned long)ub + (unsigned long)-(lb + 1)); - } else { - assert(!"Unreachable"); - return -1; - } - *range_r = bounds_range; - return 0; -} - -int -per_long_range_rebase(long v, long lb, long ub, unsigned long *output) { - unsigned long range; - - assert(lb <= ub); - - if(v < lb || v > ub || per__long_range(lb, ub, &range) < 0) { - /* Range error. */ - return -1; - } - - /* - * Fundamentally what we're doing is returning (v-lb). - * However, this triggers undefined behavior when the word width - * of signed (v) is the same as the size of unsigned (*output). - * In practice, it triggers the UndefinedSanitizer. Therefore we shall - * compute the ranges accurately to avoid C's undefined behavior. - */ - if((v < 0) == (lb < 0)) { - *output = v-lb; - return 0; - } else if(v < 0) { - unsigned long rebased = 1 + (unsigned long)-(v+1) + (unsigned long)lb; - assert(rebased <= range); /* By construction */ - *output = rebased; - return 0; - } else if(lb < 0) { - unsigned long rebased = 1 + (unsigned long)-(lb+1) + (unsigned long)v; - assert(rebased <= range); /* By construction */ - *output = rebased; - return 0; - } else { - assert(!"Unreachable"); - return -1; - } -} - -int -per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp) { - unsigned long range; - - if(per__long_range(lb, ub, &range) != 0) { - return -1; - } - - if(inp > range) { - /* - * We can encode something in the given number of bits that technically - * exceeds the range. This is an avenue for security errors, - * so we don't allow that. - */ - return -1; - } - - if(inp <= LONG_MAX) { - *outp = (long)inp + lb; - } else { - *outp = (lb + LONG_MAX + 1) + (long)((inp - LONG_MAX) - 1); - } - - return 0; -} - -int32_t -aper_get_align(asn_per_data_t *pd) { - - if(pd->nboff & 0x7) { - ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)pd->nboff & 0x7)); - return per_get_few_bits(pd, 8 - (pd->nboff & 0x7)); - } - return 0; -} - -ssize_t -aper_get_length(asn_per_data_t *pd, int range, int ebits, int *repeat) { - ssize_t value; - - *repeat = 0; - - if (range <= 65536 && range >= 0) - return aper_get_nsnnwn(pd, range); - - if (aper_get_align(pd) < 0) - return -1; - - if(ebits >= 0) return per_get_few_bits(pd, ebits); - - value = per_get_few_bits(pd, 8); - if(value < 0) return -1; - if((value & 128) == 0) /* #10.9.3.6 */ - return (value & 0x7F); - if((value & 64) == 0) { /* #10.9.3.7 */ - value = ((value & 63) << 8) | per_get_few_bits(pd, 8); - if(value < 0) return -1; - return value; - } - value &= 63; /* this is "m" from X.691, #10.9.3.8 */ - if(value < 1 || value > 4) - return -1; - *repeat = 1; - return (16384 * value); -} - -ssize_t -aper_get_nslength(asn_per_data_t *pd) { - ssize_t length; - - ASN_DEBUG("Getting normally small length"); - - if(per_get_few_bits(pd, 1) == 0) { - length = per_get_few_bits(pd, 6) + 1; - if(length <= 0) return -1; - ASN_DEBUG("l=%ld", length); - return length; - } else { - int repeat; - length = aper_get_length(pd, -1, -1, &repeat); - if(length >= 0 && !repeat) return length; - return -1; /* Error, or do not support >16K extensions */ - } -} - -ssize_t -aper_get_nsnnwn(asn_per_data_t *pd, int range) { - ssize_t value; - int bytes = 0; - - ASN_DEBUG("getting nsnnwn with range %d", range); - - if(range <= 255) { - int i; - - if (range < 0) return -1; - /* 1 -> 8 bits */ - for (i = 1; i <= 8; i++) { - int upper = 1 << i; - if (upper >= range) - break; - } - value = per_get_few_bits(pd, i); - return value; - } else if (range == 256){ - /* 1 byte */ - bytes = 1; - } else if (range <= 65536) { - /* 2 bytes */ - bytes = 2; - } else { - return -1; - } - if (aper_get_align(pd) < 0) - return -1; - value = per_get_few_bits(pd, 8 * bytes); - return value; -} - -int aper_put_align(asn_per_outp_t *po) { - - if(po->nboff & 0x7) { - ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)po->nboff & 0x7)); - if(per_put_few_bits(po, 0x00, (8 - (po->nboff & 0x7)))) - return -1; - } - return 0; -} - -ssize_t -aper_put_length(asn_per_outp_t *po, int range, size_t length) { - - ASN_DEBUG("APER put length %zu with range %d", length, range); - - /* 10.9 X.691 Note 2 */ - if (range <= 65536 && range >= 0) - return aper_put_nsnnwn(po, range, length); - - if (aper_put_align(po) < 0) - return -1; - - if(length <= 127) /* #10.9.3.6 */{ - return per_put_few_bits(po, length, 8) - ? -1 : (ssize_t)length; - } - else if(length < 16384) /* #10.9.3.7 */ - return per_put_few_bits(po, length|0x8000, 16) - ? -1 : (ssize_t)length; - - length >>= 14; - if(length > 4) length = 4; - - return per_put_few_bits(po, 0xC0 | length, 8) - ? -1 : (ssize_t)(length << 14); -} - - -int -aper_put_nslength(asn_per_outp_t *po, size_t length) { - - if(length <= 64) { - /* #10.9.3.4 */ - 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) { - /* This might happen in case of >16K extensions */ - return -1; - } - } - - return 0; -} - -int -aper_put_nsnnwn(asn_per_outp_t *po, int range, int number) { - int bytes; - - ASN_DEBUG("aper put nsnnwn %d with range %d", number, range); - /* 10.5.7.1 X.691 */ - if(range < 0) { - int i; - for (i = 1; ; i++) { - int bits = 1 << (8 * i); - if (number <= bits) - break; - } - bytes = i; - assert(i <= 4); - } - if(range <= 255) { - int i; - for (i = 1; i <= 8; i++) { - int bits = 1 << i; - if (range <= bits) - break; - } - return per_put_few_bits(po, number, i); - } else if(range == 256) { - bytes = 1; - } else if(range <= 65536) { - bytes = 2; - } else { /* Ranges > 64K */ - int i; - for (i = 1; ; i++) { - int bits = 1 << (8 * i); - if (range <= bits) - break; - } - assert(i <= 4); - bytes = i; - } - if(aper_put_align(po) < 0) /* Aligning on octet */ - return -1; -/* if(per_put_few_bits(po, bytes, 8)) - return -1; -*/ - return per_put_few_bits(po, number, 8 * bytes); -} diff --git a/src/codec_utils/E2AP/per_support.h b/src/codec_utils/E2AP/per_support.h deleted file mode 100644 index 23079c94f..000000000 --- a/src/codec_utils/E2AP/per_support.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2005-2017 Lev Walkin . All rights reserved. - * Redistribution and modifications are permitted subject to BSD license. - */ -#ifndef _PER_SUPPORT_H_ -#define _PER_SUPPORT_H_ - -#include /* Platform-specific types */ -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Pre-computed PER constraints. - */ -typedef struct asn_per_constraint_s { - enum asn_per_constraint_flags { - APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */ - APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */ - APC_CONSTRAINED = 0x2, /* Fully constrained */ - APC_EXTENSIBLE = 0x4 /* May have extension */ - } flags; - int range_bits; /* Full number of bits in the range */ - int effective_bits; /* Effective bits */ - long lower_bound; /* "lb" value */ - long upper_bound; /* "ub" value */ -} asn_per_constraint_t; -typedef struct asn_per_constraints_s { - asn_per_constraint_t value; - asn_per_constraint_t size; - int (*value2code)(unsigned int value); - int (*code2value)(unsigned int code); -} asn_per_constraints_t; - -/* Temporary compatibility layer. Will get removed. */ -typedef struct asn_bit_data_s asn_per_data_t; -#define per_get_few_bits(data, bits) asn_get_few_bits(data, bits) -#define per_get_undo(data, bits) asn_get_undo(data, bits) -#define per_get_many_bits(data, dst, align, bits) \ - asn_get_many_bits(data, dst, align, bits) - -/* - * X.691 (08/2015) #11.9 "General rules for encoding a length determinant" - * Get the length "n" from the Unaligned PER stream. - */ -ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits, - size_t lower_bound, int *repeat); - -ssize_t aper_get_length(asn_per_data_t *pd, int range, - int effective_bound_bits, int *repeat); - -/* - * Get the normally small length "n". - */ -ssize_t uper_get_nslength(asn_per_data_t *pd); -ssize_t aper_get_nslength(asn_per_data_t *pd); - -/* - * Get the normally small non-negative whole number. - */ -ssize_t uper_get_nsnnwn(asn_per_data_t *pd); -ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range); - -/* X.691-2008/11, #11.5.6 */ -int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits); - - -/* Temporary compatibility layer. Will get removed. */ -typedef struct asn_bit_outp_s asn_per_outp_t; -#define per_put_few_bits(out, bits, obits) asn_put_few_bits(out, bits, obits) -#define per_put_many_bits(out, src, nbits) asn_put_many_bits(out, src, nbits) -#define per_put_aligned_flush(out) asn_put_aligned_flush(out) - - -/* - * Rebase the given value as an offset into the range specified by the - * lower bound (lb) and upper bound (ub). - * RETURN VALUES: - * -1: Conversion failed due to range problems. - * 0: Conversion was successful. - */ -int per_long_range_rebase(long v, long lb, long ub, unsigned long *output); -/* The inverse operation: restores the value by the offset and its bounds. */ -int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp); - -/* X.691-2008/11, #11.5 */ -int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits); - -/* - * X.691 (08/2015) #11.9 "General rules for encoding a length determinant" - * Put the length "whole_length" to the Unaligned PER stream. - * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed. - * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block. - * This function returns the number of units which may be flushed - * in the next units saving iteration. - */ -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); - -/* Align the current bit position to octet bundary */ -int aper_put_align(asn_per_outp_t *po); -int32_t aper_get_align(asn_per_data_t *pd); - -/* - * Put the normally small length "n" to the Unaligned PER stream. - * Returns 0 or -1. - */ -int uper_put_nslength(asn_per_outp_t *po, size_t length); - -int aper_put_nslength(asn_per_outp_t *po, size_t length); - -/* - * Put the normally small non-negative whole number. - */ -int uper_put_nsnnwn(asn_per_outp_t *po, int n); - -int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number); - -#ifdef __cplusplus -} -#endif - -#endif /* _PER_SUPPORT_H_ */ diff --git a/src/cu_stub/cu_stub_sctp.h b/src/cu_stub/cu_stub_sctp.h index d2e08b6b0..5102cc751 100644 --- a/src/cu_stub/cu_stub_sctp.h +++ b/src/cu_stub/cu_stub_sctp.h @@ -31,6 +31,7 @@ #define MAX_RETRY 5 #define DU_SCTP_DOWN 0 #define DU_SCTP_UP 1 +#define DU_SCTP_CONNECTING 2 /* Global variable declaration */ U8 socket_type; /* Socket type */ diff --git a/src/du_app/du_cell_mgr.c b/src/du_app/du_cell_mgr.c index fb7479831..c94b564b1 100644 --- a/src/du_app/du_cell_mgr.c +++ b/src/du_app/du_cell_mgr.c @@ -19,8 +19,6 @@ /* This file contains message handling functionality for DU APP */ #include "du_cell_mgr.h" #include "du_cfg.h" -#include "PLMN-Identity.h" -#include "RICeventTriggerDefinition.h" extern DuCfgParams duCfgParam; @@ -106,145 +104,6 @@ S16 procCellsToBeActivated(Cells_to_be_Activated_List_t cellsToActivate) return ROK; } -/****************************************************************** -* -* @brief Processes E2 Setup Response sent by RIC -* -* @details -* -* Function : procE2SetupRsp -* -* Functionality: Processes E2 Setup Response sent by RIC -* -* @params[in] E2AP_PDU_t ASN decoded E2AP message -* @return ROK - success -* RFAILED - failure -* -* ****************************************************************/ -S16 procE2SetupRsp(E2AP_PDU_t *e2apMsg) -{ - E2setupResponse_t *e2SetRspMsg; - E2apMsgDb e2SetupRspDb; - U8 idx; - - DU_LOG("\nE2AP : E2 Setup Response received"); - duCb.e2Status = TRUE; //Set E2 status as true - e2SetRspMsg = &e2apMsg->choice.successfulOutcome->value.choice.E2setupResponse; - - for(idx=0; idxprotocolIEs.list.count; idx++) - { - switch(e2SetRspMsg->protocolIEs.list.array[idx]->id) - { - case ProtocolIE_IDE2_id_GlobalRIC_ID: - { - /* To store the Ric Id Params */ - U32 recvBufLen; - memset(&e2SetupRspDb.plmn, 0, sizeof(PLMN_IdentityE2_t)); - - recvBufLen = sizeof(e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.pLMN_Identity); - - bitStringToInt(&e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.ric_ID, &e2SetupRspDb.ricId); - - aper_decode(0, &asn_DEF_PLMN_IdentityE2, (void **)&e2SetupRspDb.plmn, &e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.pLMN_Identity, recvBufLen, 0, 0); - //xer_fprint(stdout, &asn_DEF_PLMN_IdentityE2, &e2SetupRspDb.plmn); - - break; - } - default: - DU_LOG("\nE2AP : Invalid IE received in E2SetupRsp:%ld", - e2SetRspMsg->protocolIEs.list.array[idx]->id); - break; - } - } - RETVALUE(ROK); -} - -/****************************************************************** -* -* @brief Processes RIC Subscription Req sent by RIC -* -* @details -* -* Function : procRicSubsReq -* -* Functionality: Processes E2 Setup Response sent by CU -* -* @params[in] E2AP_PDU_t ASN decoded E2AP message -* @return ROK - success -* RFAILED - failure -* -* ****************************************************************/ - -S16 procRicSubsReq(E2AP_PDU_t *e2apMsg) -{ - S16 ret = ROK; - U8 idx; - U8 ied; - RICsubscriptionRequest_t *ricSubsReq; - RICaction_ToBeSetup_ItemIEs_t *actionItem; - E2apMsgDb ricReqDb; - - DU_LOG("\nE2AP : Ric Subscription request received"); - ricSubsReq = &e2apMsg->choice.initiatingMessage->value.choice.RICsubscriptionRequest; - - for(idx=0; idxprotocolIEs.list.count; idx++) - { - switch(ricSubsReq->protocolIEs.list.array[idx]->id) - { - case ProtocolIE_IDE2_id_RICrequestID: - { - ricReqDb.ricReqId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICrequestID.ricRequestorID; - ricReqDb.ricInstanceId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICrequestID.ricInstanceID; - break; - } - case ProtocolIE_IDE2_id_RANfunctionID: - { - ricReqDb.ranFuncId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RANfunctionID; - break; - } - case ProtocolIE_IDE2_id_RICsubscriptionDetails: - { - U32 recvBufLen; - memset(&ricReqDb.ricEventTrigger, 0, sizeof(RICeventTriggerDefinition_t)); - - recvBufLen = sizeof(ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricEventTriggerDefinition); - - aper_decode(0, &asn_DEF_RICeventTriggerDefinition, (void **)&ricReqDb.ricEventTrigger, &(ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricEventTriggerDefinition), recvBufLen, 0, 0); - //xer_fprint(stdout, &asn_DEF_RICeventTriggerDefinition, &ricReqDb.ricEventTrigger); - - actionItem = *ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricAction_ToBeSetup_List.list.array; - - for(ied = 0; ied < ricSubsReq->protocolIEs.list.array[idx]->value.choice.\ - RICsubscriptionDetails.ricAction_ToBeSetup_List.list.count; ied++) - { - switch(actionItem->id) - { - case ProtocolIE_IDE2_id_RICaction_ToBeSetup_Item: - { - ricReqDb.ricActionId = actionItem->value.choice.RICaction_ToBeSetup_Item.ricActionID; - ricReqDb.ricActionType = actionItem->value.choice.RICaction_ToBeSetup_Item.ricActionType; - break; - } - default: - DU_LOG("\nE2AP : Invalid IE received in RicSetupLst:%ld",actionItem->id); - break; - } - } - - break; - } - - default: - DU_LOG("\nE2AP : Invalid IE received in Ric SubsReq:%ld", - ricSubsReq->protocolIEs.list.array[idx]->id); - break; - } - } - ret = BuildAndSendRicSubscriptionRsp(); - - RETVALUE(ret); -} - /****************************************************************** * * @brief Processes F1 Setup Response sent by CU diff --git a/src/du_app/du_cfg.c b/src/du_app/du_cfg.c index a142f3560..8f958fc9e 100644 --- a/src/du_app/du_cfg.c +++ b/src/du_app/du_cfg.c @@ -184,6 +184,24 @@ S16 readMacCfg() RETVALUE(ROK); } + +/******************************************************************* + * + * @brief Configures the DU Parameters + * + * @details + * + * Function : fillDuPort + * + * Functionality: + * - fills the DU Ports. + * + * @params[in] duPort array to be filled + * @return ROK - success + * RFAILED - failure + * + * ****************************************************************/ + S16 fillDuPort(U16 *duPort) { duPort[F1_INTERFACE] = DU_PORT; /* DU Port idx 0 38472 */ @@ -228,12 +246,10 @@ S16 readCfg() /* F1 CU IP Address and Port*/ duCfgParam.sctpParams.cuIpAddr.ipV4Addr = ipv4_cu; duCfgParam.sctpParams.cuPort = CU_PORT; - duCfgParam.sctpParams.itfType.f1Itf = F1_INTERFACE; /* Fill RIC Params */ duCfgParam.sctpParams.ricIpAddr.ipV4Addr = ipv4_ric; duCfgParam.sctpParams.ricPort = RIC_PORT; - duCfgParam.sctpParams.itfType.e2Itf = E2_INTERFACE; /* EGTP Parameters */ diff --git a/src/du_app/du_cfg.h b/src/du_app/du_cfg.h index 1d1ce122e..f7dd4d88f 100644 --- a/src/du_app/du_cfg.h +++ b/src/du_app/du_cfg.h @@ -837,12 +837,6 @@ typedef struct f1Ipaddr U32 ipV4Addr; }F1IpAddr; -typedef struct -{ - U8 f1Itf; - U8 e2Itf; -}ItfType; - typedef struct sctpParams { F1IpAddr duIpAddr; @@ -851,7 +845,6 @@ typedef struct sctpParams U16 cuPort; F1IpAddr ricIpAddr; U16 ricPort; - ItfType itfType; }SctpParams; typedef struct f1EgtpParams diff --git a/src/du_app/du_e2ap_msg_hdl.c b/src/du_app/du_e2ap_msg_hdl.c index 6c98f3ec8..4814dc9e5 100644 --- a/src/du_app/du_e2ap_msg_hdl.c +++ b/src/du_app/du_e2ap_msg_hdl.c @@ -27,9 +27,6 @@ DuCfgParams duCfgParam; char encBuf[ENC_BUF_MAX_LEN]; int encBufSize; -S16 SendE2APMsg(Region , Pool ); - - /******************************************************************* * * @brief Writes the encoded chunks into a buffer @@ -166,49 +163,6 @@ S16 BuildGlobalgNB(GlobalE2node_gNB_ID_t *gNbId) RETVALUE(ROK); } -#if 0 -RANfunction_ItemIEs_t* BuildRanFuncItems(RANfunction_ItemIEs_t *items) -{ - U8 byteSize; - U8 tmp; - byteSize = 3; - - items->id = ProtocolIE_IDE2_id_RANfunctionID; - items->criticality = CriticalityE2_ignore; - items->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; - - items->value.choice.RANfunction_Item.ranFunctionID = 1; - BuildPlmn(duCfgParam.srvdCellLst[0].duCellInfo.cellInfo.nrCgi.plmn, &items->value.choice.RANfunction_Item.ranFunctionDefinition); - items->value.choice.RANfunction_Item.ranFunctionRevision = 4; - - RETVALUE(items); -} - -S16 BuildRANList(RANfunctions_List_t *ranFuncList) -{ - U8 elementCnt; - U8 eleidx; - S16 ret; - RANfunction_ItemIEs_t *funcItems; - - elementCnt = 1; - ranFuncList->list.count = elementCnt; - ranFuncList->list.size = elementCnt * sizeof(RANfunction_ItemIEs_t); - DU_ALLOC(ranFuncList->list.array, \ - ranFuncList->list.size); - if(ranFuncList->list.array == NULLP) - { - DU_LOG("\nE2AP : Memory allocation for RAN Function List failed"); - RETVALUE(RFAILED); - } - /* Fill RAN function Params */ - DU_ALLOC(ranFuncList->list.array[0], sizeof(RANfunction_ItemIEs_t)); - BuildRanFuncItems(ranFuncList->list.array[0]); - - RETVALUE(ROK); -} -#endif - /******************************************************************* * * @brief Builds and Send the E2SetupRequest @@ -557,6 +511,145 @@ S16 BuildAndSendRicSubscriptionRsp() RETVALUE(ROK); } +/****************************************************************** +* +* @brief Processes E2 Setup Response sent by RIC +* +* @details +* +* Function : procE2SetupRsp +* +* Functionality: Processes E2 Setup Response sent by RIC +* +* @params[in] E2AP_PDU_t ASN decoded E2AP message +* @return ROK - success +* RFAILED - failure +* +* ****************************************************************/ +S16 procE2SetupRsp(E2AP_PDU_t *e2apMsg) +{ + E2setupResponse_t *e2SetRspMsg; + E2apMsgDb e2SetupRspDb; + U8 idx; + + DU_LOG("\nE2AP : E2 Setup Response received"); + duCb.e2Status = TRUE; //Set E2 status as true + e2SetRspMsg = &e2apMsg->choice.successfulOutcome->value.choice.E2setupResponse; + + for(idx=0; idxprotocolIEs.list.count; idx++) + { + switch(e2SetRspMsg->protocolIEs.list.array[idx]->id) + { + case ProtocolIE_IDE2_id_GlobalRIC_ID: + { + /* To store the Ric Id Params */ + U32 recvBufLen; + memset(&e2SetupRspDb.plmn, 0, sizeof(PLMN_IdentityE2_t)); + + recvBufLen = sizeof(e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.pLMN_Identity); + + bitStringToInt(&e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.ric_ID, &e2SetupRspDb.ricId); + + aper_decode(0, &asn_DEF_PLMN_IdentityE2, (void **)&e2SetupRspDb.plmn, &e2SetRspMsg->protocolIEs.list.array[idx]->value.choice.GlobalRIC_ID.pLMN_Identity, recvBufLen, 0, 0); + //xer_fprint(stdout, &asn_DEF_PLMN_IdentityE2, &e2SetupRspDb.plmn); + + break; + } + default: + DU_LOG("\nE2AP : Invalid IE received in E2SetupRsp:%ld", + e2SetRspMsg->protocolIEs.list.array[idx]->id); + break; + } + } + RETVALUE(ROK); +} + +/****************************************************************** +* +* @brief Processes RIC Subscription Req sent by RIC +* +* @details +* +* Function : procRicSubsReq +* +* Functionality: Processes E2 Setup Response sent by CU +* +* @params[in] E2AP_PDU_t ASN decoded E2AP message +* @return ROK - success +* RFAILED - failure +* +* ****************************************************************/ + +S16 procRicSubsReq(E2AP_PDU_t *e2apMsg) +{ + S16 ret = ROK; + U8 idx; + U8 ied; + RICsubscriptionRequest_t *ricSubsReq; + RICaction_ToBeSetup_ItemIEs_t *actionItem; + E2apMsgDb ricReqDb; + + DU_LOG("\nE2AP : Ric Subscription request received"); + ricSubsReq = &e2apMsg->choice.initiatingMessage->value.choice.RICsubscriptionRequest; + + for(idx=0; idxprotocolIEs.list.count; idx++) + { + switch(ricSubsReq->protocolIEs.list.array[idx]->id) + { + case ProtocolIE_IDE2_id_RICrequestID: + { + ricReqDb.ricReqId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICrequestID.ricRequestorID; + ricReqDb.ricInstanceId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICrequestID.ricInstanceID; + break; + } + case ProtocolIE_IDE2_id_RANfunctionID: + { + ricReqDb.ranFuncId = ricSubsReq->protocolIEs.list.array[idx]->value.choice.RANfunctionID; + break; + } + case ProtocolIE_IDE2_id_RICsubscriptionDetails: + { + U32 recvBufLen; + memset(&ricReqDb.ricEventTrigger, 0, sizeof(RICeventTriggerDefinition_t)); + + recvBufLen = sizeof(ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricEventTriggerDefinition); + + aper_decode(0, &asn_DEF_RICeventTriggerDefinition, (void **)&ricReqDb.ricEventTrigger, &(ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricEventTriggerDefinition), recvBufLen, 0, 0); + //xer_fprint(stdout, &asn_DEF_RICeventTriggerDefinition, &ricReqDb.ricEventTrigger); + + actionItem = *ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricAction_ToBeSetup_List.list.array; + + for(ied = 0; ied < ricSubsReq->protocolIEs.list.array[idx]->value.choice.\ + RICsubscriptionDetails.ricAction_ToBeSetup_List.list.count; ied++) + { + switch(actionItem->id) + { + case ProtocolIE_IDE2_id_RICaction_ToBeSetup_Item: + { + ricReqDb.ricActionId = actionItem->value.choice.RICaction_ToBeSetup_Item.ricActionID; + ricReqDb.ricActionType = actionItem->value.choice.RICaction_ToBeSetup_Item.ricActionType; + break; + } + default: + DU_LOG("\nE2AP : Invalid IE received in RicSetupLst:%ld",actionItem->id); + break; + } + } + + break; + } + + default: + DU_LOG("\nE2AP : Invalid IE received in Ric SubsReq:%ld", + ricSubsReq->protocolIEs.list.array[idx]->id); + break; + } + } + ret = BuildAndSendRicSubscriptionRsp(); + + RETVALUE(ret); +} + /******************************************************************* * @@ -750,7 +843,7 @@ S16 SendE2APMsg(Region region, Pool pool) { SPrntMsg(mBuf, 0,0); - if(sctpSend(mBuf, duCfgParam.sctpParams.itfType.e2Itf) != ROK) + if(sctpSend(mBuf, E2_INTERFACE) != ROK) { DU_LOG("\nE2AP : SCTP Send for E2 failed"); SPutMsg(mBuf); @@ -759,7 +852,7 @@ S16 SendE2APMsg(Region region, Pool pool) } else { - DU_LOG("\nF1AP : SAddPstMsgMult failed"); + DU_LOG("\nE2AP : SAddPstMsgMult failed"); SPutMsg(mBuf); RETVALUE(RFAILED); } @@ -767,7 +860,7 @@ S16 SendE2APMsg(Region region, Pool pool) } else { - DU_LOG("\nF1AP : Failed to allocate memory"); + DU_LOG("\nE2AP : Failed to allocate memory"); RETVALUE(RFAILED); } @@ -890,3 +983,7 @@ void E2APMsgHdlr(Buffer *mBuf) }/* End of switch(e2apMsg->present) */ } /* End of E2APMsgHdlr */ + +/********************************************************************** + End of file +**********************************************************************/ diff --git a/src/du_app/du_e2ap_msg_hdl.h b/src/du_app/du_e2ap_msg_hdl.h index fee93b502..bf0d99143 100644 --- a/src/du_app/du_e2ap_msg_hdl.h +++ b/src/du_app/du_e2ap_msg_hdl.h @@ -16,7 +16,7 @@ ################################################################################ *******************************************************************************/ -/* This file contains all F1AP message handler related functionality */ +/* This file contains all E2AP message handler related functionality */ #include #include #include @@ -57,6 +57,7 @@ typedef struct e2apDb }E2apMsgDb; S16 BuildAndSendE2SetupReq(); +S16 SendE2APMsg(Region , Pool ); void E2APMsgHdlr(Buffer *mBuf); /********************************************************************** diff --git a/src/du_app/du_f1ap_msg_hdl.c b/src/du_app/du_f1ap_msg_hdl.c index 3e78cfc3d..713db1c07 100644 --- a/src/du_app/du_f1ap_msg_hdl.c +++ b/src/du_app/du_f1ap_msg_hdl.c @@ -994,7 +994,7 @@ S16 SendF1APMsg(Region region, Pool pool) { SPrntMsg(mBuf, 0,0); - if(sctpSend(mBuf, duCfgParam.sctpParams.itfType.f1Itf) != ROK) + if(sctpSend(mBuf, F1_INTERFACE) != ROK) { DU_LOG("\nF1AP : SCTP Send failed"); SPutMsg(mBuf); diff --git a/src/du_app/du_mgr.h b/src/du_app/du_mgr.h index 80fe02a9e..562aaef4e 100644 --- a/src/du_app/du_mgr.h +++ b/src/du_app/du_mgr.h @@ -74,7 +74,7 @@ #define EVTTNLMGMTREQ 6 #define EVTTNLMGMTCFM 7 #define EVTTTIIND 8 -#define EVTSTRTPOLL 9 +#define EVTSTARTPOLL 9 #define EVTRICDATA 10 /* Selector */ @@ -176,7 +176,7 @@ typedef struct duCb TskInit init; /* DU Init */ //DuLSapCb **macSap; /* MAC SAP */ Bool f1Status; /* Status of F1 connection */ - Bool e2Status; /* Status of F1 connection */ + Bool e2Status; /* Status of E2 connection */ CmHashListCp cellLst; /* List of cells at DU APP of type DuCellCb */ CmHashListCp actvCellLst; /* List of cells activated/to be activated of type DuCellCb */ /* pointer to store the address of macCellCfg params used to send du-app to MAC */ diff --git a/src/du_app/du_msg_hdl.c b/src/du_app/du_msg_hdl.c index ecf5a3e13..f7ae39945 100644 --- a/src/du_app/du_msg_hdl.c +++ b/src/du_app/du_msg_hdl.c @@ -920,7 +920,7 @@ S16 duSctpNtfyHdl(Buffer *mBuf, CmInetSctpNotification *ntfy) { DU_LOG("\nDU_APP : SCTP communication UP"); //Setup F1-C - if(ntfy->u.assocChange.assocId == f1Params.assocId & (!duCb.f1Status)) + if(ntfy->u.assocChange.assocId == f1Params.assocId) { /* Build and send F1 Setup response */ if(BuildAndSendF1SetupReq() != ROK) @@ -929,7 +929,7 @@ S16 duSctpNtfyHdl(Buffer *mBuf, CmInetSctpNotification *ntfy) } } //Setup E2 - if(ntfy->u.assocChange.assocId == ricParams.assocId & (!duCb.e2Status)) + if(ntfy->u.assocChange.assocId == ricParams.assocId) { /* Build and send F1 Setup response */ if(BuildAndSendE2SetupReq() != ROK) @@ -1398,32 +1398,6 @@ S16 duSendSchCfg() return ROK; } -/******************************************************************* - * - * @brief Checks the status of the received information - * - * @details - * - * Function : duCheckReqStatus - * - * Functionality: - * Checks the status of the received information - * - * @params[in] Confirm status - * @return ROK - success - * RFAILED - failure - * - ******************************************************************/ -S16 duCheckReqStatus(CmStatus *cfm) -{ - S16 ret = ROK; - if(cfm->status != LCM_PRIM_OK) - { - DU_LOG("\nDU_APP : Failed to process the request successfully"); - ret = RFAILED; - } - RETVALUE(ret); -} /************************************************************************** * @brief Function to configure SCTP params and @@ -1445,24 +1419,20 @@ S16 duCheckReqStatus(CmStatus *cfm) S16 duLayerConfigComplete() { S16 ret = ROK; - CmStatus cfm; DU_LOG("\nDU_APP : Configuring all Layer is complete"); - duSctpCfgReq(duCfgParam.sctpParams, &cfm); - if((ret = duCheckReqStatus(&cfm)) != ROK) + if((ret = duSctpCfgReq(duCfgParam.sctpParams)) != ROK) { DU_LOG("\nDU_APP : Failed configuring Sctp Params"); ret = RFAILED; } - duSctpAssocReq(duCfgParam.sctpParams.itfType.f1Itf, &cfm); - if((ret = duCheckReqStatus(&cfm)) != ROK) + if((ret = duSctpAssocReq(F1_INTERFACE)) != ROK) { DU_LOG("\nDU_APP : Failed to send AssocReq F1"); ret = RFAILED; } - duSctpAssocReq(duCfgParam.sctpParams.itfType.e2Itf, &cfm); - if((ret = duCheckReqStatus(&cfm)) != ROK) + if((ret = duSctpAssocReq(E2_INTERFACE)) != ROK) { DU_LOG("\nDU_APP : Failed to send AssocReq E2"); ret = RFAILED; diff --git a/src/du_app/du_sctp.c b/src/du_app/du_sctp.c index 8e6bcb719..28576af18 100644 --- a/src/du_app/du_sctp.c +++ b/src/du_app/du_sctp.c @@ -82,7 +82,7 @@ S16 sctpActvTsk(Pst *pst, Buffer *mBuf) { switch(pst->event) { - case EVTSTRTPOLL: + case EVTSTARTPOLL: { sctpSockPoll(); break; @@ -94,6 +94,32 @@ S16 sctpActvTsk(Pst *pst, Buffer *mBuf) SExitTsk(); return ROK; } +/******************************************************************* + * + * @brief Checks the status of the received information + * + * @details + * + * Function : duCheckReqStatus + * + * Functionality: + * Checks the status of the received information + * + * @params[in] Confirm status + * @return ROK - success + * RFAILED - failure + * + ******************************************************************/ +S16 duCheckReqStatus(CmStatus *cfm) +{ + S16 ret = ROK; + if(cfm->status != LCM_PRIM_OK) + { + DU_LOG("\nDU_APP : Failed to process the request successfully"); + ret = RFAILED; + } + RETVALUE(ret); +} /************************************************************************** * @brief Function to configure the Sctp Params during config Request @@ -114,15 +140,18 @@ S16 sctpActvTsk(Pst *pst, Buffer *mBuf) * ***************************************************************************/ -S16 duSctpCfgReq(SctpParams sctpCfg, CmStatus *cfm) +S16 duSctpCfgReq(SctpParams sctpCfg) { + S16 ret = ROK; + CmStatus cfm; + /* Fill F1 Params */ f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres; f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr; f1Params.destPort = sctpCfg.cuPort; f1Params.itfState = DU_SCTP_DOWN; - f1Params.srcPort = sctpCfg.duPort[0]; - f1Params.recvMsgSet = ROK; + f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE]; + f1Params.recvMsgSet = ROK; cmMemset ((U8 *)&f1Params.sockFd, -1, sizeof(CmInetFd)); fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr); fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr); @@ -132,8 +161,8 @@ S16 duSctpCfgReq(SctpParams sctpCfg, CmStatus *cfm) ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr; ricParams.destPort = sctpCfg.ricPort; ricParams.itfState = DU_SCTP_DOWN; - ricParams.srcPort = sctpCfg.duPort[1]; - ricParams.recvMsgSet = ROK; + ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE]; + ricParams.recvMsgSet = ROK; cmMemset ((U8 *)&ricParams.sockFd, -1, sizeof(CmInetFd)); fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr); fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr); @@ -145,10 +174,12 @@ S16 duSctpCfgReq(SctpParams sctpCfg, CmStatus *cfm) pollingState = FALSE; /* Fill Cfm Status */ - cfm->status = LCM_PRIM_OK; - cfm->reason = LCM_REASON_NOT_APPL; + cfm.status = LCM_PRIM_OK; + cfm.reason = LCM_REASON_NOT_APPL; - RETVALUE(ROK); + ret = duCheckReqStatus(&cfm); + + RETVALUE(ret); } /******************************************************************* @@ -263,11 +294,11 @@ S16 establishReq(DuSctpDestCb *paramPtr) paramPtr->itfState = DU_SCTP_UP; } - /* Post the EVTSTRTPOLL Msg */ + /* Post the EVTSTARTPOLL Msg */ if(!pollingState) { pollingState = TRUE; - duFillSctpPst(&pst, EVTSTRTPOLL); + duFillSctpPst(&pst, EVTSTARTPOLL); } RETVALUE(ret); @@ -292,10 +323,12 @@ S16 establishReq(DuSctpDestCb *paramPtr) * *******************************************************************************/ -S16 duSctpAssocReq(U8 itfType, CmStatus *cfm) +S16 duSctpAssocReq(U8 itfType) { S16 ret = ROK; + CmStatus cfm; DuSctpDestCb *paramPtr = NULLP; + if(SGetSBuf(DU_APP_MEM_REGION, DU_POOL, (Data **)¶mPtr, (Size)sizeof(DuSctpDestCb)) != ROK) { printf("\nDU_APP : Failed to allocate memory"); @@ -324,14 +357,15 @@ S16 duSctpAssocReq(U8 itfType, CmStatus *cfm) if(ret != ROK) { DU_LOG("\nSCTP : ASSOC Req Failed."); - cfm->status = LCM_PRIM_NOK; - cfm->reason = LCM_REASON_NOT_APPL; + cfm.status = LCM_PRIM_NOK; + cfm.reason = LCM_REASON_NOT_APPL; } else { - cfm->status = LCM_PRIM_OK; - cfm->reason = LCM_REASON_NOT_APPL; + cfm.status = LCM_PRIM_OK; + cfm.reason = LCM_REASON_NOT_APPL; } + ret = duCheckReqStatus(&cfm); RETVALUE(ret); } diff --git a/src/du_app/du_sctp.h b/src/du_app/du_sctp.h index edace7ef8..70a59bb70 100644 --- a/src/du_app/du_sctp.h +++ b/src/du_app/du_sctp.h @@ -32,6 +32,7 @@ #define DU_SCTP_DOWN 0 #define DU_SCTP_UP 1 +#define DU_SCTP_CONNECTING 2 #define MAX_RETRY 5 /* Global variable declaration */ @@ -79,11 +80,11 @@ void sctpAssocReq(); void sendToDuApp(Buffer *mBuf, Event event); S16 sctpSend(Buffer *mBuf, U8 itfType); typedef S16 (*SctpNtfy) ARGS((Buffer *mBuf, CmInetSctpNotification *ntfy)); -S16 duSctpCfgReq(SctpParams sctpCfg, CmStatus *cfm); +S16 duSctpCfgReq(SctpParams sctpCfg); S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr); S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr); S16 establishReq(DuSctpDestCb *paramPtr); -S16 duSctpAssocReq(U8 itfType, CmStatus *cfm); +S16 duSctpAssocReq(U8 itfType); S16 duFillSctpPst(Pst *pst, Event event); S16 sctpSetSockOpts(CmInetFd *sock_Fd); S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo, Bool recvMsgSet); -- 2.16.6