2
* This file is part of nzbget
4
* Copyright (C) 2007-2009 Andrei Prygounkov <hugbug@users.sourceforge.net>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
* $Date: 2009-12-08 23:26:21 +0100 (Tue, 08 Dec 2009) $
43
#include <sys/statvfs.h>
51
// function "svn_version" is automatically generated in file "svn_version.cpp" on each build
52
const char* svn_version(void);
58
// from http://www.codeproject.com/cpp/xgetopt.asp
59
// Original Author: Hans Dietrich (hdietrich2@hotmail.com)
60
// Released to public domain from author (thanks)
61
// Slightly modified by Andrei Prygounkov
63
char *optarg; // global argument pointer
64
int optind = 0; // global argv index
66
int getopt(int argc, char *argv[], char *optstring)
68
static char *next = NULL;
74
if (next == NULL || *next == '\0')
79
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
83
optarg = argv[optind];
87
if (strcmp(argv[optind], "--") == 0)
92
optarg = argv[optind];
97
next++; // skip past -
102
char *cp = strchr(optstring, c);
104
if (cp == NULL || c == ':')
106
fprintf(stderr, "Invalid option %c", c);
118
else if (optind < argc)
120
optarg = argv[optind];
125
fprintf(stderr, "Option %c needs an argument", c);
133
DirBrowser::DirBrowser(const char* szPath)
135
char szMask[MAX_PATH + 1];
136
int len = strlen(szPath);
137
if (szPath[len] == '\\' || szPath[len] == '/')
139
snprintf(szMask, MAX_PATH + 1, "%s*.*", szPath);
143
snprintf(szMask, MAX_PATH + 1, "%s%c*.*", szPath, (int)PATH_SEPARATOR);
145
szMask[MAX_PATH] = '\0';
146
m_hFile = _findfirst(szMask, &m_FindData);
150
DirBrowser::~DirBrowser()
158
const char* DirBrowser::Next()
163
bOK = m_hFile != -1L;
168
bOK = _findnext(m_hFile, &m_FindData) == 0;
172
return m_FindData.name;
179
DirBrowser::DirBrowser(const char* szPath)
181
m_pDir = opendir(szPath);
184
DirBrowser::~DirBrowser()
192
const char* DirBrowser::Next()
196
m_pFindData = readdir(m_pDir);
199
return m_pFindData->d_name;
207
char Util::VersionRevisionBuf[40];
209
char* Util::BaseFileName(const char* filename)
211
char* p = (char*)strrchr(filename, PATH_SEPARATOR);
212
char* p1 = (char*)strrchr(filename, ALT_PATH_SEPARATOR);
215
if ((p && p < p1) || !p)
226
return (char*)filename;
230
void Util::NormalizePathSeparators(char* szPath)
232
for (char* p = szPath; *p; p++)
234
if (*p == ALT_PATH_SEPARATOR)
241
bool Util::ForceDirectories(const char* szPath)
243
char* szNormPath = strdup(szPath);
244
NormalizePathSeparators(szNormPath);
245
int iLen = strlen(szNormPath);
246
if ((iLen > 0) && szNormPath[iLen-1] == PATH_SEPARATOR
252
szNormPath[iLen-1] = '\0';
256
bool bOK = !stat(szNormPath, &buffer) && S_ISDIR(buffer.st_mode);
259
&& strlen(szNormPath) > 2
263
char* szParentPath = strdup(szNormPath);
265
char* p = (char*)strrchr(szParentPath, PATH_SEPARATOR);
269
if (p - szParentPath == 2 && szParentPath[1] == ':' && strlen(szParentPath) > 2)
271
szParentPath[3] = '\0';
278
if (strlen(szParentPath) != strlen(szPath))
280
bOK = ForceDirectories(szParentPath);
285
mkdir(szNormPath, S_DIRMODE);
286
bOK = !stat(szNormPath, &buffer) && S_ISDIR(buffer.st_mode);
294
bool Util::DirEmpty(const char* szDirFilename)
296
DirBrowser dir(szDirFilename);
297
while (const char* filename = dir.Next())
299
if (strcmp(filename, ".") && strcmp(filename, ".."))
307
bool Util::LoadFileIntoBuffer(const char* szFileName, char** pBuffer, int* pBufferLength)
309
FILE* pFile = fopen(szFileName, "rb");
316
fseek(pFile , 0 , SEEK_END);
317
int iSize = ftell(pFile);
320
// allocate memory to contain the whole file.
321
*pBuffer = (char*) malloc(iSize + 1);
327
// copy the file into the buffer.
328
fread(*pBuffer, 1, iSize, pFile);
332
(*pBuffer)[iSize] = 0;
334
*pBufferLength = iSize + 1;
339
bool Util::SetFileSize(const char* szFilename, int iSize)
343
FILE* pFile = fopen(szFilename, "ab");
346
bOK = _chsize_s(pFile->_file, iSize) == 0;
351
FILE* pFile = fopen(szFilename, "ab");
356
// there are no reliable function to expand file on POSIX, so we must try different approaches,
357
// starting with the fastest one and hoping it will work
358
// 1) set file size using function "truncate" (it is fast, if works)
359
truncate(szFilename, iSize);
360
// check if it worked
361
pFile = fopen(szFilename, "ab");
364
fseek(pFile, 0, SEEK_END);
365
bOK = ftell(pFile) == iSize;
368
// 2) truncate did not work, expanding the file by writing in it (it is slow)
370
truncate(szFilename, 0);
371
pFile = fopen(szFilename, "ab");
373
fwrite(&c, 1, iSize, pFile);
374
bOK = ftell(pFile) == iSize;
382
//replace bad chars in filename
383
void Util::MakeValidFilename(char* szFilename, char cReplaceChar, bool bAllowSlashes)
385
const char* szReplaceChars = bAllowSlashes ? ":*?\"><'\n\r\t" : "\\/:*?\"><'\n\r\t";
386
char* p = szFilename;
389
if (strchr(szReplaceChars, *p))
393
if (bAllowSlashes && *p == ALT_PATH_SEPARATOR)
400
// remove trailing dots and spaces. they are not allowed in directory names on windows,
401
// but we remove them on posix also, in a case the directory is accessed from windows via samba.
402
for (int iLen = strlen(szFilename); iLen > 0 && (szFilename[iLen - 1] == '.' || szFilename[iLen - 1] == ' '); iLen--)
404
szFilename[iLen - 1] = '\0';
408
long long Util::JoinInt64(unsigned long Hi, unsigned long Lo)
410
return (((long long)Hi) << 32) + Lo;
413
void Util::SplitInt64(long long Int64, unsigned long* Hi, unsigned long* Lo)
415
*Hi = (unsigned long)(Int64 >> 32);
416
*Lo = (unsigned long)Int64;
419
float Util::Int64ToFloat(long long Int64)
421
unsigned long Hi = (unsigned long)(Int64 >> 32);
422
unsigned long Lo = (unsigned long)Int64;
423
return ((unsigned long)(1 << 30)) * 4.0f * Hi + Lo;
426
/* Base64 decryption is taken from
427
* Article "BASE 64 Decoding and Encoding Class 2003" by Jan Raddatz
428
* http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5099/
431
const static char BASE64_DEALPHABET [128] =
433
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9
434
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19
435
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29
436
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39
437
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49
438
54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50 - 59
439
0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69
440
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
441
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
442
25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90 - 99
443
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
444
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
445
49, 50, 51, 0, 0, 0, 0, 0 // 120 - 127
448
unsigned int DecodeByteQuartet(char* szInputBuffer, char* szOutputBuffer)
450
unsigned int buffer = 0;
452
if (szInputBuffer[3] == '=')
454
if (szInputBuffer[2] == '=')
456
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[0]]) << 6;
457
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[1]]) << 6;
458
buffer = buffer << 14;
460
szOutputBuffer [0] = (char)(buffer >> 24);
466
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[0]]) << 6;
467
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[1]]) << 6;
468
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[2]]) << 6;
469
buffer = buffer << 8;
471
szOutputBuffer [0] = (char)(buffer >> 24);
472
szOutputBuffer [1] = (char)(buffer >> 16);
479
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[0]]) << 6;
480
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[1]]) << 6;
481
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[2]]) << 6;
482
buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[3]]) << 6;
483
buffer = buffer << 2;
485
szOutputBuffer [0] = (char)(buffer >> 24);
486
szOutputBuffer [1] = (char)(buffer >> 16);
487
szOutputBuffer [2] = (char)(buffer >> 8);
495
unsigned int Util::DecodeBase64(char* szInputBuffer, int iInputBufferLength, char* szOutputBuffer)
497
unsigned int InputBufferIndex = 0;
498
unsigned int OutputBufferIndex = 0;
499
unsigned int InputBufferLength = iInputBufferLength > 0 ? iInputBufferLength : strlen(szInputBuffer);
501
char ByteQuartet [4];
503
while (InputBufferIndex < InputBufferLength)
505
// Ignore all characters except the ones in BASE64_ALPHABET
506
if ((szInputBuffer [InputBufferIndex] >= 48 && szInputBuffer [InputBufferIndex] <= 57) ||
507
(szInputBuffer [InputBufferIndex] >= 65 && szInputBuffer [InputBufferIndex] <= 90) ||
508
(szInputBuffer [InputBufferIndex] >= 97 && szInputBuffer [InputBufferIndex] <= 122) ||
509
szInputBuffer [InputBufferIndex] == '+' ||
510
szInputBuffer [InputBufferIndex] == '/' ||
511
szInputBuffer [InputBufferIndex] == '=')
513
ByteQuartet [i] = szInputBuffer [InputBufferIndex];
520
OutputBufferIndex += DecodeByteQuartet(ByteQuartet, szOutputBuffer + OutputBufferIndex);
525
// OutputBufferIndex gives us the next position of the next decoded character
526
// inside our output buffer and thus represents the number of decoded characters
528
return OutputBufferIndex;
534
char* Util::XmlEncode(const char* raw)
536
// calculate the required outputstring-size based on number of xml-entities and their sizes
537
int iReqSize = strlen(raw);
538
for (const char* p = raw; *p; p++)
540
unsigned char ch = *p;
563
char* result = (char*)malloc(iReqSize + 1);
566
char* output = result;
567
for (const char* p = raw; ; p++)
569
unsigned char ch = *p;
575
strcpy(output, "<");
579
strcpy(output, ">");
583
strcpy(output, "&");
587
strcpy(output, "'");
591
strcpy(output, """);
597
sprintf(output, "&#%i;", ch);
614
void Util::XmlDecode(char* raw)
617
for (char* p = raw;;)
626
if (!strncmp(p, "lt;", 3))
631
else if (!strncmp(p, "gt;", 3))
636
else if (!strncmp(p, "amp;", 4))
641
else if (!strncmp(p, "apos;", 5))
646
else if (!strncmp(p, "quot;", 5))
669
const char* Util::XmlFindTag(const char* szXml, const char* szTag, int* pValueLength)
672
snprintf(szOpenTag, 100, "<%s>", szTag);
673
szOpenTag[100-1] = '\0';
675
char szCloseTag[100];
676
snprintf(szCloseTag, 100, "</%s>", szTag);
677
szCloseTag[100-1] = '\0';
679
char szOpenCloseTag[100];
680
snprintf(szOpenCloseTag, 100, "<%s/>", szTag);
681
szOpenCloseTag[100-1] = '\0';
683
const char* pstart = strstr(szXml, szOpenTag);
684
const char* pstartend = strstr(szXml, szOpenCloseTag);
685
if (!pstart && !pstartend) return NULL;
687
if (pstartend && (!pstart || pstartend < pstart))
693
const char* pend = strstr(pstart, szCloseTag);
694
if (!pend) return NULL;
696
int iTagLen = strlen(szOpenTag);
697
*pValueLength = (int)(pend - pstart - iTagLen);
699
return pstart + iTagLen;
702
bool Util::XmlParseTagValue(const char* szXml, const char* szTag, char* szValueBuf, int iValueBufSize, const char** pTagEnd)
705
const char* szValue = XmlFindTag(szXml, szTag, &iValueLen);
710
int iLen = iValueLen < iValueBufSize ? iValueLen : iValueBufSize - 1;
711
strncpy(szValueBuf, szValue, iLen);
712
szValueBuf[iLen] = '\0';
715
*pTagEnd = szValue + iValueLen;
720
bool Util::MoveFile(const char* szSrcFilename, const char* szDstFilename)
722
bool bOK = rename(szSrcFilename, szDstFilename) == 0;
725
if (!bOK && (errno == EXDEV))
727
FILE* infile = fopen(szSrcFilename, "rb");
733
FILE* outfile = fopen(szDstFilename, "wb+");
740
static const int BUFFER_SIZE = 1024 * 50;
741
char* buffer = (char*)malloc(BUFFER_SIZE);
743
int cnt = BUFFER_SIZE;
744
while (cnt == BUFFER_SIZE)
746
cnt = (int)fread(buffer, 1, BUFFER_SIZE, infile);
747
fwrite(buffer, 1, cnt, outfile);
754
bOK = remove(szSrcFilename) == 0;
761
bool Util::FileExists(const char* szFilename)
764
bool bExists = !stat(szFilename, &buffer) && S_ISREG(buffer.st_mode);
768
bool Util::DirectoryExists(const char* szDirFilename)
771
bool bExists = !stat(szDirFilename, &buffer) && S_ISDIR(buffer.st_mode);
775
bool Util::CreateDirectory(const char* szDirFilename)
777
mkdir(szDirFilename, S_DIRMODE);
778
return DirectoryExists(szDirFilename);
781
long long Util::FileSize(const char* szFilename)
784
struct _stat32i64 buffer;
785
_stat32i64(szFilename, &buffer);
788
struct stat64 buffer;
789
stat64(szFilename, &buffer);
792
stat(szFilename, &buffer);
795
return buffer.st_size;
798
char* Util::JsonEncode(const char* raw)
800
// calculate the required outputstring-size based on number of escape-entities and their sizes
801
int iReqSize = strlen(raw);
802
for (const char* p = raw; *p; p++)
804
unsigned char ch = *p;
825
char* result = (char*)malloc(iReqSize + 1);
828
char* output = result;
829
for (const char* p = raw; ; p++)
831
unsigned char ch = *p;
837
strcpy(output, "\\\"");
841
strcpy(output, "\\\\");
845
strcpy(output, "\\/");
849
strcpy(output, "\\b");
853
strcpy(output, "\\f");
857
strcpy(output, "\\n");
861
strcpy(output, "\\r");
865
strcpy(output, "\\t");
871
sprintf(output, "\\u%04x", ch);
888
void Util::JsonDecode(char* raw)
891
for (char* p = raw;;)
927
*output++ = (char)strtol(p + 1, NULL, 16);
931
// unknown escape-sequence, should never occur
947
const char* Util::JsonFindField(const char* szJsonText, const char* szFieldName, int* pValueLength)
950
snprintf(szOpenTag, 100, "\"%s\"", szFieldName);
951
szOpenTag[100-1] = '\0';
953
const char* pstart = strstr(szJsonText, szOpenTag);
954
if (!pstart) return NULL;
956
pstart += strlen(szOpenTag);
958
return JsonNextValue(pstart, pValueLength);
961
const char* Util::JsonNextValue(const char* szJsonText, int* pValueLength)
963
const char* pstart = szJsonText;
965
while (*pstart && strchr(" ,[{:\r\n\t\f", *pstart)) pstart++;
966
if (!*pstart) return NULL;
968
const char* pend = pstart;
971
bool bStr = ch == '"';
980
if (!*++pend || !*++pend) return NULL;
983
if (bStr && ch == '"')
988
else if (!bStr && strchr(" ,]}\r\n\t\f", ch))
995
*pValueLength = (int)(pend - pstart);
999
long long Util::FreeDiskSize(const char* szPath)
1002
ULARGE_INTEGER lFree, lDummy;
1003
if (GetDiskFreeSpaceEx(szPath, &lFree, &lDummy, &lDummy))
1005
return lFree.QuadPart;
1008
struct statvfs diskdata;
1009
if (!statvfs(szPath, &diskdata))
1011
return (long long)diskdata.f_bsize * (long long)diskdata.f_bavail;
1017
bool Util::RenameBak(const char* szFilename, const char* szBakPart, bool bRemoveOldExtension, char* szNewNameBuf, int iNewNameBufSize)
1019
char szChangedFilename[1024];
1021
if (bRemoveOldExtension)
1023
strncpy(szChangedFilename, szFilename, 1024);
1024
szChangedFilename[1024-1] = '\0';
1025
char* szExtension = strrchr(szChangedFilename, '.');
1028
*szExtension = '\0';
1033
snprintf(bakname, 1024, "%s.%s", bRemoveOldExtension ? szChangedFilename : szFilename, szBakPart);
1034
bakname[1024-1] = '\0';
1038
while (!stat(bakname, &buffer))
1040
snprintf(bakname, 1024, "%s.%i.%s", bRemoveOldExtension ? szChangedFilename : szFilename, i++, szBakPart);
1041
bakname[1024-1] = '\0';
1046
strncpy(szNewNameBuf, bakname, iNewNameBufSize);
1049
bool bOK = !rename(szFilename, bakname);
1054
bool Util::ExpandHomePath(const char* szFilename, char* szBuffer, int iBufSize)
1056
if (szFilename && (szFilename[0] == '~') && (szFilename[1] == '/'))
1060
char* home = getenv("HOME");
1063
struct passwd *pw = getpwuid(getuid());
1075
if (home[strlen(home)-1] == '/')
1077
snprintf(szBuffer, iBufSize, "%s%s", home, szFilename + 2);
1081
snprintf(szBuffer, iBufSize, "%s/%s", home, szFilename + 2);
1083
szBuffer[iBufSize - 1] = '\0';
1087
strncpy(szBuffer, szFilename, iBufSize);
1088
szBuffer[iBufSize - 1] = '\0';
1094
void Util::ExpandFileName(const char* szFilename, char* szBuffer, int iBufSize)
1096
if (szFilename[0] != '\0' && szFilename[0] != '/')
1098
char szCurDir[MAX_PATH + 1];
1099
getcwd(szCurDir, sizeof(szCurDir) - 1); // 1 char reserved for adding backslash
1101
if (szFilename[0] == '.' && szFilename[1] == '/')
1105
snprintf(szBuffer, iBufSize, "%s/%s", szCurDir, szFilename + iOffset);
1109
strncpy(szBuffer, szFilename, iBufSize);
1110
szBuffer[iBufSize - 1] = '\0';
1115
void Util::FormatFileSize(char * szBuffer, int iBufLen, long long lFileSize)
1117
if (lFileSize > 1024 * 1024 * 1000)
1119
snprintf(szBuffer, iBufLen, "%.2f GB", (float)(Util::Int64ToFloat(lFileSize) / 1024 / 1024 / 1024));
1121
else if (lFileSize > 1024 * 1000)
1123
snprintf(szBuffer, iBufLen, "%.2f MB", (float)(Util::Int64ToFloat(lFileSize) / 1024 / 1024));
1125
else if (lFileSize > 1000)
1127
snprintf(szBuffer, iBufLen, "%.2f KB", (float)(Util::Int64ToFloat(lFileSize) / 1024));
1131
snprintf(szBuffer, iBufLen, "%i B", (int)lFileSize);
1133
szBuffer[iBufLen - 1] = '\0';
1136
void Util::InitVersionRevision()
1139
if ((strlen(svn_version()) > 0) && strstr(VERSION, "testing"))
1141
snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s-r%s", VERSION, svn_version());
1146
snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s", VERSION);
1150
bool Util::SplitCommandLine(const char* szCommandLine, char*** argv)
1154
char* pszArgList[100];
1155
unsigned int iLen = 0;
1156
bool bEscaping = false;
1158
for (const char* p = szCommandLine; ; p++)
1167
if (p[1] == '\'' && iLen < sizeof(szBuf) - 1)
1178
else if (iLen < sizeof(szBuf) - 1)
1189
else if (c == '\'' && bSpace)
1194
else if (iLen < sizeof(szBuf) - 1)
1202
if ((bSpace || !*p) && iLen > 0 && iArgCount < 100)
1208
pszArgList[iArgCount] = strdup(szBuf);
1222
pszArgList[iArgCount] = NULL;
1223
*argv = (char**)malloc((iArgCount + 1) * sizeof(char*));
1224
memcpy(*argv, pszArgList, sizeof(char*) * (iArgCount + 1));
1227
return iArgCount > 0;