2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_cons.c,v 1.7 2005-08-11 14:21:55 adam Exp $
11 * \brief Implements ODR constructed codec.
22 void odr_setlenlen(ODR o, int len)
27 int odr_constructed_begin(ODR o, void *xxp, int zclass, int tag,
32 int lenlen = o->lenlen;
36 o->lenlen = 1; /* reset lenlen */
42 if ((res = ber_tag(o, xxp, o->t_class, o->t_tag, &cons, 1, name)) < 0)
47 /* push the odr_constack */
48 if (o->op->stack_top && o->op->stack_top->next)
51 o->op->stack_top = o->op->stack_top->next;
53 else if (o->op->stack_top && !o->op->stack_top->next)
55 /* must allocate new entry (not first) */
57 struct odr_constack *st;
58 /* check size first */
59 for (st = o->op->stack_top; st; st = st->prev)
62 if (sz >= ODR_MAX_STACK)
64 odr_seterror(o, OSTACK, 30);
67 o->op->stack_top->next = (struct odr_constack *)
68 odr_malloc(o, sizeof(*o->op->stack_top));
69 o->op->stack_top->next->prev = o->op->stack_top;
70 o->op->stack_top->next->next = 0;
72 o->op->stack_top = o->op->stack_top->next;
74 else if (!o->op->stack_top)
77 if (!o->op->stack_first)
79 /* first item must be allocated */
80 o->op->stack_first = (struct odr_constack *)
81 odr_malloc(o, sizeof(*o->op->stack_top));
82 o->op->stack_first->prev = 0;
83 o->op->stack_first->next = 0;
85 o->op->stack_top = o->op->stack_first;
86 assert(o->op->stack_top->prev == 0);
88 o->op->stack_top->lenb = o->bp;
89 o->op->stack_top->len_offset = odr_tell(o);
90 o->op->stack_top->name = name ? name : "?";
91 if (o->direction == ODR_ENCODE)
93 static unsigned char dummy[sizeof(int)+1];
95 o->op->stack_top->lenlen = lenlen;
97 if (odr_write(o, dummy, lenlen) < 0) /* dummy */
103 else if (o->direction == ODR_DECODE)
105 if ((res = ber_declen(o->bp, &o->op->stack_top->len,
108 odr_seterror(o, OOTHER, 31);
112 o->op->stack_top->lenlen = res;
114 if (o->op->stack_top->len > odr_max(o))
116 odr_seterror(o, OOTHER, 32);
121 else if (o->direction == ODR_PRINT)
124 odr_printf(o, "{\n");
129 odr_seterror(o, OOTHER, 33);
133 o->op->stack_top->base = o->bp;
134 o->op->stack_top->base_offset = odr_tell(o);
138 int odr_constructed_more(ODR o)
142 if (ODR_STACK_EMPTY(o))
144 if (o->op->stack_top->len >= 0)
145 return o->bp - o->op->stack_top->base < o->op->stack_top->len;
147 return (!(*o->bp == 0 && *(o->bp + 1) == 0));
150 int odr_constructed_end(ODR o)
157 if (ODR_STACK_EMPTY(o))
159 odr_seterror(o, OOTHER, 34);
162 switch (o->direction)
165 if (o->op->stack_top->len < 0)
167 if (*o->bp++ == 0 && *(o->bp++) == 0)
174 odr_seterror(o, OOTHER, 35);
178 else if (o->bp - o->op->stack_top->base !=
179 o->op->stack_top->len)
181 odr_seterror(o, OCONLEN, 36);
188 odr_seek(o, ODR_S_SET, o->op->stack_top->len_offset);
189 if ((res = ber_enclen(o, pos - o->op->stack_top->base_offset,
190 o->op->stack_top->lenlen, 1)) < 0)
192 odr_seterror(o, OLENOV, 37);
195 odr_seek(o, ODR_S_END, 0);
196 if (res == 0) /* indefinite encoding */
198 if (odr_putc(o, 0) < 0 || odr_putc(o, 0) < 0)
207 odr_printf(o, "}\n");
210 odr_seterror(o, OOTHER, 38);
217 * indent-tabs-mode: nil
219 * vim: shiftwidth=4 tabstop=8 expandtab