0002944f832e601f7a083870b353635b4b8c26ed
[yaz-moved-to-github.git] / src / ber_any.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file ber_any.c
8  * \brief Implements BER ANY encoding and decoding.
9  *
10  * This source file implements BER encoding and decoding of
11  * the ANY type.
12  */
13
14 #if HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #include <assert.h>
19 #include "odr-priv.h"
20
21 int ber_any(ODR o, Odr_any **p)
22 {
23     int res;
24
25     switch (o->direction)
26     {
27     case ODR_DECODE:
28         if ((res = completeBER(o->bp, odr_max(o))) <= 0)        /* FIX THIS */
29         {
30             odr_seterror(o, OPROTO, 2);
31             return 0;
32         }
33         (*p)->buf = (unsigned char *)odr_malloc(o, res);
34         memcpy((*p)->buf, o->bp, res);
35         (*p)->len = res;
36 #if OCT_SIZE
37         (*p)->size = res;
38 #endif
39         o->bp += res;
40         return 1;
41     case ODR_ENCODE:
42         if (odr_write(o, (*p)->buf, (*p)->len) < 0)
43             return 0;
44         return 1;
45     default: odr_seterror(o, OOTHER, 3); return 0;
46     }
47 }
48
49 #define BER_ANY_DEBUG 0
50
51 int completeBER_n(const unsigned char *buf, int len, int level)
52 {
53     int res, ll, zclass, tag, cons;
54     const unsigned char *b = buf;
55
56     if (level > 1000)
57     {
58 #if BER_ANY_DEBUG
59         yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
60 #endif
61         return -2;
62     }
63     if (len < 2)
64         return 0;
65     if (!buf[0] && !buf[1])
66         return -2;
67     if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
68         return 0;
69     b += res;
70     len -= res;
71     assert (len >= 0);
72     res = ber_declen(b, &ll, len);
73     if (res == -2)
74     {
75 #if BER_ANY_DEBUG
76         yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
77 #endif
78         return -1;  /* error */
79     }
80     if (res == -1)
81     {
82 #if BER_ANY_DEBUG
83         yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
84 #endif
85         return 0;    /* incomplete length */
86     }
87     b += res;
88     len -= res;
89     if (ll >= 0)
90     {   /* definite length */
91         if (len < ll)
92         {
93 #if BER_ANY_DEBUG
94             yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
95                     level, len, ll);
96 #endif
97             return 0;
98         }
99         return ll + (b-buf);
100     }
101     /* indefinite length */
102     if (!cons)
103     {   /* if primitive, it's an error */
104 #if BER_ANY_DEBUG
105         yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
106                 level, ll, len, res);
107 #endif
108         return -1;   /* error */
109     }
110     /* constructed - cycle through children */
111     while (len >= 2)
112     {
113         if (b[0] == 0 && b[1] == 0)
114             break;
115         res = completeBER_n(b, len, level+1);
116         if (res <= 0)
117             return res;
118         b += res;
119         len -= res;
120     }
121     if (len < 2)
122         return 0;
123     return (b - buf) + 2;
124 }
125
126 int completeBER(const unsigned char *buf, int len)
127 {
128     int res = completeBER_n(buf, len, 0);
129 #if BER_ANY_DEBUG
130     yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
131 #endif
132     if (res < 0)
133         return len;
134     return res;
135 }
136 /*
137  * Local variables:
138  * c-basic-offset: 4
139  * c-file-style: "Stroustrup"
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144