Skip to content

Merge APER implementation from mouse07410's repository #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -1433,12 +1433,14 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
OUT("xer_type_decoder_f %s_decode_xer;\n", p);
OUT("xer_type_encoder_f %s_encode_xer;\n", p);
if(arg->flags & A1C_GEN_OER) {
OUT("oer_type_decoder_f %s_decode_oer;\n", p);
OUT("oer_type_encoder_f %s_encode_oer;\n", p);
OUT("oer_type_decoder_f %s_decode_oer;\n", p);
OUT("oer_type_encoder_f %s_encode_oer;\n", p);
}
if(arg->flags & A1C_GEN_PER) {
OUT("per_type_decoder_f %s_decode_uper;\n", p);
OUT("per_type_encoder_f %s_encode_uper;\n", p);
OUT("per_type_decoder_f %s_decode_uper;\n", p);
OUT("per_type_encoder_f %s_encode_uper;\n", p);
OUT("per_type_decoder_f %s_decode_aper;\n", p);
OUT("per_type_encoder_f %s_encode_aper;\n", p);
}
}

Expand Down
179 changes: 176 additions & 3 deletions skeletons/ANY.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ asn_TYPE_operation_t asn_OP_ANY = {
0,
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
0, 0,
0, 0, 0, 0,
#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 */
Expand Down Expand Up @@ -110,6 +112,37 @@ ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
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;
Expand All @@ -134,6 +167,30 @@ ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
}
}

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;
Expand Down Expand Up @@ -161,6 +218,33 @@ ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
}
}

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;

Expand Down Expand Up @@ -204,8 +288,7 @@ ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
if(!st) RETURN(RC_FAIL);
}

ASN_DEBUG("PER Decoding ANY type");

ASN_DEBUG("UPER Decoding ANY type");

st->size = 0;
do {
Expand Down Expand Up @@ -273,5 +356,95 @@ ANY_encode_uper(const asn_TYPE_descriptor_t *td,
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 */

5 changes: 5 additions & 0 deletions skeletons/ANY.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ der_type_encoder_f ANY_encode_der;
xer_type_encoder_f ANY_encode_xer;
per_type_decoder_f ANY_decode_uper;
per_type_encoder_f ANY_encode_uper;
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
Expand All @@ -44,10 +46,13 @@ per_type_encoder_f ANY_encode_uper;

/* 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);
ANY_t *ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr);

/* 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);
int ANY_to_type_aper(ANY_t *, asn_TYPE_descriptor_t *td, void **struct_ptr);

#define ANY_fromBuf(s, buf, size) OCTET_STRING_fromBuf((s), (buf), (size))
#define ANY_new_fromBuf(buf, size) OCTET_STRING_new_fromBuf( \
Expand Down
4 changes: 4 additions & 0 deletions skeletons/BIT_STRING.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ asn_TYPE_operation_t asn_OP_BIT_STRING = {
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
#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 */
Expand Down
2 changes: 2 additions & 0 deletions skeletons/BIT_STRING.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ asn_random_fill_f BIT_STRING_random_fill;
#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

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions skeletons/BMPString.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ asn_TYPE_operation_t asn_OP_BMPString = {
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
#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 */
Expand Down
2 changes: 2 additions & 0 deletions skeletons/BMPString.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ xer_type_encoder_f BMPString_encode_xer;
#define BMPString_encode_der OCTET_STRING_encode_der
#define BMPString_decode_uper OCTET_STRING_decode_uper
#define BMPString_encode_uper OCTET_STRING_encode_uper
#define BMPString_decode_aper OCTET_STRING_decode_aper
#define BMPString_encode_aper OCTET_STRING_encode_aper

#ifdef __cplusplus
}
Expand Down
57 changes: 57 additions & 0 deletions skeletons/BOOLEAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ asn_TYPE_operation_t asn_OP_BOOLEAN = {
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
0,
0,
#else
BOOLEAN_decode_uper, /* Unaligned PER decoder */
BOOLEAN_encode_uper, /* Unaligned PER encoder */
BOOLEAN_decode_aper, /* Aligned PER decoder */
BOOLEAN_encode_aper, /* Aligned PER encoder */
#endif /* ASN_DISABLE_PER_SUPPORT */
BOOLEAN_random_fill,
0 /* Use generic outmost tag fetcher */
Expand Down Expand Up @@ -310,6 +314,59 @@ BOOLEAN_encode_uper(const asn_TYPE_descriptor_t *td,
ASN__ENCODED_OK(er);
}

asn_dec_rval_t
BOOLEAN_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;
BOOLEAN_t *st = (BOOLEAN_t *)*sptr;

(void)opt_codec_ctx;
(void)constraints;
(void)td;

if(!st) {
st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
if(!st) ASN__DECODE_FAILED;
}

/*
* Extract a single bit
*/
switch(per_get_few_bits(pd, 1)) {
case 1:
*st = 1;
break;
case 0:
*st = 0;
break;
case -1:
default:
ASN__DECODE_STARVED;
}

ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");

rv.code = RC_OK;
rv.consumed = 1;
return rv;
}

asn_enc_rval_t
BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
const asn_per_constraints_t *constraints,
const void *sptr, asn_per_outp_t *po) {
const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
asn_enc_rval_t er;

(void)constraints;

if(!st) ASN__ENCODE_FAILED;

per_put_few_bits(po, *st ? 1 : 0, 1);

ASN__ENCODED_OK(er);
}

#endif /* ASN_DISABLE_PER_SUPPORT */

#ifndef ASN_DISABLE_OER_SUPPORT
Expand Down
2 changes: 2 additions & 0 deletions skeletons/BOOLEAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ oer_type_decoder_f BOOLEAN_decode_oer;
oer_type_encoder_f BOOLEAN_encode_oer;
per_type_decoder_f BOOLEAN_decode_uper;
per_type_encoder_f BOOLEAN_encode_uper;
per_type_decoder_f BOOLEAN_decode_aper;
per_type_encoder_f BOOLEAN_encode_aper;
xer_type_decoder_f BOOLEAN_decode_xer;
xer_type_encoder_f BOOLEAN_encode_xer;
asn_random_fill_f BOOLEAN_random_fill;
Expand Down
Loading