PQF native support for SRW ;)
[yaz-moved-to-github.git] / odr / odr.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: odr.c,v 1.40 2003-02-12 15:06:43 adam Exp $
6  *
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <yaz/xmalloc.h>
16 #include "odr-priv.h"
17
18 Odr_null *ODR_NULLVAL = (Odr_null *) "NULL";  /* the presence of a null value */
19
20 Odr_null *odr_nullval (void)
21 {
22     return ODR_NULLVAL;
23 }
24
25 char *odr_errlist[] =
26 {
27     "No (unknown) error",
28     "Memory allocation failed",
29     "System error",
30     "No space in buffer",
31     "Required data element missing",
32     "Unexpected tag",
33     "Other error",
34     "Protocol error",
35     "Malformed data",
36     "Stack overflow",
37     "Length of constructed type different from sum of members",
38     "Overflow writing definite length of constructed type",
39     "HTTP Bad Request"
40 };
41
42 char *odr_errmsg(int n)
43 {
44     return odr_errlist[n];
45 }
46
47 void odr_perror(ODR o, char *message)
48 {
49     fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
50 }
51
52 int odr_geterror(ODR o)
53 {
54     return o->error;
55 }
56
57 void odr_setprint(ODR o, FILE *file)
58 {
59     o->print = file;
60 }
61
62 int odr_set_charset(ODR o, const char *to, const char *from)
63 {
64     if (o->op->iconv_handle)
65         yaz_iconv_close (o->op->iconv_handle);
66
67     o->op->iconv_handle = yaz_iconv_open (to, from);
68     if (o->op->iconv_handle == 0)
69         return -1;
70     return 0;
71 }
72
73 #include <yaz/log.h>
74
75 ODR odr_createmem(int direction)
76 {
77     ODR r;
78
79     if (!(r = (ODR)xmalloc(sizeof(*r))))
80         return 0;
81     r->direction = direction;
82     r->print = stderr;
83     r->buf = 0;
84     r->size = r->pos = r->top = 0;
85     r->can_grow = 1;
86     r->mem = nmem_create();
87     r->enable_bias = 1;
88     r->op = (struct Odr_private *) xmalloc (sizeof(*r->op));
89     r->op->odr_ber_tag.lclass = -1;
90     r->op->iconv_handle = 0;
91     odr_reset(r);
92     yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, r);
93     return r;
94 }
95
96 void odr_reset(ODR o)
97 {
98     o->error = ONONE;
99     o->bp = o->buf;
100     odr_seek(o, ODR_S_SET, 0);
101     o->top = 0;
102     o->t_class = -1;
103     o->t_tag = -1;
104     o->indent = 0;
105     o->op->stackp = -1;
106     nmem_reset(o->mem);
107     o->choice_bias = -1;
108     o->lenlen = 1;
109     if (o->op->iconv_handle != 0)
110         yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
111     yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
112 }
113     
114 void odr_destroy(ODR o)
115 {
116     nmem_destroy(o->mem);
117     if (o->buf && o->can_grow)
118        xfree(o->buf);
119     if (o->print && o->print != stderr)
120         fclose(o->print);
121     if (o->op->iconv_handle != 0)
122         yaz_iconv_close (o->op->iconv_handle);
123     xfree(o->op);
124     xfree(o);
125     yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
126 }
127
128 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
129 {
130     o->bp = (unsigned char *) buf;
131
132     o->buf = (unsigned char *) buf;
133     o->can_grow = can_grow;
134     o->top = o->pos = 0;
135     o->size = len;
136 }
137
138 char *odr_getbuf(ODR o, int *len, int *size)
139 {
140     *len = o->top;
141     if (size)
142         *size = o->size;
143     return (char*) o->buf;
144 }