spec: S1AP/NGAP update to v16.4.0(2021-01-04)

This commit is contained in:
Sukchan Lee 2021-01-24 23:43:42 -05:00
parent 61778f9142
commit ada01fca8f
3130 changed files with 280188 additions and 91812 deletions

View File

@ -4,447 +4,72 @@
*/
#include <asn_internal.h>
#include <ANY.h>
#include <errno.h>
asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
sizeof(ANY_t),
offsetof(ANY_t, _asn_ctx),
ASN_OSUBV_ANY
sizeof(ANY_t),
offsetof(ANY_t, _asn_ctx),
ASN_OSUBV_ANY
};
asn_TYPE_operation_t asn_OP_ANY = {
OCTET_STRING_free,
OCTET_STRING_print,
OCTET_STRING_compare,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_hex,
ANY_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OCTET_STRING_print,
#else
0,
0,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0, 0, 0, 0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
#else
ANY_decode_uper,
ANY_encode_uper,
ANY_decode_aper,
ANY_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
0, /* Random fill is not defined for ANY type */
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_hex,
ANY_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
0,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
ANY_decode_uper,
ANY_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
ANY_decode_aper,
ANY_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
0, /* Random fill is not defined for ANY type */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_ANY = {
"ANY",
"ANY",
&asn_OP_ANY,
0, 0, 0, 0,
{ 0, 0, asn_generic_no_constraint }, /* No constraints */
0, 0, /* No members */
&asn_SPC_ANY_specs,
"ANY",
"ANY",
&asn_OP_ANY,
0, 0, 0, 0,
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_no_constraint
}, /* No constraints */
0, 0, /* No members */
&asn_SPC_ANY_specs,
};
#undef RETURN
#define RETURN(_code) \
do { \
asn_dec_rval_t tmprval; \
tmprval.code = _code; \
tmprval.consumed = consumed_myself; \
return tmprval; \
} while(0)
asn_enc_rval_t
ANY_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) {
if(flags & XER_F_CANONICAL) {
/*
* Canonical XER-encoding of ANY type is not supported.
*/
ASN__ENCODE_FAILED;
}
/* Dump as binary */
return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
}
struct _callback_arg {
uint8_t *buffer;
size_t offset;
size_t size;
};
static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
int
ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
struct _callback_arg arg;
asn_enc_rval_t erval = {0,0,0};
if(!st || !td) {
errno = EINVAL;
return -1;
}
if(!sptr) {
if(st->buf) FREEMEM(st->buf);
st->size = 0;
return 0;
}
arg.offset = arg.size = 0;
arg.buffer = 0;
erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
if(erval.encoded == -1) {
if(arg.buffer) FREEMEM(arg.buffer);
return -1;
}
assert((size_t)erval.encoded == arg.offset);
if(st->buf) FREEMEM(st->buf);
st->buf = arg.buffer;
st->size = arg.offset;
return 0;
}
int
ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
uint8_t *buffer = NULL;
ssize_t erval;
if(!st || !td) {
errno = EINVAL;
return -1;
}
if(!sptr) {
if(st->buf) FREEMEM(st->buf);
st->size = 0;
return 0;
}
erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
if(erval == -1) {
if(buffer) FREEMEM(buffer);
return -1;
}
assert((size_t)erval > 0);
if(st->buf) FREEMEM(st->buf);
st->buf = buffer;
st->size = erval;
return 0;
}
ANY_t *
ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
ANY_t tmp;
ANY_t *st;
if(!td || !sptr) {
errno = EINVAL;
return 0;
}
memset(&tmp, 0, sizeof(tmp));
if(ANY_fromType(&tmp, td, sptr)) return 0;
st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
if(st) {
*st = tmp;
return st;
} else {
FREEMEM(tmp.buf);
return 0;
}
}
ANY_t *
ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) {
ANY_t tmp;
ANY_t *st;
if(!td || !sptr) {
errno = EINVAL;
return 0;
}
memset(&tmp, 0, sizeof(tmp));
if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
if(st) {
*st = tmp;
return st;
} else {
FREEMEM(tmp.buf);
return 0;
}
}
int
ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
asn_dec_rval_t rval;
void *newst = 0;
if(!st || !td || !struct_ptr) {
errno = EINVAL;
return -1;
}
if(st->buf == 0) {
/* Nothing to convert, make it empty. */
*struct_ptr = (void *)0;
return 0;
}
rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
if(rval.code == RC_OK) {
*struct_ptr = newst;
return 0;
} else {
/* Remove possibly partially decoded data. */
ASN_STRUCT_FREE(*td, newst);
return -1;
}
}
int
ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
asn_dec_rval_t rval;
void *newst = 0;
if(!st || !td || !struct_ptr) {
errno = EINVAL;
return -1;
}
if(st->buf == 0) {
/* Nothing to convert, make it empty. */
*struct_ptr = (void *)0;
return 0;
}
rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
if(rval.code == RC_OK) {
*struct_ptr = newst;
return 0;
} else {
/* Remove possibly partially decoded data. */
ASN_STRUCT_FREE(*td, newst);
return -1;
}
}
static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
struct _callback_arg *arg = (struct _callback_arg *)key;
if((arg->offset + size) >= arg->size) {
size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
void *p = REALLOC(arg->buffer, nsize);
if(!p) return -1;
arg->buffer = (uint8_t *)p;
arg->size = nsize;
}
memcpy(arg->buffer + arg->offset, buffer, size);
arg->offset += size;
assert(arg->offset < arg->size);
return 0;
}
#ifndef ASN_DISABLE_PER_SUPPORT
asn_dec_rval_t
ANY_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_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_ANY_specs;
size_t consumed_myself = 0;
int repeat;
ANY_t *st = (ANY_t *)*sptr;
(void)opt_codec_ctx;
(void)constraints;
/*
* Allocate the structure.
*/
if(!st) {
st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
ASN_DEBUG("UPER Decoding ANY type");
st->size = 0;
do {
ssize_t raw_len;
ssize_t len_bytes;
ssize_t len_bits;
void *p;
int ret;
/* Get the PER length */
raw_len = uper_get_length(pd, -1, 0, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
if(raw_len == 0 && st->buf) break;
ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
repeat ? "repeat" : "once", td->name);
len_bytes = raw_len;
len_bits = len_bytes * 8;
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += len_bits;
st->size += len_bytes;
} while(repeat);
st->buf[st->size] = 0; /* nul-terminate */
RETURN(RC_OK);
}
asn_enc_rval_t
ANY_encode_uper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_per_outp_t *po) {
const ANY_t *st = (const ANY_t *)sptr;
asn_enc_rval_t er = {0, 0, 0};
const uint8_t *buf;
size_t size;
int ret;
(void)constraints;
if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
buf = st->buf;
size = st->size;
do {
int need_eom = 0;
ssize_t may_save = uper_put_length(po, size, &need_eom);
if(may_save < 0) ASN__ENCODE_FAILED;
ret = per_put_many_bits(po, buf, may_save * 8);
if(ret) ASN__ENCODE_FAILED;
buf += may_save;
size -= may_save;
assert(!(may_save & 0x07) || !size);
if(need_eom && uper_put_length(po, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
} while(size);
ASN__ENCODED_OK(er);
}
asn_dec_rval_t
ANY_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_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_ANY_specs;
size_t consumed_myself = 0;
int repeat;
ANY_t *st = (ANY_t *)*sptr;
(void)opt_codec_ctx;
(void)constraints;
/*
* Allocate the structure.
*/
if(!st) {
st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
ASN_DEBUG("APER Decoding ANY type");
st->size = 0;
do {
ssize_t raw_len;
ssize_t len_bytes;
ssize_t len_bits;
void *p;
int ret;
/* Get the PER length */
raw_len = aper_get_length(pd, -1, 0, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
if(raw_len == 0 && st->buf) break;
ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
repeat ? "repeat" : "once", td->name);
len_bytes = raw_len;
len_bits = len_bytes * 8;
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += len_bits;
st->size += len_bytes;
} while(repeat);
st->buf[st->size] = 0; /* nul-terminate */
RETURN(RC_OK);
}
asn_enc_rval_t
ANY_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_per_outp_t *po) {
const ANY_t *st = (const ANY_t *)sptr;
asn_enc_rval_t er = {0, 0, 0};
const uint8_t *buf;
size_t size;
int ret;
(void)constraints;
if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
buf = st->buf;
size = st->size;
do {
int need_eom = 0;
ssize_t may_save = uper_put_length(po, size, &need_eom);
if(may_save < 0) ASN__ENCODE_FAILED;
ret = per_put_many_bits(po, buf, may_save * 8);
if(ret) ASN__ENCODE_FAILED;
buf += may_save;
size -= may_save;
assert(!(may_save & 0x07) || !size);
if(need_eom && uper_put_length(po, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
} while(size);
ASN__ENCODED_OK(er);
}
#endif /* ASN_DISABLE_PER_SUPPORT */

View File

@ -22,23 +22,34 @@ extern asn_TYPE_descriptor_t asn_DEF_ANY;
extern asn_TYPE_operation_t asn_OP_ANY;
extern asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs;
asn_struct_free_f ANY_free;
asn_struct_print_f ANY_print;
ber_type_decoder_f ANY_decode_ber;
der_type_encoder_f ANY_encode_der;
#define ANY_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define ANY_print OCTET_STRING_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define ANY_compare OCTET_STRING_compare
#define ANY_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define ANY_decode_ber OCTET_STRING_decode_ber
#define ANY_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define ANY_decode_xer OCTET_STRING_decode_xer_hex
xer_type_encoder_f ANY_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f ANY_decode_uper;
per_type_encoder_f ANY_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f ANY_decode_aper;
per_type_encoder_f ANY_encode_aper;
#define ANY_free OCTET_STRING_free
#define ANY_print OCTET_STRING_print
#define ANY_compare OCTET_STRING_compare
#define ANY_constraint asn_generic_no_constraint
#define ANY_decode_ber OCTET_STRING_decode_ber
#define ANY_encode_der OCTET_STRING_encode_der
#define ANY_decode_xer OCTET_STRING_decode_xer_hex
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
/******************************
* Handy conversion routines. *
@ -46,13 +57,17 @@ per_type_encoder_f ANY_encode_aper;
/* Convert another ASN.1 type into the ANY. This implies DER encoding. */
int ANY_fromType(ANY_t *, asn_TYPE_descriptor_t *td, void *struct_ptr);
int ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr);
ANY_t *ANY_new_fromType(asn_TYPE_descriptor_t *td, void *struct_ptr);
#if !defined(ASN_DISABLE_APER_SUPPORT)
int ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr);
ANY_t *ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr);
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
/* Convert the contents of the ANY type into the specified type. */
int ANY_to_type(ANY_t *, asn_TYPE_descriptor_t *td, void **struct_ptr);
#if !defined(ASN_DISABLE_APER_SUPPORT)
int ANY_to_type_aper(ANY_t *, asn_TYPE_descriptor_t *td, void **struct_ptr);
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#define ANY_fromBuf(s, buf, size) OCTET_STRING_fromBuf((s), (buf), (size))
#define ANY_new_fromBuf(buf, size) OCTET_STRING_new_fromBuf( \

190
lib/asn1c/common/ANY_aper.c Normal file
View File

@ -0,0 +1,190 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <ANY.h>
#include <errno.h>
#undef RETURN
#define RETURN(_code) \
do { \
asn_dec_rval_t tmprval; \
tmprval.code = _code; \
tmprval.consumed = consumed_myself; \
return tmprval; \
} while(0)
int
ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
uint8_t *buffer = NULL;
ssize_t erval;
if(!st || !td) {
errno = EINVAL;
return -1;
}
if(!sptr) {
if(st->buf) FREEMEM(st->buf);
st->size = 0;
return 0;
}
erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
if(erval == -1) {
if(buffer) FREEMEM(buffer);
return -1;
}
assert((size_t)erval > 0);
if(st->buf) FREEMEM(st->buf);
st->buf = buffer;
st->size = erval;
return 0;
}
ANY_t *
ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) {
ANY_t tmp;
ANY_t *st;
if(!td || !sptr) {
errno = EINVAL;
return 0;
}
memset(&tmp, 0, sizeof(tmp));
if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
if(st) {
*st = tmp;
return st;
} else {
FREEMEM(tmp.buf);
return 0;
}
}
int
ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
asn_dec_rval_t rval;
void *newst = 0;
if(!st || !td || !struct_ptr) {
errno = EINVAL;
return -1;
}
if(st->buf == 0) {
/* Nothing to convert, make it empty. */
*struct_ptr = (void *)0;
return 0;
}
rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
if(rval.code == RC_OK) {
*struct_ptr = newst;
return 0;
} else {
/* Remove possibly partially decoded data. */
ASN_STRUCT_FREE(*td, newst);
return -1;
}
}
asn_dec_rval_t
ANY_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_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_ANY_specs;
size_t consumed_myself = 0;
int repeat;
ANY_t *st = (ANY_t *)*sptr;
(void)opt_codec_ctx;
(void)constraints;
/*
* Allocate the structure.
*/
if(!st) {
st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
ASN_DEBUG("APER Decoding ANY type");
st->size = 0;
do {
ssize_t raw_len;
ssize_t len_bytes;
ssize_t len_bits;
void *p;
int ret;
/* Get the PER length */
raw_len = aper_get_length(pd, -1, 0, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
if(raw_len == 0 && st->buf) break;
ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
repeat ? "repeat" : "once", td->name);
len_bytes = raw_len;
len_bits = len_bytes * 8;
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += len_bits;
st->size += len_bytes;
} while(repeat);
st->buf[st->size] = 0; /* nul-terminate */
RETURN(RC_OK);
}
asn_enc_rval_t
ANY_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_per_outp_t *po) {
const ANY_t *st = (const ANY_t *)sptr;
asn_enc_rval_t er = {0, 0, 0};
const uint8_t *buf;
size_t size;
int ret;
(void)constraints;
if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
buf = st->buf;
size = st->size;
do {
int need_eom = 0;
ssize_t may_save = aper_put_length(po, -1, size, &need_eom);
if(may_save < 0) ASN__ENCODE_FAILED;
ret = per_put_many_bits(po, buf, may_save * 8);
if(ret) ASN__ENCODE_FAILED;
buf += may_save;
size -= may_save;
assert(!(may_save & 0x07) || !size);
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
} while(size);
ASN__ENCODED_OK(er);
}

View File

@ -4,61 +4,89 @@
*/
#include <asn_internal.h>
#include <BIT_STRING.h>
#include <asn_internal.h>
/*
* BIT STRING basic type description.
*/
static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
(ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
};
asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
sizeof(BIT_STRING_t),
offsetof(BIT_STRING_t, _asn_ctx),
ASN_OSUBV_BIT
sizeof(BIT_STRING_t),
offsetof(BIT_STRING_t, _asn_ctx),
ASN_OSUBV_BIT
};
asn_TYPE_operation_t asn_OP_BIT_STRING = {
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
BIT_STRING_print,
BIT_STRING_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
OCTET_STRING_decode_xer_binary,
BIT_STRING_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
BIT_STRING_print,
#else
BIT_STRING_decode_oer,
BIT_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
BIT_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
#else
BIT_STRING_decode_uper, /* Unaligned PER decoder */
BIT_STRING_encode_uper, /* Unaligned PER encoder */
OCTET_STRING_decode_aper, /* Aligned PER decoder */
OCTET_STRING_encode_aper, /* Aligned PER encoder */
#endif /* ASN_DISABLE_PER_SUPPORT */
BIT_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_binary,
BIT_STRING_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
BIT_STRING_decode_oer,
BIT_STRING_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
BIT_STRING_decode_uper, /* Unaligned PER decoder */
BIT_STRING_encode_uper, /* Unaligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper, /* Aligned PER decoder */
OCTET_STRING_encode_aper, /* Aligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
BIT_STRING_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
"BIT STRING",
"BIT_STRING",
&asn_OP_BIT_STRING,
asn_DEF_BIT_STRING_tags,
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
asn_DEF_BIT_STRING_tags, /* Same as above */
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
{ 0, 0, BIT_STRING_constraint },
0, 0, /* No members */
&asn_SPC_BIT_STRING_specs
"BIT STRING",
"BIT_STRING",
&asn_OP_BIT_STRING,
asn_DEF_BIT_STRING_tags,
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
asn_DEF_BIT_STRING_tags, /* Same as above */
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
BIT_STRING_constraint
},
0, 0, /* No members */
&asn_SPC_BIT_STRING_specs
};
/*
@ -87,136 +115,10 @@ BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
return 0;
}
static const char *_bit_pattern[16] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
asn_enc_rval_t
BIT_STRING_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) {
asn_enc_rval_t er = {0, 0, 0};
char scratch[128];
char *p = scratch;
char *scend = scratch + (sizeof(scratch) - 10);
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
int xcan = (flags & XER_F_CANONICAL);
uint8_t *buf;
uint8_t *end;
if(!st || !st->buf)
ASN__ENCODE_FAILED;
er.encoded = 0;
buf = st->buf;
end = buf + st->size - 1; /* Last byte is special */
/*
* Binary dump
*/
for(; buf < end; buf++) {
int v = *buf;
int nline = xcan?0:(((buf - st->buf) % 8) == 0);
if(p >= scend || nline) {
ASN__CALLBACK(scratch, p - scratch);
p = scratch;
if(nline) ASN__TEXT_INDENT(1, ilevel);
}
memcpy(p + 0, _bit_pattern[v >> 4], 4);
memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
p += 8;
}
if(!xcan && ((buf - st->buf) % 8) == 0)
ASN__TEXT_INDENT(1, ilevel);
ASN__CALLBACK(scratch, p - scratch);
p = scratch;
if(buf == end) {
int v = *buf;
int ubits = st->bits_unused;
int i;
for(i = 7; i >= ubits; i--)
*p++ = (v & (1 << i)) ? 0x31 : 0x30;
ASN__CALLBACK(scratch, p - scratch);
}
if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
ASN__ENCODED_OK(er);
cb_failed:
ASN__ENCODE_FAILED;
}
/*
* BIT STRING specific contents printer.
*/
int
BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
const char * const h2c = "0123456789ABCDEF";
char scratch[64];
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
uint8_t *buf;
uint8_t *end;
char *p = scratch;
(void)td; /* Unused argument */
if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
ilevel++;
buf = st->buf;
end = buf + st->size;
/*
* Hexadecimal dump.
*/
for(; buf < end; buf++) {
if((buf - st->buf) % 16 == 0 && (st->size > 16)
&& buf != st->buf) {
_i_INDENT(1);
/* Dump the string */
if(cb(scratch, p - scratch, app_key) < 0) return -1;
p = scratch;
}
*p++ = h2c[*buf >> 4];
*p++ = h2c[*buf & 0x0F];
*p++ = 0x20;
}
if(p > scratch) {
p--; /* Eat the tailing space */
if((st->size > 16)) {
_i_INDENT(1);
}
/* Dump the incomplete 16-bytes row */
if(cb(scratch, p - scratch, app_key) < 0)
return -1;
}
if(st->bits_unused) {
int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
st->bits_unused, st->bits_unused == 1 ? "" : "s");
assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
if(ret > 0 && ret < (ssize_t)sizeof(scratch)
&& cb(scratch, ret, app_key) < 0)
return -1;
}
return 0;
}
/*
* Non-destructively remove the trailing 0-bits from the given bit string.
*/
static const BIT_STRING_t *
const BIT_STRING_t *
BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
const uint8_t *b;
union {
@ -271,6 +173,7 @@ BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
const asn_OCTET_STRING_specifics_t *specs = td->specifics;
(void)specs;
assert(specs && specs->subvariant == ASN_OSUBV_BIT);
if(a && b) {
@ -303,354 +206,3 @@ BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
return 1;
}
}
#ifndef ASN_DISABLE_PER_SUPPORT
#undef RETURN
#define RETURN(_code) \
do { \
asn_dec_rval_t tmprval; \
tmprval.code = _code; \
tmprval.consumed = consumed_myself; \
return tmprval; \
} while(0)
static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
asn_dec_rval_t
BIT_STRING_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_OCTET_STRING_specifics_t *specs = td->specifics
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_BIT_STRING_specs;
const asn_per_constraints_t *pc =
constraints ? constraints : td->encoding_constraints.per_constraints;
const asn_per_constraint_t *csiz;
asn_dec_rval_t rval = { RC_OK, 0 };
BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
ssize_t consumed_myself = 0;
int repeat;
(void)opt_codec_ctx;
if(pc) {
csiz = &pc->size;
} else {
csiz = &asn_DEF_BIT_STRING_constraint_size;
}
if(specs->subvariant != ASN_OSUBV_BIT) {
ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
RETURN(RC_FAIL);
}
/*
* Allocate the string.
*/
if(!st) {
st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
if(csiz->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
if(inext < 0) RETURN(RC_WMORE);
if(inext) {
csiz = &asn_DEF_BIT_STRING_constraint_size;
}
}
if(csiz->effective_bits >= 0) {
FREEMEM(st->buf);
st->size = (csiz->upper_bound + 7) >> 3;
st->buf = (uint8_t *)MALLOC(st->size + 1);
if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
}
/* X.691, #16.5: zero-length encoding */
/* X.691, #16.6: short fixed length encoding (up to 2 octets) */
/* X.691, #16.7: long fixed length encoding (up to 64K octets) */
if(csiz->effective_bits == 0) {
int ret;
ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += csiz->upper_bound;
st->buf[st->size] = 0;
st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
RETURN(RC_OK);
}
st->size = 0;
do {
ssize_t raw_len;
ssize_t len_bytes;
ssize_t len_bits;
void *p;
int ret;
/* Get the PER length */
raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
&repeat);
if(raw_len < 0) RETURN(RC_WMORE);
if(raw_len == 0 && st->buf) break;
ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
(long)csiz->effective_bits, (long)raw_len,
repeat ? "repeat" : "once", td->name);
len_bits = raw_len;
len_bytes = (len_bits + 7) >> 3;
if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
/* len_bits be multiple of 16K if repeat is set */
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
if(ret < 0) RETURN(RC_WMORE);
st->size += len_bytes;
} while(repeat);
st->buf[st->size] = 0; /* nul-terminate */
return rval;
}
asn_enc_rval_t
BIT_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_BIT_STRING_specs;
const asn_per_constraints_t *pc =
constraints ? constraints : td->encoding_constraints.per_constraints;
const asn_per_constraint_t *csiz;
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
BIT_STRING_t compact_bstr; /* Do not modify this directly! */
asn_enc_rval_t er = { 0, 0, 0 };
int inext = 0; /* Lies not within extension root */
size_t size_in_bits;
const uint8_t *buf;
int ret;
int ct_extensible;
if(!st || (!st->buf && st->size))
ASN__ENCODE_FAILED;
if(specs->subvariant == ASN_OSUBV_BIT) {
if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
ASN__ENCODE_FAILED;
} else {
ASN__ENCODE_FAILED;
}
if(pc) {
csiz = &pc->size;
} else {
csiz = &asn_DEF_BIT_STRING_constraint_size;
}
ct_extensible = csiz->flags & APC_EXTENSIBLE;
/* Figure out the size without the trailing bits */
st = BIT_STRING__compactify(st, &compact_bstr);
size_in_bits = 8 * st->size - st->bits_unused;
ASN_DEBUG(
"Encoding %s into %" ASN_PRI_SIZE " bits"
" (%ld..%ld, effective %d)%s",
td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
csiz->effective_bits, ct_extensible ? " EXT" : "");
/* Figure out whether size lies within PER visible constraint */
if(csiz->effective_bits >= 0) {
if((ssize_t)size_in_bits > csiz->upper_bound) {
if(ct_extensible) {
csiz = &asn_DEF_BIT_STRING_constraint_size;
inext = 1;
} else {
ASN__ENCODE_FAILED;
}
}
} else {
inext = 0;
}
if(ct_extensible) {
/* Declare whether length is [not] within extension root */
if(per_put_few_bits(po, inext, 1))
ASN__ENCODE_FAILED;
}
if(csiz->effective_bits >= 0 && !inext) {
int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
ASN_DEBUG(
"Encoding %" ASN_PRI_SIZE " bytes (%ld), length (in %d bits) trailer %d; actual "
"value %" ASN_PRI_SSIZE "",
st->size, size_in_bits - csiz->lower_bound, csiz->effective_bits,
add_trailer,
add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound);
ret = per_put_few_bits(
po, add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound,
csiz->effective_bits);
if(ret) ASN__ENCODE_FAILED;
ret = per_put_many_bits(po, st->buf, size_in_bits);
if(ret) ASN__ENCODE_FAILED;
if(add_trailer) {
static const uint8_t zeros[16];
size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
while(trailing_zero_bits > 0) {
if(trailing_zero_bits > 8 * sizeof(zeros)) {
ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
trailing_zero_bits -= 8 * sizeof(zeros);
} else {
ret = per_put_many_bits(po, zeros, trailing_zero_bits);
trailing_zero_bits = 0;
}
if(ret) ASN__ENCODE_FAILED;
}
}
ASN__ENCODED_OK(er);
}
ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
buf = st->buf;
do {
int need_eom = 0;
ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
if(maySave < 0) ASN__ENCODE_FAILED;
ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "", maySave, size_in_bits);
ret = per_put_many_bits(po, buf, maySave);
if(ret) ASN__ENCODE_FAILED;
buf += maySave >> 3;
size_in_bits -= maySave;
assert(!(maySave & 0x07) || !size_in_bits);
if(need_eom && uper_put_length(po, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
} while(size_in_bits);
ASN__ENCODED_OK(er);
}
#endif /* ASN_DISABLE_PER_SUPPORT */
asn_random_fill_result_t
BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
const asn_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_BIT_STRING_specs;
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};
static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
126, 127, 128, 16383, 16384, 16385,
65534, 65535, 65536, 65537};
uint8_t *buf;
uint8_t *bend;
uint8_t *b;
size_t rnd_bits, rnd_len;
BIT_STRING_t *st;
if(max_length == 0) return result_skipped;
switch(specs->subvariant) {
case ASN_OSUBV_ANY:
return result_failed;
case ASN_OSUBV_BIT:
break;
default:
break;
}
/* Figure out how far we should go */
rnd_bits = lengths[asn_random_between(
0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
if(!constraints || !constraints->per_constraints)
constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc = &constraints->per_constraints->size;
if(pc->flags & APC_CONSTRAINED) {
long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
? pc->upper_bound
: (ssize_t)max_length;
if(max_length < (size_t)pc->lower_bound) {
return result_skipped;
}
if(pc->flags & APC_EXTENSIBLE) {
switch(asn_random_between(0, 5)) {
case 0:
if(pc->lower_bound > 0) {
rnd_bits = pc->lower_bound - 1;
break;
}
/* Fall through */
case 1:
rnd_bits = pc->upper_bound + 1;
break;
case 2:
/* Keep rnd_bits from the table */
if(rnd_bits < max_length) {
break;
}
/* Fall through */
default:
rnd_bits = asn_random_between(pc->lower_bound,
suggested_upper_bound);
}
} else {
rnd_bits =
asn_random_between(pc->lower_bound, suggested_upper_bound);
}
} else {
rnd_bits = asn_random_between(0, max_length - 1);
}
} else if(rnd_bits >= max_length) {
rnd_bits = asn_random_between(0, max_length - 1);
}
rnd_len = (rnd_bits + 7) / 8;
buf = CALLOC(1, rnd_len + 1);
if(!buf) return result_failed;
bend = &buf[rnd_len];
for(b = buf; b < bend; b++) {
*(uint8_t *)b = asn_random_between(0, 255);
}
*b = 0; /* Zero-terminate just in case. */
if(*sptr) {
st = *sptr;
FREEMEM(st->buf);
} else {
st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) {
FREEMEM(buf);
return result_failed;
}
}
st->buf = buf;
st->size = rnd_len;
st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
if(st->bits_unused) {
assert(st->size > 0);
st->buf[st->size-1] &= 0xff << st->bits_unused;
}
result_ok.length = st->size;
return result_ok;
}

