Fixed bug in inetd code. The server listened on tcp:@:9999 even
[yaz-moved-to-github.git] / include / odr.h
1 /*
2  * Copyright (c) 1995-1997, 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.24  1997-11-24 11:33:56  adam
28  * Using function odr_nullval() instead of global ODR_NULLVAL when
29  * appropriate.
30  *
31  * Revision 1.23  1997/09/01 08:49:49  adam
32  * New windows NT/95 port using MSV5.0. To export DLL functions the
33  * YAZ_EXPORT modifier was added. Defined in yconfig.h.
34  *
35  * Revision 1.22  1997/07/21 12:44:30  adam
36  * Moved definitions of nmem_block and nmem_control.
37  *
38  * Revision 1.21  1997/05/14 06:53:41  adam
39  * C++ support.
40  *
41  * Revision 1.20  1997/04/30 08:52:08  quinn
42  * Null
43  *
44  * Revision 1.19  1996/10/08  12:58:08  adam
45  * New ODR function, odr_choice_enable_bias, to control behaviour of
46  * odr_choice_bias.
47  *
48  * Revision 1.18  1996/07/26  13:38:14  quinn
49  * Various smaller things. Gathered header-files.
50  *
51  * Revision 1.17  1995/11/08  17:41:27  quinn
52  * Smallish.
53  *
54  * Revision 1.16  1995/11/01  13:54:36  quinn
55  * Minor adjustments
56  *
57  * Revision 1.15  1995/10/18  16:12:36  quinn
58  * Better diagnostics.
59  *
60  * Revision 1.14  1995/09/29  17:12:04  quinn
61  * Smallish
62  *
63  * Revision 1.13  1995/09/29  17:01:49  quinn
64  * More Windows work
65  *
66  * Revision 1.12  1995/09/27  15:02:48  quinn
67  * Modified function heads & prototypes.
68  *
69  * Revision 1.11  1995/08/15  12:00:09  quinn
70  * Updated External
71  *
72  * Revision 1.10  1995/08/10  08:54:33  quinn
73  * Added Explain.
74  *
75  * Revision 1.9  1995/06/19  12:38:26  quinn
76  * Reorganized include-files. Added small features.
77  *
78  * Revision 1.8  1995/06/16  13:16:04  quinn
79  * Fixed Defaultdiagformat.
80  *
81  * Revision 1.7  1995/05/29  08:11:32  quinn
82  * Moved oid from odr/asn to util.
83  *
84  * Revision 1.6  1995/05/22  14:47:00  quinn
85  * ODR_NULLVAL --> void
86  *
87  * Revision 1.5  1995/05/22  11:31:24  quinn
88  * Added PDUs
89  *
90  * Revision 1.4  1995/05/16  08:50:33  quinn
91  * License, documentation, and memory fixes
92  *
93  * Revision 1.3  1995/05/15  11:55:54  quinn
94  * Work on asynchronous activity.
95  *
96  * Revision 1.2  1995/04/18  08:14:37  quinn
97  * Added dynamic memory allocation on encoding
98  *
99  * Revision 1.1  1995/03/30  09:39:41  quinn
100  * Moved .h files to include directory
101  *
102  * Revision 1.15  1995/03/29  15:39:57  quinn
103  * Fixed bugs in the bitmask operations
104  *
105  * Revision 1.14  1995/03/27  08:33:15  quinn
106  * Added more OID utilities.
107  *
108  * Revision 1.13  1995/03/17  10:17:44  quinn
109  * Added memory management.
110  *
111  * Revision 1.12  1995/03/14  10:27:38  quinn
112  * Modified makefile to use common lib
113  * Beginning to add memory management to odr
114  *
115  * Revision 1.11  1995/03/10  11:44:41  quinn
116  * Fixed serious stack-bug in odr_cons_begin
117  *
118  * Revision 1.10  1995/03/08  12:12:16  quinn
119  * Added better error checking.
120  *
121  * Revision 1.9  1995/03/07  10:10:00  quinn
122  * Added some headers for Adam.
123  *
124  * Revision 1.8  1995/03/07  09:23:16  quinn
125  * Installing top-level API and documentation.
126  *
127  * Revision 1.7  1995/02/10  15:55:29  quinn
128  * Bug fixes, mostly.
129  *
130  * Revision 1.6  1995/02/09  15:51:47  quinn
131  * Works better now.
132  *
133  * Revision 1.5  1995/02/07  17:52:59  quinn
134  * A damn mess, but now things work, I think.
135  *
136  * Revision 1.4  1995/02/06  16:45:03  quinn
137  * Small mods.
138  *
139  * Revision 1.3  1995/02/03  17:04:36  quinn
140  * *** empty log message ***
141  *
142  * Revision 1.2  1995/02/02  20:38:50  quinn
143  * Updates.
144  *
145  * Revision 1.1  1995/02/02  16:21:53  quinn
146  * First kick.
147  *
148  */
149
150 #ifndef ODR_H
151 #define ODR_H
152
153 #include <stdio.h>
154 #include <string.h>
155
156 #include <nmem.h>
157 #include <yconfig.h>
158
159 #ifdef __cplusplus
160 extern "C" {
161 #endif
162
163 #ifndef bool_t
164 #define bool_t int
165 #endif
166
167 /*
168  * Tag modes
169  */
170 #define ODR_NONE -1
171 #define ODR_IMPLICIT 0
172 #define ODR_EXPLICIT 1
173
174 /*
175  * Classes
176  */
177 #define ODR_UNIVERSAL   0
178 #define ODR_APPLICATION 1
179 #define ODR_CONTEXT     2
180 #define ODR_PRIVATE     3
181
182 /*
183  * UNIVERSAL tags
184  */
185 #define ODR_BOOLEAN     1
186 #define ODR_INTEGER     2
187 #define ODR_BITSTRING   3
188 #define ODR_OCTETSTRING 4
189 #define ODR_NULL        5
190 #define ODR_OID         6
191 #define ODR_ODESC       7
192 #define ODR_EXTERNAL    8
193 #define ODR_REAL        9
194 #define ODR_ENUM        10
195 #define ODR_SEQUENCE    16
196 #define ODR_SET         17
197 #define ODR_NUMERICSTRING   18
198 #define ODR_PRINTABLESTRING 19
199 #define ODR_GENERALIZEDTIME 24
200 #define ODR_GRAPHICSTRING   25
201 #define ODR_VISIBLESTRING   26
202 #define ODR_GENERALSTRING   27
203
204 /*
205  * odr stream directions
206  */
207 #define ODR_DECODE      0
208 #define ODR_ENCODE      1
209 #define ODR_PRINT       2
210
211 typedef struct odr_oct
212 {
213     unsigned char *buf;
214     int len;
215     int size;
216 } Odr_oct;
217
218 typedef void Odr_null;
219 extern Odr_null *ODR_NULLVAL;
220
221 typedef Odr_oct Odr_any;
222
223 typedef struct odr_bitmask
224 {
225 #define ODR_BITMASK_SIZE 256
226     unsigned char bits[ODR_BITMASK_SIZE];
227     int top;
228 } Odr_bitmask;
229
230 typedef int Odr_oid;   /* terminate by -1 */
231
232 typedef struct odr_constack
233 {
234     unsigned char *base;         /* starting point of data */
235     int base_offset;
236     int len;                     /* length of data, if known, else -1
237                                         (decoding only) */
238     unsigned char *lenb;         /* where to encode length */
239     int len_offset;
240     int lenlen;                  /* length of length-field */
241 } odr_constack;
242
243 #define ODR_S_SET     0
244 #define ODR_S_CUR     1
245 #define ODR_S_END     2
246
247 typedef struct odr_ecblock
248 {
249     int can_grow;         /* are we allowed to reallocate */
250     unsigned char *buf;            /* memory handle */
251     int pos;              /* current position */
252     int top;              /* top of buffer */
253     int size;             /* current buffer size */
254 } odr_ecblock;
255
256 typedef struct {      /* used to be statics in ber_tag... */
257     int lclass;
258     int ltag;
259     int br;
260     int lcons;
261 } Odr_ber_tag;
262
263 typedef struct odr
264 {
265     int direction;       /* the direction of this stream */
266
267     int error;           /* current error state (0==OK) */
268     unsigned char *buf;  /* for encoding or decoding */
269     int buflen;          /* size of buffer for encoding, len for decoding */
270     unsigned char *bp;   /* position in buffer */
271     int left;            /* bytes remaining in buffer */
272
273     odr_ecblock ecb;     /* memory control block */
274
275     int t_class;         /* implicit tagging (-1==default tag) */
276     int t_tag;
277
278     int enable_bias;     /* force choice enable flag */
279     int choice_bias;     /* force choice */
280     int lenlen;          /* force length-of-lenght (odr_setlen()) */
281
282     FILE *print;         /* output file for direction print */
283     int indent;          /* current indent level for printing */
284
285     NMEM mem;            /* memory handle for decoding (primarily) */
286
287     /* stack for constructed types */
288 #define ODR_MAX_STACK 50
289     int stackp;          /* top of stack (-1 == initial state) */
290     odr_constack stack[ODR_MAX_STACK];
291
292     Odr_ber_tag odr_ber_tag;
293 } *ODR;
294
295 typedef int (*Odr_fun)();
296
297 typedef struct odr_arm
298 {
299     int tagmode;
300     int zclass;
301     int tag;
302     int which;
303     Odr_fun fun;
304 } Odr_arm;
305
306 /*
307  * Error control.
308  */
309 #define ONONE           0
310 #define OMEMORY         1
311 #define OSYSERR         2
312 #define OSPACE          3
313 #define OREQUIRED       4
314 #define OUNEXPECTED     5
315 #define OOTHER          6
316 #define OPROTO          7
317 #define ODATA           8
318 #define OSTACK          9
319 #define OCONLEN        10
320 #define OLENOV         11
321
322 extern char *odr_errlist[];
323
324 YAZ_EXPORT int odr_geterror(ODR o);
325 YAZ_EXPORT void odr_perror(ODR o, char *message);
326 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
327 YAZ_EXPORT ODR odr_createmem(int direction);
328 YAZ_EXPORT void odr_reset(ODR o);
329 YAZ_EXPORT void odr_destroy(ODR o);
330 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
331 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
332 YAZ_EXPORT void *odr_malloc(ODR o, int size);
333 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
334 YAZ_EXPORT Odr_null *odr_nullval(void);
335 #define odr_release_mem(m) nmem_destroy(m)
336 #define ODR_MEM NMEM
337
338 #define odr_implicit(o, t, p, cl, tg, opt)\
339         (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
340
341 #define odr_explicit(o, t, p, cl, tg, opt)\
342         ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
343         t ((o), (p), (opt)) &&\
344         odr_constructed_end(o) : opt))
345
346 #define ODR_MASK_ZERO(mask)\
347     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
348     (mask)->top = -1))
349
350 #define ODR_MASK_SET(mask, num)\
351     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
352     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
353
354 #define ODR_MASK_CLEAR(mask, num)\
355     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
356
357 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
358     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
359
360 /* Private macro.
361  * write a single character at the current position - grow buffer if
362  * necessary.
363  * (no, we're not usually this anal about our macros, but this baby is
364  *  next to unreadable without some indentation  :)
365  */
366 #define odr_putc(o, c) \
367 ( \
368     ( \
369         (o)->ecb.pos < (o)->ecb.size ? \
370         ( \
371             (o)->ecb.buf[(o)->ecb.pos++] = (c), \
372             0 \
373         ) : \
374         ( \
375             odr_grow_block(&(o)->ecb, 1) == 0 ? \
376             ( \
377                 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
378                 0 \
379             ) : \
380             ( \
381                 (o)->error = OSPACE, \
382                 -1 \
383             ) \
384         ) \
385     ) == 0 ? \
386     ( \
387         (o)->ecb.pos > (o)->ecb.top ? \
388         ( \
389             (o)->ecb.top = (o)->ecb.pos, \
390             0 \
391         ) : \
392         0 \
393     ) : \
394         -1 \
395 ) \
396
397 #define odr_tell(o) ((o)->ecb.pos)
398 #define odr_offset(o) ((o)->bp - (o)->buf)
399 #define odr_ok(o) (!(o)->error)
400 #define odr_getmem(o) ((o)->mem)
401 #define odr_setmem(o, v) ((o)->mem = (v))
402
403 #define ODR_MAXNAME 256
404
405 #ifdef __cplusplus
406 }
407 #endif
408
409 #include <prt.h>
410 #include <xmalloc.h>
411
412 #endif