2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: ber_tag.c,v 1.1 2003-10-27 12:21:30 adam Exp $
17 * if p: write tag. return 1 (success) or -1 (error).
20 * if tag && zclass match up, advance pointer and return 1. set cons.
21 * else leave pointer unchanged. Return 0.
23 * Should perhaps be odr_tag?
25 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
28 struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
30 char **pp = (char **)p;
32 if (o->direction == ODR_DECODE)
35 if (o->op->stackp < 0)
37 odr_seek(o, ODR_S_SET, 0);
40 odr_ber_tag->lclass = -1;
49 odr_seterror(o, OREQUIRED, 24);
50 odr_setelement (o, name);
54 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
57 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
58 *constructed, o->stackp);
63 if (o->op->stackp > -1 && !odr_constructed_more(o))
67 odr_seterror(o, OREQUIRED, 25);
68 odr_setelement(o, name);
72 if (odr_ber_tag->lclass < 0)
74 if ((odr_ber_tag->br =
75 ber_dectag(o->bp, &odr_ber_tag->lclass,
76 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
79 odr_seterror(o, OPROTO, 26);
80 odr_setelement(o, name);
85 "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
86 odr_ber_tag->lclass, odr_ber_tag->ltag,
87 odr_ber_tag->lcons, o->stackp);
90 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
92 o->bp += odr_ber_tag->br;
93 *constructed = odr_ber_tag->lcons;
94 odr_ber_tag->lclass = -1;
101 odr_seterror(o, OREQUIRED, 27);
102 odr_setelement(o, name);
109 odr_seterror(o,OREQUIRED, 28);
110 odr_setelement(o, name);
114 odr_seterror(o, OOTHER, 29);
115 odr_setelement(o, name);
121 * BER-encode a zclass/tag/constructed package (identifier octets). Return
122 * number of bytes encoded, or -1 if out of bounds.
124 int ber_enctag(ODR o, int zclass, int tag, int constructed)
126 int cons = (constructed ? 1 : 0), n = 0;
127 unsigned char octs[sizeof(int)], b;
129 b = (zclass << 6) & 0XC0;
130 b |= (cons << 5) & 0X20;
134 if (odr_putc(o, b) < 0)
141 if (odr_putc(o, b) < 0)
145 octs[n++] = tag & 0X7F;
153 oo = octs[n] | ((n > 0) << 7);
154 if (odr_putc(o, oo) < 0)
162 * Decode BER identifier octets. Return number of bytes read or -1 for error.
164 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
165 int *constructed, int max)
173 *constructed = (*b >> 5) & 0X01;
174 if ((*tag = *b & 0x1F) <= 30)
184 while (b[l++] & 0X80);