View File

@ -24,22 +24,45 @@ extern asn_TYPE_descriptor_t asn_DEF_BIT_STRING;
extern asn_TYPE_operation_t asn_OP_BIT_STRING;
extern asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs;
asn_struct_print_f BIT_STRING_print; /* Human-readable output */
#define BIT_STRING_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f BIT_STRING_print; /* Human-readable output */
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f BIT_STRING_compare;
asn_constr_check_f BIT_STRING_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define BIT_STRING_decode_ber OCTET_STRING_decode_ber
#define BIT_STRING_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define BIT_STRING_decode_xer OCTET_STRING_decode_xer_binary
xer_type_encoder_f BIT_STRING_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f BIT_STRING_decode_oer;
oer_type_encoder_f BIT_STRING_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f BIT_STRING_decode_uper;
per_type_encoder_f BIT_STRING_encode_uper;
asn_random_fill_f BIT_STRING_random_fill;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define BIT_STRING_decode_aper OCTET_STRING_decode_aper
#define BIT_STRING_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#define BIT_STRING_free OCTET_STRING_free
#define BIT_STRING_decode_ber OCTET_STRING_decode_ber
#define BIT_STRING_encode_der OCTET_STRING_encode_der
#define BIT_STRING_decode_xer OCTET_STRING_decode_xer_binary
#define BIT_STRING_decode_aper OCTET_STRING_decode_aper
#define BIT_STRING_encode_aper OCTET_STRING_encode_aper
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f BIT_STRING_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
const BIT_STRING_t *BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp);
#ifdef __cplusplus
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <BIT_STRING.h>
/*
* BIT STRING specific contents printer.
*/
int
BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
const char * const h2c = "0123456789ABCDEF";
char scratch[64];
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
uint8_t *buf;
uint8_t *end;
char *p = scratch;
(void)td; /* Unused argument */
if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
ilevel++;
buf = st->buf;
end = buf + st->size;
/*
* Hexadecimal dump.
*/
for(; buf < end; buf++) {
if((buf - st->buf) % 16 == 0 && (st->size > 16)
&& buf != st->buf) {
_i_INDENT(1);
/* Dump the string */
if(cb(scratch, p - scratch, app_key) < 0) return -1;
p = scratch;
}
*p++ = h2c[*buf >> 4];
*p++ = h2c[*buf & 0x0F];
*p++ = 0x20;
}
if(p > scratch) {
p--; /* Eat the tailing space */
if((st->size > 16)) {
_i_INDENT(1);
}
/* Dump the incomplete 16-bytes row */
if(cb(scratch, p - scratch, app_key) < 0)
return -1;
}
if(st->bits_unused) {
int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
st->bits_unused, st->bits_unused == 1 ? "" : "s");
assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
if(ret > 0 && ret < (ssize_t)sizeof(scratch)
&& cb(scratch, ret, app_key) < 0)
return -1;
}
return 0;
}

View File

@ -0,0 +1,124 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <BIT_STRING.h>
asn_random_fill_result_t
BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
const asn_OCTET_STRING_specifics_t *specs =
td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_BIT_STRING_specs;
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};
static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
126, 127, 128, 16383, 16384, 16385,
65534, 65535, 65536, 65537};
uint8_t *buf;
uint8_t *bend;
uint8_t *b;
size_t rnd_bits, rnd_len;
BIT_STRING_t *st;
if(max_length == 0) return result_skipped;
switch(specs->subvariant) {
case ASN_OSUBV_ANY:
return result_failed;
case ASN_OSUBV_BIT:
break;
default:
break;
}
/* Figure out how far we should go */
rnd_bits = lengths[asn_random_between(
0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
if(!constraints || !constraints->per_constraints)
constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc = &constraints->per_constraints->size;
if(pc->flags & APC_CONSTRAINED) {
long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
? pc->upper_bound
: (ssize_t)max_length;
if(max_length < (size_t)pc->lower_bound) {
return result_skipped;
}
if(pc->flags & APC_EXTENSIBLE) {
switch(asn_random_between(0, 5)) {
case 0:
if(pc->lower_bound > 0) {
rnd_bits = pc->lower_bound - 1;
break;
}
/* Fall through */
case 1:
rnd_bits = pc->upper_bound + 1;
break;
case 2:
/* Keep rnd_bits from the table */
if(rnd_bits < max_length) {
break;
}
/* Fall through */
default:
rnd_bits = asn_random_between(pc->lower_bound,
suggested_upper_bound);
}
} else {
rnd_bits =
asn_random_between(pc->lower_bound, suggested_upper_bound);
}
} else {
rnd_bits = asn_random_between(0, max_length - 1);
}
} else {
#else
if(!constraints) constraints = &td->encoding_constraints;
{
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
if(rnd_bits >= max_length) {
rnd_bits = asn_random_between(0, max_length - 1);
}
}
rnd_len = (rnd_bits + 7) / 8;
buf = CALLOC(1, rnd_len + 1);
if(!buf) return result_failed;
bend = &buf[rnd_len];
for(b = buf; b < bend; b++) {
*(uint8_t *)b = asn_random_between(0, 255);
}
*b = 0; /* Zero-terminate just in case. */
if(*sptr) {
st = *sptr;
FREEMEM(st->buf);
} else {
st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) {
FREEMEM(buf);
return result_failed;
}
}
st->buf = buf;
st->size = rnd_len;
st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
if(st->bits_unused) {
assert(st->size > 0);
st->buf[st->size-1] &= 0xff << st->bits_unused;
}
result_ok.length = st->size;
return result_ok;
}

View File

@ -9,50 +9,78 @@
* GraphicString basic type description.
*/
static const ber_tlv_tag_t asn_DEF_GraphicString_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (25 << 2)), /* [UNIVERSAL 25] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
(ASN_TAG_CLASS_UNIVERSAL | (25 << 2)), /* [UNIVERSAL 25] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
};
asn_TYPE_operation_t asn_OP_GraphicString = {
OCTET_STRING_free,
OCTET_STRING_print, /* non-ascii string */
OCTET_STRING_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_hex,
OCTET_STRING_encode_xer, /* Can't expect it to be ASCII/UTF8 */
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OCTET_STRING_print, /* non-ascii string */
#else
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
#else
OCTET_STRING_decode_uper, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_uper,
OCTET_STRING_decode_aper, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
OCTET_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_hex,
OCTET_STRING_encode_xer, /* Can't expect it to be ASCII/UTF8 */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
OCTET_STRING_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_GraphicString = {
"GraphicString",
"GraphicString",
&asn_OP_GraphicString,
asn_DEF_GraphicString_tags,
sizeof(asn_DEF_GraphicString_tags)
/ sizeof(asn_DEF_GraphicString_tags[0]) - 1,
asn_DEF_GraphicString_tags,
sizeof(asn_DEF_GraphicString_tags)
/ sizeof(asn_DEF_GraphicString_tags[0]),
{ 0, 0, asn_generic_unknown_constraint },
0, 0, /* No members */
0 /* No specifics */
"GraphicString",
"GraphicString",
&asn_OP_GraphicString,
asn_DEF_GraphicString_tags,
sizeof(asn_DEF_GraphicString_tags)
/ sizeof(asn_DEF_GraphicString_tags[0]) - 1,
asn_DEF_GraphicString_tags,
sizeof(asn_DEF_GraphicString_tags)
/ sizeof(asn_DEF_GraphicString_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_unknown_constraint
},
0, 0, /* No members */
0 /* No specifics */
};

View File

@ -16,18 +16,34 @@ typedef OCTET_STRING_t GraphicString_t; /* Implemented via OCTET STRING */
extern asn_TYPE_descriptor_t asn_DEF_GraphicString;
extern asn_TYPE_operation_t asn_OP_GraphicString;
#define GraphicString_free OCTET_STRING_free
#define GraphicString_print OCTET_STRING_print
#define GraphicString_compare OCTET_STRING_compare
#define GraphicString_constraint asn_generic_unknown_constraint
#define GraphicString_decode_ber OCTET_STRING_decode_ber
#define GraphicString_encode_der OCTET_STRING_encode_der
#define GraphicString_decode_xer OCTET_STRING_decode_xer_hex
#define GraphicString_encode_xer OCTET_STRING_encode_xer
#define GraphicString_decode_uper OCTET_STRING_decode_uper
#define GraphicString_encode_uper OCTET_STRING_encode_uper
#define GraphicString_decode_aper OCTET_STRING_decode_aper
#define GraphicString_encode_aper OCTET_STRING_encode_aper
#define GraphicString_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define GraphicString_print OCTET_STRING_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define GraphicString_compare OCTET_STRING_compare
#define GraphicString_constraint asn_generic_unknown_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define GraphicString_decode_ber OCTET_STRING_decode_ber
#define GraphicString_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define GraphicString_decode_xer OCTET_STRING_decode_xer_hex
#define GraphicString_encode_xer OCTET_STRING_encode_xer
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define GraphicString_decode_uper OCTET_STRING_decode_uper
#define GraphicString_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define GraphicString_decode_aper OCTET_STRING_decode_aper
#define GraphicString_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#ifdef __cplusplus
}

File diff suppressed because it is too large Load Diff

View File

@ -35,21 +35,48 @@ typedef struct asn_INTEGER_specifics_s {
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
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);
#define INTEGER_free ASN__PRIMITIVE_TYPE_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f INTEGER_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f INTEGER_compare;
#define INTEGER_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define INTEGER_decode_ber ber_decode_primitive
der_type_encoder_f INTEGER_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f INTEGER_decode_xer;
xer_type_encoder_f INTEGER_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f INTEGER_decode_oer;
oer_type_encoder_f INTEGER_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f INTEGER_decode_uper;
per_type_encoder_f INTEGER_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f INTEGER_decode_aper;
per_type_encoder_f INTEGER_encode_aper;
asn_random_fill_f INTEGER_random_fill;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f INTEGER_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
/***********************************
* Some handy conversion routines. *

View File

@ -0,0 +1,304 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <INTEGER.h>
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 ? */
int range = ct->upper_bound - ct->lower_bound + 1;
if (ct->range_bits < 8 || (ct->range_bits == 8 && range < 256)) {
if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
ASN__ENCODE_FAILED;
} else if (ct->range_bits == 8) {
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;) {
int need_eom = 0;
ssize_t mayEncode = aper_put_length(po, -1, end - buf, &need_eom);
if(mayEncode < 0)
ASN__ENCODE_FAILED;
if(per_put_many_bits(po, buf, 8 * mayEncode))
ASN__ENCODE_FAILED;
buf += mayEncode;
if(need_eom && aper_put_length(po, -1, 0, 0)) ASN__ENCODE_FAILED;
}
ASN__ENCODED_OK(er);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <INTEGER.h>
/*
* 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("<absent>", 8, app_key);
else
ret = INTEGER__dump(td, st, cb, app_key, 0);
return (ret < 0) ? -1 : 0;
}

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <INTEGER.h>
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 {
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;
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
const asn_per_constraints_t *ct;
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);
}
}
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
}
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;
}
}

View File

@ -3,55 +3,83 @@
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <asn_codecs_prim.h>
#include <NULL.h>
/*
* NULL basic type description.
*/
static const ber_tlv_tag_t asn_DEF_NULL_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (5 << 2))
(ASN_TAG_CLASS_UNIVERSAL | (5 << 2))
};
asn_TYPE_operation_t asn_OP_NULL = {
NULL_free,
NULL_print,
NULL_compare,
NULL_decode_ber,
NULL_encode_der, /* Special handling of DER encoding */
NULL_decode_xer,
NULL_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
NULL_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
NULL_print,
#else
NULL_decode_oer,
NULL_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
NULL_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
NULL_decode_ber,
NULL_encode_der, /* Special handling of DER encoding */
#else
NULL_decode_uper, /* Unaligned PER decoder */
NULL_encode_uper, /* Unaligned PER encoder */
NULL_decode_aper, /* Aligned PER decoder */
NULL_encode_aper, /* Aligned PER encoder */
#endif /* ASN_DISABLE_PER_SUPPORT */
NULL_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
NULL_decode_xer,
NULL_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
NULL_decode_oer,
NULL_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
NULL_decode_uper, /* Unaligned PER decoder */
NULL_encode_uper, /* Unaligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
NULL_decode_aper, /* Aligned PER decoder */
NULL_encode_aper, /* Aligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
NULL_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_NULL = {
"NULL",
"NULL",
&asn_OP_NULL,
asn_DEF_NULL_tags,
sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
asn_DEF_NULL_tags, /* Same as above */
sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
{ 0, 0, asn_generic_no_constraint },
0, 0, /* No members */
0 /* No specifics */
"NULL",
"NULL",
&asn_OP_NULL,
asn_DEF_NULL_tags,
sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
asn_DEF_NULL_tags, /* Same as above */
sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_no_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
void
@ -71,106 +99,6 @@ NULL_free(const asn_TYPE_descriptor_t *td, void *ptr,
}
}
/*
* Decode NULL type.
*/
asn_dec_rval_t
NULL_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **bool_value,
const void *buf_ptr, size_t size, int tag_mode) {
NULL_t *st = (NULL_t *)*bool_value;
asn_dec_rval_t rval;
ber_tlv_len_t length;
if(st == NULL) {
st = (NULL_t *)(*bool_value = CALLOC(1, sizeof(*st)));
if(st == NULL) {
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
}
ASN_DEBUG("Decoding %s as NULL (tm=%d)", td->name, tag_mode);
/*
* Check tags.
*/
rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, tag_mode, 0,
&length, 0);
if(rval.code != RC_OK) {
return rval;
}
// X.690-201508, #8.8.2, length shall be zero.
if(length != 0) {
ASN_DEBUG("Decoding %s as NULL failed: too much data", td->name);
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
return rval;
}
asn_enc_rval_t
NULL_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr, 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};
erval.encoded = der_write_tags(td, 0, tag_mode, 0, tag, cb, app_key);
if(erval.encoded == -1) {
erval.failed_type = td;
erval.structure_ptr = ptr;
}
ASN__ENCODED_OK(erval);
}
asn_enc_rval_t
NULL_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) {
asn_enc_rval_t er = {0,0,0};
(void)td;
(void)sptr;
(void)ilevel;
(void)flags;
(void)cb;
(void)app_key;
/* XMLNullValue is empty */
er.encoded = 0;
ASN__ENCODED_OK(er);
}
static enum xer_pbd_rval
NULL__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
const void *chunk_buf, size_t chunk_size) {
(void)td;
(void)sptr;
(void)chunk_buf; /* Going to be empty according to the rules below. */
/*
* There must be no content in self-terminating <NULL/> tag.
*/
if(chunk_size)
return XPBD_BROKEN_ENCODING;
else
return XPBD_BODY_CONSUMED;
}
asn_dec_rval_t
NULL_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(NULL_t), opt_mname, buf_ptr, size,
NULL__xer_body_decode);
}
int
NULL_compare(const asn_TYPE_descriptor_t *td, const void *a, const void *b) {
(void)td;
@ -178,180 +106,3 @@ NULL_compare(const asn_TYPE_descriptor_t *td, const void *a, const void *b) {
(void)b;
return 0;
}
int
NULL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(sptr) {
return (cb("<present>", 9, app_key) < 0) ? -1 : 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}
#ifndef ASN_DISABLE_OER_SUPPORT
asn_dec_rval_t
NULL_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) {
asn_dec_rval_t rv = {RC_OK, 0};
(void)opt_codec_ctx;
(void)td;
(void)constraints;
(void)ptr;
(void)size;
if(!*sptr) {
*sptr = MALLOC(sizeof(NULL_t));
if(*sptr) {
*(NULL_t *)*sptr = 0;
} else {
ASN__DECODE_FAILED;
}
}
return rv;
}
asn_enc_rval_t
NULL_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) {
asn_enc_rval_t er = {0,0,0};
(void)td;
(void)sptr;
(void)constraints;
(void)cb;
(void)app_key;
er.encoded = 0; /* Encoding in 0 bytes. */
ASN__ENCODED_OK(er);
}
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifndef ASN_DISABLE_PER_SUPPORT
asn_dec_rval_t
NULL_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) {
asn_dec_rval_t rv;
(void)opt_codec_ctx;
(void)td;
(void)constraints;
(void)pd;
if(!*sptr) {
*sptr = MALLOC(sizeof(NULL_t));
if(*sptr) {
*(NULL_t *)*sptr = 0;
} else {
ASN__DECODE_FAILED;
}
}
/*
* NULL type does not have content octets.
*/
rv.code = RC_OK;
rv.consumed = 0;
return rv;
}
asn_enc_rval_t
NULL_encode_uper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_per_outp_t *po) {
asn_enc_rval_t er = {0,0,0};
(void)td;
(void)constraints;
(void)sptr;
(void)po;
er.encoded = 0;
ASN__ENCODED_OK(er);
}
asn_dec_rval_t
NULL_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) {
asn_dec_rval_t rv = {RC_OK, 0};
(void)opt_codec_ctx;
(void)td;
(void)constraints;
(void)pd;
if(!*sptr) {
*sptr = MALLOC(sizeof(NULL_t));
if(*sptr) {
*(NULL_t *)*sptr = 0;
} else {
ASN__DECODE_FAILED;
}
}
/*
* NULL type does not have content octets.
*/
rv.code = RC_OK;
rv.consumed = 0;
return rv;
}
asn_enc_rval_t
NULL_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
asn_enc_rval_t er = {0,0,0};
(void)td;
(void)constraints;
(void)sptr;
(void)po;
er.encoded = 0;
ASN__ENCODED_OK(er);
}
#endif /* ASN_DISABLE_PER_SUPPORT */
asn_random_fill_result_t
NULL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constr,
size_t max_length) {
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};
NULL_t *st = *sptr;
(void)td;
(void)constr;
if(max_length == 0) return result_skipped;
if(st == NULL) {
st = (NULL_t *)(*sptr = CALLOC(1, sizeof(*st)));
if(st == NULL) {
return result_failed;
}
}
return result_ok;
}

View File

@ -21,21 +21,42 @@ extern asn_TYPE_descriptor_t asn_DEF_NULL;
extern asn_TYPE_operation_t asn_OP_NULL;
asn_struct_free_f NULL_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f NULL_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f NULL_compare;
#define NULL_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f NULL_decode_ber;
der_type_encoder_f NULL_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f NULL_decode_xer;
xer_type_encoder_f NULL_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f NULL_decode_oer;
oer_type_encoder_f NULL_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f NULL_decode_uper;
per_type_encoder_f NULL_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f NULL_decode_aper;
per_type_encoder_f NULL_encode_aper;
asn_random_fill_f NULL_random_fill;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#define NULL_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f NULL_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NULL.h>
asn_dec_rval_t
NULL_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) {
asn_dec_rval_t rv = {RC_OK, 0};
(void)opt_codec_ctx;
(void)td;
(void)constraints;
(void)pd;
if(!*sptr) {
*sptr = MALLOC(sizeof(NULL_t));
if(*sptr) {
*(NULL_t *)*sptr = 0;
} else {
ASN__DECODE_FAILED;
}
}
/*
* NULL type does not have content octets.
*/
rv.code = RC_OK;
rv.consumed = 0;
return rv;
}
asn_enc_rval_t
NULL_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
asn_enc_rval_t er = {0,0,0};
(void)td;
(void)constraints;
(void)sptr;
(void)po;
er.encoded = 0;
ASN__ENCODED_OK(er);
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NULL.h>
int
NULL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(sptr) {
return (cb("<present>", 9, app_key) < 0) ? -1 : 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NULL.h>
asn_random_fill_result_t
NULL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constr,
size_t max_length) {
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};
NULL_t *st = *sptr;
(void)td;
(void)constr;
if(max_length == 0) return result_skipped;
if(st == NULL) {
st = (NULL_t *)(*sptr = CALLOC(1, sizeof(*st)));
if(st == NULL) {
return result_failed;
}
}
return result_ok;
}

View File

@ -16,360 +16,88 @@
* NativeEnumerated basic type description.
*/
static const ber_tlv_tag_t asn_DEF_NativeEnumerated_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (10 << 2))
(ASN_TAG_CLASS_UNIVERSAL | (10 << 2))
};
asn_TYPE_operation_t asn_OP_NativeEnumerated = {
NativeInteger_free,
NativeInteger_print,
NativeInteger_compare,
NativeInteger_decode_ber,
NativeInteger_encode_der,
NativeInteger_decode_xer,
NativeEnumerated_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
NativeInteger_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
NativeInteger_print,
#else
NativeEnumerated_decode_oer,
NativeEnumerated_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
NativeInteger_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
NativeInteger_decode_ber,
NativeInteger_encode_der,
#else
NativeEnumerated_decode_uper,
NativeEnumerated_encode_uper,
NativeEnumerated_decode_aper,
NativeEnumerated_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
NativeEnumerated_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
NativeInteger_decode_xer,
NativeEnumerated_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
NativeEnumerated_decode_oer,
NativeEnumerated_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
NativeEnumerated_decode_uper,
NativeEnumerated_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
NativeEnumerated_decode_aper,
NativeEnumerated_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
NativeEnumerated_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
"ENUMERATED", /* The ASN.1 type is still ENUMERATED */
"ENUMERATED",
&asn_OP_NativeEnumerated,
asn_DEF_NativeEnumerated_tags,
sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
asn_DEF_NativeEnumerated_tags, /* Same as above */
sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
{ 0, 0, asn_generic_no_constraint },
0, 0, /* No members */
0 /* No specifics */
"ENUMERATED", /* The ASN.1 type is still ENUMERATED */
"ENUMERATED",
&asn_OP_NativeEnumerated,
asn_DEF_NativeEnumerated_tags,
sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
asn_DEF_NativeEnumerated_tags, /* Same as above */
sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_no_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
asn_enc_rval_t
NativeEnumerated_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 asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
asn_enc_rval_t er = {0,0,0};
const long *native = (const long *)sptr;
const asn_INTEGER_enum_map_t *el;
(void)ilevel;
(void)flags;
if(!native) ASN__ENCODE_FAILED;
el = INTEGER_map_value2enum(specs, *native);
if(el) {
er.encoded =
asn__format_to_callback(cb, app_key, "<%s/>", el->enum_name);
if(er.encoded < 0) ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
} else {
ASN_DEBUG(
"ASN.1 forbids dealing with "
"unknown value of ENUMERATED type");
ASN__ENCODE_FAILED;
}
}
asn_dec_rval_t
NativeEnumerated_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 = td->specifics;
asn_dec_rval_t rval = { RC_OK, 0 };
long *native = (long *)*sptr;
const asn_per_constraint_t *ct;
long value;
(void)opt_codec_ctx;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__DECODE_FAILED; /* Mandatory! */
if(!specs) ASN__DECODE_FAILED;
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
if(ct->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
if(inext < 0) ASN__DECODE_STARVED;
if(inext) ct = 0;
}
if(ct && ct->range_bits >= 0) {
value = per_get_few_bits(pd, ct->range_bits);
if(value < 0) ASN__DECODE_STARVED;
if(value >= (specs->extension
? specs->extension - 1 : specs->map_count))
ASN__DECODE_FAILED;
} else {
if(!specs->extension)
ASN__DECODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
value = uper_get_nsnnwn(pd);
if(value < 0) ASN__DECODE_STARVED;
value += specs->extension - 1;
if(value >= specs->map_count)
ASN__DECODE_FAILED;
}
*native = specs->value2enum[value].nat_value;
ASN_DEBUG("Decoded %s = %ld", td->name, *native);
return rval;
}
static int
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
int
NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
const asn_INTEGER_enum_map_t *a = ap;
const asn_INTEGER_enum_map_t *b = bp;
if(a->nat_value == b->nat_value)
return 0;
if(a->nat_value < b->nat_value)
return -1;
return 1;
}
asn_enc_rval_t
NativeEnumerated_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};
long native, value;
const asn_per_constraint_t *ct;
int inext = 0;
asn_INTEGER_enum_map_t key;
const asn_INTEGER_enum_map_t *kf;
if(!sptr) ASN__ENCODE_FAILED;
if(!specs) ASN__ENCODE_FAILED;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__ENCODE_FAILED; /* Mandatory! */
ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
er.encoded = 0;
native = *(const long *)sptr;
key.nat_value = native;
kf = bsearch(&key, specs->value2enum, specs->map_count,
sizeof(key), NativeEnumerated__compar_value2enum);
if(!kf) {
ASN_DEBUG("No element corresponds to %ld", native);
ASN__ENCODE_FAILED;
}
value = kf - specs->value2enum;
if(ct->range_bits >= 0) {
int cmpWith = specs->extension
? specs->extension - 1 : specs->map_count;
if(value >= cmpWith)
inext = 1;
}
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;
}
if(ct && ct->range_bits >= 0) {
if(per_put_few_bits(po, value, ct->range_bits))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
if(!specs->extension)
ASN__ENCODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
value, specs->extension, inext,
value - (inext ? (specs->extension - 1) : 0));
if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
asn_dec_rval_t
NativeEnumerated_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 };
long *native = (long *)*sptr;
const asn_per_constraint_t *ct;
long value;
(void)opt_codec_ctx;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__DECODE_FAILED; /* Mandatory! */
if(!specs) ASN__DECODE_FAILED;
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
if(ct->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
if(inext < 0) ASN__DECODE_STARVED;
if(inext) ct = 0;
}
/* Deal with APER padding */
if(ct && ct->upper_bound >= 255) {
int padding = 0;
padding = (8 - (pd->moved % 8)) % 8;
ASN_DEBUG("For NativeEnumerated %s,offset= %lu Padding bits = %d", td->name, pd->moved, padding);
ASN_DEBUG("For NativeEnumerated %s, upper bound = %lu", td->name, ct->upper_bound);
if(padding > 0)
per_get_few_bits(pd, padding);
}
if(ct && ct->range_bits >= 0) {
value = per_get_few_bits(pd, ct->range_bits);
if(value < 0) ASN__DECODE_STARVED;
if(value >= (specs->extension
? specs->extension - 1 : specs->map_count))
ASN__DECODE_FAILED;
} else {
if(!specs->extension)
ASN__DECODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
/* XXX handle indefinite index length > 64k */
value = aper_get_nsnnwn(pd, 65537);
if(value < 0) ASN__DECODE_STARVED;
value += specs->extension - 1;
//if(value >= specs->map_count)
// ASN__DECODE_FAILED;
if(value >= specs->map_count) {
ASN_DEBUG("Decoded unknown index value %s = %ld", td->name, value);
/* unknown index. Workaround => set the first enumeration value */
*native = specs->value2enum[0].nat_value;
return rval;
}
}
*native = specs->value2enum[value].nat_value;
ASN_DEBUG("Decoded %s = %ld", td->name, *native);
return rval;
}
asn_enc_rval_t
NativeEnumerated_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};
long native, value;
const asn_per_constraint_t *ct;
int inext = 0;
asn_INTEGER_enum_map_t key;
asn_INTEGER_enum_map_t *kf;
if(!sptr) ASN__ENCODE_FAILED;
if(!specs) ASN__ENCODE_FAILED;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__ENCODE_FAILED; /* Mandatory! */
ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
er.encoded = 0;
native = *(const long *)sptr;
if(native < 0) ASN__ENCODE_FAILED;
key.nat_value = native;
kf = bsearch(&key, specs->value2enum, specs->map_count,
sizeof(key), NativeEnumerated__compar_value2enum);
if(!kf) {
ASN_DEBUG("No element corresponds to %ld", native);
ASN__ENCODE_FAILED;
}
value = kf - specs->value2enum;
if(ct->range_bits >= 0) {
int cmpWith = specs->extension
? specs->extension - 1 : specs->map_count;
if(value >= cmpWith)
inext = 1;
}
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;
}
if(ct && ct->range_bits >= 0) {
if(per_put_few_bits(po, value, ct->range_bits))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
if(!specs->extension)
ASN__ENCODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
value, specs->extension, inext,
value - (inext ? (specs->extension - 1) : 0));
if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
const asn_INTEGER_enum_map_t *a = ap;
const asn_INTEGER_enum_map_t *b = bp;
if(a->nat_value == b->nat_value)
return 0;
if(a->nat_value < b->nat_value)
return -1;
return 1;
}
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */

