2 * Copyright (c) 1995, Index Data.
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,
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.
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.
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
27 * Revision 1.9 1995-06-19 12:38:26 quinn
28 * Reorganized include-files. Added small features.
30 * Revision 1.8 1995/06/16 13:16:04 quinn
31 * Fixed Defaultdiagformat.
33 * Revision 1.7 1995/05/29 08:11:32 quinn
34 * Moved oid from odr/asn to util.
36 * Revision 1.6 1995/05/22 14:47:00 quinn
37 * ODR_NULLVAL --> void
39 * Revision 1.5 1995/05/22 11:31:24 quinn
42 * Revision 1.4 1995/05/16 08:50:33 quinn
43 * License, documentation, and memory fixes
45 * Revision 1.3 1995/05/15 11:55:54 quinn
46 * Work on asynchronous activity.
48 * Revision 1.2 1995/04/18 08:14:37 quinn
49 * Added dynamic memory allocation on encoding
51 * Revision 1.1 1995/03/30 09:39:41 quinn
52 * Moved .h files to include directory
54 * Revision 1.15 1995/03/29 15:39:57 quinn
55 * Fixed bugs in the bitmask operations
57 * Revision 1.14 1995/03/27 08:33:15 quinn
58 * Added more OID utilities.
60 * Revision 1.13 1995/03/17 10:17:44 quinn
61 * Added memory management.
63 * Revision 1.12 1995/03/14 10:27:38 quinn
64 * Modified makefile to use common lib
65 * Beginning to add memory management to odr
67 * Revision 1.11 1995/03/10 11:44:41 quinn
68 * Fixed serious stack-bug in odr_cons_begin
70 * Revision 1.10 1995/03/08 12:12:16 quinn
71 * Added better error checking.
73 * Revision 1.9 1995/03/07 10:10:00 quinn
74 * Added some headers for Adam.
76 * Revision 1.8 1995/03/07 09:23:16 quinn
77 * Installing top-level API and documentation.
79 * Revision 1.7 1995/02/10 15:55:29 quinn
82 * Revision 1.6 1995/02/09 15:51:47 quinn
85 * Revision 1.5 1995/02/07 17:52:59 quinn
86 * A damn mess, but now things work, I think.
88 * Revision 1.4 1995/02/06 16:45:03 quinn
91 * Revision 1.3 1995/02/03 17:04:36 quinn
92 * *** empty log message ***
94 * Revision 1.2 1995/02/02 20:38:50 quinn
97 * Revision 1.1 1995/02/02 16:21:53 quinn
116 #define ODR_IMPLICIT 0
117 #define ODR_EXPLICIT 1
122 #define ODR_UNIVERSAL 0
123 #define ODR_APPLICATION 1
124 #define ODR_CONTEXT 2
125 #define ODR_PRIVATE 3
130 #define ODR_BOOLEAN 1
131 #define ODR_INTEGER 2
132 #define ODR_BITSTRING 3
133 #define ODR_OCTETSTRING 4
137 #define ODR_EXTERNAL 8
140 #define ODR_SEQUENCE 16
142 #define ODR_NUMERICSTRING 18
143 #define ODR_PRINTABLESTRING 19
144 #define ODR_GRAPHICSTRING 25
145 #define ODR_VISIBLESTRING 26
146 #define ODR_GENERALSTRING 27
149 * odr stream directions
155 typedef struct odr_oct
162 typedef void Odr_null;
163 extern Odr_null *ODR_NULLVAL;
165 typedef Odr_oct Odr_any;
167 typedef struct odr_bitmask
169 #define ODR_BITMASK_SIZE 256
170 unsigned char bits[ODR_BITMASK_SIZE];
174 typedef int Odr_oid; /* terminate by -1 */
176 typedef struct odr_constack
178 unsigned char *base; /* starting point of data */
180 int len; /* length of data, if known, else -1
182 unsigned char *lenb; /* where to encode length */
184 int lenlen; /* length of length-field */
187 struct odr_memblock; /* defined in odr_mem.c */
188 typedef struct odr_memblock *ODR_MEM;
194 typedef struct odr_ecblock
196 int can_grow; /* are we allowed to reallocate */
197 unsigned char *buf; /* memory handle */
198 int pos; /* current position */
199 int top; /* top of buffer */
200 int size; /* current buffer size */
206 int direction; /* the direction of this stream */
208 int error; /* current error state (0==OK) */
209 unsigned char *buf; /* for encoding or decoding */
210 int buflen; /* size of buffer for encoding, len for decoding */
211 unsigned char *bp; /* position in buffer */
212 int left; /* bytes remaining in buffer */
214 odr_ecblock ecb; /* memory control block */
216 int t_class; /* implicit tagging (-1==default tag) */
219 FILE *print; /* output file for direction print */
220 int indent; /* current indent level for printing */
222 struct odr_memblock *mem;
224 /* stack for constructed types */
225 #define ODR_MAX_STACK 50
226 int stackp; /* top of stack (-1 == initial state) */
227 odr_constack stack[ODR_MAX_STACK];
230 typedef int (*Odr_fun)();
232 typedef struct odr_arm
249 #define OUNEXPECTED 5
256 extern char *odr_errlist[];
258 int odr_geterror(ODR o);
259 void odr_perror(ODR o, char *message);
260 void odr_setprint(ODR o, FILE *file);
261 ODR odr_createmem(int direction);
262 void odr_reset(ODR o);
263 void odr_destroy(ODR o);
264 void odr_setbuf(ODR o, char *buf, int len, int can_grow);
265 char *odr_getbuf(ODR o, int *len, int *size);
266 void *odr_malloc(ODR o, int size);
267 ODR_MEM odr_extract_mem(ODR o);
268 void odr_release_mem(ODR_MEM p);
270 #define odr_implicit(o, t, p, cl, tg, opt)\
271 (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
273 #define odr_explicit(o, t, p, cl, tg, opt)\
274 ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
275 t ((o), (p), (opt)) &&\
276 odr_constructed_end(o) : opt))
278 #define ODR_MASK_ZERO(mask)\
279 ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
282 #define ODR_MASK_SET(mask, num)\
283 (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
284 (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
286 #define ODR_MASK_CLEAR(mask, num)\
287 ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
289 #define ODR_MASK_GET(mask, num) ( ((num) >> 3 <= (mask)->top) ? \
290 ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
293 * write a single character at the current position - grow buffer if
295 * (no, we're not usually this anal about our macros, but this baby is
296 * next to unreadable without some indentation :)
298 #define odr_putc(o, c) \
301 (o)->ecb.pos < (o)->ecb.size ? \
303 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
307 odr_grow_block(&(o)->ecb, 1) == 0 ? \
309 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
313 (o)->error = OSPACE, \
319 (o)->ecb.pos > (o)->ecb.top ? \
321 (o)->ecb.top = (o)->ecb.pos, \
329 #define odr_tell(o) ((o)->ecb.pos)
330 #define odr_ok(o) (!(o)->error)
332 #define ODR_MAXNAME 256