1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements BER tags encoding and decoding
10 * This source file implements BER encoding and decoding of
21 * \brief Encode/decode BER tags
25 * if p: write tag. return 1 (success) or -1 (error).
30 * if tag && zclass match up, advance pointer and return 1. set cons.
31 * else leave pointer unchanged. Return 0.
34 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
37 struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
39 char **pp = (char **)p;
41 if (o->direction == ODR_DECODE)
44 if (ODR_STACK_EMPTY(o))
46 odr_seek(o, ODR_S_SET, 0);
49 odr_ber_tag->lclass = -1;
58 odr_seterror(o, OREQUIRED, 24);
59 odr_setelement (o, name);
63 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
67 if (ODR_STACK_NOT_EMPTY(o) && !odr_constructed_more(o))
71 odr_seterror(o, OREQUIRED, 25);
72 odr_setelement(o, name);
76 if (odr_ber_tag->lclass < 0)
78 if ((odr_ber_tag->br =
79 ber_dectag(o->bp, &odr_ber_tag->lclass,
80 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
83 odr_seterror(o, OPROTO, 26);
84 odr_setelement(o, name);
88 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
90 o->bp += odr_ber_tag->br;
91 *constructed = odr_ber_tag->lcons;
92 odr_ber_tag->lclass = -1;
99 odr_seterror(o, OREQUIRED, 27);
100 odr_setelement(o, name);
107 odr_seterror(o,OREQUIRED, 28);
108 odr_setelement(o, name);
112 odr_seterror(o, OOTHER, 29);
113 odr_setelement(o, name);
119 * \brief BER-encode a zclass/tag/constructed package (identifier octets).
121 * Return 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 * \brief Decodes BER identifier octets.
163 * Returns number of bytes read or -1 for error.
165 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
166 int *constructed, int max)
174 *constructed = (*b >> 5) & 0X01;
175 if ((*tag = *b & 0x1F) <= 30)
185 while (b[l++] & 0X80);
191 * c-file-style: "Stroustrup"
192 * indent-tabs-mode: nil
194 * vim: shiftwidth=4 tabstop=8 expandtab