View File

@ -21,22 +21,49 @@ extern "C" {
extern asn_TYPE_descriptor_t asn_DEF_NativeEnumerated;
extern asn_TYPE_operation_t asn_OP_NativeEnumerated;
xer_type_encoder_f NativeEnumerated_encode_xer;
oer_type_decoder_f NativeEnumerated_decode_oer;
oer_type_encoder_f NativeEnumerated_encode_oer;
per_type_decoder_f NativeEnumerated_decode_uper;
per_type_encoder_f NativeEnumerated_encode_uper;
per_type_decoder_f NativeEnumerated_decode_aper;
per_type_encoder_f NativeEnumerated_encode_aper;
#define NativeEnumerated_free NativeInteger_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define NativeEnumerated_print NativeInteger_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define NativeEnumerated_compare NativeInteger_compare
#define NativeEnumerated_free NativeInteger_free
#define NativeEnumerated_print NativeInteger_print
#define NativeEnumerated_compare NativeInteger_compare
#define NativeEnumerated_random_fill NativeInteger_random_fill
#define NativeEnumerated_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define NativeEnumerated_decode_ber NativeInteger_decode_ber
#define NativeEnumerated_encode_der NativeInteger_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define NativeEnumerated_decode_xer NativeInteger_decode_xer
xer_type_encoder_f NativeEnumerated_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f NativeEnumerated_decode_oer;
oer_type_encoder_f NativeEnumerated_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f NativeEnumerated_decode_uper;
per_type_encoder_f NativeEnumerated_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f NativeEnumerated_decode_aper;
per_type_encoder_f NativeEnumerated_encode_aper;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
#define NativeEnumerated_random_fill NativeInteger_random_fill
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
int NativeEnumerated__compar_value2enum(
const void *ap,
const void *bp);
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NativeEnumerated.h>
asn_dec_rval_t
NativeEnumerated_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 };
long *native = (long *)*sptr;
const asn_per_constraint_t *ct;
long value;
(void)opt_codec_ctx;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__DECODE_FAILED; /* Mandatory! */
if(!specs) ASN__DECODE_FAILED;
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
if(ct->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
if(inext < 0) ASN__DECODE_STARVED;
if(inext) ct = 0;
}
/* Deal with APER padding */
if(ct && ct->upper_bound >= 255) {
int padding = 0;
padding = (8 - (pd->moved % 8)) % 8;
ASN_DEBUG("For NativeEnumerated %s,offset= %lu Padding bits = %d", td->name, pd->moved, padding);
ASN_DEBUG("For NativeEnumerated %s, upper bound = %lu", td->name, ct->upper_bound);
if(padding > 0)
per_get_few_bits(pd, padding);
}
if(ct && ct->range_bits >= 0) {
value = per_get_few_bits(pd, ct->range_bits);
if(value < 0) ASN__DECODE_STARVED;
if(value >= (specs->extension
? specs->extension - 1 : specs->map_count))
ASN__DECODE_FAILED;
} else {
if(!specs->extension)
ASN__DECODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
/* XXX handle indefinite index length > 64k */
value = aper_get_nsnnwn(pd, 65537);
if(value < 0) ASN__DECODE_STARVED;
value += specs->extension - 1;
//if(value >= specs->map_count)
// ASN__DECODE_FAILED;
if(value >= specs->map_count) {
ASN_DEBUG("Decoded unknown index value %s = %ld", td->name, value);
/* unknown index. Workaround => set the first enumeration value */
*native = specs->value2enum[0].nat_value;
return rval;
}
}
*native = specs->value2enum[value].nat_value;
ASN_DEBUG("Decoded %s = %ld", td->name, *native);
return rval;
}
asn_enc_rval_t
NativeEnumerated_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};
long native, value;
const asn_per_constraint_t *ct;
int inext = 0;
asn_INTEGER_enum_map_t key;
asn_INTEGER_enum_map_t *kf;
if(!sptr) ASN__ENCODE_FAILED;
if(!specs) ASN__ENCODE_FAILED;
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ASN__ENCODE_FAILED; /* Mandatory! */
ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
er.encoded = 0;
native = *(const long *)sptr;
if(native < 0) ASN__ENCODE_FAILED;
key.nat_value = native;
kf = bsearch(&key, specs->value2enum, specs->map_count,
sizeof(key), NativeEnumerated__compar_value2enum);
if(!kf) {
ASN_DEBUG("No element corresponds to %ld", native);
ASN__ENCODE_FAILED;
}
value = kf - specs->value2enum;
if(ct->range_bits >= 0) {
int cmpWith = specs->extension
? specs->extension - 1 : specs->map_count;
if(value >= cmpWith)
inext = 1;
}
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;
}
if(ct && ct->range_bits >= 0) {
if(per_put_few_bits(po, value, ct->range_bits))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
if(!specs->extension)
ASN__ENCODE_FAILED;
/*
* X.691, #10.6: normally small non-negative whole number;
*/
ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
value, specs->extension, inext,
value - (inext ? (specs->extension - 1) : 0));
if(aper_put_nsnnwn(po,
ct->upper_bound - ct->lower_bound + 1,
value - (inext ? (specs->extension - 1) : 0)))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}

View File

@ -17,408 +17,79 @@
* NativeInteger basic type description.
*/
static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
};
asn_TYPE_operation_t asn_OP_NativeInteger = {
NativeInteger_free,
NativeInteger_print,
NativeInteger_compare,
NativeInteger_decode_ber,
NativeInteger_encode_der,
NativeInteger_decode_xer,
NativeInteger_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
NativeInteger_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
NativeInteger_print,
#else
NativeInteger_decode_oer, /* OER decoder */
NativeInteger_encode_oer, /* Canonical OER encoder */
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
NativeInteger_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
NativeInteger_decode_ber,
NativeInteger_encode_der,
#else
NativeInteger_decode_uper, /* Unaligned PER decoder */
NativeInteger_encode_uper, /* Unaligned PER encoder */
NativeInteger_decode_aper, /* Aligned PER decoder */
NativeInteger_encode_aper, /* Aligned PER encoder */
#endif /* ASN_DISABLE_PER_SUPPORT */
NativeInteger_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
NativeInteger_decode_xer,
NativeInteger_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
NativeInteger_decode_oer, /* OER decoder */
NativeInteger_encode_oer, /* Canonical OER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
NativeInteger_decode_uper, /* Unaligned PER decoder */
NativeInteger_encode_uper, /* Unaligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
NativeInteger_decode_aper, /* Aligned PER decoder */
NativeInteger_encode_aper, /* Aligned PER encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
NativeInteger_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
"INTEGER", /* The ASN.1 type is still INTEGER */
"INTEGER",
&asn_OP_NativeInteger,
asn_DEF_NativeInteger_tags,
sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
asn_DEF_NativeInteger_tags, /* Same as above */
sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
{ 0, 0, asn_generic_no_constraint },
0, 0, /* No members */
0 /* No specifics */
"INTEGER", /* The ASN.1 type is still INTEGER */
"INTEGER",
&asn_OP_NativeInteger,
asn_DEF_NativeInteger_tags,
sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
asn_DEF_NativeInteger_tags, /* Same as above */
sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_no_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
/*
* Decode INTEGER type.
*/
asn_dec_rval_t
NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **nint_ptr,
const void *buf_ptr, size_t size, int tag_mode) {
const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
long *native = (long *)*nint_ptr;
asn_dec_rval_t rval;
ber_tlv_len_t length;
/*
* If the structure is not there, allocate it.
*/
if(native == NULL) {
native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
if(native == NULL) {
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
}
ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
td->name, tag_mode);
/*
* Check tags.
*/
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;
}
/*
* ASN.1 encoded INTEGER: buf_ptr, length
* Fill the native, at the same time checking for overflow.
* If overflow occurred, return with RC_FAIL.
*/
{
INTEGER_t tmp;
union {
const void *constbuf;
void *nonconstbuf;
} unconst_buf;
long l;
unconst_buf.constbuf = buf_ptr;
tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
tmp.size = length;
if((specs&&specs->field_unsigned)
? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
: asn_INTEGER2long(&tmp, &l)) {
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
*native = l;
}
rval.code = RC_OK;
rval.consumed += length;
ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
(long)rval.consumed, (long)length, td->name, (long)*native);
return rval;
}
/*
* Encode the NativeInteger using the standard INTEGER type DER encoder.
*/
asn_enc_rval_t
NativeInteger_encode_der(const asn_TYPE_descriptor_t *sd, const void *ptr,
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
unsigned long native = *(const unsigned long *)ptr; /* Disable sign ext. */
asn_enc_rval_t erval = {0,0,0};
INTEGER_t tmp;
#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
tmp.buf = (uint8_t *)&native;
tmp.size = sizeof(native);
#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
uint8_t buf[sizeof(native)];
uint8_t *p;
/* Prepare a fake INTEGER */
for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
*p = (uint8_t)native;
tmp.buf = buf;
tmp.size = sizeof(buf);
#endif /* WORDS_BIGENDIAN */
/* Encode fake INTEGER */
erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
if(erval.structure_ptr == &tmp) {
erval.structure_ptr = ptr;
}
return erval;
}
/*
* Decode the chunk of XML text encoding INTEGER.
*/
asn_dec_rval_t
NativeInteger_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) {
const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
asn_dec_rval_t rval;
INTEGER_t st;
void *st_ptr = (void *)&st;
long *native = (long *)*sptr;
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
memset(&st, 0, sizeof(st));
rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
opt_mname, buf_ptr, size);
if(rval.code == RC_OK) {
long l;
if((specs&&specs->field_unsigned)
? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
: asn_INTEGER2long(&st, &l)) {
rval.code = RC_FAIL;
rval.consumed = 0;
} else {
*native = l;
}
} else {
/*
* Cannot restart from the middle;
* there is no place to save state in the native type.
* Request a continuation from the very beginning.
*/
rval.consumed = 0;
}
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
return rval;
}
asn_enc_rval_t
NativeInteger_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 asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
char scratch[32]; /* Enough for 64-bit int */
asn_enc_rval_t er = {0,0,0};
const long *native = (const long *)sptr;
(void)ilevel;
(void)flags;
if(!native) ASN__ENCODE_FAILED;
er.encoded = snprintf(scratch, sizeof(scratch),
(specs && specs->field_unsigned)
? "%lu" : "%ld", *native);
if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
|| cb(scratch, er.encoded, app_key) < 0)
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
#ifndef ASN_DISABLE_PER_SUPPORT
asn_dec_rval_t
NativeInteger_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;
long *native = (long *)*sptr;
INTEGER_t tmpint;
void *tmpintptr = &tmpint;
(void)opt_codec_ctx;
ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
memset(&tmpint, 0, sizeof tmpint);
rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
&tmpintptr, pd);
if(rval.code == RC_OK) {
if((specs&&specs->field_unsigned)
? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
: asn_INTEGER2long(&tmpint, native))
rval.code = RC_FAIL;
else
ASN_DEBUG("NativeInteger %s got value %ld",
td->name, *native);
}
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return rval;
}
asn_enc_rval_t
NativeInteger_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};
long native;
INTEGER_t tmpint;
if(!sptr) ASN__ENCODE_FAILED;
native = *(const long *)sptr;
ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
memset(&tmpint, 0, sizeof(tmpint));
if((specs&&specs->field_unsigned)
? asn_ulong2INTEGER(&tmpint, native)
: asn_long2INTEGER(&tmpint, native))
ASN__ENCODE_FAILED;
er = INTEGER_encode_uper(td, constraints, &tmpint, po);
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return er;
}
asn_dec_rval_t
NativeInteger_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;
long *native = (long *)*sptr;
INTEGER_t tmpint;
void *tmpintptr = &tmpint;
(void)opt_codec_ctx;
ASN_DEBUG("Decoding NativeInteger %s (APER)", td->name);
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
memset(&tmpint, 0, sizeof tmpint);
rval = INTEGER_decode_aper(opt_codec_ctx, td, constraints,
&tmpintptr, pd);
if(rval.code == RC_OK) {
if((specs&&specs->field_unsigned)
? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
: asn_INTEGER2long(&tmpint, native))
rval.code = RC_FAIL;
else
ASN_DEBUG("NativeInteger %s got value %ld",
td->name, *native);
}
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return rval;
}
asn_enc_rval_t
NativeInteger_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};
long native;
INTEGER_t tmpint;
if(!sptr) ASN__ENCODE_FAILED;
native = *(const long *)sptr;
ASN_DEBUG("Encoding NativeInteger %s %ld (APER)", td->name, native);
memset(&tmpint, 0, sizeof(tmpint));
if((specs&&specs->field_unsigned)
? asn_ulong2INTEGER(&tmpint, (unsigned long)native)
: asn_long2INTEGER(&tmpint, native))
ASN__ENCODE_FAILED;
er = INTEGER_encode_aper(td, constraints, &tmpint, po);
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return er;
}
#endif /* ASN_DISABLE_PER_SUPPORT */
/*
* INTEGER specific human-readable output.
*/
int
NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
const long *native = (const long *)sptr;
char scratch[32]; /* Enough for 64-bit int */
int ret;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(native) {
long value = *native;
ret = snprintf(scratch, sizeof(scratch),
(specs && specs->field_unsigned) ? "%lu" : "%ld", value);
assert(ret > 0 && (size_t)ret < sizeof(scratch));
if(cb(scratch, ret, app_key) < 0) return -1;
if(specs && (value >= 0 || !specs->field_unsigned)) {
const asn_INTEGER_enum_map_t *el =
INTEGER_map_value2enum(specs, value);
if(el) {
if(cb(" (", 2, app_key) < 0) return -1;
if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
if(cb(")", 1, app_key) < 0) return -1;
}
}
return 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}
void
NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
enum asn_struct_free_method method) {
@ -474,77 +145,3 @@ NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const v
return 1;
}
}
asn_random_fill_result_t
NativeInteger_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};
long *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 = (long *)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);
}
}
}
*sptr = st;
*st = value;
return result_ok;
}

View File

@ -22,22 +22,43 @@ extern "C" {
extern asn_TYPE_descriptor_t asn_DEF_NativeInteger;
extern asn_TYPE_operation_t asn_OP_NativeInteger;
asn_struct_free_f NativeInteger_free;
asn_struct_free_f NativeInteger_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f NativeInteger_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f NativeInteger_compare;
#define NativeInteger_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f NativeInteger_decode_ber;
der_type_encoder_f NativeInteger_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f NativeInteger_decode_xer;
xer_type_encoder_f NativeInteger_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f NativeInteger_decode_oer;
oer_type_encoder_f NativeInteger_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f NativeInteger_decode_uper;
per_type_encoder_f NativeInteger_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f NativeInteger_decode_aper;
per_type_encoder_f NativeInteger_encode_aper;
asn_random_fill_f NativeInteger_random_fill;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#define NativeInteger_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f NativeInteger_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NativeInteger.h>
asn_dec_rval_t
NativeInteger_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;
long *native = (long *)*sptr;
INTEGER_t tmpint;
void *tmpintptr = &tmpint;
(void)opt_codec_ctx;
ASN_DEBUG("Decoding NativeInteger %s (APER)", td->name);
if(!native) {
native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
memset(&tmpint, 0, sizeof tmpint);
rval = INTEGER_decode_aper(opt_codec_ctx, td, constraints,
&tmpintptr, pd);
if(rval.code == RC_OK) {
if((specs&&specs->field_unsigned)
? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
: asn_INTEGER2long(&tmpint, native))
rval.code = RC_FAIL;
else
ASN_DEBUG("NativeInteger %s got value %ld",
td->name, *native);
}
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return rval;
}
asn_enc_rval_t
NativeInteger_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};
long native;
INTEGER_t tmpint;
if(!sptr) ASN__ENCODE_FAILED;
native = *(const long *)sptr;
ASN_DEBUG("Encoding NativeInteger %s %ld (APER)", td->name, native);
memset(&tmpint, 0, sizeof(tmpint));
if((specs&&specs->field_unsigned)
? asn_ulong2INTEGER(&tmpint, (unsigned long)native)
: asn_long2INTEGER(&tmpint, native))
ASN__ENCODE_FAILED;
er = INTEGER_encode_aper(td, constraints, &tmpint, po);
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
return er;
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NativeInteger.h>
/*
* INTEGER specific human-readable output.
*/
int
NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;
const long *native = (const long *)sptr;
char scratch[32]; /* Enough for 64-bit int */
int ret;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(native) {
long value = *native;
ret = snprintf(scratch, sizeof(scratch),
(specs && specs->field_unsigned) ? "%lu" : "%ld", value);
assert(ret > 0 && (size_t)ret < sizeof(scratch));
if(cb(scratch, ret, app_key) < 0) return -1;
if(specs && (value >= 0 || !specs->field_unsigned)) {
const asn_INTEGER_enum_map_t *el =
INTEGER_map_value2enum(specs, value);
if(el) {
if(cb(" (", 2, app_key) < 0) return -1;
if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
if(cb(")", 1, app_key) < 0) return -1;
}
}
return 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <NativeInteger.h>
asn_random_fill_result_t
NativeInteger_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};
long *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 = (long *)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 {
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;
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
const asn_per_constraints_t *ct;
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);
}
}
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
}
*sptr = st;
*st = value;
return result_ok;
}

View File

@ -5,7 +5,7 @@
#include <asn_internal.h>
#include <INTEGER.h>
#include <OBJECT_IDENTIFIER.h>
#include <OCTET_STRING.h>
#include <asn_codecs_prim.h>
#include <limits.h> /* for CHAR_BIT */
#include <errno.h>
@ -13,50 +13,79 @@
* OBJECT IDENTIFIER basic type description.
*/
static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
(ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
};
asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER = {
ASN__PRIMITIVE_TYPE_free,
OBJECT_IDENTIFIER_print,
OCTET_STRING_compare, /* Implemented in terms of a string comparison */
ber_decode_primitive,
der_encode_primitive,
OBJECT_IDENTIFIER_decode_xer,
OBJECT_IDENTIFIER_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
ASN__PRIMITIVE_TYPE_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OBJECT_IDENTIFIER_print,
#else
OBJECT_IDENTIFIER_decode_oer,
OBJECT_IDENTIFIER_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare, /* Implemented in terms of a string comparison */
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_decode_primitive,
der_encode_primitive,
#else
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
OBJECT_IDENTIFIER_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OBJECT_IDENTIFIER_decode_xer,
OBJECT_IDENTIFIER_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OBJECT_IDENTIFIER_decode_oer,
OBJECT_IDENTIFIER_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
OBJECT_IDENTIFIER_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
"OBJECT IDENTIFIER",
"OBJECT_IDENTIFIER",
&asn_OP_OBJECT_IDENTIFIER,
asn_DEF_OBJECT_IDENTIFIER_tags,
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
{ 0, 0, OBJECT_IDENTIFIER_constraint },
0, 0, /* No members */
0 /* No specifics */
"OBJECT IDENTIFIER",
"OBJECT_IDENTIFIER",
&asn_OP_OBJECT_IDENTIFIER,
asn_DEF_OBJECT_IDENTIFIER_tags,
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
OBJECT_IDENTIFIER_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
int
@ -143,11 +172,12 @@ OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, size_t arcbuf_len,
}
static ssize_t
ssize_t
OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st,
asn_app_consume_bytes_f *cb, void *app_key) {
char scratch[32];
asn_oid_arc_t arc0, arc1;
asn_oid_arc_t arc0 = 0;
asn_oid_arc_t arc1 = 0;
size_t produced = 0;
size_t off = 0;
ssize_t rd;
@ -195,105 +225,11 @@ OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st,
return produced;
}
static enum xer_pbd_rval
OBJECT_IDENTIFIER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
const void *chunk_buf, size_t chunk_size) {
OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
const char *chunk_end = (const char *)chunk_buf + chunk_size;
const char *endptr;
asn_oid_arc_t s_arcs[10];
asn_oid_arc_t *arcs = s_arcs;
ssize_t num_arcs;
ssize_t ret;
(void)td;
num_arcs = OBJECT_IDENTIFIER_parse_arcs(
(const char *)chunk_buf, chunk_size, arcs,
sizeof(s_arcs) / sizeof(s_arcs[0]), &endptr);
if(num_arcs < 0) {
/* Expecting more than zero arcs */
return XPBD_BROKEN_ENCODING;
} else if(num_arcs == 0) {
return XPBD_NOT_BODY_IGNORE;
}
assert(endptr == chunk_end);
if((size_t)num_arcs > sizeof(s_arcs)/sizeof(s_arcs[0])) {
arcs = (asn_oid_arc_t *)MALLOC(num_arcs * sizeof(asn_oid_arc_t));
if(!arcs) return XPBD_SYSTEM_FAILURE;
ret = OBJECT_IDENTIFIER_parse_arcs((const char *)chunk_buf, chunk_size,
arcs, num_arcs, &endptr);
if(ret != num_arcs)
return XPBD_SYSTEM_FAILURE; /* assert?.. */
}
/*
* Convert arcs into BER representation.
*/
ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, num_arcs);
if(arcs != s_arcs) FREEMEM(arcs);
return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
}
asn_dec_rval_t
OBJECT_IDENTIFIER_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(OBJECT_IDENTIFIER_t), opt_mname,
buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
}
asn_enc_rval_t
OBJECT_IDENTIFIER_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 OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
asn_enc_rval_t er = {0,0,0};
(void)ilevel;
(void)flags;
if(!st || !st->buf) {
ASN__ENCODE_FAILED;
}
er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
if(er.encoded < 0) ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
int
OBJECT_IDENTIFIER_print(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb,
void *app_key) {
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* Dump preamble */
if(cb("{ ", 2, app_key) < 0)
return -1;
if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) {
return -1;
}
return (cb(" }", 2, app_key) < 0) ? -1 : 0;
}
ssize_t
OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *st, asn_oid_arc_t *arcs,
size_t arc_slots) {
asn_oid_arc_t arc0, arc1;
asn_oid_arc_t arc0 = 0;
asn_oid_arc_t arc1 = 0;
size_t num_arcs = 0;
size_t off;
ssize_t rd;
@ -588,69 +524,3 @@ OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
errno = EINVAL; /* Broken OID */
return -1;
}
/*
* Generate values from the list of interesting values, or just a random
* value up to the upper limit.
*/
static asn_oid_arc_t
OBJECT_IDENTIFIER__biased_random_arc(asn_oid_arc_t upper_bound) {
const asn_oid_arc_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
size_t idx;
switch(asn_random_between(0, 2)) {
case 0:
idx = asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1);
if(values[idx] < upper_bound) {
return values[idx];
}
/* Fall through */
case 1:
return asn_random_between(0, upper_bound);
case 2:
default:
return upper_bound;
}
}
asn_random_fill_result_t
OBJECT_IDENTIFIER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
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};
OBJECT_IDENTIFIER_t *st;
asn_oid_arc_t arcs[5];
size_t arcs_len = asn_random_between(2, 5);
size_t i;
(void)constraints;
if(max_length < arcs_len) return result_skipped;
if(*sptr) {
st = *sptr;
} else {
st = CALLOC(1, sizeof(*st));
}
arcs[0] = asn_random_between(0, 2);
arcs[1] = OBJECT_IDENTIFIER__biased_random_arc(
arcs[0] <= 1 ? 39 : (ASN_OID_ARC_MAX - 80));
for(i = 2; i < arcs_len; i++) {
arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(ASN_OID_ARC_MAX);
}
if(OBJECT_IDENTIFIER_set_arcs(st, arcs, arcs_len)) {
if(st != *sptr) {
ASN_STRUCT_FREE(*td, st);
}
return result_failed;
}
*sptr = st;
result_ok.length = st->size;
return result_ok;
}

View File

@ -21,23 +21,47 @@ typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t;
extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER;
extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER;
ssize_t OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st,
asn_app_consume_bytes_f *cb,
void *app_key);
#define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f OBJECT_IDENTIFIER_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define OBJECT_IDENTIFIER_compare OCTET_STRING_compare
asn_constr_check_f OBJECT_IDENTIFIER_constraint;
der_type_encoder_f OBJECT_IDENTIFIER_encode_der;
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive
#define OBJECT_IDENTIFIER_encode_der der_encode_primitive
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer;
xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer;
asn_random_fill_f OBJECT_IDENTIFIER_random_fill;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free
#define OBJECT_IDENTIFIER_compare OCTET_STRING_compare
#define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive
#define OBJECT_IDENTIFIER_encode_der der_encode_primitive
#define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive
#define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive
#define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper
#define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper
#define OBJECT_IDENTIFIER_decode_aper OCTET_STRING_decode_aper
#define OBJECT_IDENTIFIER_encode_aper OCTET_STRING_encode_aper
#if !defined(ASN_DISABLE_OER_SUPPORT)
#define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive
#define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper
#define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define OBJECT_IDENTIFIER_decode_aper OCTET_STRING_decode_aper
#define OBJECT_IDENTIFIER_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f OBJECT_IDENTIFIER_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
/**********************************
* Some handy conversion routines *

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OBJECT_IDENTIFIER.h>
int
OBJECT_IDENTIFIER_print(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb,
void *app_key) {
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* Dump preamble */
if(cb("{ ", 2, app_key) < 0)
return -1;
if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) {
return -1;
}
return (cb(" }", 2, app_key) < 0) ? -1 : 0;
}

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OBJECT_IDENTIFIER.h>
/*
* Generate values from the list of interesting values, or just a random
* value up to the upper limit.
*/
static asn_oid_arc_t
OBJECT_IDENTIFIER__biased_random_arc(asn_oid_arc_t upper_bound) {
const asn_oid_arc_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
size_t idx;
switch(asn_random_between(0, 2)) {
case 0:
idx = asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1);
if(values[idx] < upper_bound) {
return values[idx];
}
/* Fall through */
case 1:
return asn_random_between(0, upper_bound);
case 2:
default:
return upper_bound;
}
}
asn_random_fill_result_t
OBJECT_IDENTIFIER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
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};
OBJECT_IDENTIFIER_t *st;
asn_oid_arc_t arcs[5];
size_t arcs_len = asn_random_between(2, 5);
size_t i;
(void)constraints;
if(max_length < arcs_len) return result_skipped;
if(*sptr) {
st = *sptr;
} else {
st = CALLOC(1, sizeof(*st));
}
arcs[0] = asn_random_between(0, 2);
arcs[1] = OBJECT_IDENTIFIER__biased_random_arc(
arcs[0] <= 1 ? 39 : (ASN_OID_ARC_MAX - 80));
for(i = 2; i < arcs_len; i++) {
arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(ASN_OID_ARC_MAX);
}
if(OBJECT_IDENTIFIER_set_arcs(st, arcs, arcs_len)) {
if(st != *sptr) {
ASN_STRUCT_FREE(*td, st);
}
return result_failed;
}
*sptr = st;
result_ok.length = st->size;
return result_ok;
}

