0a0976fbf0427efa2106d451519b0fa82ad8af03
[yaz-moved-to-github.git] / include / odr.h
1 /*
2  * Copyright (c) 1995, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * $Log: odr.h,v $
27  * Revision 1.12  1995-09-27 15:02:48  quinn
28  * Modified function heads & prototypes.
29  *
30  * Revision 1.11  1995/08/15  12:00:09  quinn
31  * Updated External
32  *
33  * Revision 1.10  1995/08/10  08:54:33  quinn
34  * Added Explain.
35  *
36  * Revision 1.9  1995/06/19  12:38:26  quinn
37  * Reorganized include-files. Added small features.
38  *
39  * Revision 1.8  1995/06/16  13:16:04  quinn
40  * Fixed Defaultdiagformat.
41  *
42  * Revision 1.7  1995/05/29  08:11:32  quinn
43  * Moved oid from odr/asn to util.
44  *
45  * Revision 1.6  1995/05/22  14:47:00  quinn
46  * ODR_NULLVAL --> void
47  *
48  * Revision 1.5  1995/05/22  11:31:24  quinn
49  * Added PDUs
50  *
51  * Revision 1.4  1995/05/16  08:50:33  quinn
52  * License, documentation, and memory fixes
53  *
54  * Revision 1.3  1995/05/15  11:55:54  quinn
55  * Work on asynchronous activity.
56  *
57  * Revision 1.2  1995/04/18  08:14:37  quinn
58  * Added dynamic memory allocation on encoding
59  *
60  * Revision 1.1  1995/03/30  09:39:41  quinn
61  * Moved .h files to include directory
62  *
63  * Revision 1.15  1995/03/29  15:39:57  quinn
64  * Fixed bugs in the bitmask operations
65  *
66  * Revision 1.14  1995/03/27  08:33:15  quinn
67  * Added more OID utilities.
68  *
69  * Revision 1.13  1995/03/17  10:17:44  quinn
70  * Added memory management.
71  *
72  * Revision 1.12  1995/03/14  10:27:38  quinn
73  * Modified makefile to use common lib
74  * Beginning to add memory management to odr
75  *
76  * Revision 1.11  1995/03/10  11:44:41  quinn
77  * Fixed serious stack-bug in odr_cons_begin
78  *
79  * Revision 1.10  1995/03/08  12:12:16  quinn
80  * Added better error checking.
81  *
82  * Revision 1.9  1995/03/07  10:10:00  quinn
83  * Added some headers for Adam.
84  *
85  * Revision 1.8  1995/03/07  09:23:16  quinn
86  * Installing top-level API and documentation.
87  *
88  * Revision 1.7  1995/02/10  15:55:29  quinn
89  * Bug fixes, mostly.
90  *
91  * Revision 1.6  1995/02/09  15:51:47  quinn
92  * Works better now.
93  *
94  * Revision 1.5  1995/02/07  17:52:59  quinn
95  * A damn mess, but now things work, I think.
96  *
97  * Revision 1.4  1995/02/06  16:45:03  quinn
98  * Small mods.
99  *
100  * Revision 1.3  1995/02/03  17:04:36  quinn
101  * *** empty log message ***
102  *
103  * Revision 1.2  1995/02/02  20:38:50  quinn
104  * Updates.
105  *
106  * Revision 1.1  1995/02/02  16:21:53  quinn
107  * First kick.
108  *
109  */
110
111 #ifndef ODR_H
112 #define ODR_H
113
114 #include <stdio.h>
115 #include <string.h>
116
117 #include <yconfig.h>
118
119 #ifndef bool_t
120 #define bool_t int
121 #endif
122
123 /*
124  * Tag modes
125  */
126 #define ODR_NONE -1
127 #define ODR_IMPLICIT 0
128 #define ODR_EXPLICIT 1
129
130 /*
131  * Classes
132  */
133 #define ODR_UNIVERSAL   0
134 #define ODR_APPLICATION 1
135 #define ODR_CONTEXT     2
136 #define ODR_PRIVATE     3
137
138 /*
139  * UNIVERSAL tags
140  */
141 #define ODR_BOOLEAN     1
142 #define ODR_INTEGER     2
143 #define ODR_BITSTRING   3
144 #define ODR_OCTETSTRING 4
145 #define ODR_NULL        5
146 #define ODR_OID         6
147 #define ODR_ODESC       7
148 #define ODR_EXTERNAL    8
149 #define ODR_REAL        9
150 #define ODR_ENUM        10
151 #define ODR_SEQUENCE    16
152 #define ODR_SET         17
153 #define ODR_NUMERICSTRING   18
154 #define ODR_PRINTABLESTRING 19
155 #define ODR_GENERALIZEDTIME 24
156 #define ODR_GRAPHICSTRING   25
157 #define ODR_VISIBLESTRING   26
158 #define ODR_GENERALSTRING   27
159
160 /*
161  * odr stream directions
162  */
163 #define ODR_DECODE      0
164 #define ODR_ENCODE      1
165 #define ODR_PRINT       2
166
167 typedef struct odr_oct
168 {
169     unsigned char *buf;
170     int len;
171     int size;
172 } Odr_oct;
173
174 typedef void Odr_null;
175 extern Odr_null *ODR_NULLVAL;
176
177 typedef Odr_oct Odr_any;
178
179 typedef struct odr_bitmask
180 {
181 #define ODR_BITMASK_SIZE 256
182     unsigned char bits[ODR_BITMASK_SIZE];
183     int top;
184 } Odr_bitmask;
185
186 typedef int Odr_oid;   /* terminate by -1 */
187
188 typedef struct odr_constack
189 {
190     unsigned char *base;         /* starting point of data */
191     int base_offset;
192     int len;                     /* length of data, if known, else -1
193                                         (decoding only) */
194     unsigned char *lenb;         /* where to encode length */
195     int len_offset;
196     int lenlen;                  /* length of length-field */
197 } odr_constack;
198
199 struct odr_memblock; /* defined in odr_mem.c */
200 typedef struct odr_memblock *ODR_MEM;
201
202 #define ODR_S_SET     0
203 #define ODR_S_CUR     1
204 #define ODR_S_END     2
205
206 typedef struct odr_ecblock
207 {
208     int can_grow;         /* are we allowed to reallocate */
209     unsigned char *buf;            /* memory handle */
210     int pos;              /* current position */
211     int top;              /* top of buffer */
212     int size;             /* current buffer size */
213 } odr_ecblock;
214
215
216 typedef struct odr
217 {
218     int direction;       /* the direction of this stream */
219
220     int error;           /* current error state (0==OK) */
221     unsigned char *buf;  /* for encoding or decoding */
222     int buflen;          /* size of buffer for encoding, len for decoding */
223     unsigned char *bp;   /* position in buffer */
224     int left;            /* bytes remaining in buffer */
225
226     odr_ecblock ecb;     /* memory control block */
227
228     int t_class;         /* implicit tagging (-1==default tag) */
229     int t_tag;
230
231     int choice_bias;     /* force choice */
232
233     FILE *print;         /* output file for direction print */
234     int indent;          /* current indent level for printing */
235
236     struct odr_memblock *mem;
237
238     /* stack for constructed types */
239 #define ODR_MAX_STACK 50
240     int stackp;          /* top of stack (-1 == initial state) */
241     odr_constack stack[ODR_MAX_STACK];
242 } *ODR;
243
244 typedef int (*Odr_fun)();
245
246 typedef struct odr_arm
247 {
248     int tagmode;
249     int class;
250     int tag;
251     int which;
252     Odr_fun fun;
253 } Odr_arm;
254
255 /*
256  * Error control.
257  */
258 #define ONONE           0
259 #define OMEMORY         1
260 #define OSYSERR         2
261 #define OSPACE          3
262 #define OREQUIRED       4
263 #define OUNEXPECTED     5
264 #define OOTHER          6
265 #define OPROTO          7
266 #define ODATA           8
267 #define OSTACK          9
268 #define OCONLEN        10
269
270 extern char *odr_errlist[];
271
272 int MDF odr_geterror(ODR o);
273 void MDF odr_perror(ODR o, char *message);
274 void MDF odr_setprint(ODR o, FILE *file);
275 ODR MDF odr_createmem(int direction);
276 void MDF odr_reset(ODR o);
277 void MDF odr_destroy(ODR o);
278 void MDF odr_setbuf(ODR o, char *buf, int len, int can_grow);
279 char MDF *odr_getbuf(ODR o, int *len, int *size);
280 void MDF *odr_malloc(ODR o, int size);
281 ODR_MEM MDF odr_extract_mem(ODR o);
282 void MDF odr_release_mem(ODR_MEM p);
283
284 #define odr_implicit(o, t, p, cl, tg, opt)\
285         (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
286
287 #define odr_explicit(o, t, p, cl, tg, opt)\
288         ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
289         t ((o), (p), (opt)) &&\
290         odr_constructed_end(o) : opt))
291
292 #define ODR_MASK_ZERO(mask)\
293     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
294     (mask)->top = -1))
295
296 #define ODR_MASK_SET(mask, num)\
297     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
298     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
299
300 #define ODR_MASK_CLEAR(mask, num)\
301     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
302
303 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
304     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
305
306 /* Private macro.
307  * write a single character at the current position - grow buffer if
308  * necessary.
309  * (no, we're not usually this anal about our macros, but this baby is
310  *  next to unreadable without some indentation  :)
311  */
312 #define odr_putc(o, c) \
313 ( \
314     ( \
315         (o)->ecb.pos < (o)->ecb.size ? \
316         ( \
317             (o)->ecb.buf[(o)->ecb.pos++] = (c), \
318             0 \
319         ) : \
320         ( \
321             odr_grow_block(&(o)->ecb, 1) == 0 ? \
322             ( \
323                 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
324                 0 \
325             ) : \
326             ( \
327                 (o)->error = OSPACE, \
328                 -1 \
329             ) \
330         ) \
331     ) == 0 ? \
332     ( \
333         (o)->ecb.pos > (o)->ecb.top ? \
334         ( \
335             (o)->ecb.top = (o)->ecb.pos, \
336             0 \
337         ) : \
338         0 \
339     ) : \
340         -1 \
341 ) \
342
343 #define odr_tell(o) ((o)->ecb.pos)
344 #define odr_ok(o) (!(o)->error)
345
346 #define ODR_MAXNAME 256
347
348 #include <prt.h>
349 #include <dmalloc.h>
350
351 #endif