e26a748ccdb1b8ecba0d58afd5e11babae9bcab4
[yaz-moved-to-github.git] / odr / odr_cons.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Log: odr_cons.c,v $
6  * Revision 1.21  2000-01-31 13:15:21  adam
7  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
8  * that some characters are not surrounded by spaces in resulting term.
9  * ILL-code updates.
10  *
11  * Revision 1.20  1999/11/30 13:47:11  adam
12  * Improved installation. Moved header files to include/yaz.
13  *
14  * Revision 1.19  1999/04/20 09:56:48  adam
15  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
16  * Modified all encoders/decoders to reflect this change.
17  *
18  * Revision 1.18  1997/05/14 06:53:58  adam
19  * C++ support.
20  *
21  * Revision 1.17  1996/10/23 12:31:24  adam
22  * Added 'static' modifier to dummy variable in odr_constructed_begin.
23  *
24  * Revision 1.16  1996/07/26  13:38:20  quinn
25  * Various smaller things. Gathered header-files.
26  *
27  * Revision 1.15  1995/09/29  17:12:23  quinn
28  * Smallish
29  *
30  * Revision 1.14  1995/09/27  15:02:58  quinn
31  * Modified function heads & prototypes.
32  *
33  * Revision 1.13  1995/08/15  11:16:39  quinn
34  * Fixed pretty-printers.
35  *
36  * Revision 1.12  1995/06/19  12:38:47  quinn
37  * Added BER dumper.
38  *
39  * Revision 1.11  1995/05/16  08:50:53  quinn
40  * License, documentation, and memory fixes
41  *
42  * Revision 1.10  1995/04/18  08:15:21  quinn
43  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
44  * neater. We'll make the same change for decoding one day.
45  *
46  * Revision 1.9  1995/03/28  09:15:49  quinn
47  * Fixed bug in the printing mode
48  *
49  * Revision 1.8  1995/03/15  11:18:04  quinn
50  * Fixed serious bug in odr_cons
51  *
52  * Revision 1.7  1995/03/10  11:44:41  quinn
53  * Fixed serious stack-bug in odr_cons_begin
54  *
55  * Revision 1.6  1995/03/08  12:12:23  quinn
56  * Added better error checking.
57  *
58  * Revision 1.5  1995/02/10  18:57:25  quinn
59  * More in the way of error-checking.
60  *
61  * Revision 1.4  1995/02/10  15:55:29  quinn
62  * Bug fixes, mostly.
63  *
64  * Revision 1.3  1995/02/09  15:51:48  quinn
65  * Works better now.
66  *
67  * Revision 1.2  1995/02/07  17:52:59  quinn
68  * A damn mess, but now things work, I think.
69  *
70  * Revision 1.1  1995/02/02  16:21:53  quinn
71  * First kick.
72  *
73  */
74
75 #include <yaz/odr.h>
76
77 void odr_setlenlen(ODR o, int len)
78 {
79     o->lenlen = len;
80 }
81
82 int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
83                           const char *name)
84 {
85     int res;
86     int cons = 1;
87     int lenlen = o->lenlen;
88
89     if (o->error)
90         return 0;
91     o->lenlen = 1; /* reset lenlen */
92     if (o->t_class < 0)
93     {
94         o->t_class = zclass;
95         o->t_tag = tag;
96     }
97     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, 1)) < 0)
98         return 0;
99     if (!res || !cons)
100         return 0;
101
102     if (o->stackp == ODR_MAX_STACK - 1)
103     {
104         o->error = OSTACK;
105         return 0;
106     }
107     o->stack[++(o->stackp)].lenb = o->bp;
108     o->stack[o->stackp].len_offset = odr_tell(o);
109 #ifdef ODR_DEBUG
110     fprintf(stderr, "[cons_begin(%d)]", o->stackp);
111 #endif
112     if (o->direction == ODR_ENCODE)
113     {
114         static unsigned char dummy[sizeof(int)+1];
115
116         o->stack[o->stackp].lenlen = lenlen;
117
118         if (odr_write(o, dummy, lenlen) < 0) /* dummy */
119             return 0;
120     }
121     else if (o->direction == ODR_DECODE)
122     {
123         if ((res = ber_declen(o->bp, &o->stack[o->stackp].len)) < 0)
124             return 0;
125         o->stack[o->stackp].lenlen = res;
126         o->bp += res;
127     }
128     else if (o->direction == ODR_PRINT)
129     {
130         odr_prname(o, name);
131         fprintf(o->print, "{\n");
132         o->indent++;
133     }
134     else
135     {
136         o->error = OOTHER;
137         return 0;
138     }
139     o->stack[o->stackp].base = o->bp;
140     o->stack[o->stackp].base_offset = odr_tell(o);
141     return 1;
142 }
143
144 int odr_constructed_more(ODR o)
145 {
146     if (o->error)
147         return 0;
148     if (o->stackp < 0)
149         return 0;
150     if (o->stack[o->stackp].len >= 0)
151         return o->bp - o->stack[o->stackp].base < o->stack[o->stackp].len;
152     else
153         return (!(*o->bp == 0 && *(o->bp + 1) == 0));
154 }
155
156 int odr_constructed_end(ODR o)
157 {
158     int res;
159     int pos;
160
161     if (o->error)
162         return 0;
163     if (o->stackp < 0)
164     {
165         o->error = OOTHER;
166         return 0;
167     }
168     switch (o->direction)
169     {
170         case ODR_DECODE:
171             if (o->stack[o->stackp].len < 0)
172             {
173                 if (*o->bp++ == 0 && *(o->bp++) == 0)
174                 {
175                     o->stackp--;
176                     return 1;
177                 }
178                 else
179                 {
180                     o->error = OOTHER;
181                     return 0;
182                 }
183             }
184             else if (o->bp - o->stack[o->stackp].base !=
185                 o->stack[o->stackp].len)
186             {
187                 o->error = OCONLEN;
188                 return 0;
189             }
190             o->stackp--;
191             return 1;
192         case ODR_ENCODE:
193             pos = odr_tell(o);
194             odr_seek(o, ODR_S_SET, o->stack[o->stackp].len_offset);
195             if ((res = ber_enclen(o, pos - o->stack[o->stackp].base_offset,
196                 o->stack[o->stackp].lenlen, 1)) < 0)
197             {
198                 o->error = OLENOV;
199                 return 0;
200             }
201             odr_seek(o, ODR_S_END, 0);
202             if (res == 0)   /* indefinite encoding */
203             {
204 #ifdef ODR_DEBUG
205                 fprintf(stderr, "[cons_end(%d): indefinite]", o->stackp);
206 #endif
207                 if (odr_putc(o, 0) < 0 || odr_putc(o, 0) < 0)
208                     return 0;
209             }
210 #ifdef ODR_DEBUG
211             else
212             {
213                 fprintf(stderr, "[cons_end(%d): definite]", o->stackp);
214             }
215 #endif
216             o->stackp--;
217             return 1;
218         case ODR_PRINT:
219             o->stackp--;
220             o->indent--;
221             odr_prname(o, 0);
222             fprintf(o->print, "}\n");
223             return 1;
224         default:
225             o->error = OOTHER;
226             return 0;
227     }
228 }