This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
Util.h
321 lines (266 loc) · 8.62 KB
/
Util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* This file is part of nzbget
*
* Copyright (C) 2007-2015 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Revision$
* $Date$
*
*/
#ifndef UTIL_H
#define UTIL_H
#include "NString.h"
#ifdef WIN32
extern int optind, opterr;
extern char *optarg;
int getopt(int argc, char *argv[], char *optstring);
#endif
class Util
{
public:
static bool MatchFileExt(const char* filename, const char* extensionList, const char* listSeparator);
static int64 GetCurrentTicks();
/*
* Split command line into arguments.
* Uses spaces and single quotation marks as separators.
* May return empty list if bad escaping was detected.
*/
static std::vector<CString> SplitCommandLine(const char* commandLine);
static int64 JoinInt64(uint32 Hi, uint32 Lo);
static void SplitInt64(int64 Int64, uint32* Hi, uint32* Lo);
static void TrimRight(char* str);
static char* Trim(char* str);
static bool EmptyStr(const char* str) { return !str || !*str; }
static std::vector<CString> SplitStr(const char* str, const char* separators);
/* replace all occurences of szFrom to szTo in string szStr with a limitation that szTo must be shorter than szFrom */
static char* ReduceStr(char* str, const char* from, const char* to);
/* Calculate Hash using Bob Jenkins (1996) algorithm */
static uint32 HashBJ96(const char* buffer, int bufSize, uint32 initValue);
#ifdef WIN32
static bool RegReadStr(HKEY keyRoot, const char* keyName, const char* valueName, char* buffer, int* bufLen);
#endif
static void SetStandByMode(bool standBy);
static time_t CurrentTime();
/* cross platform version of GNU timegm, which is similar to mktime but takes an UTC time as parameter */
static time_t Timegm(tm const *t);
static void FormatTime(time_t timeSec, char* buffer, int bufsize);
static CString FormatTime(time_t timeSec);
static CString FormatSpeed(int bytesPerSecond);
static CString FormatSize(int64 fileSize);
/*
* Returns program version and revision number as string formatted like "0.7.0-r295".
* If revision number is not available only version is returned ("0.7.0").
*/
static const char* VersionRevision() { return VersionRevisionBuf; };
static char VersionRevisionBuf[100];
static void Init();
static uint32 Crc32(uchar *block, uint32 length);
static uint32 Crc32m(uint32 startCrc, uchar *block, uint32 length);
static uint32 Crc32Combine(uint32 crc1, uint32 crc2, uint32 len2);
/*
* Returns number of available CPU cores or -1 if it could not be determined
*/
static int NumberOfCpuCores();
};
class WebUtil
{
public:
static uint32 DecodeBase64(char* inputBuffer, int inputBufferLength, char* outputBuffer);
/*
* Encodes string to be used as content of xml-tag.
*/
static CString XmlEncode(const char* raw);
/*
* Decodes string from xml.
* The string is decoded on the place overwriting the content of raw-data.
*/
static void XmlDecode(char* raw);
/*
* Returns pointer to tag-content and length of content in iValueLength
* The returned pointer points to the part of source-string, no additional strings are allocated.
*/
static const char* XmlFindTag(const char* xml, const char* tag, int* valueLength);
/*
* Parses tag-content into szValueBuf.
*/
static bool XmlParseTagValue(const char* xml, const char* tag, char* valueBuf, int valueBufSize, const char** tagEnd);
/*
* Replaces all tags with spaces effectively providing the text content only.
* The string is transformed in-place overwriting the previous content.
*/
static void XmlStripTags(char* xml);
/*
* Replaces all entities with spaces.
* The string is transformed in-place overwriting the previous content.
*/
static void XmlRemoveEntities(char* raw);
/*
* Creates JSON-string by replace the certain characters with escape-sequences.
*/
static CString JsonEncode(const char* raw);
/*
* Decodes JSON-string.
* The string is decoded on the place overwriting the content of raw-data.
*/
static void JsonDecode(char* raw);
/*
* Returns pointer to field-content and length of content in iValueLength
* The returned pointer points to the part of source-string, no additional strings are allocated.
*/
static const char* JsonFindField(const char* jsonText, const char* fieldName, int* valueLength);
/*
* Returns pointer to field-content and length of content in iValueLength
* The returned pointer points to the part of source-string, no additional strings are allocated.
*/
static const char* JsonNextValue(const char* jsonText, int* valueLength);
/*
* Unquote http quoted string.
* The string is decoded on the place overwriting the content of raw-data.
*/
static void HttpUnquote(char* raw);
/*
* Decodes URL-string.
* The string is decoded on the place overwriting the content of raw-data.
*/
static void UrlDecode(char* raw);
/*
* Makes valid URL by replacing of spaces with "%20".
*/
static CString UrlEncode(const char* raw);
/*
* Converts ISO-8859-1 (aka Latin-1) into UTF-8.
*/
static CString Latin1ToUtf8(const char* str);
static time_t ParseRfc822DateTime(const char* dateTimeStr);
};
class URL
{
private:
CString m_address;
CString m_protocol;
CString m_user;
CString m_password;
CString m_host;
CString m_resource;
int m_port;
bool m_tls;
bool m_valid;
void ParseUrl();
public:
URL(const char* address);
bool IsValid() { return m_valid; }
const char* GetAddress() { return m_address; }
const char* GetProtocol() { return m_protocol; }
const char* GetUser() { return m_user; }
const char* GetPassword() { return m_password; }
const char* GetHost() { return m_host; }
const char* GetResource() { return m_resource; }
int GetPort() { return m_port; }
bool GetTls() { return m_tls; }
};
class RegEx
{
private:
#ifdef HAVE_REGEX_H
regex_t m_context;
std::unique_ptr<regmatch_t[]> m_matches;
#endif
bool m_valid;
int m_matchBufSize;
public:
RegEx(const char *pattern, int matchBufSize = 100);
~RegEx();
bool IsValid() { return m_valid; }
bool Match(const char* str);
int GetMatchCount();
int GetMatchStart(int index);
int GetMatchLen(int index);
};
class WildMask
{
private:
typedef std::vector<int> IntList;
CString m_pattern;
bool m_wantsPositions;
int m_wildCount;
IntList m_wildStart;
IntList m_wildLen;
void ExpandArray();
public:
WildMask(const char* pattern, bool wantsPositions = false);
bool Match(const char* text);
int GetMatchCount() { return m_wildCount; }
int GetMatchStart(int index) { return m_wildStart[index]; }
int GetMatchLen(int index) { return m_wildLen[index]; }
};
#ifndef DISABLE_GZIP
class ZLib
{
public:
/*
* calculates the size required for output buffer
*/
static uint32 GZipLen(int inputBufferLength);
/*
* compresses inputBuffer and returns the size of bytes written to
* outputBuffer or 0 if the buffer is too small or an error occured.
*/
static uint32 GZip(const void* inputBuffer, int inputBufferLength, void* outputBuffer, int outputBufferLength);
};
class GUnzipStream
{
public:
enum EStatus
{
zlError,
zlFinished,
zlOK
};
private:
void* m_zStream;
void* m_outputBuffer;
int m_bufferSize;
public:
GUnzipStream(int BufferSize);
~GUnzipStream();
/*
* set next memory block for uncompression
*/
void Write(const void *inputBuffer, int inputBufferLength);
/*
* get next uncompressed memory block.
* iOutputBufferLength - the size of uncompressed block. if it is "0" the next compressed block must be provided via "Write".
*/
EStatus Read(const void **outputBuffer, int *outputBufferLength);
};
#endif
class Tokenizer
{
private:
BString<1024> m_defaultBuf;
char* m_dataString;
bool m_inplaceBuf;
const char* m_separators;
char* m_savePtr;
bool m_working;
public:
Tokenizer(const char* dataString, const char* separators);
Tokenizer(char* dataString, const char* separators, bool inplaceBuf);
~Tokenizer();
char* Next();
};
#endif