YAZ 5.35.1
ber_any.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
14#if HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18#include <assert.h>
19#include "odr-priv.h"
20
21int ber_any(ODR o, Odr_any **p)
22{
23 int res;
24
25 switch (o->direction)
26 {
27 case ODR_DECODE:
28 if ((res = completeBER(o->op->bp, odr_max(o))) <= 0) /* FIX THIS */
29 {
30 odr_seterror(o, OPROTO, 2);
31 return 0;
32 }
33 (*p)->buf = (char *)odr_malloc(o, res);
34 memcpy((*p)->buf, o->op->bp, res);
35 (*p)->len = res;
36 o->op->bp += res;
37 return 1;
38 case ODR_ENCODE:
39 if (odr_write(o, (*p)->buf, (*p)->len) < 0)
40 return 0;
41 return 1;
42 default: odr_seterror(o, OOTHER, 3); return 0;
43 }
44}
45
46#define BER_ANY_DEBUG 0
47
48int completeBER_n(const char *buf, int len, int level)
49{
50 int res, ll, zclass, tag, cons;
51 const char *b = buf;
52
53 if (level > 1000)
54 {
55#if BER_ANY_DEBUG
56 yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
57#endif
58 return -2;
59 }
60 if (len < 2)
61 return 0;
62 if (!buf[0] && !buf[1])
63 return -2;
64 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
65 return 0;
66 b += res;
67 len -= res;
68 assert (len >= 0);
69 res = ber_declen(b, &ll, len);
70 if (res == -2)
71 {
72#if BER_ANY_DEBUG
73 yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
74#endif
75 return -1; /* error */
76 }
77 if (res == -1)
78 {
79#if BER_ANY_DEBUG
80 yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
81#endif
82 return 0; /* incomplete length */
83 }
84 b += res;
85 len -= res;
86 if (ll >= 0)
87 { /* definite length */
88 if (len < ll)
89 {
90#if BER_ANY_DEBUG
91 yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
92 level, len, ll);
93#endif
94 return 0;
95 }
96 return ll + (b-buf);
97 }
98 /* indefinite length */
99 if (!cons)
100 { /* if primitive, it's an error */
101#if BER_ANY_DEBUG
102 yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
103 level, ll, len, res);
104#endif
105 return -1; /* error */
106 }
107 /* constructed - cycle through children */
108 while (len >= 2)
109 {
110 if (b[0] == 0 && b[1] == 0)
111 break;
112 res = completeBER_n(b, len, level+1);
113 if (res <= 0)
114 return res;
115 b += res;
116 len -= res;
117 }
118 if (len < 2)
119 return 0;
120 return (b - buf) + 2;
121}
122
123int completeBER(const char *buf, int len)
124{
125 int res = completeBER_n(buf, len, 0);
126#if BER_ANY_DEBUG
127 yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
128#endif
129 if (res < 0)
130 return len;
131 return res;
132}
133/*
134 * Local variables:
135 * c-basic-offset: 4
136 * c-file-style: "Stroustrup"
137 * indent-tabs-mode: nil
138 * End:
139 * vim: shiftwidth=4 tabstop=8 expandtab
140 */
141
int completeBER(const char *buf, int len)
determine whether a buffer is a complete BER buffer
Definition ber_any.c:123
int ber_any(ODR o, Odr_any **p)
Definition ber_any.c:21
int completeBER_n(const char *buf, int len, int level)
Definition ber_any.c:48
int ber_declen(const char *buf, int *len, int max)
Definition ber_len.c:93
int ber_dectag(const char *cp, int *zclass, int *tag, int *constructed, int max)
Decodes BER identifier octets.
Definition ber_tag.c:165
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition log.c:487
#define YLOG_LOG
log level: log (regular)
Definition log.h:48
Internal ODR definitions.
#define odr_max(o)
Definition odr-priv.h:47
void odr_seterror(ODR o, int error, int id)
Definition odr.c:118
#define ODR_DECODE
Definition odr.h:95
#define OOTHER
Definition odr.h:156
#define ODR_ENCODE
Definition odr.h:96
#define OPROTO
Definition odr.h:157
void * odr_malloc(ODR o, size_t size)
Definition odr_mem.c:31
int odr_write(ODR o, const char *buf, int bytes)
Definition odr_mem.c:98
const char * bp
Definition odr-priv.h:85
Definition odr.h:100
Definition odr.h:125
struct Odr_private * op
Definition odr.h:132
int direction
Definition odr.h:126