First kick.
[yaz-moved-to-github.git] / odr / odr_cons.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_cons.c,v $
7  * Revision 1.1  1995-02-02 16:21:53  quinn
8  * First kick.
9  *
10  */
11
12 #include <odr.h>
13
14 int odr_constructed_begin(ODR o, void *p, int class, int tag, int opt)
15 {
16     int res;
17     int cons = 1;
18
19     if (o->direction == ODR_ENCODE && !*(char*)p)
20         return opt;
21     if (o->t_class < 0)
22     {
23         o->t_class = class;
24         o->t_tag = tag;
25     }
26     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons)) < 0)
27         return 0;
28     if (!res || !cons)
29         return opt;
30
31     o->stack[++(o->stackp)].lenb = o->bp;
32     if (o->direction == ODR_ENCODE || o->direction == ODR_PRINT)
33     {
34         o->stack[o->stackp].lenlen = 1;
35         o->bp++;
36         o->left--;
37     }
38     else if (o->direction == ODR_DECODE)
39     {
40         if ((res = ber_declen(o->bp, &o->stack[o->stackp].len)) < 0)
41             return 0;
42         o->stack[o->stackp].lenlen = res;
43         o->bp += res;
44         o->left -= res;
45     }
46     else return 0;
47
48     o->stack[o->stackp].base = o->bp;
49     return 1;
50 }
51
52 int odr_constructed_end(ODR o)
53 {
54     int res;
55
56     if (o->stackp < 0)
57         return 0;
58     switch (o->direction)
59     {
60         case ODR_DECODE:
61             if (o->stack[o->stackp].len < 0)
62             {
63                 if (*o->bp++ == 0 && *(o->bp++) == 0)
64                 {
65                     o->left -= 2;
66                     return 1;
67                 }
68                 else
69                     return 0;
70             }
71             else if (o->bp - o->stack[o->stackp].base !=
72                 o->stack[o->stackp].len)
73                 return 0;
74             o->stackp--;
75             return 1;
76         case ODR_ENCODE:
77             if ((res = ber_enclen(o->stack[o->stackp].lenb,
78                 o->bp - o->stack[o->stackp].base,
79                 o->stack[o->stackp].lenlen, 1)) < 0)
80                     return 0;
81             if (res == 0)   /* indefinite encoding */
82             {
83                 *(o->bp++) = *(o->bp++) = 0;
84                 o->left--;
85             }
86             o->stackp--;
87             return 1;
88         case ODR_PRINT: return 1;
89         default: return 0;
90     }
91 }