2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ber_any.c,v 1.6 2007-01-03 08:42:15 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
50 int completeBER_n(const unsigned char *buf, int len, int level)
52 int res, ll, zclass, tag, cons;
53 const unsigned char *b = buf;
58 yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
64 if (!buf[0] && !buf[1])
66 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
71 res = ber_declen(b, &ll, len);
75 yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
77 return -1; /* error */
82 yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
84 return 0; /* incomplete length */
89 { /* definite length */
93 yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
100 /* indefinite length */
102 { /* if primitive, it's an error */
104 yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
105 level, ll, len, res);
107 return -1; /* error */
109 /* constructed - cycle through children */
112 if (b[0] == 0 && b[1] == 0)
114 if (!(res = completeBER_n(b, len, level+1)))
123 return (b - buf) + 2;
126 int completeBER(const unsigned char *buf, int len)
128 int res = completeBER_n(buf, len, 0);
130 yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
139 * indent-tabs-mode: nil
141 * vim: shiftwidth=4 tabstop=8 expandtab