YAZ 5.38.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
13
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_n(o->op->bp, odr_max(o), 0)) <= 0)
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 -1;
59 }
60 if (len < 2)
61 return 0;
62 if (buf[0] == 0 && buf[1] == 0)
63 return -1;
64 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
65 return 0;
66 b += res;
67 len -= res;
68 if (len < 0)
69 return -1;
70 res = ber_declen(b, &ll, len);
71 if (res == -2)
72 {
73#if BER_ANY_DEBUG
74 yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
75#endif
76 return -1; /* error */
77 }
78 if (res == -1)
79 {
80#if BER_ANY_DEBUG
81 yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
82#endif
83 return 0; /* incomplete length */
84 }
85 b += res;
86 len -= res;
87 if (ll >= 0)
88 { /* definite length */
89 if (len < ll)
90 {
91#if BER_ANY_DEBUG
92 yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
93 level, len, ll);
94#endif
95 return 0;
96 }
97 return ll + (b-buf);
98 }
99 /* indefinite length */
100 if (!cons)
101 { /* if primitive, it's an error */
102#if BER_ANY_DEBUG
103 yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
104 level, ll, len, res);
105#endif
106 return -1; /* error */
107 }
108 /* constructed - cycle through children */
109 while (len >= 2)
110 {
111 if (b[0] == 0 && b[1] == 0)
112 break;
113 res = completeBER_n(b, len, level+1);
114 if (res <= 0)
115 return res;
116 b += res;
117 len -= res;
118 }
119 if (len < 2)
120 return 0;
121 return (b - buf) + 2;
122}
123
124int completeBER(const char *buf, int len)
125{
126 int res = completeBER_n(buf, len, 0);
127#if BER_ANY_DEBUG
128 yaz_log(YLOG_LOG, "completeBER len=%d res=%d", len, res);
129#endif
130 if (res < 0)
131 return len;
132 return res;
133}
134/*
135 * Local variables:
136 * c-basic-offset: 4
137 * c-file-style: "Stroustrup"
138 * indent-tabs-mode: nil
139 * End:
140 * vim: shiftwidth=4 tabstop=8 expandtab
141 */
142
int completeBER(const char *buf, int len)
determine whether a buffer is a complete BER buffer
Definition ber_any.c:124
int ber_any(ODR o, Odr_any **p)
Definition ber_any.c:21
int completeBER_n(const char *buf, int len, int level)
determine whether a buffer is a complete BER buffer
Definition ber_any.c:48
int ber_declen(const char *buf, int *len, int max)
Definition ber_len.c:94
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
Odr_oct Odr_any
Definition odr.h:108
struct odr * ODR
Definition odr.h:121
#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
struct Odr_private * op
Definition odr.h:132
int direction
Definition odr.h:126