2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ber_tag.c,v 1.4 2005-01-15 19:47:11 adam Exp $
10 * \brief Implements BER tags encoding and decoding
12 * This source file implements BER encoding and decoding of
24 * if p: write tag. return 1 (success) or -1 (error).
27 * if tag && zclass match up, advance pointer and return 1. set cons.
28 * else leave pointer unchanged. Return 0.
30 * Should perhaps be odr_tag?
32 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
35 struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
37 char **pp = (char **)p;
39 if (o->direction == ODR_DECODE)
42 if (o->op->stackp < 0)
44 odr_seek(o, ODR_S_SET, 0);
47 odr_ber_tag->lclass = -1;
56 odr_seterror(o, OREQUIRED, 24);
57 odr_setelement (o, name);
61 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
64 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
65 *constructed, o->op->stackp);
70 if (o->op->stackp > -1 && !odr_constructed_more(o))
74 odr_seterror(o, OREQUIRED, 25);
75 odr_setelement(o, name);
79 if (odr_ber_tag->lclass < 0)
81 if ((odr_ber_tag->br =
82 ber_dectag(o->bp, &odr_ber_tag->lclass,
83 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
86 odr_seterror(o, OPROTO, 26);
87 odr_setelement(o, name);
92 "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
93 odr_ber_tag->lclass, odr_ber_tag->ltag,
94 odr_ber_tag->lcons, o->op->stackp);
97 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
99 o->bp += odr_ber_tag->br;
100 *constructed = odr_ber_tag->lcons;
101 odr_ber_tag->lclass = -1;
108 odr_seterror(o, OREQUIRED, 27);
109 odr_setelement(o, name);
116 odr_seterror(o,OREQUIRED, 28);
117 odr_setelement(o, name);
121 odr_seterror(o, OOTHER, 29);
122 odr_setelement(o, name);
128 * BER-encode a zclass/tag/constructed package (identifier octets). Return
129 * number of bytes encoded, or -1 if out of bounds.
131 int ber_enctag(ODR o, int zclass, int tag, int constructed)
133 int cons = (constructed ? 1 : 0), n = 0;
134 unsigned char octs[sizeof(int)], b;
136 b = (zclass << 6) & 0XC0;
137 b |= (cons << 5) & 0X20;
141 if (odr_putc(o, b) < 0)
148 if (odr_putc(o, b) < 0)
152 octs[n++] = tag & 0X7F;
160 oo = octs[n] | ((n > 0) << 7);
161 if (odr_putc(o, oo) < 0)
169 * Decode BER identifier octets. Return number of bytes read or -1 for error.
171 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
172 int *constructed, int max)
180 *constructed = (*b >> 5) & 0X01;
181 if ((*tag = *b & 0x1F) <= 30)
191 while (b[l++] & 0X80);