X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=odr%2Fodr.c;h=da919a5d687e9abcf917878b1db6337620ffa15d;hb=0442642bb213139271943552e5c1bc731ee96872;hp=bfa833462c74be85ea9a9a57b8b3bffe0bf7dc22;hpb=eb2d32be86dd9b1db4303f37dcd4bc3b8927e5ad;p=yaz-moved-to-github.git diff --git a/odr/odr.c b/odr/odr.c index bfa8334..da919a5 100644 --- a/odr/odr.c +++ b/odr/odr.c @@ -1,37 +1,26 @@ /* - * Copyright (C) 1994, Index Data I/S - * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: odr.c,v $ - * Revision 1.7 1995-03-10 11:44:41 quinn - * Fixed serious stack-bug in odr_cons_begin - * - * Revision 1.6 1995/03/08 12:12:15 quinn - * Added better error checking. - * - * Revision 1.5 1995/03/07 13:28:57 quinn - * *** empty log message *** - * - * Revision 1.4 1995/03/07 13:16:13 quinn - * Fixed bug in odr_reset - * - * Revision 1.3 1995/03/07 10:21:31 quinn - * odr_errno-->odr_error - * - * Revision 1.2 1995/03/07 10:19:05 quinn - * Addded some method functions to the ODR type. - * - * Revision 1.1 1995/03/07 09:23:15 quinn - * Installing top-level API and documentation. + * Copyright (c) 1995-2003, Index Data + * See the file LICENSE for details. * + * $Id: odr.c,v 1.45 2003-06-19 21:09:25 adam Exp $ * */ +#if HAVE_CONFIG_H +#include +#endif #include #include -#include +#include +#include "odr-priv.h" + +Odr_null *ODR_NULLVAL = (Odr_null *) "NULL"; /* the presence of a null value */ + +Odr_null *odr_nullval (void) +{ + return ODR_NULLVAL; +} char *odr_errlist[] = { @@ -44,12 +33,27 @@ char *odr_errlist[] = "Other error", "Protocol error", "Malformed data", - "Stack overflow" + "Stack overflow", + "Length of constructed type different from sum of members", + "Overflow writing definite length of constructed type", + "Bad HTTP Request" }; +char *odr_errmsg(int n) +{ + return odr_errlist[n]; +} + void odr_perror(ODR o, char *message) { - fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]); + const char *e = odr_getelement(o); + int err, x; + + err = odr_geterrorx(o, &x); + fprintf(stderr, "%s: %s (code %d:%d)", message, odr_errlist[err], err, x); + if (e && *e) + fprintf (stderr, " element %s", e); + fprintf(stderr, "\n"); } int odr_geterror(ODR o) @@ -57,49 +61,123 @@ int odr_geterror(ODR o) return o->error; } +int odr_geterrorx(ODR o, int *x) +{ + if (x) + *x = o->op->error_id; + return o->error; +} + +char *odr_getelement(ODR o) +{ + return o->op->element; +} + +void odr_seterror(ODR o, int error, int id) +{ + o->error = error; + o->op->error_id = id; + o->op->element[0] = '\0'; +} + +void odr_setelement(ODR o, const char *element) +{ + if (element) + { + strncpy(o->op->element, element, sizeof(o->op->element)-1); + o->op->element[sizeof(o->op->element)-1] = '\0'; + } +} + void odr_setprint(ODR o, FILE *file) { o->print = file; } +int odr_set_charset(ODR o, const char *to, const char *from) +{ + if (o->op->iconv_handle) + yaz_iconv_close (o->op->iconv_handle); + o->op->iconv_handle = 0; + if (to && from) + { + o->op->iconv_handle = yaz_iconv_open (to, from); + if (o->op->iconv_handle == 0) + return -1; + } + return 0; +} + +#include + ODR odr_createmem(int direction) { - struct odr *r; + ODR r; - if (!(r = malloc(sizeof(*r)))) - return 0; + if (!(r = (ODR)xmalloc(sizeof(*r)))) + return 0; r->direction = direction; - r->print = stdout; + r->print = stderr; r->buf = 0; - r->buflen = 0; + r->size = r->pos = r->top = 0; + r->can_grow = 1; + r->mem = nmem_create(); + r->enable_bias = 1; + r->op = (struct Odr_private *) xmalloc (sizeof(*r->op)); + r->op->odr_ber_tag.lclass = -1; + r->op->iconv_handle = 0; odr_reset(r); + yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, r); return r; } void odr_reset(ODR o) { - o->error = ONONE; + odr_seterror(o, ONONE, 0); o->bp = o->buf; - o->left = o->buflen; + odr_seek(o, ODR_S_SET, 0); + o->top = 0; o->t_class = -1; o->t_tag = -1; o->indent = 0; - o->stackp = -1; + o->op->stackp = -1; + nmem_reset(o->mem); + o->choice_bias = -1; + o->lenlen = 1; + if (o->op->iconv_handle != 0) + yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0); + yaz_log (LOG_DEBUG, "odr_reset o=%p", o); } void odr_destroy(ODR o) { - free(o); + nmem_destroy(o->mem); + if (o->buf && o->can_grow) + xfree(o->buf); + if (o->print && o->print != stderr) + fclose(o->print); + if (o->op->iconv_handle != 0) + yaz_iconv_close (o->op->iconv_handle); + xfree(o->op); + xfree(o); + yaz_log (LOG_DEBUG, "odr_destroy o=%p", o); } -void odr_setbuf(ODR o, char *buf, int len) +void odr_setbuf(ODR o, char *buf, int len, int can_grow) { - o->buf = o->bp = (unsigned char *) buf; - o->buflen = o->left = len; + o->bp = (unsigned char *) buf; + + o->buf = (unsigned char *) buf; + o->can_grow = can_grow; + o->top = o->pos = 0; + o->size = len; } -char *odr_getbuf(ODR o, int *len) +char *odr_getbuf(ODR o, int *len, int *size) { - *len = o->bp - o->buf; - return (char *) o->buf; + *len = o->top; + if (size) + *size = o->size; + return (char*) o->buf; } +