2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_cons.c,v 1.5 2005-01-15 19:47:14 adam Exp $
11 * \brief Implements ODR constructed codec.
20 void odr_setlenlen(ODR o, int len)
25 int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
30 int lenlen = o->lenlen;
34 o->lenlen = 1; /* reset lenlen */
40 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, 1, name)) < 0)
45 if (o->op->stackp == ODR_MAX_STACK - 1)
47 odr_seterror(o, OSTACK, 30);
50 o->op->stack[++(o->op->stackp)].lenb = o->bp;
51 o->op->stack[o->op->stackp].len_offset = odr_tell(o);
52 o->op->stack_names[o->op->stackp] = name ? name : "?";
53 o->op->stack_names[o->op->stackp + 1] = 0;
55 fprintf(stderr, "[cons_begin(%d)]", o->op->stackp);
57 if (o->direction == ODR_ENCODE)
59 static unsigned char dummy[sizeof(int)+1];
61 o->op->stack[o->op->stackp].lenlen = lenlen;
63 if (odr_write(o, dummy, lenlen) < 0) /* dummy */
65 o->op->stack_names[o->op->stackp] = 0;
70 else if (o->direction == ODR_DECODE)
72 if ((res = ber_declen(o->bp, &o->op->stack[o->op->stackp].len,
75 odr_seterror(o, OOTHER, 31);
76 o->op->stack_names[o->op->stackp] = 0;
80 o->op->stack[o->op->stackp].lenlen = res;
82 if (o->op->stack[o->op->stackp].len > odr_max(o))
84 odr_seterror(o, OOTHER, 32);
85 o->op->stack_names[o->op->stackp] = 0;
90 else if (o->direction == ODR_PRINT)
98 odr_seterror(o, OOTHER, 33);
99 o->op->stack_names[o->op->stackp] = 0;
103 o->op->stack[o->op->stackp].base = o->bp;
104 o->op->stack[o->op->stackp].base_offset = odr_tell(o);
108 int odr_constructed_more(ODR o)
112 if (o->op->stackp < 0)
114 if (o->op->stack[o->op->stackp].len >= 0)
115 return o->bp - o->op->stack[o->op->stackp].base < o->op->stack[o->op->stackp].len;
117 return (!(*o->bp == 0 && *(o->bp + 1) == 0));
120 int odr_constructed_end(ODR o)
127 if (o->op->stackp < 0)
129 odr_seterror(o, OOTHER, 34);
132 o->op->stack_names[o->op->stackp] = 0;
133 switch (o->direction)
136 if (o->op->stack[o->op->stackp].len < 0)
138 if (*o->bp++ == 0 && *(o->bp++) == 0)
145 odr_seterror(o, OOTHER, 35);
149 else if (o->bp - o->op->stack[o->op->stackp].base !=
150 o->op->stack[o->op->stackp].len)
152 odr_seterror(o, OCONLEN, 36);
159 odr_seek(o, ODR_S_SET, o->op->stack[o->op->stackp].len_offset);
160 if ((res = ber_enclen(o, pos - o->op->stack[o->op->stackp].base_offset,
161 o->op->stack[o->op->stackp].lenlen, 1)) < 0)
163 odr_seterror(o, OLENOV, 37);
166 odr_seek(o, ODR_S_END, 0);
167 if (res == 0) /* indefinite encoding */
170 fprintf(stderr, "[cons_end(%d): indefinite]", o->op->stackp);
172 if (odr_putc(o, 0) < 0 || odr_putc(o, 0) < 0)
178 fprintf(stderr, "[cons_end(%d): definite]", o->op->stackp);
187 odr_printf(o, "}\n");
190 odr_seterror(o, OOTHER, 38);