From b0df126f997b4d9f51be961a2fdbd4ebea001b84 Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Mon, 6 Feb 1995 16:45:03 +0000 Subject: [PATCH] Small mods. --- odr/odr_seq.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++-- odr/test.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/odr/odr_seq.c b/odr/odr_seq.c index 97ddc47..68c0c55 100644 --- a/odr/odr_seq.c +++ b/odr/odr_seq.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: odr_seq.c,v $ - * Revision 1.1 1995-02-02 16:21:54 quinn + * Revision 1.2 1995-02-06 16:45:03 quinn + * Small mods. + * + * Revision 1.1 1995/02/02 16:21:54 quinn * First kick. * */ @@ -23,7 +26,7 @@ int odr_sequence_begin(ODR o, void *p, int size) if (odr_constructed_begin(o, p, o->t_class, o->t_tag, 0)) { - if (o->direction == ODR_DECODE) + if (o->direction == ODR_DECODE && size) *pp = nalloc(o, size); return 1; } @@ -35,3 +38,49 @@ int odr_sequence_end(ODR o) { return odr_constructed_end(o); } + +int odr_sequence_more(ODR o) +{ + if (o->stackp < 0) + return 0; + if (o->stack[o->stackp].len >= 0) + return o->bp - o->stack[o->stackp].base < o->stack[o->stackp].len; + else + return (!(*o->bp == 0 && *(o->bp + 1) == 0)); +} + +int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num) +{ + char **pp = (char**) p; /* for dereferencing */ + char *tmp; + int size = 0; + + if (!odr_sequence_begin(o, p, 0)) + return 0; + + if (o->direction = ODR_DECODE) + *num = 0; + while (odr_sequence_more(o)) + { + /* outgrown array? */ + if (*num * sizeof(void*) >= size) + { + /* double the buffer size */ + tmp = nalloc(o, sizeof(void*) * (size += size ? size : + 128)); + if (*num) + { + memcpy(tmp, *pp, *num * sizeof(void*)); + /* + * For now, we just throw the old *p away, since we use + * nibble memory anyway (disgusting, isn't it?). + */ + *pp = tmp; + } + } + if (!(*type)(o, (*pp)[*num++], 0)) + return 0; + *num++; + } + return odr_sequence_end(o); +} diff --git a/odr/test.c b/odr/test.c index 894ef1b..6da7b89 100644 --- a/odr/test.c +++ b/odr/test.c @@ -49,6 +49,38 @@ int z_InitRequest(ODR o, Z_InitRequest **p, int opt) odr_sequence_end(o); } +struct A +{ + int which; + union + { + int *b; /* integer */ + char *c; /* visstring */ + } u; +}; + +int f_A(ODR o, struct A **p, int opt) +{ + int res; + Odr_arm arm[] = + { + { -1, -1, -1, 0, (Odr_fun) odr_integer }, + { ODR_IMPLICIT, ODR_CONTEXT, 200, 1, (Odr_fun) odr_visiblestring }, + { -1, -1, -1, -1, 0 } + }; + + if (o->direction == ODR_DECODE && !*p) + *p = nalloc(o, sizeof(**p)); + res = odr_choice(o, arm, &(*p)->u, &(*p)->which); + if (!res) + { + *p = 0; + return opt; + } + return 1; +} + +#if 0 int main() { int i; @@ -96,3 +128,32 @@ int main() odr_oid(&o, &oidp2, 0); } +#endif + +int main() +{ + struct A ch, *chp1, *chp2; + int b = 9999; + char *c = "Nu tester vi en satans forpulet CHOICE!"; + struct odr o; + unsigned char buf[4096]; + + ch.u.c = c; + ch.which = 1; + chp1 = &ch; + + o.buf = buf; + o.bp=o.buf; + o.left = o.buflen = 1024; + o.direction = ODR_ENCODE; + o.t_class = -1; + + f_A(&o, &chp1, 0); + + o.direction = ODR_DECODE; + o.bp = o.buf; + + f_A(&o, &chp2, 0); + + return 0; +} -- 1.7.10.4