File indexing completed on 2025-01-05 04:37:09
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef BTBENCODER_H 0007 #define BTBENCODER_H 0008 0009 #include <ktorrent_export.h> 0010 #include <util/file.h> 0011 0012 class QIODevice; 0013 0014 namespace bt 0015 { 0016 class File; 0017 0018 /** 0019 * @author Joris Guisson 0020 * 0021 * Interface for classes which wish to receive the output from a BEncoder. 0022 */ 0023 class KTORRENT_EXPORT BEncoderOutput 0024 { 0025 public: 0026 virtual ~BEncoderOutput() 0027 { 0028 } 0029 /** 0030 * Write a string of characters. 0031 * @param str The string 0032 * @param len The length of the string 0033 */ 0034 virtual void write(const char *str, Uint32 len) = 0; 0035 }; 0036 0037 /** 0038 * Writes the output of a bencoder to a file 0039 */ 0040 class KTORRENT_EXPORT BEncoderFileOutput : public BEncoderOutput 0041 { 0042 File *fptr; 0043 0044 public: 0045 BEncoderFileOutput(File *fptr); 0046 0047 void write(const char *str, Uint32 len) override; 0048 }; 0049 0050 /** 0051 * Write the output of a BEncoder to a QByteArray 0052 */ 0053 class KTORRENT_EXPORT BEncoderBufferOutput : public BEncoderOutput 0054 { 0055 QByteArray &data; 0056 Uint32 ptr; 0057 0058 public: 0059 BEncoderBufferOutput(QByteArray &data); 0060 0061 void write(const char *str, Uint32 len) override; 0062 }; 0063 0064 class KTORRENT_EXPORT BEncoderIODeviceOutput : public BEncoderOutput 0065 { 0066 QIODevice *dev; 0067 0068 public: 0069 BEncoderIODeviceOutput(QIODevice *dev); 0070 0071 void write(const char *str, Uint32 len) override; 0072 }; 0073 0074 /** 0075 * @author Joris Guisson 0076 * @brief Helper class to b-encode stuff. 0077 * 0078 * This class b-encodes data. For more details about b-encoding, see 0079 * the BitTorrent protocol docs. The data gets written to a BEncoderOutput 0080 * thing. 0081 */ 0082 class KTORRENT_EXPORT BEncoder 0083 { 0084 BEncoderOutput *out; 0085 bool del; 0086 0087 public: 0088 /** 0089 * Constructor, output gets written to a file. 0090 * @param fptr The File to write to 0091 */ 0092 BEncoder(File *fptr); 0093 0094 /** 0095 * Constructor, output gets written to a BEncoderOutput object. 0096 * @param out The BEncoderOutput 0097 */ 0098 BEncoder(BEncoderOutput *out); 0099 0100 /** 0101 * Constructor, output gets written to a QIODevice object. 0102 * @param dev The QIODevice 0103 */ 0104 BEncoder(QIODevice *dev); 0105 0106 virtual ~BEncoder(); 0107 0108 /** 0109 * Begin a dictionary.Should have a corresponding end call. 0110 */ 0111 void beginDict(); 0112 0113 /** 0114 * Begin a list. Should have a corresponding end call. 0115 */ 0116 void beginList(); 0117 0118 template<class T> void write(const QByteArray &key, T val) 0119 { 0120 write(key); 0121 write(val); 0122 } 0123 0124 /** 0125 * Write a boolean (is encoded as an intà 0126 * @param val 0127 */ 0128 void write(bool val); 0129 0130 /** 0131 * Write a float 0132 * @param val 0133 */ 0134 void write(float val); 0135 0136 /** 0137 * Write an int 0138 * @param val 0139 */ 0140 void write(Uint32 val); 0141 0142 /** 0143 * Write an int64 0144 * @param val 0145 */ 0146 void write(Uint64 val); 0147 0148 private: 0149 /** 0150 * Write a string 0151 * @param str 0152 */ 0153 void write(const char *str); 0154 0155 public: 0156 /** 0157 * Write a QByteArray 0158 * @param data 0159 */ 0160 void write(const QByteArray &data); 0161 0162 /** 0163 * Write a data array 0164 * @param data 0165 * @param size of data 0166 */ 0167 void write(const Uint8 *data, Uint32 size); 0168 0169 /** 0170 * End a beginDict or beginList call. 0171 */ 0172 void end(); 0173 }; 0174 0175 } 0176 0177 #endif