2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ber_tag.c,v 1.6 2005-08-11 14:21:55 adam Exp $
10 * \brief Implements BER tags encoding and decoding
12 * This source file implements BER encoding and decoding of
23 * \brief Encode/decode BER tags
27 * if p: write tag. return 1 (success) or -1 (error).
32 * if tag && zclass match up, advance pointer and return 1. set cons.
33 * else leave pointer unchanged. Return 0.
36 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
39 struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
41 char **pp = (char **)p;
43 if (o->direction == ODR_DECODE)
46 if (ODR_STACK_EMPTY(o))
48 odr_seek(o, ODR_S_SET, 0);
51 odr_ber_tag->lclass = -1;
60 odr_seterror(o, OREQUIRED, 24);
61 odr_setelement (o, name);
65 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
69 if (ODR_STACK_NOT_EMPTY(o) && !odr_constructed_more(o))
73 odr_seterror(o, OREQUIRED, 25);
74 odr_setelement(o, name);
78 if (odr_ber_tag->lclass < 0)
80 if ((odr_ber_tag->br =
81 ber_dectag(o->bp, &odr_ber_tag->lclass,
82 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
85 odr_seterror(o, OPROTO, 26);
86 odr_setelement(o, name);
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 * \brief BER-encode a zclass/tag/constructed package (identifier octets).
123 * Return number of bytes encoded, or -1 if out of bounds.
125 int ber_enctag(ODR o, int zclass, int tag, int constructed)
127 int cons = (constructed ? 1 : 0), n = 0;
128 unsigned char octs[sizeof(int)], b;
130 b = (zclass << 6) & 0XC0;
131 b |= (cons << 5) & 0X20;
135 if (odr_putc(o, b) < 0)
142 if (odr_putc(o, b) < 0)
146 octs[n++] = tag & 0X7F;
154 oo = octs[n] | ((n > 0) << 7);
155 if (odr_putc(o, oo) < 0)
163 * \brief Decodes BER identifier octets.
165 * Returns number of bytes read or -1 for error.
167 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
168 int *constructed, int max)
176 *constructed = (*b >> 5) & 0X01;
177 if ((*tag = *b & 0x1F) <= 30)
187 while (b[l++] & 0X80);
193 * indent-tabs-mode: nil
195 * vim: shiftwidth=4 tabstop=8 expandtab