2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: ber_tag.c,v 1.23 2002-07-25 12:51:08 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;
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))
65 if (odr_ber_tag->lclass < 0)
67 if ((odr_ber_tag->br = ber_dectag(o->bp, &odr_ber_tag->lclass,
68 &odr_ber_tag->ltag, &odr_ber_tag->lcons)) <= 0)
75 "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
76 odr_ber_tag->lclass, odr_ber_tag->ltag,
77 odr_ber_tag->lcons, o->stackp);
80 if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
82 o->bp += odr_ber_tag->br;
83 *constructed = odr_ber_tag->lcons;
84 odr_ber_tag->lclass = -1;
104 * BER-encode a zclass/tag/constructed package (identifier octets). Return
105 * number of bytes encoded, or -1 if out of bounds.
107 int ber_enctag(ODR o, int zclass, int tag, int constructed)
109 int cons = (constructed ? 1 : 0), n = 0;
110 unsigned char octs[sizeof(int)], b;
112 b = (zclass << 6) & 0XC0;
113 b |= (cons << 5) & 0X20;
117 if (odr_putc(o, b) < 0)
124 if (odr_putc(o, b) < 0)
128 octs[n++] = tag & 0X7F;
136 oo = octs[n] | ((n > 0) << 7);
137 if (odr_putc(o, oo) < 0)
145 * Decode BER identifier octets. Return number of bytes read or -1 for error.
147 int ber_dectag(const unsigned char *buf, int *zclass, int *tag, int *constructed)
149 const unsigned char *b = buf;
152 *constructed = (*b >> 5) & 0X01;
153 if ((*tag = *b & 0x1F) <= 30)
161 if (b - buf >= 5) /* Precaution */
164 while (*(b++) & 0X80);