File diff suppressed because it is too large Load Diff

View File

@ -22,27 +22,68 @@ extern asn_TYPE_descriptor_t asn_DEF_OCTET_STRING;
extern asn_TYPE_operation_t asn_OP_OCTET_STRING;
asn_struct_free_f OCTET_STRING_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f OCTET_STRING_print;
asn_struct_print_f OCTET_STRING_print_utf8;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f OCTET_STRING_compare;
#define OCTET_STRING_constraint asn_generic_no_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f OCTET_STRING_decode_ber;
der_type_encoder_f OCTET_STRING_encode_der;
xer_type_decoder_f OCTET_STRING_decode_xer_hex; /* Hexadecimal */
xer_type_decoder_f OCTET_STRING_decode_xer_binary; /* 01010111010 */
xer_type_decoder_f OCTET_STRING_decode_xer_utf8; /* ASCII/UTF-8 */
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f OCTET_STRING_decode_xer_hex; /* Hexadecimal */
xer_type_decoder_f OCTET_STRING_decode_xer_binary; /* 01010111010 */
xer_type_decoder_f OCTET_STRING_decode_xer_utf8; /* ASCII/UTF-8 */
xer_type_encoder_f OCTET_STRING_encode_xer;
xer_type_encoder_f OCTET_STRING_encode_xer_utf8;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f OCTET_STRING_decode_oer;
oer_type_encoder_f OCTET_STRING_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f OCTET_STRING_decode_uper;
per_type_encoder_f OCTET_STRING_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f OCTET_STRING_decode_aper;
per_type_encoder_f OCTET_STRING_encode_aper;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f OCTET_STRING_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
#define OCTET_STRING_constraint asn_generic_no_constraint
#define OCTET_STRING_decode_xer OCTET_STRING_decode_xer_hex
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
int OCTET_STRING_per_get_characters(
asn_per_data_t *po,
uint8_t *buf,
size_t units,
unsigned int bpc,
unsigned int unit_bits,
long lb,
long ub,
const asn_per_constraints_t *pc);
int OCTET_STRING_per_put_characters(
asn_per_outp_t *po,
const uint8_t *buf,
size_t units,
unsigned int bpc,
unsigned int unit_bits,
long lb,
long ub,
const asn_per_constraints_t *pc);
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
/******************************
* Handy conversion routines. *
******************************/
@ -95,6 +136,23 @@ size_t OCTET_STRING_random_length_constrained(
const asn_TYPE_descriptor_t *, const asn_encoding_constraints_t *,
size_t max_length);
#if !defined(ASN_DISABLE_BER_SUPPORT)
struct _stack_el {
ber_tlv_len_t left; /* What's left to read (or -1) */
ber_tlv_len_t got; /* What was actually processed */
unsigned cont_level; /* Depth of subcontainment */
int want_nulls; /* Want null "end of content" octets? */
int bits_chopped; /* Flag in BIT STRING mode */
ber_tlv_tag_t tag; /* For debugging purposes */
struct _stack_el *prev;
struct _stack_el *next;
};
struct _stack {
struct _stack_el *tail;
struct _stack_el *cur_ptr;
};
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,412 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OCTET_STRING.h>
#include <BIT_STRING.h> /* for .bits_unused member */
#undef RETURN
#define RETURN(_code) do {\
asn_dec_rval_t tmprval;\
tmprval.code = _code;\
tmprval.consumed = consumed_myself;\
return tmprval;\
} while(0)
static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
{ APC_CONSTRAINED, 8, 8, 0, 255 },
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
0, 0
};
asn_dec_rval_t
OCTET_STRING_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_OCTET_STRING_specifics_t *specs = td->specifics
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_OCTET_STRING_specs;
const asn_per_constraints_t *pc = constraints
? constraints
: td->encoding_constraints.per_constraints;
const asn_per_constraint_t *cval;
const asn_per_constraint_t *csiz;
asn_dec_rval_t rval = { RC_OK, 0 };
BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
ssize_t consumed_myself = 0;
int repeat;
enum {
OS__BPC_BIT = 0,
OS__BPC_CHAR = 1,
OS__BPC_U16 = 2,
OS__BPC_U32 = 4
} bpc; /* Bytes per character */
unsigned int unit_bits;
unsigned int canonical_unit_bits;
(void)opt_codec_ctx;
if(pc) {
cval = &pc->value;
csiz = &pc->size;
} else {
cval = &asn_DEF_OCTET_STRING_constraints.value;
csiz = &asn_DEF_OCTET_STRING_constraints.size;
}
switch(specs->subvariant) {
default:
/*
case ASN_OSUBV_ANY:
ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
RETURN(RC_FAIL);
*/
case ASN_OSUBV_BIT:
canonical_unit_bits = unit_bits = 1;
bpc = OS__BPC_BIT;
break;
case ASN_OSUBV_ANY:
case ASN_OSUBV_STR:
canonical_unit_bits = unit_bits = 8;
/*
if(cval->flags & APC_CONSTRAINED)
unit_bits = cval->range_bits;
*/
bpc = OS__BPC_CHAR;
break;
case ASN_OSUBV_U16:
canonical_unit_bits = unit_bits = 16;
if(cval->flags & APC_CONSTRAINED)
unit_bits = cval->range_bits;
bpc = OS__BPC_U16;
break;
case ASN_OSUBV_U32:
canonical_unit_bits = unit_bits = 32;
if(cval->flags & APC_CONSTRAINED)
unit_bits = cval->range_bits;
bpc = OS__BPC_U32;
break;
}
/*
* Allocate the string.
*/
if(!st) {
st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) RETURN(RC_FAIL);
}
ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
if(csiz->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
if(inext < 0) RETURN(RC_WMORE);
if(inext) {
csiz = &asn_DEF_OCTET_STRING_constraints.size;
cval = &asn_DEF_OCTET_STRING_constraints.value;
unit_bits = canonical_unit_bits;
}
}
if(csiz->effective_bits >= 0) {
FREEMEM(st->buf);
if(bpc) {
st->size = csiz->upper_bound * bpc;
} else {
st->size = (csiz->upper_bound + 7) >> 3;
}
st->buf = (uint8_t *)MALLOC(st->size + 1);
if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
}
/* X.691, #16.5: zero-length encoding */
/* X.691, #16.6: short fixed length encoding (up to 2 octets) */
/* X.691, #16.7: long fixed length encoding (up to 64K octets) */
if(csiz->effective_bits == 0) {
int ret;
/* X.691 #16 NOTE 1 for fixed length (<= 16 bits) strings */
if (st->size > 2 || csiz->range_bits != 0) {
if (aper_get_align(pd) < 0)
RETURN(RC_FAIL);
}
if(bpc) {
ASN_DEBUG("Decoding OCTET STRING size %ld",
csiz->upper_bound);
ret = OCTET_STRING_per_get_characters(pd, st->buf,
csiz->upper_bound,
bpc, unit_bits,
cval->lower_bound,
cval->upper_bound,
pc);
if(ret > 0) RETURN(RC_FAIL);
} else {
ASN_DEBUG("Decoding BIT STRING size %ld",
csiz->upper_bound);
ret = per_get_many_bits(pd, st->buf, 0,
unit_bits * csiz->upper_bound);
}
if(ret < 0) RETURN(RC_WMORE);
consumed_myself += unit_bits * csiz->upper_bound;
st->buf[st->size] = 0;
if(bpc == 0) {
int ubs = (csiz->upper_bound & 0x7);
st->bits_unused = ubs ? 8 - ubs : 0;
}
RETURN(RC_OK);
}
st->size = 0;
do {
ssize_t raw_len;
ssize_t len_bytes;
ssize_t len_bits;
void *p;
int ret;
repeat = 0;
/* Get the PER length */
if (csiz->upper_bound - csiz->lower_bound == 0)
/* Indefinite length case */
raw_len = aper_get_length(pd, -1, csiz->effective_bits, &repeat);
else
raw_len = aper_get_length(pd, csiz->upper_bound - csiz->lower_bound + 1, csiz->effective_bits, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
raw_len += csiz->lower_bound;
ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
(long)csiz->effective_bits, (long)raw_len,
repeat ? "repeat" : "once", td->name);
/* X.691 #16 NOTE 1 for fixed length (<=16 bits) strings */
if ((raw_len > 2) || (csiz->upper_bound > 2) || (csiz->range_bits != 0))
{
if (aper_get_align(pd) < 0)
RETURN(RC_FAIL);
}
if(bpc) {
len_bytes = raw_len * bpc;
len_bits = len_bytes * unit_bits;
} else {
len_bits = raw_len;
len_bytes = (len_bits + 7) >> 3;
if(len_bits & 0x7)
st->bits_unused = 8 - (len_bits & 0x7);
/* len_bits be multiple of 16K if repeat is set */
}
p = REALLOC(st->buf, st->size + len_bytes + 1);
if(!p) RETURN(RC_FAIL);
st->buf = (uint8_t *)p;
if(bpc) {
ret = OCTET_STRING_per_get_characters(pd,
&st->buf[st->size],
raw_len, bpc,
unit_bits,
cval->lower_bound,
cval->upper_bound,
pc);
if(ret > 0) RETURN(RC_FAIL);
} else {
ret = per_get_many_bits(pd, &st->buf[st->size],
0, len_bits);
}
if(ret < 0) RETURN(RC_WMORE);
st->size += len_bytes;
} while(repeat);
st->buf[st->size] = 0; /* nul-terminate */
return rval;
}
asn_enc_rval_t
OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_OCTET_STRING_specifics_t *specs = td->specifics
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_OCTET_STRING_specs;
const asn_per_constraints_t *pc = constraints
? constraints
: td->encoding_constraints.per_constraints;
const asn_per_constraint_t *cval;
const asn_per_constraint_t *csiz;
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
asn_enc_rval_t er = { 0, 0, 0 };
int inext = 0; /* Lies not within extension root */
unsigned int unit_bits;
unsigned int canonical_unit_bits;
unsigned int sizeinunits;
const uint8_t *buf;
int ret;
enum {
OS__BPC_BIT = 0,
OS__BPC_CHAR = 1,
OS__BPC_U16 = 2,
OS__BPC_U32 = 4
} bpc; /* Bytes per character */
int ct_extensible;
if(!st || (!st->buf && st->size))
ASN__ENCODE_FAILED;
if(pc) {
cval = &pc->value;
csiz = &pc->size;
} else {
cval = &asn_DEF_OCTET_STRING_constraints.value;
csiz = &asn_DEF_OCTET_STRING_constraints.size;
}
ct_extensible = csiz->flags & APC_EXTENSIBLE;
switch(specs->subvariant) {
default:
/*
case ASN_OSUBV_ANY:
ASN__ENCODE_FAILED;
*/
case ASN_OSUBV_BIT:
canonical_unit_bits = unit_bits = 1;
bpc = OS__BPC_BIT;
sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
ASN_DEBUG("BIT STRING of %d bytes",
sizeinunits);
break;
case ASN_OSUBV_ANY:
case ASN_OSUBV_STR:
canonical_unit_bits = unit_bits = 8;
/*
if(cval->flags & APC_CONSTRAINED)
unit_bits = 8;
*/
bpc = OS__BPC_CHAR;
sizeinunits = st->size;
break;
case ASN_OSUBV_U16:
canonical_unit_bits = unit_bits = 16;
if(cval->flags & APC_CONSTRAINED)
unit_bits = cval->range_bits;
bpc = OS__BPC_U16;
sizeinunits = st->size / 2;
break;
case ASN_OSUBV_U32:
canonical_unit_bits = unit_bits = 32;
if(cval->flags & APC_CONSTRAINED)
unit_bits = cval->range_bits;
bpc = OS__BPC_U32;
sizeinunits = st->size / 4;
break;
}
ASN_DEBUG("Encoding %s into %d units of %d bits"
" (%ld..%ld, effective %d)%s",
td->name, sizeinunits, unit_bits,
csiz->lower_bound, csiz->upper_bound,
csiz->effective_bits, ct_extensible ? " EXT" : "");
/* Figure out wheter size lies within PER visible constraint */
if(csiz->effective_bits >= 0) {
if((int)sizeinunits < csiz->lower_bound
|| (int)sizeinunits > csiz->upper_bound) {
if(ct_extensible) {
cval = &asn_DEF_OCTET_STRING_constraints.value;
csiz = &asn_DEF_OCTET_STRING_constraints.size;
unit_bits = canonical_unit_bits;
inext = 1;
} else
ASN__ENCODE_FAILED;
}
} else {
inext = 0;
}
if(ct_extensible) {
/* Declare whether length is [not] within extension root */
if(per_put_few_bits(po, inext, 1))
ASN__ENCODE_FAILED;
}
/* X.691, #16.5: zero-length encoding */
/* X.691, #16.6: short fixed length encoding (up to 2 octets) */
/* X.691, #16.7: long fixed length encoding (up to 64K octets) */
if(csiz->effective_bits >= 0) {
ASN_DEBUG("Encoding %lu bytes (%ld), length in %d bits",
st->size, sizeinunits - csiz->lower_bound,
csiz->effective_bits);
if (csiz->effective_bits > 0) {
ret = aper_put_length(po,
csiz->upper_bound - csiz->lower_bound + 1,
sizeinunits - csiz->lower_bound, 0);
if(ret) ASN__ENCODE_FAILED;
}
if (csiz->effective_bits > 0 || (st->size > 2)
|| (csiz->upper_bound > (2 * 8 / unit_bits))
|| (csiz->range_bits != 0))
{ /* X.691 #16 NOTE 1 for fixed length (<=16 bits) strings*/
if (aper_put_align(po) < 0)
ASN__ENCODE_FAILED;
}
if(bpc) {
ret = OCTET_STRING_per_put_characters(po, st->buf,
sizeinunits,
bpc, unit_bits,
cval->lower_bound,
cval->upper_bound,
pc);
} else {
ret = per_put_many_bits(po, st->buf,
sizeinunits * unit_bits);
}
if(ret) ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
ASN_DEBUG("Encoding %lu bytes", st->size);
if(sizeinunits == 0) {
if(aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}
buf = st->buf;
while(sizeinunits) {
int need_eom = 0;
ssize_t maySave = aper_put_length(po, -1, sizeinunits, &need_eom);
if(maySave < 0) ASN__ENCODE_FAILED;
ASN_DEBUG("Encoding %ld of %ld",
(long)maySave, (long)sizeinunits);
if(bpc) {
ret = OCTET_STRING_per_put_characters(po, buf, maySave,
bpc, unit_bits,
cval->lower_bound,
cval->upper_bound,
pc);
} else {
ret = per_put_many_bits(po, buf, maySave * unit_bits);
}
if(ret) ASN__ENCODE_FAILED;
if(bpc)
buf += maySave * bpc;
else
buf += maySave >> 3;
sizeinunits -= maySave;
assert(!(maySave & 0x07) || !sizeinunits);
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
ASN__ENCODED_OK(er);
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OCTET_STRING.h>
int
OCTET_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
const char * const h2c = "0123456789ABCDEF";
const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
char scratch[16 * 3 + 4];
char *p = scratch;
uint8_t *buf;
uint8_t *end;
size_t i;
(void)td; /* Unused argument */
if(!st || (!st->buf && st->size))
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/*
* Dump the contents of the buffer in hexadecimal.
*/
buf = st->buf;
end = buf + st->size;
for(i = 0; buf < end; buf++, i++) {
if(!(i % 16) && (i || st->size > 16)) {
if(cb(scratch, p - scratch, app_key) < 0)
return -1;
_i_INDENT(1);
p = scratch;
}
*p++ = h2c[(*buf >> 4) & 0x0F];
*p++ = h2c[*buf & 0x0F];
*p++ = 0x20;
}
if(p > scratch) {
p--; /* Remove the tail space */
if(cb(scratch, p - scratch, app_key) < 0)
return -1;
}
return 0;
}
int
OCTET_STRING_print_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
int ilevel, asn_app_consume_bytes_f *cb,
void *app_key) {
const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(st && (st->buf || !st->size)) {
return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}

View File

@ -0,0 +1,209 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OCTET_STRING.h>
/*
* Biased function for randomizing character values around their limits.
*/
static uint32_t
OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
assert(lb <= ub);
switch(asn_random_between(0, 16)) {
case 0:
if(lb < ub) return lb + 1;
/* Fall through */
case 1:
return lb;
case 2:
if(lb < ub) return ub - 1;
/* Fall through */
case 3:
return ub;
default:
return asn_random_between(lb, ub);
}
}
asn_random_fill_result_t
OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
const asn_OCTET_STRING_specifics_t *specs = td->specifics
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_OCTET_STRING_specs;
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};
unsigned int unit_bytes = 1;
unsigned long clb = 0; /* Lower bound on char */
unsigned long cub = 255; /* Higher bound on char value */
uint8_t *buf;
uint8_t *bend;
uint8_t *b;
size_t rnd_len;
OCTET_STRING_t *st;
if(max_length == 0 && !*sptr) return result_skipped;
switch(specs->subvariant) {
default:
case ASN_OSUBV_ANY:
return result_failed;
case ASN_OSUBV_BIT:
/* Handled by BIT_STRING itself. */
return result_failed;
case ASN_OSUBV_STR:
unit_bytes = 1;
clb = 0;
cub = 255;
break;
case ASN_OSUBV_U16:
unit_bytes = 2;
clb = 0;
cub = 65535;
break;
case ASN_OSUBV_U32:
unit_bytes = 4;
clb = 0;
cub = 0x10FFFF;
break;
}
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
if(!constraints || !constraints->per_constraints)
constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc = &constraints->per_constraints->value;
if(pc->flags & APC_SEMI_CONSTRAINED) {
clb = pc->lower_bound;
} else if(pc->flags & APC_CONSTRAINED) {
clb = pc->lower_bound;
cub = pc->upper_bound;
}
}
#else
if(!constraints) constraints = &td->encoding_constraints;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
rnd_len =
OCTET_STRING_random_length_constrained(td, constraints, max_length);
buf = CALLOC(unit_bytes, rnd_len + 1);
if(!buf) return result_failed;
bend = &buf[unit_bytes * rnd_len];
switch(unit_bytes) {
case 1:
for(b = buf; b < bend; b += unit_bytes) {
*(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
}
*(uint8_t *)b = 0;
break;
case 2:
for(b = buf; b < bend; b += unit_bytes) {
uint32_t code = OCTET_STRING__random_char(clb, cub);
b[0] = code >> 8;
b[1] = code;
}
*(uint16_t *)b = 0;
break;
case 4:
for(b = buf; b < bend; b += unit_bytes) {
uint32_t code = OCTET_STRING__random_char(clb, cub);
b[0] = code >> 24;
b[1] = code >> 16;
b[2] = code >> 8;
b[3] = code;
}
*(uint32_t *)b = 0;
break;
}
if(*sptr) {
st = *sptr;
FREEMEM(st->buf);
} else {
st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!st) {
FREEMEM(buf);
return result_failed;
}
}
st->buf = buf;
st->size = unit_bytes * rnd_len;
result_ok.length = st->size;
return result_ok;
}
size_t
OCTET_STRING_random_length_constrained(
const asn_TYPE_descriptor_t *td,
const asn_encoding_constraints_t *constraints, size_t max_length) {
const unsigned lengths[] = {0, 1, 2, 3, 4, 8,
126, 127, 128, 16383, 16384, 16385,
65534, 65535, 65536, 65537};
size_t rnd_len;
/* Figure out how far we should go */
rnd_len = lengths[asn_random_between(
0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
if(!constraints || !constraints->per_constraints)
constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc = &constraints->per_constraints->size;
if(pc->flags & APC_CONSTRAINED) {
long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
? pc->upper_bound
: (ssize_t)max_length;
if(max_length <= (size_t)pc->lower_bound) {
return pc->lower_bound;
}
if(pc->flags & APC_EXTENSIBLE) {
switch(asn_random_between(0, 5)) {
case 0:
if(pc->lower_bound > 0) {
rnd_len = pc->lower_bound - 1;
break;
}
/* Fall through */
case 1:
rnd_len = pc->upper_bound + 1;
break;
case 2:
/* Keep rnd_len from the table */
if(rnd_len <= max_length) {
break;
}
/* Fall through */
default:
rnd_len = asn_random_between(pc->lower_bound,
suggested_upper_bound);
}
} else {
rnd_len =
asn_random_between(pc->lower_bound, suggested_upper_bound);
}
} else {
rnd_len = asn_random_between(0, max_length);
}
} else {
#else
if(!constraints) constraints = &td->encoding_constraints;
{
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
if(rnd_len > max_length) {
rnd_len = asn_random_between(0, max_length);
}
}
return rnd_len;
}

View File

@ -5,505 +5,54 @@
#include <asn_internal.h>
#include <OPEN_TYPE.h>
#include <constr_CHOICE.h>
#include <per_opentype.h>
#include <errno.h>
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 */
OPEN_TYPE_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OPEN_TYPE_print,
#else
OPEN_TYPE_decode_oer,
OPEN_TYPE_encode_oer,
#endif
#ifdef ASN_DISABLE_PER_SUPPORT
0, 0, 0, 0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OPEN_TYPE_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OPEN_TYPE_decode_ber,
OPEN_TYPE_encode_der,
#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 */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OPEN_TYPE_decode_xer,
OPEN_TYPE_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OPEN_TYPE_decode_oer,
OPEN_TYPE_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OPEN_TYPE_decode_uper,
OPEN_TYPE_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OPEN_TYPE_decode_aper,
OPEN_TYPE_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
0, /* Random fill is not supported for open type */
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
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 */

View File

@ -6,69 +6,97 @@
#define ASN_OPEN_TYPE_H
#include <asn_application.h>
///////////#include <per_support.h>
#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);
#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_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);
#define OPEN_TYPE_free CHOICE_free
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);
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define OPEN_TYPE_print CHOICE_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
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);
#define OPEN_TYPE_compare CHOICE_compare
#define OPEN_TYPE_constraint CHOICE_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
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);
#define OPEN_TYPE_decode_ber NULL
#define OPEN_TYPE_encode_der CHOICE_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
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);
#define OPEN_TYPE_decode_xer NULL
#define OPEN_TYPE_encode_xer CHOICE_encode_xer
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
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);
#define OPEN_TYPE_decode_oer NULL
#define OPEN_TYPE_encode_oer CHOICE_encode_oer
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
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);
#define OPEN_TYPE_decode_uper NULL
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);
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
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);
#define OPEN_TYPE_decode_aper NULL
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);
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
extern asn_TYPE_operation_t asn_OP_OPEN_TYPE;
#ifdef __cplusplus
}

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OPEN_TYPE.h>
#include <constr_CHOICE.h>
#include <aper_opentype.h>
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);
}

View File

