2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-03-10 11:44:40 quinn
8 * Fixed serious stack-bug in odr_cons_begin
10 * Revision 1.7 1995/03/08 12:12:13 quinn
11 * Added better error checking.
13 * Revision 1.6 1995/02/14 11:54:33 quinn
16 * Revision 1.5 1995/02/10 18:57:24 quinn
17 * More in the way of error-checking.
19 * Revision 1.4 1995/02/10 15:55:28 quinn
22 * Revision 1.3 1995/02/09 15:51:46 quinn
25 * Revision 1.2 1995/02/07 17:52:59 quinn
26 * A damn mess, but now things work, I think.
28 * Revision 1.1 1995/02/02 16:21:53 quinn
38 * if p: write tag. return 1 (success) or -1 (error).
41 * if tag && class match up, advance pointer and return 1. set cons.
42 * else leave pointer unchanged. Return 0.
44 * Should perhaps be odr_tag?
46 int ber_tag(ODR o, void *p, int class, int tag, int *constructed, int opt)
48 static int lclass = -1, ltag, br, lcons; /* save t&c rather than
53 if (o->direction == ODR_DECODE)
70 if ((rd = ber_enctag(o->bp, class, tag, *constructed, o->left))
79 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", class, tag,
80 *constructed, o->stackp);
84 if (o->stackp > -1 && !odr_constructed_more(o))
92 if ((br = ber_dectag(o->bp, &lclass, <ag, &lcons)) <= 0)
98 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", lclass, ltag,
102 if (class == lclass && tag == ltag)
106 *constructed = lcons;
113 o->error = OREQUIRED;
118 o->error = OREQUIRED;
120 default: o->error = OOTHER; return 0;
125 * BER-encode a class/tag/constructed package (identifier octets). Return
126 * number of bytes encoded, or -1 if out of bounds.
128 int ber_enctag(unsigned char *buf, int class, int tag, int constructed, int len)
130 int cons = (constructed ? 1 : 0), n = 0;
131 unsigned char octs[sizeof(int)], *b = buf;
133 *b = (class << 6) & 0XC0;
134 *b |= (cons << 5) & 0X20;
145 octs[n++] = tag & 0X7F;
147 if (n >= len) /* bounds check */
152 *(b++) = octs[n] | ((n > 0) << 7);
158 * Decode BER identifier octets. Return number of bytes read or -1 for error.
160 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
162 unsigned char *b = buf;
165 *constructed = (*b >> 5) & 0X01;
166 if ((*tag = *b & 0x1F) <= 30)
174 if (b - buf >= 5) /* Precaution */
177 while (*(b++) & 0X80);