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

get rid of zer_* in favor of generic asn_encoder and asn_decoder

parent a75b247f
...@@ -56,59 +56,33 @@ _buf_writer(const void *buffer, size_t size, void *app_key) { ...@@ -56,59 +56,33 @@ _buf_writer(const void *buffer, size_t size, void *app_key) {
return 0; return 0;
} }
enum der_or_xer {
AS_DER,
AS_XER,
AS_CXER,
};
static void static void
save_object_as(PDU_t *st, enum der_or_xer how) { save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
asn_enc_rval_t rval; /* Return value */ asn_enc_rval_t rval; /* Return value */
buf_offset = 0; buf_offset = 0;
/* rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
* Save object using specified method.
*/
switch(how) {
case AS_DER:
rval = der_encode(&asn_DEF_PDU, st,
_buf_writer, 0);
break;
case AS_XER:
rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
_buf_writer, 0);
break;
case AS_CXER:
rval = xer_encode(&asn_DEF_PDU, st, XER_F_CANONICAL,
_buf_writer, 0);
break;
}
if (rval.encoded == -1) {
fprintf(stderr,
"Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1);
return;
}
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); if (rval.encoded == -1) {
fprintf(stderr,
"Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1);
return;
}
fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zd\n", buf_offset, rval.encoded);
// assert(buf_offset == rval.encoded);
} }
static PDU_t * static PDU_t *
load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum der_or_xer how) { load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
asn_dec_rval_t rval; asn_dec_rval_t rval;
asn_dec_rval_t (*zer_decode)(struct asn_codec_ctx_s *,
asn_TYPE_descriptor_t *, void **, const void *, size_t);
PDU_t *st = 0; PDU_t *st = 0;
size_t csize = 1; size_t csize = 1;
if(how == AS_DER)
zer_decode = ber_decode;
else
zer_decode = xer_decode;
if(getenv("INITIAL_CHUNK_SIZE")) if(getenv("INITIAL_CHUNK_SIZE"))
csize = atoi(getenv("INITIAL_CHUNK_SIZE")); csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
...@@ -137,7 +111,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, ...@@ -137,7 +111,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size,
fprintf(stderr, "=== end ===\n"); fprintf(stderr, "=== end ===\n");
} }
#endif #endif
rval = zer_decode(0, &asn_DEF_PDU, (void **)&st, rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
fbuf + fbuf_offset, fbuf + fbuf_offset,
fbuf_chunk < fbuf_left fbuf_chunk < fbuf_left
? fbuf_chunk : fbuf_left); ? fbuf_chunk : fbuf_left);
...@@ -151,7 +125,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, ...@@ -151,7 +125,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size,
if(expectation != EXP_BROKEN) { if(expectation != EXP_BROKEN) {
assert(rval.code == RC_OK); assert(rval.code == RC_OK);
if(how == AS_DER) { if(syntax == ATS_BER) {
assert(fbuf_offset == (ssize_t)size); assert(fbuf_offset == (ssize_t)size);
} else { } else {
assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
...@@ -211,18 +185,18 @@ static void ...@@ -211,18 +185,18 @@ static void
process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) { process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
PDU_t *st; PDU_t *st;
st = load_object_from(expectation, fbuf, size, AS_XER); st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
if(!st) return; if(!st) return;
/* Save and re-load as DER */ /* Save and re-load as DER */
save_object_as(st, AS_DER); save_object_as(st, ATS_DER);
st = load_object_from(expectation, buf, buf_offset, AS_DER); st = load_object_from(expectation, buf, buf_offset, ATS_BER);
assert(st); assert(st);
save_object_as(st, save_object_as(st,
(expectation == EXP_CXER_EXACT (expectation == EXP_CXER_EXACT
|| expectation == EXP_CXER_DIFF) || expectation == EXP_CXER_DIFF)
? AS_CXER : AS_XER); ? ATS_CANONICAL_XER : ATS_BASIC_XER);
fprintf(stderr, "=== original ===\n"); fprintf(stderr, "=== original ===\n");
fwrite(fbuf, 1, size, stderr); fwrite(fbuf, 1, size, stderr);
fprintf(stderr, "=== re-encoded ===\n"); fprintf(stderr, "=== re-encoded ===\n");
......
...@@ -50,54 +50,33 @@ _buf_writer(const void *buffer, size_t size, void *app_key) { ...@@ -50,54 +50,33 @@ _buf_writer(const void *buffer, size_t size, void *app_key) {
return 0; return 0;
} }
enum der_or_xer {
AS_DER,
AS_XER,
};
static void static void
save_object_as(PDU_t *st, enum der_or_xer how) { save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
asn_enc_rval_t rval; /* Return value */ asn_enc_rval_t rval; /* Return value */
buf_offset = 0; buf_offset = 0;
/* rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
* Save object using specified method.
*/ if (rval.encoded == -1) {
switch(how) {
case AS_DER:
rval = der_encode(&asn_DEF_PDU, st,
_buf_writer, 0);
break;
case AS_XER:
rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
_buf_writer, 0);
break;
}
if (rval.encoded == -1) {
fprintf(stderr, fprintf(stderr,
"Cannot encode %s: %s\n", "Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno)); rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1); assert(rval.encoded != -1);
return; return;
} }
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
// assert(buf_offset == rval.encoded);
} }
static PDU_t * static PDU_t *
load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum der_or_xer how) { load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
asn_dec_rval_t rval; asn_dec_rval_t rval;
asn_dec_rval_t (*zer_decode)(struct asn_codec_ctx_s *,
asn_TYPE_descriptor_t *, void **, const void *, size_t);
PDU_t *st = 0; PDU_t *st = 0;
size_t csize = 1; size_t csize = 1;
if(how == AS_DER)
zer_decode = ber_decode;
else
zer_decode = xer_decode;
if(getenv("INITIAL_CHUNK_SIZE")) if(getenv("INITIAL_CHUNK_SIZE"))
csize = atoi(getenv("INITIAL_CHUNK_SIZE")); csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
...@@ -126,7 +105,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, ...@@ -126,7 +105,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size,
fprintf(stderr, "=== end ===\n"); fprintf(stderr, "=== end ===\n");
} }
#endif #endif
rval = zer_decode(0, &asn_DEF_PDU, (void **)&st, rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
fbuf + fbuf_offset, fbuf + fbuf_offset,
fbuf_chunk < fbuf_left fbuf_chunk < fbuf_left
? fbuf_chunk : fbuf_left); ? fbuf_chunk : fbuf_left);
...@@ -140,7 +119,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, ...@@ -140,7 +119,7 @@ load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size,
if(expectation != EXP_BROKEN) { if(expectation != EXP_BROKEN) {
assert(rval.code == RC_OK); assert(rval.code == RC_OK);
if(how == AS_DER) { if(syntax == ATS_BER) {
assert(fbuf_offset == (ssize_t)size); assert(fbuf_offset == (ssize_t)size);
} else { } else {
assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
...@@ -200,15 +179,15 @@ static void ...@@ -200,15 +179,15 @@ static void
process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) { process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
PDU_t *st; PDU_t *st;
st = load_object_from(expectation, fbuf, size, AS_XER); st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
if(!st) return; if(!st) return;
/* Save and re-load as DER */ /* Save and re-load as DER */
save_object_as(st, AS_DER); save_object_as(st, ATS_DER);
st = load_object_from(expectation, buf, buf_offset, AS_DER); st = load_object_from(expectation, buf, buf_offset, ATS_BER);
assert(st); assert(st);
save_object_as(st, AS_XER); save_object_as(st, ATS_BASIC_XER);
fprintf(stderr, "=== original ===\n"); fprintf(stderr, "=== original ===\n");
fwrite(fbuf, 1, size, stderr); fwrite(fbuf, 1, size, stderr);
fprintf(stderr, "=== re-encoded ===\n"); fprintf(stderr, "=== re-encoded ===\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment