*** empty log message ***

pespin/master
Lev Walkin 2006-07-13 11:19:01 +00:00
parent 397d59d448
commit 1eded3544e
50 changed files with 1231 additions and 388 deletions

View File

@ -0,0 +1,219 @@
#undef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <T.h>
uint8_t buf1[] = {
32 | (2 << 6), /* [0], constructed */
25, /* L */
/* string [0] IMPLICIT UTF8String, */
(2 << 6), /* [0] */
16, /* L */
'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z',
/* beta [2] IMPLICIT INTEGER OPTIONAL */
(2 << 6) + 2, /* [2] */
5, /* L */
0,
75,
0x4b,
75,
75,
};
uint8_t buf1_reconstr[] = {
32 | (2 << 6), /* [0], constructed */
24, /* L */
/* string [0] IMPLICIT UTF8String, */
(2 << 6), /* [0] */
16, /* L */
'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z',
/* beta [2] IMPLICIT INTEGER OPTIONAL */
(2 << 6) + 2, /* [2] */
4, /* L */
75,
75,
75,
0x4b,
};
static void
check(T_t *tp, uint8_t *buf, int size, size_t consumed) {
asn_dec_rval_t rval;
tp = memset(tp, 0, sizeof(*tp));
fprintf(stderr, "Buf %p (%d)\n", buf, size);
rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
fprintf(stderr, "Returned code %d, consumed %d\n",
(int)rval.code, (int)rval.consumed);
assert(rval.code == RC_OK);
assert(rval.consumed == consumed);
assert(tp->choice.seq.string.size == 16);
assert(strcmp(tp->choice.seq.string.buf, "zzzzzzzzzzzzzzzz") == 0);
assert(tp->choice.seq.alpha == NULL);
assert(tp->choice.seq.beta);
assert(*tp->choice.seq.beta == 0x4b4b4b4b);
}
size_t buf_pos;
size_t buf_size;
uint8_t *buf;
static int
buf_fill(const void *buffer, size_t size, void *app_key) {
(void)app_key; /* Unused argument */
if(buf_pos + size > buf_size) {
fprintf(stderr, "%d + %d > %d\n",
(int)buf_pos, (int)size, (int)buf_size);
return -1;
}
memcpy(buf + buf_pos, buffer, size);
buf_pos += size;
fprintf(stderr, " written %d (%d)\n", (int)size, (int)buf_pos);
return 0;
}
static void
compare(T_t *tp, uint8_t *cmp_buf, int cmp_buf_size) {
asn_enc_rval_t erval;
int i;
buf_size = cmp_buf_size + 100;
buf = alloca(buf_size);
buf_pos = 0;
/*
* Try to re-create using DER encoding.
*/
erval = der_encode(&asn_DEF_T, tp, buf_fill, 0);
assert(erval.encoded != -1);
if(erval.encoded != cmp_buf_size) {
printf("%d != %d\n", erval.encoded, cmp_buf_size);
}
assert(erval.encoded == cmp_buf_size);
for(i = 0; i < cmp_buf_size; i++) {
if(buf[i] != cmp_buf[i]) {
fprintf(stderr, "Recreated buffer content mismatch:\n");
fprintf(stderr, "Byte %d, %x != %x (%d != %d)\n",
i,
buf[i], cmp_buf[i],
buf[i], cmp_buf[i]
);
}
assert(buf[i] == cmp_buf[i]);
}
}
static void
partial_read(uint8_t *buf_0, size_t size) {
T_t t, *tp;
asn_dec_rval_t rval;
size_t i1, i2;
uint8_t *buf_1 = alloca(size);
uint8_t *buf_2 = alloca(size);
uint8_t *buf_3 = alloca(size);
fprintf(stderr, "\nPartial read sequence...\n");
/*
* Divide the space (size) into three blocks in various combinations:
* |<----->i1<----->i2<----->|
* ^ buf_0 ^ buf_0+size
* Try to read block by block.
*/
for(i1 = 0; i1 < size; i1++) {
for(i2 = i1; i2 < size; i2++) {
uint8_t *chunk1 = buf_0;
size_t size1 = i1;
uint8_t *chunk2 = buf_0 + size1;
size_t size2 = i2 - i1;
uint8_t *chunk3 = buf_0 + size1 + size2;
size_t size3 = size - size1 - size2;
fprintf(stderr, "\n%d:{%d, %d, %d}...\n",
(int)size, (int)size1, (int)size2, (int)size3);
memset(buf_1, 0, size);
memset(buf_2, 0, size);
memset(buf_3, 0, size);
memcpy(buf_1, chunk1, size1);
memcpy(buf_2, chunk2, size2);
memcpy(buf_3, chunk3, size3);
tp = memset(&t, 0, sizeof(t));
fprintf(stderr, "=> Chunk 1 (%d):\n", (int)size1);
rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
buf_1, size1);
assert(rval.code == RC_WMORE);
assert(rval.consumed <= size1);
if(rval.consumed < size1) {
int leftover = size1 - rval.consumed;
memcpy(buf_2, buf_1 + rval.consumed, leftover);
memcpy(buf_2 + leftover, chunk2, size2);
size2 += leftover;
}
fprintf(stderr, "=> Chunk 2 (%d):\n", (int)size2);
rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
buf_2, size2);
assert(rval.code == RC_WMORE);
assert(rval.consumed <= size2);
if(rval.consumed < size2) {
int leftover = size2 - rval.consumed;
memcpy(buf_3, buf_2 + rval.consumed, leftover);
memcpy(buf_3 + leftover, chunk3, size3);
size3 += leftover;
}
fprintf(stderr, "=> Chunk 3 (%d):\n", (int)size3);
rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
buf_3, size3);
assert(rval.code == RC_OK);
assert(rval.consumed == size3);
asn_DEF_T.free_struct(&asn_DEF_T, &t, 1);
}
}
}
int
main(int ac, char **av) {
T_t t;
(void)ac; /* Unused argument */
(void)av; /* Unused argument */
/* Check exact buf1 */
check(&t, buf1, sizeof(buf1), sizeof(buf1));
compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
asn_fprint(stderr, &asn_DEF_T, &t);
asn_DEF_T.free_struct(&asn_DEF_T, &t, 1);
/* Check slightly more than buf1 */
check(&t, buf1, sizeof(buf1) + 10, sizeof(buf1));
compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
asn_fprint(stderr, &asn_DEF_T, &t);
asn_DEF_T.free_struct(&asn_DEF_T, &t, 1);
/* Split the buffer in parts and check decoder restartability */
partial_read(buf1, sizeof(buf1));
return 0;
}

View File

@ -0,0 +1,142 @@
#undef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <LogLine.h>
uint8_t buf0[] = {
48, /* LogLine SEQUENCE */
24, /* L */
22, /* IA5String */
4, /* L */
/* "zzz\007" */
122, 122, 122, 7,
48, /* varsets SEQUENCE OF VariablePartSet */
16, /* L */
48, /* VariablePart */
14, /* L */
48, /* vparts SEQUENCE OF VariablePart */
7, /* L */
49, /* VariablePart */
5,
26, /* VisibleString */
3,
49, 50, 51, /* 1 2 3 */
48, /* ActionItem SEQUENCE */
3, /* L */
10, /* accept-as ENUMERATED */
1, /* L */
0,
};
uint8_t buf1[] = {
48, /* LogLine SEQUENCE */
19, /* L */
22, /* IA5String */
6, /* L */
/* "static" */
115, 116, 97, 116, 105, 99,
48, /* varsets SEQUENCE OF VariablePartSet */
9, /* L */
48, /* VariablePart */
7, /* L */
48, /* vparts SEQUENCE OF VariablePart */
0, /* L */
48, /* ActionItem SEQUENCE */
3, /* L */
10, /* accept-as ENUMERATED */
1, /* L */
0,
};
static void
check(LogLine_t *tp, uint8_t *ptr, int size, size_t consumed) {
asn_dec_rval_t rval;
tp = memset(tp, 0, sizeof(*tp));
fprintf(stderr, "Buf %p (%d)\n", ptr, size);
rval = ber_decode(0, &asn_DEF_LogLine, (void **)&tp, ptr, size);
fprintf(stderr, "Returned code %d, consumed %d\n",
(int)rval.code, (int)rval.consumed);
assert(rval.code == RC_OK);
assert(rval.consumed == consumed);
asn_fprint(stderr, &asn_DEF_LogLine, tp);
asn_DEF_LogLine.free_struct(&asn_DEF_LogLine, tp, 1);
}
uint8_t *buf;
uint8_t buf_size;
uint8_t buf_pos;
static int
buf_fill(const void *buffer, size_t size, void *app_key) {
(void)app_key; /* Unused argument */
assert(buf_pos + size <= buf_size);
memcpy(buf + buf_pos, buffer, size);
buf_pos += size;
return 0;
}
static void
check_serialize() {
LogLine_t ll;
VariablePartSet_t vps;
VariablePart_t vp;
VisibleString_t vpart;
asn_enc_rval_t erval;
int i;
memset(&ll, 0, sizeof(ll));
memset(&vps, 0, sizeof(vps));
memset(&vp, 0, sizeof(vp));
memset(&vpart, 0, sizeof(vpart));
vpart.buf = "123";
vpart.size = 3;
vp.present = VariablePart_PR_vset;
ASN_SET_ADD(&vp.choice.vset, &vpart);
vps.resolution.accept_as = accept_as_unknown;
ASN_SEQUENCE_ADD(&vps.vparts, &vp);
ASN_SEQUENCE_ADD(&ll.varsets, &vps);
ll.line_digest.buf = "zzz\007";
ll.line_digest.size = 4;
asn_fprint(stderr, &asn_DEF_LogLine, &ll);
buf_size = 128;
buf = alloca(buf_size);
erval = der_encode(&asn_DEF_LogLine, &ll, buf_fill, 0);
assert(erval.encoded > 1);
fprintf(stderr, "Encoded in %d bytes\n", erval.encoded);
fprintf(stderr, "\n");
for(i = 0; i < buf_pos; i++) {
fprintf(stderr, "%d ", buf[i]);
}
fprintf(stderr, "\n\n");
assert(erval.encoded == sizeof(buf0));
assert(memcmp(buf0, buf, sizeof(buf0)) == 0);
}
int
main(int ac, char **av) {
LogLine_t t;
(void)ac; /* Unused argument */
(void)av; /* Unused argument */
check_serialize();
check(&t, buf0, sizeof(buf0), sizeof(buf0));
check(&t, buf1, sizeof(buf1), sizeof(buf1));
return 0;
}

