2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: ber_any.c,v 1.22 2003-02-14 18:49:23 adam Exp $
14 int ber_any(ODR o, Odr_any **p)
17 int left = o->size - (o->bp - o->buf);
22 if ((res = completeBER(o->bp, left)) <= 0) /* FIX THIS */
27 (*p)->buf = (unsigned char *)odr_malloc(o, res);
28 memcpy((*p)->buf, o->bp, res);
29 (*p)->len = (*p)->size = res;
33 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
36 default: o->error = OOTHER; return 0;
41 * Return length of BER-package or 0.
43 int completeBER(const unsigned char *buf, int len)
45 int res, ll, zclass, tag, cons;
46 const unsigned char *b = buf;
50 if (!buf[0] && !buf[1])
52 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
53 && buf[1] >= 0x20 && buf[1] < 0x7f
54 && buf[2] >= 0x20 && buf[2] < 0x7f)
56 /* deal with HTTP request/response */
57 int i = 2, content_len = 0;
61 if (buf[i] == '\r' && buf[i+1] == '\n')
64 if (buf[i] == '\r' && buf[i+1] == '\n')
66 /* i += 2 seems not to work with GCC -O2 ..
67 so i+2 is used instead .. */
68 if (len >= (i+2)+ content_len)
69 return (i+2)+ content_len;
74 if (!memcmp(buf+i, "Content-Length:", 15))
80 while (i <= len-4 && isdigit(buf[i]))
81 content_len = content_len*10 + (buf[i++] - '0');
82 if (content_len < 0) /* prevent negative offsets */
92 /* BER from now on .. */
93 if ((res = ber_dectag(b, &zclass, &tag, &cons)) <= 0)
99 if ((res = ber_declen(b, &ll)) <= 0)
106 return (len >= ll ? ll + (b-buf) : 0);
109 /* constructed - cycle through children */
112 if (*b == 0 && *(b + 1) == 0)
114 if (!(res = completeBER(b, len)))
121 return (b - buf) + 2;