2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-02-10 15:55:28 quinn
10 * Revision 1.3 1995/02/09 15:51:46 quinn
13 * Revision 1.2 1995/02/07 17:52:59 quinn
14 * A damn mess, but now things work, I think.
16 * Revision 1.1 1995/02/02 16:21:53 quinn
26 * if p: write tag. return 1 (success) or -1 (error).
29 * if tag && class match up, advance pointer and return 1. set cons.
30 * else leave pointer unchanged. Return 0.
32 int ber_tag(ODR o, const void *p, int class, int tag, int *constructed)
34 static int lclass = -1, ltag, br, lcons; /* save t&c rather than
39 if (o->buf == o->bp) /* This is insurance. It shouldn't be necessary */
46 if ((rd = ber_enctag(o->bp, class, tag, *constructed, o->left))
52 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", class, tag,
57 if (o->stackp > -1 && !odr_constructed_more(o))
61 if ((br = ber_dectag(o->bp, &lclass, <ag, &lcons)) <= 0)
64 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", lclass, ltag,
68 if (class == lclass && tag == ltag)
78 case ODR_PRINT: return p != 0;
84 * BER-encode a class/tag/constructed package (identifier octets). Return
85 * number of bytes encoded, or -1 if out of bounds.
87 int ber_enctag(unsigned char *buf, int class, int tag, int constructed, int len)
89 int cons = (constructed ? 1 : 0), n = 0;
90 unsigned char octs[sizeof(int)], *b = buf;
92 *b = (class << 6) & 0XC0;
93 *b |= (cons << 5) & 0X20;
104 octs[n++] = tag & 0X7F;
106 if (n >= len) /* bounds check */
111 *(b++) = octs[n] | ((n > 0) << 7);
117 * Decode BER identifier octets. Return number of bytes read or -1 for error.
119 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
121 unsigned char *b = buf;
124 *constructed = (*b >> 5) & 0X01;
125 if ((*tag = *b & 0x1F) <= 30)
133 if (b - buf >= 5) /* Precaution */
136 while (*(b++) & 0X80);