View File

@ -0,0 +1,203 @@
#undef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <T1.h>
#include <T2.h>
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;
printf("=> [");
for(; b < bend; b++)
printf(" %02X", *b);
printf("]:%ld\n", (long)size);
buf_offset += size;
return 0;
}
static int
save_object(void *bs, asn_TYPE_descriptor_t *td) {
asn_enc_rval_t rval; /* Return value */
int i;
buf_offset = 0;
rval = der_encode(td, bs, _buf_writer, 0);
if (rval.encoded == -1) {
fprintf(stderr,
"Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1);
return -1; /* JIC */
}
buf[buf_offset++] = 0xab; /* Finalize with garbage */
asn_fprint(stderr, td, bs);
printf("OUT: [");
for(i = 0; i < buf_offset; i++)
printf(" %02x", buf[i]);
printf("]\n");
return 0;
}
static int
load_object(void *bs, asn_TYPE_descriptor_t *td) {
asn_dec_rval_t rval;
fprintf(stderr, "\nLOADING OBJECT OF SIZE %d\n", buf_offset);
rval = ber_decode(0, td, (void **)&bs, buf, buf_offset);
assert(rval.code == RC_OK);
asn_fprint(stderr, td, bs);
return (rval.code == RC_OK)?0:-1;
}
/* [3] IMPLICIT SEQUENCE { b BOOLEAN } */
uint8_t test_any_buf1[] = { 0xa3, 0x80, /* [3], constructed, indefinite */
0x01, 0x01, 0xff, /* b BOOLEAN ::= TRUE */
0x00, 0x00 /* End of content octets */ };
/* b BOOLEAN */
uint8_t test_any_buf2[] = { 0x01, 0x01, 0x13 };
int
main() {
asn_TYPE_descriptor_t *td1 = &asn_DEF_T1;
asn_TYPE_descriptor_t *td2 = &asn_DEF_T2;
T1_t t1, t1_new;
T2_t t2, t2_new;
int ret;
/*
* Test the T1 with constructed indefinite length ANY encoding.
*/
memset(&t1, 0, sizeof(t1));
memset(&t1_new, 0, sizeof(t1_new));
t1.i = 112233;
t1.any.buf = test_any_buf1;
t1.any.size = sizeof(test_any_buf1);
/* Save->Load must succeed */
save_object(&t1, td1);
ret = load_object(&t1_new, td1);
assert(ret == 0);
assert(t1_new.i == 112233);
assert(t1_new.any.size == sizeof(test_any_buf1));
assert(memcmp(t1_new.any.buf, test_any_buf1, sizeof(test_any_buf1)) == 0);
/*
* Test the T1 with primitive encoding.
*/
memset(&t1, 0, sizeof(t1));
memset(&t1_new, 0, sizeof(t1_new));
t1.i = -112233;
t1.any.buf = test_any_buf2;
t1.any.size = sizeof(test_any_buf2);
/* Save->Load must succeed */
save_object(&t1, td1);
ret = load_object(&t1_new, td1);
assert(ret == 0);
assert(t1_new.i == -112233);
assert(t1_new.any.size == sizeof(test_any_buf2));
assert(memcmp(t1_new.any.buf, test_any_buf2, sizeof(test_any_buf2)) == 0);
/*
* Test the T2 empty sequence.
*/
memset(&t2, 0, sizeof(t2));
memset(&t2_new, 0, sizeof(t2_new));
t2.i = 332211;
t2.any = calloc(1, sizeof(*t2.any));
t2.any->buf = 0;
t2.any->size = 0;
/* Save->Load must succeed */
save_object(&t2, td2);
ret = load_object(&t2_new, td2);
assert(ret == 0);
assert(t2_new.i == 332211);
assert(t2_new.any->size == 0);
/*
* Test the T2 sequence.
*/
memset(&t2, 0, sizeof(t2));
memset(&t2_new, 0, sizeof(t2_new));
t2.i = 332211;
t2.any = calloc(1, sizeof(*t2.any));
t2.any->buf = test_any_buf1;
t2.any->size = sizeof(test_any_buf1);
/* Save->Load must succeed */
save_object(&t2, td2);
ret = load_object(&t2_new, td2);
assert(ret == 0);
assert(t2_new.i == 332211);
assert(t2_new.any->size == sizeof(test_any_buf1));
assert(memcmp(t2_new.any->buf, test_any_buf1, sizeof(test_any_buf1)) == 0);
/*
* Test the T2 sequence with primitive encoding.
*/
memset(&t2, 0, sizeof(t2));
memset(&t2_new, 0, sizeof(t2_new));
t2.i = 0;
t2.any = calloc(1, sizeof(*t2.any));
t2.any->buf = test_any_buf2;
t2.any->size = sizeof(test_any_buf2);
/* Save->Load must succeed */
save_object(&t2, td2);
ret = load_object(&t2_new, td2);
assert(ret == 0);
assert(t2_new.i == 0);
assert(t2_new.any->size == sizeof(test_any_buf2));
assert(memcmp(t2_new.any->buf, test_any_buf2, sizeof(test_any_buf2)) == 0);
/*
* Test T2 with ANY element omitted.
*/
free(t2.any);
t2.any = 0;
memset(&t2_new, 0, sizeof(t2_new));
save_object(&t2, td2);
ret = load_object(&t2_new, td2);
assert(ret == 0);
assert(t2_new.i == 0);
assert(t2_new.any == 0);
printf("OK\n");
return ret;
}

View File

@ -1176,7 +1176,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
INDENT(+1);
OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {");
OUT("\n");
if(asn1c_emit_constraint_checking_code(arg) == 1) {
OUT("/* Replace with underlying type checker */\n");
@ -1184,7 +1184,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
"= asn_DEF_%s.check_constraints;\n",
asn1c_type_name(arg, expr, TNF_SAFE));
OUT("return td->check_constraints"
"(td, sptr, app_errlog, app_key);\n");
"(td, sptr, ctfailcb, app_key);\n");
}
INDENT(-1);
OUT("}\n");
@ -2127,12 +2127,12 @@ emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
OUT("static int\n");
OUT("memb_%s_constraint_%d(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
INDENT(+1);
OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n");
tmp_arg = *arg;
tmp_arg.expr = expr;
if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
OUT("return td->check_constraints"
"(td, sptr, app_errlog, app_key);\n");
"(td, sptr, ctfailcb, app_key);\n");
}
INDENT(-1);
OUT("}\n");

