2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ber_any.c,v 1.4 2005-06-25 15:46:03 adam Exp $
10 * \brief Implements BER ANY encoding and decoding.
12 * This source file implements BER encoding and decoding of
23 int ber_any(ODR o, Odr_any **p)
30 if ((res = completeBER(o->bp, odr_max(o))) <= 0) /* FIX THIS */
32 odr_seterror(o, OPROTO, 2);
35 (*p)->buf = (unsigned char *)odr_malloc(o, res);
36 memcpy((*p)->buf, o->bp, res);
37 (*p)->len = (*p)->size = res;
41 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
44 default: odr_seterror(o, OOTHER, 3); return 0;
48 #define BER_ANY_DEBUG 0
51 * Return length of BER-package or 0.
53 int completeBER_n(const unsigned char *buf, int len, int level)
55 int res, ll, zclass, tag, cons;
56 const unsigned char *b = buf;
59 if (len > 5000000 || level > 1000)
63 yaz_log(LOG_LOG, "completeBER lev=%d len=%d", level, len);
70 if (!buf[0] && !buf[1])
72 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
75 /* removed, since ber_dectag never reads that far .. */
82 res = ber_declen(b, &ll, len);
87 yaz_log(LOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
89 return -1; /* error */
95 yaz_log(LOG_LOG, "<<<<<<<<< return3 lev=%d res=-1", level);
97 return 0; /* incomplete length */
103 yaz_log(LOG_LOG, "<<<<<<<<< return2 lev=%d len=%d res=%d ll=%d",
104 level, len, res, ll);
106 return -1; /* error */
109 /* no longer necessary, since ber_declen never reads that far (returns -1) */
113 yaz_log(LOG_LOG, "<<<<<<<<< return4 lev=%d res=%d len=%d",
121 { /* definite length */
124 yaz_log(LOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
127 return (len >= ll ? ll + (b-buf) : 0);
129 /* indefinite length */
131 { /* if primitive, it's an error */
133 yaz_log(LOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
134 level, ll, len, res);
136 return -1; /* error */
138 /* constructed - cycle through children */
141 if (b[0] == 0 && b[1] == 0)
143 if (!(res = completeBER_n(b, len, level+1)))
152 return (b - buf) + 2;
155 int completeBER(const unsigned char *buf, int len)
157 int res = completeBER_n(buf, len, 0);
165 * indent-tabs-mode: nil
167 * vim: shiftwidth=4 tabstop=8 expandtab