Fixed possible buf in proto.c
[yaz-moved-to-github.git] / odr / odr_seq.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_seq.c,v $
7  * Revision 1.9  1995-03-17 10:17:57  quinn
8  * Added memory management.
9  *
10  * Revision 1.8  1995/03/15  11:18:05  quinn
11  * Fixed serious bug in odr_cons
12  *
13  * Revision 1.7  1995/03/08  12:12:30  quinn
14  * Added better error checking.
15  *
16  * Revision 1.6  1995/02/10  15:55:29  quinn
17  * Bug fixes, mostly.
18  *
19  * Revision 1.5  1995/02/09  15:51:49  quinn
20  * Works better now.
21  *
22  * Revision 1.4  1995/02/07  17:53:00  quinn
23  * A damn mess, but now things work, I think.
24  *
25  * Revision 1.3  1995/02/07  14:13:46  quinn
26  * Bug fixes.
27  *
28  * Revision 1.2  1995/02/06  16:45:03  quinn
29  * Small mods.
30  *
31  * Revision 1.1  1995/02/02  16:21:54  quinn
32  * First kick.
33  *
34  */
35
36 #include <odr.h>
37 #include <assert.h>
38
39 int odr_sequence_begin(ODR o, void *p, int size)
40 {
41     char **pp = (char**) p;
42
43     if (o->error)
44         return 0;
45     if (o->t_class < 0)
46     {
47         o->t_class = ODR_UNIVERSAL;
48         o->t_tag = ODR_SEQUENCE;
49     }
50     if (o->direction == ODR_DECODE)
51         *pp = 0;
52     if (odr_constructed_begin(o, p, o->t_class, o->t_tag))
53     {
54         if (o->direction == ODR_DECODE && size)
55             *pp = odr_malloc(o, size);
56         if (o->direction == ODR_PRINT)
57         {
58             fprintf(o->print, "%s{\n", odr_indent(o));
59             o->indent++;
60         }
61         return 1;
62     }
63     else
64         return 0;
65 }
66
67 int odr_sequence_end(ODR o)
68 {
69     if (o->direction == ODR_PRINT)
70     {
71         assert(o->indent > 0);
72         o->indent--;
73         fprintf(o->print, "%s}\n", odr_indent(o));
74     }
75     return odr_constructed_end(o);    
76 }
77
78 int odr_sequence_more(ODR o)
79 {
80     return odr_constructed_more(o);
81 }
82
83 int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num)
84 {
85     char ***pp = (char***) p;  /* for dereferencing */
86     char **tmp;
87     char *dummy = "Nothing";
88     int size = 0, i;
89
90     if (!odr_sequence_begin(o, &dummy, 0))
91         return 0;
92
93     switch (o->direction)
94     {
95         case ODR_DECODE:
96             *num = 0;
97             while (odr_sequence_more(o))
98             {
99                 /* outgrown array? */
100                 if (*num * sizeof(void*) >= size)
101                 {
102                     /* double the buffer size */
103                     tmp = odr_malloc(o, sizeof(void*) * (size += size ? size :
104                         128));
105                     if (*num)
106                     {
107                         memcpy(tmp, *pp, *num * sizeof(void*));
108                         /*
109                          * For now, we just throw the old *p away, since we use
110                          * nibble memory anyway (disgusting, isn't it?).
111                          */
112                     }
113                     *pp = tmp;
114                 }
115                 if (!(*type)(o, (*pp) + *num, 0))
116                     return 0;
117                 (*num)++;
118             }
119             break;
120         case ODR_ENCODE: case ODR_PRINT:
121 #ifdef ODR_DEBUG
122             fprintf(stderr, "[seqof: num=%d]", *num);
123 #endif
124             for (i = 0; i < *num; i++)
125             {
126 #ifdef ODR_DEBUG
127                 fprintf(stderr, "[seqof: elem #%d]", i);
128 #endif
129                 if (!(*type)(o, *pp + i, 0))
130                     return 0;
131             }
132             break;
133         default:
134             o->error = OOTHER;
135             return 0;
136     }
137     return odr_sequence_end(o);
138 }