Fixed possible buf in proto.c
[yaz-moved-to-github.git] / include / odr.h
1
2 /*
3  * Copyright (C) 1994, Index Data I/S 
4  * All rights reserved.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: odr.h,v $
8  * Revision 1.1  1995-03-30 09:39:41  quinn
9  * Moved .h files to include directory
10  *
11  * Revision 1.15  1995/03/29  15:39:57  quinn
12  * Fixed bugs in the bitmask operations
13  *
14  * Revision 1.14  1995/03/27  08:33:15  quinn
15  * Added more OID utilities.
16  *
17  * Revision 1.13  1995/03/17  10:17:44  quinn
18  * Added memory management.
19  *
20  * Revision 1.12  1995/03/14  10:27:38  quinn
21  * Modified makefile to use common lib
22  * Beginning to add memory management to odr
23  *
24  * Revision 1.11  1995/03/10  11:44:41  quinn
25  * Fixed serious stack-bug in odr_cons_begin
26  *
27  * Revision 1.10  1995/03/08  12:12:16  quinn
28  * Added better error checking.
29  *
30  * Revision 1.9  1995/03/07  10:10:00  quinn
31  * Added some headers for Adam.
32  *
33  * Revision 1.8  1995/03/07  09:23:16  quinn
34  * Installing top-level API and documentation.
35  *
36  * Revision 1.7  1995/02/10  15:55:29  quinn
37  * Bug fixes, mostly.
38  *
39  * Revision 1.6  1995/02/09  15:51:47  quinn
40  * Works better now.
41  *
42  * Revision 1.5  1995/02/07  17:52:59  quinn
43  * A damn mess, but now things work, I think.
44  *
45  * Revision 1.4  1995/02/06  16:45:03  quinn
46  * Small mods.
47  *
48  * Revision 1.3  1995/02/03  17:04:36  quinn
49  * *** empty log message ***
50  *
51  * Revision 1.2  1995/02/02  20:38:50  quinn
52  * Updates.
53  *
54  * Revision 1.1  1995/02/02  16:21:53  quinn
55  * First kick.
56  *
57  */
58
59 #ifndef ODR_H
60 #define ODR_H
61
62 #include <stdio.h>
63 #include <string.h>
64
65 #ifndef bool_t
66 #define bool_t int
67 #endif
68
69 /*
70  * Tag modes
71  */
72 #define ODR_NONE -1
73 #define ODR_IMPLICIT 0
74 #define ODR_EXPLICIT 1
75
76 /*
77  * Classes
78  */
79 #define ODR_UNIVERSAL   0
80 #define ODR_APPLICATION 1
81 #define ODR_CONTEXT     2
82 #define ODR_PRIVATE     3
83
84 /*
85  * UNIVERSAL tags
86  */
87 #define ODR_BOOLEAN     1
88 #define ODR_INTEGER     2
89 #define ODR_BITSTRING   3
90 #define ODR_OCTETSTRING 4
91 #define ODR_NULL        5
92 #define ODR_OID         6
93 #define ODR_ODESC       7
94 #define ODR_EXTERNAL    8
95 #define ODR_REAL        9
96 #define ODR_ENUM        10
97 #define ODR_SEQUENCE    16
98 #define ODR_SET         17
99 #define ODR_NUMERICSTRING   18
100 #define ODR_PRINTABLESTRING 19
101 #define ODR_GRAPHICSTRING   25
102 #define ODR_VISIBLESTRING   26
103
104 /*
105  * odr stream directions
106  */
107 #define ODR_DECODE      0
108 #define ODR_ENCODE      1
109 #define ODR_PRINT       2
110
111 typedef struct odr_oct
112 {
113     unsigned char *buf;
114     int len;
115     int size;
116 } Odr_oct;
117
118 typedef Odr_oct Odr_any;
119
120 typedef struct odr_bitmask
121 {
122 #define ODR_BITMASK_SIZE 256
123     unsigned char bits[ODR_BITMASK_SIZE];
124     int top;
125 } Odr_bitmask;
126
127 #define ODR_OID_SIZE 100
128 typedef int Odr_oid;   /* terminate by -1 */
129
130 typedef struct odr_constack
131 {
132     unsigned char *base;         /* starting point of data */
133     int len;                     /* length of data, if known, else -1
134                                         (decoding only) */
135     unsigned char *lenb;         /* where to encode length */
136     int lenlen;                  /* length of length-field */
137 } odr_constack;
138
139 struct odr_memblock; /* defined in odr_mem.c */
140
141 typedef struct odr
142 {
143     int direction;       /* the direction of this stream */
144
145     int error;           /* current error state (0==OK) */
146     unsigned char *buf;  /* for encoding or decoding */
147     int buflen;          /* size of buffer for encoding, len for decoding */
148     unsigned char *bp;   /* position in buffer */
149     int left;            /* bytes remaining in buffer */
150
151     int t_class;         /* implicit tagging (-1==default tag) */
152     int t_tag;
153
154     FILE *print;         /* output file for direction print */
155     int indent;          /* current indent level for printing */
156
157     struct odr_memblock *mem;
158
159     /* stack for constructed types */
160 #define ODR_MAX_STACK 50
161     int stackp;          /* top of stack (-1 == initial state) */
162     odr_constack stack[ODR_MAX_STACK];
163 } *ODR;
164
165 typedef int (*Odr_fun)();
166
167 typedef struct odr_arm
168 {
169     int tagmode;
170     int class;
171     int tag;
172     int which;
173     Odr_fun fun;
174 } Odr_arm;
175
176 /*
177  * Error control.
178  */
179 #define ONONE           0
180 #define OMEMORY         1
181 #define OSYSERR         2
182 #define OSPACE          3
183 #define OREQUIRED       4
184 #define OUNEXPECTED     5
185 #define OOTHER          6
186 #define OPROTO          7
187 #define ODATA           8
188 #define OSTACK          9
189
190 extern char *odr_errlist[];
191
192 int odr_geterror(ODR o);
193 void odr_perror(ODR o, char *message);
194 void odr_setprint(ODR o, FILE *file);
195 ODR odr_createmem(int direction);
196 void odr_reset(ODR o);
197 void odr_destroy(ODR o);
198 void odr_setbuf(ODR o, char *buf, int len);
199 char *odr_getbuf(ODR o, int *len);
200 void *odr_malloc(ODR o, int size);
201
202 #define odr_implicit(o, t, p, cl, tg, opt)\
203         (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
204
205 #define odr_explicit(o, t, p, cl, tg, opt)\
206         ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
207         t ((o), (p), (opt)) &&\
208         odr_constructed_end(o) : opt))
209
210 #define ODR_MASK_ZERO(mask)\
211     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
212     (mask)->top = -1))
213
214 #define ODR_MASK_SET(mask, num)\
215     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
216     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
217
218 #define ODR_MASK_CLEAR(mask, num)\
219     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
220
221 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
222     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
223
224 #include <prt.h>
225 #include <dmalloc.h>
226
227 #endif