File indexing completed on 2024-12-15 04:50:15

0001 /*
0002   This file is part of libkldap.
0003   SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "ldapcontrol.h"
0009 #include "ber.h"
0010 
0011 #include <QSharedData>
0012 
0013 using namespace KLDAPCore;
0014 
0015 class LdapControlPrivate : public QSharedData
0016 {
0017 public:
0018     LdapControlPrivate() = default;
0019 
0020     LdapControlPrivate(const LdapControlPrivate &other) = default;
0021 
0022     QString mOid;
0023     QByteArray mValue;
0024     bool mCritical = false;
0025 };
0026 
0027 LdapControl::LdapControl()
0028     : d(new LdapControlPrivate)
0029 {
0030     setControl(QString(), QByteArray(), false);
0031 }
0032 
0033 LdapControl::LdapControl(const QString &oid, const QByteArray &value, bool critical)
0034     : d(new LdapControlPrivate)
0035 {
0036     setControl(oid, value, critical);
0037 }
0038 
0039 LdapControl::LdapControl(const LdapControl &that)
0040     : d(that.d)
0041 {
0042     setControl(that.d->mOid, that.d->mValue, that.d->mCritical);
0043 }
0044 
0045 LdapControl &LdapControl::operator=(const LdapControl &that)
0046 {
0047     if (this != &that) {
0048         d = that.d;
0049     }
0050 
0051     setControl(that.d->mOid, that.d->mValue, that.d->mCritical);
0052 
0053     return *this;
0054 }
0055 
0056 LdapControl::~LdapControl() = default;
0057 
0058 void LdapControl::setControl(const QString &oid, const QByteArray &value, bool critical)
0059 {
0060     d->mOid = oid;
0061     d->mValue = value;
0062     d->mCritical = critical;
0063 }
0064 
0065 QString LdapControl::oid() const
0066 {
0067     return d->mOid;
0068 }
0069 
0070 QByteArray LdapControl::value() const
0071 {
0072     return d->mValue;
0073 }
0074 
0075 bool LdapControl::critical() const
0076 {
0077     return d->mCritical;
0078 }
0079 
0080 void LdapControl::setOid(const QString &oid)
0081 {
0082     d->mOid = oid;
0083 }
0084 
0085 void LdapControl::setValue(const QByteArray &value)
0086 {
0087     d->mValue = value;
0088 }
0089 
0090 void LdapControl::setCritical(bool critical)
0091 {
0092     d->mCritical = critical;
0093 }
0094 
0095 int LdapControl::parsePageControl(QByteArray &cookie) const
0096 {
0097     if (d->mOid != QLatin1StringView("1.2.840.113556.1.4.319")) {
0098         return -1;
0099     }
0100 
0101     Ber ber(d->mValue);
0102     int size;
0103     if (ber.scanf(QStringLiteral("{iO}"), &size, &cookie) == -1) {
0104         return -1;
0105     } else {
0106         return size;
0107     }
0108 }
0109 
0110 LdapControl LdapControl::createPageControl(int pagesize, const QByteArray &cookie)
0111 {
0112     LdapControl control;
0113     Ber ber;
0114 
0115     ber.printf(QStringLiteral("{iO}"), pagesize, &cookie);
0116     control.setOid(QStringLiteral("1.2.840.113556.1.4.319"));
0117     control.setValue(ber.flatten());
0118     return control;
0119 }
0120 
0121 void LdapControl::insert(LdapControls &list, const LdapControl &ctrl)
0122 {
0123     LdapControls::iterator it;
0124     LdapControls::iterator endit = list.end();
0125     const QString oid = ctrl.oid();
0126 
0127     for (it = list.begin(); it != endit; ++it) {
0128         if (it->oid() == oid) {
0129             *it = ctrl;
0130             return;
0131         }
0132     }
0133     list.append(ctrl);
0134 }