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