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