@ -9,50 +9,78 @@
* ObjectDescriptor basic type description.
*/
static const ber_tlv_tag_t asn_DEF_ObjectDescriptor_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (7 << 2)), /* [UNIVERSAL 7] IMPLICIT ... */
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
(ASN_TAG_CLASS_UNIVERSAL | (7 << 2)), /* [UNIVERSAL 7] IMPLICIT ... */
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
};
asn_TYPE_operation_t asn_OP_ObjectDescriptor = {
OCTET_STRING_free,
OCTET_STRING_print_utf8, /* Treat as ASCII subset (it's not) */
OCTET_STRING_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OCTET_STRING_print_utf8, /* Treat as ASCII subset (it's not) */
#else
0,
0,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
#else
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
0, /* Not supported for ObjectDescriptor */
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
0,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
0, /* Not supported for ObjectDescriptor */
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_ObjectDescriptor = {
"ObjectDescriptor",
"ObjectDescriptor",
&asn_OP_ObjectDescriptor,
asn_DEF_ObjectDescriptor_tags,
sizeof(asn_DEF_ObjectDescriptor_tags)
/ sizeof(asn_DEF_ObjectDescriptor_tags[0]) - 1,
asn_DEF_ObjectDescriptor_tags,
sizeof(asn_DEF_ObjectDescriptor_tags)
/ sizeof(asn_DEF_ObjectDescriptor_tags[0]),
{ 0, 0, asn_generic_unknown_constraint },
0, 0, /* No members */
0 /* No specifics */
"ObjectDescriptor",
"ObjectDescriptor",
&asn_OP_ObjectDescriptor,
asn_DEF_ObjectDescriptor_tags,
sizeof(asn_DEF_ObjectDescriptor_tags)
/ sizeof(asn_DEF_ObjectDescriptor_tags[0]) - 1,
asn_DEF_ObjectDescriptor_tags,
sizeof(asn_DEF_ObjectDescriptor_tags)
/ sizeof(asn_DEF_ObjectDescriptor_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_generic_unknown_constraint
},
0, 0, /* No members */
0 /* No specifics */
};

View File

@ -16,17 +16,32 @@ typedef GraphicString_t ObjectDescriptor_t; /* Implemented via GraphicString */
extern asn_TYPE_descriptor_t asn_DEF_ObjectDescriptor;
extern asn_TYPE_operation_t asn_OP_ObjectDescriptor;
#define ObjectDescriptor_free OCTET_STRING_free
#define ObjectDescriptor_print OCTET_STRING_print_utf8
#define ObjectDescriptor_constraint asn_generic_unknown_constraint
#define ObjectDescriptor_decode_ber OCTET_STRING_decode_ber
#define ObjectDescriptor_encode_der OCTET_STRING_encode_der
#define ObjectDescriptor_decode_xer OCTET_STRING_decode_xer_utf8
#define ObjectDescriptor_encode_xer OCTET_STRING_encode_xer_utf8
#define ObjectDescriptor_decode_uper OCTET_STRING_decode_uper
#define ObjectDescriptor_encode_uper OCTET_STRING_encode_uper
#define ObjectDescriptor_decode_aper OCTET_STRING_decode_aper
#define ObjectDescriptor_encode_aper OCTET_STRING_encode_aper
#define ObjectDescriptor_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define ObjectDescriptor_print OCTET_STRING_print_utf8
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define ObjectDescriptor_constraint asn_generic_unknown_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define ObjectDescriptor_decode_ber OCTET_STRING_decode_ber
#define ObjectDescriptor_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define ObjectDescriptor_decode_xer OCTET_STRING_decode_xer_utf8
#define ObjectDescriptor_encode_xer OCTET_STRING_encode_xer_utf8
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define ObjectDescriptor_decode_uper OCTET_STRING_decode_uper
#define ObjectDescriptor_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define ObjectDescriptor_decode_aper OCTET_STRING_decode_aper
#define ObjectDescriptor_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -30,68 +30,98 @@ static const int _PrintableString_code2value[74] = {
* PrintableString basic type description.
*/
static const ber_tlv_tag_t asn_DEF_PrintableString_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (19 << 2)), /* [UNIVERSAL 19] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
(ASN_TAG_CLASS_UNIVERSAL | (19 << 2)), /* [UNIVERSAL 19] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
};
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
static int asn_DEF_PrintableString_v2c(unsigned int value) {
return _PrintableString_alphabet[value > 255 ? 0 : value] - 1;
return _PrintableString_alphabet[value > 255 ? 0 : value] - 1;
}
static int asn_DEF_PrintableString_c2v(unsigned int code) {
if(code < 74)
return _PrintableString_code2value[code];
return -1;
if(code < 74)
return _PrintableString_code2value[code];
return -1;
}
static asn_per_constraints_t asn_DEF_PrintableString_per_constraints = {
{ APC_CONSTRAINED, 4, 4, 0x20, 0x39 }, /* Value */
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
asn_DEF_PrintableString_v2c,
asn_DEF_PrintableString_c2v
{ APC_CONSTRAINED, 4, 4, 0x20, 0x39 }, /* Value */
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
asn_DEF_PrintableString_v2c,
asn_DEF_PrintableString_c2v
};
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_TYPE_operation_t asn_OP_PrintableString = {
OCTET_STRING_free,
OCTET_STRING_print_utf8, /* ASCII subset */
OCTET_STRING_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OCTET_STRING_print_utf8, /* ASCII subset */
#else
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
#else
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
OCTET_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
OCTET_STRING_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_PrintableString = {
"PrintableString",
"PrintableString",
&asn_OP_PrintableString,
asn_DEF_PrintableString_tags,
sizeof(asn_DEF_PrintableString_tags)
/ sizeof(asn_DEF_PrintableString_tags[0]) - 1,
asn_DEF_PrintableString_tags,
sizeof(asn_DEF_PrintableString_tags)
/ sizeof(asn_DEF_PrintableString_tags[0]),
{ 0, &asn_DEF_PrintableString_per_constraints, PrintableString_constraint },
0, 0, /* No members */
0 /* No specifics */
"PrintableString",
"PrintableString",
&asn_OP_PrintableString,
asn_DEF_PrintableString_tags,
sizeof(asn_DEF_PrintableString_tags)
/ sizeof(asn_DEF_PrintableString_tags[0]) - 1,
asn_DEF_PrintableString_tags,
sizeof(asn_DEF_PrintableString_tags)
/ sizeof(asn_DEF_PrintableString_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
&asn_DEF_PrintableString_per_constraints,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
PrintableString_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
int
PrintableString_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb,

View File

@ -16,19 +16,34 @@ typedef OCTET_STRING_t PrintableString_t; /* Implemented via OCTET STRING */
extern asn_TYPE_descriptor_t asn_DEF_PrintableString;
extern asn_TYPE_operation_t asn_OP_PrintableString;
#define PrintableString_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define PrintableString_print OCTET_STRING_print_utf8
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define PrintableString_compare OCTET_STRING_compare
asn_constr_check_f PrintableString_constraint;
#define PrintableString_free OCTET_STRING_free
#define PrintableString_print OCTET_STRING_print_utf8
#define PrintableString_compare OCTET_STRING_compare
#define PrintableString_decode_ber OCTET_STRING_decode_ber
#define PrintableString_encode_der OCTET_STRING_encode_der
#define PrintableString_decode_xer OCTET_STRING_decode_xer_utf8
#define PrintableString_encode_xer OCTET_STRING_encode_xer_utf8
#define PrintableString_decode_uper OCTET_STRING_decode_uper
#define PrintableString_encode_uper OCTET_STRING_encode_uper
#define PrintableString_decode_aper OCTET_STRING_decode_aper
#define PrintableString_encode_aper OCTET_STRING_encode_aper
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define PrintableString_decode_ber OCTET_STRING_decode_ber
#define PrintableString_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define PrintableString_decode_xer OCTET_STRING_decode_xer_utf8
#define PrintableString_encode_xer OCTET_STRING_encode_xer_utf8
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define PrintableString_decode_uper OCTET_STRING_decode_uper
#define PrintableString_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define PrintableString_decode_aper OCTET_STRING_decode_aper
#define PrintableString_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -0,0 +1,219 @@
/*-
* Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <UTF8String.h>
/*
* UTF8String basic type description.
*/
static const ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
};
asn_TYPE_operation_t asn_OP_UTF8String = {
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
UTF8String_print,
#else
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
UTF8String_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_UTF8String = {
"UTF8String",
"UTF8String",
&asn_OP_UTF8String,
asn_DEF_UTF8String_tags,
sizeof(asn_DEF_UTF8String_tags)
/ sizeof(asn_DEF_UTF8String_tags[0]) - 1,
asn_DEF_UTF8String_tags,
sizeof(asn_DEF_UTF8String_tags)
/ sizeof(asn_DEF_UTF8String_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
UTF8String_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
/*
* This is the table of length expectations.
* The second half of this table is only applicable to the long sequences.
*/
static const int UTF8String_ht[2][16] = {
{ /* 0x0 ... 0x7 */
/* 0000..0111 */
1, 1, 1, 1, 1, 1, 1, 1,
/* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
0, 0, 0, 0, 2, 2, 3, -1 },
{ /* 0xF0 .. 0xF7 */
/* 11110000..11110111 */
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, -1, -1 }
};
static const int32_t UTF8String_mv[7] = { 0, 0,
0x00000080,
0x00000800,
0x00010000,
0x00200000,
0x04000000
};
/* Internal aliases for return codes */
#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
#define U8E_NOTCONT -3 /* Continuation expectation failed */
#define U8E_NOTMIN -4 /* Not minimal length encoding */
#define U8E_EINVAL -5 /* Invalid arguments */
int
UTF8String_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
switch(len) {
case U8E_EINVAL:
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given", td->name);
break;
case U8E_TRUNC:
ASN__CTFAIL(app_key, td, sptr,
"%s: truncated UTF-8 sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_ILLSTART:
ASN__CTFAIL(app_key, td, sptr,
"%s: UTF-8 illegal start of encoding (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTCONT:
ASN__CTFAIL(app_key, td, sptr,
"%s: UTF-8 not continuation (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTMIN:
ASN__CTFAIL(app_key, td, sptr,
"%s: UTF-8 not minimal sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;
}
return (len < 0) ? -1 : 0;
}
static ssize_t
UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
size_t length;
uint8_t *buf = st->buf;
uint8_t *end = buf + st->size;
uint32_t *dstend = dst + dstlen;
for(length = 0; buf < end; length++) {
int ch = *buf;
uint8_t *cend;
int32_t value;
int want;
/* Compute the sequence length */
want = UTF8String_ht[0][ch >> 4];
switch(want) {
case -1:
/* Second half of the table, long sequence */
want = UTF8String_ht[1][ch & 0x0F];
if(want != -1) break;
/* Fall through */
case 0:
return U8E_ILLSTART;
}
/* assert(want >= 1 && want <= 6) */
/* Check character sequence length */
if(buf + want > end) return U8E_TRUNC;
value = ch & (0xff >> want);
cend = buf + want;
for(buf++; buf < cend; buf++) {
ch = *buf;
if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
value = (value << 6) | (ch & 0x3F);
}
if(value < UTF8String_mv[want])
return U8E_NOTMIN;
if(dst < dstend)
*dst++ = value; /* Record value */
}
if(dst < dstend) *dst = 0; /* zero-terminate */
return length;
}
ssize_t
UTF8String_length(const UTF8String_t *st) {
if(st && st->buf) {
return UTF8String__process(st, 0, 0);
} else {
return U8E_EINVAL;
}
}
size_t
UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
if(st && st->buf) {
ssize_t ret = UTF8String__process(st, dst, dstlen);
return (ret < 0) ? 0 : ret;
} else {
return 0;
}
}

View File

@ -0,0 +1,80 @@
/*-
* Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _UTF8String_H_
#define _UTF8String_H_
#include <OCTET_STRING.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef OCTET_STRING_t UTF8String_t; /* Implemented via OCTET STRING */
extern asn_TYPE_descriptor_t asn_DEF_UTF8String;
extern asn_TYPE_operation_t asn_OP_UTF8String;
#define UTF8String_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f UTF8String_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define UTF8String_compare OCTET_STRING_compare
asn_constr_check_f UTF8String_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define UTF8String_decode_ber OCTET_STRING_decode_ber
#define UTF8String_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define UTF8String_decode_xer OCTET_STRING_decode_xer_utf8
#define UTF8String_encode_xer OCTET_STRING_encode_xer_utf8
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define UTF8String_decode_uper OCTET_STRING_decode_uper
#define UTF8String_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define UTF8String_decode_aper OCTET_STRING_decode_aper
#define UTF8String_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f UTF8String_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
/*
* Returns length of the given UTF-8 string in characters,
* or a negative error code:
* -1: UTF-8 sequence truncated
* -2: Illegal UTF-8 sequence start
* -3: Continuation expectation failed
* -4: Not minimal length encoding
* -5: Invalid arguments
*/
ssize_t UTF8String_length(const UTF8String_t *st);
/*
* Convert the UTF-8 string into a sequence of wide characters.
* Returns the number of characters necessary.
* Returned value might be greater than dstlen.
* In case of conversion error, 0 is returned.
*
* If st points to a valid UTF-8 string, calling
* UTF8String_to_wcs(st, 0, 0);
* is equivalent to
* UTF8String_length(const UTF8String_t *st);
*/
size_t UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen);
#ifdef __cplusplus
}
#endif
#endif /* _UTF8String_H_ */

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <UTF8String.h>
int
UTF8String_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
const UTF8String_t *st = (const UTF8String_t *)sptr;
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
if(st && st->buf) {
return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <UTF8String.h>
/*
* Biased function for randomizing UTF-8 sequences.
*/
static size_t
UTF8String__random_char(uint8_t *b, size_t size) {
static const struct rnd_value {
const char *value;
size_t size;
} values[] = {{"\0", 1},
{"\x01", 1},
{"\x7f", 1},
{"\xc2\xa2", 2},
{"\xe2\x82\xac", 3},
{"\xf0\x90\x8d\x88", 4},
{"\xf4\x8f\xbf\xbf", 4}};
const struct rnd_value *v;
size_t max_idx = 0;
switch(size) {
case 0:
assert(size != 0);
return 0;
case 1:
max_idx = 2;
break;
case 2:
max_idx = 3;
break;
default:
case 4:
max_idx = sizeof(values) / sizeof(values[0]) - 1;
break;
}
v = &values[asn_random_between(0, max_idx)];
memcpy(b, v->value, v->size);
return v->size;
}
asn_random_fill_result_t
UTF8String_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
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};
uint8_t *buf;
uint8_t *bend;
uint8_t *b;
size_t rnd_len;
size_t idx;
UTF8String_t *st;
if(max_length == 0 && !*sptr) return result_skipped;
/* Figure out how far we should go */
rnd_len = OCTET_STRING_random_length_constrained(td, constraints,
max_length / 4);
buf = CALLOC(4, rnd_len + 1);
if(!buf) return result_failed;
bend = &buf[4 * rnd_len];
for(b = buf, idx = 0; idx < rnd_len; idx++) {
b += UTF8String__random_char(b, (bend - b));
}
*(uint8_t *)b = 0;
if(*sptr) {
st = *sptr;
FREEMEM(st->buf);
} else {
st = (OCTET_STRING_t *)(*sptr = CALLOC(1, sizeof(UTF8String_t)));
if(!st) {
FREEMEM(buf);
return result_failed;
}
}
st->buf = buf;
st->size = b - buf;
assert(UTF8String_length(st) == (ssize_t)rnd_len);
return result_ok;
}

View File

@ -0,0 +1,130 @@
/*-
* Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <VisibleString.h>
/*
* VisibleString basic type description.
*/
static const ber_tlv_tag_t asn_DEF_VisibleString_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
};
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
static asn_per_constraints_t asn_DEF_VisibleString_constraints = {
{ APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
0, 0
};
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_TYPE_operation_t asn_OP_VisibleString = {
OCTET_STRING_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
OCTET_STRING_print_utf8, /* ASCII subset */
#else
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
OCTET_STRING_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
OCTET_STRING_decode_oer,
OCTET_STRING_encode_oer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
OCTET_STRING_decode_aper,
OCTET_STRING_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
OCTET_STRING_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_VisibleString = {
"VisibleString",
"VisibleString",
&asn_OP_VisibleString,
asn_DEF_VisibleString_tags,
sizeof(asn_DEF_VisibleString_tags)
/ sizeof(asn_DEF_VisibleString_tags[0]) - 1,
asn_DEF_VisibleString_tags,
sizeof(asn_DEF_VisibleString_tags)
/ sizeof(asn_DEF_VisibleString_tags[0]),
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
&asn_DEF_VisibleString_constraints,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
VisibleString_constraint
},
0, 0, /* No members */
0 /* No specifics */
};
int
VisibleString_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const VisibleString_t *st = (const VisibleString_t *)sptr;
if(st && st->buf) {
uint8_t *buf = st->buf;
uint8_t *end = buf + st->size;
/*
* Check the alphabet of the VisibleString.
* ISO646, ISOReg#6
* The alphabet is a subset of ASCII between the space
* and "~" (tilde).
*/
for(; buf < end; buf++) {
if(*buf < 0x20 || *buf > 0x7e) {
ASN__CTFAIL(app_key, td, sptr,
"%s: value byte %ld (%d) "
"not in VisibleString alphabet (%s:%d)",
td->name,
(long)((buf - st->buf) + 1),
*buf,
__FILE__, __LINE__);
return -1;
}
}
} else {
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
return 0;
}

View File

@ -0,0 +1,52 @@
/*-
* Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _VisibleString_H_
#define _VisibleString_H_
#include <OCTET_STRING.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef OCTET_STRING_t VisibleString_t; /* Implemented via OCTET STRING */
extern asn_TYPE_descriptor_t asn_DEF_VisibleString;
extern asn_TYPE_operation_t asn_OP_VisibleString;
#define VisibleString_free OCTET_STRING_free
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define VisibleString_print OCTET_STRING_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
#define VisibleString_compare OCTET_STRING_compare
asn_constr_check_f VisibleString_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define VisibleString_decode_ber OCTET_STRING_decode_ber
#define VisibleString_encode_der OCTET_STRING_encode_der
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define VisibleString_decode_xer OCTET_STRING_decode_xer_hex
#define VisibleString_encode_xer OCTET_STRING_encode_xer
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define VisibleString_decode_uper OCTET_STRING_decode_uper
#define VisibleString_encode_uper OCTET_STRING_encode_uper
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define VisibleString_decode_aper OCTET_STRING_decode_aper
#define VisibleString_encode_aper OCTET_STRING_encode_aper
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#ifdef __cplusplus
}
#endif
#endif /* _VisibleString_H_ */

View File

@ -0,0 +1,91 @@
#include <asn_application.h>
#include <asn_internal.h>
#include <aper_decoder.h>
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 <limits.h> */
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;
}

View File

@ -0,0 +1,47 @@
/*-
* Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _APER_DECODER_H_
#define _APER_DECODER_H_
#include <asn_application.h>
#include <aper_support.h>
#ifdef __cplusplus
extern "C" {
#endif
struct asn_TYPE_descriptor_s; /* Forward declaration */
/*
* 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 */
);
#ifdef __cplusplus
}
#endif
#endif /* _APER_DECODER_H_ */

View File

@ -0,0 +1,129 @@
#include <asn_application.h>
#include <asn_internal.h>
#include <aper_encoder.h>
/*
* Argument type and callback necessary for aper_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;
}
asn_enc_rval_t
aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void *buffer, size_t buffer_size) {
enc_to_buf_arg key;
key.buffer = buffer;
key.left = buffer_size;
if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
}
ssize_t
aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void **buffer_r) {
asn_enc_rval_t er = {0,0,0};
enc_dyn_arg key;
memset(&key, 0, sizeof(key));
er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
switch(er.encoded) {
case -1:
FREEMEM(key.buffer);
return -1;
case 0:
FREEMEM(key.buffer);
key.buffer = MALLOC(1);
if(key.buffer) {
*(char *)key.buffer = '\0';
*buffer_r = key.buffer;
return 1;
} else {
return -1;
}
default:
*buffer_r = key.buffer;
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
return ((er.encoded + 7) >> 3);
}
}
/*
* Internally useful functions.
*/
/* Flush partially filled buffer */
static int
_aper_encode_flush_outp(asn_per_outp_t *po) {
uint8_t *buf;
if(po->nboff == 0 && po->buffer == po->tmpspace)
return 0;
buf = po->buffer + (po->nboff >> 3);
/* Make sure we account for the last, partially filled */
if(po->nboff & 0x07) {
buf[0] &= 0xff << (8 - (po->nboff & 0x07));
buf++;
}
if (po->output) {
return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
}
return 0;
}
asn_enc_rval_t
aper_encode(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
asn_per_outp_t po;
asn_enc_rval_t er = {0,0,0};
/*
* Invoke type-specific encoder.
*/
if(!td || !td->op->aper_encoder)
ASN__ENCODE_FAILED; /* PER is not compiled in */
po.buffer = po.tmpspace;
po.nboff = 0;
po.nbits = 8 * sizeof(po.tmpspace);
po.output = cb ? cb : ignore_output;
po.op_key = app_key;
po.flushed_bytes = 0;
er = td->op->aper_encoder(td, constraints, sptr, &po);
if(er.encoded != -1) {
size_t bits_to_flush;
bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
/* Set number of bits encoded to a firm value */
er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
if(_aper_encode_flush_outp(&po))
ASN__ENCODE_FAILED;
}
return er;
}

View File

@ -0,0 +1,63 @@
/*-
* Copyright (c) 2006-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _APER_ENCODER_H_
#define _APER_ENCODER_H_
#include <asn_application.h>
#include <aper_support.h>
#ifdef __cplusplus
extern "C" {
#endif
struct asn_TYPE_descriptor_s; /* Forward declaration */
/*
* Aligned PER encoder of any ASN.1 type. May be invoked by the application.
* WARNING: This function returns the number of encoded bits in the .encoded
* field of the return value. Use the following formula to convert to bytes:
* bytes = ((.encoded + 7) / 8)
*/
asn_enc_rval_t aper_encode(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
asn_app_consume_bytes_f *consume_bytes_cb, /* Data collector */
void *app_key /* Arbitrary callback argument */
);
/*
* A variant of aper_encode() which encodes data into the existing buffer
* WARNING: This function returns the number of encoded bits in the .encoded
* field of the return value.
*/
asn_enc_rval_t aper_encode_to_buffer(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
void *buffer, /* Pre-allocated buffer */
size_t buffer_size /* Initial buffer size (max) */
);
/*
* A variant of aper_encode_to_buffer() which allocates buffer itself.
* Returns the number of bytes in the buffer or -1 in case of failure.
* WARNING: This function produces a "Production of the complete encoding",
* with length of at least one octet. Contrast this to precise bit-packing
* encoding of aper_encode() and aper_encode_to_buffer().
*/
ssize_t
aper_encode_to_new_buffer(
const struct asn_TYPE_descriptor_s *td,
const asn_per_constraints_t *constraints,
const void *sptr,
void **buffer_r
);
#ifdef __cplusplus
}
#endif
#endif /* _APER_ENCODER_H_ */

View File

@ -0,0 +1,149 @@
/*
* Copyright (c) 2007 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <aper_encoder.h>
#include <aper_support.h>
#include <aper_opentype.h>
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;) {
int need_eom = 0;
ssize_t maySave = aper_put_length(po, -1, toGo, &need_eom);
if(maySave < 0) break;
if(per_put_many_bits(po, bptr, maySave * 8)) break;
bptr = (char *)bptr + maySave;
toGo -= maySave;
if(need_eom && aper_put_length(po, -1, 0, 0)) {
FREEMEM(buf);
return -1;
}
}
FREEMEM(buf);
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 = "<unknown extension>";
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;
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2007-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _APER_OPENTYPE_H_
#define _APER_OPENTYPE_H_
#include <per_opentype.h>
#ifdef __cplusplus
extern "C" {
#endif
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 /* _APER_OPENTYPE_H_ */

View File

@ -0,0 +1,279 @@
/*
* Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_system.h>
#include <asn_internal.h>
#include <aper_support.h>
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 */
}
}
#if !defined(USE_OLDER_APER_NSNNWN)
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;
int length;
/* handle indefinite range */
length = per_get_few_bits(pd, 1);
if (length == 0)
return per_get_few_bits(pd, 6);
if (aper_get_align(pd) < 0)
return -1;
length = per_get_few_bits(pd, 8);
/* the length is not likely to be that big */
if (length > 4)
return -1;
value = 0;
if (per_get_many_bits(pd, (uint8_t *)&value, 0, length * 8) < 0)
return -1;
return value;
}
if (aper_get_align(pd) < 0)
return -1;
value = per_get_few_bits(pd, 8 * bytes);
return value;
}
#else /* old APER codec */
ssize_t
aper_get_nsnnwn(asn_per_data_t *pd, int dummy_range) {
ssize_t value;
ASN_DEBUG("Get the normally small non-negative whole number APER");
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;
}
#endif /* don't use old APER */
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, int *need_eom) {
int dummy = 0;
if(!need_eom) need_eom = &dummy;
*need_eom = 0;
ASN_DEBUG("APER put length %zu with range %d", length, range);
/* 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;
*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);
}
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, 0) != (ssize_t)length) {
/* This might happen in case of >16K extensions */
return -1;
}
}
return 0;
}
#if !defined(USE_OLDER_APER_NSNNWN)
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);
}
#else /* preserve old code base in case */
int
aper_put_nsnnwn(asn_per_outp_t *po, int dummy_range, int n) {
int bytes;
ASN_DEBUG("aper_put_nsnnwn");
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);
}
#endif /* which aper_put_nsnnwn() */

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _APER_SUPPORT_H_
#define _APER_SUPPORT_H_
#include <asn_system.h> /* Platform-specific types */
#include <per_support.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
* Get the length "n" from the Aligned PER stream.
*/
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 aper_get_nslength(asn_per_data_t *pd);
/*
* Get the normally small non-negative whole number.
*/
ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range);
/*
* X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
* Put the length "whole_length" to the Aligned 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 aper_put_length(asn_per_outp_t *po, int range, size_t length,
int *opt_need_eom);
/* Align the current bit position to octet bundary */
int aper_put_align(asn_per_outp_t *po);
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 aper_put_nslength(asn_per_outp_t *po, size_t length);
/*
* Put the normally small non-negative whole number.
*/
int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number);
#ifdef __cplusplus
}
#endif
#endif /* _APER_SUPPORT_H_ */

View File

@ -226,7 +226,9 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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};
#if !defined(ASN_DISABLE_XER_SUPPORT)
enum xer_encoder_flags_e xer_flags = XER_F_CANONICAL;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
(void)opt_codec_ctx; /* Parameters are not checked on encode yet. */
@ -265,6 +267,7 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
errno = ENOENT; /* Randomization doesn't make sense on output. */
ASN__ENCODE_FAILED;
#if !defined(ASN_DISABLE_BER_SUPPORT)
case ATS_BER:
/* BER is a superset of DER. */
/* Fall through. */
@ -273,27 +276,29 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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. */
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* DER is not defined for this type. */
errno = ENOENT; /* DER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
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. */
errno = ENOENT; /* Transfer syntax is not defined for any type. */
ASN__ENCODE_FAILED;
#else
case ATS_BER:
case ATS_DER:
case ATS_CER:
errno = ENOENT; /* BER is not defined. */
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 */
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
case ATS_BASIC_OER:
/* CANONICAL-OER is a superset of BASIC-OER. */
/* Fall through. */
@ -302,27 +307,25 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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. */
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* OER is not defined for this type. */
errno = ENOENT; /* OER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
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. */
#else
case ATS_BASIC_OER:
case ATS_CANONICAL_OER:
errno = ENOENT; /* OER is not defined. */
ASN__ENCODE_FAILED;
break;
#else /* ASN_DISABLE_PER_SUPPORT */
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
case ATS_UNALIGNED_BASIC_PER:
/* CANONICAL-UPER is a superset of BASIC-UPER. */
/* Fall through. */
@ -331,9 +334,9 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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. */
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* UPER is not defined for this type. */
errno = ENOENT; /* UPER is not defined for this type. */
}
} else {
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
@ -343,16 +346,24 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
errno = EBADF;
ASN__ENCODE_FAILED;
}
er.encoded = 8; /* Exactly 8 zero bits is added. */
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. */
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
#else
case ATS_UNALIGNED_BASIC_PER:
case ATS_UNALIGNED_CANONICAL_PER:
errno = ENOENT; /* UPER is not defined. */
ASN__ENCODE_FAILED;
break;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
case ATS_ALIGNED_BASIC_PER:
/* CANONICAL-APER is a superset of BASIC-APER. */
/* Fall through. */
@ -361,9 +372,9 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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. */
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* APER is not defined for this type. */
errno = ENOENT; /* APER is not defined for this type. */
}
} else {
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
@ -373,18 +384,25 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
errno = EBADF;
ASN__ENCODE_FAILED;
}
er.encoded = 8; /* Exactly 8 zero bits is added. */
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. */
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
#endif /* ASN_DISABLE_PER_SUPPORT */
#else
case ATS_ALIGNED_BASIC_PER:
case ATS_ALIGNED_CANONICAL_PER:
errno = ENOENT; /* APER is not defined. */
ASN__ENCODE_FAILED;
break;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
case ATS_BASIC_XER:
/* CANONICAL-XER is a superset of BASIC-XER. */
xer_flags &= ~XER_F_CANONICAL;
@ -395,16 +413,23 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
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. */
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* XER is not defined for this type. */
errno = ENOENT; /* XER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
#else
case ATS_BASIC_XER:
case ATS_CANONICAL_XER:
errno = ENOENT; /* XER is not defined. */
ASN__ENCODE_FAILED;
break;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
default:
errno = ENOENT;
@ -430,6 +455,7 @@ asn_decode(const asn_codec_ctx_t *opt_codec_ctx,
ASN__DECODE_FAILED;
case ATS_RANDOM:
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
if(!td->op->random_fill) {
ASN__DECODE_FAILED;
} else {
@ -441,41 +467,55 @@ asn_decode(const asn_codec_ctx_t *opt_codec_ctx,
}
}
break;
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
case ATS_DER:
case ATS_BER:
#if !defined(ASN_DISABLE_BER_SUPPORT)
return ber_decode(opt_codec_ctx, td, sptr, buffer, size);
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
case ATS_BASIC_OER:
case ATS_CANONICAL_OER:
#ifdef ASN_DISABLE_OER_SUPPORT
#if !defined(ASN_DISABLE_OER_SUPPORT)
return oer_decode(opt_codec_ctx, td, sptr, buffer, size);
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#else
return oer_decode(opt_codec_ctx, td, sptr, buffer, size);
#endif
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
case ATS_UNALIGNED_BASIC_PER:
case ATS_UNALIGNED_CANONICAL_PER:
#ifdef ASN_DISABLE_PER_SUPPORT
#if !defined(ASN_DISABLE_UPER_SUPPORT)
return uper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#else
return uper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
#endif
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
case ATS_ALIGNED_BASIC_PER:
case ATS_ALIGNED_CANONICAL_PER:
#ifdef ASN_DISABLE_PER_SUPPORT
#if !defined(ASN_DISABLE_APER_SUPPORT)
return aper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#else
return aper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
#endif
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
case ATS_BASIC_XER:
case ATS_CANONICAL_XER:
#if !defined(ASN_DISABLE_XER_SUPPORT)
return xer_decode(opt_codec_ctx, td, sptr, buffer, size);
#else
errno = ENOENT;
ASN__DECODE_FAILED;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
}
}

