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