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 #pragma once
0009 
0010 #include <QByteArray>
0011 
0012 #include "kldap_core_export.h"
0013 #include <memory>
0014 // clazy:excludeall=copyable-polymorphic
0015 
0016 namespace KLDAPCore
0017 {
0018 /**
0019  * This class allows encoding and decoding Qt structures using Basic
0020  * Encoding Rules.
0021  */
0022 class KLDAP_CORE_EXPORT Ber
0023 {
0024 public:
0025     /**
0026      * Constructs a Ber object.
0027      */
0028     Ber();
0029     /**
0030      * Constructs a Ber object from the value.
0031      */
0032     explicit Ber(const QByteArray &value);
0033     /**
0034      * Destroys the Ber object.
0035      */
0036     ~Ber();
0037 
0038     Ber(const Ber &that);
0039     Ber &operator=(const Ber &that);
0040 
0041     /**
0042      * Returns the Ber object as a flat QByteArray.
0043      */
0044     [[nodiscard]] QByteArray flatten() const;
0045 
0046     /**
0047      * Appends the data with the specified format to the Ber object.
0048      * This function works like printf, except that it's appending the
0049      * parameters, not replacing them. The allowed format characters and
0050      * the expected parameter types are:
0051      * <ul>
0052      *   <li>
0053      *     b  Boolean.  An int parameter should be supplied.
0054      *     A boolean element is output.
0055      *   </li>
0056      *   <li>
0057      *     e  Enumeration.  An int parameter should be supplied.
0058      *     An  enumeration  element is output.
0059      *   </li>
0060      *   <li>
0061      *     i  Integer.   An int parameter should be supplied.
0062      *     An integer element is output.
0063      *   </li>
0064      *   <li>
0065      *     B  Bitstring.  A pointer to a QByteArray which contains the
0066      *     bitstring is supplied, followed by the number of bits in the
0067      *     bitstring.  A bitstring element is output.
0068      *   </li>
0069      *   <li>
0070      *     n  Null.  No parameter is required. A null element is output.
0071      *   </li>
0072      *   <li>
0073      *     O,o,s  Octet  string.  A QByteArray * is supplied.
0074      *     An octet string element is output.
0075      *         Due to versatility of Qt's QByteArray, these three format
0076      *         strings are all accepts the same parameter, but using the 's'
0077      *         format the string will be encoded only to the first zero
0078      *         character (a null terminated string)!
0079      *   </li>
0080      *   <li>
0081      *     t  Tag.  An int specifying the tag to give the next element
0082      *     is provided. This works across calls.
0083      *   </li>
0084      *   <li>
0085      *     v,V  Several octet strings. A QList<QByteArray>* is supplied.
0086      *     Note that a construct like ’{v}’ is required to get an actual
0087      *     SEQUENCE OF octet strings. Also note that the 'v' format recognizes
0088      *     the QByteArray only to the first zero character, so it's not
0089      *     appropriate for binary data, just only for null terminated strings!
0090      *   </li>
0091      *   <li>
0092      *     {  Begin sequence. No parameter is required.
0093      *   </li>
0094      *   <li>
0095      *     }  End sequence.  No parameter is required.
0096      *   </li>
0097      *   <li>
0098      *     [  Begin set.  No parameter is required.
0099      *   </li>
0100      *   <li>
0101      *     ]  End set.  No parameter is required.
0102      *   </li>
0103      * </ul>
0104      */
0105     int printf(QString format, ...); // Passing by-value since it's used by va_start
0106     int scanf(QString format, ...);
0107     unsigned int peekTag(int &size);
0108     unsigned int skipTag(int &size);
0109 
0110 private:
0111     class BerPrivate;
0112     std::unique_ptr<BerPrivate> const d;
0113 };
0114 }