2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-09-29 17:12:21 quinn
10 * Revision 1.12 1995/09/27 15:02:57 quinn
11 * Modified function heads & prototypes.
13 * Revision 1.11 1995/05/16 08:50:48 quinn
14 * License, documentation, and memory fixes
16 * Revision 1.10 1995/04/18 08:15:18 quinn
17 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
18 * neater. We'll make the same change for decoding one day.
20 * Revision 1.9 1995/03/15 08:37:18 quinn
21 * Fixed protocol bugs.
23 * Revision 1.8 1995/03/10 11:44:40 quinn
24 * Fixed serious stack-bug in odr_cons_begin
26 * Revision 1.7 1995/03/08 12:12:13 quinn
27 * Added better error checking.
29 * Revision 1.6 1995/02/14 11:54:33 quinn
32 * Revision 1.5 1995/02/10 18:57:24 quinn
33 * More in the way of error-checking.
35 * Revision 1.4 1995/02/10 15:55:28 quinn
38 * Revision 1.3 1995/02/09 15:51:46 quinn
41 * Revision 1.2 1995/02/07 17:52:59 quinn
42 * A damn mess, but now things work, I think.
44 * Revision 1.1 1995/02/02 16:21:53 quinn
54 * if p: write tag. return 1 (success) or -1 (error).
57 * if tag && class match up, advance pointer and return 1. set cons.
58 * else leave pointer unchanged. Return 0.
60 * Should perhaps be odr_tag?
62 int ber_tag(ODR o, void *p, int class, int tag, int *constructed, int opt)
64 static int lclass = -1, ltag, br, lcons; /* save t&c rather than
69 if (o->direction == ODR_DECODE)
74 odr_seek(o, ODR_S_SET, 0);
88 if ((rd = ber_enctag(o, class, tag, *constructed)) < 0)
91 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", class, tag,
92 *constructed, o->stackp);
96 if (o->stackp > -1 && !odr_constructed_more(o))
104 if ((br = ber_dectag(o->bp, &lclass, <ag, &lcons)) <= 0)
110 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", lclass, ltag,
114 if (class == lclass && tag == ltag)
118 *constructed = lcons;
125 o->error = OREQUIRED;
130 o->error = OREQUIRED;
132 default: o->error = OOTHER; return 0;
137 * BER-encode a class/tag/constructed package (identifier octets). Return
138 * number of bytes encoded, or -1 if out of bounds.
140 int ber_enctag(ODR o, int class, int tag, int constructed)
142 int cons = (constructed ? 1 : 0), n = 0;
143 unsigned char octs[sizeof(int)], b;
145 b = (class << 6) & 0XC0;
146 b |= (cons << 5) & 0X20;
150 if (odr_putc(o, b) < 0)
157 if (odr_putc(o, b) < 0)
161 octs[n++] = tag & 0X7F;
169 oo = octs[n] | ((n > 0) << 7);
170 if (odr_putc(o, oo) < 0)
178 * Decode BER identifier octets. Return number of bytes read or -1 for error.
180 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
182 unsigned char *b = buf;
185 *constructed = (*b >> 5) & 0X01;
186 if ((*tag = *b & 0x1F) <= 30)
194 if (b - buf >= 5) /* Precaution */
197 while (*(b++) & 0X80);