View File

@ -111,7 +111,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
*/
OUT("if(!sptr) {\n");
INDENT(+1);
OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
OUT("_ASN_CTFAIL(app_key, td, sptr,\n");
OUT("\t\"%%s: value not given (%%s:%%d)\",\n");
OUT("\ttd->name, __FILE__, __LINE__);\n");
OUT("return -1;\n");
@ -177,7 +177,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
case ASN_CONSTR_SEQUENCE_OF:
case ASN_CONSTR_SET_OF:
OUT("/* Perform validation of the inner elements */\n");
OUT("return td->check_constraints(td, sptr, app_errlog, app_key);\n");
OUT("return td->check_constraints(td, sptr, ctfailcb, app_key);\n");
break;
default:
OUT("/* Constraint check succeeded */\n");
@ -186,7 +186,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
INDENT(-1);
OUT("} else {\n");
INDENT(+1);
OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
OUT("_ASN_CTFAIL(app_key, td, sptr,\n");
OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n");
OUT("\ttd->name, __FILE__, __LINE__);\n");
OUT("return -1;\n");
@ -519,7 +519,7 @@ emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) {
case ASN_STRING_UTF8String:
OUT("size = UTF8String_length(st);\n");
OUT("if((ssize_t)size < 0) {\n");
OUT("\t_ASN_ERRLOG(app_errlog, app_key,\n");
OUT("\t_ASN_CTFAIL(app_key, td, sptr,\n");
OUT("\t\t\"%%s: UTF-8: broken encoding (%%s:%%d)\",\n");
OUT("\t\ttd->name, __FILE__, __LINE__);\n");
OUT("\treturn -1;\n");
@ -581,7 +581,7 @@ emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_rang
OUT("if(asn_INTEGER2long(st, &value)) {\n");
INDENT(+1);
OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
OUT("_ASN_CTFAIL(app_key, td, sptr,\n");
OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
OUT("\ttd->name, __FILE__, __LINE__);\n");
OUT("return -1;\n");
@ -595,7 +595,7 @@ emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_rang
} else {
OUT("if(asn_REAL2double(st, &value)) {\n");
INDENT(+1);
OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
OUT("_ASN_CTFAIL(app_key, td, sptr,\n");
OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
OUT("\ttd->name, __FILE__, __LINE__);\n");
OUT("return -1;\n");

View File

@ -45,18 +45,18 @@ asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
*/
int
BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
if(st && st->buf) {
if(st->size == 1 && st->bits_unused) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: invalid padding byte (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -148,14 +148,14 @@ asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
*/
int
GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
time_t tloc;
errno = EPERM; /* Just an unlikely error code */
tloc = asn_GT2time(st, 0, 0);
if(tloc == -1 && errno != EPERM) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: Invalid time format: %s (%s:%d)",
td->name, strerror(errno), __FILE__, __LINE__);
return -1;

View File

@ -37,7 +37,7 @@ asn_TYPE_descriptor_t asn_DEF_IA5String = {
int
IA5String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const IA5String_t *st = (const IA5String_t *)sptr;
if(st && st->buf) {
@ -49,7 +49,7 @@ IA5String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
*/
for(; buf < end; buf++) {
if(*buf > 0x7F) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value byte %ld out of range: "
"%d > 127 (%s:%d)",
td->name,
@ -60,7 +60,7 @@ IA5String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
}
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -168,6 +168,7 @@ NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
const void *buf_ptr, size_t size) {
asn_dec_rval_t rval;
REAL_t *st = 0;
REAL_t **stp = &st;
double *Dbl = (double *)*sptr;
if(!Dbl) {
@ -180,7 +181,7 @@ NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
}
}
rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname,
rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
buf_ptr, size);
if(rval.code == RC_OK) {
if(asn_REAL2double(st, Dbl)) {

View File

@ -37,7 +37,7 @@ asn_TYPE_descriptor_t asn_DEF_NumericString = {
int
NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const NumericString_t *st = (const NumericString_t *)sptr;
if(st && st->buf) {
@ -55,7 +55,7 @@ NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
continue;
}
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value byte %ld (%d) "
"not in NumericString alphabet (%s:%d)",
td->name,
@ -65,7 +65,7 @@ NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
return -1;
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -39,19 +39,19 @@ asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
int
OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
if(st && st->buf) {
if(st->size < 1) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: at least one numerical value "
"expected (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -60,7 +60,7 @@ static int _PrintableString_alphabet[256] = {
int
PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const PrintableString_t *st = (const PrintableString_t *)sptr;
if(st && st->buf) {
@ -73,7 +73,7 @@ PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
*/
for(; buf < end; buf++) {
if(!_PrintableString_alphabet[*buf]) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value byte %ld (%d) "
"not in PrintableString alphabet "
"(%s:%d)",
@ -85,7 +85,7 @@ PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
}
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -48,14 +48,14 @@ asn_TYPE_descriptor_t asn_DEF_UTCTime = {
*/
int
UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const UTCTime_t *st = (const UTCTime_t *)sptr;
time_t tloc;
errno = EPERM; /* Just an unlikely error code */
tloc = asn_UT2time(st, 0, 0);
if(tloc == -1 && errno != EPERM) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: Invalid time format: %s (%s:%d)",
td->name, strerror(errno), __FILE__, __LINE__);
return -1;

View File

@ -67,30 +67,30 @@ static int32_t UTF8String_mv[7] = { 0, 0,
int
UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
switch(len) {
case U8E_EINVAL:
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given", td->name);
break;
case U8E_TRUNC:
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: truncated UTF-8 sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_ILLSTART:
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: UTF-8 illegal start of encoding (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTCONT:
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: UTF-8 not continuation (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTMIN:
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: UTF-8 not minimal sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;

View File

@ -37,7 +37,7 @@ asn_TYPE_descriptor_t asn_DEF_VisibleString = {
int
VisibleString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const VisibleString_t *st = (const VisibleString_t *)sptr;
if(st && st->buf) {
@ -52,7 +52,7 @@ VisibleString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
*/
for(; buf < end; buf++) {
if(*buf < 0x20 || *buf > 0x7e) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value byte %ld (%d) "
"not in VisibleString alphabet (%s:%d)",
td->name,
@ -63,7 +63,7 @@ VisibleString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
}
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -1,15 +1,15 @@
/*-
* Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
/*
* Application-level ASN.1 API.
* Application-level ASN.1 callbacks.
*/
#ifndef _ASN_APPLICATION_H_
#define _ASN_APPLICATION_H_
#include <asn_system.h> /* for platform-dependent types */
#include <asn_codecs.h> /* for ASN.1 codecs specifics */
#include "asn_system.h" /* for platform-dependent types */
#include "asn_codecs.h" /* for ASN.1 codecs specifics */
#ifdef __cplusplus
extern "C" {
@ -25,10 +25,24 @@ extern "C" {
typedef int (asn_app_consume_bytes_f)(const void *buffer, size_t size,
void *application_specific_key);
#include <constr_TYPE.h> /* for asn_TYPE_descriptor_t */
/*
* A callback of this type is called whenever constraint validation fails
* on some ASN.1 type. See "constraints.h" for more details on constraint
* validation.
* This callback specifies a descriptor of the ASN.1 type which failed
* the constraint check, as well as human readable message on what
* particular constraint has failed.
*/
typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
const void *structure_which_failed_ptr,
const char *error_message_format, ...)
__attribute__((format(printf, 4, 5)));
#ifdef __cplusplus
}
#endif
#include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
#endif /* _ASN_APPLICATION_H_ */

View File

@ -9,7 +9,7 @@
#ifndef _ASN_INTERNAL_H_
#define _ASN_INTERNAL_H_
#include <asn_application.h> /* Application-visible API */
#include "asn_application.h" /* Application-visible API */
#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
#include <assert.h> /* for assert() macro */

View File

@ -475,12 +475,12 @@ CHOICE_outmost_tag(asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber
int
CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
int present;
if(!sptr) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
@ -499,7 +499,7 @@ CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(!memb_ptr) {
if(elm->optional)
return 0;
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: mandatory CHOICE element %s absent (%s:%d)",
td->name, elm->name, __FILE__, __LINE__);
return -1;
@ -510,10 +510,10 @@ CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(elm->memb_constraints) {
return elm->memb_constraints(elm->type, memb_ptr,
app_errlog, app_key);
ctfailcb, app_key);
} else {
int ret = elm->type->check_constraints(elm->type,
memb_ptr, app_errlog, app_key);
memb_ptr, ctfailcb, app_key);
/*
* Cannot inherit it eralier:
* need to make sure we get the updated version.
@ -522,7 +522,7 @@ CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
return ret;
}
} else {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: no CHOICE element given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;

View File

@ -972,11 +972,11 @@ SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
int
SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
int edx;
if(!sptr) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
@ -994,7 +994,7 @@ SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(!memb_ptr) {
if(elm->optional)
continue;
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: mandatory element %s absent (%s:%d)",
td->name, elm->name, __FILE__, __LINE__);
return -1;
@ -1005,11 +1005,11 @@ SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(elm->memb_constraints) {
int ret = elm->memb_constraints(elm->type, memb_ptr,
app_errlog, app_key);
ctfailcb, app_key);
if(ret) return ret;
} else {
int ret = elm->type->check_constraints(elm->type,
memb_ptr, app_errlog, app_key);
memb_ptr, ctfailcb, app_key);
if(ret) return ret;
/*
* Cannot inherit it earlier:

View File

@ -938,11 +938,11 @@ SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
int
SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
int edx;
if(!sptr) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
@ -960,7 +960,7 @@ SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(!memb_ptr) {
if(elm->optional)
continue;
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: mandatory element %s absent (%s:%d)",
td->name, elm->name, __FILE__, __LINE__);
return -1;
@ -971,11 +971,11 @@ SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(elm->memb_constraints) {
int ret = elm->memb_constraints(elm->type, memb_ptr,
app_errlog, app_key);
ctfailcb, app_key);
if(ret) return ret;
} else {
int ret = elm->type->check_constraints(elm->type,
memb_ptr, app_errlog, app_key);
memb_ptr, ctfailcb, app_key);
if(ret) return ret;
/*
* Cannot inherit it earlier:

View File

@ -812,14 +812,14 @@ SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
int
SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
asn_TYPE_member_t *elm = td->elements;
asn_constr_check_f *constr;
const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
int i;
if(!sptr) {
_ASN_ERRLOG(app_errlog, app_key,
_ASN_CTFAIL(app_key, td,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
@ -838,7 +838,7 @@ SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
if(!memb_ptr) continue;
ret = constr(elm->type, memb_ptr, app_errlog, app_key);
ret = constr(elm->type, memb_ptr, ctfailcb, app_key);
if(ret) return ret;
}

View File

@ -1,9 +1,9 @@
#include <asn_internal.h>
#include <constraints.h>
#include "asn_internal.h"
#include "constraints.h"
int
asn_generic_no_constraint(asn_TYPE_descriptor_t *type_descriptor,
const void *struct_ptr, asn_app_consume_bytes_f *cb, void *key) {
const void *struct_ptr, asn_app_constraint_failed_f *cb, void *key) {
(void)type_descriptor; /* Unused argument */
(void)struct_ptr; /* Unused argument */
@ -16,7 +16,7 @@ asn_generic_no_constraint(asn_TYPE_descriptor_t *type_descriptor,
int
asn_generic_unknown_constraint(asn_TYPE_descriptor_t *type_descriptor,
const void *struct_ptr, asn_app_consume_bytes_f *cb, void *key) {
const void *struct_ptr, asn_app_constraint_failed_f *cb, void *key) {
(void)type_descriptor; /* Unused argument */
(void)struct_ptr; /* Unused argument */
@ -27,98 +27,67 @@ asn_generic_unknown_constraint(asn_TYPE_descriptor_t *type_descriptor,
return 0;
}
struct __fill_errbuf_arg {
char *errbuf;
struct errbufDesc {
asn_TYPE_descriptor_t *failed_type;
const void *failed_struct_ptr;
char *errbuf;
size_t errlen;
size_t erroff;
};
static int
__fill_errbuf(const void *buffer, size_t size, void *app_key) {
struct __fill_errbuf_arg *arg = (struct __fill_errbuf_arg *)app_key;
size_t avail = arg->errlen - arg->erroff;
static void
_asn_i_ctfailcb(void *key, asn_TYPE_descriptor_t *td, const void *sptr, const char *fmt, ...) {
struct errbufDesc *arg = key;
va_list ap;
ssize_t vlen;
ssize_t maxlen;
if(avail > size)
avail = size + 1;
arg->failed_type = td;
arg->failed_struct_ptr = sptr;
switch(avail) {
default:
memcpy(arg->errbuf + arg->erroff, buffer, avail - 1);
arg->erroff += avail - 1;
case 1:
arg->errbuf[arg->erroff] = '\0';
case 0:
return 0;
maxlen = arg->errlen;
if(maxlen <= 0)
return;
va_start(ap, fmt);
vlen = vsnprintf(arg->errbuf, maxlen, fmt, ap);
va_end(ap);
if(vlen >= maxlen) {
arg->errbuf[maxlen-1] = '\0'; /* Ensuring libc correctness */
arg->errlen = maxlen - 1; /* Not counting termination */
return;
} else if(vlen >= 0) {
arg->errbuf[vlen] = '\0'; /* Ensuring libc correctness */
arg->errlen = vlen; /* Not counting termination */
} else {
/*
* The libc on this system is broken.
*/
vlen = sizeof("<broken vsnprintf>") - 1;
maxlen--;
arg->errlen = vlen < maxlen ? vlen : maxlen;
memcpy(arg->errbuf, "<broken vsnprintf>", arg->errlen);
arg->errbuf[arg->errlen] = 0;
}
return;
}
int
asn_check_constraints(asn_TYPE_descriptor_t *type_descriptor,
const void *struct_ptr, char *errbuf, size_t *errlen) {
const void *struct_ptr, char *errbuf, size_t *errlen) {
struct errbufDesc arg;
int ret;
if(errlen) {
struct __fill_errbuf_arg arg;
int ret;
arg.failed_type = 0;
arg.failed_struct_ptr = 0;
arg.errbuf = errbuf;
arg.errlen = errlen ? *errlen : 0;
arg.errbuf = errbuf;
arg.errlen = *errlen;
arg.erroff = 0;
ret = type_descriptor->check_constraints(type_descriptor,
struct_ptr, __fill_errbuf, &arg);
if(ret == -1)
*errlen = arg.erroff;
ret = type_descriptor->check_constraints(type_descriptor,
struct_ptr, _asn_i_ctfailcb, &arg);
if(ret == -1 && errlen)
*errlen = arg.errlen;
return ret;
} else {
return type_descriptor->check_constraints(type_descriptor,
struct_ptr, 0, 0);
}
return ret;
}
void
_asn_i_log_error(asn_app_consume_bytes_f *cb, void *key, const char *fmt, ...) {
char buf[64];
char *p;
va_list ap;
ssize_t ret;
size_t len;
va_start(ap, fmt);
ret = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if(ret < 0) {
/*
* The libc on this system is broken.
*/
ret = sizeof("<broken vsnprintf>") - 1;
memcpy(buf, "<broken vsnprintf>", ret + 1);
/* Fall through */
}
if(ret < (ssize_t)sizeof(buf)) {
(void)cb(buf, ret, key);
return;
}
/*
* More space required to hold the message.
*/
len = ret + 1;
p = (char *)alloca(len);
if(!p) return; /* Can fail on !x86. */
va_start(ap, fmt);
ret = vsnprintf(p, len, fmt, ap);
va_end(ap);
if(ret < 0 || ret >= (ssize_t)len) {
ret = sizeof("<broken vsnprintf>") - 1;
memcpy(buf, "<broken vsnprintf>", ret + 1);
p = buf;
}
(void)cb(p, ret, key);
}

View File

@ -1,5 +1,5 @@
/*-
* Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted