~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to .pc/fix_fsf_address.patch/Util.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2013-07-18 14:50:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130718145028-qhxse81w1sj5w424
Tags: 11.0+dfsg-1
* New upstream release (Closes: #701896)
* Repackage original tarball to remove copies of jquery and twitter-
  bootstrap
* debian/watch: Update for new versioning scheme
* debian/patches: Remove all old patches, add one patch:
  - dont-embed-libraries.patch: Don't install embedded jquery and bootstrap 
    libraries
* debian/combat: Upgrade to debhelper combat 9
* debian/control:
  - Fix Vcs-Git field
  - Adjust debhelper version for combat level 9
  - Add jquery and bootstrap to depends for integrated webserver
  - Add python to recommends for post-processing scripts
  - Bump build-depends on libpar2-dev to support the cancel function
* debian/links:
  - Use the system jquery and bootstrap libraries
* debian/rules:
  - Add get-orig-source target to build modified upstream tarball
* Adjust sample nzbget.conf:
  - Only listen to 127.0.0.1 instead of 0.0.0.0
  - Use nzbget.conf as template for webui configuration
* Adjust sample nzbgetd init file:
  - Point to correct location of nzbget binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  This file is part of nzbget
3
 
 *
4
 
 *  Copyright (C) 2007-2009 Andrei Prygounkov <hugbug@users.sourceforge.net>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 *
20
 
 * $Revision: 368 $
21
 
 * $Date: 2009-12-08 23:26:21 +0100 (Tue, 08 Dec 2009) $
22
 
 *
23
 
 */
24
 
 
25
 
 
26
 
#ifdef HAVE_CONFIG_H
27
 
#include <config.h>
28
 
#endif
29
 
 
30
 
#ifdef WIN32
31
 
#include "win32.h"
32
 
#endif
33
 
 
34
 
#include <stdlib.h>
35
 
#include <string.h>
36
 
#include <sys/stat.h>
37
 
#include <stdio.h>
38
 
#include <errno.h>
39
 
#ifdef WIN32
40
 
#include <direct.h>
41
 
#else
42
 
#include <unistd.h>
43
 
#include <sys/statvfs.h>
44
 
#include <pwd.h>
45
 
#endif
46
 
 
47
 
#include "nzbget.h"
48
 
#include "Util.h"
49
 
 
50
 
#ifndef WIN32
51
 
// function "svn_version" is automatically generated in file "svn_version.cpp" on each build
52
 
const char* svn_version(void);
53
 
#endif
54
 
 
55
 
#ifdef WIN32
56
 
 
57
 
// getopt for WIN32:
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
62
 
 
63
 
char    *optarg;                // global argument pointer
64
 
int             optind = 0;     // global argv index
65
 
 
66
 
int getopt(int argc, char *argv[], char *optstring)
67
 
{
68
 
        static char *next = NULL;
69
 
        if (optind == 0)
70
 
                next = NULL;
71
 
 
72
 
        optarg = NULL;
73
 
 
74
 
        if (next == NULL || *next == '\0')
75
 
        {
76
 
                if (optind == 0)
77
 
                        optind++;
78
 
 
79
 
                if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
80
 
                {
81
 
                        optarg = NULL;
82
 
                        if (optind < argc)
83
 
                                optarg = argv[optind];
84
 
                        return -1;
85
 
                }
86
 
 
87
 
                if (strcmp(argv[optind], "--") == 0)
88
 
                {
89
 
                        optind++;
90
 
                        optarg = NULL;
91
 
                        if (optind < argc)
92
 
                                optarg = argv[optind];
93
 
                        return -1;
94
 
                }
95
 
 
96
 
                next = argv[optind];
97
 
                next++;         // skip past -
98
 
                optind++;
99
 
        }
100
 
 
101
 
        char c = *next++;
102
 
        char *cp = strchr(optstring, c);
103
 
 
104
 
        if (cp == NULL || c == ':')
105
 
        {
106
 
                fprintf(stderr, "Invalid option %c", c);
107
 
                return '?';
108
 
        }
109
 
 
110
 
        cp++;
111
 
        if (*cp == ':')
112
 
        {
113
 
                if (*next != '\0')
114
 
                {
115
 
                        optarg = next;
116
 
                        next = NULL;
117
 
                }
118
 
                else if (optind < argc)
119
 
                {
120
 
                        optarg = argv[optind];
121
 
                        optind++;
122
 
                }
123
 
                else
124
 
                {
125
 
                        fprintf(stderr, "Option %c needs an argument", c);
126
 
                        return '?';
127
 
                }
128
 
        }
129
 
 
130
 
        return c;
131
 
}
132
 
 
133
 
DirBrowser::DirBrowser(const char* szPath)
134
 
{
135
 
        char szMask[MAX_PATH + 1];
136
 
        int len = strlen(szPath);
137
 
        if (szPath[len] == '\\' || szPath[len] == '/')
138
 
        {
139
 
                snprintf(szMask, MAX_PATH + 1, "%s*.*", szPath);
140
 
        }
141
 
        else
142
 
        {
143
 
                snprintf(szMask, MAX_PATH + 1, "%s%c*.*", szPath, (int)PATH_SEPARATOR);
144
 
        }
145
 
        szMask[MAX_PATH] = '\0';
146
 
        m_hFile = _findfirst(szMask, &m_FindData);
147
 
        m_bFirst = true;
148
 
}
149
 
 
150
 
DirBrowser::~DirBrowser()
151
 
{
152
 
        if (m_hFile != -1L)
153
 
        {
154
 
                _findclose(m_hFile);
155
 
        }
156
 
}
157
 
 
158
 
const char* DirBrowser::Next()
159
 
{
160
 
        bool bOK = false;
161
 
        if (m_bFirst)
162
 
        {
163
 
                bOK = m_hFile != -1L;
164
 
                m_bFirst = false;
165
 
        }
166
 
        else
167
 
        {
168
 
                bOK = _findnext(m_hFile, &m_FindData) == 0;
169
 
        }
170
 
        if (bOK)
171
 
        {
172
 
                return m_FindData.name;
173
 
        }
174
 
        return NULL;
175
 
}
176
 
 
177
 
#else
178
 
 
179
 
DirBrowser::DirBrowser(const char* szPath)
180
 
{
181
 
        m_pDir = opendir(szPath);
182
 
}
183
 
 
184
 
DirBrowser::~DirBrowser()
185
 
{
186
 
        if (m_pDir)
187
 
        {
188
 
                closedir(m_pDir);
189
 
        }
190
 
}
191
 
 
192
 
const char* DirBrowser::Next()
193
 
{
194
 
        if (m_pDir)
195
 
        {
196
 
                m_pFindData = readdir(m_pDir);
197
 
                if (m_pFindData)
198
 
                {
199
 
                        return m_pFindData->d_name;
200
 
                }
201
 
        }
202
 
        return NULL;
203
 
}
204
 
 
205
 
#endif
206
 
 
207
 
char Util::VersionRevisionBuf[40];
208
 
 
209
 
char* Util::BaseFileName(const char* filename)
210
 
{
211
 
        char* p = (char*)strrchr(filename, PATH_SEPARATOR);
212
 
        char* p1 = (char*)strrchr(filename, ALT_PATH_SEPARATOR);
213
 
        if (p1)
214
 
        {
215
 
                if ((p && p < p1) || !p)
216
 
                {
217
 
                        p = p1;
218
 
                }
219
 
        }
220
 
        if (p)
221
 
        {
222
 
                return p + 1;
223
 
        }
224
 
        else
225
 
        {
226
 
                return (char*)filename;
227
 
        }
228
 
}
229
 
 
230
 
void Util::NormalizePathSeparators(char* szPath)
231
 
{
232
 
        for (char* p = szPath; *p; p++) 
233
 
        {
234
 
                if (*p == ALT_PATH_SEPARATOR) 
235
 
                {
236
 
                        *p = PATH_SEPARATOR;
237
 
                }
238
 
        }
239
 
}
240
 
 
241
 
bool Util::ForceDirectories(const char* szPath)
242
 
{
243
 
        char* szNormPath = strdup(szPath);
244
 
        NormalizePathSeparators(szNormPath);
245
 
        int iLen = strlen(szNormPath);
246
 
        if ((iLen > 0) && szNormPath[iLen-1] == PATH_SEPARATOR
247
 
#ifdef WIN32
248
 
                && iLen > 3
249
 
#endif
250
 
                )
251
 
        {
252
 
                szNormPath[iLen-1] = '\0';
253
 
        }
254
 
 
255
 
        struct stat buffer;
256
 
        bool bOK = !stat(szNormPath, &buffer) && S_ISDIR(buffer.st_mode);
257
 
        if (!bOK
258
 
#ifdef WIN32
259
 
                && strlen(szNormPath) > 2
260
 
#endif
261
 
                )
262
 
        {
263
 
                char* szParentPath = strdup(szNormPath);
264
 
                bOK = true;
265
 
                char* p = (char*)strrchr(szParentPath, PATH_SEPARATOR);
266
 
                if (p)
267
 
                {
268
 
#ifdef WIN32
269
 
                        if (p - szParentPath == 2 && szParentPath[1] == ':' && strlen(szParentPath) > 2)
270
 
                        {
271
 
                                szParentPath[3] = '\0';
272
 
                        }
273
 
                        else
274
 
#endif
275
 
                        {
276
 
                                *p = '\0';
277
 
                        }
278
 
                        if (strlen(szParentPath) != strlen(szPath))
279
 
                        {
280
 
                                bOK = ForceDirectories(szParentPath);
281
 
                        }
282
 
                }
283
 
                if (bOK)
284
 
                {
285
 
                        mkdir(szNormPath, S_DIRMODE);
286
 
                        bOK = !stat(szNormPath, &buffer) && S_ISDIR(buffer.st_mode);
287
 
                }
288
 
                free(szParentPath);
289
 
        }
290
 
        free(szNormPath);
291
 
        return bOK;
292
 
}
293
 
 
294
 
bool Util::DirEmpty(const char* szDirFilename)
295
 
{
296
 
        DirBrowser dir(szDirFilename);
297
 
        while (const char* filename = dir.Next())
298
 
        {
299
 
                if (strcmp(filename, ".") && strcmp(filename, ".."))
300
 
                {
301
 
                        return false;
302
 
                }
303
 
        }
304
 
        return true;
305
 
}
306
 
 
307
 
bool Util::LoadFileIntoBuffer(const char* szFileName, char** pBuffer, int* pBufferLength)
308
 
{
309
 
    FILE* pFile = fopen(szFileName, "rb");
310
 
    if (!pFile)
311
 
    {
312
 
        return false;
313
 
    }
314
 
 
315
 
    // obtain file size.
316
 
    fseek(pFile , 0 , SEEK_END);
317
 
    int iSize  = ftell(pFile);
318
 
    rewind(pFile);
319
 
 
320
 
    // allocate memory to contain the whole file.
321
 
    *pBuffer = (char*) malloc(iSize + 1);
322
 
    if (!*pBuffer)
323
 
    {
324
 
        return false;
325
 
    }
326
 
 
327
 
    // copy the file into the buffer.
328
 
    fread(*pBuffer, 1, iSize, pFile);
329
 
 
330
 
    fclose(pFile);
331
 
 
332
 
    (*pBuffer)[iSize] = 0;
333
 
 
334
 
    *pBufferLength = iSize + 1;
335
 
 
336
 
    return true;
337
 
}
338
 
 
339
 
bool Util::SetFileSize(const char* szFilename, int iSize)
340
 
{
341
 
        bool bOK = false;
342
 
#ifdef WIN32
343
 
        FILE* pFile = fopen(szFilename, "ab");
344
 
        if (pFile)
345
 
        {
346
 
                bOK = _chsize_s(pFile->_file, iSize) == 0;
347
 
                fclose(pFile);
348
 
        }
349
 
#else
350
 
        // create file
351
 
        FILE* pFile = fopen(szFilename, "ab");
352
 
        if (pFile)
353
 
        {
354
 
                fclose(pFile);
355
 
        }
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");
362
 
        if (pFile)
363
 
        {
364
 
                fseek(pFile, 0, SEEK_END);
365
 
                bOK = ftell(pFile) == iSize;
366
 
                if (!bOK)
367
 
                {
368
 
                        // 2) truncate did not work, expanding the file by writing in it (it is slow)
369
 
                        fclose(pFile);
370
 
                        truncate(szFilename, 0);
371
 
                        pFile = fopen(szFilename, "ab");
372
 
                        char c = '0';
373
 
                        fwrite(&c, 1, iSize, pFile);
374
 
                        bOK = ftell(pFile) == iSize;
375
 
                }
376
 
                fclose(pFile);
377
 
        }
378
 
#endif
379
 
        return bOK;
380
 
}
381
 
 
382
 
//replace bad chars in filename
383
 
void Util::MakeValidFilename(char* szFilename, char cReplaceChar, bool bAllowSlashes)
384
 
{
385
 
        const char* szReplaceChars = bAllowSlashes ? ":*?\"><'\n\r\t" : "\\/:*?\"><'\n\r\t";
386
 
        char* p = szFilename;
387
 
        while (*p)
388
 
        {
389
 
                if (strchr(szReplaceChars, *p))
390
 
                {
391
 
                        *p = cReplaceChar;
392
 
                }
393
 
                if (bAllowSlashes && *p == ALT_PATH_SEPARATOR)
394
 
                {
395
 
                        *p = PATH_SEPARATOR;
396
 
                }
397
 
                p++;
398
 
        }
399
 
 
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--) 
403
 
        {
404
 
                szFilename[iLen - 1] = '\0';
405
 
        }
406
 
}
407
 
 
408
 
