File indexing completed on 2024-12-01 12:30:47

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 #include "kcodecsidentity.h"
0020 
0021 #include <QDebug>
0022 
0023 #include <QByteArray>
0024 
0025 #include <cstring>
0026 
0027 using namespace KCodecs;
0028 
0029 class IdentityEnDecoder : public Encoder, public Decoder
0030 {
0031 protected:
0032     friend class KCodecs::IdentityCodec;
0033     explicit IdentityEnDecoder(Codec::NewlineType newline)
0034         : Encoder(Codec::NewlineLF)
0035     {
0036         if (newline == Codec::NewlineCRLF) {
0037             qWarning() << "IdentityEnDecoder: CRLF isn't yet supported!";
0038         }
0039     }
0040 
0041 public:
0042     ~IdentityEnDecoder() override
0043     {
0044     }
0045 
0046     bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) override
0047     {
0048         return decode(scursor, send, dcursor, dend);
0049     }
0050 
0051     bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) override;
0052 
0053     bool finish(char *&dcursor, const char *const dend) override
0054     {
0055         Q_UNUSED(dcursor);
0056         Q_UNUSED(dend);
0057         return true;
0058     }
0059 };
0060 
0061 Encoder *IdentityCodec::makeEncoder(Codec::NewlineType newline) const
0062 {
0063     return new IdentityEnDecoder(newline);
0064 }
0065 
0066 Decoder *IdentityCodec::makeDecoder(Codec::NewlineType newline) const
0067 {
0068     return new IdentityEnDecoder(newline);
0069 }
0070 
0071 /********************************************************/
0072 /********************************************************/
0073 /********************************************************/
0074 
0075 bool IdentityEnDecoder::decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend)
0076 {
0077     const int size = qMin(send - scursor, dcursor - dend);
0078     if (size > 0) {
0079         std::memmove(dcursor, scursor, size);
0080         dcursor += size;
0081         scursor += size;
0082     }
0083     return scursor == send;
0084 }
0085 
0086 QByteArray IdentityCodec::encode(const QByteArray &src, Codec::NewlineType newline) const
0087 {
0088     if (newline == Codec::NewlineCRLF) {
0089         qWarning() << "IdentityCodec::encode(): CRLF not yet supported!";
0090     }
0091     return src;
0092 }
0093 
0094 QByteArray IdentityCodec::decode(const QByteArray &src, Codec::NewlineType newline) const
0095 {
0096     if (newline == Codec::NewlineCRLF) {
0097         qWarning() << "IdentityCodec::decode(): CRLF not yet supported!";
0098     }
0099     return src;
0100 }