2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: ber_tag.c,v 1.2 2004-04-14 12:58:27 adam Exp $
16 * if p: write tag. return 1 (success) or -1 (error).
19 * if tag && zclass match up, advance pointer and return 1. set cons.
20 * else leave pointer unchanged. Return 0.
22 * Should perhaps be odr_tag?
24 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
27 struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
29 char **pp = (char **)p;
31 if (o->direction == ODR_DECODE)
34 if (o->op->stackp < 0)
36 odr_seek(o, ODR_S_SET, 0);
39 odr_ber_tag->lclass = -1;
48 odr_seterror(o, OREQUIRED, 24);
49 odr_setelement (o, name);
53 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
56 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
57 *constructed, o->op->stackp);
62 if (o->op->stackp > -1 && !odr_constructed_more(o))
66 odr_seterror(o, OREQUIRED, 25);
67 odr_setelement(o, name);
71 if (odr_ber_tag->lclass < 0)
73 if ((odr_ber_tag->br =
74 ber_dectag(o->bp, &odr_ber_tag->lclass,
75 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
78 odr_seterror(o, OPROTO, 26);
79 odr_setelement(o, name);
84 "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
85 odr_ber_tag->lclass, odr_ber_tag->ltag,
86 odr_ber_tag->lcons, o->op->stackp);
89 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
91 o->bp += odr_ber_tag->br;
92 *constructed = odr_ber_tag->lcons;
93 odr_ber_tag->lclass = -1;
100 odr_seterror(o, OREQUIRED, 27);
101 odr_setelement(o, name);
108 odr_seterror(o,OREQUIRED, 28);
109 odr_setelement(o, name);
113 odr_seterror(o, OOTHER, 29);
114 odr_setelement(o, name);
120 * BER-encode a zclass/tag/constructed package (identifier octets). Return
121 * number of bytes encoded, or -1 if out of bounds.
123 int ber_enctag(ODR o, int zclass, int tag, int constructed)
125 int cons = (constructed ? 1 : 0), n = 0;
126 unsigned char octs[sizeof(int)], b;
128 b = (zclass << 6) & 0XC0;
129 b |= (cons << 5) & 0X20;
133 if (odr_putc(o, b) < 0)
140 if (odr_putc(o, b) < 0)
144 octs[n++] = tag & 0X7F;
152 oo = octs[n] | ((n > 0) << 7);
153 if (odr_putc(o, oo) < 0)
161 * Decode BER identifier octets. Return number of bytes read or -1 for error.
163 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
164 int *constructed, int max)
172 *constructed = (*b >> 5) & 0X01;
173 if ((*tag = *b & 0x1F) <= 30)
183 while (b[l++] & 0X80);