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.25 2003-03-11 11:03:31 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)
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;
47 odr_seterror(o, OREQUIRED, 24);
50 if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
53 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
54 *constructed, o->stackp);
59 if (o->op->stackp > -1 && !odr_constructed_more(o))
62 odr_seterror(o, OREQUIRED, 25);
65 if (odr_ber_tag->lclass < 0)
67 if ((odr_ber_tag->br =
68 ber_dectag(o->bp, &odr_ber_tag->lclass,
69 &odr_ber_tag->ltag, &odr_ber_tag->lcons,
72 odr_seterror(o, OPROTO, 26);
77 "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
78 odr_ber_tag->lclass, odr_ber_tag->ltag,
79 odr_ber_tag->lcons, o->stackp);
82 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
84 o->bp += odr_ber_tag->br;
85 *constructed = odr_ber_tag->lcons;
86 odr_ber_tag->lclass = -1;
92 odr_seterror(o, OREQUIRED, 27);
97 odr_seterror(o,OREQUIRED, 28);
100 odr_seterror(o, OOTHER, 29);
106 * BER-encode a zclass/tag/constructed package (identifier octets). Return
107 * number of bytes encoded, or -1 if out of bounds.
109 int ber_enctag(ODR o, int zclass, int tag, int constructed)
111 int cons = (constructed ? 1 : 0), n = 0;
112 unsigned char octs[sizeof(int)], b;
114 b = (zclass << 6) & 0XC0;
115 b |= (cons << 5) & 0X20;
119 if (odr_putc(o, b) < 0)
126 if (odr_putc(o, b) < 0)
130 octs[n++] = tag & 0X7F;
138 oo = octs[n] | ((n > 0) << 7);
139 if (odr_putc(o, oo) < 0)
147 * Decode BER identifier octets. Return number of bytes read or -1 for error.
149 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
150 int *constructed, int max)
158 *constructed = (*b >> 5) & 0X01;
159 if ((*tag = *b & 0x1F) <= 30)
169 while (b[l++] & 0X80);