9#include "kldap_config.h"
11#include "ldap_core_debug.h"
14#include <QVarLengthArray>
28#include "w32-ldap-help.h"
37#define ber_memfree(x) ldap_memfree(x)
39#define ber_memfree(x) win_ldap_memfree(x)
45using namespace KLDAPCore;
47class Q_DECL_HIDDEN
Ber::BerPrivate
51 BerElement *mBer =
nullptr;
59 d->mBer = ber_alloc_t(LBER_USE_DER);
67 bv.bv_val = (
char *)value.
data();
68 bv.bv_len = value.
size();
69 d->mBer = ber_init(&bv);
82 if (ber_flatten(that.d->mBer, &bv) == 0) {
83 d->mBer = ber_init(bv);
88Ber &Ber::operator=(
const Ber &that)
95 if (ber_flatten(that.d->mBer, &bv) == 0) {
96 d->mBer = ber_init(bv);
106 if (ber_flatten(d->mBer, &bv) == 0) {
107 ret = QByteArray(bv->bv_val, bv->bv_len);
117 va_start(args, format);
122 while (i < format.
length()) {
129 ber_int_t v = va_arg(args,
int);
130 ret = ber_printf(d->mBer, fmt, v);
135 QByteArray *B = va_arg(args, QByteArray *);
136 int Bc = va_arg(args,
int);
137 ret = ber_printf(d->mBer, fmt, B->
data(), Bc);
141 QByteArray *o = va_arg(args, QByteArray *);
142 ret = ber_printf(d->mBer, fmt, o->
data(), o->
size());
146 QByteArray *O = va_arg(args, QByteArray *);
148 bv.bv_val = (
char *)O->
data();
149 bv.bv_len = O->
size();
150 ret = ber_printf(d->mBer, fmt, &bv);
154 QByteArray *s = va_arg(args, QByteArray *);
155 ret = ber_printf(d->mBer, fmt, s->
data());
160 unsigned int t = va_arg(args,
unsigned int);
161 ret = ber_printf(d->mBer, fmt, t);
165 QList<QByteArray> *v = va_arg(args, QList<QByteArray> *);
166 QVarLengthArray<const char *> l(v->
count() + 1);
168 for (j = 0; j < v->
count(); j++) {
169 l[j] = v->
at(j).data();
172 ret = ber_printf(d->mBer, fmt, l.data());
176 QList<QByteArray> *V = va_arg(args, QList<QByteArray> *);
177 QVarLengthArray<struct berval *> bv(V->
count() + 1);
178 QVarLengthArray<struct berval> bvs(V->
count());
180 for (j = 0; j < V->
count(); j++) {
181 bvs[j].bv_val = (
char *)V->
at(j).data();
182 bvs[j].bv_len = V->
at(j).size();
185 bv[V->
count()] =
nullptr;
186 ret = ber_printf(d->mBer, fmt, bv.data());
194 ret = ber_printf(d->mBer, fmt);
197 qCWarning(LDAP_CORE_LOG) <<
"Invalid BER format parameter: '" << fmt <<
"'";
200 qCDebug(LDAP_CORE_LOG) <<
"ber_printf format:" << fmt <<
"ret:" << ret;
209int Ber::scanf(QString format, ...)
213 va_start(args, format);
218 while (i < format.
length()) {
226 int *v = va_arg(args,
int *);
227 ret = ber_scanf(d->mBer, fmt, v);
232 QByteArray *B = va_arg(args, QByteArray *);
233 int *Bc = va_arg(args,
int *);
235 ret = ber_scanf(d->mBer, fmt, &c, Bc);
237 *B = QByteArray(c, (*Bc + 7) / 8);
243 QByteArray *o = va_arg(args, QByteArray *);
245 ret = ber_scanf(d->mBer, fmt, &bv);
247 *o = QByteArray(bv.bv_val, bv.bv_len);
248 ber_memfree(bv.bv_val);
253 QByteArray *O = va_arg(args, QByteArray *);
255 ret = ber_scanf(d->mBer, fmt, &bv);
257 *O = QByteArray(bv->bv_val, bv->bv_len);
263 QByteArray *m = va_arg(args, QByteArray *);
265 ret = ber_scanf(d->mBer, fmt, &bv);
267 *m = QByteArray(bv->bv_val, bv->bv_len);
272 QByteArray *a = va_arg(args, QByteArray *);
274 ret = ber_scanf(d->mBer, fmt, &c);
283 QByteArray *s = va_arg(args, QByteArray *);
285 ber_len_t l =
sizeof(buf);
286 ret = ber_scanf(d->mBer, fmt, &buf, &l);
288 *s = QByteArray(buf, l);
294 unsigned int *t = va_arg(args,
unsigned int *);
295 ret = ber_scanf(d->mBer, fmt, t);
299 QList<QByteArray> *v = va_arg(args, QList<QByteArray> *);
302 ret = ber_scanf(d->mBer, fmt, &c);
303 if (ret != -1 && c) {
306 v->
append(QByteArray(*c));
310 ber_memfree((
char *)c2);
315 QList<QByteArray> *v = va_arg(args, QList<QByteArray> *);
318 ret = ber_scanf(d->mBer, fmt, &bv);
319 if (ret != -1 && bv) {
322 v->
append(QByteArray((*bv)->bv_val, (*bv)->bv_len));
335 ret = ber_scanf(d->mBer, fmt);
338 qCWarning(LDAP_CORE_LOG) <<
"Invalid BER format parameter: '" << fmt <<
"'";
342 qCDebug(LDAP_CORE_LOG) <<
"ber_scanf format:" << fmt <<
"ret:" << ret;
351unsigned int Ber::peekTag(
int &size)
355 ret = ber_peek_tag(d->mBer, &len);
360unsigned int Ber::skipTag(
int &size)
364 ret = ber_skip_tag(d->mBer, &len);
374 qCritical() <<
"LDAP support not compiled";
380 qCritical() <<
"LDAP support not compiled";
390 qCritical() <<
"LDAP support not compiled";
393Ber &Ber::operator=(
const Ber &that)
398 qCritical() <<
"LDAP support not compiled";
404 qCritical() <<
"LDAP support not compiled";
411 qCritical() <<
"LDAP support not compiled";
415int Ber::scanf(
QString format, ...)
418 qCritical() <<
"LDAP support not compiled";
422unsigned int Ber::peekTag(
int &size)
425 qCritical() <<
"LDAP support not compiled";
429unsigned int Ber::skipTag(
int &size)
432 qCritical() <<
"LDAP support not compiled";
This class allows encoding and decoding Qt structures using Basic Encoding Rules.
QByteArray flatten() const
Returns the Ber object as a flat QByteArray.
int printf(QString format,...)
Appends the data with the specified format to the Ber object.
Ber()
Constructs a Ber object.
~Ber()
Destroys the Ber object.
qsizetype size() const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
qsizetype length() const const
QByteArray toLatin1() const const