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