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