Fixed typo
[yaz-moved-to-github.git] / src / ber_any.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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 = (*p)->size = res;
36         o->bp += res;
37         return 1;
38     case ODR_ENCODE:
39         if (odr_write(o, (*p)->buf, (*p)->len) < 0)
40             return 0;
41         return 1;
42     default: odr_seterror(o, OOTHER, 3); return 0;
43     }
44 }
45
46 #define BER_ANY_DEBUG 0
47
48 int completeBER_n(const unsigned char *buf, int len, int level)
49 {
50     int res, ll, zclass, tag, cons;
51     const unsigned char *b = buf;
52     
53     if (level > 1000)
54     {
55 #if BER_ANY_DEBUG
56         yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
57 #endif
58         return -2;
59     }
60     if (len < 2)
61         return 0;
62     if (!buf[0] && !buf[1])
63         return -2;
64     if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
65         return 0;
66     b += res;
67     len -= res;
68     assert (len >= 0);
69     res = ber_declen(b, &ll, len);
70     if (res == -2)
71     {
72 #if BER_ANY_DEBUG
73         yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
74 #endif
75         return -1;  /* error */
76     }
77     if (res == -1)  
78     {
79 #if BER_ANY_DEBUG
80         yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
81 #endif
82         return 0;    /* incomplete length */
83     }
84     b += res;
85     len -= res;
86     if (ll >= 0)
87     {   /* definite length */
88         if (len < ll)
89         {
90 #if BER_ANY_DEBUG
91             yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
92                     level, len, ll);
93 #endif
94             return 0;
95         }
96         return ll + (b-buf);
97     }
98     /* indefinite length */
99     if (!cons)
100     {   /* if primitive, it's an error */
101 #if BER_ANY_DEBUG
102         yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
103                 level, ll, len, res);
104 #endif
105         return -1;   /* error */
106     }
107     /* constructed - cycle through children */
108     while (len >= 2)
109     {
110         if (b[0] == 0 && b[1] == 0)
111             break;
112         if (!(res = completeBER_n(b, len, level+1)))
113             return 0;
114         if (res == -1)
115             return -1;
116         b += res;
117         len -= res;
118     }
119     if (len < 2)
120         return 0;
121     return (b - buf) + 2;
122 }
123
124 int completeBER(const unsigned char *buf, int len)
125 {
126     int res = completeBER_n(buf, len, 0);
127 #if BER_ANY_DEBUG
128     yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
129 #endif
130     if (res < 0)
131         return len;
132     return res;
133 }
134 /*
135  * Local variables:
136  * c-basic-offset: 4
137  * c-file-style: "Stroustrup"
138  * indent-tabs-mode: nil
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */
142