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.20 1997-04-30 08:52:08 quinn
30 * Revision 1.19 1996/10/08 12:58:08 adam
31 * New ODR function, odr_choice_enable_bias, to control behaviour of
34 * Revision 1.18 1996/07/26 13:38:14 quinn
35 * Various smaller things. Gathered header-files.
37 * Revision 1.17 1995/11/08 17:41:27 quinn
40 * Revision 1.16 1995/11/01 13:54:36 quinn
43 * Revision 1.15 1995/10/18 16:12:36 quinn
46 * Revision 1.14 1995/09/29 17:12:04 quinn
49 * Revision 1.13 1995/09/29 17:01:49 quinn
52 * Revision 1.12 1995/09/27 15:02:48 quinn
53 * Modified function heads & prototypes.
55 * Revision 1.11 1995/08/15 12:00:09 quinn
58 * Revision 1.10 1995/08/10 08:54:33 quinn
61 * Revision 1.9 1995/06/19 12:38:26 quinn
62 * Reorganized include-files. Added small features.
64 * Revision 1.8 1995/06/16 13:16:04 quinn
65 * Fixed Defaultdiagformat.
67 * Revision 1.7 1995/05/29 08:11:32 quinn
68 * Moved oid from odr/asn to util.
70 * Revision 1.6 1995/05/22 14:47:00 quinn
71 * ODR_NULLVAL --> void
73 * Revision 1.5 1995/05/22 11:31:24 quinn
76 * Revision 1.4 1995/05/16 08:50:33 quinn
77 * License, documentation, and memory fixes
79 * Revision 1.3 1995/05/15 11:55:54 quinn
80 * Work on asynchronous activity.
82 * Revision 1.2 1995/04/18 08:14:37 quinn
83 * Added dynamic memory allocation on encoding
85 * Revision 1.1 1995/03/30 09:39:41 quinn
86 * Moved .h files to include directory
88 * Revision 1.15 1995/03/29 15:39:57 quinn
89 * Fixed bugs in the bitmask operations
91 * Revision 1.14 1995/03/27 08:33:15 quinn
92 * Added more OID utilities.
94 * Revision 1.13 1995/03/17 10:17:44 quinn
95 * Added memory management.
97 * Revision 1.12 1995/03/14 10:27:38 quinn
98 * Modified makefile to use common lib
99 * Beginning to add memory management to odr
101 * Revision 1.11 1995/03/10 11:44:41 quinn
102 * Fixed serious stack-bug in odr_cons_begin
104 * Revision 1.10 1995/03/08 12:12:16 quinn
105 * Added better error checking.
107 * Revision 1.9 1995/03/07 10:10:00 quinn
108 * Added some headers for Adam.
110 * Revision 1.8 1995/03/07 09:23:16 quinn
111 * Installing top-level API and documentation.
113 * Revision 1.7 1995/02/10 15:55:29 quinn
116 * Revision 1.6 1995/02/09 15:51:47 quinn
119 * Revision 1.5 1995/02/07 17:52:59 quinn
120 * A damn mess, but now things work, I think.
122 * Revision 1.4 1995/02/06 16:45:03 quinn
125 * Revision 1.3 1995/02/03 17:04:36 quinn
126 * *** empty log message ***
128 * Revision 1.2 1995/02/02 20:38:50 quinn
131 * Revision 1.1 1995/02/02 16:21:53 quinn
153 #define ODR_IMPLICIT 0
154 #define ODR_EXPLICIT 1
159 #define ODR_UNIVERSAL 0
160 #define ODR_APPLICATION 1
161 #define ODR_CONTEXT 2
162 #define ODR_PRIVATE 3
167 #define ODR_BOOLEAN 1
168 #define ODR_INTEGER 2
169 #define ODR_BITSTRING 3
170 #define ODR_OCTETSTRING 4
174 #define ODR_EXTERNAL 8
177 #define ODR_SEQUENCE 16
179 #define ODR_NUMERICSTRING 18
180 #define ODR_PRINTABLESTRING 19
181 #define ODR_GENERALIZEDTIME 24
182 #define ODR_GRAPHICSTRING 25
183 #define ODR_VISIBLESTRING 26
184 #define ODR_GENERALSTRING 27
187 * odr stream directions
193 typedef struct odr_oct
200 typedef void Odr_null;
201 extern Odr_null *ODR_NULLVAL;
203 typedef Odr_oct Odr_any;
205 typedef struct odr_bitmask
207 #define ODR_BITMASK_SIZE 256
208 unsigned char bits[ODR_BITMASK_SIZE];
212 typedef int Odr_oid; /* terminate by -1 */
214 typedef struct odr_constack
216 unsigned char *base; /* starting point of data */
218 int len; /* length of data, if known, else -1
220 unsigned char *lenb; /* where to encode length */
222 int lenlen; /* length of length-field */
225 struct odr_memblock; /* defined in odr_mem.c */
226 typedef struct odr_memblock *ODR_MEM;
232 typedef struct odr_ecblock
234 int can_grow; /* are we allowed to reallocate */
235 unsigned char *buf; /* memory handle */
236 int pos; /* current position */
237 int top; /* top of buffer */
238 int size; /* current buffer size */
244 int direction; /* the direction of this stream */
246 int error; /* current error state (0==OK) */
247 unsigned char *buf; /* for encoding or decoding */
248 int buflen; /* size of buffer for encoding, len for decoding */
249 unsigned char *bp; /* position in buffer */
250 int left; /* bytes remaining in buffer */
252 odr_ecblock ecb; /* memory control block */
254 int t_class; /* implicit tagging (-1==default tag) */
257 int enable_bias; /* force choice enable flag */
258 int choice_bias; /* force choice */
259 int lenlen; /* force length-of-lenght (odr_setlen()) */
261 FILE *print; /* output file for direction print */
262 int indent; /* current indent level for printing */
264 NMEM mem; /* memory handle for decoding (primarily) */
266 /* stack for constructed types */
267 #define ODR_MAX_STACK 50
268 int stackp; /* top of stack (-1 == initial state) */
269 odr_constack stack[ODR_MAX_STACK];
272 typedef int (*Odr_fun)();
274 typedef struct odr_arm
291 #define OUNEXPECTED 5
299 extern char *odr_errlist[];
301 int odr_geterror(ODR o);
302 void odr_perror(ODR o, char *message);
303 void odr_setprint(ODR o, FILE *file);
304 ODR odr_createmem(int direction);
305 void odr_reset(ODR o);
306 void odr_destroy(ODR o);
307 void odr_setbuf(ODR o, char *buf, int len, int can_grow);
308 char *odr_getbuf(ODR o, int *len, int *size);
309 void *odr_malloc(ODR o, int size);
310 NMEM odr_extract_mem(ODR o);
311 #define odr_release_mem(m) nmem_destroy(m)
314 #define odr_implicit(o, t, p, cl, tg, opt)\
315 (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
317 #define odr_explicit(o, t, p, cl, tg, opt)\
318 ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
319 t ((o), (p), (opt)) &&\
320 odr_constructed_end(o) : opt))
322 #define ODR_MASK_ZERO(mask)\
323 ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
326 #define ODR_MASK_SET(mask, num)\
327 (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
328 (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
330 #define ODR_MASK_CLEAR(mask, num)\
331 ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
333 #define ODR_MASK_GET(mask, num) ( ((num) >> 3 <= (mask)->top) ? \
334 ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
337 * write a single character at the current position - grow buffer if
339 * (no, we're not usually this anal about our macros, but this baby is
340 * next to unreadable without some indentation :)
342 #define odr_putc(o, c) \
345 (o)->ecb.pos < (o)->ecb.size ? \
347 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
351 odr_grow_block(&(o)->ecb, 1) == 0 ? \
353 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
357 (o)->error = OSPACE, \
363 (o)->ecb.pos > (o)->ecb.top ? \
365 (o)->ecb.top = (o)->ecb.pos, \
373 #define odr_tell(o) ((o)->ecb.pos)
374 #define odr_offset(o) ((o)->bp - (o)->buf)
375 #define odr_ok(o) (!(o)->error)
376 #define odr_getmem(o) ((o)->mem)
377 #define odr_setmem(o, v) ((o)->mem = (v))
379 #define ODR_MAXNAME 256