2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: ber_any.c,v 1.1 2003-10-27 12:21:30 adam Exp $
14 int ber_any(ODR o, Odr_any **p)
21 if ((res = completeBER(o->bp, odr_max(o))) <= 0) /* FIX THIS */
23 odr_seterror(o, OPROTO, 2);
26 (*p)->buf = (unsigned char *)odr_malloc(o, res);
27 memcpy((*p)->buf, o->bp, res);
28 (*p)->len = (*p)->size = res;
32 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
35 default: odr_seterror(o, OOTHER, 3); return 0;
39 #define BER_ANY_DEBUG 0
42 * Return length of BER-package or 0.
44 int completeBER_n(const unsigned char *buf, int len, int level)
46 int res, ll, zclass, tag, cons;
47 const unsigned char *b = buf;
50 if (len > 5000000 || level > 1000)
54 yaz_log(LOG_LOG, "completeBER lev=%d len=%d", level, len);
61 if (!buf[0] && !buf[1])
63 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
66 /* removed, since ber_dectag never reads that far .. */
73 res = ber_declen(b, &ll, len);
78 yaz_log(LOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
80 return -1; /* error */
86 yaz_log(LOG_LOG, "<<<<<<<<< return3 lev=%d res=-1", level);
88 return 0; /* incomplete length */
94 yaz_log(LOG_LOG, "<<<<<<<<< return2 lev=%d len=%d res=%d ll=%d",
97 return -1; /* error */
100 /* no longer necessary, since ber_declen never reads that far (returns -1) */
104 yaz_log(LOG_LOG, "<<<<<<<<< return4 lev=%d res=%d len=%d",
112 { /* definite length */
115 yaz_log(LOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
118 return (len >= ll ? ll + (b-buf) : 0);
120 /* indefinite length */
122 { /* if primitive, it's an error */
124 yaz_log(LOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
125 level, ll, len, res);
127 return -1; /* error */
129 /* constructed - cycle through children */
132 if (b[0] == 0 && b[1] == 0)
134 if (!(res = completeBER_n(b, len, level+1)))
143 return (b - buf) + 2;
146 int completeBER(const unsigned char *buf, int len)
148 int res = completeBER_n(buf, len, 0);