long long Util::JoinInt64(unsigned long Hi, unsigned long Lo)
409
 
{
410
 
        return (((long long)Hi) << 32) + Lo;
411
 
}
412
 
 
413
 
void Util::SplitInt64(long long Int64, unsigned long* Hi, unsigned long* Lo)
414
 
{
415
 
        *Hi = (unsigned long)(Int64 >> 32);
416
 
        *Lo = (unsigned long)Int64;
417
 
}
418
 
 
419
 
float Util::Int64ToFloat(long long Int64)
420
 
{
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;
424
 
}
425
 
 
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/
429
 
 */
430
 
 
431
 
const static char BASE64_DEALPHABET [128] = 
432
 
        {
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
446
 
        };
447
 
 
448
 
unsigned int DecodeByteQuartet(char* szInputBuffer, char* szOutputBuffer)
449
 
{
450
 
        unsigned int buffer = 0;
451
 
 
452
 
        if (szInputBuffer[3] == '=')
453
 
        {
454
 
                if (szInputBuffer[2] == '=')
455
 
                {
456
 
                        buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[0]]) << 6;
457
 
                        buffer = (buffer | BASE64_DEALPHABET [(int)szInputBuffer[1]]) << 6;
458
 
                        buffer = buffer << 14;
459
 
 
460
 
                        szOutputBuffer [0] = (char)(buffer >> 24);
461
 
                        
462
 
                        return 1;
463
 
                }
464
 
                else
465
 
                {
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;
470
 
 
471
 
                        szOutputBuffer [0] = (char)(buffer >> 24);
472
 
                        szOutputBuffer [1] = (char)(buffer >> 16);
473
 
                        
474
 
                        return 2;
475
 
                }
476
 
        }
477
 
        else
478
 
        {
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;
484
 
 
485
 
                szOutputBuffer [0] = (char)(buffer >> 24);
486
 
                szOutputBuffer [1] = (char)(buffer >> 16);
487
 
                szOutputBuffer [2] = (char)(buffer >> 8);
488
 
 
489
 
                return 3;
490
 
        }
491
 
 
492
 
        return 0;
493
 
}
494
 
 
495
 
unsigned int Util::DecodeBase64(char* szInputBuffer, int iInputBufferLength, char* szOutputBuffer)
496
 
{
497
 
        unsigned int InputBufferIndex  = 0;
498
 
        unsigned int OutputBufferIndex = 0;
499
 
        unsigned int InputBufferLength = iInputBufferLength > 0 ? iInputBufferLength : strlen(szInputBuffer);
500
 
 
501
 
        char ByteQuartet [4];
502
 
        int i = 0;
503
 
        while (InputBufferIndex < InputBufferLength)
504
 
        {
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] == '=')
512
 
                {
513
 
                        ByteQuartet [i] = szInputBuffer [InputBufferIndex];
514
 
                        i++;
515
 
                }
516
 
                
517
 
                InputBufferIndex++;
518
 
                
519
 
                if (i == 4) {
520
 
                        OutputBufferIndex += DecodeByteQuartet(ByteQuartet, szOutputBuffer + OutputBufferIndex);
521
 
                        i = 0;
522
 
                }
523
 
        }
524
 
 
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
527
 
        // in our buffer.
528
 
        return OutputBufferIndex;
529
 
}
530
 
 
531
 
/* END - Base64
532
 
*/
533
 
 
534
 
char* Util::XmlEncode(const char* raw)
535
 
{
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++)
539
 
        {
540
 
                unsigned char ch = *p;
541
 
                switch (ch)
542
 
                {
543
 
                        case '>':
544
 
                        case '<':
545
 
                                iReqSize += 4;
546
 
                                break;
547
 
                        case '&':
548
 
                                iReqSize += 5;
549
 
                                break;
550
 
                        case '\'':
551
 
                        case '\"':
552
 
                                iReqSize += 6;
553
 
                                break;
554
 
                        default:
555
 
                                if (ch >= 0x80)
556
 
                                {
557
 
                                        iReqSize += 6;
558
 
                                        break;
559
 
                                }
560
 
                }
561
 
        }
562
 
 
563
 
        char* result = (char*)malloc(iReqSize + 1);
564
 
 
565
 
        // copy string
566
 
        char* output = result;
567
 
        for (const char* p = raw; ; p++)
568
 
        {
569
 
                unsigned char ch = *p;
570
 
                switch (ch)
571
 
                {
572
 
                        case '\0':
573
 
                                goto BreakLoop;
574
 
                        case '<':
575
 
                                strcpy(output, "&lt;");
576
 
                                output += 4;
577
 
                                break;
578
 
                        case '>':
579
 
                                strcpy(output, "&gt;");
580
 
                                output += 4;
581
 
                                break;
582
 
                        case '&':
583
 
                                strcpy(output, "&amp;");
584
 
                                output += 5;
585
 
                                break;
586
 
                        case '\'':
587
 
                                strcpy(output, "&apos;");
588
 
                                output += 6;
589
 
                                break;
590
 
                        case '\"':
591
 
                                strcpy(output, "&quot;");
592
 
                                output += 6;
593
 
                                break;
594
 
                        default:
595
 
                                if (ch >= 0x80)
596
 
                                {
597
 
                                        sprintf(output, "&#%i;", ch);
598
 
                                        output += 6;
599
 
                                }
600
 
                                else
601
 
                                {
602
 
                                        *output++ = ch;
603
 
                                }
604
 
                                break;
605
 
                }
606
 
        }
607
 
BreakLoop:
608
 
 
609
 
        *output = '\0';
610
 
 
611
 
        return result;
612
 
}
613
 
 
614
 
void Util::XmlDecode(char* raw)
615
 
{
616
 
        char* output = raw;
617
 
        for (char* p = raw;;)
618
 
        {
619
 
                switch (*p)
620
 
                {
621
 
                        case '\0':
622
 
                                goto BreakLoop;
623
 
                        case '&':
624
 
                                {
625
 
                                        p++;
626
 
                                        if (!strncmp(p, "lt;", 3))
627
 
                                        {
628
 
                                                *output++ = '<';
629
 
                                                p += 3;
630
 
                                        }
631
 
                                        else if (!strncmp(p, "gt;", 3))
632
 
                                        {
633
 
                                                *output++ = '>';
634
 
                                                p += 3;
635
 
                                        }
636
 
                                        else if (!strncmp(p, "amp;", 4))
637
 
                                        {
638
 
                                                *output++ = '&';
639
 
                                                p += 4;
640
 
                                        }
641
 
                                        else if (!strncmp(p, "apos;", 5))
642
 
                                        {
643
 
                                                *output++ = '\'';
644
 
                                                p += 5;
645
 
                                        }
646
 
                                        else if (!strncmp(p, "quot;", 5))
647
 
                                        {
648
 
                                                *output++ = '\"';
649
 
                                                p += 5;
650
 
                                        }
651
 
                                        else
652
 
                                        {
653
 
                                                // unknown entity
654
 
                                                *output++ = *(p-1);
655
 
                                                p++;
656
 
                                        }
657
 
                                        break;
658
 
                                }
659
 
                        default:
660
 
                                *output++ = *p++;
661
 
                                break;
662
 
                }
663
 
        }
664
 
BreakLoop:
665
 
 
666
 
        *output = '\0';
667
 
}
668
 
 
669
 
const char* Util::XmlFindTag(const char* szXml, const char* szTag, int* pValueLength)
670
 
{
671
 
        char szOpenTag[100];
672
 
        snprintf(szOpenTag, 100, "<%s>", szTag);
673
 
        szOpenTag[100-1] = '\0';
674
 
 
675
 
        char szCloseTag[100];
676
 
        snprintf(szCloseTag, 100, "</%s>", szTag);
677
 
        szCloseTag[100-1] = '\0';
678
 
 
679
 
        char szOpenCloseTag[100];
680
 
        snprintf(szOpenCloseTag, 100, "<%s/>", szTag);
681
 
        szOpenCloseTag[100-1] = '\0';
682
 
 
683
 
        const char* pstart = strstr(szXml, szOpenTag);
684
 
        const char* pstartend = strstr(szXml, szOpenCloseTag);
685
 
        if (!pstart && !pstartend) return NULL;
686
 
 
687
 
        if (pstartend && (!pstart || pstartend < pstart))
688
 
        {
689
 
                *pValueLength = 0;
690
 
                return pstartend;
691
 
        }
692
 
 
693
 
        const char* pend = strstr(pstart, szCloseTag);
694
 
        if (!pend) return NULL;
695
 
 
696
 
        int iTagLen = strlen(szOpenTag);
697
 
        *pValueLength = (int)(pend - pstart - iTagLen);
698
 
 
699
 
        return pstart + iTagLen;
700
 
}
701
 
 
702
 
bool Util::XmlParseTagValue(const char* szXml, const char* szTag, char* szValueBuf, int iValueBufSize, const char** pTagEnd)
703
 
{
704
 
        int iValueLen = 0;
705
 
        const char* szValue = XmlFindTag(szXml, szTag, &iValueLen);
706
 
        if (!szValue)
707
 
        {
708
 
                return false;
709
 
        }
710
 
        int iLen = iValueLen < iValueBufSize ? iValueLen : iValueBufSize - 1;
711
 
        strncpy(szValueBuf, szValue, iLen);
712
 
        szValueBuf[iLen] = '\0';
713
 
        if (pTagEnd)
714
 
        {
715
 
                *pTagEnd = szValue + iValueLen;
716
 
        }
717
 
        return true;
718
 
}
719
 
 
720
 
bool Util::MoveFile(const char* szSrcFilename, const char* szDstFilename)
721
 
{
722
 
        bool bOK = rename(szSrcFilename, szDstFilename) == 0;
723
 
 
724
 
#ifndef WIN32
725
 
        if (!bOK && (errno == EXDEV))
726
 
        {
727
 
                FILE* infile = fopen(szSrcFilename, "rb");
728
 
                if (!infile)
729
 
                {
730
 
                        return false;
731
 
                }
732
 
 
733
 
                FILE* outfile = fopen(szDstFilename, "wb+");
734
 
                if (!outfile)
735
 
                {
736
 
                        fclose(infile);
737
 
                        return false;
738
 
                }
739
 
 
740
 
                static const int BUFFER_SIZE = 1024 * 50;
741
 
                char* buffer = (char*)malloc(BUFFER_SIZE);
742
 
 
743
 
                int cnt = BUFFER_SIZE;
744
 
                while (cnt == BUFFER_SIZE)
745
 
                {
746
 
                        cnt = (int)fread(buffer, 1, BUFFER_SIZE, infile);
747
 
                        fwrite(buffer, 1, cnt, outfile);
748
 
                }
749
 
 
750
 
                fclose(infile);
751
 
                fclose(outfile);
752
 
                free(buffer);
753
 
 
754
 
                bOK = remove(szSrcFilename) == 0;
755
 
        }
756
 
#endif
757
 
 
758
 
        return bOK;
759
 
}
760
 
 
761
 
bool Util::FileExists(const char* szFilename)
762
 
{
763
 
        struct stat buffer;
764
 
        bool bExists = !stat(szFilename, &buffer) && S_ISREG(buffer.st_mode);
765
 
        return bExists;
766
 
}
767
 
 
768
 
bool Util::DirectoryExists(const char* szDirFilename)
769
 
{
770
 
        struct stat buffer;
771
 
        bool bExists = !stat(szDirFilename, &buffer) && S_ISDIR(buffer.st_mode);
772
 
        return bExists;
773
 
}
774
 
 
775
 
bool Util::CreateDirectory(const char* szDirFilename)
776
 
{
777
 
        mkdir(szDirFilename, S_DIRMODE);
778
 
        return DirectoryExists(szDirFilename);
779
 
}
780
 
 
781
 
long long Util::FileSize(const char* szFilename)
782
 
{
783
 
#ifdef WIN32
784
 
        struct _stat32i64 buffer;
785
 
        _stat32i64(szFilename, &buffer);
786
 
#else
787
 
#ifdef HAVE_STAT64
788
 
        struct stat64 buffer;
789
 
        stat64(szFilename, &buffer);
790
 
#else
791
 
        struct stat buffer;
792
 
        stat(szFilename, &buffer);
793
 
#endif
794
 
#endif
795
 
        return buffer.st_size;
796
 
}
797
 
 
798
 
char* Util::JsonEncode(const char* raw)
799
 
{
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++)
803
 
        {
804
 
                unsigned char ch = *p;
805
 
                switch (ch)
806
 
                {
807
 
                        case '\"':
808
 
                        case '\\':
809
 
                        case '/':
810
 
                        case '\b':
811
 
                        case '\f':
812
 
                        case '\n':
813
 
                        case '\r':
814
 
                        case '\t':
815
 
                                iReqSize += 1;
816
 
                        default:
817
 
                                if (ch >= 0x80)
818
 
                                {
819
 
                                        iReqSize += 6;
820
 
                                        break;
821
 
                                }
822
 
                }
823
 
        }
824
 
 
825
 
        char* result = (char*)malloc(iReqSize + 1);
826
 
 
827
 
        // copy string
828
 
        char* output = result;
829
 
        for (const char* p = raw; ; p++)
830
 
        {
831
 
                unsigned char ch = *p;
832
 
                switch (ch)
833
 
                {
834
 
                        case '\0':
835
 
                                goto BreakLoop;
836
 
                        case '"':
837
 
                                strcpy(output, "\\\"");
838
 
                                output += 2;
839
 
                                break;
840
 
                        case '\\':
841
 
                                strcpy(output, "\\\\");
842
 
                                output += 2;
843
 
                                break;
844
 
                        case '/':
845
 
                                strcpy(output, "\\/");
846
 
                                output += 2;
847
 
                                break;
848
 
                        case '\b':
849
 
                                strcpy(output, "\\b");
850
 
                                output += 2;
851
 
                                break;
852
 
                        case '\f':
853
 
                                strcpy(output, "\\f");
854
 
                                output += 2;
855
 
                                break;
856
 
                        case '\n':
857
 
                                strcpy(output, "\\n");
858
 
                                output += 2;
859
 
                                break;
860
 
                        case '\r':
861
 
                                strcpy(output, "\\r");
862
 
                                output += 2;
863
 
                                break;
864
 
                        case '\t':
865
 
                                strcpy(output, "\\t");
866
 
                                output += 2;
867
 
                                break;
868
 
                        default:
869
 
                                if (ch >= 0x80)
870
 
                                {
871
 
                                        sprintf(output, "\\u%04x", ch);
872
 
                                        output += 6;
873
 
                                }
874
 
                                else
875
 
                                {
876
 
                                        *output++ = ch;
877
 
                                }
878
 
                                break;
879
 
                }
880
 
        }
881
 
BreakLoop:
882
 
 
883
 
        *output = '\0';
884
 
 
885
 
        return result;
886
 
}
887
 
 
888
 
void Util::JsonDecode(char* raw)
889
 
{
890
 
        char* output = raw;
891
 
        for (char* p = raw;;)
892
 
        {
893
 
                switch (*p)
894
 
                {
895
 
                        case '\0':
896
 
                                goto BreakLoop;
897
 
                        case '\\':
898
 
                                {
899
 
                                        p++;
900
 
                                        switch (*p)
901
 
                                        {
902
 
                                                case '"':
903
 
                                                        *output++ = '"';
904
 
                                                        break;
905
 
                                                case '\\':
906
 
                                                        *output++ = '\\';
907
 
                                                        break;
908
 
                                                case '/':
909
 
                                                        *output++ = '/';
910
 
                                                        break;
911
 
                                                case 'b':
912
 
                                                        *output++ = '\b';
913
 
                                                        break;
914
 
                                                case 'f':
915
 
                                                        *output++ = '\f';
916
 
                                                        break;
917
 
                                                case 'n':
918
 
                                                        *output++ = '\n';
919
 
                                                        break;
920
 
                                                case 'r':
921
 
                                                        *output++ = '\r';
922
 
                                                        break;
923
 
                                                case 't':
924
 
                                                        *output++ = '\t';
925
 
                                                        break;
926
 
                                                case 'u':
927
 
                                                        *output++ = (char)strtol(p + 1, NULL, 16);
928
 
                                                        p += 4;
929
 
                                                        break;
930
 
                                                default:
931
 
                                                        // unknown escape-sequence, should never occur
932
 
                                                        *output++ = *p;
933
 
                                                        break;
934
 
                                        }
935
 
                                        p++;
936
 
                                }
937
 
                        default:
938
 
                                *output++ = *p++;
939
 
                                break;
940
 
                }
941
 
        }
942
 
BreakLoop:
943
 
 
944
 
        *output = '\0';
945
 
}
946
 
 
947
 
const char* Util::JsonFindField(const char* szJsonText, const char* szFieldName, int* pValueLength)
948
 
{
949
 
        char szOpenTag[100];
950
 
        snprintf(szOpenTag, 100, "\"%s\"", szFieldName);
951
 
        szOpenTag[100-1] = '\0';
952
 
 
953
 
        const char* pstart = strstr(szJsonText, szOpenTag);
954
 
        if (!pstart) return NULL;
955
 
 
956
 
        pstart += strlen(szOpenTag);
957
 
 
958
 
        return JsonNextValue(pstart, pValueLength);
959
 
}
960
 
 
961
 
const char* Util::JsonNextValue(const char* szJsonText, int* pValueLength)
962
 
{
963
 
        const char* pstart = szJsonText;
964
 
 
965
 
        while (*pstart && strchr(" ,[{:\r\n\t\f", *pstart)) pstart++;
966
 
        if (!*pstart) return NULL;
967
 
 
968
 
        const char* pend = pstart;
969
 
 
970
 
        char ch = *pend;
971
 
        bool bStr = ch == '"';
972
 
        if (bStr)
973
 
        {
974
 
                ch = *++pend;
975
 
        }
976
 
        while (ch)
977
 
        {
978
 
                if (ch == '\\')
979
 
                {
980
 
                        if (!*++pend || !*++pend) return NULL;
981
 
                        ch = *pend;
982
 
                }
983
 
                if (bStr && ch == '"')
984
 
                {
985
 
                        pend++;
986
 
                        break;
987
 
                }
988
 
                else if (!bStr && strchr(" ,]}\r\n\t\f", ch))
989
 
                {
990
 
                        break;
991
 
                }
992
 
                ch = *++pend;
993
 
        }
994
 
 
995
 
        *pValueLength = (int)(pend - pstart);
996
 
        return pstart;
997
 
}
998
 
 
999
 
long long Util::FreeDiskSize(const char* szPath)
1000
 
{
1001
 
#ifdef WIN32
1002
 
        ULARGE_INTEGER lFree, lDummy;
1003
 
        if (GetDiskFreeSpaceEx(szPath, &lFree, &lDummy, &lDummy))
1004
 
        {
1005
 
                return lFree.QuadPart;
1006
 
        }
1007
 
#else
1008
 
        struct statvfs diskdata;
1009
 
        if (!statvfs(szPath, &diskdata)) 
1010
 
        {
1011
 
                return (long long)diskdata.f_bsize * (long long)diskdata.f_bavail;
1012
 
        }
1013
 
#endif
1014
 
        return -1;
1015
 
}
1016
 
 
1017
 
bool Util::RenameBak(const char* szFilename, const char* szBakPart, bool bRemoveOldExtension, char* szNewNameBuf, int iNewNameBufSize)
1018
 
{
1019
 
        char szChangedFilename[1024];
1020
 
 
1021
 
        if (bRemoveOldExtension)
1022
 
        {
1023
 
                strncpy(szChangedFilename, szFilename, 1024);
1024
 
                szChangedFilename[1024-1] = '\0';
1025
 
                char* szExtension = strrchr(szChangedFilename, '.');
1026
 
                if (szExtension)
1027
 
                {
1028
 
                        *szExtension = '\0';
1029
 
                }
1030
 
        }
1031
 
 
1032
 
        char bakname[1024];
1033
 
        snprintf(bakname, 1024, "%s.%s", bRemoveOldExtension ? szChangedFilename : szFilename, szBakPart);
1034
 
        bakname[1024-1] = '\0';
1035
 
 
1036
 
        int i = 2;
1037
 
        struct stat buffer;
1038
 
        while (!stat(bakname, &buffer))
1039
 
        {
1040
 
                snprintf(bakname, 1024, "%s.%i.%s", bRemoveOldExtension ? szChangedFilename : szFilename, i++, szBakPart);
1041
 
                bakname[1024-1] = '\0';
1042
 
        }
1043
 
 
1044
 
        if (szNewNameBuf)
1045
 
        {
1046
 
                strncpy(szNewNameBuf, bakname, iNewNameBufSize);
1047
 
        }
1048
 
 
1049
 
        bool bOK = !rename(szFilename, bakname);
1050
 
        return bOK;
1051
 
}
1052
 
 
1053
 
#ifndef WIN32
1054
 
bool Util::ExpandHomePath(const char* szFilename, char* szBuffer, int iBufSize)
1055
 
{
1056
 
        if (szFilename && (szFilename[0] == '~') && (szFilename[1] == '/'))
1057
 
        {
1058
 
                // expand home-dir
1059
 
 
1060
 
                char* home = getenv("HOME");
1061
 
                if (!home)
1062
 
                {
1063
 
                        struct passwd *pw = getpwuid(getuid());
1064
 
                        if (pw)
1065
 
                        {
1066
 
                                home = pw->pw_dir;
1067
 
                        }
1068
 
                }
1069
 
 
1070
 
                if (!home)
1071
 
                {
1072
 
                        return false;
1073
 
                }
1074
 
 
1075
 
                if (home[strlen(home)-1] == '/')
1076
 
                {
1077
 
                        snprintf(szBuffer, iBufSize, "%s%s", home, szFilename + 2);
1078
 
                }
1079
 
                else
1080
 
                {
1081
 
                        snprintf(szBuffer, iBufSize, "%s/%s", home, szFilename + 2);
1082
 
                }
1083
 
                szBuffer[iBufSize - 1] = '\0';
1084
 
        }
1085
 
        else
1086
 
        {
1087
 
                strncpy(szBuffer, szFilename, iBufSize);
1088
 
                szBuffer[iBufSize - 1] = '\0';
1089
 
        }
1090
 
        
1091
 
        return true;
1092
 
}
1093
 
 
1094
 
void Util::ExpandFileName(const char* szFilename, char* szBuffer, int iBufSize)
1095
 
{
1096
 
        if (szFilename[0] != '\0' && szFilename[0] != '/')
1097
 
        {
1098
 
                char szCurDir[MAX_PATH + 1];
1099
 
                getcwd(szCurDir, sizeof(szCurDir) - 1); // 1 char reserved for adding backslash
1100
 
                int iOffset = 0;
1101
 
                if (szFilename[0] == '.' && szFilename[1] == '/')
1102
 
                {
1103
 
                        iOffset += 2;
1104
 
                }
1105
 
                snprintf(szBuffer, iBufSize, "%s/%s", szCurDir, szFilename + iOffset);
1106
 
        }
1107
 
        else
1108
 
        {
1109
 
                strncpy(szBuffer, szFilename, iBufSize);
1110
 
                szBuffer[iBufSize - 1] = '\0';
1111
 
        }
1112
 
}
1113
 
#endif
1114
 
 
1115
 
void Util::FormatFileSize(char * szBuffer, int iBufLen, long long lFileSize)
1116
 
{
1117
 
        if (lFileSize > 1024 * 1024 * 1000)
1118
 
        {
1119
 
                snprintf(szBuffer, iBufLen, "%.2f GB", (float)(Util::Int64ToFloat(lFileSize) / 1024 / 1024 / 1024));
1120
 
        }
1121
 
        else if (lFileSize > 1024 * 1000)
1122
 
        {
1123
 
                snprintf(szBuffer, iBufLen, "%.2f MB", (float)(Util::Int64ToFloat(lFileSize) / 1024 / 1024));
1124
 
        }
1125
 
        else if (lFileSize > 1000)
1126
 
        {
1127
 
                snprintf(szBuffer, iBufLen, "%.2f KB", (float)(Util::Int64ToFloat(lFileSize) / 1024));
1128
 
        }
1129
 
        else 
1130
 
        {
1131
 
                snprintf(szBuffer, iBufLen, "%i B", (int)lFileSize);
1132
 
        }
1133
 
        szBuffer[iBufLen - 1] = '\0';
1134
 
}
1135
 
 
1136
 
void Util::InitVersionRevision()
1137
 
{
1138
 
#ifndef WIN32
1139
 
        if ((strlen(svn_version()) > 0) && strstr(VERSION, "testing"))
1140
 
        {
1141
 
                snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s-r%s", VERSION, svn_version());
1142
 
        }
1143
 
        else
1144
 
#endif
1145
 
        {
1146
 
                snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s", VERSION);
1147
 
        }
1148
 
}
1149
 
 
1150
 
bool Util::SplitCommandLine(const char* szCommandLine, char*** argv)
1151
 
{
1152
 
        int iArgCount = 0;
1153
 
        char szBuf[1024];
1154
 
        char* pszArgList[100];
1155
 
        unsigned int iLen = 0;
1156
 
        bool bEscaping = false;
1157
 
        bool bSpace = true;
1158
 
        for (const char* p = szCommandLine; ; p++)
1159
 
        {
1160
 
                if (*p)
1161
 
                {
1162
 
                        const char c = *p;
1163
 
                        if (bEscaping)
1164
 
                        {
1165
 
                                if (c == '\'')
1166
 
                                {
1167
 
                                        if (p[1] == '\'' && iLen < sizeof(szBuf) - 1)
1168
 
                                        {
1169
 
                                                szBuf[iLen++] = c;
1170
 
                                                p++;
1171
 
                                        }
1172
 
                                        else
1173
 
                                        {
1174
 
                                                bEscaping = false;
1175
 
                                                bSpace = true;
1176
 
                                        }
1177
 
                                }
1178
 
                                else if (iLen < sizeof(szBuf) - 1)
1179
 
                                {
1180
 
                                        szBuf[iLen++] = c;
1181
 
                                }
1182
 
                        }
1183
 
                        else
1184
 
                        {
1185
 
                                if (c == ' ')
1186
 
                                {
1187
 
                                        bSpace = true;
1188
 
                                }
1189
 
                                else if (c == '\'' && bSpace)
1190
 
                                {
1191
 
                                        bEscaping = true;
1192
 
                                        bSpace = false;
1193
 
                                }
1194
 
                                else if (iLen < sizeof(szBuf) - 1)
1195
 
                                {
1196
 
                                        szBuf[iLen++] = c;
1197
 
                                        bSpace = false;
1198
 
                                }
1199
 
                        }
1200
 
                }
1201
 
 
1202
 
                if ((bSpace || !*p) && iLen > 0 && iArgCount < 100)
1203
 
                {
1204
 
                        //add token
1205
 
                        szBuf[iLen] = '\0';
1206
 
                        if (argv)
1207
 
                        {
1208
 
                                pszArgList[iArgCount] = strdup(szBuf);
1209
 
                        }
1210
 
                        (iArgCount)++;
1211
 
                        iLen = 0;
1212
 
                }
1213
 
 
1214
 
                if (!*p)
1215
 
                {
1216
 
                        break;
1217
 
                }
1218
 
        }
1219
 
 
1220
 
        if (argv)
1221
 
        {
1222
 
                pszArgList[iArgCount] = NULL;
1223
 
                *argv = (char**)malloc((iArgCount + 1) * sizeof(char*));
1224
 
                memcpy(*argv, pszArgList, sizeof(char*) * (iArgCount + 1));
1225
 
        }
1226
 
 
1227
 
        return iArgCount > 0;
1228
 
}