File indexing completed on 2025-01-05 04:37:30

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTFUNCTIONS_H
0007 #define BTFUNCTIONS_H
0008 
0009 #include "constants.h"
0010 #include <QString>
0011 #include <ktorrent_export.h>
0012 
0013 namespace bt
0014 {
0015 struct TorrentStats;
0016 
0017 KTORRENT_EXPORT double Percentage(const TorrentStats &s);
0018 
0019 inline void WriteUint64(Uint8 *buf, Uint32 off, Uint64 val)
0020 {
0021     buf[off + 0] = (Uint8)((val & 0xFF00000000000000ULL) >> 56);
0022     buf[off + 1] = (Uint8)((val & 0x00FF000000000000ULL) >> 48);
0023     buf[off + 2] = (Uint8)((val & 0x0000FF0000000000ULL) >> 40);
0024     buf[off + 3] = (Uint8)((val & 0x000000FF00000000ULL) >> 32);
0025     buf[off + 4] = (Uint8)((val & 0x00000000FF000000ULL) >> 24);
0026     buf[off + 5] = (Uint8)((val & 0x0000000000FF0000ULL) >> 16);
0027     buf[off + 6] = (Uint8)((val & 0x000000000000FF00ULL) >> 8);
0028     buf[off + 7] = (Uint8)((val & 0x00000000000000FFULL) >> 0);
0029 }
0030 
0031 inline Uint64 ReadUint64(const Uint8 *buf, Uint64 off)
0032 {
0033     Uint64 tmp = ((Uint64)buf[off] << 56) | ((Uint64)buf[off + 1] << 48) | ((Uint64)buf[off + 2] << 40) | ((Uint64)buf[off + 3] << 32)
0034         | ((Uint64)buf[off + 4] << 24) | ((Uint64)buf[off + 5] << 16) | ((Uint64)buf[off + 6] << 8) | ((Uint64)buf[off + 7] << 0);
0035 
0036     return tmp;
0037 }
0038 
0039 inline void WriteUint32(Uint8 *buf, Uint32 off, Uint32 val)
0040 {
0041     buf[off + 0] = (Uint8)((val & 0xFF000000) >> 24);
0042     buf[off + 1] = (Uint8)((val & 0x00FF0000) >> 16);
0043     buf[off + 2] = (Uint8)((val & 0x0000FF00) >> 8);
0044     buf[off + 3] = (Uint8)(val & 0x000000FF);
0045 }
0046 
0047 inline Uint32 ReadUint32(const Uint8 *buf, Uint32 off)
0048 {
0049     return (buf[off] << 24) | (buf[off + 1] << 16) | (buf[off + 2] << 8) | buf[off + 3];
0050 }
0051 
0052 inline void WriteUint16(Uint8 *buf, Uint32 off, Uint16 val)
0053 {
0054     buf[off + 0] = (Uint8)((val & 0xFF00) >> 8);
0055     buf[off + 1] = (Uint8)(val & 0x000FF);
0056 }
0057 
0058 inline Uint16 ReadUint16(const Uint8 *buf, Uint32 off)
0059 {
0060     return (buf[off] << 8) | buf[off + 1];
0061 }
0062 
0063 inline void WriteInt64(Uint8 *buf, Uint32 off, Int64 val)
0064 {
0065     buf[off + 0] = (Uint8)((val & 0xFF00000000000000ULL) >> 56);
0066     buf[off + 1] = (Uint8)((val & 0x00FF000000000000ULL) >> 48);
0067     buf[off + 2] = (Uint8)((val & 0x0000FF0000000000ULL) >> 40);
0068     buf[off + 3] = (Uint8)((val & 0x000000FF00000000ULL) >> 32);
0069     buf[off + 4] = (Uint8)((val & 0x00000000FF000000ULL) >> 24);
0070     buf[off + 5] = (Uint8)((val & 0x0000000000FF0000ULL) >> 16);
0071     buf[off + 6] = (Uint8)((val & 0x000000000000FF00ULL) >> 8);
0072     buf[off + 7] = (Uint8)((val & 0x00000000000000FFULL) >> 0);
0073 }
0074 
0075 inline Int64 ReadInt64(const Uint8 *buf, Uint32 off)
0076 {
0077     Int64 tmp = ((Int64)buf[off] << 56) | ((Int64)buf[off + 1] << 48) | ((Int64)buf[off + 2] << 40) | ((Int64)buf[off + 3] << 32) | ((Int64)buf[off + 4] << 24)
0078         | ((Int64)buf[off + 5] << 16) | ((Int64)buf[off + 6] << 8) | ((Int64)buf[off + 7] << 0);
0079 
0080     return tmp;
0081 }
0082 
0083 inline void WriteInt32(Uint8 *buf, Uint32 off, Int32 val)
0084 {
0085     buf[off + 0] = (Uint8)((val & 0xFF000000) >> 24);
0086     buf[off + 1] = (Uint8)((val & 0x00FF0000) >> 16);
0087     buf[off + 2] = (Uint8)((val & 0x0000FF00) >> 8);
0088     buf[off + 3] = (Uint8)(val & 0x000000FF);
0089 }
0090 
0091 inline Int32 ReadInt32(const Uint8 *buf, Uint32 off)
0092 {
0093     return (Int32)(buf[off] << 24) | (buf[off + 1] << 16) | (buf[off + 2] << 8) | buf[off + 3];
0094 }
0095 
0096 inline void WriteInt16(Uint8 *buf, Uint32 off, Int16 val)
0097 {
0098     buf[off + 0] = (Uint8)((val & 0xFF00) >> 8);
0099     buf[off + 1] = (Uint8)(val & 0x000FF);
0100 }
0101 
0102 inline Int16 ReadInt16(const Uint8 *buf, Uint32 off)
0103 {
0104     return (Int16)(buf[off] << 8) | buf[off + 1];
0105 }
0106 
0107 KTORRENT_EXPORT void UpdateCurrentTime();
0108 
0109 KTORRENT_EXPORT extern TimeStamp global_time_stamp;
0110 
0111 inline TimeStamp CurrentTime()
0112 {
0113     return global_time_stamp;
0114 }
0115 
0116 KTORRENT_EXPORT TimeStamp Now();
0117 
0118 KTORRENT_EXPORT QString DirSeparator();
0119 KTORRENT_EXPORT bool IsMultimediaFile(const QString &filename);
0120 
0121 /**
0122  * Maximize the file and memory limits using setrlimit.
0123  */
0124 KTORRENT_EXPORT bool MaximizeLimits();
0125 
0126 /// Get the maximum number of open files
0127 KTORRENT_EXPORT Uint32 MaxOpenFiles();
0128 
0129 /// Get the current number of open files
0130 KTORRENT_EXPORT Uint32 CurrentOpenFiles();
0131 
0132 /// Can we open another file ?
0133 KTORRENT_EXPORT bool OpenFileAllowed();
0134 
0135 /// Set the network interface to use (null means all interfaces)
0136 KTORRENT_EXPORT void SetNetworkInterface(const QString &iface);
0137 
0138 /// Get the network interface which needs to be used (this will return the name e.g. eth0, wlan0 ...)
0139 KTORRENT_EXPORT QString NetworkInterface();
0140 
0141 /// Get the IP address of the network interface
0142 KTORRENT_EXPORT QString NetworkInterfaceIPAddress(const QString &iface);
0143 
0144 /// Get all the IP addresses of the network interface
0145 KTORRENT_EXPORT QStringList NetworkInterfaceIPAddresses(const QString &iface);
0146 
0147 /// Get the current IPv6 address
0148 KTORRENT_EXPORT QString CurrentIPv6Address();
0149 
0150 const double TO_KB = 1024.0;
0151 const double TO_MEG = (1024.0 * 1024.0);
0152 const double TO_GIG = (1024.0 * 1024.0 * 1024.0);
0153 
0154 KTORRENT_EXPORT QString BytesToString(bt::Uint64 bytes);
0155 KTORRENT_EXPORT QString BytesPerSecToString(double speed);
0156 KTORRENT_EXPORT QString DurationToString(bt::Uint32 nsecs);
0157 
0158 template<class T> int CompareVal(T a, T b)
0159 {
0160     if (a < b)
0161         return -1;
0162     else if (a > b)
0163         return 1;
0164     else
0165         return 0;
0166 }
0167 
0168 template<class T> QString hex(T val)
0169 {
0170     return QStringLiteral("0x%1").arg(val, 0, 16);
0171 }
0172 
0173 struct KTORRENT_EXPORT RecursiveEntryGuard {
0174     bool *guard;
0175 
0176     RecursiveEntryGuard(bool *g)
0177         : guard(g)
0178     {
0179         *guard = true;
0180     }
0181 
0182     ~RecursiveEntryGuard()
0183     {
0184         *guard = false;
0185     }
0186 };
0187 
0188 /**
0189     Global initialization function, should be called, in the applications main function.
0190  */
0191 KTORRENT_EXPORT bool InitLibKTorrent();
0192 }
0193 
0194 #endif