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