mirror of https://gerrit.osmocom.org/asn1c
more PER support
parent
7cbbc90647
commit
725883b28d
|
@ -1,7 +1,12 @@
|
|||
|
||||
0.9.22: 2006-Sep-22
|
||||
0.9.22: 2006-Oct-08
|
||||
|
||||
* Added -pdu=all and -pdu=<type> switches to asn1c.
|
||||
* Added PER support for most known-multiplier string types:
|
||||
IA5String, VisibleString, PrintableString;
|
||||
useful types: GeneralizedTime, UTCTime, ObjectDescriptor;
|
||||
as well as REAL and OBJECT IDENTIFIER.
|
||||
TODO: SET, UniversalString and BMPString.
|
||||
|
||||
0.9.21: 2006-Sep-17
|
||||
|
||||
|
|
|
@ -11,7 +11,8 @@ EXTRA_DIST = \
|
|||
$(check_SCRIPTS) \
|
||||
check-*.c* \
|
||||
data-62 \
|
||||
data-70
|
||||
data-70 \
|
||||
data-119
|
||||
|
||||
dist-hook:
|
||||
rm -rf `find $(distdir) -name CVS -or -name .cvsignore`
|
||||
|
|
|
@ -165,7 +165,8 @@ EXTRA_DIST = \
|
|||
$(check_SCRIPTS) \
|
||||
check-*.c* \
|
||||
data-62 \
|
||||
data-70
|
||||
data-70 \
|
||||
data-119
|
||||
|
||||
all: all-am
|
||||
|
||||
|
|
|
@ -0,0 +1,363 @@
|
|||
/*
|
||||
* Mode of operation:
|
||||
* Each of the *.in files is XER-decoded, then converted into DER,
|
||||
* then decoded from DER and encoded into XER again. The resulting
|
||||
* stream is compared with the corresponding .out file.
|
||||
*/
|
||||
#undef NDEBUG
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h> /* for chdir(2) */
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <PDU.h>
|
||||
|
||||
enum expectation {
|
||||
EXP_OK, /* Encoding/decoding must succeed */
|
||||
EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
|
||||
EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
|
||||
EXP_BROKEN, /* Decoding must fail */
|
||||
EXP_DIFFERENT, /* Reconstruction will yield different encoding */
|
||||
EXP_PER_NOCOMP, /* Not PER compatible */
|
||||
};
|
||||
|
||||
static unsigned char buf[4096];
|
||||
static int buf_offset;
|
||||
|
||||
static int
|
||||
_buf_writer(const void *buffer, size_t size, void *app_key) {
|
||||
unsigned char *b, *bend;
|
||||
(void)app_key;
|
||||
assert(buf_offset + size < sizeof(buf));
|
||||
memcpy(buf + buf_offset, buffer, size);
|
||||
b = buf + buf_offset;
|
||||
bend = b + size;
|
||||
fprintf(stderr, "=> [");
|
||||
for(; b < bend; b++) {
|
||||
if(*b >= 32 && *b < 127 && *b != '%')
|
||||
fprintf(stderr, "%c", *b);
|
||||
else
|
||||
fprintf(stderr, "%%%02x", *b);
|
||||
}
|
||||
fprintf(stderr, "]:%ld\n", (long)size);
|
||||
buf_offset += size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum enctype {
|
||||
AS_PER,
|
||||
AS_DER,
|
||||
AS_XER,
|
||||
AS_CXER,
|
||||
};
|
||||
|
||||
static void
|
||||
save_object_as(PDU_t *st, enum expectation exp, enum enctype how) {
|
||||
asn_enc_rval_t rval; /* Return value */
|
||||
|
||||
buf_offset = 0;
|
||||
|
||||
/*
|
||||
* Save object using specified method.
|
||||
*/
|
||||
switch(how) {
|
||||
case AS_PER:
|
||||
rval = uper_encode(&asn_DEF_PDU, st,
|
||||
_buf_writer, 0);
|
||||
if(exp == EXP_PER_NOCOMP)
|
||||
assert(rval.encoded == -1);
|
||||
else
|
||||
assert(rval.encoded > 0);
|
||||
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
|
||||
return;
|
||||
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);
|
||||
}
|
||||
|
||||
static PDU_t *
|
||||
load_object_from(const char *fname, enum expectation expectation, char *fbuf, int size, enum enctype how) {
|
||||
asn_dec_rval_t rval;
|
||||
PDU_t *st = 0;
|
||||
int csize = 1;
|
||||
|
||||
if(getenv("INITIAL_CHUNK_SIZE"))
|
||||
csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
|
||||
|
||||
/* Perform multiple iterations with multiple chunks sizes */
|
||||
for(; csize < 20; csize += 1) {
|
||||
int fbuf_offset = 0;
|
||||
int fbuf_left = size;
|
||||
int fbuf_chunk = csize;
|
||||
|
||||
fprintf(stderr, "LOADING OBJECT OF SIZE %d FROM [%s] as %s,"
|
||||
" chunks %d\n",
|
||||
size, fname, how==AS_PER?"PER":"XER", csize);
|
||||
|
||||
if(st) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
st = 0;
|
||||
|
||||
do {
|
||||
fprintf(stderr, "Decoding bytes %d..%d (left %d)\n",
|
||||
fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left,
|
||||
fbuf_left);
|
||||
if(st) {
|
||||
fprintf(stderr, "=== currently ===\n");
|
||||
asn_fprint(stderr, &asn_DEF_PDU, st);
|
||||
fprintf(stderr, "=== end ===\n");
|
||||
}
|
||||
switch(how) {
|
||||
case AS_XER:
|
||||
rval = xer_decode(0, &asn_DEF_PDU, (void **)&st,
|
||||
fbuf + fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left);
|
||||
break;
|
||||
case AS_PER:
|
||||
rval = uper_decode(0, &asn_DEF_PDU,
|
||||
(void **)&st, fbuf + fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left, 0, 0);
|
||||
if(rval.code == RC_WMORE) {
|
||||
rval.consumed = 0; /* Not restartable */
|
||||
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
||||
st = 0;
|
||||
fprintf(stderr, "-> PER wants more\n");
|
||||
} else {
|
||||
fprintf(stderr, "-> PER ret %d/%d\n",
|
||||
rval.code, rval.consumed);
|
||||
/* uper_decode() returns _bits_ */
|
||||
rval.consumed += 7;
|
||||
rval.consumed /= 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
fbuf_offset += rval.consumed;
|
||||
fbuf_left -= rval.consumed;
|
||||
if(rval.code == RC_WMORE)
|
||||
fbuf_chunk += 1; /* Give little more */
|
||||
else
|
||||
fbuf_chunk = csize; /* Back off */
|
||||
} while(fbuf_left && rval.code == RC_WMORE);
|
||||
|
||||
if(expectation != EXP_BROKEN) {
|
||||
assert(rval.code == RC_OK);
|
||||
if(how == AS_PER) {
|
||||
fprintf(stderr, "[left %d, off %d, size %d]\n",
|
||||
fbuf_left, fbuf_offset, size);
|
||||
assert(fbuf_offset == size);
|
||||
} else {
|
||||
assert(fbuf_offset - size < 2
|
||||
|| (fbuf_offset + 1 /* "\n" */ == size
|
||||
&& fbuf[size - 1] == '\n')
|
||||
|| (fbuf_offset + 2 /* "\r\n" */ == size
|
||||
&& fbuf[size - 2] == '\r'
|
||||
&& fbuf[size - 1] == '\n')
|
||||
);
|
||||
}
|
||||
} else {
|
||||
assert(rval.code != RC_OK);
|
||||
fprintf(stderr, "Failed, but this was expected\n");
|
||||
asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
st = 0; /* ignore leak for now */
|
||||
}
|
||||
}
|
||||
|
||||
if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
|
||||
return st;
|
||||
}
|
||||
|
||||
static int
|
||||
xer_encoding_equal(char *obuf, size_t osize, char *nbuf, size_t nsize) {
|
||||
char *oend = obuf + osize;
|
||||
char *nend = nbuf + nsize;
|
||||
|
||||
if((osize && !nsize) || (!osize && nsize))
|
||||
return 0; /* not equal apriori */
|
||||
|
||||
while(1) {
|
||||
while(obuf < oend && isspace(*obuf)) obuf++;
|
||||
while(nbuf < nend && isspace(*nbuf)) nbuf++;
|
||||
|
||||
if(obuf == oend || nbuf == nend) {
|
||||
if(obuf == oend && nbuf == nend)
|
||||
break;
|
||||
fprintf(stderr, "%s data in reconstructed encoding\n",
|
||||
(obuf == oend) ? "More" : "Less");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(*obuf != *nbuf) {
|
||||
printf("%c%c != %c%c\n",
|
||||
obuf[0], obuf[1],
|
||||
nbuf[0], nbuf[1]);
|
||||
return 0;
|
||||
}
|
||||
obuf++, nbuf++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
process_XER_data(const char *fname, enum expectation expectation, char *fbuf, int size) {
|
||||
PDU_t *st;
|
||||
int ret;
|
||||
|
||||
st = load_object_from(fname, expectation, fbuf, size, AS_XER);
|
||||
if(!st) return;
|
||||
|
||||
/* Save and re-load as DER */
|
||||
save_object_as(st, expectation, AS_PER);
|
||||
if(expectation == EXP_PER_NOCOMP)
|
||||
return; /* Already checked */
|
||||
st = load_object_from("buffer", expectation, buf, buf_offset, AS_PER);
|
||||
assert(st);
|
||||
|
||||
save_object_as(st,
|
||||
expectation,
|
||||
(expectation == EXP_CXER_EXACT
|
||||
|| expectation == EXP_CXER_DIFF)
|
||||
? AS_CXER : AS_XER);
|
||||
fprintf(stderr, "=== original ===\n");
|
||||
fwrite(fbuf, 1, size, stderr);
|
||||
fprintf(stderr, "=== re-encoded ===\n");
|
||||
fwrite(buf, 1, buf_offset, stderr);
|
||||
fprintf(stderr, "=== end ===\n");
|
||||
|
||||
switch(expectation) {
|
||||
case EXP_DIFFERENT:
|
||||
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
case EXP_BROKEN:
|
||||
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
case EXP_CXER_EXACT:
|
||||
buf[buf_offset++] = '\n';
|
||||
assert(size == buf_offset);
|
||||
assert(memcmp(fbuf, buf, size) == 0);
|
||||
break;
|
||||
case EXP_CXER_DIFF:
|
||||
buf[buf_offset++] = '\n';
|
||||
assert(size != buf_offset
|
||||
|| memcmp(fbuf, buf, size));
|
||||
break;
|
||||
case EXP_OK:
|
||||
case EXP_PER_NOCOMP:
|
||||
assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
}
|
||||
|
||||
asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Decode the .der files and try to regenerate them.
|
||||
*/
|
||||
static int
|
||||
process(const char *fname) {
|
||||
char fbuf[4096];
|
||||
char *ext = strrchr(fname, '.');
|
||||
enum expectation expectation;
|
||||
int ret;
|
||||
int rd;
|
||||
FILE *fp;
|
||||
|
||||
if(ext == 0 || strcmp(ext, ".in"))
|
||||
return 0;
|
||||
|
||||
switch(ext[-1]) {
|
||||
case 'B': /* The file is intentionally broken */
|
||||
expectation = EXP_BROKEN; break;
|
||||
case 'D': /* Reconstructing should yield different data */
|
||||
expectation = EXP_DIFFERENT; break;
|
||||
case 'E': /* Byte to byte exact reconstruction */
|
||||
expectation = EXP_CXER_EXACT; break;
|
||||
case 'X': /* Should fail byte-to-byte comparison */
|
||||
expectation = EXP_CXER_DIFF; break;
|
||||
case 'P': /* Incompatible with PER */
|
||||
expectation = EXP_PER_NOCOMP; break;
|
||||
default:
|
||||
expectation = EXP_OK; break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "\nProcessing file [../%s]\n", fname);
|
||||
|
||||
ret = chdir("../data-119");
|
||||
assert(ret == 0);
|
||||
fp = fopen(fname, "r");
|
||||
ret = chdir("../test-check-119.-gen-PER");
|
||||
assert(ret == 0);
|
||||
assert(fp);
|
||||
|
||||
rd = fread(fbuf, 1, sizeof(fbuf), fp);
|
||||
fclose(fp);
|
||||
|
||||
assert(rd < sizeof(fbuf)); /* expect small files */
|
||||
|
||||
process_XER_data(fname, expectation, fbuf, rd);
|
||||
|
||||
fprintf(stderr, "Finished [%s]\n", fname);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main() {
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
int processed_files = 0;
|
||||
char *str;
|
||||
|
||||
/* Process a specific test file */
|
||||
str = getenv("DATA_119_FILE");
|
||||
if(str && strncmp(str, "data-119-", 9) == 0) {
|
||||
process(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dir = opendir("../data-119");
|
||||
assert(dir);
|
||||
|
||||
/*
|
||||
* Process each file in that directory.
|
||||
*/
|
||||
while((dent = readdir(dir))) {
|
||||
if(strncmp(dent->d_name, "data-119-", 9) == 0)
|
||||
if(process(dent->d_name))
|
||||
processed_files++;
|
||||
}
|
||||
|
||||
assert(processed_files);
|
||||
closedir(dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,363 @@
|
|||
/*
|
||||
* Mode of operation:
|
||||
* Each of the *.in files is XER-decoded, then converted into DER,
|
||||
* then decoded from DER and encoded into XER again. The resulting
|
||||
* stream is compared with the corresponding .out file.
|
||||
*/
|
||||
#undef NDEBUG
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h> /* for chdir(2) */
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <PDU.h>
|
||||
|
||||
enum expectation {
|
||||
EXP_OK, /* Encoding/decoding must succeed */
|
||||
EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
|
||||
EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
|
||||
EXP_BROKEN, /* Decoding must fail */
|
||||
EXP_DIFFERENT, /* Reconstruction will yield different encoding */
|
||||
EXP_PER_NOCOMP, /* Not PER compatible */
|
||||
};
|
||||
|
||||
static unsigned char buf[4096];
|
||||
static int buf_offset;
|
||||
|
||||
static int
|
||||
_buf_writer(const void *buffer, size_t size, void *app_key) {
|
||||
unsigned char *b, *bend;
|
||||
(void)app_key;
|
||||
assert(buf_offset + size < sizeof(buf));
|
||||
memcpy(buf + buf_offset, buffer, size);
|
||||
b = buf + buf_offset;
|
||||
bend = b + size;
|
||||
fprintf(stderr, "=> [");
|
||||
for(; b < bend; b++) {
|
||||
if(*b >= 32 && *b < 127 && *b != '%')
|
||||
fprintf(stderr, "%c", *b);
|
||||
else
|
||||
fprintf(stderr, "%%%02x", *b);
|
||||
}
|
||||
fprintf(stderr, "]:%ld\n", (long)size);
|
||||
buf_offset += size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum enctype {
|
||||
AS_PER,
|
||||
AS_DER,
|
||||
AS_XER,
|
||||
AS_CXER,
|
||||
};
|
||||
|
||||
static void
|
||||
save_object_as(PDU_t *st, enum expectation exp, enum enctype how) {
|
||||
asn_enc_rval_t rval; /* Return value */
|
||||
|
||||
buf_offset = 0;
|
||||
|
||||
/*
|
||||
* Save object using specified method.
|
||||
*/
|
||||
switch(how) {
|
||||
case AS_PER:
|
||||
rval = uper_encode(&asn_DEF_PDU, st,
|
||||
_buf_writer, 0);
|
||||
if(exp == EXP_PER_NOCOMP)
|
||||
assert(rval.encoded == -1);
|
||||
else
|
||||
assert(rval.encoded > 0);
|
||||
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
|
||||
return;
|
||||
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);
|
||||
}
|
||||
|
||||
static PDU_t *
|
||||
load_object_from(const char *fname, enum expectation expectation, char *fbuf, int size, enum enctype how) {
|
||||
asn_dec_rval_t rval;
|
||||
PDU_t *st = 0;
|
||||
int csize = 1;
|
||||
|
||||
if(getenv("INITIAL_CHUNK_SIZE"))
|
||||
csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
|
||||
|
||||
/* Perform multiple iterations with multiple chunks sizes */
|
||||
for(; csize < 20; csize += 1) {
|
||||
int fbuf_offset = 0;
|
||||
int fbuf_left = size;
|
||||
int fbuf_chunk = csize;
|
||||
|
||||
fprintf(stderr, "LOADING OBJECT OF SIZE %d FROM [%s] as %s,"
|
||||
" chunks %d\n",
|
||||
size, fname, how==AS_PER?"PER":"XER", csize);
|
||||
|
||||
if(st) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
st = 0;
|
||||
|
||||
do {
|
||||
fprintf(stderr, "Decoding bytes %d..%d (left %d)\n",
|
||||
fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left,
|
||||
fbuf_left);
|
||||
if(st) {
|
||||
fprintf(stderr, "=== currently ===\n");
|
||||
asn_fprint(stderr, &asn_DEF_PDU, st);
|
||||
fprintf(stderr, "=== end ===\n");
|
||||
}
|
||||
switch(how) {
|
||||
case AS_XER:
|
||||
rval = xer_decode(0, &asn_DEF_PDU, (void **)&st,
|
||||
fbuf + fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left);
|
||||
break;
|
||||
case AS_PER:
|
||||
rval = uper_decode(0, &asn_DEF_PDU,
|
||||
(void **)&st, fbuf + fbuf_offset,
|
||||
fbuf_chunk < fbuf_left
|
||||
? fbuf_chunk : fbuf_left, 0, 0);
|
||||
if(rval.code == RC_WMORE) {
|
||||
rval.consumed = 0; /* Not restartable */
|
||||
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
||||
st = 0;
|
||||
fprintf(stderr, "-> PER wants more\n");
|
||||
} else {
|
||||
fprintf(stderr, "-> PER ret %d/%d\n",
|
||||
rval.code, rval.consumed);
|
||||
/* uper_decode() returns _bits_ */
|
||||
rval.consumed += 7;
|
||||
rval.consumed /= 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
fbuf_offset += rval.consumed;
|
||||
fbuf_left -= rval.consumed;
|
||||
if(rval.code == RC_WMORE)
|
||||
fbuf_chunk += 1; /* Give little more */
|
||||
else
|
||||
fbuf_chunk = csize; /* Back off */
|
||||
} while(fbuf_left && rval.code == RC_WMORE);
|
||||
|
||||
if(expectation != EXP_BROKEN) {
|
||||
assert(rval.code == RC_OK);
|
||||
if(how == AS_PER) {
|
||||
fprintf(stderr, "[left %d, off %d, size %d]\n",
|
||||
fbuf_left, fbuf_offset, size);
|
||||
assert(fbuf_offset == size);
|
||||
} else {
|
||||
assert(fbuf_offset - size < 2
|
||||
|| (fbuf_offset + 1 /* "\n" */ == size
|
||||
&& fbuf[size - 1] == '\n')
|
||||
|| (fbuf_offset + 2 /* "\r\n" */ == size
|
||||
&& fbuf[size - 2] == '\r'
|
||||
&& fbuf[size - 1] == '\n')
|
||||
);
|
||||
}
|
||||
} else {
|
||||
assert(rval.code != RC_OK);
|
||||
fprintf(stderr, "Failed, but this was expected\n");
|
||||
asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
st = 0; /* ignore leak for now */
|
||||
}
|
||||
}
|
||||
|
||||
if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
|
||||
return st;
|
||||
}
|
||||
|
||||
static int
|
||||
xer_encoding_equal(char *obuf, size_t osize, char *nbuf, size_t nsize) {
|
||||
char *oend = obuf + osize;
|
||||
char *nend = nbuf + nsize;
|
||||
|
||||
if((osize && !nsize) || (!osize && nsize))
|
||||
return 0; /* not equal apriori */
|
||||
|
||||
while(1) {
|
||||
while(obuf < oend && isspace(*obuf)) obuf++;
|
||||
while(nbuf < nend && isspace(*nbuf)) nbuf++;
|
||||
|
||||
if(obuf == oend || nbuf == nend) {
|
||||
if(obuf == oend && nbuf == nend)
|
||||
break;
|
||||
fprintf(stderr, "%s data in reconstructed encoding\n",
|
||||
(obuf == oend) ? "More" : "Less");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(*obuf != *nbuf) {
|
||||
printf("%c%c != %c%c\n",
|
||||
obuf[0], obuf[1],
|
||||
nbuf[0], nbuf[1]);
|
||||
return 0;
|
||||
}
|
||||
obuf++, nbuf++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
process_XER_data(const char *fname, enum expectation expectation, char *fbuf, int size) {
|
||||
PDU_t *st;
|
||||
int ret;
|
||||
|
||||
st = load_object_from(fname, expectation, fbuf, size, AS_XER);
|
||||
if(!st) return;
|
||||
|
||||
/* Save and re-load as DER */
|
||||
save_object_as(st, expectation, AS_PER);
|
||||
if(expectation == EXP_PER_NOCOMP)
|
||||
return; /* Already checked */
|
||||
st = load_object_from("buffer", expectation, buf, buf_offset, AS_PER);
|
||||
assert(st);
|
||||
|
||||
save_object_as(st,
|
||||
expectation,
|
||||
(expectation == EXP_CXER_EXACT
|
||||
|| expectation == EXP_CXER_DIFF)
|
||||
? AS_CXER : AS_XER);
|
||||
fprintf(stderr, "=== original ===\n");
|
||||
fwrite(fbuf, 1, size, stderr);
|
||||
fprintf(stderr, "=== re-encoded ===\n");
|
||||
fwrite(buf, 1, buf_offset, stderr);
|
||||
fprintf(stderr, "=== end ===\n");
|
||||
|
||||
switch(expectation) {
|
||||
case EXP_DIFFERENT:
|
||||
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
case EXP_BROKEN:
|
||||
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
case EXP_CXER_EXACT:
|
||||
buf[buf_offset++] = '\n';
|
||||
assert(size == buf_offset);
|
||||
assert(memcmp(fbuf, buf, size) == 0);
|
||||
break;
|
||||
case EXP_CXER_DIFF:
|
||||
buf[buf_offset++] = '\n';
|
||||
assert(size != buf_offset
|
||||
|| memcmp(fbuf, buf, size));
|
||||
break;
|
||||
case EXP_OK:
|
||||
case EXP_PER_NOCOMP:
|
||||
assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
|
||||
break;
|
||||
}
|
||||
|
||||
asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Decode the .der files and try to regenerate them.
|
||||
*/
|
||||
static int
|
||||
process(const char *fname) {
|
||||
char fbuf[4096];
|
||||
char *ext = strrchr(fname, '.');
|
||||
enum expectation expectation;
|
||||
int ret;
|
||||
int rd;
|
||||
FILE *fp;
|
||||
|
||||
if(ext == 0 || strcmp(ext, ".in"))
|
||||
return 0;
|
||||
|
||||
switch(ext[-1]) {
|
||||
case 'B': /* The file is intentionally broken */
|
||||
expectation = EXP_BROKEN; break;
|
||||
case 'D': /* Reconstructing should yield different data */
|
||||
expectation = EXP_DIFFERENT; break;
|
||||
case 'E': /* Byte to byte exact reconstruction */
|
||||
expectation = EXP_CXER_EXACT; break;
|
||||
case 'X': /* Should fail byte-to-byte comparison */
|
||||
expectation = EXP_CXER_DIFF; break;
|
||||
case 'P': /* Incompatible with PER */
|
||||
expectation = EXP_PER_NOCOMP; break;
|
||||
default:
|
||||
expectation = EXP_OK; break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "\nProcessing file [../%s]\n", fname);
|
||||
|
||||
ret = chdir("../data-119");
|
||||
assert(ret == 0);
|
||||
fp = fopen(fname, "r");
|
||||
ret = chdir("../test-check-119.-gen-PER");
|
||||
assert(ret == 0);
|
||||
assert(fp);
|
||||
|
||||
rd = fread(fbuf, 1, sizeof(fbuf), fp);
|
||||
fclose(fp);
|
||||
|
||||
assert(rd < sizeof(fbuf)); /* expect small files */
|
||||
|
||||
process_XER_data(fname, expectation, fbuf, rd);
|
||||
|
||||
fprintf(stderr, "Finished [%s]\n", fname);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main() {
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
int processed_files = 0;
|
||||
char *str;
|
||||
|
||||
/* Process a specific test file */
|
||||
str = getenv("DATA_119_FILE");
|
||||
if(str && strncmp(str, "data-119-", 9) == 0) {
|
||||
process(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dir = opendir("../data-119");
|
||||
assert(dir);
|
||||
|
||||
/*
|
||||
* Process each file in that directory.
|
||||
*/
|
||||
while((dent = readdir(dir))) {
|
||||
if(strncmp(dent->d_name, "data-119-", 9) == 0)
|
||||
if(process(dent->d_name))
|
||||
processed_files++;
|
||||
}
|
||||
|
||||
assert(processed_files);
|
||||
closedir(dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -40,7 +40,7 @@ _buf_writer(const void *buffer, size_t size, void *app_key) {
|
|||
if(*b >= 32 && *b < 127 && *b != '%')
|
||||
fprintf(stderr, "%c", *b);
|
||||
else
|
||||
fprintf(stderr, "%%02x", *b);
|
||||
fprintf(stderr, "%%%02x", *b);
|
||||
}
|
||||
fprintf(stderr, "]:%ld\n", (long)size);
|
||||
buf_offset += size;
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
Mode of operation:
|
||||
|
||||
Each of the *.in files is XER-decoded, then converted into PER,
|
||||
then decoded back from PER, then encoded into XER again,
|
||||
and finally compared to the original encoding.
|
||||
|
||||
Naming conventions:
|
||||
|
||||
*-B.in - The file is intentionally broken
|
||||
*-P.in - Is not PER compatible, PER encoding must fail.
|
||||
*-E.in - CXER reconstruction should yield byte-wise identical data.
|
||||
|
||||
Otherwise, a reconstructed buffer should loosely match the original.
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ns></ns>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ns>0123456789</ns>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ns> </ns>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ns>z</ns>
|
||||
</PDU>
|
|
@ -0,0 +1,4 @@
|
|||
<PDU>
|
||||
<ia5>yabloko</ia5>
|
||||
<vs>yabloko</vs>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ia5>ÑÂÌÏËÏ</ia5>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ia5-c>non-capitals</ia5-c>
|
||||
</PDU>
|
|
@ -0,0 +1,4 @@
|
|||
<PDU>
|
||||
<ia5-c>CAPITALS</ia5-c>
|
||||
<vs-c>CAPITALS</vs-c>
|
||||
</PDU>
|
|
@ -0,0 +1,8 @@
|
|||
<PDU>
|
||||
<ia5></ia5>
|
||||
<ia5-c></ia5-c>
|
||||
<ia5-ce></ia5-ce>
|
||||
<vs></vs>
|
||||
<vs-c></vs-c>
|
||||
<vs-ce></vs-ce>
|
||||
</PDU>
|
|
@ -0,0 +1,9 @@
|
|||
<PDU>
|
||||
<ia5-ir>BAZ</ia5-ir>
|
||||
<vs-ir>BAZ</vs-ir>
|
||||
<pr-ir>BAZ</pr-ir>
|
||||
<ns-ir>19</ns-ir>
|
||||
<ut-c>Do not respect SIZE constraint</ut-c>
|
||||
<ut-ce>Do not respect SIZE constraint</ut-ce>
|
||||
<ut-ir>ABCabc</ut-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ia5-ir>FAIL</ia5-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<vs-ir>FAIL</vs-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<pr-ir>FAIL</pr-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ns-ir>13</ns-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<ut-ir>ABCabc,12234</ut-ir>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<real>3.14159265</real>
|
||||
</PDU>
|
|
@ -0,0 +1,3 @@
|
|||
<PDU>
|
||||
<oid>1.3.6.1.4.1.9363.1.5.1</oid>
|
||||
</PDU>
|
|
@ -1,7 +1,7 @@
|
|||
Mode of operation:
|
||||
|
||||
Each of the *.in files is XER-decoded, then converted into DER or PER,
|
||||
then decoded back from DER (PER), then encoded into XER again,
|
||||
Each of the *.in files is XER-decoded, then converted into DER,
|
||||
then decoded back from DER, then encoded into XER again,
|
||||
and finally compared to the original encoding.
|
||||
|
||||
Naming conventions:
|
||||
|
|
|
@ -768,4 +768,8 @@ ssize_t der_write_tags(asn_TYPE_descriptor_t *td, size_t slen, int tag_mode, int
|
|||
|
||||
asn_dec_rval_t xer_decode_general(asn_codec_ctx_t *opt_codec_ctx, asn_struct_ctx_t *ctx, void *struct_key, const char *xml_tag, const void *buf_ptr, size_t size, int (*otd)(void *struct_key, const void *chunk_buf, size_t chunk_size), ssize_t (*br)(void *struct_key, const void *chunk_buf, size_t chunk_size, int have_more)) { asn_dec_rval_t rv = { 0, 0 }; (void)opt_codec_ctx; (void)ctx; (void)struct_key; (void)xml_tag; (void)buf_ptr; (void)size; (void)otd; (void)br; return rv; }
|
||||
|
||||
asn_dec_rval_t OCTET_STRING_decode_uper(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_uper(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; }
|
||||
|
||||
int xer_is_whitespace(const void *b, size_t s) { (void)b; (void)s; return 0; }
|
||||
|
|
|
@ -40,9 +40,8 @@ static int compute_extensions_start(asn1p_expr_t *expr);
|
|||
static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type);
|
||||
static int emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int emit_members_PER_constraints(arg_t *arg);
|
||||
static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int juscountvalues, char *type);
|
||||
static int emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
|
||||
static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
|
||||
static int emit_include_dependencies(arg_t *arg);
|
||||
|
@ -340,8 +339,6 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
|
|||
if(expr_elements_count(arg, expr)) {
|
||||
int comp_mode = 0; /* {root,ext=1,root,root,...} */
|
||||
|
||||
if(emit_members_PER_constraints(arg))
|
||||
return -1;
|
||||
OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
|
||||
|
@ -580,8 +577,6 @@ asn1c_lang_C_type_SET_def(arg_t *arg) {
|
|||
if(expr_elements_count(arg, expr)) {
|
||||
int comp_mode = 0; /* {root,ext=1,root,root,...} */
|
||||
|
||||
if(emit_members_PER_constraints(arg))
|
||||
return -1;
|
||||
OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
|
||||
|
@ -782,8 +777,6 @@ asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
|
|||
/*
|
||||
* Print out the table according to which parsing is performed.
|
||||
*/
|
||||
if(emit_members_PER_constraints(arg))
|
||||
return -1;
|
||||
OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
INDENT(+1);
|
||||
|
@ -932,8 +925,6 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
|
|||
*/
|
||||
if(expr_elements_count(arg, expr)) {
|
||||
|
||||
if(emit_members_PER_constraints(arg))
|
||||
return -1;
|
||||
OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
|
||||
|
@ -1157,16 +1148,6 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
REDIR(OT_STAT_DEFS);
|
||||
|
||||
/*
|
||||
* Print out asn_DEF_<type>_[all_]tags[] vectors.
|
||||
*/
|
||||
tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
|
||||
|
||||
emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
|
||||
0, etd_spec);
|
||||
|
||||
REDIR(OT_CODE);
|
||||
|
||||
/*
|
||||
|
@ -1182,6 +1163,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
|
|||
INDENT(+1);
|
||||
OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {");
|
||||
OUT("\n");
|
||||
DEBUG("expr constraint checking code for %s", p);
|
||||
if(asn1c_emit_constraint_checking_code(arg) == 1) {
|
||||
OUT("/* Replace with underlying type checker */\n");
|
||||
OUT("td->check_constraints "
|
||||
|
@ -1195,6 +1177,20 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
|
|||
OUT("\n");
|
||||
}
|
||||
|
||||
REDIR(OT_STAT_DEFS);
|
||||
|
||||
/*
|
||||
* Print out asn_DEF_<type>_[all_]tags[] vectors.
|
||||
*/
|
||||
tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
|
||||
DEBUG("emit tag vectors for %s %d, %d, %d", expr->Identifier,
|
||||
tv_mode, tags_count, all_tags_count);
|
||||
|
||||
emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
|
||||
0, etd_spec);
|
||||
|
||||
REDIR(OT_CODE);
|
||||
|
||||
/*
|
||||
* Emit suicidal functions.
|
||||
*/
|
||||
|
@ -1588,7 +1584,7 @@ _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e f
|
|||
if(p) *tag2el = p;
|
||||
else return -1;
|
||||
|
||||
DEBUG("Found tag for %s: %ld",
|
||||
if(0) DEBUG("Found tag for %s: %ld",
|
||||
arg->expr->Identifier,
|
||||
(long)tag.tag_value);
|
||||
|
||||
|
@ -1678,14 +1674,17 @@ emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_ta
|
|||
|
||||
/* Fetch a chain of tags */
|
||||
tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
|
||||
if(tags_count < 0)
|
||||
if(tags_count < 0) {
|
||||
DEBUG("fail to fetch tags for %s", expr->Identifier);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Fetch a chain of tags */
|
||||
all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
|
||||
&all_tags, AFT_FULL_COLLECT);
|
||||
if(all_tags_count < 0) {
|
||||
if(tags) free(tags);
|
||||
DEBUG("fail to fetch tags chain for %s", expr->Identifier);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -1772,8 +1771,24 @@ expr_get_type(arg_t *arg, asn1p_expr_t *expr) {
|
|||
return A1TC_INVALID;
|
||||
}
|
||||
|
||||
static asn1c_integer_t
|
||||
PER_FROM_alphabet_characters(asn1cnst_range_t *range) {
|
||||
asn1c_integer_t numchars = 0;
|
||||
if(range->el_count) {
|
||||
int i;
|
||||
for(i = 0; i < range->el_count; i++)
|
||||
numchars
|
||||
+= PER_FROM_alphabet_characters(range->elements[i]);
|
||||
} else {
|
||||
assert(range->left.type == ARE_VALUE);
|
||||
assert(range->right.type == ARE_VALUE);
|
||||
numchars = 1 + (range->right.value - range->left.value);
|
||||
}
|
||||
return numchars;
|
||||
}
|
||||
|
||||
static int
|
||||
emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type) {
|
||||
emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alphabetsize, char *type) {
|
||||
if(!range || range->incompatible || range->not_PER_visible) {
|
||||
OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
|
||||
return 0;
|
||||
|
@ -1790,6 +1805,11 @@ emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *typ
|
|||
if(range->empty_constraint)
|
||||
r = 0;
|
||||
|
||||
if(alphabetsize) {
|
||||
/* X.691: 27.5.2 */
|
||||
r = PER_FROM_alphabet_characters(range);
|
||||
}
|
||||
|
||||
/* Compute real constraint */
|
||||
for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) {
|
||||
if(r <= cover)
|
||||
|
@ -1823,6 +1843,7 @@ emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *typ
|
|||
ebits = -1;
|
||||
}
|
||||
}
|
||||
|
||||
OUT("{ APC_CONSTRAINED%s,%s% d, % d, ",
|
||||
range->extensible
|
||||
? " | APC_EXTENSIBLE" : "",
|
||||
|
@ -1867,10 +1888,27 @@ emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *typ
|
|||
}
|
||||
|
||||
static int
|
||||
emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) {
|
||||
emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) {
|
||||
int save_target = arg->target->target;
|
||||
asn1cnst_range_t *range;
|
||||
asn1p_expr_type_e etype;
|
||||
|
||||
if((arg->flags & A1C_GEN_PER)
|
||||
&& (expr->constraints
|
||||
|| expr->expr_type == ASN_BASIC_ENUMERATED
|
||||
|| expr->expr_type == ASN_CONSTR_CHOICE)
|
||||
) {
|
||||
/* Fall through */
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
REDIR(OT_CTDEFS);
|
||||
|
||||
OUT("static asn_per_constraints_t "
|
||||
"asn_PER_%s_constr_%d = {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
|
||||
etype = expr_get_type(arg, expr);
|
||||
|
||||
INDENT(+1);
|
||||
|
@ -1903,20 +1941,21 @@ emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) {
|
|||
tmprng.left.value = 0;
|
||||
tmprng.right.type = ARE_VALUE;
|
||||
tmprng.right.value = eidx < 0 ? 0 : eidx;
|
||||
if(emit_single_member_PER_constraint(arg, &tmprng, 0))
|
||||
if(emit_single_member_PER_constraint(arg, &tmprng, 0, 0))
|
||||
return -1;
|
||||
} else if(etype & ASN_STRING_KM_MASK) {
|
||||
range = asn1constraint_compute_PER_range(etype,
|
||||
expr->combined_constraints, ACT_CT_FROM,
|
||||
0, 0, 0);
|
||||
if(emit_single_member_PER_constraint(arg, range, 0))
|
||||
DEBUG("Emitting FROM constraint for %s", expr->Identifier);
|
||||
if(emit_single_member_PER_constraint(arg, range, 1, 0))
|
||||
return -1;
|
||||
asn1constraint_range_free(range);
|
||||
} else {
|
||||
range = asn1constraint_compute_PER_range(etype,
|
||||
expr->combined_constraints, ACT_EL_RANGE,
|
||||
0, 0, 0);
|
||||
if(emit_single_member_PER_constraint(arg, range, 0))
|
||||
if(emit_single_member_PER_constraint(arg, range, 0, 0))
|
||||
return -1;
|
||||
asn1constraint_range_free(range);
|
||||
}
|
||||
|
@ -1924,36 +1963,55 @@ emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) {
|
|||
|
||||
range = asn1constraint_compute_PER_range(etype,
|
||||
expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0);
|
||||
if(emit_single_member_PER_constraint(arg, range, "SIZE"))
|
||||
if(emit_single_member_PER_constraint(arg, range, 0, "SIZE"))
|
||||
return -1;
|
||||
asn1constraint_range_free(range);
|
||||
OUT("\n");
|
||||
OUT(",\n");
|
||||
|
||||
if((etype & ASN_STRING_KM_MASK) && (expr->_mark & TM_PERFROMCT)) {
|
||||
int old_target = arg->target->target;
|
||||
REDIR(OT_CODE);
|
||||
|
||||
OUT("static int asn_PER_MAP_%s_%d_v2c(unsigned int value) {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
OUT("\tif(value >= sizeof(permitted_alphabet_table_%d)/"
|
||||
"sizeof(permitted_alphabet_table_%d[0]))\n",
|
||||
expr->_type_unique_index,
|
||||
expr->_type_unique_index);
|
||||
OUT("\t\treturn -1;\n");
|
||||
OUT("\treturn permitted_alphabet_table_%d[value] - 1;\n",
|
||||
expr->_type_unique_index);
|
||||
OUT("}\n");
|
||||
|
||||
OUT("static int asn_PER_MAP_%s_%d_c2v(unsigned int code) {\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
OUT("\tif(code >= sizeof(permitted_alphabet_code2value_%d)/"
|
||||
"sizeof(permitted_alphabet_code2value_%d[0]))\n",
|
||||
expr->_type_unique_index,
|
||||
expr->_type_unique_index);
|
||||
OUT("\t\treturn -1;\n");
|
||||
OUT("\treturn permitted_alphabet_code2value_%d[code];\n",
|
||||
expr->_type_unique_index);
|
||||
OUT("}\n");
|
||||
|
||||
REDIR(old_target);
|
||||
|
||||
OUT("asn_PER_MAP_%s_%d_v2c,\t/* Value to PER code map */\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
OUT("asn_PER_MAP_%s_%d_c2v\t/* PER code to value map */\n",
|
||||
MKID(expr), expr->_type_unique_index);
|
||||
} else if(etype & ASN_STRING_KM_MASK) {
|
||||
DEBUG("No PER value map necessary for %s", MKID(expr));
|
||||
OUT("0, 0\t/* No PER character map necessary */\n");
|
||||
} else {
|
||||
OUT("0, 0\t/* No PER value map */\n");
|
||||
}
|
||||
|
||||
INDENT(-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
OUT("};\n");
|
||||
|
||||
static int
|
||||
emit_members_PER_constraints(arg_t *arg) {
|
||||
asn1p_expr_t *expr = arg->expr;
|
||||
asn1p_expr_t *v;
|
||||
|
||||
if(!(arg->flags & A1C_GEN_PER))
|
||||
return 0;
|
||||
|
||||
TQ_FOR(v, &(expr->members), next) {
|
||||
if(v->constraints
|
||||
|| v->expr_type == ASN_BASIC_ENUMERATED
|
||||
|| v->expr_type == ASN_CONSTR_CHOICE) {
|
||||
OUT("static asn_per_constraints_t "
|
||||
"asn_PER_memb_%s_constr_%d = {\n",
|
||||
MKID(v), v->_type_unique_index);
|
||||
if(emit_single_member_PER_constraints(arg, v))
|
||||
return -1;
|
||||
OUT("};\n");
|
||||
}
|
||||
}
|
||||
REDIR(save_target);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -2138,7 +2196,7 @@ emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
|
|||
if(expr->constraints
|
||||
|| expr->expr_type == ASN_BASIC_ENUMERATED
|
||||
|| expr->expr_type == ASN_CONSTR_CHOICE) {
|
||||
OUT("&asn_PER_memb_%s_constr_%d,\n",
|
||||
OUT("&asn_PER_%s_constr_%d,\n",
|
||||
MKID(expr),
|
||||
expr->_type_unique_index);
|
||||
} else {
|
||||
|
@ -2182,6 +2240,7 @@ emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
|
|||
OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n");
|
||||
tmp_arg = *arg;
|
||||
tmp_arg.expr = expr;
|
||||
DEBUG("member constraint checking code for %s", p);
|
||||
if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
|
||||
OUT("return td->check_constraints"
|
||||
"(td, sptr, ctfailcb, app_key);\n");
|
||||
|
@ -2190,6 +2249,9 @@ emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
|
|||
OUT("}\n");
|
||||
OUT("\n");
|
||||
|
||||
if(emit_member_PER_constraints(arg, expr))
|
||||
return -1;
|
||||
|
||||
REDIR(save_target);
|
||||
|
||||
return 0;
|
||||
|
@ -2206,17 +2268,8 @@ emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_
|
|||
|
||||
terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
|
||||
|
||||
if((arg->flags & A1C_GEN_PER)
|
||||
&& (expr->constraints
|
||||
|| expr->expr_type == ASN_BASIC_ENUMERATED
|
||||
|| expr->expr_type == ASN_CONSTR_CHOICE)
|
||||
) {
|
||||
OUT("static asn_per_constraints_t asn_PER_%s_constr_%d = {\n",
|
||||
p, expr->_type_unique_index);
|
||||
if(emit_single_member_PER_constraints(arg, expr))
|
||||
return -1;
|
||||
OUT("};\n");
|
||||
}
|
||||
if(emit_member_PER_constraints(arg, expr))
|
||||
return -1;
|
||||
|
||||
if(HIDE_INNER_DEFS)
|
||||
OUT("static /* Use -fall-defs-global to expose */\n");
|
||||
|
|
|
@ -316,7 +316,8 @@ asn1c_emit_constraint_tables(arg_t *arg, int got_size) {
|
|||
}
|
||||
OUT("};\n");
|
||||
|
||||
if((arg->flags & A1C_GEN_PER)) {
|
||||
if((arg->flags & A1C_GEN_PER)
|
||||
&& (etype & ASN_STRING_KM_MASK)) {
|
||||
int c;
|
||||
OUT("static int permitted_alphabet_code2value_%d[%d] = {\n",
|
||||
arg->expr->_type_unique_index, cardinal);
|
||||
|
@ -328,6 +329,8 @@ asn1c_emit_constraint_tables(arg_t *arg, int got_size) {
|
|||
}
|
||||
OUT("};\n");
|
||||
OUT("\n");
|
||||
DEBUG("code2value map gen for %s", arg->expr->Identifier);
|
||||
arg->expr->_mark |= TM_PERFROMCT;
|
||||
}
|
||||
|
||||
OUT("\n");
|
||||
|
|
|
@ -158,7 +158,8 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
|
|||
while(top_parent->parent_expr)
|
||||
top_parent = top_parent->parent_expr;
|
||||
|
||||
DEBUG("asn1c_type_name(%s: 0x%x)", expr->Identifier, expr->expr_type);
|
||||
if(0) DEBUG("asn1c_type_name(%s: 0x%x)",
|
||||
expr->Identifier, expr->expr_type);
|
||||
|
||||
switch(expr->expr_type) {
|
||||
case A1TC_REFERENCE:
|
||||
|
|
|
@ -22,6 +22,7 @@ typedef struct compiler_streams {
|
|||
OT_POST_INCLUDE,/* #include after type definition */
|
||||
OT_CTABLES, /* Constraint tables */
|
||||
OT_CODE, /* Some code */
|
||||
OT_CTDEFS, /* Constraint definitions */
|
||||
OT_STAT_DEFS, /* Static definitions */
|
||||
OT_MAX
|
||||
} target;
|
||||
|
@ -34,7 +35,7 @@ typedef struct compiler_streams {
|
|||
} compiler_streams_t;
|
||||
|
||||
static char *_compiler_stream2str[] __attribute__ ((unused))
|
||||
= { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "STAT-DEFS" };
|
||||
= { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" };
|
||||
|
||||
int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
|
||||
|
||||
|
|
|
@ -273,10 +273,12 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
|
|||
fwrite(ot->buf, ot->len, 1, fp_c);
|
||||
TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
|
||||
fwrite(ot->buf, ot->len, 1, fp_c);
|
||||
TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
|
||||
fwrite(ot->buf, ot->len, 1, fp_c);
|
||||
TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
|
||||
fwrite(ot->buf, ot->len, 1, fp_c);
|
||||
|
||||
assert(OT_MAX == 10); /* Protection from reckless changes */
|
||||
assert(OT_MAX == 11); /* Protection from reckless changes */
|
||||
|
||||
fclose(fp_c);
|
||||
fclose(fp_h);
|
||||
|