Added body-of-text to BIB-1 ANY and the WAIS profile
[yaz-moved-to-github.git] / odr / odr.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr.c,v $
7  * Revision 1.20  1995-11-08 17:41:32  quinn
8  * Smallish.
9  *
10  * Revision 1.19  1995/11/01  13:54:41  quinn
11  * Minor adjustments
12  *
13  * Revision 1.18  1995/09/29  17:12:22  quinn
14  * Smallish
15  *
16  * Revision 1.17  1995/09/29  17:01:50  quinn
17  * More Windows work
18  *
19  * Revision 1.16  1995/09/27  15:02:57  quinn
20  * Modified function heads & prototypes.
21  *
22  * Revision 1.15  1995/08/15  12:00:22  quinn
23  * Updated External
24  *
25  * Revision 1.14  1995/06/19  12:38:46  quinn
26  * Added BER dumper.
27  *
28  * Revision 1.13  1995/05/22  11:32:02  quinn
29  * Fixing Interface to odr_null.
30  *
31  * Revision 1.12  1995/05/16  08:50:49  quinn
32  * License, documentation, and memory fixes
33  *
34  * Revision 1.11  1995/05/15  11:56:08  quinn
35  * More work on memory management.
36  *
37  * Revision 1.10  1995/04/18  08:15:20  quinn
38  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
39  * neater. We'll make the same change for decoding one day.
40  *
41  * Revision 1.9  1995/04/10  10:23:11  quinn
42  * Smallish changes.
43  *
44  * Revision 1.8  1995/03/17  10:17:43  quinn
45  * Added memory management.
46  *
47  * Revision 1.7  1995/03/10  11:44:41  quinn
48  * Fixed serious stack-bug in odr_cons_begin
49  *
50  * Revision 1.6  1995/03/08  12:12:15  quinn
51  * Added better error checking.
52  *
53  * Revision 1.5  1995/03/07  13:28:57  quinn
54  * *** empty log message ***
55  *
56  * Revision 1.4  1995/03/07  13:16:13  quinn
57  * Fixed bug in odr_reset
58  *
59  * Revision 1.3  1995/03/07  10:21:31  quinn
60  * odr_errno-->odr_error
61  *
62  * Revision 1.2  1995/03/07  10:19:05  quinn
63  * Addded some method functions to the ODR type.
64  *
65  * Revision 1.1  1995/03/07  09:23:15  quinn
66  * Installing top-level API and documentation.
67  *
68  *
69  */
70
71 #include <stdio.h>
72 #include <stdlib.h>
73
74 #include <xmalloc.h>
75 #include <odr.h>
76
77 Odr_null *ODR_NULLVAL = "NULL";  /* the presence of a null value */
78
79 char *odr_errlist[] =
80 {
81     "No (unknown) error",
82     "Memory allocation failed",
83     "System error",
84     "No space in buffer",
85     "Required data element missing",
86     "Unexpected tag",
87     "Other error",
88     "Protocol error",
89     "Malformed data",
90     "Stack overflow",
91     "Length of constructed type different from sum of members"
92 };
93
94 char *odr_errmsg(int n)
95 {
96     return odr_errlist[n];
97 }
98
99 void odr_perror(ODR o, char *message)
100 {
101     fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
102 }
103
104 int odr_geterror(ODR o)
105 {
106     return o->error;
107 }
108
109 void odr_setprint(ODR o, FILE *file)
110 {
111     o->print = file;
112 }
113
114 ODR odr_createmem(int direction)
115 {
116     struct odr *r;
117
118     if (!(r = xmalloc(sizeof(*r))))
119         return 0;
120     r->direction = direction;
121     r->print = stderr;
122     r->buf = 0;
123     r->ecb.buf = 0;
124     r->ecb.size = r->ecb.pos = r->ecb.top = 0;
125     r->ecb.can_grow = 1;
126     r->buflen = 0;
127     r->mem = nmem_create();
128     odr_reset(r);
129     return r;
130 }
131
132 void odr_reset(ODR o)
133 {
134     o->error = ONONE;
135     o->bp = o->buf;
136     odr_seek(o, ODR_S_SET, 0);
137     o->ecb.top = 0;
138     o->left = o->buflen;
139     o->t_class = -1;
140     o->t_tag = -1;
141     o->indent = 0;
142     o->stackp = -1;
143     nmem_reset(o->mem);
144     o->choice_bias = -1;
145 }
146     
147 void odr_destroy(ODR o)
148 {
149     nmem_destroy(o->mem);
150     if (o->ecb.buf && o->ecb.can_grow)
151        xfree(o->ecb.buf);
152     if (o->print != stderr)
153         fclose(o->print);
154    xfree(o);
155 }
156
157 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
158 {
159     o->buf = o->bp = (unsigned char *) buf;
160     o->buflen = o->left = len;
161
162     o->ecb.buf = (unsigned char *) buf;
163     o->ecb.can_grow = can_grow;
164     o->ecb.top = o->ecb.pos = 0;
165     o->ecb.size = len;
166 }
167
168 char *odr_getbuf(ODR o, int *len, int *size)
169 {
170     *len = o->ecb.top;
171     if (size)
172         *size = o->ecb.size;
173     return (char*) o->ecb.buf;
174 }