2 * Copyright (c) 1995-1997, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1997-09-17 12:10:33 adam
10 * Revision 1.15 1997/09/01 08:51:06 adam
11 * New windows NT/95 port using MSV5.0. Had to avoid a few static
12 * variables used in function ber_tag. These are now part of the
15 * Revision 1.14 1997/05/14 06:53:56 adam
18 * Revision 1.13 1995/09/29 17:12:21 quinn
21 * Revision 1.12 1995/09/27 15:02:57 quinn
22 * Modified function heads & prototypes.
24 * Revision 1.11 1995/05/16 08:50:48 quinn
25 * License, documentation, and memory fixes
27 * Revision 1.10 1995/04/18 08:15:18 quinn
28 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
29 * neater. We'll make the same change for decoding one day.
31 * Revision 1.9 1995/03/15 08:37:18 quinn
32 * Fixed protocol bugs.
34 * Revision 1.8 1995/03/10 11:44:40 quinn
35 * Fixed serious stack-bug in odr_cons_begin
37 * Revision 1.7 1995/03/08 12:12:13 quinn
38 * Added better error checking.
40 * Revision 1.6 1995/02/14 11:54:33 quinn
43 * Revision 1.5 1995/02/10 18:57:24 quinn
44 * More in the way of error-checking.
46 * Revision 1.4 1995/02/10 15:55:28 quinn
49 * Revision 1.3 1995/02/09 15:51:46 quinn
52 * Revision 1.2 1995/02/07 17:52:59 quinn
53 * A damn mess, but now things work, I think.
55 * Revision 1.1 1995/02/02 16:21:53 quinn
65 * if p: write tag. return 1 (success) or -1 (error).
68 * if tag && zclass match up, advance pointer and return 1. set cons.
69 * else leave pointer unchanged. Return 0.
71 * Should perhaps be odr_tag?
73 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt)
75 Odr_ber_tag *odr_ber_tag = &o->odr_ber_tag;
79 if (o->direction == ODR_DECODE)
84 odr_seek(o, ODR_S_SET, 0);
87 odr_ber_tag->lclass = -1;
98 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
101 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
102 *constructed, o->stackp);
107 if (o->stackp > -1 && !odr_constructed_more(o))
110 o->error = OREQUIRED;
113 if (odr_ber_tag->lclass < 0)
115 if ((odr_ber_tag->br = ber_dectag(o->bp, &odr_ber_tag->lclass,
116 &odr_ber_tag->ltag, &odr_ber_tag->lcons)) <= 0)
122 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", odr_ber_tag->lclass, odr_ber_tag->ltag,
123 odr_ber_tag->lcons, o->stackp);
126 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
128 o->bp += odr_ber_tag->br;
129 o->left -= odr_ber_tag->br;
130 *constructed = odr_ber_tag->lcons;
131 odr_ber_tag->lclass = -1;
137 o->error = OREQUIRED;
142 o->error = OREQUIRED;
151 * BER-encode a zclass/tag/constructed package (identifier octets). Return
152 * number of bytes encoded, or -1 if out of bounds.
154 int ber_enctag(ODR o, int zclass, int tag, int constructed)
156 int cons = (constructed ? 1 : 0), n = 0;
157 unsigned char octs[sizeof(int)], b;
159 b = (zclass << 6) & 0XC0;
160 b |= (cons << 5) & 0X20;
164 if (odr_putc(o, b) < 0)
171 if (odr_putc(o, b) < 0)
175 octs[n++] = tag & 0X7F;
183 oo = octs[n] | ((n > 0) << 7);
184 if (odr_putc(o, oo) < 0)
192 * Decode BER identifier octets. Return number of bytes read or -1 for error.
194 int ber_dectag(unsigned char *buf, int *zclass, int *tag, int *constructed)
196 unsigned char *b = buf;
199 *constructed = (*b >> 5) & 0X01;
200 if ((*tag = *b & 0x1F) <= 30)
208 if (b - buf >= 5) /* Precaution */
211 while (*(b++) & 0X80);