2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: ber_any.c,v 1.21 2003-02-12 15:06:43 adam Exp $
12 int ber_any(ODR o, Odr_any **p)
15 int left = o->size - (o->bp - o->buf);
20 if ((res = completeBER(o->bp, left)) <= 0) /* FIX THIS */
25 (*p)->buf = (unsigned char *)odr_malloc(o, res);
26 memcpy((*p)->buf, o->bp, res);
27 (*p)->len = (*p)->size = res;
31 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
34 default: o->error = OOTHER; return 0;
39 * Return length of BER-package or 0.
41 int completeBER(const unsigned char *buf, int len)
43 int res, ll, zclass, tag, cons;
44 const unsigned char *b = buf;
48 if (!buf[0] && !buf[1])
50 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
51 && buf[1] >= 0x20 && buf[1] < 0x7f
52 && buf[2] >= 0x20 && buf[2] < 0x7f)
54 /* deal with HTTP request/response */
55 int i = 2, content_len = 0;
59 if (buf[i] == '\r' && buf[i+1] == '\n')
62 if (buf[i] == '\r' && buf[i+1] == '\n')
64 /* i += 2 seems not to work with GCC -O2 ..
65 so i+2 is used instead .. */
66 if (len >= (i+2)+ content_len)
67 return (i+2)+ content_len;
72 if (!memcmp(buf+i, "Content-Length:", 15))
78 while (i <= len-4 && isdigit(buf[i]))
79 content_len = content_len*10 + (buf[i++] - '0');
80 if (content_len < 0) /* prevent negative offsets */
90 /* BER from now on .. */
91 if ((res = ber_dectag(b, &zclass, &tag, &cons)) <= 0)
97 if ((res = ber_declen(b, &ll)) <= 0)
104 return (len >= ll ? ll + (b-buf) : 0);
107 /* constructed - cycle through children */
110 if (*b == 0 && *(b + 1) == 0)
112 if (!(res = completeBER(b, len)))
119 return (b - buf) + 2;