c28ebb58e9c56156c79934a48742c7e564451d64
[yaz-moved-to-github.git] / src / ber_any.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: ber_any.c,v 1.1 2003-10-27 12:21:30 adam Exp $
6  */
7 #if HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <assert.h>
12 #include "odr-priv.h"
13
14 int ber_any(ODR o, Odr_any **p)
15 {
16     int res;
17     
18     switch (o->direction)
19     {
20     case ODR_DECODE:
21         if ((res = completeBER(o->bp, odr_max(o))) <= 0)        /* FIX THIS */
22         {
23             odr_seterror(o, OPROTO, 2);
24             return 0;
25         }
26         (*p)->buf = (unsigned char *)odr_malloc(o, res);
27         memcpy((*p)->buf, o->bp, res);
28         (*p)->len = (*p)->size = res;
29         o->bp += res;
30         return 1;
31     case ODR_ENCODE:
32         if (odr_write(o, (*p)->buf, (*p)->len) < 0)
33             return 0;
34         return 1;
35     default: odr_seterror(o, OOTHER, 3); return 0;
36     }
37 }
38
39 #define BER_ANY_DEBUG 0
40
41 /*
42  * Return length of BER-package or 0.
43  */
44 int completeBER_n(const unsigned char *buf, int len, int level)
45 {
46     int res, ll, zclass, tag, cons;
47     const unsigned char *b = buf;
48     int bad = 0;
49     
50     if (len > 5000000 || level > 1000)
51     {
52         bad = 1;
53 #if BER_ANY_DEBUG
54         yaz_log(LOG_LOG, "completeBER lev=%d len=%d", level, len);
55 #endif
56         if (level > 1000)
57             return -2;
58     }
59     if (len < 2)
60         return 0;
61     if (!buf[0] && !buf[1])
62         return -2;
63     if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
64         return 0;
65 #if 0
66 /* removed, since ber_dectag never reads that far .. */
67     if (res > len)
68         return 0;
69 #endif
70     b += res;
71     len -= res;
72     assert (len >= 0);
73     res = ber_declen(b, &ll, len);
74     if (res == -2)
75     {
76 #if BER_ANY_DEBUG
77         if (bad)
78             yaz_log(LOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
79 #endif
80         return -1;  /* error */
81     }
82     if (res == -1)  
83     {
84 #if BER_ANY_DEBUG
85         if (bad)
86             yaz_log(LOG_LOG, "<<<<<<<<< return3 lev=%d res=-1", level);
87 #endif
88         return 0;    /* incomplete length */
89     }
90     if (ll > 5000000)
91     {
92 #if BER_ANY_DEBUG
93         if (bad)
94             yaz_log(LOG_LOG, "<<<<<<<<< return2 lev=%d len=%d res=%d ll=%d",
95                     level, len, res, ll);
96 #endif
97         return -1;  /* error */
98     }
99 #if 0
100 /* no longer necessary, since ber_declen never reads that far (returns -1) */
101     if (res > len)
102     {
103         if (bad)
104             yaz_log(LOG_LOG, "<<<<<<<<< return4 lev=%d res=%d len=%d",
105                     level, res, len);
106         return 0;
107     }
108 #endif
109     b += res;
110     len -= res;
111     if (ll >= 0)
112     {   /* definite length */
113 #if BER_ANY_DEBUG
114         if (bad && len < ll)
115             yaz_log(LOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
116                     level, len, ll);
117 #endif
118         return (len >= ll ? ll + (b-buf) : 0);
119     }
120     /* indefinite length */
121     if (!cons)
122     {   /* if primitive, it's an error */
123 #if BER_ANY_DEBUG
124         yaz_log(LOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
125                 level, ll, len, res);
126 #endif
127         return -1;   /* error */
128     }
129     /* constructed - cycle through children */
130     while (len >= 2)
131     {
132         if (b[0] == 0 && b[1] == 0)
133             break;
134         if (!(res = completeBER_n(b, len, level+1)))
135             return 0;
136         if (res == -1)
137             return -1;
138         b += res;
139         len -= res;
140     }
141     if (len < 2)
142         return 0;
143     return (b - buf) + 2;
144 }
145
146 int completeBER(const unsigned char *buf, int len)
147 {
148     int res = completeBER_n(buf, len, 0);
149     if (res < 0)
150         return len;
151     return res;
152 }