View File

@ -10,6 +10,7 @@
#include "asn_system.h" /* for platform-dependent types */
#include "asn_codecs.h" /* for ASN.1 codecs specifics */
#include "asn_config.h"
#ifdef __cplusplus
extern "C" {

View File

@ -4,115 +4,6 @@
*/
#include <asn_internal.h>
#include <asn_codecs_prim.h>
#include <errno.h>
/*
* 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,
@ -138,180 +29,3 @@ ASN__PRIMITIVE_TYPE_free(const asn_TYPE_descriptor_t *td, void *sptr,
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 (<MINUS-INFINITY>, <enum-element>, 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:
* "<INTEGER>123<!--/--> </INTEGER>"
* ^- 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:
* "1<tag_start..."
*/
arg->want_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;
}

View File

@ -12,23 +12,27 @@ 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! */
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;
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f ber_decode_primitive;
der_type_encoder_f der_encode_primitive;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
/*
* 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 */
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,
@ -43,6 +47,7 @@ asn_dec_rval_t xer_decode_primitive(
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);
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#ifdef __cplusplus
}

View File

@ -0,0 +1,6 @@
// Generated automatically. Don't edit manually!
#define ASN_DISABLE_BER_SUPPORT 1
#define ASN_DISABLE_XER_SUPPORT 1
#define ASN_DISABLE_OER_SUPPORT 1
#define ASN_DISABLE_UPER_SUPPORT 1

View File

@ -21,6 +21,15 @@
extern "C" {
#endif
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#include <uper_decoder.h>
#include <uper_encoder.h>
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#include <aper_decoder.h>
#include <aper_encoder.h>
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
/* 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 */
@ -136,6 +145,13 @@ asn__format_to_callback(
* Check stack against overflow, if limit is set.
*/
#define ASN__DEFAULT_STACK_MAX (30000)
#ifdef ASN_DISABLE_STACK_OVERFLOW_CHECK
static int CC_NOTUSED
ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
(void)ctx;
return 0;
}
#else
static int CC_NOTUSED
ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
if(ctx && ctx->max_stack_size) {
@ -153,6 +169,7 @@ ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
}
return 0;
}
#endif
#ifdef __cplusplus
}

View File

@ -43,12 +43,17 @@ asn_random_between(intmax_t lb, intmax_t rb) {
uintmax_t value = 0;
uintmax_t got_entropy = 0;
(void)intmax_max;
assert(RAND_MAX > 0xffffff); /* Seen 7ffffffd! */
assert(range < intmax_max);
for(; got_entropy < range;) {
got_entropy = (got_entropy << 24) | 0xffffff;
#ifdef HAVE_RANDOM
value = (value << 24) | (random() % 0xffffff);
#else
value = (value << 24) | (rand() % 0xffffff);
#endif
}
return lb + (intmax_t)(value % (range + 1));

View File

@ -75,7 +75,9 @@ typedef unsigned int uint32_t;
#else /* !defined(__vxworks) */
#include <inttypes.h> /* C99 specifies this file */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> /* for ntohl() */
#endif
#define sys_ntohl(foo) ntohl(foo)
#endif /* defined(__vxworks) */
@ -86,11 +88,25 @@ typedef unsigned int uint32_t;
#else
#define CC_ATTRIBUTE(attr)
#endif
#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var))
#if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(gnu_printf, fmt, var))
#elif defined(__GNUC__)
#if defined(ANDROID)
#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(__format__(__printf__, fmt, var))
#else
#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var))
#endif
#else
#define CC_PRINTFLIKE(fmt, var)
#endif
#define CC_NOTUSED CC_ATTRIBUTE(unused)
#ifndef CC_ATTR_NO_SANITIZE
#if __GNUC__ < 8
#define CC_ATTR_NO_SANITIZE(what)
#else
#define CC_ATTR_NO_SANITIZE(what) CC_ATTRIBUTE(no_sanitize(what))
#endif
#endif
/* Figure out if thread safety is requested */
#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))

File diff suppressed because it is too large Load Diff

View File

@ -40,23 +40,58 @@ typedef struct asn_CHOICE_specifics_s {
* A set specialized functions dealing with the CHOICE type.
*/
asn_struct_free_f CHOICE_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f CHOICE_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f CHOICE_compare;
asn_constr_check_f CHOICE_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f CHOICE_decode_ber;
der_type_encoder_f CHOICE_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f CHOICE_decode_xer;
xer_type_encoder_f CHOICE_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f CHOICE_decode_oer;
oer_type_encoder_f CHOICE_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f CHOICE_decode_uper;
per_type_encoder_f CHOICE_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f CHOICE_decode_aper;
per_type_encoder_f CHOICE_encode_aper;
asn_outmost_tag_f CHOICE_outmost_tag;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f CHOICE_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
asn_outmost_tag_f CHOICE_outmost_tag;
extern asn_TYPE_operation_t asn_OP_CHOICE;
unsigned _fetch_present_idx(
const void *struct_ptr,
unsigned off,
unsigned size);
void _set_present_idx(
void *sptr,
unsigned offset,
unsigned size,
unsigned present);
/*
* Return the 1-based choice variant presence index.
* Returns 0 in case of error.

View File

@ -0,0 +1,178 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_CHOICE.h>
#include <aper_opentype.h>
asn_dec_rval_t
CHOICE_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_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
asn_dec_rval_t rv;
const asn_per_constraint_t *ct;
asn_TYPE_member_t *elm; /* CHOICE's element */
void *memb_ptr;
void **memb_ptr2;
void *st = *sptr;
int value;
if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
ASN__DECODE_FAILED;
/*
* Create the target structure if it is not present already.
*/
if(!st) {
st = *sptr = CALLOC(1, specs->struct_size);
if(!st) ASN__DECODE_FAILED;
}
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ct = 0;
if(ct && ct->flags & APC_EXTENSIBLE) {
value = per_get_few_bits(pd, 1);
if(value < 0) ASN__DECODE_STARVED;
if(value) ct = 0; /* Not restricted */
}
if(ct && ct->range_bits >= 0) {
value = per_get_few_bits(pd, ct->range_bits);
if(value < 0) ASN__DECODE_STARVED;
ASN_DEBUG("CHOICE %s got index %d in range %d",
td->name, value, ct->range_bits);
if(value > ct->upper_bound)
ASN__DECODE_FAILED;
} else {
if(specs->ext_start == -1)
ASN__DECODE_FAILED;
value = aper_get_nsnnwn(pd, ct->upper_bound - ct->lower_bound + 1);
if(value < 0) ASN__DECODE_STARVED;
value += specs->ext_start;
if((unsigned)value >= td->elements_count)
ASN__DECODE_FAILED;
}
/* Adjust if canonical order is different from natural order */
if(specs->from_canonical_order)
value = specs->from_canonical_order[value];
/* Set presence to be able to free it later */
_set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
elm = &td->elements[value];
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
memb_ptr = (char *)st + elm->memb_offset;
memb_ptr2 = &memb_ptr;
}
ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
if(ct && ct->range_bits >= 0) {
rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
elm->encoding_constraints.per_constraints, memb_ptr2, pd);
} else {
rv = aper_open_type_get(opt_codec_ctx, elm->type,
elm->encoding_constraints.per_constraints, memb_ptr2, pd);
}
if(rv.code != RC_OK)
ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
elm->name, td->name, rv.code);
return rv;
}
asn_enc_rval_t
CHOICE_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
const asn_TYPE_member_t *elm; /* CHOICE's element */
const asn_per_constraint_t *ct;
const void *memb_ptr;
int present;
if(!sptr) ASN__ENCODE_FAILED;
ASN_DEBUG("Encoding %s as CHOICE using ALIGNED PER", td->name);
if(constraints) ct = &constraints->value;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->value;
else ct = 0;
present = _fetch_present_idx(sptr,
specs->pres_offset, specs->pres_size);
/*
* If the structure was not initialized properly, it cannot be encoded:
* can't deduce what to encode in the choice type.
*/
if(present <= 0 || (unsigned)present > td->elements_count)
ASN__ENCODE_FAILED;
else
present--;
/* Adjust if canonical order is different from natural order */
if(specs->to_canonical_order)
present = specs->to_canonical_order[present];
ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
if(ct && ct->range_bits >= 0) {
if(present < ct->lower_bound
|| present > ct->upper_bound) {
if(ct->flags & APC_EXTENSIBLE) {
if(per_put_few_bits(po, 1, 1))
ASN__ENCODE_FAILED;
} else {
ASN__ENCODE_FAILED;
}
ct = 0;
}
}
if(ct && ct->flags & APC_EXTENSIBLE) {
if(per_put_few_bits(po, 0, 1))
ASN__ENCODE_FAILED;
}
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(ct && ct->range_bits >= 0) {
if(per_put_few_bits(po, present, ct->range_bits))
ASN__ENCODE_FAILED;
return elm->type->op->aper_encoder(elm->type, elm->encoding_constraints.per_constraints,
memb_ptr, po);
} else {
asn_enc_rval_t rval = {0,0,0};
if(specs->ext_start == -1)
ASN__ENCODE_FAILED;
int n = present - specs->ext_start;
if(n <= 63) {
if(n < 0) ASN__ENCODE_FAILED;
if(per_put_few_bits(po, n, 7)) ASN__ENCODE_FAILED;
} else
ASN__ENCODE_FAILED;
if(aper_open_type_put(elm->type, elm->encoding_constraints.per_constraints,
memb_ptr, po))
ASN__ENCODE_FAILED;
rval.encoded = 0;
ASN__ENCODED_OK(rval);
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_CHOICE.h>
int
CHOICE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
unsigned present;
if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
/*
* Print that element.
*/
if(present > 0 && present <= td->elements_count) {
asn_TYPE_member_t *elm = &td->elements[present-1];
const void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
}
/* Print member's name and stuff */
if(0) {
if(cb(elm->name, strlen(elm->name), app_key) < 0
|| cb(": ", 2, app_key) < 0)
return -1;
}
return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
cb, app_key);
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_CHOICE.h>
asn_random_fill_result_t
CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constr,
size_t max_length) {
const asn_CHOICE_specifics_t *specs =
(const asn_CHOICE_specifics_t *)td->specifics;
asn_random_fill_result_t res;
asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
const asn_TYPE_member_t *elm;
unsigned present;
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
void *st = *sptr;
if(max_length == 0) return result_skipped;
(void)constr;
if(st == NULL) {
st = CALLOC(1, specs->struct_size);
if(st == NULL) {
return result_failed;
}
}
present = asn_random_between(1, td->elements_count);
elm = &td->elements[present - 1];
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
memb_ptr = (char *)st + elm->memb_offset;
memb_ptr2 = &memb_ptr;
}
res = elm->type->op->random_fill(elm->type, memb_ptr2,
&elm->encoding_constraints, max_length);
_set_present_idx(st, specs->pres_offset, specs->pres_size, present);
if(res.code == ARFILL_OK) {
*sptr = st;
} else {
if(st == *sptr) {
ASN_STRUCT_RESET(*td, st);
} else {
ASN_STRUCT_FREE(*td, st);
}
}
return res;
}

File diff suppressed because it is too large Load Diff

View File

@ -45,20 +45,43 @@ typedef struct asn_SEQUENCE_specifics_s {
* A set specialized functions dealing with the SEQUENCE type.
*/
asn_struct_free_f SEQUENCE_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f SEQUENCE_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f SEQUENCE_compare;
asn_constr_check_f SEQUENCE_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f SEQUENCE_decode_ber;
der_type_encoder_f SEQUENCE_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f SEQUENCE_decode_xer;
xer_type_encoder_f SEQUENCE_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f SEQUENCE_decode_oer;
oer_type_encoder_f SEQUENCE_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f SEQUENCE_decode_uper;
per_type_encoder_f SEQUENCE_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f SEQUENCE_decode_aper;
per_type_encoder_f SEQUENCE_encode_aper;
asn_random_fill_f SEQUENCE_random_fill;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f SEQUENCE_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
extern asn_TYPE_operation_t asn_OP_SEQUENCE;
#ifdef __cplusplus

View File

@ -7,301 +7,56 @@
#include <constr_SEQUENCE_OF.h>
#include <asn_SEQUENCE_OF.h>
/*
* The DER encoder of the SEQUENCE OF type.
*/
asn_enc_rval_t
SEQUENCE_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr,
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_TYPE_member_t *elm = td->elements;
const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(ptr);
size_t computed_size = 0;
ssize_t encoding_size = 0;
asn_enc_rval_t erval = {0,0,0};
int edx;
ASN_DEBUG("Estimating size of SEQUENCE OF %s", td->name);
/*
* Gather the length of the underlying members sequence.
*/
for(edx = 0; edx < list->count; edx++) {
void *memb_ptr = list->array[edx];
if(!memb_ptr) continue;
erval = elm->type->op->der_encoder(elm->type, memb_ptr,
0, elm->tag,
0, 0);
if(erval.encoded == -1)
return erval;
computed_size += erval.encoded;
}
/*
* Encode the TLV for the sequence itself.
*/
encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
cb, app_key);
if(encoding_size == -1) {
erval.encoded = -1;
erval.failed_type = td;
erval.structure_ptr = ptr;
return erval;
}
computed_size += encoding_size;
if(!cb) {
erval.encoded = computed_size;
ASN__ENCODED_OK(erval);
}
ASN_DEBUG("Encoding members of SEQUENCE OF %s", td->name);
/*
* Encode all members.
*/
for(edx = 0; edx < list->count; edx++) {
void *memb_ptr = list->array[edx];
if(!memb_ptr) continue;
erval = elm->type->op->der_encoder(elm->type, memb_ptr,
0, elm->tag,
cb, app_key);
if(erval.encoded == -1)
return erval;
encoding_size += erval.encoded;
}
if(computed_size != (size_t)encoding_size) {
/*
* Encoded size is not equal to the computed size.
*/
erval.encoded = -1;
erval.failed_type = td;
erval.structure_ptr = ptr;
} else {
erval.encoded = computed_size;
erval.structure_ptr = 0;
erval.failed_type = 0;
}
return erval;
}
asn_enc_rval_t
SEQUENCE_OF_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) {
asn_enc_rval_t er = {0,0,0};
const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
const asn_TYPE_member_t *elm = td->elements;
const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(sptr);
const char *mname = specs->as_XMLValueList
? 0
: ((*elm->name) ? elm->name : elm->type->xml_tag);
size_t mlen = mname ? strlen(mname) : 0;
int xcan = (flags & XER_F_CANONICAL);
int i;
if(!sptr) ASN__ENCODE_FAILED;
er.encoded = 0;
for(i = 0; i < list->count; i++) {
asn_enc_rval_t tmper = {0,0,0};
void *memb_ptr = list->array[i];
if(!memb_ptr) continue;
if(mname) {
if(!xcan) ASN__TEXT_INDENT(1, ilevel);
ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
}
tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
flags, cb, app_key);
if(tmper.encoded == -1) return tmper;
er.encoded += tmper.encoded;
if(tmper.encoded == 0 && specs->as_XMLValueList) {
const char *name = elm->type->xml_tag;
size_t len = strlen(name);
if(!xcan) ASN__TEXT_INDENT(1, ilevel + 1);
ASN__CALLBACK3("<", 1, name, len, "/>", 2);
}
if(mname) {
ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
}
}
if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
ASN__ENCODED_OK(er);
cb_failed:
ASN__ENCODE_FAILED;
}
#ifndef ASN_DISABLE_PER_SUPPORT
asn_enc_rval_t
SEQUENCE_OF_encode_uper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_anonymous_sequence_ *list;
const asn_per_constraint_t *ct;
asn_enc_rval_t er = {0,0,0};
const asn_TYPE_member_t *elm = td->elements;
size_t encoded_edx;
if(!sptr) ASN__ENCODE_FAILED;
list = _A_CSEQUENCE_FROM_VOID(sptr);
er.encoded = 0;
ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
if(constraints) ct = &constraints->size;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->size;
else ct = 0;
/* If extensible constraint, check if size is in root */
if(ct) {
int not_in_root =
(list->count < ct->lower_bound || list->count > ct->upper_bound);
ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
if(ct->flags & APC_EXTENSIBLE) {
/* Declare whether size is in extension root */
if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
if(not_in_root) ct = 0;
} else if(not_in_root && ct->effective_bits >= 0) {
ASN__ENCODE_FAILED;
}
}
if(ct && ct->effective_bits >= 0) {
/* X.691, #19.5: No length determinant */
if(per_put_few_bits(po, list->count - ct->lower_bound,
ct->effective_bits))
ASN__ENCODE_FAILED;
} else if(list->count == 0) {
/* When the list is empty add only the length determinant
* X.691, #20.6 and #11.9.4.1
*/
if (uper_put_length(po, 0, 0)) {
ASN__ENCODE_FAILED;
}
ASN__ENCODED_OK(er);
}
for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
ssize_t may_encode;
size_t edx;
int need_eom = 0;
if(ct && ct->effective_bits >= 0) {
may_encode = list->count;
} else {
may_encode =
uper_put_length(po, list->count - encoded_edx, &need_eom);
if(may_encode < 0) ASN__ENCODE_FAILED;
}
for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
void *memb_ptr = list->array[edx];
if(!memb_ptr) ASN__ENCODE_FAILED;
er = elm->type->op->uper_encoder(
elm->type, elm->encoding_constraints.per_constraints, memb_ptr,
po);
if(er.encoded == -1) ASN__ENCODE_FAILED;
}
if(need_eom && uper_put_length(po, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
encoded_edx += may_encode;
}
ASN__ENCODED_OK(er);
}
asn_enc_rval_t
SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_anonymous_sequence_ *list;
const asn_per_constraint_t *ct;
asn_enc_rval_t er = {0,0,0};
asn_TYPE_member_t *elm = td->elements;
int seq;
if(!sptr) ASN__ENCODE_FAILED;
list = _A_CSEQUENCE_FROM_VOID(sptr);
er.encoded = 0;
ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
if(constraints) ct = &constraints->size;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->size;
else ct = 0;
/* If extensible constraint, check if size is in root */
if(ct) {
int not_in_root = (list->count < ct->lower_bound
|| list->count > ct->upper_bound);
ASN_DEBUG("lb %ld ub %ld %s",
ct->lower_bound, ct->upper_bound,
ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
if(ct->flags & APC_EXTENSIBLE) {
/* Declare whether size is in extension root */
if(per_put_few_bits(po, not_in_root, 1))
ASN__ENCODE_FAILED;
if(not_in_root) ct = 0;
} else if(not_in_root && ct->effective_bits >= 0)
ASN__ENCODE_FAILED;
}
if(ct && ct->effective_bits >= 0) {
/* X.691, #19.5: No length determinant */
/* if(per_put_few_bits(po, list->count - ct->lower_bound,
ct->effective_bits))
ASN__ENCODE_FAILED;
*/
if (ct->lower_bound == ct->upper_bound && ct->upper_bound < 65536) {
/* No length determinant */
} else if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0)
ASN__ENCODE_FAILED;
}
for(seq = -1; seq < list->count;) {
ssize_t mayEncode;
int need_eom = 0;
if(seq < 0) seq = 0;
if(ct && ct->effective_bits >= 0) {
mayEncode = list->count;
} else {
mayEncode = aper_put_length(po, -1, list->count - seq, &need_eom);
if(mayEncode < 0) ASN__ENCODE_FAILED;
}
while(mayEncode--) {
void *memb_ptr = list->array[seq++];
if(!memb_ptr) ASN__ENCODE_FAILED;
er = elm->type->op->aper_encoder(elm->type,
elm->encoding_constraints.per_constraints, memb_ptr, po);
if(er.encoded == -1)
ASN__ENCODE_FAILED;
}
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
ASN__ENCODED_OK(er);
}
#endif /* ASN_DISABLE_PER_SUPPORT */
asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
SEQUENCE_OF_free,
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
SEQUENCE_OF_print,
#else
0,
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
SEQUENCE_OF_compare,
#if !defined(ASN_DISABLE_BER_SUPPORT)
SEQUENCE_OF_decode_ber,
SEQUENCE_OF_encode_der,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
SEQUENCE_OF_decode_xer,
SEQUENCE_OF_encode_xer,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
SEQUENCE_OF_decode_oer, /* Same as SET OF decoder. */
SEQUENCE_OF_encode_oer, /* Same as SET OF encoder */
#else
0,
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
SEQUENCE_OF_decode_uper, /* Same as SET OF decoder */
SEQUENCE_OF_encode_uper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
SEQUENCE_OF_decode_aper,
SEQUENCE_OF_encode_aper,
#else
0,
0,
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
SEQUENCE_OF_random_fill,
#else
0,
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
0 /* Use generic outmost tag fetcher */
};
int
SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
@ -330,35 +85,3 @@ SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
return 0;
}
asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
SEQUENCE_OF_free,
SEQUENCE_OF_print,
SEQUENCE_OF_compare,
SEQUENCE_OF_decode_ber,
SEQUENCE_OF_encode_der,
SEQUENCE_OF_decode_xer,
SEQUENCE_OF_encode_xer,
#ifdef ASN_DISABLE_OER_SUPPORT
0,
0,
#else
SEQUENCE_OF_decode_oer, /* Same as SET OF decoder. */
SEQUENCE_OF_encode_oer, /* Same as SET OF encoder */
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
#else
SEQUENCE_OF_decode_uper, /* Same as SET OF decoder */
SEQUENCE_OF_encode_uper,
SEQUENCE_OF_decode_aper,
SEQUENCE_OF_encode_aper,
#endif /* ASN_DISABLE_PER_SUPPORT */
SEQUENCE_OF_random_fill,
0 /* Use generic outmost tag fetcher */
};

View File

