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