From 0a7d4354288c747883261e9f430a7e0069225a5b Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 11 Aug 2004 12:15:38 +0000 Subject: [PATCH] Added odr_set_stream which is is a more generic to odr_setprint. odr_set_stream takes a stream handle, pointer to puts function and pointer to close function. The close function - if non-NULL - will be called during odr_destroy. --- NEWS | 5 ++++ include/yaz/odr.h | 11 ++++++-- src/odr-priv.h | 4 ++- src/odr.c | 80 +++++++++++++++++++++++++++++++++++++++-------------- src/odr_any.c | 4 +-- src/odr_bit.c | 4 +-- src/odr_bool.c | 4 +-- src/odr_choice.c | 4 +-- src/odr_cons.c | 6 ++-- src/odr_enum.c | 4 +-- src/odr_int.c | 4 +-- src/odr_null.c | 4 +-- src/odr_oct.c | 14 +++++----- src/odr_oid.c | 8 +++--- src/odr_util.c | 6 ++-- src/zgdu.c | 12 ++++---- 16 files changed, 114 insertions(+), 60 deletions(-) diff --git a/NEWS b/NEWS index b4d968d..0d286dc 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,10 @@ Possible compatibility problems with earlier versions marked with '*'. +Added odr_set_stream which is is a more generic to odr_setprint. +odr_set_stream takes a stream handle, pointer to puts function and +pointer to close function. The close function - if non-NULL - will be +called during odr_destroy. + --- 2.0.23 2004/08/11 Fix buffer overrun in CQL parser when dealing with proximity (%). diff --git a/include/yaz/odr.h b/include/yaz/odr.h index 1a2e690..3dd1478 100644 --- a/include/yaz/odr.h +++ b/include/yaz/odr.h @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: odr.h,v 1.14 2003-11-26 16:24:04 mike Exp $ + * $Id: odr.h,v 1.15 2004-08-11 12:15:38 adam Exp $ */ #ifndef ODR_H @@ -143,7 +143,7 @@ typedef struct odr int choice_bias; /* force choice */ int lenlen; /* force length-of-lenght (odr_setlen()) */ - FILE *print; /* output file for direction print */ + FILE *print; /* output file handler for direction print */ int indent; /* current indent level for printing */ NMEM mem; /* memory handle for decoding (primarily) */ @@ -327,6 +327,13 @@ YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt, YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from); +YAZ_EXPORT void odr_set_stream(ODR o, void *handle, + void (*stream_puts)(void *handle, + const char *strz), + void (*stream_close)(void *handle)); + +YAZ_EXPORT void odr_printf(ODR o, const char *fmt, ...); + YAZ_END_CDECL #include diff --git a/src/odr-priv.h b/src/odr-priv.h index 6aef58b..ce69d09 100644 --- a/src/odr-priv.h +++ b/src/odr-priv.h @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: odr-priv.h,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr-priv.h,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #ifndef ODR_PRIV_H @@ -53,6 +53,8 @@ struct Odr_private { yaz_iconv_t iconv_handle; int error_id; char element[80]; + void (*stream_puts)(void *handle, const char *strz); + void (*stream_close)(void *handle); }; /* Private macro. diff --git a/src/odr.c b/src/odr.c index acc7b19..7a0dab1 100644 --- a/src/odr.c +++ b/src/odr.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 1995-2003, Index Data + * Copyright (c) 1995-2004, Index Data * See the file LICENSE for details. * - * $Id: odr.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr.c,v 1.2 2004-08-11 12:15:38 adam Exp $ * */ #if HAVE_CONFIG_H @@ -11,6 +11,7 @@ #include #include +#include #include #include "odr-priv.h" @@ -89,9 +90,30 @@ void odr_setelement(ODR o, const char *element) } } +void odr_FILE_puts(void *handle, const char *strz) +{ + fputs(strz, (FILE*) handle); +} + +void odr_FILE_close(void *handle) +{ + FILE *f = (FILE *) handle; + if (f && f != stderr && f != stdout) + fclose(f); +} + void odr_setprint(ODR o, FILE *file) { - o->print = file; + odr_set_stream(o, file, odr_FILE_puts, odr_FILE_close); +} + +void odr_set_stream(ODR o, void *handle, + void (*stream_puts)(void *handle, const char *strz), + void (*stream_close)(void *handle)) +{ + o->print = handle; + o->op->stream_puts = stream_puts; + o->op->stream_close = stream_close; } int odr_set_charset(ODR o, const char *to, const char *from) @@ -112,23 +134,23 @@ int odr_set_charset(ODR o, const char *to, const char *from) ODR odr_createmem(int direction) { - ODR r; + ODR o; - if (!(r = (ODR)xmalloc(sizeof(*r)))) + if (!(o = (ODR)xmalloc(sizeof(*o)))) return 0; - r->direction = direction; - r->print = stderr; - r->buf = 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; + o->direction = direction; + o->buf = 0; + o->size = o->pos = o->top = 0; + o->can_grow = 1; + o->mem = nmem_create(); + o->enable_bias = 1; + o->op = (struct Odr_private *) xmalloc (sizeof(*o->op)); + o->op->odr_ber_tag.lclass = -1; + o->op->iconv_handle = 0; + odr_setprint(o, stderr); + odr_reset(o); + yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, o); + return o; } void odr_reset(ODR o) @@ -154,8 +176,8 @@ void odr_destroy(ODR 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->stream_close) + o->op->stream_close(o->print); if (o->op->iconv_handle != 0) yaz_iconv_close (o->op->iconv_handle); xfree(o->op); @@ -181,3 +203,21 @@ char *odr_getbuf(ODR o, int *len, int *size) return (char*) o->buf; } +void odr_printf(ODR o, const char *fmt, ...) +{ + va_list ap; + char buf[4096]; + + va_start(ap, fmt); +#ifdef WIN32 + _vsnprintf(buf, sizeof(buf)-1, fmt, ap); +#else +#if HAVE_VSNPRINTF + vsnprintf(buf, sizeof(buf), fmt, ap); +#else + vsprintf(buf, fmt, ap); +#endif +#endif + o->op->stream_puts(o->print, buf); + va_end(ap); +} diff --git a/src/odr_any.c b/src/odr_any.c index ac1a4f5..78d77ae 100644 --- a/src/odr_any.c +++ b/src/odr_any.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_any.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_any.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -24,7 +24,7 @@ int odr_any(ODR o, Odr_any **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "ANY (len=%d)\n", (*p)->len); + odr_printf(o, "ANY (len=%d)\n", (*p)->len); return 1; } if (o->direction == ODR_DECODE) diff --git a/src/odr_bit.c b/src/odr_bit.c index 05ef420..11a1d2e 100644 --- a/src/odr_bit.c +++ b/src/odr_bit.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_bit.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_bit.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -34,7 +34,7 @@ int odr_bitstring(ODR o, Odr_bitmask **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "BITSTRING(len=%d)\n",(*p)->top + 1); + odr_printf(o, "BITSTRING(len=%d)\n",(*p)->top + 1); return 1; } if (o->direction == ODR_DECODE) diff --git a/src/odr_bool.c b/src/odr_bool.c index 36c858b..151d050 100644 --- a/src/odr_bool.c +++ b/src/odr_bool.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_bool.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_bool.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -34,7 +34,7 @@ int odr_bool(ODR o, int **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "%s\n", (**p ? "TRUE" : "FALSE")); + odr_printf(o, "%s\n", (**p ? "TRUE" : "FALSE")); return 1; } if (cons) diff --git a/src/odr_choice.c b/src/odr_choice.c index 53b6516..bc31ddf 100644 --- a/src/odr_choice.c +++ b/src/odr_choice.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_choice.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_choice.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -33,7 +33,7 @@ int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp, if (name) { odr_prname(o, name); - fprintf (o->print, "choice\n"); + odr_printf(o, "choice\n"); } } for (i = 0; arm[i].fun; i++) diff --git a/src/odr_cons.c b/src/odr_cons.c index 7f455dd..48c327f 100644 --- a/src/odr_cons.c +++ b/src/odr_cons.c @@ -2,7 +2,7 @@ * Copyright (c) 1995-2003, Index Data * See the file LICENSE for details. * - * $Id: odr_cons.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_cons.c,v 1.2 2004-08-11 12:15:38 adam Exp $ * */ #if HAVE_CONFIG_H @@ -79,7 +79,7 @@ int odr_constructed_begin(ODR o, void *p, int zclass, int tag, else if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "{\n"); + odr_printf(o, "{\n"); o->indent++; } else @@ -171,7 +171,7 @@ int odr_constructed_end(ODR o) o->op->stackp--; o->indent--; odr_prname(o, 0); - fprintf(o->print, "}\n"); + odr_printf(o, "}\n"); return 1; default: odr_seterror(o, OOTHER, 38); diff --git a/src/odr_enum.c b/src/odr_enum.c index 45710f6..76aadd0 100644 --- a/src/odr_enum.c +++ b/src/odr_enum.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_enum.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_enum.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -33,7 +33,7 @@ int odr_enum(ODR o, int **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "%d\n", **p); + odr_printf(o, "%d\n", **p); return 1; } if (cons) diff --git a/src/odr_int.c b/src/odr_int.c index 33e86fb..9e21a25 100644 --- a/src/odr_int.c +++ b/src/odr_int.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_int.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_int.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -33,7 +33,7 @@ int odr_integer(ODR o, int **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "%d\n", **p); + odr_printf(o, "%d\n", **p); return 1; } if (cons) diff --git a/src/odr_null.c b/src/odr_null.c index 940ca9d..ac79240 100644 --- a/src/odr_null.c +++ b/src/odr_null.c @@ -2,7 +2,7 @@ * Copyright (c) 1995-2004, Index Data * See the file LICENSE for details. * - * $Id: odr_null.c,v 1.2 2004-03-09 20:49:04 adam Exp $ + * $Id: odr_null.c,v 1.3 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -32,7 +32,7 @@ int odr_null(ODR o, Odr_null **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "NULL\n"); + odr_printf(o, "NULL\n"); return 1; } if (cons) diff --git a/src/odr_oct.c b/src/odr_oct.c index f8e2682..27cd9b0 100644 --- a/src/odr_oct.c +++ b/src/odr_oct.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_oct.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_oct.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -35,17 +35,17 @@ int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name) { int i; odr_prname(o, name); - fprintf(o->print, "OCTETSTRING(len=%d)", (*p)->len); + odr_printf(o, "OCTETSTRING(len=%d)", (*p)->len); for (i = 0; i<(*p)->len; i++) { if (i < 5 || i > ((*p)->len - 4)) { - fprintf (o->print, " %02X", (*p)->buf[i]); + odr_printf(o, " %02X", (*p)->buf[i]); } else if (i == 5) - fprintf (o->print, " .. "); + odr_printf(o, " .. "); } - fprintf(o->print, "\n"); + odr_printf(o, "\n"); return 1; } if (o->direction == ODR_DECODE) @@ -83,7 +83,7 @@ int odr_cstring(ODR o, char **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "'%s'\n", *p); + odr_printf(o, "'%s'\n", *p); return 1; } t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */ @@ -130,7 +130,7 @@ int odr_iconv_string(ODR o, char **p, int opt, const char *name) if (o->direction == ODR_PRINT) { odr_prname(o, name); - fprintf(o->print, "'%s'\n", *p); + odr_printf(o, "'%s'\n", *p); return 1; } t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */ diff --git a/src/odr_oid.c b/src/odr_oid.c index 5ff3f18..056395c 100644 --- a/src/odr_oid.c +++ b/src/odr_oid.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: odr_oid.c,v 1.1 2003-10-27 12:21:33 adam Exp $ + * $Id: odr_oid.c,v 1.2 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -41,10 +41,10 @@ int odr_oid(ODR o, Odr_oid **p, int opt, const char *name) int i; odr_prname(o, name); - fprintf(o->print, "OID:"); + odr_printf(o, "OID:"); for (i = 0; (*p)[i] > -1; i++) - fprintf(o->print, " %d", (*p)[i]); - fprintf(o->print, "\n"); + odr_printf(o, " %d", (*p)[i]); + odr_printf(o, "\n"); return 1; } if (o->direction == ODR_DECODE) diff --git a/src/odr_util.c b/src/odr_util.c index 2566143..dc37ca3 100644 --- a/src/odr_util.c +++ b/src/odr_util.c @@ -2,7 +2,7 @@ * Copyright (c) 1995-2004, Index Data * See the file LICENSE for details. * - * $Id: odr_util.c,v 1.3 2004-01-05 14:46:52 adam Exp $ + * $Id: odr_util.c,v 1.4 2004-08-11 12:15:38 adam Exp $ */ #if HAVE_CONFIG_H #include @@ -17,9 +17,9 @@ void odr_prname(ODR o, const char *name) { if (name) - fprintf (o->print, "%*s%s ", o->indent*4, "", name); + odr_printf(o, "%*s%s ", o->indent*4, "", name); else - fprintf (o->print, "%*s", o->indent*4, ""); + odr_printf(o, "%*s", o->indent*4, ""); } int odp_more_chunks(ODR o, const unsigned char *base, int len) diff --git a/src/zgdu.c b/src/zgdu.c index 0469d5f..fcd7be9 100644 --- a/src/zgdu.c +++ b/src/zgdu.c @@ -2,7 +2,7 @@ * Copyright (c) 2002-2004, Index Data. * See the file LICENSE for details. * - * $Id: zgdu.c,v 1.9 2004-02-25 12:59:56 adam Exp $ + * $Id: zgdu.c,v 1.10 2004-08-11 12:15:38 adam Exp $ */ #include @@ -380,9 +380,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) (*p)->u.HTTP_Response->content_len); if (o->direction == ODR_PRINT) { - fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top - top0, - o->buf + top0); - fprintf(o->print, "-- \n"); + odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0, + o->buf + top0); + odr_printf(o, "-- \n"); } break; case Z_GDU_HTTP_Request: @@ -418,9 +418,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) (*p)->u.HTTP_Request->content_len); if (o->direction == ODR_PRINT) { - fprintf(o->print, "-- HTTP request:\n%.*s\n", o->top - top0, + odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0, o->buf + top0); - fprintf(o->print, "-- \n"); + odr_printf(o, "-- \n"); } break; case Z_GDU_Z3950: -- 1.7.10.4