Skip to content

Commit

Permalink
Correct APER processing (somehow #125 appeared to be applied incomple…
Browse files Browse the repository at this point in the history
…te?!)
  • Loading branch information
Uri Blumenthal committed Mar 27, 2017
1 parent b5424fa commit d624968
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 30 deletions.
25 changes: 23 additions & 2 deletions asn1c/unber.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,30 @@ OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts,
return er;
}

asn_dec_rval_t OCTET_STRING_decode_aper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv = { 0, 0 }; (void)ctx; (void)td; (void)cts; (void)sptr; (void)pd; return rv; }
asn_dec_rval_t
OCTET_STRING_decode_aper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td,
asn_per_constraints_t *cts, void **sptr,
asn_per_data_t *pd)
{
asn_dec_rval_t rv = { 0, 0 };
(void)ctx;
(void)td;
(void)cts;
(void)sptr;
(void)pd;
return rv;
}

asn_enc_rval_t OCTET_STRING_encode_aper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void *sptr, asn_per_outp_t *po) { asn_enc_rval_t er = { 0, 0, 0 }; (void)td; (void)cts; (void)sptr; (void)po; return er; }
asn_enc_rval_t
OCTET_STRING_encode_aper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts,
void *sptr, asn_per_outp_t *po) {
asn_enc_rval_t er = { 0, 0, 0 };
(void)td;
(void)cts;
(void)sptr;
(void)po;
return er;
}

