File indexing completed on 2024-04-21 14:53:57

0001 /*  -*- c++ -*-
0002     SPDX-FileCopyrightText: 2004 Marc Mutz <mutz@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 /**
0007   @file
0008   This file is part of the API for handling @ref MIME data and
0009   defines the Identity, @ref seven-bit-text, @ref eight-bit-text,
0010   and @ref eight-bit-binary @ref Codec classes.
0011 
0012   @brief
0013   Defines the classes IdentityCodec, SevenBitCodec, EightBitCodec,
0014   and BinaryCodec.
0015 
0016   @authors Marc Mutz \<mutz@kde.org\>
0017 */
0018 
0019 #ifndef KCODECS_IDENTITY_H
0020 #define KCODECS_IDENTITY_H
0021 
0022 #include "kcodecs.h"
0023 
0024 class QByteArray;
0025 
0026 namespace KCodecs
0027 {
0028 /**
0029   @brief
0030   A class representing the Identify @ref codec.
0031 */
0032 class IdentityCodec : public Codec
0033 {
0034 public:
0035     /**
0036       Constructs the Identity codec.
0037     */
0038     IdentityCodec()
0039         : Codec()
0040     {
0041     }
0042 
0043     /**
0044       Destroys the codec.
0045     */
0046     ~IdentityCodec() override
0047     {
0048     }
0049 
0050     using Codec::decode;
0051     using Codec::encode;
0052 
0053     /**
0054       @copydoc
0055       QByteArray Codec::encode()
0056     */
0057     QByteArray encode(const QByteArray &src, Codec::NewlineType newline = Codec::NewlineLF) const override;
0058 
0059     /**
0060       @copydoc
0061       QByteArray Codec::decode()
0062     */
0063     QByteArray decode(const QByteArray &src, Codec::NewlineType newline = Codec::NewlineLF) const override;
0064 
0065     /**
0066       @copydoc
0067       Codec::maxEncodedSizeFor()
0068     */
0069     int maxEncodedSizeFor(int insize, Codec::NewlineType newline = Codec::NewlineLF) const override
0070     {
0071         if (newline == Codec::NewlineCRLF) {
0072             return 2 * insize;
0073         } else {
0074             return insize;
0075         }
0076     }
0077 
0078     /**
0079       @copydoc
0080       Codec::maxDecodedSizeFor()
0081     */
0082     int maxDecodedSizeFor(int insize, Codec::NewlineType newline) const override
0083     {
0084         if (newline == Codec::NewlineCRLF) {
0085             return 2 * insize;
0086         } else {
0087             return insize;
0088         }
0089     }
0090 
0091     /**
0092       @copydoc
0093       Codec::makeEncoder()
0094     */
0095     Encoder *makeEncoder(Codec::NewlineType newline = Codec::NewlineCRLF) const override;
0096 
0097     /**
0098       @copydoc
0099       Codec::makeDecoder()
0100     */
0101     Decoder *makeDecoder(Codec::NewlineType newline = Codec::NewlineCRLF) const override;
0102 };
0103 
0104 /**
0105   @brief
0106   A class representing the @ref codec for @ref seven-bit-text.
0107 */
0108 class SevenBitCodec : public IdentityCodec
0109 {
0110 public:
0111     /**
0112       Constructs the 7-bit codec.
0113     */
0114     SevenBitCodec()
0115         : IdentityCodec()
0116     {
0117     }
0118 
0119     /**
0120       Destroys the codec.
0121     */
0122     ~SevenBitCodec() override
0123     {
0124     }
0125 
0126     /**
0127       @copydoc
0128       Codec::name()
0129     */
0130     const char *name() const override
0131     {
0132         return "7bit";
0133     }
0134 };
0135 
0136 /**
0137   @brief
0138   A class representing the @ref codec for @ref eight-bit-text.
0139 */
0140 class EightBitCodec : public IdentityCodec
0141 {
0142 public:
0143     /**
0144       Constructs the 8-bit codec.
0145     */
0146     EightBitCodec()
0147         : IdentityCodec()
0148     {
0149     }
0150 
0151     /**
0152       Destroys the codec.
0153     */
0154     ~EightBitCodec() override
0155     {
0156     }
0157 
0158     /**
0159       @copydoc
0160       Codec::name()
0161     */
0162     const char *name() const override
0163     {
0164         return "8bit";
0165     }
0166 };
0167 
0168 /**
0169   @brief
0170   A class representing the @ref codec for @ref eight-bit-binary.
0171 */
0172 class BinaryCodec : public IdentityCodec
0173 {
0174 public:
0175     /**
0176       Constructs the 8-bit-binary codec.
0177     */
0178     BinaryCodec()
0179         : IdentityCodec()
0180     {
0181     }
0182 
0183     /**
0184       Destroys the codec.
0185     */
0186     ~BinaryCodec() override
0187     {
0188     }
0189 
0190     /**
0191       @copydoc
0192       Codec::name()
0193     */
0194     const char *name() const override
0195     {
0196         return "binary";
0197     }
0198 
0199     /**
0200       @copydoc
0201       Codec::maxEncodedSizeFor()
0202     */
0203     int maxEncodedSizeFor(int insize, Codec::NewlineType newline = Codec::NewlineLF) const override
0204     {
0205         Q_UNUSED(newline);
0206         return insize;
0207     }
0208 
0209     /**
0210       @copydoc
0211       Codec::maxDecodedSizeFor()
0212     */
0213     int maxDecodedSizeFor(int insize, Codec::NewlineType newline = Codec::NewlineLF) const override
0214     {
0215         Q_UNUSED(newline);
0216         return insize;
0217     }
0218 };
0219 
0220 } // namespace KCodecs
0221 
0222 #endif // KCODECS_IDENTITY_H