1 /*
2  * Copyright 2013 Vitaly Valtman
3  * Copyright 2014 Canonical Ltd.
4  * Authors:
5  *      Roberto Mier
6  *      Tiago Herrmann
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 3.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef UTILS_H
23 #define UTILS_H
24 
25 #ifdef DEBUG
26 #define RES_PRE 8
27 #define RES_AFTER 8
28 #define MAX_BLOCKS 1000000
29 void *blocks[MAX_BLOCKS];
30 void *free_blocks[MAX_BLOCKS];
31 qint32 usedBlocks;
32 qint32 freeBlocksCnt;
33 #endif
34 
35 #include <QObject>
36 #include <openssl/bn.h>
37 #include "constants.h"
38 #include "libqtelegram_global.h"
39 #include <QLoggingCategory>
40 
Q_DECLARE_LOGGING_CATEGORY(TG_UTIL_UTILS)41 Q_DECLARE_LOGGING_CATEGORY(TG_UTIL_UTILS)
42 
43 
44 class LIBQTELEGRAMSHARED_EXPORT Utils : public QObject
45 {
46     Q_OBJECT
47 public:
48     explicit Utils(QObject *parent = 0);
49 
50     static qint32 randomBytes(void *buffer, qint32 count);
51     static qint32 serializeBignum(const BIGNUM *b, char *buffer, qint32 maxlen);
52     static double getUTime(qint32 clockId);
53     static quint64 gcd (quint64 a, quint64 b);
54 
55     static RSA *rsaLoadPublicKey (const QString &publicKeyName);
56     static qint64 computeRSAFingerprint(RSA *key);
57 
58     static qint32 check_g (uchar p[256], BIGNUM *g);
59     static qint32 check_g_bn (BIGNUM *p, BIGNUM *g);
60 
61     static void ensure(qint32 r);
62     static void ensurePtr(void *p);
63     static void freeSecure (void *ptr, qint32 size);
64     static void secureZeroMemory(void *dst, int val, size_t count);
65 
66     static void *talloc(size_t size);
67     static qint32 tinflate (void *input, qint32 ilen, void *output, qint32 olen);
68 
69     static QString toHex(qint32 x);
70     static QString toHex(void *buffer, qint32 size);
71 
72     static BIGNUM *padBytesAndGetBignum(const QByteArray &bytes);
73     static BIGNUM *bytesToBignum(const QByteArray &bytes);
74     static QByteArray bignumToBytes(BIGNUM *bignum);
75     static qint64 getKeyFingerprint(uchar *sharedKey);
76 
77     static QString getDeviceModel();
78     static QString getSystemVersion();
79     static QString getAppVersion();
80 
81     static qint64 getMediaDuration(const QString &resource, const QString &type);
82     static QString createThumbnail(const QString &resource);
83 
84     static QString parsePhoneNumberDigits(const QString &phoneNumber);
85 
86     static QByteArray pToBigEndian(quint32 p);
87     static qint64 findDivider(qint64 pq);
88 };
89 
90 #ifdef Q_OS_WIN
91 qint64 lrand48();
92 #endif
93 
94 #endif // UTILS_H
95