KCodecs 5.109.0
kcodecs.h
1/*
2 SPDX-FileCopyrightText: 2000-2001 Dawit Alemayehu <adawit@kde.org>
3 SPDX-FileCopyrightText: 2001 Rik Hemsley (rikkus) <rik@kde.org>
4 SPDX-FileCopyrightText: 2001-2002 Marc Mutz <mutz@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7
8 The quoted-printable codec as described in RFC 2045, section 6.7. is by
9 Rik Hemsley (C) 2001.
10*/
11
12#ifndef KCODECS_H
13#define KCODECS_H
14
15#include <kcodecs_export.h>
16
17#include <QString>
18
19#include <memory>
20
21#if KCODECS_ENABLE_DEPRECATED_SINCE(5, 79)
22// KBase64 methods were moved to KCodecs for kdelibs 3.1
23#define KBase64 KCodecs
24#else
25#define KBase64 KBase64_is_deprecated_use_KCodecs
26#endif
27
28template<typename T, typename U>
29class QHash;
30
31class QByteArray;
32class QIODevice;
33
64namespace KCodecs
65{
75KCODECS_EXPORT QByteArray quotedPrintableEncode(const QByteArray &in, bool useCRLF = true);
76
95KCODECS_EXPORT void quotedPrintableEncode(const QByteArray &in, QByteArray &out, bool useCRLF);
96
106KCODECS_EXPORT QByteArray quotedPrintableDecode(const QByteArray &in);
107
125KCODECS_EXPORT void quotedPrintableDecode(const QByteArray &in, QByteArray &out);
126
127#if KCODECS_ENABLE_DEPRECATED_SINCE(5, 56)
140KCODECS_EXPORT
141KCODECS_DEPRECATED_VERSION(5, 56, "Not implemented")
142QByteArray uuencode(const QByteArray &in);
143#endif
144
145#if KCODECS_ENABLE_DEPRECATED_SINCE(5, 56)
162KCODECS_EXPORT
163KCODECS_DEPRECATED_VERSION(5, 56, "Not implemented")
164void uuencode(const QByteArray &in, QByteArray &out);
165#endif
166
177KCODECS_EXPORT QByteArray uudecode(const QByteArray &in);
178
198KCODECS_EXPORT void uudecode(const QByteArray &in, QByteArray &out);
199
214KCODECS_EXPORT QByteArray base64Encode(const QByteArray &in);
215
216#if KCODECS_ENABLE_DEPRECATED_SINCE(5, 5)
222KCODECS_EXPORT
223KCODECS_DEPRECATED_VERSION(5, 5, "Use QByteArray base64Encode(const QByteArray &)")
224QByteArray base64Encode(const QByteArray &in, bool insertLFs);
225#endif
226
248KCODECS_EXPORT void base64Encode(const QByteArray &in, QByteArray &out, bool insertLFs = false);
249
257KCODECS_EXPORT QByteArray base64Decode(const QByteArray &in);
258
276KCODECS_EXPORT void base64Decode(const QByteArray &in, QByteArray &out);
277
285KCODECS_EXPORT QString decodeRFC2047String(const QString &text);
286
292 NoOption = 0,
294};
295
311KCODECS_EXPORT QString decodeRFC2047String(const QByteArray &src,
312 QByteArray *usedCS,
313 const QByteArray &defaultCS = QByteArray(),
314 CharsetOption option = NoOption);
315
330KCODECS_EXPORT QByteArray encodeRFC2047String(const QString &src, const QByteArray &charset);
331
341KCODECS_EXPORT QByteArray base45Decode(const QByteArray &in);
342
343class Encoder;
344class EncoderPrivate;
345class Decoder;
346class DecoderPrivate;
347
387class KCODECS_EXPORT Codec
388{
389public:
390 enum NewlineType {
391 NewlineLF,
392 NewlineCRLF,
393 };
394
400 static Codec *codecForName(const char *name);
401
407 static Codec *codecForName(const QByteArray &name);
408
417 virtual int maxEncodedSizeFor(int insize, NewlineType newline = NewlineLF) const = 0;
418
427 virtual int maxDecodedSizeFor(int insize, NewlineType newline = NewlineLF) const = 0;
428
436 virtual Encoder *makeEncoder(NewlineType newline = NewlineLF) const = 0;
437
445 virtual Decoder *makeDecoder(NewlineType newline = NewlineLF) const = 0;
446
483 virtual bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend, NewlineType newline = NewlineLF) const;
484
521 virtual bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend, NewlineType newline = NewlineLF) const;
522
533 virtual QByteArray encode(const QByteArray &src, NewlineType newline = NewlineLF) const;
534
545 virtual QByteArray decode(const QByteArray &src, NewlineType newline = NewlineLF) const;
546
550 virtual const char *name() const = 0;
551
555 virtual ~Codec()
556 {
557 }
558
559protected:
564 {
565 }
566};
567
650class KCODECS_EXPORT Decoder
651{
652protected:
653 friend class Codec;
654 friend class DecoderPrivate;
655
662 Decoder(Codec::NewlineType newline = Codec::NewlineLF);
663
664public:
668 virtual ~Decoder();
669
679 virtual bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) = 0;
680
689 virtual bool finish(char *&dcursor, const char *const dend) = 0;
690
691protected:
692 //@cond PRIVATE
693 std::unique_ptr<DecoderPrivate> const d;
694 //@endcond
695};
696
706class KCODECS_EXPORT Encoder
707{
708protected:
709 friend class Codec;
710 friend class EncoderPrivate;
711
717 explicit Encoder(Codec::NewlineType newline = Codec::NewlineLF);
718
719public:
723 virtual ~Encoder();
724
734 virtual bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) = 0;
735
743 virtual bool finish(char *&dcursor, const char *const dend) = 0;
744
745protected:
749 enum {
750 maxBufferedChars = 8,
751 };
752
763 bool write(char ch, char *&dcursor, const char *const dend);
764
775 bool flushOutputBuffer(char *&dcursor, const char *const dend);
776
784 bool writeCRLF(char *&dcursor, const char *const dend);
785
786protected:
787 //@cond PRIVATE
788 std::unique_ptr<EncoderPrivate> const d;
789 //@endcond
790};
791
792} // namespace KCodecs
793
794#endif // KCODECS_H
An abstract base class of codecs for common mail transfer encodings.
Definition kcodecs.h:388
virtual bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend, NewlineType newline=NewlineLF) const
Convenience wrapper that can be used for small chunks of data when you can provide a large enough buf...
static Codec * codecForName(const char *name)
Returns a codec associated with the specified name.
virtual Decoder * makeDecoder(NewlineType newline=NewlineLF) const =0
Creates the decoder for the codec.
virtual int maxDecodedSizeFor(int insize, NewlineType newline=NewlineLF) const =0
Computes the maximum size, in characters, needed for the deccoding.
virtual const char * name() const =0
Returns the name of the encoding.
Codec()
Constructs the codec.
Definition kcodecs.h:563
static Codec * codecForName(const QByteArray &name)
Returns a codec associated with the specified name.
virtual Encoder * makeEncoder(NewlineType newline=NewlineLF) const =0
Creates the encoder for the codec.
virtual QByteArray decode(const QByteArray &src, NewlineType newline=NewlineLF) const
Even more convenient, but also a bit slower and more memory intensive, since it allocates storage for...
virtual int maxEncodedSizeFor(int insize, NewlineType newline=NewlineLF) const =0
Computes the maximum size, in characters, needed for the encoding.
virtual QByteArray encode(const QByteArray &src, NewlineType newline=NewlineLF) const
Even more convenient, but also a bit slower and more memory intensive, since it allocates storage for...
virtual ~Codec()
Destroys the codec.
Definition kcodecs.h:555
virtual bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend, NewlineType newline=NewlineLF) const
Convenience wrapper that can be used for small chunks of data when you can provide a large enough buf...
Stateful CTE decoder class.
Definition kcodecs.h:651
Decoder(Codec::NewlineType newline=Codec::NewlineLF)
Protected constructor.
virtual ~Decoder()
Destroys the decoder.
virtual bool finish(char *&dcursor, const char *const dend)=0
Call this method to finalize the output stream.
virtual bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend)=0
Decodes a chunk of data, maintaining state information between calls.
Stateful encoder class.
Definition kcodecs.h:707
virtual ~Encoder()
Destroys the encoder.
bool write(char ch, char *&dcursor, const char *const dend)
Writes character ch to the output stream or the output buffer, depending on whether or not the output...
bool flushOutputBuffer(char *&dcursor, const char *const dend)
Writes characters from the output buffer to the output stream.
Encoder(Codec::NewlineType newline=Codec::NewlineLF)
Protected constructor.
virtual bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend)=0
Encodes a chunk of data, maintaining state information between calls.
bool writeCRLF(char *&dcursor, const char *const dend)
Convenience function.
virtual bool finish(char *&dcursor, const char *const dend)=0
Call this method to finalize the output stream.
A wrapper class for the most commonly used encoding and decoding algorithms.
Definition kcharsets.h:27
QByteArray base64Decode(const QByteArray &in)
Decodes the given data that was encoded using the base64 algorithm.
QString decodeRFC2047String(const QString &text)
Decodes string text according to RFC2047, i.e., the construct =?charset?[qb]?encoded?...
CharsetOption
Charset options for RFC2047 encoder.
Definition kcodecs.h:291
@ ForceDefaultCharset
No special option.
Definition kcodecs.h:293
QByteArray base45Decode(const QByteArray &in)
Decodes the given data that was encoded using the base45 codec.
QByteArray quotedPrintableDecode(const QByteArray &in)
Decodes a quoted-printable encoded data.
QByteArray quotedPrintableEncode(const QByteArray &in, bool useCRLF=true)
Encodes the given data using the quoted-printable algorithm.
QByteArray uudecode(const QByteArray &in)
Decodes the given data using the uudecode algorithm.
QByteArray base64Encode(const QByteArray &in)
Encodes the given data using the base64 algorithm.
QByteArray encodeRFC2047String(const QString &src, const QByteArray &charset)
Encodes string src according to RFC2047 using charset charset.