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