From 173d9f50b7a6c63dff6c74dc5c3efe985f75ef39 Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Thu, 2 Feb 1995 20:38:49 +0000 Subject: [PATCH] Updates. --- odr/ber_bit.c | 68 +++++++++++++++++++++++++++++++++++++++ odr/ber_oct.c | 28 ++++------------ odr/odr_bit.c | 48 ++++++++++++++++++++++++++++ odr/odr_oct.c | 52 +++++++++++++++++++++++++++++- odr/odr_util.c | 19 +++++++++++ odr/test.c | 97 +++++++++++++++++++++++++++++++++++++------------------- 6 files changed, 256 insertions(+), 56 deletions(-) create mode 100644 odr/ber_bit.c create mode 100644 odr/odr_bit.c diff --git a/odr/ber_bit.c b/odr/ber_bit.c new file mode 100644 index 0000000..ce8e21a --- /dev/null +++ b/odr/ber_bit.c @@ -0,0 +1,68 @@ +/* + * Copyright (C) 1994, Index Data I/S + * All rights reserved. + * Sebastian Hammer, Adam Dickmeiss + * + * $Log: ber_bit.c,v $ + * Revision 1.1 1995-02-02 20:38:49 quinn + * Updates. + * + * + */ + +#include + +int ber_bitstring(ODR o, ODR_BITMASK *p, int cons) +{ + int res, len; + unsigned char *base; + + switch (o->direction) + { + case ODR_DECODE: + if ((res = ber_declen(o->bp, &len)) < 0) + return 0; + o->bp += res; + o->left -= res; + if (cons) /* fetch component strings */ + { + base = o->bp; + while (odp_more_chunks(o, base, len)) + if (!odr_bitstring(o, &p, 0)) + return 0; + return 1; + } + /* primitive bitstring */ + if (len < 0) + return 0; + if (len == 0) + return 1; + if (len - 1 > ODR_BITMASK_SIZE) + return 0; + o->bp++; /* silently ignore the unused-bits field */ + o->left--; + len--; + memcpy(p->bits + p->top + 1, o->bp, len); + p->top += len; + o->bp += len; + o->left -= len; + return 1; + case ODR_ENCODE: + if ((res = ber_enclen(o->bp, p->top + 2, 5, 0)) < 0) + return 0; + o->bp += res; + o->left -= res; + if (p->top + 2 > o->left) + return 0; + *(o->bp++) = 0; /* no unused bits here */ + o->left--; + if (p->top < 0) + return 1; + memcpy(o->bp, p->bits, p->top + 1); + o->bp += p->top + 1; + o->left -= p->top +1; + return 1; + case ODR_PRINT: return 1; + default: return 0; + } +} diff --git a/odr/ber_oct.c b/odr/ber_oct.c index 41c568b..9b5a2fd 100644 --- a/odr/ber_oct.c +++ b/odr/ber_oct.c @@ -4,32 +4,16 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: ber_oct.c,v $ - * Revision 1.1 1995-02-02 16:21:52 quinn + * Revision 1.2 1995-02-02 20:38:50 quinn + * Updates. + * + * Revision 1.1 1995/02/02 16:21:52 quinn * First kick. * */ #include -static int more_chunks(ODR o, unsigned char *base, int len) -{ - if (!len) - return 0; - if (len < 0) /* indefinite length */ - { - if (*o->bp == 0 && *(o->bp + 1) == 0) - { - o->bp += 2; - o->left -= 2; - return 0; - } - else - return 1; - } - else - return o->bp - base < len; -} - int ber_octetstring(ODR o, ODR_OCT *p, int cons) { int res, len; @@ -45,7 +29,7 @@ int ber_octetstring(ODR o, ODR_OCT *p, int cons) if (cons) /* fetch component strings */ { base = o->bp; - while (more_chunks(o, base, len)) + while (odp_more_chunks(o, base, len)) if (!odr_octetstring(o, &p, 0)) return 0; return 1; @@ -57,7 +41,7 @@ int ber_octetstring(ODR o, ODR_OCT *p, int cons) return 1; if (len > p->size - p->len) { - c = nalloc(o, p->size += len); + c = nalloc(o, p->size += len + 1); if (p->len) memcpy(c, p->buf, p->len); p->buf = c; diff --git a/odr/odr_bit.c b/odr/odr_bit.c new file mode 100644 index 0000000..80434b7 --- /dev/null +++ b/odr/odr_bit.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 1994, Index Data I/S + * All rights reserved. + * Sebastian Hammer, Adam Dickmeiss + * + * $Log: odr_bit.c,v $ + * Revision 1.1 1995-02-02 20:38:50 quinn + * Updates. + * + * + */ + +#include +#include + +/* + * Top level bitstring string en/decoder. + * Returns 1 on success, 0 on error. + */ +int odr_bitstring(ODR o, ODR_BITMASK **p, int opt) +{ + int res, cons = 0; + + if (o->t_class < 0) + { + o->t_class = ODR_UNIVERSAL; + o->t_tag = ODR_BITSTRING; + } + if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0) + return 0; + if (!res) + { + *p = 0; + return opt; + } + if (o->direction == ODR_PRINT) + { + fprintf(o->print, "BITSTRING(len=%d)\n", (*p)->top + 1); + return 1; + } + if (o->direction == ODR_DECODE && !*p) + { + *p = nalloc(o, sizeof(ODR_BITMASK)); + memset((*p)->bits, 0, ODR_BITMASK_SIZE); + (*p)->top = -1; + } + return ber_bitstring(o, *p, cons); +} diff --git a/odr/odr_oct.c b/odr/odr_oct.c index c825983..4daccb3 100644 --- a/odr/odr_oct.c +++ b/odr/odr_oct.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: odr_oct.c,v $ - * Revision 1.1 1995-02-02 16:21:54 quinn + * Revision 1.2 1995-02-02 20:38:51 quinn + * Updates. + * + * Revision 1.1 1995/02/02 16:21:54 quinn * First kick. * */ @@ -45,3 +48,50 @@ int odr_octetstring(ODR o, ODR_OCT **p, int opt) } return ber_octetstring(o, *p, cons); } + +/* + * Friendlier interface to octetstring. + */ +int odr_visiblestring(ODR o, char **p, int opt) +{ + int cons = 0, res; + ODR_OCT *t; + + if (o->t_class < 0) + { + o->t_class = ODR_UNIVERSAL; + o->t_tag = ODR_VISIBLESTRING; + } + if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0) + return 0; + if (!res) + { + *p = 0; + return opt; + } + if (o->direction == ODR_PRINT) + { + fprintf(o->print, "'%s'\n", *p); + return 1; + } + t = nalloc(o, sizeof(ODR_OCT)); /* wrapper for octstring */ + if (o->direction == ODR_ENCODE) + { + t->buf = (unsigned char *) *p; + t->size = t->len = strlen(*p); + } + else + { + t->size= 0; + t->len = 0; + t->buf = 0; + } + if (!ber_octetstring(o, t, cons)) + return 0; + if (o->direction == ODR_DECODE) + { + *p = (char *) t->buf; + *(*p + t->len) = '\0'; /* ber_octs reserves space for this */ + } + return 1; +} diff --git a/odr/odr_util.c b/odr/odr_util.c index 6814c60..af26bae 100644 --- a/odr/odr_util.c +++ b/odr/odr_util.c @@ -3,3 +3,22 @@ void *nalloc(ODR o, int size) { return malloc(size); } char *odr_indent(ODR o) {return "";} + +int odp_more_chunks(ODR o, unsigned char *base, int len) +{ + if (!len) + return 0; + if (len < 0) /* indefinite length */ + { + if (*o->bp == 0 && *(o->bp + 1) == 0) + { + o->bp += 2; + o->left -= 2; + return 0; + } + else + return 1; + } + else + return o->bp - base < len; +} diff --git a/odr/test.c b/odr/test.c index 16f7c44..94e0aa9 100644 --- a/odr/test.c +++ b/odr/test.c @@ -1,41 +1,83 @@ #include - #include -int odr_dummy(ODR o, int **p, int opt) +typedef ODR_BITMASK Z_ReferenceId; + +typedef struct Z_InitRequest { - return odr_implicit(o, odr_integer, p, ODR_PRIVATE, 10, opt); -} + Z_ReferenceId *referenceId; /* OPTIONAL */ + ODR_BITMASK *options; + ODR_BITMASK *protocolVersion; + int *preferredMessageSize; + int *maximumRecordSize; + char *idAuthentication; /* OPTIONAL */ + char *implementationId; /* OPTIONAL */ + char *implementationName; /* OPTIONAL */ + char *implementationVersion; /* OPTIONAL */ +} Z_InitRequest; -struct dummy +int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt) { - int *alfa; - int *beta; -}; + return odr_implicit(o, odr_octetstring, (ODR_OCT**) p, ODR_CONTEXT, 2, opt); +} -int odr_dummy2(ODR o, struct dummy **p, int opt) +int z_InitRequest(ODR o, Z_InitRequest **p, int opt) { - struct dummy *pp; + Z_InitRequest *pp; - if (!odr_sequence_begin(o, p, sizeof(**p))) + if (!odr_sequence_begin(o, p, sizeof(Z_InitRequest))) return opt; pp = *p; return - odr_implicit(o, odr_integer, &pp->alfa, ODR_CONTEXT, 1, 1) && - odr_implicit(o, odr_integer, &pp->beta, ODR_CONTEXT, 2, 1) && - odr_sequence_end(o); + z_ReferenceId(o, &pp->referenceId, 1) && + odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, + 3, 0) && + odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) && + odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT, + 5, 0) && + odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT, + 6, 0) && + odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT, + 7, 1) && + odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT, + 110, 1) && + odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT, + 111, 1) && + odr_implicit(o, odr_visiblestring, &pp->implementationVersion, + ODR_CONTEXT, 112, 1) && + odr_sequence_end(o); } int main() { int i; - unsigned char buf[1024]; + unsigned char buf[4048]; struct odr o; - int test=-99999; - int *tp = &test, *tp2; - ODR_OCT bbb, *bbb1, *bbb2; - ODR_OCT ccc, *ccc1; - char *str1 = "FOO", *str2 = "BAR"; + Z_InitRequest ireq, *ireqp, *ireq2p; + ODR_BITMASK options, protocolVersion; + char *iId = "YAZ", *iName = "Yet Another Z39.50 Implementation", + *iVersion = "0.1"; + int maximumRS = 4096, preferredMS = 2048; + + ODR_MASK_ZERO(&protocolVersion); + ODR_MASK_SET(&protocolVersion, 0); + ODR_MASK_SET(&protocolVersion, 1); + + ODR_MASK_ZERO(&options); + ODR_MASK_SET(&options, 0); + ODR_MASK_SET(&options, 1); + ODR_MASK_SET(&options, 2); + + ireq.referenceId = 0; + ireq.protocolVersion = &protocolVersion; + ireq.options = &options; + ireq.preferredMessageSize = &preferredMS; + ireq.maximumRecordSize = &maximumRS; + ireq.idAuthentication = 0; + ireq.implementationId = iId; + ireq.implementationName = iName; + ireq.implementationVersion = iVersion; + ireqp = &ireq; o.buf = buf; o.bp=o.buf; @@ -43,21 +85,10 @@ int main() o.direction = ODR_ENCODE; o.t_class = -1; - bbb.buf = (unsigned char *) str1; - bbb.len = bbb.size = strlen(str1); - bbb1 = &bbb; - - ccc.buf = (unsigned char*) str2; - ccc.len = ccc.size = strlen(str2); - ccc1 = &ccc; - - odr_constructed_begin(&o, &bbb1, ODR_UNIVERSAL, ODR_OCTETSTRING, 0); - odr_octetstring(&o, &bbb1, 0); - odr_octetstring(&o, &ccc1, 0); - odr_constructed_end(&o); + z_InitRequest(&o, &ireqp, 0); o.direction = ODR_DECODE; o.bp = o.buf; - odr_octetstring(&o, &bbb2, 0); + z_InitRequest(&o, &ireq2p, 0); } -- 1.7.10.4