2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-03-08 12:12:13 quinn
8 * Added better error checking.
10 * Revision 1.6 1995/02/14 11:54:33 quinn
13 * Revision 1.5 1995/02/10 18:57:24 quinn
14 * More in the way of error-checking.
16 * Revision 1.4 1995/02/10 15:55:28 quinn
19 * Revision 1.3 1995/02/09 15:51:46 quinn
22 * Revision 1.2 1995/02/07 17:52:59 quinn
23 * A damn mess, but now things work, I think.
25 * Revision 1.1 1995/02/02 16:21:53 quinn
35 * if p: write tag. return 1 (success) or -1 (error).
38 * if tag && class match up, advance pointer and return 1. set cons.
39 * else leave pointer unchanged. Return 0.
41 * Should perhaps be odr_tag?
43 int ber_tag(ODR o, void *p, int class, int tag, int *constructed, int opt)
45 static int lclass = -1, ltag, br, lcons; /* save t&c rather than
50 if (o->direction == ODR_DECODE)
67 if ((rd = ber_enctag(o->bp, class, tag, *constructed, o->left))
76 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", class, tag,
81 if (o->stackp > -1 && !odr_constructed_more(o))
89 if ((br = ber_dectag(o->bp, &lclass, <ag, &lcons)) <= 0)
95 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", lclass, ltag,
99 if (class == lclass && tag == ltag)
103 *constructed = lcons;
110 o->error = OREQUIRED;
115 o->error = OREQUIRED;
117 default: o->error = OOTHER; return 0;
122 * BER-encode a class/tag/constructed package (identifier octets). Return
123 * number of bytes encoded, or -1 if out of bounds.
125 int ber_enctag(unsigned char *buf, int class, int tag, int constructed, int len)
127 int cons = (constructed ? 1 : 0), n = 0;
128 unsigned char octs[sizeof(int)], *b = buf;
130 *b = (class << 6) & 0XC0;
131 *b |= (cons << 5) & 0X20;
142 octs[n++] = tag & 0X7F;
144 if (n >= len) /* bounds check */
149 *(b++) = octs[n] | ((n > 0) << 7);
155 * Decode BER identifier octets. Return number of bytes read or -1 for error.
157 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
159 unsigned char *b = buf;
162 *constructed = (*b >> 5) & 0X01;
163 if ((*tag = *b & 0x1F) <= 30)
171 if (b - buf >= 5) /* Precaution */
174 while (*(b++) & 0X80);