size_t
xer_whitespace_span(const void *chunk_buf, size_t chunk_size) {
Expand Down
8 changes: 6 additions & 2 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,10 +1358,14 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
OUT("\n");

p = MKID(expr);
if(HIDE_INNER_DEFS) OUT("static ");
if(HIDE_INNER_DEFS) {
OUT("static ");
}
OUT("asn_enc_rval_t\n");
OUT("%s", p);
if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
if(HIDE_INNER_DEFS) {
OUT("_%d", expr->_type_unique_index);
}
OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
INDENTED(
OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
Expand Down
63 changes: 52 additions & 11 deletions skeletons/converter-sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ static int opt_onepdu; /* -1 (decode single PDU) */
static enum input_format {
INP_BER, /* -iber: BER input */
INP_XER, /* -ixer: XER input */
INP_PER /* -iper: Unaligned PER input */
INP_PER, /* -iper: Unaligned PER input */
INP_APER /* -iaper: Aligned PER input */
} iform; /* -i<format> */

/* Output data format selector */
static enum output_format {
OUT_XER, /* -oxer: XER (XML) output */
OUT_DER, /* -oder: DER (BER) output */
OUT_PER, /* -oper: Unaligned PER output */
OUT_APER, /* -oaper: Aligned PER output */
OUT_TEXT, /* -otext: semi-structured text */
OUT_NULL /* -onull: No pretty-printing */
} oform; /* -o<format> */
Expand Down Expand Up @@ -105,13 +107,17 @@ main(int ac, char *av[]) {
if(optarg[0] == 'x') { iform = INP_XER; break; }
if(pduType->uper_decoder
&& optarg[0] == 'p') { iform = INP_PER; break; }
if(pduType->aper_decoder
&& optarg[0] == 'a') { iform = INP_APER; break; }
fprintf(stderr, "-i<format>: '%s': improper format selector\n",
optarg);
exit(EX_UNAVAILABLE);
case 'o':
if(optarg[0] == 'd') { oform = OUT_DER; break; }
if(pduType->uper_encoder
&& optarg[0] == 'p') { oform = OUT_PER; break; }
if(pduType->aper_encoder
&& optarg[0] == 'a') { oform = OUT_APER; break; }
if(optarg[0] == 'x') { oform = OUT_XER; break; }
if(optarg[0] == 't') { oform = OUT_TEXT; break; }
if(optarg[0] == 'n') { oform = OUT_NULL; break; }
Expand Down Expand Up @@ -195,21 +201,27 @@ main(int ac, char *av[]) {
fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
fprintf(stderr, "Where options are:\n");
if(pduType->uper_decoder)
fprintf(stderr,
" -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
if(pduType->aper_decoder)
fprintf(stderr,
" -iaper Input is in Aligned PER (Packed Encoding Rules)\n");
fprintf(stderr,
" -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
fprintf(stderr,
" -iber Input is in BER (Basic Encoding Rules)%s\n",
" -iber Input is in BER (Basic Encoding Rules)%s\n",
iform == INP_PER ? "" : " (DEFAULT)");
fprintf(stderr,
" -ixer Input is in XER (XML Encoding Rules)\n");
" -ixer Input is in XER (XML Encoding Rules)\n");
if(pduType->uper_encoder)
fprintf(stderr,
" -oper Output in Unaligned PER (Packed Encoding Rules)\n");
if(pduType->aper_encoder)
fprintf(stderr,
" -oaper Output in Aligned PER (Packed Encoding Rules)\n");
fprintf(stderr,
" -oper Output in Unaligned PER (Packed Encoding Rules)\n");
fprintf(stderr,
" -oder Output in DER (Distinguished Encoding Rules)\n"
" -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
" -otext Output in plain semi-structured text (dump)\n"
" -onull Verify (decode) input, but do not output\n");
" -oder Output in DER (Distinguished Encoding Rules)\n"
" -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
" -otext Output in plain semi-structured text (dump)\n"
" -onull Verify (decode) input, but do not output\n");
if(pduType->uper_decoder)
fprintf(stderr,
" -per-nopad Assume PER PDUs are not padded (-iper)\n");
Expand Down Expand Up @@ -322,8 +334,20 @@ main(int ac, char *av[]) {
}
DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
break;
case OUT_APER:
erv = aper_encode(pduType, structure, write_out, stdout);
if(erv.encoded < 0) {
fprintf(stderr,
"%s: Cannot convert %s into Aligned PER\n",
name, pduType->name);
exit(EX_UNAVAILABLE);
}
DEBUG("Encoded in %ld bits of APER", (long)erv.encoded);
break;

}


ASN_STRUCT_FREE(*pduType, structure);
}

Expand Down Expand Up @@ -664,6 +688,23 @@ data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *na
break;
}
break;
case INP_APER:
rval = aper_decode_complete(opt_codec_ctx, pduType,
(void **)&structure, i_bptr, i_size);
switch(rval.code) {
case RC_OK:
/* Fall through */
case RC_FAIL:
break;
case RC_WMORE:
/* PER does not support restartability */
ASN_STRUCT_FREE(*pduType, structure);
structure = 0;
rval.consumed = 0;
/* Continue accumulating data */
break;
}
break;
}
DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
(long)DynamicBuffer.length,
Expand Down
25 changes: 13 additions & 12 deletions skeletons/per_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,28 @@ asn_dec_rval_t uper_decode(struct asn_codec_ctx_s *opt_codec_ctx,

/*
* Aligned PER decoder of a "complete encoding" as per X.691#10.1.
* On success, this call always returns (.consumed >= 1), as per X.691#10.1.3.
* On success, this call always returns (.consumed >= 1), in BITS, as per X.691#10.1.3.
*/
asn_dec_rval_t aper_decode_complete(struct asn_codec_ctx_s *opt_codec_ctx,
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 */
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(struct asn_codec_ctx_s *opt_codec_ctx,
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 */
);
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.
*/
Expand Down
6 changes: 6 additions & 0 deletions skeletons/per_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
static asn_enc_rval_t uper_encode_internal(asn_TYPE_descriptor_t *td, asn_per_constraints_t *, void *sptr, asn_app_consume_bytes_f *cb, void *app_key);

static asn_enc_rval_t aper_encode_internal(asn_TYPE_descriptor_t *td, asn_per_constraints_t *, void *sptr, asn_app_consume_bytes_f *cb, void *app_key);

asn_enc_rval_t
uper_encode(asn_TYPE_descriptor_t *td, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
return uper_encode_internal(td, 0, sptr, cb, app_key);
}

asn_enc_rval_t
aper_encode(asn_TYPE_descriptor_t *td, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
return aper_encode_internal(td, 0, sptr, cb, app_key);
}

/*
* Argument type and callback necessary for uper_encode_to_buffer().
*/
Expand Down
14 changes: 11 additions & 3 deletions skeletons/per_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ asn_enc_rval_t uper_encode(struct asn_TYPE_descriptor_s *type_descriptor,
void *app_key /* Arbitrary callback argument */
);

asn_enc_rval_t aper_encode(struct asn_TYPE_descriptor_s *type_descriptor,
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
Expand Down Expand Up @@ -60,9 +66,11 @@ ssize_t uper_encode_to_new_buffer(

ssize_t
aper_encode_to_new_buffer(struct asn_TYPE_descriptor_s *td,
asn_per_constraints_t *constraints,
void *sptr,
void **buffer_r);
asn_per_constraints_t *constraints,
void *sptr,
void **buffer_r
);

/*
* Type of the generic PER encoder function.
*/
Expand Down

0 comments on commit d624968

Please sign in to comment.