1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements BER ANY encoding and decoding.
10 * This source file implements BER encoding and decoding of
21 int ber_any(ODR o, Odr_any **p)
28 if ((res = completeBER(o->bp, odr_max(o))) <= 0) /* FIX THIS */
30 odr_seterror(o, OPROTO, 2);
33 (*p)->buf = (unsigned char *)odr_malloc(o, res);
34 memcpy((*p)->buf, o->bp, res);
35 (*p)->len = (*p)->size = res;
39 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
42 default: odr_seterror(o, OOTHER, 3); return 0;
46 #define BER_ANY_DEBUG 0
48 int completeBER_n(const unsigned char *buf, int len, int level)
50 int res, ll, zclass, tag, cons;
51 const unsigned char *b = buf;
56 yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
62 if (!buf[0] && !buf[1])
64 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
69 res = ber_declen(b, &ll, len);
73 yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
75 return -1; /* error */
80 yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
82 return 0; /* incomplete length */
87 { /* definite length */
91 yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
98 /* indefinite length */
100 { /* if primitive, it's an error */
102 yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
103 level, ll, len, res);
105 return -1; /* error */
107 /* constructed - cycle through children */
110 if (b[0] == 0 && b[1] == 0)
112 if (!(res = completeBER_n(b, len, level+1)))
121 return (b - buf) + 2;
124 int completeBER(const unsigned char *buf, int len)
126 int res = completeBER_n(buf, len, 0);
128 yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
137 * c-file-style: "Stroustrup"
138 * indent-tabs-mode: nil
140 * vim: shiftwidth=4 tabstop=8 expandtab