@ -16,23 +16,45 @@ extern "C" {
* A set specialized functions dealing with the SEQUENCE OF type.
* Generally implemented using SET OF.
*/
asn_struct_compare_f SEQUENCE_OF_compare;
der_type_encoder_f SEQUENCE_OF_encode_der;
xer_type_encoder_f SEQUENCE_OF_encode_xer;
per_type_encoder_f SEQUENCE_OF_encode_uper;
per_type_encoder_f SEQUENCE_OF_encode_aper;
extern asn_TYPE_operation_t asn_OP_SEQUENCE_OF;
#define SEQUENCE_OF_free SET_OF_free
#define SEQUENCE_OF_free SET_OF_free
#define SEQUENCE_OF_print SET_OF_print
#define SEQUENCE_OF_constraint SET_OF_constraint
#define SEQUENCE_OF_decode_ber SET_OF_decode_ber
#define SEQUENCE_OF_decode_xer SET_OF_decode_xer
#define SEQUENCE_OF_decode_oer SET_OF_decode_oer
#define SEQUENCE_OF_encode_oer SET_OF_encode_oer
#define SEQUENCE_OF_decode_uper SET_OF_decode_uper
#define SEQUENCE_OF_decode_aper SET_OF_decode_aper
#define SEQUENCE_OF_random_fill SET_OF_random_fill
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
#define SEQUENCE_OF_print SET_OF_print
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f SEQUENCE_OF_compare;
#define SEQUENCE_OF_constraint SET_OF_constraint
#if !defined(ASN_DISABLE_BER_SUPPORT)
#define SEQUENCE_OF_decode_ber SET_OF_decode_ber
der_type_encoder_f SEQUENCE_OF_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
#define SEQUENCE_OF_decode_xer SET_OF_decode_xer
xer_type_encoder_f SEQUENCE_OF_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
#define SEQUENCE_OF_decode_oer SET_OF_decode_oer
#define SEQUENCE_OF_encode_oer SET_OF_encode_oer
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
#define SEQUENCE_OF_decode_uper SET_OF_decode_uper
per_type_encoder_f SEQUENCE_OF_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
#define SEQUENCE_OF_decode_aper SET_OF_decode_aper
per_type_encoder_f SEQUENCE_OF_encode_aper;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
#define SEQUENCE_OF_random_fill SET_OF_random_fill
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
extern asn_TYPE_operation_t asn_OP_SEQUENCE_OF;
#ifdef __cplusplus
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SEQUENCE_OF.h>
#include <asn_SEQUENCE_OF.h>
asn_enc_rval_t
SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_anonymous_sequence_ *list;
const asn_per_constraint_t *ct;
asn_enc_rval_t er = {0,0,0};
asn_TYPE_member_t *elm = td->elements;
int seq;
if(!sptr) ASN__ENCODE_FAILED;
list = _A_CSEQUENCE_FROM_VOID(sptr);
er.encoded = 0;
ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
if(constraints) ct = &constraints->size;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->size;
else ct = 0;
/* If extensible constraint, check if size is in root */
if(ct) {
int not_in_root = (list->count < ct->lower_bound
|| list->count > ct->upper_bound);
ASN_DEBUG("lb %ld ub %ld %s",
ct->lower_bound, ct->upper_bound,
ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
if(ct->flags & APC_EXTENSIBLE) {
/* Declare whether size is in extension root */
if(per_put_few_bits(po, not_in_root, 1))
ASN__ENCODE_FAILED;
if(not_in_root) ct = 0;
} else if(not_in_root && ct->effective_bits >= 0)
ASN__ENCODE_FAILED;
}
if(ct && ct->effective_bits >= 0) {
/* X.691, #19.5: No length determinant */
/*
if(per_put_few_bits(po, list->count - ct->lower_bound,
ct->effective_bits))
ASN__ENCODE_FAILED;
*/
if (ct->lower_bound == ct->upper_bound && ct->upper_bound < 65536) {
/* No length determinant */
} else if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0)
ASN__ENCODE_FAILED;
}
for(seq = -1; seq < list->count;) {
ssize_t mayEncode;
int need_eom = 0;
if(seq < 0) seq = 0;
if(ct && ct->effective_bits >= 0) {
mayEncode = list->count;
} else {
mayEncode = aper_put_length(po, -1, list->count - seq, &need_eom);
if(mayEncode < 0) ASN__ENCODE_FAILED;
}
while(mayEncode--) {
void *memb_ptr = list->array[seq++];
if(!memb_ptr) ASN__ENCODE_FAILED;
er = elm->type->op->aper_encoder(elm->type,
elm->encoding_constraints.per_constraints,
memb_ptr, po);
if(er.encoded == -1)
ASN__ENCODE_FAILED;
}
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
ASN__ENCODED_OK(er);
}

View File

@ -0,0 +1,452 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SEQUENCE.h>
#include <OPEN_TYPE.h>
#include <aper_opentype.h>
/*
* Check whether we are inside the extensions group.
*/
#define IN_EXTENSION_GROUP(specs, memb_idx) \
((specs)->first_extension >= 0 \
&& (unsigned)(specs)->first_extension <= (memb_idx))
asn_dec_rval_t
SEQUENCE_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_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
void *st = *sptr; /* Target structure. */
int extpresent; /* Extension additions are present */
uint8_t *opres; /* Presence of optional root members */
asn_per_data_t opmd;
asn_dec_rval_t rv;
size_t edx;
(void)constraints;
if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
ASN__DECODE_FAILED;
if(!st) {
st = *sptr = CALLOC(1, specs->struct_size);
if(!st) ASN__DECODE_FAILED;
}
ASN_DEBUG("Decoding %s as SEQUENCE (APER)", td->name);
/* Handle extensions */
if(specs->first_extension < 0) {
extpresent = 0;
} else {
extpresent = per_get_few_bits(pd, 1);
if(extpresent < 0) ASN__DECODE_STARVED;
}
/* Prepare a place and read-in the presence bitmap */
memset(&opmd, 0, sizeof(opmd));
if(specs->roms_count) {
opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
if(!opres) ASN__DECODE_FAILED;
/* Get the presence map */
if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
FREEMEM(opres);
ASN__DECODE_STARVED;
}
opmd.buffer = opres;
opmd.nbits = specs->roms_count;
ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
td->name, specs->roms_count, *opres);
} else {
opres = 0;
}
/*
* Get the sequence ROOT elements.
*/
for(edx = 0; edx < td->elements_count; edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
#if 0
int padding;
#endif
if(IN_EXTENSION_GROUP(specs, edx))
continue;
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
memb_ptr = (char *)st + elm->memb_offset;
memb_ptr2 = &memb_ptr;
}
#if 0
/* Get Padding */
padding = (8 - (pd->moved % 8)) % 8;
if(padding > 0)
ASN_DEBUG("For element %s,offset= %ld Padding bits = %d", td->name, pd->moved, padding);
#if 0 /* old way of removing padding */
per_get_few_bits(pd, padding);
#else /* Experimental fix proposed by @mhanna123 */
if(edx != (td->elements_count-1))
per_get_few_bits(pd, padding);
else {
if(specs->roms_count && (padding > 0))
ASN_DEBUG(">>>>> not skipping padding of %d bits for element:%ld out of %d", padding, edx, td->elements_count);
else
per_get_few_bits(pd, padding);
}
#endif /* dealing with padding */
#endif
/* Deal with optionality */
if(elm->optional) {
int present = per_get_few_bits(&opmd, 1);
ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
td->name, elm->name, present,
(int)opmd.nboff, (int)opmd.nbits);
if(present == 0) {
/* This element is not present */
if(elm->default_value_set) {
/* Fill-in DEFAULT */
if(elm->default_value_set(memb_ptr2)) {
FREEMEM(opres);
ASN__DECODE_FAILED;
}
ASN_DEBUG("Filled-in default");
}
/* The member is just not present */
continue;
}
/* Fall through */
}
/* Fetch the member from the stream */
ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
if(elm->flags & ATF_OPEN_TYPE) {
rv = OPEN_TYPE_aper_get(opt_codec_ctx, td, st, elm, pd);
} else {
rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
elm->encoding_constraints.per_constraints,
memb_ptr2, pd);
}
if(rv.code != RC_OK) {
ASN_DEBUG("Failed decode %s in %s",
elm->name, td->name);
FREEMEM(opres);
return rv;
}
}
/* Optionality map is not needed anymore */
FREEMEM(opres);
/*
* Deal with extensions.
*/
if(extpresent) {
ssize_t bmlength;
uint8_t *epres; /* Presence of extension members */
asn_per_data_t epmd;
bmlength = aper_get_nslength(pd);
if(bmlength < 0) ASN__DECODE_STARVED;
ASN_DEBUG("Extensions %" ASN_PRI_SSIZE " present in %s", bmlength, td->name);
epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
if(!epres) ASN__DECODE_STARVED;
/* Get the extensions map */
if(per_get_many_bits(pd, epres, 0, bmlength))
ASN__DECODE_STARVED;
memset(&epmd, 0, sizeof(epmd));
epmd.buffer = epres;
epmd.nbits = bmlength;
ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
td->name, bmlength, *epres);
/* Deal with padding */
if (aper_get_align(pd) < 0)
ASN__DECODE_STARVED;
/* Go over extensions and read them in */
for(edx = specs->first_extension; edx < td->elements_count; edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
int present;
if(!IN_EXTENSION_GROUP(specs, edx)) {
ASN_DEBUG("%ld is not extension", edx);
continue;
}
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
memb_ptr = (void *)((char *)st + elm->memb_offset);
memb_ptr2 = &memb_ptr;
}
present = per_get_few_bits(&epmd, 1);
if(present <= 0) {
if(present < 0) break; /* No more extensions */
continue;
}
ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
rv = aper_open_type_get(opt_codec_ctx, elm->type,
elm->encoding_constraints.per_constraints,
memb_ptr2, pd);
if(rv.code != RC_OK) {
FREEMEM(epres);
return rv;
}
}
/* Skip over overflow extensions which aren't present
* in this system's version of the protocol */
for(;;) {
ASN_DEBUG("Getting overflow extensions");
switch(per_get_few_bits(&epmd, 1)) {
case -1:
break;
case 0:
continue;
default:
if(aper_open_type_skip(opt_codec_ctx, pd)) {
FREEMEM(epres);
ASN__DECODE_STARVED;
}
}
break;
}
FREEMEM(epres);
}
/* Fill DEFAULT members in extensions */
for(edx = specs->roms_count; edx < specs->roms_count
+ specs->aoms_count; edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
void **memb_ptr2; /* Pointer to member pointer */
if(!elm->default_value_set) continue;
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
if(*memb_ptr2) continue;
} else {
continue; /* Extensions are all optionals */
}
/* Set default value */
if(elm->default_value_set(memb_ptr2)) {
ASN__DECODE_FAILED;
}
}
rv.consumed = 0;
rv.code = RC_OK;
return rv;
}
static int
SEQUENCE_handle_extensions_aper(const asn_TYPE_descriptor_t *td,
const void *sptr,
asn_per_outp_t *po1, asn_per_outp_t *po2) {
const asn_SEQUENCE_specifics_t *specs
= (const asn_SEQUENCE_specifics_t *)td->specifics;
int exts_present = 0;
int exts_count = 0;
size_t edx;
if(specs->first_extension < 0) {
return 0;
}
/* Find out which extensions are present */
for(edx = specs->first_extension; edx < td->elements_count; edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
const void *memb_ptr; /* Pointer to the member */
const void * const *memb_ptr2; /* Pointer to that pointer */
int present;
if(!IN_EXTENSION_GROUP(specs, edx)) {
ASN_DEBUG("%s (@%ld) is not extension", elm->type->name, edx);
continue;
}
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (const void * const *)((const char *)sptr + elm->memb_offset);
present = (*memb_ptr2 != 0);
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
memb_ptr2 = &memb_ptr;
present = 1;
}
ASN_DEBUG("checking %s (@%ld) present => %d",
elm->type->name, edx, present);
exts_count++;
exts_present += present;
/* Encode as presence marker */
if(po1 && per_put_few_bits(po1, present, 1))
return -1;
/* Encode as open type field */
if(po2 && present && aper_open_type_put(elm->type,
elm->encoding_constraints.per_constraints,
*memb_ptr2, po2))
return -1;
}
return exts_present ? exts_count : 0;
}
asn_enc_rval_t
SEQUENCE_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const asn_SEQUENCE_specifics_t *specs
= (const asn_SEQUENCE_specifics_t *)td->specifics;
asn_enc_rval_t er = {0,0,0};
int n_extensions;
size_t edx;
size_t i;
(void)constraints;
if(!sptr)
ASN__ENCODE_FAILED;
er.encoded = 0;
ASN_DEBUG("Encoding %s as SEQUENCE (APER)", td->name);
/*
* X.691#18.1 Whether structure is extensible
* and whether to encode extensions
*/
if(specs->first_extension < 0) {
n_extensions = 0; /* There are no extensions to encode */
} else {
n_extensions = SEQUENCE_handle_extensions_aper(td, sptr, 0, 0);
if(n_extensions < 0) ASN__ENCODE_FAILED;
if(per_put_few_bits(po, n_extensions ? 1 : 0, 1)) {
ASN__ENCODE_FAILED;
}
}
/* Encode a presence bitmap */
for(i = 0; i < specs->roms_count; i++) {
asn_TYPE_member_t *elm;
const void *memb_ptr; /* Pointer to the member */
const void * const *memb_ptr2; /* Pointer to that pointer */
int present;
edx = specs->oms[i];
elm = &td->elements[edx];
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (const void * const *)((const char *)sptr + elm->memb_offset);
present = (*memb_ptr2 != 0);
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
memb_ptr2 = &memb_ptr;
present = 1;
}
/* Eliminate default values */
if(present && elm->default_value_cmp
&& elm->default_value_cmp(*memb_ptr2) == 0)
present = 0;
ASN_DEBUG("Element %s %s %s->%s is %s",
elm->flags & ATF_POINTER ? "ptr" : "inline",
elm->default_value_cmp ? "def" : "wtv",
td->name, elm->name, present ? "present" : "absent");
if(per_put_few_bits(po, present, 1))
ASN__ENCODE_FAILED;
}
/*
* Encode the sequence ROOT elements.
*/
ASN_DEBUG("first_extension = %d, elements = %d", specs->first_extension,
td->elements_count);
for(edx = 0;
edx < ((specs->first_extension < 0) ? td->elements_count
: (size_t)specs->first_extension);
edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
const void *memb_ptr; /* Pointer to the member */
const void * const *memb_ptr2; /* Pointer to that pointer */
if(IN_EXTENSION_GROUP(specs, edx))
continue;
ASN_DEBUG("About to encode %s", elm->type->name);
/* Fetch the pointer to this member */
if(elm->flags & ATF_POINTER) {
memb_ptr2 = (const void * const *)((const char *)sptr + elm->memb_offset);
if(!*memb_ptr2) {
ASN_DEBUG("Element %s %ld not present",
elm->name, edx);
if(elm->optional)
continue;
/* Mandatory element is missing */
ASN__ENCODE_FAILED;
}
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
memb_ptr2 = &memb_ptr;
}
/* Eliminate default values */
if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
continue;
ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
er = elm->type->op->aper_encoder(elm->type,
elm->encoding_constraints.per_constraints,
*memb_ptr2, po);
if(er.encoded == -1)
return er;
}
/* No extensions to encode */
if(!n_extensions) ASN__ENCODED_OK(er);
ASN_DEBUG("Length of %d bit-map", n_extensions);
/* #18.8. Write down the presence bit-map length. */
if(aper_put_nslength(po, n_extensions))
ASN__ENCODE_FAILED;
ASN_DEBUG("Bit-map of %d elements", n_extensions);
/* #18.7. Encoding the extensions presence bit-map. */
/* TODO: act upon NOTE in #18.7 for canonical PER */
if(SEQUENCE_handle_extensions_aper(td, sptr, po, 0) != n_extensions)
ASN__ENCODE_FAILED;
ASN_DEBUG("Writing %d extensions", n_extensions);
/* #18.9. Encode extensions as open type fields. */
if(SEQUENCE_handle_extensions_aper(td, sptr, 0, po) != n_extensions)
ASN__ENCODE_FAILED;
ASN__ENCODED_OK(er);
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SEQUENCE.h>
int
SEQUENCE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
size_t edx;
int ret;
if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* Dump preamble */
if(cb(td->name, strlen(td->name), app_key) < 0
|| cb(" ::= {", 6, app_key) < 0)
return -1;
for(edx = 0; edx < td->elements_count; edx++) {
asn_TYPE_member_t *elm = &td->elements[edx];
const void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
if(!memb_ptr) {
if(elm->optional) continue;
/* Print <absent> line */
/* Fall through */
}
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
}
/* Indentation */
_i_INDENT(1);
/* Print the member's name and stuff */
if(cb(elm->name, strlen(elm->name), app_key) < 0
|| cb(": ", 2, app_key) < 0)
return -1;
/* Print the member itself */
ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
cb, app_key);
if(ret) return ret;
}
ilevel--;
_i_INDENT(1);
return (cb("}", 1, app_key) < 0) ? -1 : 0;
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SEQUENCE.h>
asn_random_fill_result_t
SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constr,
size_t max_length) {
const asn_SEQUENCE_specifics_t *specs =
(const asn_SEQUENCE_specifics_t *)td->specifics;
asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
void *st = *sptr;
size_t edx;
if(max_length == 0) return result_skipped;
(void)constr;
if(st == NULL) {
st = CALLOC(1, specs->struct_size);
if(st == NULL) {
return result_failed;
}
}
for(edx = 0; edx < td->elements_count; edx++) {
const asn_TYPE_member_t *elm = &td->elements[edx];
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
asn_random_fill_result_t tmpres;
if(elm->optional && asn_random_between(0, 4) == 2) {
/* Sometimes decide not to fill the optional value */
continue;
}
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
memb_ptr = (char *)st + elm->memb_offset;
memb_ptr2 = &memb_ptr;
}
tmpres = elm->type->op->random_fill(
elm->type, memb_ptr2, &elm->encoding_constraints,
max_length > result_ok.length ? max_length - result_ok.length : 0);
switch(tmpres.code) {
case ARFILL_OK:
result_ok.length += tmpres.length;
continue;
case ARFILL_SKIPPED:
assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
continue;
case ARFILL_FAILED:
if(st == *sptr) {
ASN_STRUCT_RESET(*td, st);
} else {
ASN_STRUCT_FREE(*td, st);
}
return tmpres;
}
}
*sptr = st;
return result_ok;
}

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@
#define CONSTR_SET_OF_H
#include <asn_application.h>
#include <asn_SET_OF.h>
#ifdef __cplusplus
extern "C" {
@ -26,22 +27,70 @@ typedef struct asn_SET_OF_specifics_s {
* A set specialized functions dealing with the SET OF type.
*/
asn_struct_free_f SET_OF_free;
#if !defined(ASN_DISABLE_PRINT_SUPPORT)
asn_struct_print_f SET_OF_print;
#endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
asn_struct_compare_f SET_OF_compare;
asn_constr_check_f SET_OF_constraint;
#if !defined(ASN_DISABLE_BER_SUPPORT)
ber_type_decoder_f SET_OF_decode_ber;
der_type_encoder_f SET_OF_encode_der;
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#if !defined(ASN_DISABLE_XER_SUPPORT)
xer_type_decoder_f SET_OF_decode_xer;
xer_type_encoder_f SET_OF_encode_xer;
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
oer_type_decoder_f SET_OF_decode_oer;
oer_type_encoder_f SET_OF_encode_oer;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT)
per_type_decoder_f SET_OF_decode_uper;
per_type_encoder_f SET_OF_encode_uper;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
#if !defined(ASN_DISABLE_APER_SUPPORT)
per_type_decoder_f SET_OF_decode_aper;
per_type_encoder_f SET_OF_encode_aper;
asn_random_fill_f SET_OF_random_fill;
#endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
asn_random_fill_f SET_OF_random_fill;
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
extern asn_TYPE_operation_t asn_OP_SET_OF;
/*
* Internally visible buffer holding a single encoded element.
*/
struct _el_buffer {
uint8_t *buf;
size_t length;
size_t allocated_size;
unsigned bits_unused;
};
enum SET_OF__encode_method {
SOES_DER, /* Distinguished Encoding Rules */
SOES_CUPER, /* Canonical Unaligned Packed Encoding Rules */
SOES_CAPER /* Canonical Aligned Packed Encoding Rules */
};
struct _el_buffer * SET_OF__encode_sorted(
const asn_TYPE_member_t *elm,
const asn_anonymous_set_ *list,
enum SET_OF__encode_method method);
void SET_OF__encode_sorted_free(
struct _el_buffer *el_buf,
size_t count);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,181 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SET_OF.h>
asn_enc_rval_t
SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_per_outp_t *po) {
const asn_anonymous_set_ *list;
const asn_per_constraint_t *ct;
const asn_TYPE_member_t *elm = td->elements;
struct _el_buffer *encoded_els;
asn_enc_rval_t er = {0,0,0};
int seq;
if(!sptr) ASN__ENCODE_FAILED;
list = _A_CSET_FROM_VOID(sptr);
er.encoded = 0;
ASN_DEBUG("Encoding %s as SET OF (%d)", td->name, list->count);
if(constraints) ct = &constraints->size;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->size;
else ct = 0;
/* If extensible constraint, check if size is in root */
if(ct) {
int not_in_root =
(list->count < ct->lower_bound || list->count > ct->upper_bound);
ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
if(ct->flags & APC_EXTENSIBLE) {
/* Declare whether size is in extension root */
if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
if(not_in_root) ct = 0;
} else if(not_in_root && ct->effective_bits >= 0) {
ASN__ENCODE_FAILED;
}
}
if(ct && ct->effective_bits >= 0) {
/* X.691, #19.5: No length determinant */
/*if(per_put_few_bits(po, list->count - ct->lower_bound,
ct->effective_bits))
ASN__ENCODE_FAILED;*/
if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0) {
ASN__ENCODE_FAILED;
}
}
/*
* Canonical PER #22.1 mandates dynamic sorting of the SET OF elements
* according to their encodings. Build an array of the encoded elements.
*/
encoded_els = SET_OF__encode_sorted(elm, list, SOES_CAPER);
for(seq = 0; seq < list->count;) {
ssize_t may_encode;
int need_eom = 0;
if(ct && ct->effective_bits >= 0) {
may_encode = list->count;
} else {
may_encode =
aper_put_length(po, -1, list->count - seq, &need_eom);
if(may_encode < 0) ASN__ENCODE_FAILED;
}
while(may_encode--) {
const struct _el_buffer *el = &encoded_els[seq++];
if(asn_put_many_bits(po, el->buf,
(8 * el->length) - el->bits_unused) < 0) {
break;
}
}
if(need_eom && aper_put_length(po, -1, 0, 0))
ASN__ENCODE_FAILED; /* End of Message length */
}
SET_OF__encode_sorted_free(encoded_els, list->count);
ASN__ENCODED_OK(er);
}
asn_dec_rval_t
SET_OF_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) {
asn_dec_rval_t rv = {RC_OK, 0};
const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
const asn_TYPE_member_t *elm = td->elements; /* Single one */
void *st = *sptr;
asn_anonymous_set_ *list;
const asn_per_constraint_t *ct;
int repeat = 0;
ssize_t nelems;
if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
ASN__DECODE_FAILED;
/*
* Create the target structure if it is not present already.
*/
if(!st) {
st = *sptr = CALLOC(1, specs->struct_size);
if(!st) ASN__DECODE_FAILED;
}
list = _A_SET_FROM_VOID(st);
/* Figure out which constraints to use */
if(constraints) ct = &constraints->size;
else if(td->encoding_constraints.per_constraints)
ct = &td->encoding_constraints.per_constraints->size;
else ct = 0;
if(ct && ct->flags & APC_EXTENSIBLE) {
int value = per_get_few_bits(pd, 1);
if(value < 0) ASN__DECODE_STARVED;
if(value) ct = 0; /* Not restricted! */
}
if(ct && ct->effective_bits >= 0) {
/* X.691, #19.5: No length determinant */
nelems = aper_get_nsnnwn(pd, ct->upper_bound - ct->lower_bound + 1);
ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s",
(long)nelems, ct->lower_bound, td->name);
if(nelems < 0) ASN__DECODE_STARVED;
nelems += ct->lower_bound;
} else {
nelems = -1;
}
do {
int i;
if(nelems < 0) {
nelems = aper_get_length(pd, ct ? ct->upper_bound - ct->lower_bound + 1 : -1,
ct ? ct->effective_bits : -1, &repeat);
ASN_DEBUG("Got to decode %d elements (eff %d)",
(int)nelems, (int)(ct ? ct->effective_bits : -1));
if(nelems < 0) ASN__DECODE_STARVED;
}
for(i = 0; i < nelems; i++) {
void *ptr = 0;
ASN_DEBUG("SET OF %s decoding", elm->type->name);
rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
elm->encoding_constraints.per_constraints, &ptr, pd);
ASN_DEBUG("%s SET OF %s decoded %d, %p",
td->name, elm->type->name, rv.code, ptr);
if(rv.code == RC_OK) {
if(ASN_SET_ADD(list, ptr) == 0)
continue;
ASN_DEBUG("Failed to add element into %s",
td->name);
/* Fall through */
rv.code = RC_FAIL;
} else {
ASN_DEBUG("Failed decoding %s of %s (SET OF)",
elm->type->name, td->name);
}
if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
return rv;
}
nelems = -1; /* Allow uper_get_length() */
} while(repeat);
ASN_DEBUG("Decoded %s as SET OF", td->name);
rv.code = RC_OK;
rv.consumed = 0;
return rv;
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SET_OF.h>
int
SET_OF_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_TYPE_member_t *elm = td->elements;
const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
int ret;
int i;
if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* Dump preamble */
if(cb(td->name, strlen(td->name), app_key) < 0
|| cb(" ::= {", 6, app_key) < 0)
return -1;
for(i = 0; i < list->count; i++) {
const void *memb_ptr = list->array[i];
if(!memb_ptr) continue;
_i_INDENT(1);
ret = elm->type->op->print_struct(elm->type, memb_ptr,
ilevel + 1, cb, app_key);
if(ret) return ret;
}
ilevel--;
_i_INDENT(1);
return (cb("}", 1, app_key) < 0) ? -1 : 0;
}

View File

@ -0,0 +1,152 @@
/*
* Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_SET_OF.h>
asn_random_fill_result_t
SET_OF_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints,
size_t max_length) {
const asn_SET_OF_specifics_t *specs =
(const asn_SET_OF_specifics_t *)td->specifics;
asn_random_fill_result_t res_ok = {ARFILL_OK, 0};
asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
const asn_TYPE_member_t *elm = td->elements;
void *st = *sptr;
long max_elements = 5;
long slb = 0; /* Lower size bound */
long sub = 0; /* Upper size bound */
size_t rnd_len;
if(max_length == 0) return result_skipped;
if(st == NULL) {
st = (*sptr = CALLOC(1, specs->struct_size));
if(st == NULL) {
return result_failed;
}
}
switch(asn_random_between(0, 6)) {
case 0: max_elements = 0; break;
case 1: max_elements = 1; break;
case 2: max_elements = 5; break;
case 3: max_elements = max_length; break;
case 4: max_elements = max_length / 2; break;
case 5: max_elements = max_length / 4; break;
default: break;
}
sub = slb + max_elements;
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
if(!constraints || !constraints->per_constraints)
constraints = &td->encoding_constraints;
if(constraints->per_constraints) {
const asn_per_constraint_t *pc = &constraints->per_constraints->size;
if(pc->flags & APC_SEMI_CONSTRAINED) {
slb = pc->lower_bound;
sub = pc->lower_bound + max_elements;
} else if(pc->flags & APC_CONSTRAINED) {
slb = pc->lower_bound;
sub = pc->upper_bound;
if(sub - slb > max_elements) sub = slb + max_elements;
}
}
#else
if(!constraints) constraints = &td->encoding_constraints;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
/* Bias towards edges of allowed space */
switch(asn_random_between(-1, 4)) {
default:
case -1:
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
/* Prepare lengths somewhat outside of constrained range. */
if(constraints->per_constraints
&& (constraints->per_constraints->size.flags & APC_EXTENSIBLE)) {
switch(asn_random_between(0, 5)) {
default:
case 0:
rnd_len = 0;
break;
case 1:
if(slb > 0) {
rnd_len = slb - 1;
} else {
rnd_len = 0;
}
break;
case 2:
rnd_len = asn_random_between(0, slb);
break;
case 3:
if(sub < (ssize_t)max_length) {
rnd_len = sub + 1;
} else {
rnd_len = max_length;
}
break;
case 4:
if(sub < (ssize_t)max_length) {
rnd_len = asn_random_between(sub + 1, max_length);
} else {
rnd_len = max_length;
}
break;
case 5:
rnd_len = max_length;
break;
}
break;
}
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
/* Fall through */
case 0:
rnd_len = asn_random_between(slb, sub);
break;
case 1:
if(slb < sub) {
rnd_len = asn_random_between(slb + 1, sub);
break;
}
/* Fall through */
case 2:
rnd_len = asn_random_between(slb, slb);
break;
case 3:
if(slb < sub) {
rnd_len = asn_random_between(slb, sub - 1);
break;
}
/* Fall through */
case 4:
rnd_len = asn_random_between(sub, sub);
break;
}
for(; rnd_len > 0; rnd_len--) {
asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
void *ptr = 0;
asn_random_fill_result_t tmpres = elm->type->op->random_fill(
elm->type, &ptr, &elm->encoding_constraints,
(max_length > res_ok.length ? max_length - res_ok.length : 0)
/ rnd_len);
switch(tmpres.code) {
case ARFILL_OK:
ASN_SET_ADD(list, ptr);
res_ok.length += tmpres.length;
break;
case ARFILL_SKIPPED:
break;
case ARFILL_FAILED:
assert(ptr == 0);
return tmpres;
}
}
return res_ok;
}

View File

