More in the way of error-checking.
[yaz-moved-to-github.git] / odr / ber_any.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_any.c,v $
7  * Revision 1.3  1995-02-10 18:57:24  quinn
8  * More in the way of error-checking.
9  *
10  * Revision 1.2  1995/02/10  15:55:28  quinn
11  * Bug fixes, mostly.
12  *
13  * Revision 1.1  1995/02/09  15:51:45  quinn
14  * Works better now.
15  *
16  */
17
18 #include <odr.h>
19
20 int ber_any(ODR o, Odr_any **p)
21 {
22     int res;
23
24     switch (o->direction)
25     {
26         case ODR_DECODE:
27             if ((res = completeBER(o->bp, 1000)) <= 0)        /* FIX THIS */
28                 return 0;
29             (*p)->buf = nalloc(o, res);
30             memcpy((*p)->buf, o->bp, res);
31             (*p)->len = (*p)->size = res;
32             o->bp += res;
33             o->left -= res;
34             return 1;
35         case ODR_ENCODE:
36             if ((*p)->len > o->left)
37                 return 0;
38             memcpy(o->bp , (*p)->buf, (*p)->len);
39             o->bp += (*p)->len;
40             o->left -= (*p)->len;
41             return 1;
42         default: return 0;
43     }
44 }
45
46 /*
47  * Return length of BER-package or -1.
48  */
49 int completeBER(unsigned char *buf, int len)
50 {
51     int res, ll, class, tag, cons;
52     unsigned char *b = buf;
53     
54     if (!buf[0] && !buf[1])
55         return 0;
56     if ((res = ber_dectag(b, &class, &tag, &cons)) <= 0)
57         return 0;
58     if (res > len)
59         return 0;
60     b += res;
61     len -= res;
62     if ((res = ber_declen(b, &ll)) <= 0)
63         return 0;
64     if (res > len)
65         return 0;
66     b += res;
67     len -= res;
68     if (ll >= 0)
69         return (len >= ll ? ll + (b-buf) : -1);
70     if (!cons)
71         return 0;    
72     while (1)
73     {
74         if ((res = completeBER(b, len)) < 0)
75             return 0;
76         b += res;
77         len -= res;
78         if (len < 2)
79             return 0;
80         if (*b == 0 && *(b + 1) == 0)
81             return (b - buf) + 2;
82     }
83 }