@ -70,7 +70,7 @@ _print2fp(const void *buffer, size_t size, void *app_key) {
* Some compilers do not support variable args macros.
* This function is a replacement of ASN_DEBUG() macro.
*/
void ASN_DEBUG_f(const char *fmt, ...);
void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
void ASN_DEBUG_f(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);

View File

@ -34,23 +34,45 @@ typedef struct asn_struct_ctx_s {
ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
} asn_struct_ctx_t;
#include <ber_decoder.h> /* Basic Encoding Rules decoder */
#include <der_encoder.h> /* Distinguished Encoding Rules encoder */
#include <xer_decoder.h> /* Decoder of XER (XML, text) */
#include <xer_encoder.h> /* Encoder into XER (XML, text) */
#include <per_decoder.h> /* Packet Encoding Rules decoder */
#include <per_encoder.h> /* Packet Encoding Rules encoder */
#include <constraints.h> /* Subtype constraints support */
#include <asn_random_fill.h> /* Random structures support */
#if !defined(ASN_DISABLE_BER_SUPPORT)
#include <ber_decoder.h> /* Basic Encoding Rules decoder */
#include <der_encoder.h> /* Distinguished Encoding Rules encoder */
#else
typedef void (ber_type_decoder_f)(void);
typedef void (der_type_encoder_f)(void);
#endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
#ifdef ASN_DISABLE_OER_SUPPORT
#if !defined(ASN_DISABLE_XER_SUPPORT)
#include <xer_decoder.h> /* Decoder of XER (XML, text) */
#include <xer_encoder.h> /* Encoder into XER (XML, text) */
#else
typedef void (xer_type_decoder_f)(void);
typedef void (xer_type_encoder_f)(void);
#endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
#include <per_decoder.h> /* Packet Encoding Rules decoder */
#include <per_encoder.h> /* Packet Encoding Rules encoder */
#else
typedef void (per_type_decoder_f)(void);
typedef void (per_type_encoder_f)(void);
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
#include <constraints.h> /* Subtype constraints support */
#if !defined(ASN_DISABLE_RFILL_SUPPORT)
#include <asn_random_fill.h> /* Random structures support */
#else
typedef void (asn_random_fill_f)(void);
#endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
#if !defined(ASN_DISABLE_OER_SUPPORT)
#include <oer_decoder.h> /* Octet Encoding Rules encoder */
#include <oer_encoder.h> /* Octet Encoding Rules encoder */
#else
typedef void (oer_type_decoder_f)(void);
typedef void (oer_type_encoder_f)(void);
typedef void asn_oer_constraints_t;
#else
#include <oer_decoder.h> /* Octet Encoding Rules encoder */
#include <oer_encoder.h> /* Octet Encoding Rules encoder */
#endif
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
/*
* Free the structure according to its specification.
@ -138,7 +160,7 @@ typedef asn_type_selector_result_t(asn_type_selector_f)(
const void *parent_structure_ptr);
/*
* Generalized functions for dealing with the specific type.
* Generalized functions for dealing with the speciic type.
* May be directly invoked by applications.
*/
typedef struct asn_TYPE_operation_s {
@ -163,8 +185,12 @@ typedef struct asn_TYPE_operation_s {
* A constraints tuple specifying both the OER and PER constraints.
*/
typedef struct asn_encoding_constraints_s {
#if !defined(ASN_DISABLE_OER_SUPPORT)
const struct asn_oer_constraints_s *oer_constraints;
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
const struct asn_per_constraints_s *per_constraints;
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
asn_constr_check_f *general_constraints;
} asn_encoding_constraints_t;

View File

@ -35,6 +35,7 @@ struct errbufDesc {
};
static void
CC_PRINTFLIKE(4, 5)
_asn_i_ctfailcb(void *key, const asn_TYPE_descriptor_t *td, const void *sptr,
const char *fmt, ...) {
struct errbufDesc *arg = key;

View File

@ -6,6 +6,7 @@
#define ASN1_CONSTRAINTS_VALIDATOR_H
#include <asn_system.h> /* Platform-dependent types */
#include <asn_application.h>
#ifdef __cplusplus
extern "C" {

View File

@ -17,110 +17,137 @@
libasn1c_common_sources = files('''
ANY.h
ANY.c
OCTET_STRING.h
OPEN_TYPE.h
OPEN_TYPE.c
constr_CHOICE.h
GraphicString.h
GraphicString.c
INTEGER.h
INTEGER.c
NULL.h
NULL.c
NativeEnumerated.h
NativeEnumerated.c
NativeInteger.h
NativeInteger.c
OBJECT_IDENTIFIER.h
OBJECT_IDENTIFIER.c
asn_codecs_prim.h
ObjectDescriptor.h
ObjectDescriptor.c
PrintableString.h
PrintableString.c
UTF8String.h
UTF8String.c
VisibleString.h
VisibleString.c
asn_SEQUENCE_OF.h
asn_SEQUENCE_OF.c
asn_SET_OF.h
asn_SET_OF.c
constr_CHOICE.c
constr_SEQUENCE.h
constr_SEQUENCE.c
constr_SEQUENCE_OF.h
constr_SEQUENCE_OF.c
constr_SET_OF.h
constr_SET_OF.c
asn_application.h
asn_application.c
asn_ioc.h
asn_system.h
asn_codecs.h
asn_internal.h
asn_random_fill.h
asn_bit_data.h
BIT_STRING.h
asn_codecs_prim.h
ber_tlv_length.h
ber_tlv_tag.h
ber_decoder.h
der_encoder.h
constr_TYPE.h
constraints.h
xer_support.h
xer_decoder.h
xer_encoder.h
per_support.h
per_decoder.h
per_encoder.h
per_opentype.h
oer_decoder.h
oer_encoder.h
oer_support.h
ANY.c
OPEN_TYPE.c
GraphicString.c
INTEGER.c
NULL.c
NativeEnumerated.c
NativeInteger.c
OBJECT_IDENTIFIER.c
ObjectDescriptor.c
PrintableString.c
asn_SEQUENCE_OF.c
asn_SET_OF.c
constr_CHOICE.c
constr_SEQUENCE.c
constr_SEQUENCE_OF.c
constr_SET_OF.c
asn_application.c
asn_internal.c
asn_random_fill.c
asn_bit_data.h
asn_bit_data.c
OCTET_STRING.c
BIT_STRING.h
BIT_STRING.c
asn_codecs_prim.c
ber_tlv_length.h
ber_tlv_length.c
ber_tlv_tag.h
ber_tlv_tag.c
ber_decoder.c
der_encoder.c
constr_TYPE.h
constr_TYPE.c
constraints.h
constraints.c
xer_support.c
xer_decoder.c
xer_encoder.c
per_support.c
per_decoder.h
per_encoder.h
per_support.h
per_opentype.h
per_decoder.c
per_encoder.c
per_support.c
per_opentype.c
oer_decoder.c
oer_encoder.c
oer_support.c
OPEN_TYPE_oer.c
INTEGER_oer.c
BIT_STRING_oer.c
OCTET_STRING_oer.c
NativeInteger_oer.c
NativeEnumerated_oer.c
constr_CHOICE_oer.c
constr_SEQUENCE_oer.c
constr_SET_OF_oer.c
aper_decoder.h
aper_encoder.h
aper_support.h
aper_opentype.h
aper_decoder.c
aper_encoder.c
aper_support.c
aper_opentype.c
ANY_aper.c
INTEGER_aper.c
NULL_aper.c
NativeEnumerated_aper.c
NativeInteger_aper.c
OCTET_STRING_aper.c
OPEN_TYPE_aper.c
constr_CHOICE_aper.c
constr_SEQUENCE_OF_aper.c
constr_SEQUENCE_aper.c
constr_SET_OF_aper.c
BIT_STRING_print.c
INTEGER_print.c
NULL_print.c
NativeInteger_print.c
OBJECT_IDENTIFIER_print.c
OCTET_STRING_print.c
UTF8String_print.c
constr_CHOICE_print.c
constr_SEQUENCE_print.c
constr_SET_OF_print.c
asn_random_fill.h
asn_random_fill.c
BIT_STRING_rfill.c
INTEGER_rfill.c
NULL_rfill.c
NativeInteger_rfill.c
OBJECT_IDENTIFIER_rfill.c
OCTET_STRING_rfill.c
UTF8String_rfill.c
constr_CHOICE_rfill.c
constr_SEQUENCE_rfill.c
constr_SET_OF_rfill.c
'''.split())
libasn1c_common_cc_flags = cc.get_supported_arguments([
'-Wno-parentheses-equality',
'-Wno-overflow',
'-Wno-format',
'-Wno-implicit-fallthrough',
'-Wno-missing-prototypes',
'-Wno-missing-declarations',
'-Wno-missing-field-initializers',
'-Wno-format-nonliteral',
'-Wno-format-security',
'-std=gnu99',
libasn1c_common_cc_flags = ['-DASN_DISABLE_BER_SUPPORT',
'-DASN_DISABLE_XER_SUPPORT',
'-DASN_DISABLE_OER_SUPPORT',
'-DASN_DISABLE_UPER_SUPPORT']
#libasn1c_common_cc_flags += cc.get_supported_arguments([
# '-Wno-parentheses-equality',
# '-Wno-overflow',
# '-Wno-format',
#
# '-Wno-implicit-fallthrough',
# '-Wno-missing-prototypes',
# '-Wno-missing-declarations',
# '-Wno-missing-field-initializers',
# '-Wno-format-nonliteral',
# '-Wno-format-security',
# '-Wno-type-limits',
# '-w',
# '-std=gnu99',
#])
libasn1c_common_cc_flags += cc.get_supported_arguments([
'-w', '-std=gnu99',
])
libasn1c_common_inc = include_directories('.')

View File

@ -2,184 +2,4 @@
#include <asn_internal.h>
#include <per_decoder.h>
/*
* 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 <limits.h> */
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 <limits.h> */
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;
}
// Absolutely nothing

View File

@ -14,58 +14,6 @@ extern "C" {
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.
*/

View File

@ -1,10 +1,7 @@
#include <asn_application.h>
#include <asn_internal.h>
#include <per_encoder.h>
static int _uper_encode_flush_outp(asn_per_outp_t *po);
static int
int
ignore_output(const void *data, size_t size, void *app_key) {
(void)data;
(void)size;
@ -12,81 +9,7 @@ ignore_output(const void *data, size_t size, void *app_key) {
return 0;
}
asn_enc_rval_t
uper_encode(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints, const void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_per_outp_t po;
asn_enc_rval_t er = {0,0,0};
/*
* Invoke type-specific encoder.
*/
if(!td || !td->op->uper_encoder)
ASN__ENCODE_FAILED; /* PER is not compiled in */
po.buffer = po.tmpspace;
po.nboff = 0;
po.nbits = 8 * sizeof(po.tmpspace);
po.output = cb ? cb : ignore_output;
po.op_key = app_key;
po.flushed_bytes = 0;
er = td->op->uper_encoder(td, constraints, sptr, &po);
if(er.encoded != -1) {
size_t bits_to_flush;
bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
/* Set number of bits encoded to a firm value */
er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
}
return er;
}
/*
* Argument type and callback necessary for uper_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;
}
asn_enc_rval_t
uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void *buffer, size_t buffer_size) {
enc_to_buf_arg key;
key.buffer = buffer;
key.left = buffer_size;
if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
}
typedef struct enc_dyn_arg {
void *buffer;
size_t length;
size_t allocated;
} enc_dyn_arg;
static int
int
encode_dyn_cb(const void *buffer, size_t size, void *key) {
enc_dyn_arg *arg = key;
if(arg->length + size >= arg->allocated) {
@ -110,156 +33,3 @@ encode_dyn_cb(const void *buffer, size_t size, void *key) {
arg->length += size;
return 0;
}
ssize_t
uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void **buffer_r) {
asn_enc_rval_t er = {0,0,0};
enc_dyn_arg key;
memset(&key, 0, sizeof(key));
er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
switch(er.encoded) {
case -1:
FREEMEM(key.buffer);
return -1;
case 0:
FREEMEM(key.buffer);
key.buffer = MALLOC(1);
if(key.buffer) {
*(char *)key.buffer = '\0';
*buffer_r = key.buffer;
return 1;
} else {
return -1;
}
default:
*buffer_r = key.buffer;
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
return ((er.encoded + 7) >> 3);
}
}
/*
* Internally useful functions.
*/
/* Flush partially filled buffer */
static int
_uper_encode_flush_outp(asn_per_outp_t *po) {
uint8_t *buf;
if(po->nboff == 0 && po->buffer == po->tmpspace)
return 0;
buf = po->buffer + (po->nboff >> 3);
/* Make sure we account for the last, partially filled */
if(po->nboff & 0x07) {
buf[0] &= 0xff << (8 - (po->nboff & 0x07));
buf++;
}
return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
}
asn_enc_rval_t
aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void *buffer, size_t buffer_size) {
enc_to_buf_arg key;
key.buffer = buffer;
key.left = buffer_size;
if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
}
ssize_t
aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, void **buffer_r) {
asn_enc_rval_t er = {0,0,0};
enc_dyn_arg key;
memset(&key, 0, sizeof(key));
er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
switch(er.encoded) {
case -1:
FREEMEM(key.buffer);
return -1;
case 0:
FREEMEM(key.buffer);
key.buffer = MALLOC(1);
if(key.buffer) {
*(char *)key.buffer = '\0';
*buffer_r = key.buffer;
return 1;
} else {
return -1;
}
default:
*buffer_r = key.buffer;
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
return ((er.encoded + 7) >> 3);
}
}
static int
_aper_encode_flush_outp(asn_per_outp_t *po) {
uint8_t *buf;
if(po->nboff == 0 && po->buffer == po->tmpspace)
return 0;
buf = po->buffer + (po->nboff >> 3);
/* Make sure we account for the last, partially filled */
if(po->nboff & 0x07) {
buf[0] &= 0xff << (8 - (po->nboff & 0x07));
buf++;
}
if (po->output) {
return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
}
return 0;
}
asn_enc_rval_t
aper_encode(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
asn_per_outp_t po;
asn_enc_rval_t er = {0,0,0};
/*
* Invoke type-specific encoder.
*/
if(!td || !td->op->aper_encoder)
ASN__ENCODE_FAILED; /* PER is not compiled in */
po.buffer = po.tmpspace;
po.nboff = 0;
po.nbits = 8 * sizeof(po.tmpspace);
po.output = cb ? cb : ignore_output;
po.op_key = app_key;
po.flushed_bytes = 0;
er = td->op->aper_encoder(td, constraints, sptr, &po);
if(er.encoded != -1) {
size_t bits_to_flush;
bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
/* Set number of bits encoded to a firm value */
er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
if(_aper_encode_flush_outp(&po))
ASN__ENCODE_FAILED;
}
return er;
}

View File

@ -14,70 +14,6 @@ extern "C" {
struct asn_TYPE_descriptor_s; /* Forward declaration */
/*
* Unaligned PER encoder of any ASN.1 type. May be invoked by the application.
* WARNING: This function returns the number of encoded bits in the .encoded
* field of the return value. Use the following formula to convert to bytes:
* bytes = ((.encoded + 7) / 8)
*/
asn_enc_rval_t uper_encode(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
asn_app_consume_bytes_f *consume_bytes_cb, /* Data collector */
void *app_key /* Arbitrary callback argument */
);
asn_enc_rval_t aper_encode(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
asn_app_consume_bytes_f *consume_bytes_cb, /* Data collector */
void *app_key /* Arbitrary callback argument */
);
/*
* A variant of uper_encode() which encodes data into the existing buffer
* WARNING: This function returns the number of encoded bits in the .encoded
* field of the return value.
*/
asn_enc_rval_t uper_encode_to_buffer(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
void *buffer, /* Pre-allocated buffer */
size_t buffer_size /* Initial buffer size (max) */
);
asn_enc_rval_t aper_encode_to_buffer(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
void *buffer, /* Pre-allocated buffer */
size_t buffer_size /* Initial buffer size (max) */
);
/*
* A variant of uper_encode_to_buffer() which allocates buffer itself.
* Returns the number of bytes in the buffer or -1 in case of failure.
* WARNING: This function produces a "Production of the complete encoding",
* with length of at least one octet. Contrast this to precise bit-packing
* encoding of uper_encode() and uper_encode_to_buffer().
*/
ssize_t uper_encode_to_new_buffer(
const struct asn_TYPE_descriptor_s *type_descriptor,
const asn_per_constraints_t *constraints,
const void *struct_ptr, /* Structure to be encoded */
void **buffer_r /* Buffer allocated and returned */
);
ssize_t
aper_encode_to_new_buffer(
const struct asn_TYPE_descriptor_s *td,
const asn_per_constraints_t *constraints,
const void *sptr,
void **buffer_r
);
/*
* Type of the generic PER encoder function.
*/
@ -86,6 +22,15 @@ typedef asn_enc_rval_t(per_type_encoder_f)(
const asn_per_constraints_t *constraints, const void *struct_ptr,
asn_per_outp_t *per_output);
int ignore_output(const void *data, size_t size, void *app_key);
typedef struct enc_dyn_arg {
void *buffer;
size_t length;
size_t allocated;
} enc_dyn_arg;
int encode_dyn_cb(const void *buffer, size_t size, void *key);
#ifdef __cplusplus
}
#endif

View File

@ -2,288 +2,13 @@
* Copyright (c) 2007 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <per_support.h>
#include <constr_TYPE.h>
#include <per_opentype.h>
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; /* No one 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 = "<unknown extension>";
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
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) {
@ -301,238 +26,3 @@ uper_sot_suck(const asn_codec_ctx_t *ctx, const asn_TYPE_descriptor_t *td,
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;) {
int need_eom = 0;
ssize_t maySave = aper_put_length(po, -1, toGo, &need_eom);
if(maySave < 0) break;
if(per_put_many_bits(po, bptr, maySave * 8)) break;
bptr = (char *)bptr + maySave;
toGo -= maySave;
if(need_eom && aper_put_length(po, -1, 0, 0)) {
FREEMEM(buf);
return -1;
}
}
FREEMEM(buf);
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 = "<unknown extension>";
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;
}

View File

@ -5,37 +5,17 @@
#ifndef _PER_OPENTYPE_H_
#define _PER_OPENTYPE_H_
#include <asn_internal.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);
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);
#ifdef __cplusplus
}

View File

@ -6,578 +6,4 @@
#include <asn_internal.h>
#include <per_support.h>
/*
* 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, uintmax_t *out_value, int nbits) {
uintmax_t lhalf; /* Lower half of the number*/
intmax_t 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 = ((uintmax_t)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__imax_range(intmax_t lb, intmax_t ub, uintmax_t *range_r) {
uintmax_t bounds_range;
if((ub < 0) == (lb < 0)) {
bounds_range = ub - lb;
} else if(lb < 0) {
assert(ub >= 0);
bounds_range = 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1));
} else {
assert(!"Unreachable");
return -1;
}
*range_r = bounds_range;
return 0;
}
int
per_imax_range_rebase(intmax_t v, intmax_t lb, intmax_t ub, uintmax_t *output) {
uintmax_t range;
assert(lb <= ub);
if(v < lb || v > ub || per__imax_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) {
uintmax_t rebased = 1 + (uintmax_t)-(v+1) + (uintmax_t)lb;
assert(rebased <= range); /* By construction */
*output = rebased;
return 0;
} else if(lb < 0) {
uintmax_t rebased = 1 + (uintmax_t)-(lb+1) + (uintmax_t)v;
assert(rebased <= range); /* By construction */
*output = rebased;
return 0;
} else {
assert(!"Unreachable");
return -1;
}
}
int
per_long_range_rebase(long v, intmax_t lb, intmax_t ub, unsigned long *output) {
uintmax_t tmp = *output;
int rc = per_imax_range_rebase((intmax_t)v, lb, ub, &tmp);
*output = tmp;
return rc;
}
int
per_imax_range_unrebase(uintmax_t inp, intmax_t lb, intmax_t ub, intmax_t *outp) {
uintmax_t range;
if(per__imax_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 <= INTMAX_MAX) {
*outp = (long)inp + lb;
} else {
*outp = (lb + INTMAX_MAX + 1) + (intmax_t)((inp - INTMAX_MAX) - 1);
}
return 0;
}
int
per_long_range_unrebase(unsigned long inp, intmax_t lb, intmax_t ub, long *outp) {
intmax_t tmp = *outp;
int rc = per_imax_range_unrebase((uintmax_t)inp, lb, ub, &tmp);
*outp = tmp;
return rc;
}
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 */
}
}
#if !defined(USE_OLDER_APER_NSNNWN)
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;
int length;
/* handle indefinite range */
length = per_get_few_bits(pd, 1);
if (length == 0)
return per_get_few_bits(pd, 6);
if (aper_get_align(pd) < 0)
return -1;
length = per_get_few_bits(pd, 8);
/* the length is not likely to be that big */
if (length > 4)
return -1;
value = 0;
if (per_get_many_bits(pd, (uint8_t *)&value, 0, length * 8) < 0)
return -1;
return value;
}
if (aper_get_align(pd) < 0)
return -1;
value = per_get_few_bits(pd, 8 * bytes);
return value;
}
#else /* old APER codec */
ssize_t
aper_get_nsnnwn(asn_per_data_t *pd, int dummy_range) {
ssize_t value;
ASN_DEBUG("Get the normally small non-negative whole number APER");
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;
}
#endif /* don't use old APER */
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, int *need_eom) {
int dummy = 0;
if(!need_eom) need_eom = &dummy;
*need_eom = 0;
ASN_DEBUG("APER put length %zu with range %d", length, range);
/* 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;
*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);
}
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, 0) != (ssize_t)length) {
/* This might happen in case of >16K extensions */
return -1;
}
}
return 0;
}
#if !defined(USE_OLDER_APER_NSNNWN)
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);
}
#else /* preserve old code base in case */
int
aper_put_nsnnwn(asn_per_outp_t *po, int dummy_range, int n) {
int bytes;
ASN_DEBUG("aper_put_nsnnwn");
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);
}
#endif /* which aper_put_nsnnwn() */
// Absolutely nothing

View File

@ -41,88 +41,12 @@ typedef struct asn_bit_data_s asn_per_data_t;
#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, uintmax_t *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, intmax_t lb, intmax_t ub, unsigned long *output);
int per_imax_range_rebase(intmax_t v, intmax_t lb, intmax_t ub, uintmax_t *output);
/* The inverse operation: restores the value by the offset and its bounds. */
int per_long_range_unrebase(unsigned long inp, intmax_t lb, intmax_t ub, long *outp);
int per_imax_range_unrebase(uintmax_t inp, intmax_t lb, intmax_t ub, intmax_t *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,
int *opt_need_eom);
/* 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

View File

@ -1,8 +1,8 @@
/*
* Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
* From ASN.1 module "NGAP-IEs"
* found in "../support/ngap-r16.1.0/38413-g10.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps`
* found in "../support/ngap-r16.4.0/38413-g40.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps -no-gen-BER -no-gen-XER -no-gen-OER -no-gen-UPER`
*/
#include "NGAP_AMF-TNLAssociationSetupItem.h"
@ -14,16 +14,32 @@ asn_TYPE_member_t asn_MBR_NGAP_AMF_TNLAssociationSetupItem_1[] = {
+1, /* EXPLICIT tag at current level */
&asn_DEF_NGAP_CPTransportLayerInformation,
0,
{ 0, 0, 0 },
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
0
},
0, 0, /* No default value */
"aMF-TNLAssociationAddress"
},
{ ATF_POINTER, 1, offsetof(struct NGAP_AMF_TNLAssociationSetupItem, iE_Extensions),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_NGAP_ProtocolExtensionContainer_7027P3,
&asn_DEF_NGAP_ProtocolExtensionContainer_9571P5,
0,
{ 0, 0, 0 },
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
0
},
0, 0, /* No default value */
"iE-Extensions"
},
@ -55,7 +71,15 @@ asn_TYPE_descriptor_t asn_DEF_NGAP_AMF_TNLAssociationSetupItem = {
asn_DEF_NGAP_AMF_TNLAssociationSetupItem_tags_1, /* Same as above */
sizeof(asn_DEF_NGAP_AMF_TNLAssociationSetupItem_tags_1)
/sizeof(asn_DEF_NGAP_AMF_TNLAssociationSetupItem_tags_1[0]), /* 1 */
{ 0, 0, SEQUENCE_constraint },
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
SEQUENCE_constraint
},
asn_MBR_NGAP_AMF_TNLAssociationSetupItem_1,
2, /* Elements count */
&asn_SPC_NGAP_AMF_TNLAssociationSetupItem_specs_1 /* Additional specs */

View File

@ -1,8 +1,8 @@
/*
* Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
* From ASN.1 module "NGAP-IEs"
* found in "../support/ngap-r16.1.0/38413-g10.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps`
* found in "../support/ngap-r16.4.0/38413-g40.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps -no-gen-BER -no-gen-XER -no-gen-OER -no-gen-UPER`
*/
#ifndef _NGAP_AMF_TNLAssociationSetupItem_H_

View File

@ -1,28 +1,35 @@
/*
* Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
* From ASN.1 module "NGAP-IEs"
* found in "../support/ngap-r16.1.0/38413-g10.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps`
* found in "../support/ngap-r16.4.0/38413-g40.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps -no-gen-BER -no-gen-XER -no-gen-OER -no-gen-UPER`
*/
#include "NGAP_AMF-TNLAssociationSetupList.h"
#include "NGAP_AMF-TNLAssociationSetupItem.h"
static asn_oer_constraints_t asn_OER_type_NGAP_AMF_TNLAssociationSetupList_constr_1 CC_NOTUSED = {
{ 0, 0 },
-1 /* (SIZE(1..32)) */};
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
static asn_per_constraints_t asn_PER_type_NGAP_AMF_TNLAssociationSetupList_constr_1 CC_NOTUSED = {
{ APC_UNCONSTRAINED, -1, -1, 0, 0 },
{ APC_CONSTRAINED, 5, 5, 1, 32 } /* (SIZE(1..32)) */,
0, 0 /* No PER value map */
};
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
static asn_TYPE_member_t asn_MBR_NGAP_AMF_TNLAssociationSetupList_1[] = {
{ ATF_POINTER, 0, 0,
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2)),
0,
&asn_DEF_NGAP_AMF_TNLAssociationSetupItem,
0,
{ 0, 0, 0 },
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
0
},
0, 0, /* No default value */
""
},
@ -45,7 +52,15 @@ asn_TYPE_descriptor_t asn_DEF_NGAP_AMF_TNLAssociationSetupList = {
asn_DEF_NGAP_AMF_TNLAssociationSetupList_tags_1, /* Same as above */
sizeof(asn_DEF_NGAP_AMF_TNLAssociationSetupList_tags_1)
/sizeof(asn_DEF_NGAP_AMF_TNLAssociationSetupList_tags_1[0]), /* 1 */
{ &asn_OER_type_NGAP_AMF_TNLAssociationSetupList_constr_1, &asn_PER_type_NGAP_AMF_TNLAssociationSetupList_constr_1, SEQUENCE_OF_constraint },
{
#if !defined(ASN_DISABLE_OER_SUPPORT)
0,
#endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
#if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
&asn_PER_type_NGAP_AMF_TNLAssociationSetupList_constr_1,
#endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
SEQUENCE_OF_constraint
},
asn_MBR_NGAP_AMF_TNLAssociationSetupList_1,
1, /* Single element */
&asn_SPC_NGAP_AMF_TNLAssociationSetupList_specs_1 /* Additional specs */

View File

@ -1,8 +1,8 @@
/*
* Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
* From ASN.1 module "NGAP-IEs"
* found in "../support/ngap-r16.1.0/38413-g10.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps`
* found in "../support/ngap-r16.4.0/38413-g40.asn"
* `asn1c -pdu=all -fcompound-names -findirect-choice -fno-include-deps -no-gen-BER -no-gen-XER -no-gen-OER -no-gen-UPER`
*/
#ifndef _NGAP_AMF_TNLAssociationSetupList_H_

Some files were not shown because too many files have changed in this diff Show More